diff --git a/examples/basic.cpp b/examples/basic.cpp index 0c347f8..fa90ddd 100644 --- a/examples/basic.cpp +++ b/examples/basic.cpp @@ -6,19 +6,15 @@ namespace { void init() { - glerminal_update_palette(0, 0x00000000); - glerminal_update_palette(1, 0xFF00007F); - glerminal_update_palette(2, 0x00FF007F); - - glerminal_update_sprite(1, { - 2, 2, 2, 2, 2, 2, 2, 2, - 2, 1, 1, 1, 1, 1, 1, 2, - 2, 1, 1, 1, 1, 1, 1, 2, - 2, 1, 1, 1, 1, 1, 1, 2, - 2, 1, 1, 1, 1, 1, 1, 2, - 2, 1, 1, 1, 1, 1, 1, 2, - 2, 1, 1, 1, 1, 1, 1, 2, - 2, 2, 2, 2, 2, 2, 2, 2 + glerminal_update_sprite(0xFF00007F, { + 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, + 0x00FF007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0x00FF007F, + 0x00FF007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0x00FF007F, + 0x00FF007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0x00FF007F, + 0x00FF007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0x00FF007F, + 0x00FF007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0x00FF007F, + 0x00FF007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0x00FF007F, + 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F }); } diff --git a/examples/towers.cpp b/examples/towers.cpp index 4c418fe..6a10f1a 100644 --- a/examples/towers.cpp +++ b/examples/towers.cpp @@ -9,24 +9,19 @@ namespace { for (int i = 1; i < 256; i++) { - constexpr unsigned char c = 0; + constexpr unsigned char c = 16; const unsigned char v = (255 - c) * powf((i - 1) / 256.0f, 4.0f) + c; - glerminal_update_palette(i, (v << 24) | (v << 16) | (v << 8) | 0xE0); - } - - for (int i = 1; i < 256; i++) - { - const unsigned char j = i; + const unsigned int j = (0xFF << 24) | (v << 16) | (v << 8) | v; glerminal_update_sprite(i, { - 0,j,0,0,0,0,j,0, j,j,j,j,j,j,j,j, - 0,j,0,0,0,0,j,0, - 0,j,0,0,0,0,j,0, - 0,j,0,0,0,0,j,0, - 0,j,0,0,0,0,j,0, j,j,j,j,j,j,j,j, - 0,j,0,0,0,0,j,0, + j,j,j,j,j,j,j,j, + j,j,j,j,j,j,j,j, + j,j,j,j,j,j,j,j, + j,j,j,j,j,j,j,j, + j,j,j,j,j,j,j,j, + j,j,j,j,j,j,j,j, }); } @@ -34,7 +29,8 @@ namespace { for (int j = 0; j < 25; j++) { - for (int k = 0; k < 256; k++) + const int c = rand() % 224 + 32; + for (int k = 0; k < c; k++) { glerminal_set(i, j, k, k); } @@ -66,8 +62,8 @@ namespace { for (int k = 0; k < 256; k++) { - const float ox = 0.05f * powf(k, 0.55f) * copysignf(sqrtf((i - cx) * (i - cx)), i - cx); - const float oy = 0.05f * powf(k, 0.55f) * copysignf(sqrtf((j - cy) * (j - cy)), j - cy); + const float ox = 0.025f * powf(k, 0.8f) * (i - cx); + const float oy = 0.025f * powf(k, 0.8f) * (j - cy); glerminal_offset(i, j, k, ox, oy); } } diff --git a/include/glerminal.h b/include/glerminal.h index 3d4b6ac..62528f5 100644 --- a/include/glerminal.h +++ b/include/glerminal.h @@ -17,11 +17,12 @@ typedef void (*glerminal_main_cb)(float dt); typedef struct glerminal_sprite { - unsigned char data[GLERMINAL_CELL_AREA]; + unsigned int data[GLERMINAL_CELL_AREA]; } glerminal_sprite; /** * @brief Call init once, then run the application's mainloop + * @param init initialization callback * @param main main calllback */ void glerminal_run(glerminal_init_cb init, glerminal_main_cb main); @@ -64,13 +65,6 @@ void glerminal_offset(unsigned char x, unsigned char y, unsigned char layer, flo */ void glerminal_update_sprite(unsigned char id, glerminal_sprite sprite); -/** - * @brief Update palette color at index ID - * @param id Palette index to update - * @param color New color - */ -void glerminal_update_palette(unsigned char id, unsigned int color); - #ifdef __cplusplus } #endif diff --git a/source/glerminal-private.h b/source/glerminal-private.h index 039af74..6eadf61 100644 --- a/source/glerminal-private.h +++ b/source/glerminal-private.h @@ -43,7 +43,6 @@ namespace glerminal void offset(unsigned char x, unsigned char y, unsigned char layer, float x_offset, float y_offset); void update_sprite(unsigned char id, glerminal_sprite sprite); - void update_palette_color(unsigned char id, unsigned int color); private: GLFWwindow* m_window; @@ -64,8 +63,7 @@ namespace glerminal unsigned char m_cells[GRID_AREA * LAYER_COUNT]; float m_offsets[GRID_AREA * LAYER_COUNT * 2]; - unsigned char m_sprites[CELL_SIZE * CELL_SIZE * 256]; - float m_palette[256 * 4]; + unsigned int m_sprites[CELL_SIZE * CELL_SIZE * 256]; glerminal_main_cb m_main; @@ -81,7 +79,6 @@ namespace glerminal void deinit_gl(); void update_sprites(); - void update_palette(); }; } diff --git a/source/glerminal.cpp b/source/glerminal.cpp index 2008fed..e886ab1 100644 --- a/source/glerminal.cpp +++ b/source/glerminal.cpp @@ -1,7 +1,6 @@ #include "glerminal-private.h" #define GRID_SIZE_UNIFORM_NAME "grid_size" -#define PALETTE_UNIFORM_NAME "palette" #define SPRITES_UNIFORM_NAME "sprites" #define LAYERS_UNIFORM_NAME "layers" @@ -81,12 +80,11 @@ namespace "#version 400 core\n" "in vec2 texcoord;\n" "flat in int sprite;\n" - "uniform usampler2DArray " SPRITES_UNIFORM_NAME ";\n" - "uniform vec4 " PALETTE_UNIFORM_NAME "[256];\n" + "uniform sampler2DArray " SPRITES_UNIFORM_NAME ";\n" "out vec4 FragColor;\n" "void main()\n" "{\n" - " FragColor = " PALETTE_UNIFORM_NAME "[int(texture(" SPRITES_UNIFORM_NAME ", vec3(texcoord, sprite)))];\n" + " FragColor = texture(" SPRITES_UNIFORM_NAME ", vec3(texcoord, sprite));\n" "}"; constexpr char* SCREEN_VERTEX_SHADER_SOURCE = @@ -122,8 +120,7 @@ namespace glerminal m_main(main), m_cells{ }, m_offsets{ }, - m_sprites{ }, - m_palette{ } + m_sprites{ } #ifdef _DEBUG , m_log("log.txt") #endif @@ -181,7 +178,6 @@ namespace glerminal glNamedBufferData(m_sprites_instance_vbo, sizeof(m_cells), m_cells, GL_STREAM_DRAW); glNamedBufferData(m_offsets_instance_vbo, sizeof(m_offsets), m_offsets, GL_STREAM_DRAW); update_sprites(); - update_palette(); glViewport(0, 0, GRID_WIDTH * CELL_SIZE, GRID_HEIGHT * CELL_SIZE); glUseProgram(m_program); @@ -237,17 +233,6 @@ namespace glerminal reinterpret_cast(m_sprites)[id] = sprite; } - void glerminal::update_palette_color(unsigned char id, unsigned int color) - { - if (id < 256) - { - m_palette[4 * id + 0] = ((color >> 24) & 0xFF) / 255.0f; - m_palette[4 * id + 1] = ((color >> 16) & 0xFF) / 255.0f; - m_palette[4 * id + 2] = ((color >> 8) & 0xFF) / 255.0f; - m_palette[4 * id + 3] = ((color >> 0) & 0xFF) / 255.0f; - } - } - void glerminal::init_glfw() { glfwInit(); @@ -457,15 +442,12 @@ namespace glerminal // setup uniforms m_screen_size_uniform_location = glGetUniformLocation(m_program, GRID_SIZE_UNIFORM_NAME); - m_palette_uniform_location = glGetUniformLocation(m_program, PALETTE_UNIFORM_NAME); glUseProgram(m_program); glUniform4f(m_screen_size_uniform_location, GRID_WIDTH, GRID_HEIGHT, 1.0f / GRID_WIDTH, 1.0f / GRID_HEIGHT); glUniform1i(glGetUniformLocation(m_program, SPRITES_UNIFORM_NAME), 0); - update_palette(); - // compile const unsigned int screen_vertex_shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(screen_vertex_shader, 1, &SCREEN_VERTEX_SHADER_SOURCE, nullptr); @@ -574,13 +556,7 @@ namespace glerminal void glerminal::update_sprites() { glBindTexture(GL_TEXTURE_2D_ARRAY, m_sprites_texture); - glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_R8UI, CELL_SIZE, CELL_SIZE, 256, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, m_sprites); - } - - void glerminal::update_palette() - { - glUseProgram(m_program); - glUniform4fv(m_palette_uniform_location, 256, m_palette); + glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, CELL_SIZE, CELL_SIZE, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_sprites); } } @@ -631,11 +607,4 @@ void glerminal_update_sprite(unsigned char id, glerminal_sprite sprite) if (!GLERMINAL_G) { return; } GLERMINAL_G->update_sprite(id, sprite); -} - -void glerminal_update_palette(unsigned char id, unsigned int color) -{ - if (!GLERMINAL_G) { return; } - - GLERMINAL_G->update_palette_color(id, color); } \ No newline at end of file