loading palette

This commit is contained in:
DaniTheSkunk 2023-01-05 09:01:18 +00:00
parent 062067a033
commit 3c1908317b
6 changed files with 97 additions and 2 deletions

BIN
gfx.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -5,4 +5,17 @@
typedef u32 sw_color32; 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 */ #endif /* GUARD_99E25E7F8183EF0D8D27390EBC173C04 */

View File

@ -1,12 +1,14 @@
#ifndef GUARD_6AEC99B12E1F76DC4E50DC199E93CDB5 #ifndef GUARD_6AEC99B12E1F76DC4E50DC199E93CDB5
#define GUARD_6AEC99B12E1F76DC4E50DC199E93CDB5 #define GUARD_6AEC99B12E1F76DC4E50DC199E93CDB5
#include "color32.h"
#include "color8.h" #include "color8.h"
#include "vec2i.h" #include "vec2i.h"
struct sw_image8 { struct sw_image8 {
struct sw_vec2i size; struct sw_vec2i size;
sw_color8 *_data; sw_color8 *_data;
sw_color32 palette[256];
}; };
struct sw_image8 *sw_image8_create(struct sw_vec2i size); struct sw_image8 *sw_image8_create(struct sw_vec2i size);

View File

@ -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;
}

View File

@ -1,8 +1,10 @@
#include "color32.h"
#include "error.h" #include "error.h"
#include "gl.h" #include "gl.h"
#include "image8.h" #include "image8.h"
#include "shader.h" #include "shader.h"
#include <gl/gl.h> #include <gl/gl.h>
#include <string.h>
#define GSA_NOMAIN #define GSA_NOMAIN
#include "gsa.h" #include "gsa.h"
@ -44,6 +46,7 @@ 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];
int gsa_main(int argc, char *argv[]) { int gsa_main(int argc, char *argv[]) {
(void)argc; (void)argc;
@ -58,6 +61,7 @@ int gsa_main(int argc, char *argv[]) {
sw_shaderprogram_use(program); sw_shaderprogram_use(program);
img = sw_image8_load_png("gfx.png"); img = sw_image8_load_png("gfx.png");
memcpy(palette, img->palette, sizeof(img->palette));
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);
@ -106,6 +110,14 @@ int gsa_main(int argc, char *argv[]) {
return 0; 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) { static void rect(f32 x, f32 y, f32 w, f32 h, i16 tile) {
f32 tx, ty, ts; 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].x = x;
render_verts[next_render_vert].y = y; render_verts[next_render_vert].y = y;
render_verts[next_render_vert].tx = tx; 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].tx = tx + ts;
render_verts[next_render_vert].ty = ty + ts; render_verts[next_render_vert].ty = ty + ts;
++next_render_vert; ++next_render_vert;
*/
} }
void _gsa_tick() { void _gsa_tick() {
@ -172,7 +192,12 @@ void _gsa_tick() {
rect(sprites[i].x, sprites[i].y, 16, 16, sprites[i].tile); 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); glBindVertexArray(vao);
glBufferData( glBufferData(
GL_ARRAY_BUFFER, GL_ARRAY_BUFFER,

View File

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "color32.h"
#include "error.h" #include "error.h"
#include "file.h" #include "file.h"
#include "png.h" #include "png.h"
@ -43,6 +44,8 @@ struct sw_image8 *sw_image8_load_png_data(u8 *data, u32 data_len) {
u32 width, height; u32 width, height;
u8 color_type, bit_depth; u8 color_type, bit_depth;
u8 **row_pointers; u8 **row_pointers;
png_color *png_pal;
int png_pal_size;
u32 i; u32 i;
buf = malloc(sizeof(struct sw_filebuffer)); 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); png_read_image(png, row_pointers);
free(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); free(buf);
/* TODO: cleanup of png structs? */ /* TODO: cleanup of png structs? */