Add angle changing

This commit is contained in:
shylie 2025-08-03 21:36:04 -04:00
parent d5ef14c64d
commit 58656211e5
3 changed files with 51 additions and 17 deletions

View File

@ -1,5 +1,7 @@
#include <sprstk/sprstk.h> #include <sprstk/sprstk.h>
#include <cstdlib>
namespace namespace
{ {
@ -9,7 +11,7 @@ void init(sprstk* instance, void* userdata)
for (int i = 0; i < 28; i++) for (int i = 0; i < 28; i++)
{ {
pal.colors[i] = 0x7F4F0040; pal.colors[i] = 0x7F3F0040;
} }
for (int i = 28; i < 32; i++) for (int i = 28; i < 32; i++)
@ -20,13 +22,16 @@ void init(sprstk* instance, void* userdata)
sprstk_set_palette(instance, 0, &pal); 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() 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); sprstk_run(instance);

View File

@ -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_palette(sprstk* instance, unsigned int index, const sprstk_palette* palette);
void sprstk_set_angle(sprstk* instance, float angle);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -5,6 +5,8 @@
#include <stdexcept> #include <stdexcept>
#include <cmath>
const char* MESH_SHADER_CODE = R"( const char* MESH_SHADER_CODE = R"(
#version 460 #version 460
@ -19,6 +21,7 @@ layout (location = 0) out PerVertexData
} v_out[]; } v_out[];
layout (location = 1) uniform vec3 screen_size_and_pixel_scale; layout (location = 1) uniform vec3 screen_size_and_pixel_scale;
layout (location = 2) uniform mat2 rotation_matrix;
struct TileInfo struct TileInfo
{ {
@ -56,22 +59,23 @@ void main()
{ {
positions[i] += stack_position; positions[i] += stack_position;
positions[i] *= screen_size_and_pixel_scale.zz; 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 z_offset = bitfieldExtract(t_info.position, 25, 2);
uint palette_lookup = bitfieldExtract(t_info.position, 27, 5); uint palette_lookup = bitfieldExtract(t_info.position, 27, 5);
ColorInfo c_info = color_infos[palette_lookup]; 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 a = bitfieldExtract(c_info.color[gl_LocalInvocationID.x], 0, 8);
float b = bitfieldExtract(c_info.color[int(ceil(gl_LocalInvocationID.x * (32.0 / layer_count)))], 8, 8); float b = bitfieldExtract(c_info.color[gl_LocalInvocationID.x], 8, 8);
float g = bitfieldExtract(c_info.color[int(ceil(gl_LocalInvocationID.x * (32.0 / layer_count)))], 16, 8); float g = bitfieldExtract(c_info.color[gl_LocalInvocationID.x], 16, 8);
float r = bitfieldExtract(c_info.color[int(ceil(gl_LocalInvocationID.x * (32.0 / layer_count)))], 24, 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++) 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.y += 24 * position.z / screen_size_and_pixel_scale.z;
position.xy *= vec2(0.05);
gl_MeshVerticesNV[i].gl_Position = position; gl_MeshVerticesNV[i].gl_Position = position;
v_out[i].color = vec4(r, g, b, a) / vec4(256, 256, 256, 256); 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_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; int width, height;
SDL_GetWindowSizeInPixels(sdl.window, &width, &height); SDL_GetWindowSizeInPixels(sdl.window, &width, &height);
glViewport(0, 0, 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(); 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; gl.tile_count = 0;
callbacks.update(this, dt, userdata); callbacks.update(this, dt, userdata);
glClearColor(0, 0, 0, 1); glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT); 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); SDL_GL_SwapWindow(sdl.window);
} }
@ -210,6 +219,15 @@ public:
gl.color_info_map[index] = *palette; 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: private:
sprstk_callbacks callbacks; sprstk_callbacks callbacks;
void* userdata; void* userdata;
@ -331,7 +349,7 @@ private:
int width, height; int width, height;
SDL_GetWindowSizeInPixels(sdl.window, &width, &height); SDL_GetWindowSizeInPixels(sdl.window, &width, &height);
glViewport(0, 0, 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); glGenBuffers(1, &gl.tile_buffer);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 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); 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); 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() void destroy_gl()
@ -414,4 +435,9 @@ void sprstk_set_palette(sprstk* instance, unsigned int index, const sprstk_palet
instance->set_palette(index, palette); instance->set_palette(index, palette);
} }
void sprstk_set_angle(sprstk* instance, float angle)
{
instance->set_angle(angle);
}
} }