diff options
| author | Arslaan Pathan <[email protected]> | 2026-08-13 22:14:39 +1200 |
|---|---|---|
| committer | Arslaan Pathan <[email protected]> | 2026-08-13 22:14:39 +1200 |
| commit | c2fc44dc13732eda5bfa4a2743173fb81d1ed1ff (patch) | |
| tree | dfb653159cafef8ab95e48700f741459225757a6 /jni/main.c | |
| parent | 93d033a3c6734cf92c32b2219bbb9f8e3aea55bf (diff) | |
| download | moku-assistant-c2fc44dc13732eda5bfa4a2743173fb81d1ed1ff.tar.xz moku-assistant-c2fc44dc13732eda5bfa4a2743173fb81d1ed1ff.zip | |
Add proper drawing functions, finish boilerplate application
Diffstat (limited to 'jni/main.c')
| -rw-r--r-- | jni/main.c | 91 |
1 files changed, 78 insertions, 13 deletions
@@ -36,14 +36,21 @@ struct engine { }; struct engine *engine_new(struct android_app* app) { - struct engine engine; - - memset(&engine, 0, sizeof(engine)); - engine.window_initialized = 0; - engine.app = app; - engine.running = 0; - engine.color = 0; - engine.last_update = (uint64_t)0; + struct engine *engine; + + engine = malloc(sizeof(struct engine)); + if (!engine) { + LOGE("Failed to allocate engine!"); + return NULL; + } + + engine->window_initialized = 0; + engine->app = app; + engine->running = 0; + engine->color = COLOR_RED; + engine->last_update = (uint64_t)0; + + return engine; } uint64_t get_timestamp_ns() { @@ -54,10 +61,59 @@ uint64_t get_timestamp_ns() { static void engine_schedule_tick(struct engine *engine); +static void engine_draw_frame(struct engine *engine) { + ANativeWindow_Buffer buffer; + int x, y; + size_t pixel_idx; + + if (!engine->app->window) return; + + if (ANativeWindow_lock(engine->app->window, &buffer, NULL) < 0) { + LOGE("Unable to lock window buffer"); + return; + } + + if (!engine->window_initialized) { + return; + } + + for (y = 0; y < buffer.height; y++) { + for (x = 0; x < buffer.width; x++) { + pixel_idx = y * buffer.stride + x; + ((uint32_t*)(buffer.bits))[pixel_idx] = (uint32_t)engine->color; + } + } + + ANativeWindow_unlockAndPost(engine->app->window); +} + +static void engine_update(struct engine *engine) { + uint64_t now = get_timestamp_ns(); + if (now - engine->last_update > 1000000000LL) { + switch (engine->color) { + case COLOR_RED: + engine->color = COLOR_GREEN; + break; + case COLOR_GREEN: + engine->color = COLOR_BLUE; + break; + case COLOR_BLUE: + engine->color = COLOR_RED; + break; + default: + LOGE("unexpected color %08x", (uint32_t)engine->color); + } + engine->last_update = now; + } +} + static void engine_real_tick(struct engine *engine) { if (!engine->running) return; + LOGI("tick"); engine_schedule_tick(engine); + engine_update(engine); + engine_draw_frame(engine); } static void engine_tick(long frameTimeNanos, void *data) { @@ -97,12 +153,14 @@ void engine_resume(struct engine *engine) { if (!engine->running) { engine->running = 1; engine_schedule_tick(engine); + LOGI("engine running"); } }; -void engine_pause(struct engine *engine) { - +void engine_pause(struct engine *engine) { + engine->running = 0; + LOGI("engine paused"); }; static void engine_handle_cmd(struct android_app *app, int32_t cmd) { @@ -129,14 +187,19 @@ static void engine_handle_cmd(struct android_app *app, int32_t cmd) { void android_main(struct android_app *state) { /* Declarations */ struct android_poll_source *source; - struct engine engine; + struct engine *engine; int events; /* Code starts here */ app_dummy(); - memset(&engine, 0, sizeof(engine)); - state->userData = &engine; + engine = engine_new(state); + if (!engine) { + LOGE("Failed to create engine!"); + return; + } + + state->userData = engine; state->onAppCmd = engine_handle_cmd; /* do stuff here */ @@ -149,4 +212,6 @@ void android_main(struct android_app *state) { } /* more stuff here */ } + + free(engine); } |
