diff --git a/gfx.png b/gfx.png index 0491d1a..8473f77 100644 Binary files a/gfx.png and b/gfx.png differ diff --git a/include/color32.h b/include/color32.h index 09aa899..de1fdf7 100644 --- a/include/color32.h +++ b/include/color32.h @@ -5,4 +5,17 @@ typedef u32 sw_color32; +sw_color32 sw_color32_from_rgb(u8 r, u8 g, u8 b); +sw_color32 sw_color32_from_rgba(u8 r, u8 g, u8 b, u8 a); + +u8 sw_color32_get_r(sw_color32 col); +u8 sw_color32_get_g(sw_color32 col); +u8 sw_color32_get_b(sw_color32 col); +u8 sw_color32_get_a(sw_color32 col); + +f32 sw_color32_get_rf(sw_color32 col); +f32 sw_color32_get_gf(sw_color32 col); +f32 sw_color32_get_bf(sw_color32 col); +f32 sw_color32_get_af(sw_color32 col); + #endif /* GUARD_99E25E7F8183EF0D8D27390EBC173C04 */ diff --git a/include/image8.h b/include/image8.h index 3956d3c..94cb33e 100644 --- a/include/image8.h +++ b/include/image8.h @@ -1,12 +1,14 @@ #ifndef GUARD_6AEC99B12E1F76DC4E50DC199E93CDB5 #define GUARD_6AEC99B12E1F76DC4E50DC199E93CDB5 +#include "color32.h" #include "color8.h" #include "vec2i.h" struct sw_image8 { struct sw_vec2i size; sw_color8 *_data; + sw_color32 palette[256]; }; struct sw_image8 *sw_image8_create(struct sw_vec2i size); diff --git a/src/color32.c b/src/color32.c index e69de29..05633bf 100644 --- a/src/color32.c +++ b/src/color32.c @@ -0,0 +1,41 @@ +#include "color32.h" + +sw_color32 sw_color32_from_rgb(u8 r, u8 g, u8 b) { + return (u32)r | ((u32)g << 8) | ((u32)b << 16) | ((u32)255 << 24); +} + +sw_color32 sw_color32_from_rgba(u8 r, u8 g, u8 b, u8 a) { + return (u32)r | ((u32)g << 8) | ((u32)b << 16) | ((u32)a << 24); +} + +u8 sw_color32_get_r(sw_color32 col) { + return col & 0xFF; +} + +u8 sw_color32_get_g(sw_color32 col) { + return (col >> 8) & 0xFF; +} + +u8 sw_color32_get_b(sw_color32 col) { + return (col >> 16) & 0xFF; +} + +u8 sw_color32_get_a(sw_color32 col) { + return (col >> 24) & 0xFF; +} + +f32 sw_color32_get_rf(sw_color32 col) { + return (f32)sw_color32_get_r(col) / 255.f; +} + +f32 sw_color32_get_gf(sw_color32 col) { + return (f32)sw_color32_get_g(col) / 255.f; +} + +f32 sw_color32_get_bf(sw_color32 col) { + return (f32)sw_color32_get_b(col) / 255.f; +} + +f32 sw_color32_get_af(sw_color32 col) { + return (f32)sw_color32_get_a(col) / 255.f; +} diff --git a/src/gsa.c b/src/gsa.c index fa93b0d..8541cc9 100644 --- a/src/gsa.c +++ b/src/gsa.c @@ -1,8 +1,10 @@ +#include "color32.h" #include "error.h" #include "gl.h" #include "image8.h" #include "shader.h" #include +#include #define GSA_NOMAIN #include "gsa.h" @@ -44,6 +46,7 @@ struct render_vert { static i32 next_render_vert; static struct render_vert render_verts[MAX_RENDER_VERTS * 4]; +static sw_color32 palette[256]; int gsa_main(int argc, char *argv[]) { (void)argc; @@ -58,6 +61,7 @@ int gsa_main(int argc, char *argv[]) { sw_shaderprogram_use(program); img = sw_image8_load_png("gfx.png"); + memcpy(palette, img->palette, sizeof(img->palette)); glGenTextures(1, &tex); glBindTexture(GL_TEXTURE_2D, tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); @@ -106,6 +110,14 @@ int gsa_main(int argc, char *argv[]) { return 0; } +static void add_render_vert(f32 x, f32 y, f32 tx, f32 ty) { + render_verts[next_render_vert].x = x; + render_verts[next_render_vert].y = y; + render_verts[next_render_vert].tx = tx; + render_verts[next_render_vert].ty = ty; + ++next_render_vert; +} + static void rect(f32 x, f32 y, f32 w, f32 h, i16 tile) { f32 tx, ty, ts; @@ -121,8 +133,14 @@ static void rect(f32 x, f32 y, f32 w, f32 h, i16 tile) { ); } - sw_log("tx %f | ty %f | ts %f", tx, ty, ts); + add_render_vert(x, y, tx, ty); + add_render_vert(x + w, y, tx + ts, ty); + add_render_vert(x, y + h, tx, ty + ts); + add_render_vert(x + w, y, tx + ts, ty); + add_render_vert(x, y + h, tx, ty + ts); + add_render_vert(x + w, y + h, tx + ts, ty + ts); + /* render_verts[next_render_vert].x = x; render_verts[next_render_vert].y = y; render_verts[next_render_vert].tx = tx; @@ -158,6 +176,8 @@ static void rect(f32 x, f32 y, f32 w, f32 h, i16 tile) { render_verts[next_render_vert].tx = tx + ts; render_verts[next_render_vert].ty = ty + ts; ++next_render_vert; + + */ } void _gsa_tick() { @@ -172,7 +192,12 @@ void _gsa_tick() { rect(sprites[i].x, sprites[i].y, 16, 16, sprites[i].tile); } } - + glClearColor( + sw_color32_get_rf(palette[0]), + sw_color32_get_gf(palette[0]), + sw_color32_get_bf(palette[0]), + sw_color32_get_af(palette[0]) + ); glBindVertexArray(vao); glBufferData( GL_ARRAY_BUFFER, diff --git a/src/image8.c b/src/image8.c index 804be36..d638c05 100644 --- a/src/image8.c +++ b/src/image8.c @@ -2,6 +2,7 @@ #include +#include "color32.h" #include "error.h" #include "file.h" #include "png.h" @@ -43,6 +44,8 @@ struct sw_image8 *sw_image8_load_png_data(u8 *data, u32 data_len) { u32 width, height; u8 color_type, bit_depth; u8 **row_pointers; + png_color *png_pal; + int png_pal_size; u32 i; buf = malloc(sizeof(struct sw_filebuffer)); @@ -90,6 +93,17 @@ struct sw_image8 *sw_image8_load_png_data(u8 *data, u32 data_len) { png_read_image(png, row_pointers); free(row_pointers); + + png_get_PLTE(png, info, &png_pal, &png_pal_size); + if(png_pal_size > 256) { + png_pal_size = 256; + } + for(i = 0; (i32)i < png_pal_size; ++i) { + img->palette[i] = sw_color32_from_rgb( + png_pal[i].red, png_pal[i].green, png_pal[i].blue + ); + } + free(buf); /* TODO: cleanup of png structs? */