SpaceManiac / HamSandwich

Source code and modding kit for Hamumu's Dr. Lunatic series.
https://spacemaniac.itch.io/hamsandwich
MIT License
39 stars 15 forks source link

I started, then abandoned, an SDL/OpenGL port a while ago and thought maybe it would be useful #5

Open AtkinsSJ opened 5 years ago

AtkinsSJ commented 5 years ago

Yo!

A while ago I made a start on converting the Dr Lunatic code to use SDL/OpenGL for rendering. I didn't get that far, it literally just fades the Hamumu logo in and out again, but I did write a shader that draws images using a palette, which I thought might be useful. (It surprised me how simple it is to do that.)

Vertex shader:

#version 150
in vec3 aPosition;
in vec4 aColor;
in vec2 aUV;

out vec4 vColor;
out vec2 vUV;

uniform mat4 uProjectionMatrix;

void main() {
    gl_Position = uProjectionMatrix * vec4( aPosition.xyz, 1 );
    vColor = aColor;
    vUV = aUV;
}

Fragment shader:

#version 150

uniform sampler2D uTexture;
uniform sampler1D uPalette;

in vec4 vColor;
in vec2 vUV;

out vec4 fragColor;

void main() {
    int colorIndex = int(255.0 * texture(uTexture, vUV).r);
    vec4 texel = texelFetch(uPalette, colorIndex, 0);

    fragColor = vColor;
    fragColor *= texel;
}

The palette is sent by just telling opengl to load its memory as a 1D texture. Images are sent as-is to the GPU, as 2D, single-channel textures. It means you can still use the original palette-switching code, and 256-colour bitmaps, without changing them.

Theoretically using opengl would be faster than software-rendering, but I never got far enough to test that assumption, and rendering the game itself is obviously a lot more complicated than a flat image, and might require a decent amount of restructuring.

So yeah, I don't know how helpful this is, or whether that's even a direction you'd want to go in, but I thought it's better to actually share this than just leaving it rotting on my HDD. (Maybe you already know graphicsy stuff, so I really hope this doesn't seem patronising!) The whole repo is at https://bitbucket.org/AtkinsSJ/glunatic (Not github because it was private until just now and github doesn't have free private repos and I am cheap. Sorry.) Feel free to yell at me if you have any questions.

SpaceManiac commented 5 years ago

HamSandwich is currently using SDL2 for input/windowing, but everything is still software rendering. Moving everything to OpenGL would be a lot of work, but I agree with your assessment that it would probably be faster. I've been experimenting with Android and Emscripten targets so I think it would make sense to target OpenGL ES.

Thanks for the link, I'll be sure to reference it if I ever decide to try rewriting the rendering.