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>
<option name="HEADER_GUARD_STYLE_PATTERN" value="GUARD_${UUID}" />
</Objective-C>
<clangFormatSettings>
<option name="ENABLED" value="true" />
</clangFormatSettings>
<files>
<extensions>
<pair source="c" header="h" fileNamingConvention="SNAKE_CASE" />
<pair source="cu" header="cuh" fileNamingConvention="NONE" />
<pair source="ixx" header="" fileNamingConvention="NONE" />
<pair source="mxx" header="" fileNamingConvention="NONE" />
<pair source="cppm" header="" fileNamingConvention="NONE" />
<pair source="ccm" header="" fileNamingConvention="NONE" />
<pair source="cxxm" header="" fileNamingConvention="NONE" />
<pair source="c++m" header="" fileNamingConvention="NONE" />
</extensions>
</files>
<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>
<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>
</component>

View File

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

View File

@ -2,12 +2,19 @@
#define GUARD_2B56918289975E8AA8711D6E080ED4B9
#include "shader.h"
#include "types.h"
#include "vec2f.h"
#include "vec4f.h"
#include "vertex_buffer.h"
struct sw_renderer;
struct sw_renderer2d {
struct sw_shaderprogram shader;
struct sw_renderer *renderer;
struct sw_vertex_buffer *vertex_buffer;
struct sw_vec4f color;
u32 vao, vbo;
};
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 line
- 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 */

View File

@ -11,10 +11,14 @@ struct sw_vertex_buffer {
};
struct sw_vertex_buffer *sw_vertex_buffer_create(u32 elem_size);
void sw_vertex_buffer_destroy(struct sw_vertex_buffer *vb);
/* copies elem data */
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_send(struct sw_vertex_buffer *vb);
#endif /* GUARD_B3E8DB670A75ACCB1082192FB0485037 */

View File

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

View File

@ -8,10 +8,12 @@
#include "gl.h"
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_exit(struct sw_window *win, void *data);
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");
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;
}
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) {
struct sw_renderer2d *ren;
(void)win;
(void) win;
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);
glBegin(GL_TRIANGLES);
glVertex2i(0, 0);
glVertex2i(1, 0);
glVertex2i(0, 1);
glEnd();
sw_vertex_buffer_send(ren->vertex_buffer);
glDrawArrays(GL_TRIANGLES, 0, (i32) ren->vertex_buffer->elem_count);
glBindVertexArray(0);
}
static void r_exit(struct sw_window *win, void *data) {
(void)win;
(void)data;
(void) win;
(void) data;
}

View File

@ -1,7 +1,7 @@
#version 460
layout(location = 0) in vec3 pos;
layout(location = 0) in vec2 pos;
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 "skunkworks.h"
#include "gl.h"
#include <stdlib.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) {
if(vb->elem_count == vb->elem_capacity) {
if (vb->elem_count == vb->elem_capacity) {
/* grow capacity... double for now? */
vb->elem_capacity *= 2;
sw_log("increase vertex buffer size to %i", vb->elem_capacity);
vb->data = realloc(vb->data, vb->elem_size * vb->elem_capacity);
}
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;
@ -41,3 +42,12 @@ void sw_vertex_buffer_clear(struct sw_vertex_buffer *vb) {
/* keep capacity */
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}
libdir=C:/Program Files (x86)/glew/lib
libdir=C:/Program Files (x86)/skunkworks_c/lib
includedir=${prefix}/include
Name: glew