Strategies for Device Emulation

I am creating a emulator for a retro device where that device has simple memory mapped graphic (1, 2, or 4 bits per pixel).

I anticipate that I will need convert/map between the device window representation to SDL’s representation, periodically (30 or 60 times a second) pixel by pixel.

I’m new to SDL so would like to be steered in the right direction as to what mechanisms in SDL I should be using/focusing on.

I expect I need to manipulate the underlying SDL bitmaps (or what ever they are called in SDL) directly for efficiency reasons.

So where should I start looking?

For instance is there a mapping function that can do the work for me?

You should probably create a framebuffer where you manually draw things. I’d have a (threaded) function that copies from my custom framebuffer (1-4 bits) to the SDL framebuffer. You might want to use a pallet mode too, since it’s only 8 bits and you don’t have to do any conversions. In this case, you don’t need a threaded function.

Framebuffers. I’ll start looking up the doc.

Meanwhile with framebuffers I assume there are no high level functions to manipulate pixels/rectangle as there is with a surface.

I think you can do things like blit part of a surface and stuff like that but generally you should take care of your frame buffer by yourself, especially if you use nonstandard formats.

You can take a look at one of my programs where I use the framebuffer if you need some working examples:

Downloaded your example. Nice code. Compiled and run 1st time in VS code on Linux. I see you wrote your own menuing GUI.

You are manipulating a malloc’d array ‘screen_buffer’ to generate your output.

Is the screen_buffer linked to the rendering_texture via the following code ?

        SDL_UpdateTexture(rendering_texture, NULL, screen_buffer, pitch);
        SDL_RenderCopy(renderer, rendering_texture, NULL, NULL);

Yes, the idea is that in SDL 2 you have your screen buffer which you then put in a texture, and you render that texture on screen.

Take a look here:
Ctrl+f to “If your game just wants to get fully-rendered frames to the screen”

Thanks I think I’m getting the idea.

If I wanted to leave a portion of the window for a menu bar (top strip) and a status bar (bottom strip) does SDL have the concept of sub windows where I would render the texture into the appropriate sub (main) window?

Otherwise I must always add a constant to the offset when manipulating the memory buffer!

Yes, you can do that. SDL_RenderCopy lets you chose which part of the texture or screen you want to draw. In my case they are both null because it means the entire texture and the entire screen.