diff --git a/.vscode/settings.json b/.vscode/settings.json index 63e89fd..243599b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,12 @@ "flecs.h": "c", "stdlib.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" } } \ No newline at end of file diff --git a/build.bat b/build.bat index 38b6523..0b8082d 100644 --- a/build.bat +++ b/build.bat @@ -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 \ No newline at end of file +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 \ No newline at end of file diff --git a/debug.bat b/debug.bat index 004b571..14ad243 100644 --- a/debug.bat +++ b/debug.bat @@ -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 \ No newline at end of file +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 \ No newline at end of file diff --git a/src/main.c b/src/main.c index 438eedf..c28d08f 100644 --- a/src/main.c +++ b/src/main.c @@ -3,22 +3,52 @@ // Includes Sokol GFX, Sokol GP and Sokol APP, doing all implementations. #define SOKOL_IMPL #define SOKOL_GLES3 -#define FLECS_CUSTOM_BUILD + #include "sokol_gfx.h" #include "sokol_gp.h" #include "sokol_app.h" #include "sokol_glue.h" +#include "sokol_log.h" +#include "util/sokol_memtrack.h" -#include "flecs.c" +#include +#include +#include +#include -#include // for fprintf() -#include // for exit() -#include // for sinf() and cosf() +#define ALLOC(arg) smemtrack_alloc(arg, NULL) +#define FREE(arg) smemtrack_free(arg, NULL) -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. -static void frame(void) { +static void frame(void* _userdata) +{ + userdata_t* userdata = (userdata_t*) _userdata; // Get current window size. int width = sapp_width(), height = sapp_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). 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. - sgp_set_color(0.1f, 0.1f, 0.1f, 1.0f); + sgp_set_color(0.0f, 0.0f, 0.0f, 1.0f); sgp_clear(); // Draw an animated rectangle that rotates and changes its colors. - float time = sapp_frame_count() * sapp_frame_duration(); - float r = sinf(time)*0.5+0.5, g = cosf(time)*0.5+0.5; - sgp_set_color(r, g, 0.3f, 1.0f); - sgp_rotate_at(time, 0.0f, 0.0f); - sgp_draw_filled_rect(-0.5f, -0.5f, 1.0f, 1.0f); + /*float time = sapp_frame_count() * sapp_frame_duration();*/ + sgp_state* state = sgp_query_state(); + state->thickness = 0.01f; + sgp_set_color(0.5f, 0.6f, 0.3f, 1.0f); // Begin a render pass. sg_pass pass = {.swapchain = sglue_swapchain()}; @@ -55,7 +87,10 @@ static void frame(void) { } // Called when the application is initializing. -static void init(void) { +static void init(void* _userdata) +{ + userdata_t* userdata = (userdata_t*) _userdata; + // Initialize Sokol GFX. sg_desc sgdesc = { .environment = sglue_environment(), @@ -65,35 +100,84 @@ static void init(void) { fprintf(stderr, "Failed to create Sokol GFX context!\n"); exit(-1); } - + // Initialize Sokol GP, adjust the size of command buffers for your own use. - sgp_desc sgpdesc = {0}; + sgp_desc sgpdesc = { 0 }; sgp_setup(&sgpdesc); if (!sgp_is_valid()) { fprintf(stderr, "Failed to create Sokol GP context: %s\n", sgp_get_error_message(sgp_get_last_error())); exit(-1); } - - world = ecs_init(); } // Called when the application is shutting down. -static void cleanup(void) { - ecs_fini(world); +static void cleanup(void* _userdata) +{ + userdata_t* userdata = (userdata_t*) _userdata; + + FREE(userdata->cells); + FREE(userdata); // Cleanup Sokol GP and Sokol GFX resources. sgp_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. -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)argv; return (sapp_desc){ - .init_cb = init, - .frame_cb = frame, - .cleanup_cb = cleanup, + .user_data = userdata, + .init_userdata_cb = init, + .frame_userdata_cb = frame, + .cleanup_userdata_cb = cleanup, + .event_userdata_cb = event, .window_title = "Rectangle (Sokol GP)", + .allocator = { + .alloc_fn = smemtrack_alloc, + .free_fn = smemtrack_free, + }, }; } \ No newline at end of file