SVGAlib shim for SDL2

I recently stumbled upon some source code I wrote when I was 18! I was very much into graphical effects back then, and all of that code targeted SVGAlib, which isn’t really a thing anymore. So I teamed up with ChatGPT to write a thin translation layer to SDL, and I believe I got all of my code to work! Mind you, I didn’t use any advanced features. Just setting modes, setting pixels, and setting the palette.

I (and/or ChatGPT) didn’t implement anything beyond what I needed. Therefore, only the following modes are supported:

G800x600x256 (palette-based)
G800x600x64K (16-bit color depth RGB)
G1024x768x16M (24-bit color depth RGB)

The following SVGAlib functions are implemented:

vga_clear
vga_drawline
vga_drawpixel
vga_init
vga_setcolor
vga_setmode
vga_setpalette
vga_setrgbcolor
vga_white

And that’s it, really. Also, the palette mode is implemented in software.

Does it just work without modifying the source at all? No, not quite. In SVGAlib, all graphics calls are effective immediately, and you would call e.g. sleep() or usleep() to slow down animations. In SDL2, you tell the library that it’s okay to render a frame now. Also, if you modify the palette when you already had something onscreen, and you want the existing pixels to reflect the changes to the palette (usually the case), you need to call a function that redraws the whole screen.

What this means: 1) add a call to vga_waitretrace() before every sleep or usleep(), 2) add a call to redraw_after_palette_update() after you’re done modifying the palette. So let’s say if you have a program like this:

#include <vga.h>

int main()
{
  int x, y;
  vga_init();
  vga_setmode(G800x600x256);

  for (y = 0; y <= 599; y++) {
    for (x = 0; x <= 799; x++) {
      vga_setcolor(x ^ y);
      vga_drawpixel(x, y);
    }
  }
  sleep(5);
}

You modify it like this:

#include <vga.h>

int main()
{
  int x, y;
  vga_init();
  vga_setmode(G800x600x256);

  for (y = 0; y <= 599; y++) {
    for (x = 0; x <= 799; x++) {
      vga_setcolor(x ^ y);
      vga_drawpixel(x, y);
    }
  }
  vga_waitretrace();
  sleep(5);
}

And you put the vga.h shim into your current directory and compile like this:

cc -o foobar foobar.c -I. -lm -lSDL2

That’s it! Here’s the entire vga.h:

Leave a Reply

Your email address will not be published. Required fields are marked *