renderer2d work

This commit is contained in:
DaniTheSkunk 2023-04-16 22:40:41 +00:00
parent 3ea92c4618
commit 5c8abf1a7d
9 changed files with 215 additions and 96 deletions

View File

@ -3,20 +3,45 @@
<Objective-C> <Objective-C>
<option name="HEADER_GUARD_STYLE_PATTERN" value="GUARD_${UUID}" /> <option name="HEADER_GUARD_STYLE_PATTERN" value="GUARD_${UUID}" />
</Objective-C> </Objective-C>
<clangFormatSettings> <Objective-C-extensions>
<option name="ENABLED" value="true" /> <rules>
</clangFormatSettings> <rule entity="NAMESPACE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<files> <rule entity="MACRO" visibility="ANY" specifier="ANY" prefix="" style="SCREAMING_SNAKE_CASE" suffix="" />
<extensions> <rule entity="CLASS" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<pair source="c" header="h" fileNamingConvention="SNAKE_CASE" /> <rule entity="STRUCT" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<pair source="cu" header="cuh" fileNamingConvention="NONE" /> <rule entity="ENUM" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<pair source="ixx" header="" fileNamingConvention="NONE" /> <rule entity="ENUMERATOR" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<pair source="mxx" header="" fileNamingConvention="NONE" /> <rule entity="TYPEDEF" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<pair source="cppm" header="" fileNamingConvention="NONE" /> <rule entity="UNION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<pair source="ccm" header="" fileNamingConvention="NONE" /> <rule entity="CLASS_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<pair source="cxxm" header="" fileNamingConvention="NONE" /> <rule entity="STRUCT_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<pair source="c++m" header="" fileNamingConvention="NONE" /> <rule entity="CLASS_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
</extensions> <rule entity="STRUCT_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
</files> <rule entity="GLOBAL_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="GLOBAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="PARAMETER" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="LOCAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
</rules>
</Objective-C-extensions>
<Objective-C-extensions>
<rules>
<rule entity="NAMESPACE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="MACRO" visibility="ANY" specifier="ANY" prefix="" style="SCREAMING_SNAKE_CASE" suffix="" />
<rule entity="CLASS" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="STRUCT" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="ENUM" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="ENUMERATOR" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="TYPEDEF" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="UNION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="CLASS_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="STRUCT_MEMBER_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="CLASS_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="STRUCT_MEMBER_FIELD" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="GLOBAL_FUNCTION" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="GLOBAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="PARAMETER" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
<rule entity="LOCAL_VARIABLE" visibility="ANY" specifier="ANY" prefix="" style="SNAKE_CASE" suffix="" />
</rules>
</Objective-C-extensions>
</code_scheme> </code_scheme>
</component> </component>

View File

@ -1,5 +1,5 @@
<component name="ProjectCodeStyleConfiguration"> <component name="ProjectCodeStyleConfiguration">
<state> <state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" /> <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state> </state>
</component> </component>

View File

@ -2,12 +2,19 @@
#define GUARD_2B56918289975E8AA8711D6E080ED4B9 #define GUARD_2B56918289975E8AA8711D6E080ED4B9
#include "shader.h" #include "shader.h"
#include "types.h"
#include "vec2f.h"
#include "vec4f.h"
#include "vertex_buffer.h"
struct sw_renderer; struct sw_renderer;
struct sw_renderer2d { struct sw_renderer2d {
struct sw_shaderprogram shader; struct sw_shaderprogram shader;
struct sw_renderer *renderer; struct sw_renderer *renderer;
struct sw_vertex_buffer *vertex_buffer;
struct sw_vec4f color;
u32 vao, vbo;
}; };
struct sw_renderer2d *sw_renderer2d_create(); struct sw_renderer2d *sw_renderer2d_create();
@ -22,6 +29,17 @@ struct sw_renderer2d *sw_renderer2d_create();
- draw "sprite" (quad of same size as texture) - draw "sprite" (quad of same size as texture)
- draw line - draw line
- transform matrix stack - transform matrix stack
- coordinate style, match pixel coordinates, vs 1.f full vertical range
*/ */
void sw_renderer2d_draw_quad(
struct sw_renderer2d *renderer,
struct sw_vec2f a,
struct sw_vec2f b,
struct sw_vec2f c,
struct sw_vec2f d
);
void sw_renderer2d_set_color(struct sw_renderer2d *ren, struct sw_vec4f color);
#endif /* GUARD_2B56918289975E8AA8711D6E080ED4B9 */ #endif /* GUARD_2B56918289975E8AA8711D6E080ED4B9 */

