libmach: update API, exposes init, update, and deinit functions

This commit is contained in:
Zachary Huang 2022-07-19 00:31:29 -04:00 committed by Stephen Gutekanst
parent d194dafb79
commit 77aecbe806
6 changed files with 166 additions and 138 deletions

View file

@ -1,40 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef void mach_core_callback(void*);
typedef void resize_callback(void*, uint32_t, uint32_t);
// `libmach` exported API bindings
void mach_core_set_init(mach_core_callback);
void mach_core_set_update(mach_core_callback);
void mach_core_set_deinit(mach_core_callback);
void mach_run(void);
void core_set_should_close(void*);
float core_delta_time(void*);
void* mach_init_core(void);
void mach_deinit(void*);
void mach_set_should_close(void*);
bool mach_window_should_close(void*);
int mach_update(void*, resize_callback);
float mach_delta_time(void*);
void resize_fn(void* core, uint32_t width, uint32_t height) {
printf("Resize callback: %u %u\n", width, height);
}
static float elapsed = 0;
void my_init(void* core) {
printf("My init!\n");
}
void my_update(void* core) {
float dt = core_delta_time(core);
if (elapsed < 1.0) {
elapsed += dt;
} else {
core_set_should_close(core);
}
printf("My update! total time = %f\n", elapsed);
}
void my_deinit(void* core) {
printf("My deinit!\n");
}
int main() {
mach_core_set_init(my_init);
mach_core_set_update(my_update);
mach_core_set_deinit(my_deinit);
mach_run();
void* core = mach_init_core();
if (core == 0) {
printf("Error instantiating mach core\n");
return 0;
}
while (!mach_window_should_close(core)) {
if (mach_update(core, resize_fn) == 0) {
printf("Error updating Mach\n");
break;
};
elapsed += mach_delta_time(core);
if (elapsed > 5.0) {
mach_set_should_close(core);
}
// printf("Elapsed: %f\n", elapsed);
}
mach_deinit(core);
return 0;
}