Testing + First event listener

This commit is contained in:
Clément Pons 2025-03-26 17:38:37 +01:00
parent 5f74472055
commit 7798c762ae
4 changed files with 117 additions and 27 deletions

View File

@ -13,6 +13,12 @@
"flecs.h": "c", "flecs.h": "c",
"stdlib.h": "c", "stdlib.h": "c",
"math.h": "c", "math.h": "c",
"stdio.h": "c" "stdio.h": "c",
"sokol_memtrack.h": "c",
"type_traits": "cpp",
"limits": "cpp",
"ratio": "cpp",
"sokol_log.h": "c",
"syslog.h": "c"
} }
} }

View File

@ -1 +1 @@
emcc -O3 src/main.c -o app.html -sUSE_WEBGL2 -sWASM_BIGINT -pthread -I../sokol -I../sokol_gp -I../flecs/distr --shell-file=shell.html -sFILESYSTEM=0 emcc -O3 src/main.c -o app.html -sUSE_WEBGL2 -sWASM_BIGINT -sALLOW_MEMORY_GROWTH -pthread -I../sokol -I../sokol_gp -I../flecs/distr --shell-file=shell.html -sFILESYSTEM=0

View File

@ -1 +1 @@
emcc src/main.c -o app.html -sUSE_WEBGL2 -sASSERTIONS -sWASM_BIGINT -pthread -I../sokol -I../sokol_gp -I../flecs/distr --shell-file=shell.html -sFILESYSTEM=0 -g emcc src/main.c -o app.html -sUSE_WEBGL2 -sASSERTIONS -sWASM_BIGINT -sALLOW_MEMORY_GROWTH -pthread -I../sokol -I../sokol_gp -I../flecs/distr --shell-file=shell.html -sFILESYSTEM=0 -g

View File

