pong: demo oc_request_quit() and oc_on_terminate()

* gamestate now records your score (total blocks destroyed)
* when all blocks are destroyed, quit
* print a message when the app is terminated
This commit is contained in:
Reuben Dunnington 2023-08-21 15:41:10 -07:00 committed by MartinFouilleul
parent 0973e26150
commit f4a82cd381
1 changed files with 38 additions and 0 deletions

View File

@ -4,6 +4,7 @@
#define NUM_BLOCKS_PER_ROW 7 #define NUM_BLOCKS_PER_ROW 7
#define NUM_BLOCKS 42 // 7 * 6 #define NUM_BLOCKS 42 // 7 * 6
#define NUM_BLOCKS_TO_WIN (NUM_BLOCKS - 2)
#define BLOCKS_WIDTH 810.0f #define BLOCKS_WIDTH 810.0f
#define BLOCK_HEIGHT 30.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,
3, 3, 3, 3, 3, 3, 3 3, 3, 3, 3, 3, 3, 3
}; };
int score = 0;
oc_vec2 frameSize = { 100, 100 }; oc_vec2 frameSize = { 100, 100 };
@ -98,6 +100,18 @@ ORCA_EXPORT void oc_on_init(void)
oc_arena_clear(oc_scratch()); 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) ORCA_EXPORT void oc_on_resize(u32 width, u32 height)
{ {
oc_log_info("frame resize %u, %u", width, 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); oc_log_info("Collision! direction=%d", result);
blockHealth[i] -= 1; blockHealth[i] -= 1;
if(blockHealth[i] == 0)
{
++score;
}
f32 vx = velocity.x; f32 vx = velocity.x;
f32 vy = velocity.y; 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_canvas_set_current(canvas);
oc_set_color_rgba(10.0f / 255.0f, 31.0f / 255.0f, 72.0f / 255.0f, 1); 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_image_draw(ballImage, ball);
} }
oc_matrix_pop(); 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(); oc_matrix_pop();