Merge pull request 'clang-format' (#39) from clang-format into main

Reviewed-on: #39
This commit is contained in:
MartinFouilleul 2023-08-18 15:59:40 +00:00
commit e0db03b51c
2 changed files with 140 additions and 112 deletions

14
.clang-format Normal file
View File

@ -0,0 +1,14 @@
AllowAllArgumentsOnNextLine: false
BreakBeforeBraces: Allman
ColumnLimit: 0
Cpp11BracedListStyle: false
IndentPPDirectives: BeforeHash
IndentWidth: 4
LineEnding: LF
MaxEmptyLinesToKeep: 1
PointerAlignment: Left
SeparateDefinitionBlocks: Always
SpaceBeforeParens: Never
TabWidth: 4
UseTab: Never
ReflowComments: false

View File

@ -28,7 +28,7 @@ int blockHealth[NUM_BLOCKS] = {
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
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
}; };
oc_vec2 frameSize = { 100, 100 }; oc_vec2 frameSize = { 100, 100 };
@ -49,7 +49,8 @@ int checkCollision(oc_rect block);
oc_mat2x3 flipY(oc_rect r); oc_mat2x3 flipY(oc_rect r);
oc_mat2x3 flipYAt(oc_vec2 pos); oc_mat2x3 flipYAt(oc_vec2 pos);
oc_str8 loadFile(oc_arena* arena, oc_str8 filename) { oc_str8 loadFile(oc_arena* arena, oc_str8 filename)
{
oc_file file = oc_file_open(filename, OC_FILE_ACCESS_READ, 0); oc_file file = oc_file_open(filename, OC_FILE_ACCESS_READ, 0);
if(oc_file_last_error(file) != OC_IO_OK) if(oc_file_last_error(file) != OC_IO_OK)
{ {
@ -170,22 +171,22 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
ball.x = oc_clamp(ball.x, 0, frameSize.x - ball.w); ball.x = oc_clamp(ball.x, 0, frameSize.x - ball.w);
ball.y = oc_clamp(ball.y, 0, frameSize.y - ball.h); ball.y = oc_clamp(ball.y, 0, frameSize.y - ball.h);
if (ball.x + ball.w >= frameSize.x) { if(ball.x + ball.w >= frameSize.x)
{
velocity.x = -velocity.x; velocity.x = -velocity.x;
} }
if (ball.x <= 0) { if(ball.x <= 0)
{
velocity.x = -velocity.x; velocity.x = -velocity.x;
} }
if (ball.y + ball.h >= frameSize.y) { if(ball.y + ball.h >= frameSize.y)
{
velocity.y = -velocity.y; velocity.y = -velocity.y;
} }
if( if(
ball.y <= paddle.y + paddle.h ball.y <= paddle.y + paddle.h && ball.x + ball.w >= paddle.x && ball.x <= paddle.x + paddle.w && velocity.y < 0)
&& ball.x+ball.w >= paddle.x {
&& ball.x <= paddle.x + paddle.w
&& velocity.y < 0
) {
f32 t = ((ball.x + ball.w / 2) - paddle.x) / paddle.w; f32 t = ((ball.x + ball.w / 2) - paddle.x) / paddle.w;
f32 launchAngle = lerp(-PADDLE_MAX_LAUNCH_ANGLE, PADDLE_MAX_LAUNCH_ANGLE, t); f32 launchAngle = lerp(-PADDLE_MAX_LAUNCH_ANGLE, PADDLE_MAX_LAUNCH_ANGLE, t);
f32 speed = sqrtf(velocity.x * velocity.x + velocity.y * velocity.y); f32 speed = sqrtf(velocity.x * velocity.x + velocity.y * velocity.y);
@ -198,26 +199,31 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
oc_log_info("PONG!"); oc_log_info("PONG!");
} }
if (ball.y <= 0) { if(ball.y <= 0)
{
ball.x = frameSize.x / 2. - ball.w; ball.x = frameSize.x / 2. - ball.w;
ball.y = frameSize.y / 2. - ball.h; ball.y = frameSize.y / 2. - ball.h;
} }
for (int i = 0; i < NUM_BLOCKS; i++) { for(int i = 0; i < NUM_BLOCKS; i++)
if (blockHealth[i] <= 0) { {
if(blockHealth[i] <= 0)
{
continue; continue;
} }
oc_rect r = blockRect(i); oc_rect r = blockRect(i);
int result = checkCollision(r); int result = checkCollision(r);
if (result) { if(result)
{
oc_log_info("Collision! direction=%d", result); oc_log_info("Collision! direction=%d", result);
blockHealth[i] -= 1; blockHealth[i] -= 1;
f32 vx = velocity.x; f32 vx = velocity.x;
f32 vy = velocity.y; f32 vy = velocity.y;
switch (result) { switch(result)
{
case 1: case 1:
case 5: case 5:
velocity.y = -vy; velocity.y = -vy;
@ -238,7 +244,6 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
break; break;
} }
} }
} }
oc_canvas_set_current(canvas); oc_canvas_set_current(canvas);
@ -250,13 +255,15 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
oc_mat2x3 yUp = { oc_mat2x3 yUp = {
1, 0, 0, 1, 0, 0,
0, -1, frameSize.y, 0, -1, frameSize.y
}; };
oc_matrix_push(yUp); oc_matrix_push(yUp);
{ {
for (int i = 0; i < NUM_BLOCKS; i++) { for(int i = 0; i < NUM_BLOCKS; i++)
if (blockHealth[i] <= 0) { {
if(blockHealth[i] <= 0)
{
continue; continue;
} }
@ -268,8 +275,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
int fontSize = 18; int fontSize = 18;
oc_str8 text = oc_str8_pushf(oc_scratch(), oc_str8 text = oc_str8_pushf(oc_scratch(),
"%d", blockHealth[i] "%d", blockHealth[i]);
);
oc_rect textRect = oc_text_bounding_box(pongFont, fontSize, text); oc_rect textRect = oc_text_bounding_box(pongFont, fontSize, text);
oc_vec2 textPos = { oc_vec2 textPos = {
@ -305,7 +311,8 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
oc_surface_present(surface); oc_surface_present(surface);
} }
oc_rect blockRect(int i) { oc_rect blockRect(int i)
{
int row = i / NUM_BLOCKS_PER_ROW; int row = i / NUM_BLOCKS_PER_ROW;
int col = i % NUM_BLOCKS_PER_ROW; int col = i % NUM_BLOCKS_PER_ROW;
return (oc_rect){ return (oc_rect){
@ -318,7 +325,8 @@ oc_rect blockRect(int i) {
// Returns a cardinal direction 1-8 for the collision with the block, or zero // Returns a cardinal direction 1-8 for the collision with the block, or zero
// if no collision. 1 is straight up and directions proceed clockwise. // if no collision. 1 is straight up and directions proceed clockwise.
int checkCollision(oc_rect block) { int checkCollision(oc_rect block)
{
// Note that all the logic for this game has the origin in the bottom left. // Note that all the logic for this game has the origin in the bottom left.
f32 ballx2 = ball.x + ball.w; f32 ballx2 = ball.x + ball.w;
@ -326,20 +334,15 @@ int checkCollision(oc_rect block) {
f32 blockx2 = block.x + block.w; f32 blockx2 = block.x + block.w;
f32 blocky2 = block.y + block.h; f32 blocky2 = block.y + block.h;
if ( if(ballx2 < block.x || blockx2 < ball.x || bally2 < block.y || blocky2 < ball.y)
ballx2 < block.x {
|| blockx2 < ball.x
|| bally2 < block.y
|| blocky2 < ball.y
) {
// Ball is fully outside block // Ball is fully outside block
return 0; return 0;
} }
// if ( // if ((block.x <= ball.x && ballx2 <= blockx2)
// (block.x <= ball.x && ballx2 <= blockx2) // && (block.y <= ball.y && bally2 <= blocky2))
// && (block.y <= ball.y && bally2 <= blocky2) // {
// ) {
// // Ball is fully inside block; do not consider as a collision // // Ball is fully inside block; do not consider as a collision
// return 0; // return 0;
// } // }
@ -359,102 +362,113 @@ int checkCollision(oc_rect block) {
oc_vec2 blockCenter = (oc_vec2){ block.x + block.w / 2, block.y + block.h / 2 }; oc_vec2 blockCenter = (oc_vec2){ block.x + block.w / 2, block.y + block.h / 2 };
// Moving right // Moving right
if (velocity.x > 0) { if(velocity.x > 0)
{
// Ball's top right corner // Ball's top right corner
if ( if(ballCenter.x <= block.x && block.x <= ballx2 && ballCenter.y <= block.y && block.y <= bally2)
ballCenter.x <= block.x && block.x <= ballx2 {
&& ballCenter.y <= block.y && block.y <= bally2 return 2;
) { return 2; } }
// Ball's bottom right corner // Ball's bottom right corner
if ( if(ballCenter.x <= block.x && block.x <= ballx2 && ball.y <= blocky2 && blocky2 <= ballCenter.y)
ballCenter.x <= block.x && block.x <= ballx2 {
&& ball.y <= blocky2 && blocky2 <= ballCenter.y return 4;
) { return 4; } }
// Ball's right edge // Ball's right edge
if ( if(ballCenter.x <= block.x && block.x <= ballx2)
ballCenter.x <= block.x && block.x <= ballx2 {
) { return 3; } return 3;
}
} }
// Moving up // Moving up
if (velocity.y > 0) { if(velocity.y > 0)
{
// Ball's top left corner // Ball's top left corner
if ( if(ball.x <= blockx2 && blockx2 <= ballCenter.x && ballCenter.y <= block.y && block.y <= bally2)
ball.x <= blockx2 && blockx2 <= ballCenter.x {
&& ballCenter.y <= block.y && block.y <= bally2 return 8;
) { return 8; } }
// Ball's top right corner // Ball's top right corner
if ( if(ballCenter.x <= block.x && block.x <= ballx2 && ballCenter.y <= block.y && block.y <= bally2)
ballCenter.x <= block.x && block.x <= ballx2 {
&& ballCenter.y <= block.y && block.y <= bally2 return 2;
) { return 2; } }
// Ball's top edge // Ball's top edge
if ( if(ballCenter.y <= block.y && block.y <= bally2)
ballCenter.y <= block.y && block.y <= bally2 {
) { return 1; } return 1;
}
} }
// Moving left // Moving left
if (velocity.x < 0) { if(velocity.x < 0)
{
// Ball's bottom left corner // Ball's bottom left corner
if ( if(ball.x <= blockx2 && blockx2 <= ballCenter.x && ball.y <= blocky2 && blocky2 <= ballCenter.y)
ball.x <= blockx2 && blockx2 <= ballCenter.x {
&& ball.y <= blocky2 && blocky2 <= ballCenter.y return 6;
) { return 6; } }
// Ball's top left corner // Ball's top left corner
if ( if(ball.x <= blockx2 && blockx2 <= ballCenter.x && ballCenter.y <= block.y && block.y <= bally2)
ball.x <= blockx2 && blockx2 <= ballCenter.x {
&& ballCenter.y <= block.y && block.y <= bally2 return 8;
) { return 8; } }
// Ball's left edge // Ball's left edge
if ( if(ball.x <= blockx2 && blockx2 <= ballCenter.x)
ball.x <= blockx2 && blockx2 <= ballCenter.x {
) { return 7; } return 7;
}
} }
// Moving down // Moving down
if (velocity.y < 0) { if(velocity.y < 0)
{
// Ball's bottom right corner // Ball's bottom right corner
if ( if(ballCenter.x <= block.x && block.x <= ballx2 && ball.y <= blocky2 && blocky2 <= ballCenter.y)
ballCenter.x <= block.x && block.x <= ballx2 {
&& ball.y <= blocky2 && blocky2 <= ballCenter.y return 4;
) { return 4; } }
// Ball's bottom left corner // Ball's bottom left corner
if ( if(ball.x <= blockx2 && blockx2 <= ballCenter.x && ball.y <= blocky2 && blocky2 <= ballCenter.y)
ball.x <= blockx2 && blockx2 <= ballCenter.x {
&& ball.y <= blocky2 && blocky2 <= ballCenter.y return 6;
) { return 6; } }
// Ball's bottom edge // Ball's bottom edge
if ( if(ball.y <= blocky2 && blocky2 <= ballCenter.y)
ball.y <= blocky2 && blocky2 <= ballCenter.y {
) { return 5; } return 5;
}
} }
return 0; return 0;
} }
f32 lerp(f32 a, f32 b, f32 t) { f32 lerp(f32 a, f32 b, f32 t)
{
return (1 - t) * a + t * b; return (1 - t) * a + t * b;
} }
oc_mat2x3 flipY(oc_rect r) { oc_mat2x3 flipY(oc_rect r)
{
return (oc_mat2x3){ return (oc_mat2x3){
1, 0, 0, 1, 0, 0,
0, -1, 2 * r.y + r.h, 0, -1, 2 * r.y + r.h
}; };
} }
oc_mat2x3 flipYAt(oc_vec2 pos) { oc_mat2x3 flipYAt(oc_vec2 pos)
{
return (oc_mat2x3){ return (oc_mat2x3){
1, 0, 0, 1, 0, 0,
0, -1, 2 * pos.y, 0, -1, 2 * pos.y
}; };
} }