@ -3,22 +3,52 @@
// Includes Sokol GFX, Sokol GP and Sokol APP, doing all implementations. // Includes Sokol GFX, Sokol GP and Sokol APP, doing all implementations.
#define SOKOL_IMPL #define SOKOL_IMPL
#define SOKOL_GLES3 #define SOKOL_GLES3
#define FLECS_CUSTOM_BUILD
#include "sokol_gfx.h" #include "sokol_gfx.h"
#include "sokol_gp.h" #include "sokol_gp.h"
#include "sokol_app.h" #include "sokol_app.h"
#include "sokol_glue.h" #include "sokol_glue.h"
#include "sokol_log.h"
#include "util/sokol_memtrack.h"
#include "flecs.c" #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h> // for fprintf() #define ALLOC(arg) smemtrack_alloc(arg, NULL)
#include <stdlib.h> // for exit() #define FREE(arg) smemtrack_free(arg, NULL)
#include <math.h> // for sinf() and cosf()
ecs_world_t *world; void js_log(int severity, const char* format, ...)
{
char buffer[_SLOG_LINE_LENGTH];
va_list va;
va_start(va, format);
int size = vsnprintf(buffer, _SLOG_LINE_LENGTH, format, va);
va_end(va);
slog_js_log(severity, buffer);
}
typedef struct position_t
{
float x, y;
} position_t;
typedef struct userdata_t
{
position_t* cells;
position_t pan;
float zoom;
sg_pipeline* pipeline;
} userdata_t;
// Called on every frame of the application. // Called on every frame of the application.
static void frame(void) { static void frame(void* _userdata)
{
userdata_t* userdata = (userdata_t*) _userdata;
// Get current window size. // Get current window size.
int width = sapp_width(), height = sapp_height(); int width = sapp_width(), height = sapp_height();
float ratio = width/(float)height; float ratio = width/(float)height;
@ -30,16 +60,18 @@ static void frame(void) {
// Set drawing coordinate space to (left=-ratio, right=ratio, top=1, bottom=-1). // Set drawing coordinate space to (left=-ratio, right=ratio, top=1, bottom=-1).
sgp_project(-ratio, ratio, 1.0f, -1.0f); sgp_project(-ratio, ratio, 1.0f, -1.0f);
sgp_translate(userdata->pan.x, userdata->pan.y);
sgp_scale(userdata->zoom, userdata->zoom);
// Clear the frame buffer. // Clear the frame buffer.
sgp_set_color(0.1f, 0.1f, 0.1f, 1.0f); sgp_set_color(0.0f, 0.0f, 0.0f, 1.0f);
sgp_clear(); sgp_clear();
// Draw an animated rectangle that rotates and changes its colors. // Draw an animated rectangle that rotates and changes its colors.
float time = sapp_frame_count() * sapp_frame_duration(); /*float time = sapp_frame_count() * sapp_frame_duration();*/
float r = sinf(time)*0.5+0.5, g = cosf(time)*0.5+0.5; sgp_state* state = sgp_query_state();
sgp_set_color(r, g, 0.3f, 1.0f); state->thickness = 0.01f;
sgp_rotate_at(time, 0.0f, 0.0f); sgp_set_color(0.5f, 0.6f, 0.3f, 1.0f);
sgp_draw_filled_rect(-0.5f, -0.5f, 1.0f, 1.0f);
// Begin a render pass. // Begin a render pass.
sg_pass pass = {.swapchain = sglue_swapchain()}; sg_pass pass = {.swapchain = sglue_swapchain()};
@ -55,7 +87,10 @@ static void frame(void) {
} }
// Called when the application is initializing. // Called when the application is initializing.
static void init(void) { static void init(void* _userdata)
{
userdata_t* userdata = (userdata_t*) _userdata;
// Initialize Sokol GFX. // Initialize Sokol GFX.
sg_desc sgdesc = { sg_desc sgdesc = {
.environment = sglue_environment(), .environment = sglue_environment(),
@ -67,33 +102,82 @@ static void init(void) {
} }
// Initialize Sokol GP, adjust the size of command buffers for your own use. // Initialize Sokol GP, adjust the size of command buffers for your own use.
sgp_desc sgpdesc = {0}; sgp_desc sgpdesc = { 0 };
sgp_setup(&sgpdesc); sgp_setup(&sgpdesc);
if (!sgp_is_valid()) { if (!sgp_is_valid()) {
fprintf(stderr, "Failed to create Sokol GP context: %s\n", sgp_get_error_message(sgp_get_last_error())); fprintf(stderr, "Failed to create Sokol GP context: %s\n", sgp_get_error_message(sgp_get_last_error()));
exit(-1); exit(-1);
} }
world = ecs_init();
} }
// Called when the application is shutting down. // Called when the application is shutting down.
static void cleanup(void) { static void cleanup(void* _userdata)
ecs_fini(world); {
userdata_t* userdata = (userdata_t*) _userdata;
FREE(userdata->cells);
FREE(userdata);
// Cleanup Sokol GP and Sokol GFX resources. // Cleanup Sokol GP and Sokol GFX resources.
sgp_shutdown(); sgp_shutdown();
sg_shutdown(); sg_shutdown();
smemtrack_info_t info = smemtrack_info();
//info.num_allocs
}
static void event(const sapp_event* event, void* _userdata)
{
userdata_t* userdata = (userdata_t*) _userdata;
switch(event->type)
{
case SAPP_EVENTTYPE_MOUSE_DOWN:
break;
case SAPP_EVENTTYPE_MOUSE_UP:
break;
case SAPP_EVENTTYPE_MOUSE_MOVE:
break;
case SAPP_EVENTTYPE_MOUSE_SCROLL:
if((userdata->zoom >= 3.0f && event->scroll_y > 0.0f) || (userdata->zoom <= 0.1f && event->scroll_y < 0.0f))
return;
const float diff = expf(event->scroll_y * 0.01f);
const float width = sapp_widthf(), height = sapp_heightf();
userdata->pan.x = userdata->pan.x - (event->mouse_x / (diff * userdata->zoom) - event->mouse_x / userdata->zoom);
userdata->pan.y = userdata->pan.y - (event->mouse_x / (diff * userdata->zoom) - event->mouse_x / userdata->zoom);
userdata->zoom = _sg_clamp(userdata->zoom * diff, 0.1f, 3.0f);
js_log(3, "Zoom: %f", userdata->zoom);
break;
default:
break;
}
} }
// Implement application main through Sokol APP. // Implement application main through Sokol APP.
sapp_desc sokol_main(int argc, char* argv[]) { sapp_desc sokol_main(int argc, char* argv[])
{
userdata_t* userdata = (userdata_t*) ALLOC(sizeof(userdata_t));
userdata->pan = (position_t) { .x = 0, .y = 0 };
userdata->zoom = 1;
(void)argc; (void)argc;
(void)argv; (void)argv;
return (sapp_desc){ return (sapp_desc){
.init_cb = init, .user_data = userdata,
.frame_cb = frame, .init_userdata_cb = init,
.cleanup_cb = cleanup, .frame_userdata_cb = frame,
.cleanup_userdata_cb = cleanup,
.event_userdata_cb = event,
.window_title = "Rectangle (Sokol GP)", .window_title = "Rectangle (Sokol GP)",
.allocator = {
.alloc_fn = smemtrack_alloc,
.free_fn = smemtrack_free,
},
}; };
} }