palette drawing :)

This commit is contained in:
DaniTheSkunk 2023-01-05 09:59:05 +00:00
parent 3c1908317b
commit de0e18afa4
2 changed files with 24 additions and 40 deletions

BIN
gfx.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -26,11 +26,12 @@ out_tex_coord = tex_coord;\
static char *frag_source = "#version 450\n\ static char *frag_source = "#version 450\n\
layout(location = 0) in vec2 tex_coord;\ layout(location = 0) in vec2 tex_coord;\
uniform sampler2D tex;\ uniform sampler2D tex;\
uniform vec4 palette[256];\
out vec4 color;\ out vec4 color;\
void main(){\ void main(){\
float r = texture(tex, tex_coord).r;\ int r = int(texture(tex, tex_coord).r * 255);\
if(r > 0)\ if(r > 0)\
color = vec4(0.9, 0.2, 0.1, 1.0);\ color = palette[r];\
}\ }\
"; ";
@ -47,12 +48,14 @@ struct render_vert {
static i32 next_render_vert; static i32 next_render_vert;
static struct render_vert render_verts[MAX_RENDER_VERTS * 4]; static struct render_vert render_verts[MAX_RENDER_VERTS * 4];
static sw_color32 palette[256]; static sw_color32 palette[256];
static f32 palette_gl[1024];
int gsa_main(int argc, char *argv[]) { int gsa_main(int argc, char *argv[]) {
(void)argc; (void)argc;
(void)argv; (void)argv;
u32 tex; u32 tex;
struct sw_image8 *img; struct sw_image8 *img;
i32 max_components;
sw_log("Initialising GameSkunkAdvance v0.0"); sw_log("Initialising GameSkunkAdvance v0.0");
@ -60,8 +63,12 @@ int gsa_main(int argc, char *argv[]) {
program = sw_shaderprogram_create(vert_source, frag_source); program = sw_shaderprogram_create(vert_source, frag_source);
sw_shaderprogram_use(program); sw_shaderprogram_use(program);
glGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS, &max_components);
sw_log("GL_MAX_VERTEX_UNIFORM_COMPONENTS: %i", max_components);
img = sw_image8_load_png("gfx.png"); img = sw_image8_load_png("gfx.png");
memcpy(palette, img->palette, sizeof(img->palette)); memcpy(palette, img->palette, sizeof(img->palette));
sw_debug("%08X", img->palette[1]);
glGenTextures(1, &tex); glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex); glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
@ -139,45 +146,16 @@ static void rect(f32 x, f32 y, f32 w, f32 h, i16 tile) {
add_render_vert(x + w, y, tx + ts, ty); add_render_vert(x + w, y, tx + ts, ty);
add_render_vert(x, y + h, tx, ty + ts); add_render_vert(x, y + h, tx, ty + ts);
add_render_vert(x + w, y + h, tx + ts, ty + ts); add_render_vert(x + w, y + h, tx + ts, ty + ts);
}
/* static void update_palette_gl() {
render_verts[next_render_vert].x = x; int i;
render_verts[next_render_vert].y = y; for(i = 0; i < 256; ++i) {
render_verts[next_render_vert].tx = tx; palette_gl[i * 4 + 0] = sw_color32_get_rf(palette[i]);
render_verts[next_render_vert].ty = ty; palette_gl[i * 4 + 1] = sw_color32_get_gf(palette[i]);
++next_render_vert; palette_gl[i * 4 + 2] = sw_color32_get_bf(palette[i]);
palette_gl[i * 4 + 3] = sw_color32_get_af(palette[i]);
render_verts[next_render_vert].x = x + w; }
render_verts[next_render_vert].y = y;
render_verts[next_render_vert].tx = tx + ts;
render_verts[next_render_vert].ty = ty;
++next_render_vert;
render_verts[next_render_vert].x = x;
render_verts[next_render_vert].y = y + h;
render_verts[next_render_vert].tx = tx;
render_verts[next_render_vert].ty = ty + ts;
++next_render_vert;
render_verts[next_render_vert].x = x + w;
render_verts[next_render_vert].y = y;
render_verts[next_render_vert].tx = tx + ts;
render_verts[next_render_vert].ty = ty;
++next_render_vert;
render_verts[next_render_vert].x = x;
render_verts[next_render_vert].y = y + h;
render_verts[next_render_vert].tx = tx;
render_verts[next_render_vert].ty = ty + ts;
++next_render_vert;
render_verts[next_render_vert].x = x + w;
render_verts[next_render_vert].y = y + h;
render_verts[next_render_vert].tx = tx + ts;
render_verts[next_render_vert].ty = ty + ts;
++next_render_vert;
*/
} }
void _gsa_tick() { void _gsa_tick() {
@ -198,7 +176,13 @@ void _gsa_tick() {
sw_color32_get_bf(palette[0]), sw_color32_get_bf(palette[0]),
sw_color32_get_af(palette[0]) sw_color32_get_af(palette[0])
); );
glBindVertexArray(vao); glBindVertexArray(vao);
/* TODO: only do when palette changed */
update_palette_gl();
glUniform4fv(
glGetUniformLocation(program._program, "palette"), 256, palette_gl
);
glBufferData( glBufferData(
GL_ARRAY_BUFFER, GL_ARRAY_BUFFER,
sizeof(struct render_vert) * (next_render_vert), sizeof(struct render_vert) * (next_render_vert),