diff --git a/samples/pong/src/main.c b/samples/pong/src/main.c index 3de6b0d..c734807 100644 --- a/samples/pong/src/main.c +++ b/samples/pong/src/main.c @@ -4,6 +4,7 @@ #define NUM_BLOCKS_PER_ROW 7 #define NUM_BLOCKS 42 // 7 * 6 +#define NUM_BLOCKS_TO_WIN (NUM_BLOCKS - 2) #define BLOCKS_WIDTH 810.0f #define BLOCK_HEIGHT 30.0f @@ -30,6 +31,7 @@ int blockHealth[NUM_BLOCKS] = { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }; +int score = 0; oc_vec2 frameSize = { 100, 100 }; @@ -98,6 +100,18 @@ ORCA_EXPORT void oc_on_init(void) oc_arena_clear(oc_scratch()); } +ORCA_EXPORT void oc_on_terminate(void) +{ + if(score == NUM_BLOCKS_TO_WIN) + { + oc_log_info("you win!\n"); + } + else + { + oc_log_info("goodbye world!\n"); + } +} + ORCA_EXPORT void oc_on_resize(u32 width, u32 height) { oc_log_info("frame resize %u, %u", width, height); @@ -219,6 +233,11 @@ ORCA_EXPORT void oc_on_frame_refresh(void) oc_log_info("Collision! direction=%d", result); blockHealth[i] -= 1; + if(blockHealth[i] == 0) + { + ++score; + } + f32 vx = velocity.x; f32 vy = velocity.y; @@ -246,6 +265,11 @@ ORCA_EXPORT void oc_on_frame_refresh(void) } } + if(score == NUM_BLOCKS_TO_WIN) + { + oc_request_quit(); + } + oc_canvas_set_current(canvas); oc_set_color_rgba(10.0f / 255.0f, 31.0f / 255.0f, 72.0f / 255.0f, 1); @@ -303,6 +327,20 @@ ORCA_EXPORT void oc_on_frame_refresh(void) oc_image_draw(ballImage, ball); } oc_matrix_pop(); + + // draw score text + { + oc_move_to(10, 10); + oc_str8 text = oc_str8_pushf(oc_scratch(), "Destroy all %d blocks to win! Current score: %d", NUM_BLOCKS_TO_WIN, score); + oc_rect textRect = oc_text_bounding_box(pongFont, 20, text); + oc_vec2 textPos = { 10, 10 }; + oc_matrix_push(flipYAt(textPos)); + { + oc_text_outlines(text); + oc_fill(); + } + oc_matrix_pop(); + } } oc_matrix_pop(); diff --git a/src/app/app.h b/src/app/app.h index 297165a..a6a4617 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -400,6 +400,10 @@ extern "C" ORCA_API int oc_directory_create(oc_str8 path); +#else + + void ORCA_IMPORT(oc_request_quit)(void); + #endif // !defined(OC_PLATFORM_ORCA) || !(OC_PLATFORM_ORCA) #ifdef __cplusplus diff --git a/src/runtime.c b/src/runtime.c index 3104420..5ee8118 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -796,6 +796,15 @@ i32 orca_runloop(void* user) oc_arena_clear(oc_scratch()); } + if(exports[OC_EXPORT_TERMINATE]) + { + M3Result res = m3_Call(exports[OC_EXPORT_TERMINATE], 0, 0); + if(res) + { + ORCA_WASM3_ABORT(app->runtime.m3Runtime, res, "Runtime error"); + } + } + return (0); } diff --git a/src/runtime.h b/src/runtime.h index aa7bcec..92302e4 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -26,7 +26,8 @@ X(OC_EXPORT_KEY_UP, "oc_on_key_up", "", "i") \ X(OC_EXPORT_FRAME_REFRESH, "oc_on_frame_refresh", "", "") \ X(OC_EXPORT_FRAME_RESIZE, "oc_on_resize", "", "ii") \ - X(OC_EXPORT_RAW_EVENT, "oc_on_raw_event", "", "i") + X(OC_EXPORT_RAW_EVENT, "oc_on_raw_event", "", "i") \ + X(OC_EXPORT_TERMINATE, "oc_on_terminate", "", "") typedef enum { diff --git a/src/wasmbind/core_api.json b/src/wasmbind/core_api.json index 1e4a0ea..9ffc568 100644 --- a/src/wasmbind/core_api.json +++ b/src/wasmbind/core_api.json @@ -63,5 +63,11 @@ "cname": "oc_get_host_platform", "ret": {"name": "int", "tag": "i"}, "args": [] +}, +{ + "name": "oc_request_quit", + "cname": "oc_request_quit", + "ret": {"name": "void", "tag": "v"}, + "args": [] } ]