huh/src/main.c

141 lines
2.7 KiB
C

#include <stdio.h>
#include "base.c"
#include "arena.c"
#define SOKOL_IMPL
#ifdef PLATFORM_WINDOWS
#define SOKOL_D3D11
#elifdef PLATFORM_MACOS
#define SOKOL_METAL
#elifdef PLATFORM_LINUX
#define SOKOL_GLES3
#else
#error "unsupported platform"
#endif
#include "sokol/sokol_app.h"
#include "sokol/sokol_gfx.h"
#include "sokol/sokol_glue.h"
#include "sokol/sokol_log.h"
#include "sokol/util/sokol_debugtext.h"
sg_pass_action pass_action;
static void
init(void) {
sg_setup(&(sg_desc){
.environment = sglue_environment(),
.logger.func = slog_func,
});
pass_action = (sg_pass_action) {
.colors[0] = {
.load_action = SG_LOADACTION_CLEAR,
.clear_value = { 1.0f, 0.0f, 0.0f, 1.0f }
}
};
sdtx_setup(&(sdtx_desc_t){
.fonts = {
[0] = sdtx_font_kc853(),
[1] = sdtx_font_kc854(),
[2] = sdtx_font_z1013(),
[3] = sdtx_font_cpc(),
[4] = sdtx_font_c64(),
[5] = sdtx_font_oric(),
},
});
// c23 tests
capture auto x = 0;
auto inc = closure(^{
x += 1;
});
printf("x = %d (before)\n", x);
for (ureg i = 0; i < 10; i += 1) {
inc();
}
printf("x = %d (after)\n", x);
auto arena = Logger(Linear(1024 * 1024));
scope_exit { arena_release(arena); };
sreg* a = arena_new(arena, sreg);
sreg* b = arena_new(arena, sreg);
sreg* c = arena_new(arena, sreg);
*a = 1024;
*b = 2048;
*c = 4096;
printf("%zu (%p)\n", *a, a);
printf("%zu (%p)\n", *b, b);
printf("%zu (%p)\n", *c, c);
arena_reset(arena);
c = arena_new(arena, sreg);
b = arena_new(arena, sreg);
a = arena_new(arena, sreg);
printf("%zu (%p)\n", *a, a);
printf("%zu (%p)\n", *b, b);
printf("%zu (%p)\n", *c, c);
}
static void
frame(void) {
sg_begin_pass(&(sg_pass){
.action.colors[0] = {
.load_action = SG_LOADACTION_CLEAR,
.clear_value = { 0.10f, 0.10f, 0.10f, 1.0f },
},
.swapchain = sglue_swapchain(),
});
sdtx_origin(0, 0);
sdtx_canvas(sapp_widthf(), sapp_heightf());
sdtx_font(3);
sdtx_pos(1, 1);
sdtx_color3b(0, 0xFF, 0);
sdtx_puts("press escape to quit");
sdtx_draw();
sg_end_pass();
sg_commit();
}
static void
cleanup(void) {
sdtx_shutdown();
sg_shutdown();
}
static void
event(const sapp_event* event) {
if (event->type == SAPP_EVENTTYPE_KEY_DOWN) {
if (event->key_code == SAPP_KEYCODE_ESCAPE) {
sapp_request_quit();
}
}
}
sapp_desc
sokol_main(int argc, char* argv[]) {
(void)argc;
(void)argv;
return (sapp_desc) {
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.event_cb = event,
.logger.func = slog_func,
.width = 400,
.height = 300,
.window_title = "hello, world",
};
}