added dynamic sw_vertex_buffer and changed gsa to use it

This commit is contained in:
DaniTheSkunk 2023-03-09 16:37:51 +00:00
parent 2150ea5769
commit 0e02a3466e
4 changed files with 78 additions and 22 deletions

20
include/vertex_buffer.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef GUARD_B3E8DB670A75ACCB1082192FB0485037
#define GUARD_B3E8DB670A75ACCB1082192FB0485037
#include "types.h"
struct sw_vertex_buffer {
u32 elem_size;
u32 elem_count;
u32 elem_capacity;
void *data;
};
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);
#endif /* GUARD_B3E8DB670A75ACCB1082192FB0485037 */

View File

@ -6,6 +6,7 @@
#include "shader.h"
#include "skip.h"
#include "types.h"
#include "vertex_buffer.h"
#include "window.h"
#include <gl/gl.h>
#include <string.h>
@ -39,6 +40,8 @@ static u32 tex;
static bool (*loop_stack[GSA_LOOP_STACK_SIZE])();
static i8 loop_stack_i;
static struct sw_vertex_buffer *vertex_buffer;
int gsa_main(int argc, char *argv[]) {
(void)argc;
(void)argv;
@ -53,6 +56,8 @@ int gsa_main(int argc, char *argv[]) {
data = sw_skip_get(sw_skip_self, "gfx.png", &size);
_gfx = sw_image8_load_png_data(data, size);
vertex_buffer = sw_vertex_buffer_create(sizeof(struct render_vert));
memset(loop_stack, 0, sizeof(loop_stack));
loop_stack_i = -1;
@ -137,11 +142,12 @@ void gsa_run_loop(bool (*loopfun)()) {
}
static void add_render_vert(f32 x, f32 y, f32 tx, f32 ty) {
render_verts[next_render_vert].x = x;
render_verts[next_render_vert].y = y;
render_verts[next_render_vert].tx = tx;
render_verts[next_render_vert].ty = ty;
++next_render_vert;
struct render_vert v;
v.x = x;
v.y = y;
v.tx = tx;
v.ty = ty;
sw_vertex_buffer_add(vertex_buffer, &v);
}
static void rect(f32 x, f32 y, f32 w, f32 h, i16 tile, bool half) {
@ -191,7 +197,7 @@ void _gsa_tick() {
}
}
next_render_vert = 0;
sw_vertex_buffer_clear(vertex_buffer);
for(i = 0; i < MAX_TILEMAPS; ++i) {
tcmult = maps[i].half_tile ? 2 : 1;
@ -243,11 +249,11 @@ void _gsa_tick() {
);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(struct render_vert) * (next_render_vert),
render_verts,
sizeof(struct render_vert) * vertex_buffer->elem_count,
vertex_buffer->data,
GL_DYNAMIC_DRAW
);
glDrawArrays(GL_TRIANGLES, 0, next_render_vert);
glDrawArrays(GL_TRIANGLES, 0, vertex_buffer->elem_count);
glBindVertexArray(0);
/* glEnd(); */
}

View File

@ -1,13 +0,0 @@
/***************************************************************************/
/* ___________ __ __ */
/* / _____/ | ____ __ ____ | | ____ _ _____________| | __ ______ */
/* \_____ \| |/ / | \/ \| |/ /\ \/ \/ / _ \_ __ \ |/ / / ___/ */
/* / \ <| | / | \ < \ ( <_> ) | \/ < \___ \ */
/* /_______ /__|_ \____/|___| /__|_ \ \/\_/ \____/|__| |__|_ \/____ > */
/* \/ \/ \/ \/ \/ \/ */
/***************************************************************************/
#ifndef GUARD_BAEE408B8F416CA55711305ABAF9CE6C
#define GUARD_BAEE408B8F416CA55711305ABAF9CE6C
#endif /* GUARD_BAEE408B8F416CA55711305ABAF9CE6C */

43
src/vertex_buffer.c Normal file
View File

@ -0,0 +1,43 @@
#include "vertex_buffer.h"
#include "skunkworks.h"
#include <stdlib.h>
#include <string.h>
struct sw_vertex_buffer *sw_vertex_buffer_create(u32 elem_size) {
struct sw_vertex_buffer *vb;
vb = malloc(sizeof(struct sw_vertex_buffer));
/* initial capacity... 8? dunno :P */
vb->elem_capacity = 8;
vb->data = malloc(elem_size * vb->elem_capacity);
vb->elem_count = 0;
vb->elem_size = elem_size;
return vb;
}
void sw_vertex_buffer_destroy(struct sw_vertex_buffer *vb) {
free(vb->data);
free(vb);
}
void sw_vertex_buffer_add(struct sw_vertex_buffer *vb, void *elem) {
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
);
++vb->elem_count;
}
void sw_vertex_buffer_clear(struct sw_vertex_buffer *vb) {
/* keep capacity */
vb->elem_count = 0;
}