added renderer system and ported gsa to it

This commit is contained in:
DaniTheSkunk 2023-03-08 18:38:58 +00:00
parent 42d712a26b
commit 2150ea5769
8 changed files with 89 additions and 30 deletions

View File

@ -1,5 +1,5 @@
@ECHO OFF
cl build.c src/str.c src/types.c /Iinclude && del *.obj && build.exe && del build.exe && build\gsa_simple.exe
cl build.c src/str.c src/types.c /Iinclude /nologo && del *.obj && build.exe && del build.exe && build\gsa_simple.exe
EXIT /B %ERRORLEVEL%

View File

@ -19,25 +19,8 @@ void fade_palette(f32 amount) {
bool tick_fade_in();
void test() {
struct sw_skip *skip;
u32 len;
u8 *str;
skip = sw_skip_load("build/gsa.exe");
str = sw_skip_get(skip, "test.c", &len);
sw_log("%i", str);
sw_log("%.*s", len, str);
}
void init() {
i32 x, y;
char *a, *b;
sw_str_split2("hellonyaworld", "nya", &a, &b);
printf("%s - %s\n", a, b);
// test();
gsa_copy_palette_to(pal);

20
include/renderer.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef GUARD_EAE42F57582CDD6C49D6C5F8B0CD994B
#define GUARD_EAE42F57582CDD6C49D6C5F8B0CD994B
struct sw_window;
typedef void (*sw_renderer_cb)(struct sw_window *, void *);
typedef void (*sw_render_cb)();
struct sw_renderer {
void *data;
sw_renderer_cb enter;
sw_renderer_cb exit;
sw_render_cb render;
};
struct sw_renderer *
sw_renderer_create(void *data, sw_renderer_cb enter, sw_renderer_cb exit);
void sw_renderer_destroy(struct sw_renderer *renderer);
#endif /* GUARD_EAE42F57582CDD6C49D6C5F8B0CD994B */

View File

@ -2,6 +2,7 @@
#define GUARD_F247452E0BF1EC9CD9131C2A6FD281CA
#include "framebuffer.h"
#include "renderer.h"
#include "types.h"
#include "vec2i.h"
@ -14,10 +15,16 @@ struct sw_window {
struct GLFWwindow *_window;
struct sw_framebuffer *_scaler_fb;
struct sw_renderer **_renderers;
u32 _renderer_count;
};
struct sw_window *sw_window_create(struct sw_vec2i size, char *title);
void sw_window_run(struct sw_window *window, void (*callback)());
void sw_window_run(struct sw_window *window);
void sw_window_add_renderer(
struct sw_window *window, struct sw_renderer *renderer, sw_render_cb cb
);
#endif /* GUARD_F247452E0BF1EC9CD9131C2A6FD281CA */

View File

@ -2,9 +2,11 @@
#include "error.h"
#include "gl.h"
#include "image8.h"
#include "renderer.h"
#include "shader.h"
#include "skip.h"
#include "types.h"
#include "window.h"
#include <gl/gl.h>
#include <string.h>
#define GSA_NOMAIN
@ -112,7 +114,8 @@ int gsa_main(int argc, char *argv[]) {
sw_log("gsa setup done");
init();
sw_window_run(_win, _gsa_tick);
sw_window_add_renderer(_win, sw_renderer_create(0, 0, 0), _gsa_tick);
sw_window_run(_win);
return 0;
}

21
src/renderer.c Normal file
View File

@ -0,0 +1,21 @@
#include "renderer.h"
#include <stdlib.h>
struct sw_renderer *
sw_renderer_create(void *data, sw_renderer_cb enter, sw_renderer_cb exit) {
struct sw_renderer *renderer;
renderer = malloc(sizeof(struct sw_renderer));
renderer->data = data;
renderer->enter = enter;
renderer->exit = exit;
renderer->render = 0;
return renderer;
}
void sw_renderer_destroy(struct sw_renderer *renderer) {
free(renderer);
}

View File

@ -1,7 +1,3 @@
#include "renderer2d.h"
static char *shader_vert = "\
\
";
struct sw_renderer2d *sw_renderer2d_create(){};

View File

@ -14,7 +14,6 @@
static void _tick();
static struct sw_window *_tick_window;
static void (*_tick_fn)();
static void win_callback(GLFWwindow *glfw_win, i32 width, i32 height);
#ifndef __EMSCRIPTEN__
@ -32,12 +31,13 @@ static void GLAPIENTRY gldebug(
struct sw_window *sw_window_create(struct sw_vec2i size, char *title) {
struct sw_window *win;
sw_init();
win = malloc(sizeof(struct sw_window));
win->window_size = size;
win->game_size = size;
win->_renderer_count = 0;
win->_renderers = 0;
sw_log("init glfw");
glfwInit();
glfwDefaultWindowHints();
@ -70,10 +70,9 @@ struct sw_window *sw_window_create(struct sw_vec2i size, char *title) {
return win;
}
void sw_window_run(struct sw_window *window, void (*callback)()) {
void sw_window_run(struct sw_window *window) {
sw_log("run window");
_tick_window = window;
_tick_fn = callback;
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(_tick, 60, 1);
#else
@ -83,13 +82,43 @@ void sw_window_run(struct sw_window *window, void (*callback)()) {
#endif
}
void sw_window_add_renderer(
struct sw_window *window, struct sw_renderer *renderer, sw_render_cb cb
) {
window->_renderer_count += 1;
renderer->render = cb;
window->_renderers = realloc(
window->_renderers,
sizeof(struct sw_renderer *) * window->_renderer_count
);
window->_renderers[window->_renderer_count - 1] = renderer;
}
/* private functions */
void _tick() {
u32 i;
sw_gamepad_tick();
glViewport(0, 0, _tick_window->game_size.x, _tick_window->game_size.y);
glBindFramebuffer(GL_FRAMEBUFFER, _tick_window->_scaler_fb->_fb);
_tick_fn();
for(i = 0; i < _tick_window->_renderer_count; ++i) {
struct sw_renderer *renderer;
renderer = _tick_window->_renderers[i];
if(renderer->enter) {
renderer->enter(_tick_window, renderer->data);
}
if(renderer->render) {
renderer->render();
}
if(renderer->exit) {
renderer->exit(_tick_window, renderer->data);
}
}
glViewport(0, 0, _tick_window->window_size.x, _tick_window->window_size.y);
glBindFramebuffer(GL_FRAMEBUFFER, 0);