From 58656211e5eb5dc3138a55cd3da6a964bfa45d8a Mon Sep 17 00:00:00 2001 From: shylie Date: Sun, 3 Aug 2025 21:36:04 -0400 Subject: [PATCH] Add angle changing --- examples/basic.cpp | 18 ++++++++++------ include/sprstk/sprstk.h | 2 ++ src/sprstk.cpp | 48 +++++++++++++++++++++++++++++++---------- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/examples/basic.cpp b/examples/basic.cpp index 528fd4f..2fab33b 100644 --- a/examples/basic.cpp +++ b/examples/basic.cpp @@ -1,5 +1,7 @@ #include +#include + namespace { @@ -9,7 +11,7 @@ void init(sprstk* instance, void* userdata) for (int i = 0; i < 28; i++) { - pal.colors[i] = 0x7F4F0040; + pal.colors[i] = 0x7F3F0040; } for (int i = 28; i < 32; i++) @@ -20,13 +22,16 @@ void init(sprstk* instance, void* userdata) sprstk_set_palette(instance, 0, &pal); } -void update(sprstk* instance, float dt, void* userdata) +void update(sprstk* instance, float dt, float* userdata) { - for (int i = 0; i < 16; i++) + *userdata += dt / 2; + sprstk_set_angle(instance, *userdata); + + for (int i = -512; i < 512; i++) { - for (int j = 15; j >= 0; j--) + for (int j = -512; j < 512; j++) { - sprstk_put(instance, i - 8, j - 8, i + j + 1, 0); + sprstk_put(instance, i, j, 31, 0); } } } @@ -35,7 +40,8 @@ void update(sprstk* instance, float dt, void* userdata) int main() { - sprstk* instance = sprstk_new({.init = init, .update = update}, nullptr); + float data = 0; + sprstk* instance = sprstk_new({.init = init, .update = (sprstk_update_fn)update}, &data); sprstk_run(instance); diff --git a/include/sprstk/sprstk.h b/include/sprstk/sprstk.h index 5818b1d..8784c6e 100644 --- a/include/sprstk/sprstk.h +++ b/include/sprstk/sprstk.h @@ -35,6 +35,8 @@ void sprstk_putz(sprstk* instance, int x, int y, unsigned int layers, unsigned i void sprstk_set_palette(sprstk* instance, unsigned int index, const sprstk_palette* palette); +void sprstk_set_angle(sprstk* instance, float angle); + #ifdef __cplusplus } #endif diff --git a/src/sprstk.cpp b/src/sprstk.cpp index 3051113..b45151e 100644 --- a/src/sprstk.cpp +++ b/src/sprstk.cpp @@ -5,6 +5,8 @@ #include +#include + const char* MESH_SHADER_CODE = R"( #version 460 @@ -19,6 +21,7 @@ layout (location = 0) out PerVertexData } v_out[]; layout (location = 1) uniform vec3 screen_size_and_pixel_scale; +layout (location = 2) uniform mat2 rotation_matrix; struct TileInfo { @@ -56,22 +59,23 @@ void main() { positions[i] += stack_position; positions[i] *= screen_size_and_pixel_scale.zz; - positions[i] /= screen_size_and_pixel_scale.xy; + positions[i] /= vec2(min(screen_size_and_pixel_scale.x, screen_size_and_pixel_scale.y)); } uint z_offset = bitfieldExtract(t_info.position, 25, 2); uint palette_lookup = bitfieldExtract(t_info.position, 27, 5); ColorInfo c_info = color_infos[palette_lookup]; - float a = bitfieldExtract(c_info.color[int(ceil(gl_LocalInvocationID.x * (32.0 / layer_count)))], 0, 8); - float b = bitfieldExtract(c_info.color[int(ceil(gl_LocalInvocationID.x * (32.0 / layer_count)))], 8, 8); - float g = bitfieldExtract(c_info.color[int(ceil(gl_LocalInvocationID.x * (32.0 / layer_count)))], 16, 8); - float r = bitfieldExtract(c_info.color[int(ceil(gl_LocalInvocationID.x * (32.0 / layer_count)))], 24, 8); + float a = bitfieldExtract(c_info.color[gl_LocalInvocationID.x], 0, 8); + float b = bitfieldExtract(c_info.color[gl_LocalInvocationID.x], 8, 8); + float g = bitfieldExtract(c_info.color[gl_LocalInvocationID.x], 16, 8); + float r = bitfieldExtract(c_info.color[gl_LocalInvocationID.x], 24, 8); for (uint i = 4 * gl_LocalInvocationID.x; i < 4 * gl_LocalInvocationID.x + 4; i++) { - vec4 position = vec4(positions[i % 4], float(4 * gl_LocalInvocationID.x + z_offset) / 128, 1); + vec4 position = vec4(rotation_matrix * positions[i % 4], float(4 * gl_LocalInvocationID.x + z_offset) / 128, 1); position.y += 24 * position.z / screen_size_and_pixel_scale.z; + position.xy *= vec2(0.05); gl_MeshVerticesNV[i].gl_Position = position; v_out[i].color = vec4(r, g, b, a) / vec4(256, 256, 256, 256); @@ -82,7 +86,7 @@ void main() gl_PrimitiveIndicesNV[i] = 4 * gl_LocalInvocationID.x + indices[i % 6]; } - gl_PrimitiveCountNV = layer_count * 2; + gl_PrimitiveCountNV = layer_count * 2 + 2; } )"; @@ -157,19 +161,24 @@ public: int width, height; SDL_GetWindowSizeInPixels(sdl.window, &width, &height); glViewport(0, 0, width, height); - glProgramUniform3f(gl.program, 1, width, height, 32); + glProgramUniform3f(gl.program, 1, width, height, 8); } } uint64_t current_ticks = SDL_GetTicks(); - float dt = (prev_ticks - current_ticks) / 1000.0f; + float dt = (current_ticks - prev_ticks) / 1000.0f; + prev_ticks = current_ticks; gl.tile_count = 0; callbacks.update(this, dt, userdata); glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); - glDrawMeshTasksNV(0, gl.tile_count); + int i; + for (i = 0; i < gl.tile_count; i += 65535) + { + glDrawMeshTasksNV(i, 65535); + } SDL_GL_SwapWindow(sdl.window); } @@ -210,6 +219,15 @@ public: gl.color_info_map[index] = *palette; } + void set_angle(float angle) + { + const float arr[4] = { + cosf(angle), -sinf(angle), + sinf(angle), cosf(angle) + }; + glProgramUniformMatrix2fv(gl.program, 2, 1, false, arr); + } + private: sprstk_callbacks callbacks; void* userdata; @@ -331,7 +349,7 @@ private: int width, height; SDL_GetWindowSizeInPixels(sdl.window, &width, &height); glViewport(0, 0, width, height); - glProgramUniform3f(gl.program, 1, width, height, 32); + glProgramUniform3f(gl.program, 1, width, height, 8); glGenBuffers(1, &gl.tile_buffer); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, gl.tile_buffer); @@ -345,6 +363,9 @@ private: glNamedBufferStorage(gl.color_buffer, sizeof(sprstk_palette) * (1 << 5), nullptr, GL_MAP_WRITE_BIT | GL_MAP_COHERENT_BIT | GL_MAP_PERSISTENT_BIT); gl.color_info_map = (sprstk_palette*)glMapNamedBufferRange(gl.color_buffer, 0, sizeof(sprstk_palette) * (1 << 5), GL_MAP_WRITE_BIT | GL_MAP_COHERENT_BIT); + + const float arr[4] = {1, 0, 0, 1}; + glProgramUniformMatrix2fv(gl.program, 2, 1, false, arr); } void destroy_gl() @@ -414,4 +435,9 @@ void sprstk_set_palette(sprstk* instance, unsigned int index, const sprstk_palet instance->set_palette(index, palette); } +void sprstk_set_angle(sprstk* instance, float angle) +{ + instance->set_angle(angle); +} + }