aboutsummaryrefslogtreecommitdiff
path: root/jni/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'jni/main.c')
-rw-r--r--jni/main.c91
1 files changed, 78 insertions, 13 deletions
diff --git a/jni/main.c b/jni/main.c
index 12ed679..7983d19 100644
--- a/jni/main.c
+++ b/jni/main.c
@@ -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);
}