orca/samples/pong/src/main.c

489 lines
12 KiB
C
Raw Normal View History

2023-07-02 14:24:34 +00:00
#include <math.h>
2023-04-12 14:21:03 +00:00
2023-07-01 19:33:28 +00:00
#include <orca.h>
2023-07-01 17:02:41 +00:00
#define NUM_BLOCKS_PER_ROW 7
#define NUM_BLOCKS 42 // 7 * 6
#define BLOCKS_WIDTH 810.0f
#define BLOCK_HEIGHT 30.0f
#define BLOCKS_PADDING 15.0f
#define BLOCKS_BOTTOM 300.0f
const f32 BLOCK_WIDTH = (BLOCKS_WIDTH - ((NUM_BLOCKS_PER_ROW + 1) * BLOCKS_PADDING)) / NUM_BLOCKS_PER_ROW;
2023-04-12 14:21:03 +00:00
2023-07-01 19:33:28 +00:00
#define PADDLE_MAX_LAUNCH_ANGLE 0.7f
const mg_color paddleColor = { 1, 0, 0, 1 };
mp_rect paddle = { 300, 50, 200, 24 };
2023-04-12 14:21:03 +00:00
const mg_color ballColor = { 1, 1, 0, 1 };
mp_rect ball = { 200, 200, 20, 20 };
2023-07-01 17:02:41 +00:00
vec2 velocity = { 5, 5 };
2023-04-12 14:21:03 +00:00
// This is upside down from how it will actually be drawn.
int blockHealth[NUM_BLOCKS] = {
0, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1,
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
};
2023-04-12 14:21:03 +00:00
vec2 frameSize = { 100, 100 };
2023-04-12 14:21:03 +00:00
bool leftDown = false;
bool rightDown = false;
mg_surface surface;
2023-07-01 17:02:41 +00:00
mg_canvas canvas;
mg_image waterImage;
2023-06-25 13:36:27 +00:00
mg_image ballImage;
mg_image paddleImage;
2023-06-29 01:43:52 +00:00
mg_font pongFont;
2023-05-11 16:31:20 +00:00
2023-07-01 19:33:28 +00:00
f32 lerp(f32 a, f32 b, f32 t);
2023-07-01 17:02:41 +00:00
mp_rect blockRect(int i);
int checkCollision(mp_rect block);
2023-07-04 21:14:14 +00:00
mg_mat2x3 flipY(mp_rect r);
mg_mat2x3 flipYAt(vec2 pos);
2023-07-01 17:02:41 +00:00
str8 loadFile(mem_arena* arena, str8 filename)
{
2023-07-01 17:02:41 +00:00
file_handle file = file_open(filename, FILE_ACCESS_READ, 0);
if(file_last_error(file) != IO_OK)
{
log_error("Couldn't open file %s\n", str8_to_cstring(mem_scratch(), filename));
}
u64 size = file_size(file);
char* buffer = mem_arena_alloc(arena, size);
file_read(file, size, buffer);
file_close(file);
return str8_from_buffer(size, buffer);
}
ORCA_EXPORT void OnInit(void)
2023-04-14 09:48:36 +00:00
{
surface = mg_surface_canvas();
2023-07-01 17:02:41 +00:00
canvas = mg_canvas_create();
waterImage = mg_image_create_from_data(surface, loadFile(mem_scratch(), STR8("/underwater.jpg")), false);
ballImage = mg_image_create_from_data(surface, loadFile(mem_scratch(), STR8("/ball.png")), false);
paddleImage = mg_image_create_from_data(surface, loadFile(mem_scratch(), STR8("/wall.png")), false);
2023-07-05 16:25:14 +00:00
if(mg_image_is_nil(waterImage))
{
log_error("couldn't load water image\n");
2023-07-05 16:25:14 +00:00
}
if(mg_image_is_nil(ballImage))
{
log_error("couldn't load ball image\n");
2023-07-05 16:25:14 +00:00
}
if(mg_image_is_nil(paddleImage))
{
log_error("couldn't load paddle image\n");
2023-07-05 16:25:14 +00:00
}
2023-07-01 17:02:41 +00:00
str8 fontStr = loadFile(mem_scratch(), STR8("/Literata-SemiBoldItalic.ttf"));
unicode_range ranges[5] = { UNICODE_RANGE_BASIC_LATIN,
UNICODE_RANGE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT,
UNICODE_RANGE_LATIN_EXTENDED_A,
UNICODE_RANGE_LATIN_EXTENDED_B,
UNICODE_RANGE_SPECIALS };
2023-07-01 17:02:41 +00:00
// NOTE(ben): Weird that images are "create from data" but fonts are "create from memory"
// TODO: Decide whether we're using strings or explicit pointer + length
pongFont = mg_font_create_from_memory(fontStr.len, (byte*)fontStr.ptr, 5, ranges);
mem_arena_clear(mem_scratch());
2023-04-14 09:48:36 +00:00
}
ORCA_EXPORT void OnFrameResize(u32 width, u32 height)
2023-04-12 14:21:03 +00:00
{
2023-07-01 17:02:41 +00:00
log_info("frame resize %u, %u", width, height);
frameSize.x = width;
frameSize.y = height;
2023-04-12 14:21:03 +00:00
}
ORCA_EXPORT void OnMouseDown(int button)
2023-04-12 14:21:03 +00:00
{
2023-07-01 17:02:41 +00:00
log_info("mouse down!");
2023-04-12 14:21:03 +00:00
}
ORCA_EXPORT void OnKeyDown(int key)
2023-04-12 14:21:03 +00:00
{
This commit restructures the codebase to melt the milepost platform layer into the main orca codebase. Here's a list of commits squashed in this update: - move angle files to ext/ and pull includes/libs from there when needed - remove milepost/ext/angle_headers - Collapsed milepost/ext into ext - Collapse milepost/scripts into scripts/ - collapse milepost/resources into resources/. WARN: this temporarily breaks milepost's native examples - collapse milepost/test into test/ - renamed test/ to tests/ - build milepost directly into build/bin - remove unused GLES and KHR folders from sdk/ - reorganizing milepost directory tree into app, graphics, platfrom, ui, util - collapse milepost/src to src/ - Move all native examples to sketches/ and remove milepost repo - Moving sketches resources into their own shared folder separate from the runtime's resource folder - Moving all binding code to src/wasmbind - Moving all binding code to src/wasmbind - pong: fix typo in error log - fixing log parameter order - add error logs to mg_image_create_* - Fix build scripts on windows - fixed include mistake after rebase - collapsing milepost.{h|c|m} to orca.{h|c|m} and moving from sdk/ to src/ - rename orca_app.h/main.c to runtime.h/c - collapsed sdk/ to src/ - put old sdk files into their respective dirs - renamed canvas_api.json to surface_api.json - moved all stb headers in ext/stb/ - remove unused OpenSansLatinSubset.ttf font - moving cstdlib to src/wasmlibc and removing some duplicates with src/platform - move libc stdarg and string functions from platform/ to wasmlibc/ - rename wasmlibc to libc-shim to reflect non-completeness - Expose abort/assert variadic functions and macros to orca apps, and forward libc-shim abort/assert to these - move Orca API runtime implementations to runtime_xxx - fix missing math constants when including math.h with msvc in graphics_common.c - Change name of runtime to orca_runtime. When bundling on Windows, change name of runtime executable to the name of the app.
2023-08-09 11:06:32 +00:00
if(key == MP_KEY_SPACE)
2023-07-01 17:02:41 +00:00
{
log_error("(this is just for testing errors)");
return;
}
This commit restructures the codebase to melt the milepost platform layer into the main orca codebase. Here's a list of commits squashed in this update: - move angle files to ext/ and pull includes/libs from there when needed - remove milepost/ext/angle_headers - Collapsed milepost/ext into ext - Collapse milepost/scripts into scripts/ - collapse milepost/resources into resources/. WARN: this temporarily breaks milepost's native examples - collapse milepost/test into test/ - renamed test/ to tests/ - build milepost directly into build/bin - remove unused GLES and KHR folders from sdk/ - reorganizing milepost directory tree into app, graphics, platfrom, ui, util - collapse milepost/src to src/ - Move all native examples to sketches/ and remove milepost repo - Moving sketches resources into their own shared folder separate from the runtime's resource folder - Moving all binding code to src/wasmbind - Moving all binding code to src/wasmbind - pong: fix typo in error log - fixing log parameter order - add error logs to mg_image_create_* - Fix build scripts on windows - fixed include mistake after rebase - collapsing milepost.{h|c|m} to orca.{h|c|m} and moving from sdk/ to src/ - rename orca_app.h/main.c to runtime.h/c - collapsed sdk/ to src/ - put old sdk files into their respective dirs - renamed canvas_api.json to surface_api.json - moved all stb headers in ext/stb/ - remove unused OpenSansLatinSubset.ttf font - moving cstdlib to src/wasmlibc and removing some duplicates with src/platform - move libc stdarg and string functions from platform/ to wasmlibc/ - rename wasmlibc to libc-shim to reflect non-completeness - Expose abort/assert variadic functions and macros to orca apps, and forward libc-shim abort/assert to these - move Orca API runtime implementations to runtime_xxx - fix missing math constants when including math.h with msvc in graphics_common.c - Change name of runtime to orca_runtime. When bundling on Windows, change name of runtime executable to the name of the app.
2023-08-09 11:06:32 +00:00
if(key == MP_KEY_ENTER)
2023-07-01 17:02:41 +00:00
{
log_warning("(this is just for testing warning)");
return;
}
log_info("key down: %i", key);
This commit restructures the codebase to melt the milepost platform layer into the main orca codebase. Here's a list of commits squashed in this update: - move angle files to ext/ and pull includes/libs from there when needed - remove milepost/ext/angle_headers - Collapsed milepost/ext into ext - Collapse milepost/scripts into scripts/ - collapse milepost/resources into resources/. WARN: this temporarily breaks milepost's native examples - collapse milepost/test into test/ - renamed test/ to tests/ - build milepost directly into build/bin - remove unused GLES and KHR folders from sdk/ - reorganizing milepost directory tree into app, graphics, platfrom, ui, util - collapse milepost/src to src/ - Move all native examples to sketches/ and remove milepost repo - Moving sketches resources into their own shared folder separate from the runtime's resource folder - Moving all binding code to src/wasmbind - Moving all binding code to src/wasmbind - pong: fix typo in error log - fixing log parameter order - add error logs to mg_image_create_* - Fix build scripts on windows - fixed include mistake after rebase - collapsing milepost.{h|c|m} to orca.{h|c|m} and moving from sdk/ to src/ - rename orca_app.h/main.c to runtime.h/c - collapsed sdk/ to src/ - put old sdk files into their respective dirs - renamed canvas_api.json to surface_api.json - moved all stb headers in ext/stb/ - remove unused OpenSansLatinSubset.ttf font - moving cstdlib to src/wasmlibc and removing some duplicates with src/platform - move libc stdarg and string functions from platform/ to wasmlibc/ - rename wasmlibc to libc-shim to reflect non-completeness - Expose abort/assert variadic functions and macros to orca apps, and forward libc-shim abort/assert to these - move Orca API runtime implementations to runtime_xxx - fix missing math constants when including math.h with msvc in graphics_common.c - Change name of runtime to orca_runtime. When bundling on Windows, change name of runtime executable to the name of the app.
2023-08-09 11:06:32 +00:00
if(key == MP_KEY_LEFT)
2023-07-01 17:02:41 +00:00
{
leftDown = true;
}
This commit restructures the codebase to melt the milepost platform layer into the main orca codebase. Here's a list of commits squashed in this update: - move angle files to ext/ and pull includes/libs from there when needed - remove milepost/ext/angle_headers - Collapsed milepost/ext into ext - Collapse milepost/scripts into scripts/ - collapse milepost/resources into resources/. WARN: this temporarily breaks milepost's native examples - collapse milepost/test into test/ - renamed test/ to tests/ - build milepost directly into build/bin - remove unused GLES and KHR folders from sdk/ - reorganizing milepost directory tree into app, graphics, platfrom, ui, util - collapse milepost/src to src/ - Move all native examples to sketches/ and remove milepost repo - Moving sketches resources into their own shared folder separate from the runtime's resource folder - Moving all binding code to src/wasmbind - Moving all binding code to src/wasmbind - pong: fix typo in error log - fixing log parameter order - add error logs to mg_image_create_* - Fix build scripts on windows - fixed include mistake after rebase - collapsing milepost.{h|c|m} to orca.{h|c|m} and moving from sdk/ to src/ - rename orca_app.h/main.c to runtime.h/c - collapsed sdk/ to src/ - put old sdk files into their respective dirs - renamed canvas_api.json to surface_api.json - moved all stb headers in ext/stb/ - remove unused OpenSansLatinSubset.ttf font - moving cstdlib to src/wasmlibc and removing some duplicates with src/platform - move libc stdarg and string functions from platform/ to wasmlibc/ - rename wasmlibc to libc-shim to reflect non-completeness - Expose abort/assert variadic functions and macros to orca apps, and forward libc-shim abort/assert to these - move Orca API runtime implementations to runtime_xxx - fix missing math constants when including math.h with msvc in graphics_common.c - Change name of runtime to orca_runtime. When bundling on Windows, change name of runtime executable to the name of the app.
2023-08-09 11:06:32 +00:00
if(key == MP_KEY_RIGHT)
2023-07-01 17:02:41 +00:00
{
rightDown = true;
}
2023-04-12 14:21:03 +00:00
}
ORCA_EXPORT void OnKeyUp(int key)
2023-04-12 14:21:03 +00:00
{
This commit restructures the codebase to melt the milepost platform layer into the main orca codebase. Here's a list of commits squashed in this update: - move angle files to ext/ and pull includes/libs from there when needed - remove milepost/ext/angle_headers - Collapsed milepost/ext into ext - Collapse milepost/scripts into scripts/ - collapse milepost/resources into resources/. WARN: this temporarily breaks milepost's native examples - collapse milepost/test into test/ - renamed test/ to tests/ - build milepost directly into build/bin - remove unused GLES and KHR folders from sdk/ - reorganizing milepost directory tree into app, graphics, platfrom, ui, util - collapse milepost/src to src/ - Move all native examples to sketches/ and remove milepost repo - Moving sketches resources into their own shared folder separate from the runtime's resource folder - Moving all binding code to src/wasmbind - Moving all binding code to src/wasmbind - pong: fix typo in error log - fixing log parameter order - add error logs to mg_image_create_* - Fix build scripts on windows - fixed include mistake after rebase - collapsing milepost.{h|c|m} to orca.{h|c|m} and moving from sdk/ to src/ - rename orca_app.h/main.c to runtime.h/c - collapsed sdk/ to src/ - put old sdk files into their respective dirs - renamed canvas_api.json to surface_api.json - moved all stb headers in ext/stb/ - remove unused OpenSansLatinSubset.ttf font - moving cstdlib to src/wasmlibc and removing some duplicates with src/platform - move libc stdarg and string functions from platform/ to wasmlibc/ - rename wasmlibc to libc-shim to reflect non-completeness - Expose abort/assert variadic functions and macros to orca apps, and forward libc-shim abort/assert to these - move Orca API runtime implementations to runtime_xxx - fix missing math constants when including math.h with msvc in graphics_common.c - Change name of runtime to orca_runtime. When bundling on Windows, change name of runtime executable to the name of the app.
2023-08-09 11:06:32 +00:00
if(key == MP_KEY_ENTER || key == MP_KEY_SPACE)
2023-07-01 17:02:41 +00:00
{
return;
}
log_info("key up: %i", key);
This commit restructures the codebase to melt the milepost platform layer into the main orca codebase. Here's a list of commits squashed in this update: - move angle files to ext/ and pull includes/libs from there when needed - remove milepost/ext/angle_headers - Collapsed milepost/ext into ext - Collapse milepost/scripts into scripts/ - collapse milepost/resources into resources/. WARN: this temporarily breaks milepost's native examples - collapse milepost/test into test/ - renamed test/ to tests/ - build milepost directly into build/bin - remove unused GLES and KHR folders from sdk/ - reorganizing milepost directory tree into app, graphics, platfrom, ui, util - collapse milepost/src to src/ - Move all native examples to sketches/ and remove milepost repo - Moving sketches resources into their own shared folder separate from the runtime's resource folder - Moving all binding code to src/wasmbind - Moving all binding code to src/wasmbind - pong: fix typo in error log - fixing log parameter order - add error logs to mg_image_create_* - Fix build scripts on windows - fixed include mistake after rebase - collapsing milepost.{h|c|m} to orca.{h|c|m} and moving from sdk/ to src/ - rename orca_app.h/main.c to runtime.h/c - collapsed sdk/ to src/ - put old sdk files into their respective dirs - renamed canvas_api.json to surface_api.json - moved all stb headers in ext/stb/ - remove unused OpenSansLatinSubset.ttf font - moving cstdlib to src/wasmlibc and removing some duplicates with src/platform - move libc stdarg and string functions from platform/ to wasmlibc/ - rename wasmlibc to libc-shim to reflect non-completeness - Expose abort/assert variadic functions and macros to orca apps, and forward libc-shim abort/assert to these - move Orca API runtime implementations to runtime_xxx - fix missing math constants when including math.h with msvc in graphics_common.c - Change name of runtime to orca_runtime. When bundling on Windows, change name of runtime executable to the name of the app.
2023-08-09 11:06:32 +00:00
if(key == MP_KEY_LEFT)
2023-07-01 17:02:41 +00:00
{
leftDown = false;
}
This commit restructures the codebase to melt the milepost platform layer into the main orca codebase. Here's a list of commits squashed in this update: - move angle files to ext/ and pull includes/libs from there when needed - remove milepost/ext/angle_headers - Collapsed milepost/ext into ext - Collapse milepost/scripts into scripts/ - collapse milepost/resources into resources/. WARN: this temporarily breaks milepost's native examples - collapse milepost/test into test/ - renamed test/ to tests/ - build milepost directly into build/bin - remove unused GLES and KHR folders from sdk/ - reorganizing milepost directory tree into app, graphics, platfrom, ui, util - collapse milepost/src to src/ - Move all native examples to sketches/ and remove milepost repo - Moving sketches resources into their own shared folder separate from the runtime's resource folder - Moving all binding code to src/wasmbind - Moving all binding code to src/wasmbind - pong: fix typo in error log - fixing log parameter order - add error logs to mg_image_create_* - Fix build scripts on windows - fixed include mistake after rebase - collapsing milepost.{h|c|m} to orca.{h|c|m} and moving from sdk/ to src/ - rename orca_app.h/main.c to runtime.h/c - collapsed sdk/ to src/ - put old sdk files into their respective dirs - renamed canvas_api.json to surface_api.json - moved all stb headers in ext/stb/ - remove unused OpenSansLatinSubset.ttf font - moving cstdlib to src/wasmlibc and removing some duplicates with src/platform - move libc stdarg and string functions from platform/ to wasmlibc/ - rename wasmlibc to libc-shim to reflect non-completeness - Expose abort/assert variadic functions and macros to orca apps, and forward libc-shim abort/assert to these - move Orca API runtime implementations to runtime_xxx - fix missing math constants when including math.h with msvc in graphics_common.c - Change name of runtime to orca_runtime. When bundling on Windows, change name of runtime executable to the name of the app.
2023-08-09 11:06:32 +00:00
if(key == MP_KEY_RIGHT)
2023-07-01 17:02:41 +00:00
{
rightDown = false;
}
2023-04-12 14:21:03 +00:00
}
ORCA_EXPORT void OnFrameRefresh(void)
2023-04-12 14:21:03 +00:00
{
f32 aspect = frameSize.x / frameSize.y;
2023-04-12 14:21:03 +00:00
if(leftDown)
{
2023-07-01 17:02:41 +00:00
paddle.x -= 10;
2023-04-12 14:21:03 +00:00
}
else if(rightDown)
{
2023-07-01 17:02:41 +00:00
paddle.x += 10;
2023-04-12 14:21:03 +00:00
}
paddle.x = Clamp(paddle.x, 0, frameSize.x - paddle.w);
ball.x += velocity.x;
ball.y += velocity.y;
ball.x = Clamp(ball.x, 0, frameSize.x - ball.w);
ball.y = Clamp(ball.y, 0, frameSize.y - ball.h);
if(ball.x + ball.w >= frameSize.x)
{
2023-07-01 17:02:41 +00:00
velocity.x = -velocity.x;
2023-04-12 14:21:03 +00:00
}
if(ball.x <= 0)
{
2023-07-01 17:02:41 +00:00
velocity.x = -velocity.x;
2023-04-12 14:21:03 +00:00
}
if(ball.y + ball.h >= frameSize.y)
{
2023-07-01 17:02:41 +00:00
velocity.y = -velocity.y;
2023-04-12 14:21:03 +00:00
}
if(
ball.y <= paddle.y + paddle.h && 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;
2023-07-01 19:33:28 +00:00
f32 launchAngle = lerp(-PADDLE_MAX_LAUNCH_ANGLE, PADDLE_MAX_LAUNCH_ANGLE, t);
f32 speed = sqrtf(velocity.x * velocity.x + velocity.y * velocity.y);
2023-07-01 19:33:28 +00:00
velocity = (vec2){
sinf(launchAngle) * speed,
cosf(launchAngle) * speed,
};
2023-07-01 17:02:41 +00:00
ball.y = paddle.y + paddle.h;
2023-04-20 13:47:18 +00:00
2023-07-01 17:02:41 +00:00
log_info("PONG!");
2023-04-12 14:21:03 +00:00
}
if(ball.y <= 0)
{
ball.x = frameSize.x / 2. - ball.w;
ball.y = frameSize.y / 2. - ball.h;
2023-07-01 17:02:41 +00:00
}
2023-04-14 09:48:36 +00:00
for(int i = 0; i < NUM_BLOCKS; i++)
{
if(blockHealth[i] <= 0)
{
2023-07-01 17:02:41 +00:00
continue;
}
mp_rect r = blockRect(i);
int result = checkCollision(r);
if(result)
{
2023-07-01 17:02:41 +00:00
log_info("Collision! direction=%d", result);
blockHealth[i] -= 1;
f32 vx = velocity.x;
f32 vy = velocity.y;
switch(result)
{
2023-07-01 17:02:41 +00:00
case 1:
case 5:
velocity.y = -vy;
break;
case 3:
case 7:
velocity.x = -vx;
break;
case 2:
case 6:
velocity.x = -vy;
velocity.y = -vx;
break;
case 4:
case 8:
velocity.x = vy;
velocity.y = vx;
break;
}
}
}
2023-04-14 09:48:36 +00:00
2023-07-01 17:02:41 +00:00
mg_canvas_set_current(canvas);
2023-04-14 09:48:36 +00:00
mg_set_color_rgba(10.0f / 255.0f, 31.0f / 255.0f, 72.0f / 255.0f, 1);
2023-07-01 17:02:41 +00:00
mg_clear();
2023-04-14 09:48:36 +00:00
mg_image_draw(waterImage, (mp_rect){ 0, 0, frameSize.x, frameSize.y });
2023-07-02 12:22:12 +00:00
mg_mat2x3 yUp = {
1, 0, 0,
0, -1, frameSize.y
};
2023-04-14 09:48:36 +00:00
mg_matrix_push(yUp);
2023-07-01 17:02:41 +00:00
{
for(int i = 0; i < NUM_BLOCKS; i++)
{
if(blockHealth[i] <= 0)
{
2023-07-01 17:02:41 +00:00
continue;
}
mp_rect r = blockRect(i);
2023-07-04 21:14:14 +00:00
mg_set_color_rgba(0, 0, 0, 0.2);
mg_rounded_rectangle_fill(r.x, r.y - 2, r.w, r.h, 4);
mg_set_color_rgba(0.9, 0.9, 0.9, 1);
mg_rounded_rectangle_fill(r.x, r.y, r.w, r.h, 4);
2023-07-04 21:14:14 +00:00
int fontSize = 18;
str8 text = str8_pushf(mem_scratch(),
"%d", blockHealth[i]);
2023-07-04 21:14:14 +00:00
mp_rect textRect = mg_text_bounding_box(pongFont, fontSize, text);
vec2 textPos = {
r.x + r.w / 2 - textRect.w / 2,
2023-07-04 21:14:14 +00:00
r.y + 9, // TODO: mg_text_bounding_box is returning extremely wack results for height.
};
mg_set_color_rgba(0, 0, 0, 1);
mg_set_font(pongFont);
mg_set_font_size(18);
mg_move_to(textPos.x, textPos.y);
mg_matrix_push(flipYAt(textPos));
{
mg_text_outlines(text);
mg_fill();
}
mg_matrix_pop();
2023-07-01 17:02:41 +00:00
}
2023-07-04 21:14:14 +00:00
mg_set_color_rgba(0.9, 0.9, 0.9, 1);
mg_rounded_rectangle_fill(paddle.x, paddle.y, paddle.w, paddle.h, 4);
mg_matrix_push(flipY(ball));
{
mg_image_draw(ballImage, ball);
}
mg_matrix_pop();
2023-07-01 17:02:41 +00:00
}
mg_matrix_pop();
mg_surface_prepare(surface);
mg_render(surface, canvas);
mg_surface_present(surface);
2023-04-12 14:21:03 +00:00
}
2023-07-01 17:02:41 +00:00
mp_rect blockRect(int i)
{
2023-07-01 17:02:41 +00:00
int row = i / NUM_BLOCKS_PER_ROW;
int col = i % NUM_BLOCKS_PER_ROW;
return (mp_rect){
BLOCKS_PADDING + (BLOCKS_PADDING + BLOCK_WIDTH) * col,
BLOCKS_BOTTOM + (BLOCKS_PADDING + BLOCK_HEIGHT) * row,
BLOCK_WIDTH,
BLOCK_HEIGHT
};
}
// 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.
int checkCollision(mp_rect block)
{
2023-07-01 17:02:41 +00:00
// Note that all the logic for this game has the origin in the bottom left.
f32 ballx2 = ball.x + ball.w;
f32 bally2 = ball.y + ball.h;
f32 blockx2 = block.x + block.w;
f32 blocky2 = block.y + block.h;
if(
ballx2 < block.x || blockx2 < ball.x || bally2 < block.y || blocky2 < ball.y)
{
2023-07-01 17:02:41 +00:00
// Ball is fully outside block
return 0;
}
// if (
// (block.x <= ball.x && ballx2 <= blockx2)
// && (block.y <= ball.y && bally2 <= blocky2)
// ) {
// // Ball is fully inside block; do not consider as a collision
// return 0;
// }
// If moving right, the ball can bounce off its top right corner, right
// side, or bottom right corner. Corner bounces occur if the block's bottom
// left corner is in the ball's top right quadrant, or if the block's top
// left corner is in the ball's bottom left quadrant. Otherwise, an edge
// bounce occurs if the block's left edge falls in either of the ball's
// right quadrants.
//
// This logic generalizes to other directions.
//
// We assume significant tunneling can't happen.
vec2 ballCenter = (vec2){ ball.x + ball.w / 2, ball.y + ball.h / 2 };
vec2 blockCenter = (vec2){ block.x + block.w / 2, block.y + block.h / 2 };
2023-07-01 17:02:41 +00:00
// Moving right
if(velocity.x > 0)
{
2023-07-01 17:02:41 +00:00
// Ball's top right corner
if(
ballCenter.x <= block.x && block.x <= ballx2 && ballCenter.y <= block.y && block.y <= bally2)
{
return 2;
}
2023-07-01 17:02:41 +00:00
// Ball's bottom right corner
if(
ballCenter.x <= block.x && block.x <= ballx2 && ball.y <= blocky2 && blocky2 <= ballCenter.y)
{
return 4;
}
2023-07-01 17:02:41 +00:00
// Ball's right edge
if(
ballCenter.x <= block.x && block.x <= ballx2)
{
return 3;
}
2023-07-01 17:02:41 +00:00
}
// Moving up
if(velocity.y > 0)
{
2023-07-01 17:02:41 +00:00
// Ball's top left corner
if(
ball.x <= blockx2 && blockx2 <= ballCenter.x && ballCenter.y <= block.y && block.y <= bally2)
{
return 8;
}
2023-07-01 17:02:41 +00:00
// Ball's top right corner
if(
ballCenter.x <= block.x && block.x <= ballx2 && ballCenter.y <= block.y && block.y <= bally2)
{
return 2;
}
2023-07-01 17:02:41 +00:00
// Ball's top edge
if(
ballCenter.y <= block.y && block.y <= bally2)
{
return 1;
}
2023-07-01 17:02:41 +00:00
}
// Moving left
if(velocity.x < 0)
{
2023-07-01 17:02:41 +00:00
// Ball's bottom left corner
if(
ball.x <= blockx2 && blockx2 <= ballCenter.x && ball.y <= blocky2 && blocky2 <= ballCenter.y)
{
return 6;
}
2023-07-01 17:02:41 +00:00
// Ball's top left corner
if(
ball.x <= blockx2 && blockx2 <= ballCenter.x && ballCenter.y <= block.y && block.y <= bally2)
{
return 8;
}
2023-07-01 17:02:41 +00:00
// Ball's left edge
if(
ball.x <= blockx2 && blockx2 <= ballCenter.x)
{
return 7;
}
2023-07-01 17:02:41 +00:00
}
// Moving down
if(velocity.y < 0)
{
2023-07-01 17:02:41 +00:00
// Ball's bottom right corner
if(
ballCenter.x <= block.x && block.x <= ballx2 && ball.y <= blocky2 && blocky2 <= ballCenter.y)
{
return 4;
}
2023-07-01 17:02:41 +00:00
// Ball's bottom left corner
if(
ball.x <= blockx2 && blockx2 <= ballCenter.x && ball.y <= blocky2 && blocky2 <= ballCenter.y)
{
return 6;
}
2023-07-01 17:02:41 +00:00
// Ball's bottom edge
if(
ball.y <= blocky2 && blocky2 <= ballCenter.y)
{
return 5;
}
2023-07-01 17:02:41 +00:00
}
return 0;
}
2023-07-01 19:33:28 +00:00
f32 lerp(f32 a, f32 b, f32 t)
{
2023-07-01 19:33:28 +00:00
return (1 - t) * a + t * b;
}
mg_mat2x3 flipY(mp_rect r)
{
2023-07-04 21:14:14 +00:00
return (mg_mat2x3){
1, 0, 0,
0, -1, 2 * r.y + r.h
2023-07-04 21:14:14 +00:00
};
}
mg_mat2x3 flipYAt(vec2 pos)
{
return (mg_mat2x3){
1, 0, 0,
0, -1, 2 * pos.y
};
}