I’m thinking about adding an OpenGL target to an SDL version of an
emulator I’m working on, and I’d like some tips on how to proceed.
Specifically, I’ll be using OpenGL to blit the final framebuffer only. I
won’t have access to the individual sprites or objects that make up the
display. Basically I get a 160x300 pixel buffer that has to be sent to
the screen (with the width doubled). There are two framebuffers
available, front and back.
The following is (roughly) how I do it in software mode:
Each update analyzes the differences between front and back buffers
(combination of FillRect and Update_Rects). Comparisons are not done
per-pixel, but per 4 pixel widths. This works since normally, not every
pixel changes (its a compromise between full dirty update and
When a word (4 pixel wide area) has changed, an SDL_Rect is created
with twice the horizontal width, and the contents of the 4 pixels are
copied into the SDL_Rect with each pixel copied twice (horizontal
doubling). This is going to change very soon, and the framebuffer will
be created at 320x300 internally, making the doubling unnecessary.
The framebuffer stores palette information (unsigned 8-bit values)
representing a color for that pixel. This is also going to change very
soon, as we plan to add 32-bit mode (so that each pixel in the
framebuffer is a 32-bit true-color value).
It is possible to ‘zoom’ the image by a user-defined amount. So (for
example), if ‘zoom’ is set to 3, then in addition to above horizontal
doubling, the width and height are tripled. The created SDL_Rect has a
size 3x width and height, and is filled accordingly.
What I’d like to know is how easy it will be to create an OpenGL target to
do the same thing. If possible, I’d like to see sample code on how to
set up SDL GL in 2D mode (something to do with orthographic mode?).
Also, when using OpenGL, would it be faster to simply create a texture
from the 320x300 framebuffer and send that directly to the video card
(and ignore the back framebuffer), or to stay with using two framebuffers
and update a texture somehow? And how would that be accomplished?
The emulator runs at 60 fps. What kind of performance hit can I expect by
creating a new texture of 320x300 pixels (and sending it to the card) 60
times a second?
I assume that I can get the ‘zoom’ for free with OpenGL. I wouldn’t have
to diddle with the pixels at all, just tell OpenGL to scale it. And
related to that, when I tell it to scale a full image, will it become
blurry? And is there a way to have a normal scale vs. a filtered scale?
Other than the ortho stuff, I’m not looking for any code at this point.
But I’d appreciate if someone could point me in the right direction
concerning what OpenGL functions (or methodology) should be used.