Can I write bytes directly to video memory under Linux, or is there a better way to get data onto the screen?
Asked Answered
B

1

19

I'm teaching my son programming "the right/hard way", so we're starting with C, like real men :)

Printing text to the console is fun, but I still remember the exhilaration of switching my old i386 to the 320x200x256 mode and drawing a few colored rectangles... back in the day it was as easy as invoking int 10h and then you just write bytes to [A000:0000] to draw pixels on the screen.

With modern Linux, however, such low-level access to the hardware seems to be more complicated (for obvious reasons). I've briefly looked at mmap-ing /dev/fd0 as described here - turns out there's no /dev/fb0 device on my Ubuntu 13.04. I also looked at using svgalib - however, their example code displays nothing on my monitor.

So the question is: are there easy ways to obtain direct access to video memory in modern Linux which do not require much system configuration and boilerplate code? It does not have to be a fullscreen access - opening an X window and drawing pixels there would be fine too, as long as it's done via writing data directly to memory.

Begat answered 15/7, 2013 at 0:37 Comment(5)
I recommend learning how to use SDL and then letting the system best decide how to handle slapping it onto the screen.Peroxy
@IgnacioVazquez-Abrams: Wow, thanks for mentioning SDL. I was able to compile a sample SDL program and it looks like exactly what I was looking for. If you add your comment as an answer I promise to accept it in a couple of days provided no even awesomer framework is mentioned :)Begat
Real programmers code assembly :). Or better yet, real programmers wield butterflies, go get your son a butterfly net, start him off now that he's young!Abject
@terdon: I did consider assembly too :) Do you know how to draw pixels on the screen using assembly in Linux?Begat
Sergey, I don't know how to do anything at all in assembly and am pretty limited in C for that matter. I was referring to real programmers, never said I was one,I just couldn't resist the cheap unoriginal joke.Abject
P
14

Instead of accessing the screen memory directly (which is fraught with more pitfalls than one can believe), you should consider using Simple DirectMedia Layer for your graphics (and much more!) and letting the OS handle drawing it to the screen.

Peroxy answered 15/7, 2013 at 1:24 Comment(2)
Agreed. SDL is the way to go.Emmyemmye
For the basics it is important to understand what is video memory and how it works on the base level. SDL (and SDL2 in particular) adds abstraction layer that is too fat for the starter.Prytaneum

© 2022 - 2024 — McMap. All rights reserved.