View File

@ -11,10 +11,14 @@ struct sw_vertex_buffer {
}; };
struct sw_vertex_buffer *sw_vertex_buffer_create(u32 elem_size); struct sw_vertex_buffer *sw_vertex_buffer_create(u32 elem_size);
void sw_vertex_buffer_destroy(struct sw_vertex_buffer *vb); void sw_vertex_buffer_destroy(struct sw_vertex_buffer *vb);
/* copies elem data */ /* copies elem data */
void sw_vertex_buffer_add(struct sw_vertex_buffer *vb, void *elem); void sw_vertex_buffer_add(struct sw_vertex_buffer *vb, void *elem);
void sw_vertex_buffer_clear(struct sw_vertex_buffer *vb); void sw_vertex_buffer_clear(struct sw_vertex_buffer *vb);
void sw_vertex_buffer_send(struct sw_vertex_buffer *vb);
#endif /* GUARD_B3E8DB670A75ACCB1082192FB0485037 */ #endif /* GUARD_B3E8DB670A75ACCB1082192FB0485037 */

View File

@ -10,7 +10,9 @@
#include "window.h" #include "window.h"
#include <gl/gl.h> #include <gl/gl.h>
#include <string.h> #include <string.h>
#define GSA_NOMAIN #define GSA_NOMAIN
#include "gsa.h" #include "gsa.h"
#include "gsa_input.h" #include "gsa_input.h"
@ -34,6 +36,7 @@ static u32 tex;
#define GSA_LOOP_STACK_SIZE 16 #define GSA_LOOP_STACK_SIZE 16
static bool (*loop_stack[GSA_LOOP_STACK_SIZE])(); static bool (*loop_stack[GSA_LOOP_STACK_SIZE])();
static i8 loop_stack_i; static i8 loop_stack_i;
static struct sw_vertex_buffer *vertex_buffer; static struct sw_vertex_buffer *vertex_buffer;
@ -41,8 +44,8 @@ static struct sw_vertex_buffer *vertex_buffer;
static void gsa_tick(); static void gsa_tick();
int gsa_main(int argc, char *argv[]) { int gsa_main(int argc, char *argv[]) {
(void)argc; (void) argc;
(void)argv; (void) argv;
i32 max_components; i32 max_components;
u32 size; u32 size;
u8 *data; u8 *data;
@ -70,7 +73,12 @@ int gsa_main(int argc, char *argv[]) {
frag_data = sw_skip_get_string(sw_skip_self, "gsa_frag.glsl"); frag_data = sw_skip_get_string(sw_skip_self, "gsa_frag.glsl");
vert_data = sw_skip_get_string(sw_skip_self, "gsa_vert.glsl"); vert_data = sw_skip_get_string(sw_skip_self, "gsa_vert.glsl");
program = program =
sw_shaderprogram_create((u8 *)vert_data, 0, (u8 *)frag_data, 0, false); sw_shaderprogram_create((u8 *) vert_data,
0,
(u8 *) frag_data,
0,
false
);
memcpy(palette, gfx->palette, sizeof(gfx->palette)); memcpy(palette, gfx->palette, sizeof(gfx->palette));
glGenTextures(1, &tex); glGenTextures(1, &tex);
@ -110,10 +118,11 @@ int gsa_main(int argc, char *argv[]) {
GL_FLOAT, GL_FLOAT,
GL_FALSE, GL_FALSE,
sizeof(struct render_vert), sizeof(struct render_vert),
(void *)(2 * sizeof(float)) (void *) (2 * sizeof(float))
); );
glEnableVertexAttribArray(glGetAttribLocation(program._program, "pos")); glEnableVertexAttribArray(glGetAttribLocation(program._program, "pos"));
glEnableVertexAttribArray(glGetAttribLocation(program._program, "tex_coord") glEnableVertexAttribArray(
glGetAttribLocation(program._program, "tex_coord")
); );
glBindVertexArray(0); glBindVertexArray(0);
@ -135,7 +144,7 @@ void gsa_copy_palette_from(sw_color32 *src) {
void gsa_run_loop(bool (*loopfun)()) { void gsa_run_loop(bool (*loopfun)()) {
++loop_stack_i; ++loop_stack_i;
if(loop_stack_i >= GSA_LOOP_STACK_SIZE) { if (loop_stack_i >= GSA_LOOP_STACK_SIZE) {
sw_error("exceeded loop stack size"); sw_error("exceeded loop stack size");
} }
loop_stack[loop_stack_i] = loopfun; loop_stack[loop_stack_i] = loopfun;
@ -154,8 +163,8 @@ static void rect(f32 x, f32 y, f32 w, f32 h, u16 tile, bool half) {
f32 tx, ty, ts; f32 tx, ty, ts;
f32 tilesize = half ? 8.f : 16.f; f32 tilesize = half ? 8.f : 16.f;
tx = ((f32)(tile % 256) * tilesize) / 4096.f; tx = ((f32) (tile % 256) * tilesize) / 4096.f;
ty = ((f32)(u16)(tile / 256) * tilesize) / 4096.f; ty = ((f32) (u16) (tile / 256) * tilesize) / 4096.f;
ts = 1.f / (4096.f / tilesize); ts = 1.f / (4096.f / tilesize);
add_render_vert(x, y, tx, ty); add_render_vert(x, y, tx, ty);
@ -168,7 +177,7 @@ static void rect(f32 x, f32 y, f32 w, f32 h, u16 tile, bool half) {
static void update_palette_gl() { static void update_palette_gl() {
int i; int i;
for(i = 0; i < 256; ++i) { for (i = 0; i < 256; ++i) {
palette_gl[i * 4 + 0] = sw_color32_get_rf(palette[i]); palette_gl[i * 4 + 0] = sw_color32_get_rf(palette[i]);
palette_gl[i * 4 + 1] = sw_color32_get_gf(palette[i]); palette_gl[i * 4 + 1] = sw_color32_get_gf(palette[i]);
palette_gl[i * 4 + 2] = sw_color32_get_bf(palette[i]); palette_gl[i * 4 + 2] = sw_color32_get_bf(palette[i]);
@ -181,17 +190,17 @@ static void gsa_tick() {
gsa_input_tick(); gsa_input_tick();
if(loop_stack_i == -1) { if (loop_stack_i == -1) {
tick(); tick();
} else { } else {
if(!loop_stack[loop_stack_i]()) { if (!loop_stack[loop_stack_i]()) {
--loop_stack_i; --loop_stack_i;
} }
} }
sw_vertex_buffer_clear(vertex_buffer); sw_vertex_buffer_clear(vertex_buffer);
for(i = 0; i < MAX_TILEMAPS; ++i) { for (i = 0; i < MAX_TILEMAPS; ++i) {
tcmult = maps[i].half_tile ? 2 : 1; tcmult = maps[i].half_tile ? 2 : 1;
tilesize = maps[i].half_tile ? 8 : 16; tilesize = maps[i].half_tile ? 8 : 16;
startx = maps[i].scrollx / tilesize; startx = maps[i].scrollx / tilesize;
@ -201,15 +210,15 @@ static void gsa_tick() {
startx = i32_max(0, startx); startx = i32_max(0, startx);
starty = i32_max(0, starty); starty = i32_max(0, starty);
for(x = startx; x <= endx; ++x) { for (x = startx; x <= endx; ++x) {
for(y = starty; y <= endy; ++y) { for (y = starty; y <= endy; ++y) {
u16 tile = maps[i].tiles[x][y]; u16 tile = maps[i].tiles[x][y];
if(tile) { if (tile) {
rect( rect(
(f32)(x * tilesize - maps[i].scrollx), (f32) (x * tilesize - maps[i].scrollx),
(f32)(y * tilesize - maps[i].scrolly), (f32) (y * tilesize - maps[i].scrolly),
(f32)tilesize, (f32) tilesize,
(f32)tilesize, (f32) tilesize,
tile, tile,
maps[i].half_tile maps[i].half_tile
); );
@ -218,11 +227,11 @@ static void gsa_tick() {
} }
} }
for(i = 0; i < MAX_SPRITES; ++i) { for (i = 0; i < MAX_SPRITES; ++i) {
if(sprites[i].tile > 0) { if (sprites[i].tile > 0) {
rect( rect(
(f32)sprites[i].x, (f32) sprites[i].x,
(f32)sprites[i].y, (f32) sprites[i].y,
16, 16,
16, 16,
sprites[i].tile, sprites[i].tile,
@ -246,13 +255,8 @@ static void gsa_tick() {
glUniform4fv( glUniform4fv(
glGetUniformLocation(program._program, "palette"), 256, palette_gl glGetUniformLocation(program._program, "palette"), 256, palette_gl
); );
glBufferData( sw_vertex_buffer_send(vertex_buffer);
GL_ARRAY_BUFFER, glDrawArrays(GL_TRIANGLES, 0, (i32) vertex_buffer->elem_count);
(i64)(sizeof(struct render_vert) * vertex_buffer->elem_count),
vertex_buffer->data,
GL_DYNAMIC_DRAW
);
glDrawArrays(GL_TRIANGLES, 0, (i32)vertex_buffer->elem_count);
glBindVertexArray(0); glBindVertexArray(0);
/* glEnd(); */ /* glEnd(); */
} }

View File

@ -8,10 +8,12 @@
#include "gl.h" #include "gl.h"
struct vert { struct vert {
f32 x, y, z; struct sw_vec2f pos;
struct sw_vec4f color;
}; };
static void r_enter(struct sw_window *win, void *data); static void r_enter(struct sw_window *win, void *data);
static void r_exit(struct sw_window *win, void *data); static void r_exit(struct sw_window *win, void *data);
struct sw_renderer2d *sw_renderer2d_create() { struct sw_renderer2d *sw_renderer2d_create() {
@ -24,29 +26,85 @@ struct sw_renderer2d *sw_renderer2d_create() {
frag = sw_skip_get_string(sw_skip_self, "renderer2d_frag.glsl"); frag = sw_skip_get_string(sw_skip_self, "renderer2d_frag.glsl");
vert = sw_skip_get_string(sw_skip_self, "renderer2d_vert.glsl"); vert = sw_skip_get_string(sw_skip_self, "renderer2d_vert.glsl");
ren->shader = sw_shaderprogram_create((u8 *)vert, 0, (u8 *)frag, 0, 0); ren->shader = sw_shaderprogram_create((u8 *) vert, 0, (u8 *) frag, 0, 0);
ren->vertex_buffer = sw_vertex_buffer_create(sizeof(struct vert));
ren->color = sw_vec4f(1.f, 1.f, 1.f, 1.f);
glGenVertexArrays(1, &ren->vao);
glBindVertexArray(ren->vao);
glGenBuffers(1, &ren->vbo);
glBindBuffer(GL_ARRAY_BUFFER, ren->vbo);
/* todo: vertex attrib thingies */
glVertexAttribPointer(
0, 2, GL_FLOAT, GL_FALSE, sizeof(struct vert), 0
);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
return ren; return ren;
} }
void sw_renderer2d_draw_quad(
struct sw_renderer2d *ren,
struct sw_vec2f a,
struct sw_vec2f b,
struct sw_vec2f c,
struct sw_vec2f d
) {
struct vert vert;
vert.color = ren->color;
vert.pos = a;
sw_vertex_buffer_add(ren->vertex_buffer, &vert);
vert.pos = b;
sw_vertex_buffer_add(ren->vertex_buffer, &vert);
vert.pos = c;
sw_vertex_buffer_add(ren->vertex_buffer, &vert);
vert.pos = b;
sw_vertex_buffer_add(ren->vertex_buffer, &vert);
vert.pos = d;
sw_vertex_buffer_add(ren->vertex_buffer, &vert);
vert.pos = c;
sw_vertex_buffer_add(ren->vertex_buffer, &vert);
}
void sw_renderer2d_set_color(struct sw_renderer2d *ren, struct sw_vec4f color) {
ren->color = color;
}
static void r_enter(struct sw_window *win, void *data) { static void r_enter(struct sw_window *win, void *data) {
struct sw_renderer2d *ren; struct sw_renderer2d *ren;
(void)win; (void) win;
ren = data; ren = data;
sw_vertex_buffer_clear(ren->vertex_buffer);
sw_renderer2d_draw_quad(
ren,
sw_vec2f(-0.5f, -0.5f),
sw_vec2f(0.5f, -0.5f),
sw_vec2f(-0.5f, 0.5f),
sw_vec2f(0.5f, 0.5f));
glBindVertexArray(ren->vao);
sw_shaderprogram_use(ren->shader); sw_shaderprogram_use(ren->shader);
sw_vertex_buffer_send(ren->vertex_buffer);
glBegin(GL_TRIANGLES); glDrawArrays(GL_TRIANGLES, 0, (i32) ren->vertex_buffer->elem_count);
glBindVertexArray(0);
glVertex2i(0, 0);
glVertex2i(1, 0);
glVertex2i(0, 1);
glEnd();
} }
static void r_exit(struct sw_window *win, void *data) { static void r_exit(struct sw_window *win, void *data) {
(void)win; (void) win;
(void)data; (void) data;
} }

View File

@ -1,7 +1,7 @@
#version 460 #version 460
layout(location = 0) in vec3 pos; layout(location = 0) in vec2 pos;
void main() { void main() {
gl_Position = vec4(pos, 1.0); gl_Position = vec4(pos, 0.0, 1.0);
} }

View File

@ -1,6 +1,7 @@
#include "vertex_buffer.h" #include "vertex_buffer.h"
#include "skunkworks.h" #include "skunkworks.h"
#include "gl.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -24,14 +25,14 @@ void sw_vertex_buffer_destroy(struct sw_vertex_buffer *vb) {
} }
void sw_vertex_buffer_add(struct sw_vertex_buffer *vb, void *elem) { void sw_vertex_buffer_add(struct sw_vertex_buffer *vb, void *elem) {
if(vb->elem_count == vb->elem_capacity) { if (vb->elem_count == vb->elem_capacity) {
/* grow capacity... double for now? */ /* grow capacity... double for now? */
vb->elem_capacity *= 2; vb->elem_capacity *= 2;
sw_log("increase vertex buffer size to %i", vb->elem_capacity); sw_log("increase vertex buffer size to %i", vb->elem_capacity);
vb->data = realloc(vb->data, vb->elem_size * vb->elem_capacity); vb->data = realloc(vb->data, vb->elem_size * vb->elem_capacity);
} }
memcpy( memcpy(
(u8 *)vb->data + vb->elem_count * vb->elem_size, elem, vb->elem_size (u8 *) vb->data + vb->elem_count * vb->elem_size, elem, vb->elem_size
); );
++vb->elem_count; ++vb->elem_count;
@ -41,3 +42,12 @@ void sw_vertex_buffer_clear(struct sw_vertex_buffer *vb) {
/* keep capacity */ /* keep capacity */
vb->elem_count = 0; vb->elem_count = 0;
} }
void sw_vertex_buffer_send(struct sw_vertex_buffer *vb) {
glBufferData(
GL_ARRAY_BUFFER,
vb->elem_size * vb->elem_count,
vb->data,
GL_DYNAMIC_DRAW
);
}

View File

@ -1,6 +1,6 @@
prefix=C:/Program Files (x86)/glew prefix=C:/Program Files (x86)/skunkworks_c
exec_prefix=${prefix} exec_prefix=${prefix}
libdir=C:/Program Files (x86)/glew/lib libdir=C:/Program Files (x86)/skunkworks_c/lib
includedir=${prefix}/include includedir=${prefix}/include
Name: glew Name: glew