Load images in pong example

This commit is contained in:
Martin Fouilleul 2023-06-25 15:36:27 +02:00
parent 4175efb7a5
commit d0ad92f0b8
6 changed files with 59 additions and 25 deletions

@ -1 +1 @@
Subproject commit bc0388714821eea6dd8a4e72bc24f2a899e9123f
Subproject commit 4b491ee94a034d6a5a7d90a6f1a822a3d76b53f0

View File

@ -21,6 +21,6 @@ wasmFlags="--target=wasm32 \
-D__ORCA__ \
-isystem ../../cstdlib/include -I ../../sdk -I../../milepost/ext -I ../../milepost -I ../../milepost/src -I ../../milepost/src/util -I ../../milepost/src/platform -I../.."
$CLANG $wasmFlags -o ./module.wasm ../../cstdlib/src/*.c ../../sdk/orca.c src/main.c
$CLANG $wasmFlags -o ./module.wasm ../../sdk/orca.c ../../cstdlib/src/*.c src/main.c
python3 ../../scripts/mkapp.py --orca-dir ../.. --name Pong --icon icon.png --data-file data/ball.png module.wasm
python3 ../../scripts/mkapp.py --orca-dir ../.. --name Pong --icon icon.png --data-file data/ball.png --data-file data/wall.png module.wasm

BIN
samples/pong/data/wall.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 314 KiB

View File

@ -32,7 +32,8 @@ bool rightDown = false;
mg_canvas canvas;
mg_surface surface;
mg_image image;
mg_image ballImage;
mg_image paddleImage;
mg_surface mg_surface_main(void);
@ -42,27 +43,41 @@ ORCA_EXPORT void OnInit(void)
surface = mg_surface_main();
canvas = mg_canvas_create();
//NOTE: file test
file_handle file = file_open(STR8("/ball.png"), FILE_ACCESS_READ, 0);
if(file_last_error(file) != IO_OK)
log_info("try allocating\n");
char* foo = malloc(1024);
free(foo);
log_info("allocated and freed 1024 bytes\n");
//NOTE: load ball texture
{
log_error("Couldn't open file ball.png\n");
file_handle file = file_open(STR8("/ball.png"), FILE_ACCESS_READ, 0);
if(file_last_error(file) != IO_OK)
{
log_error("Couldn't open file ball.png\n");
}
u64 size = file_size(file);
char* buffer = mem_arena_alloc(mem_scratch(), size);
file_read(file, size, buffer);
file_close(file);
ballImage = mg_image_create_from_data(surface, str8_from_buffer(size, buffer), false);
}
u64 size = file_size(file);
char* buffer = mem_arena_alloc(mem_scratch(), size);
file_read(file, size, buffer);
file_close(file);
image = mg_image_create_from_data(surface, str8_from_buffer(size, buffer), false);
file = file_open(STR8("/test.txt"), FILE_ACCESS_WRITE, FILE_OPEN_CREATE);
if(file_last_error(file) != IO_OK)
//NOTE: load paddle texture
{
log_error("Couldn't open/create file test.txt\n");
file_handle file = file_open(STR8("/wall.png"), FILE_ACCESS_READ, 0);
if(file_last_error(file) != IO_OK)
{
log_error("Couldn't open file wall.png\n");
}
u64 size = file_size(file);
char* buffer = mem_arena_alloc(mem_scratch(), size);
file_read(file, size, buffer);
file_close(file);
paddleImage = mg_image_create_from_data(surface, str8_from_buffer(size, buffer), false);
}
str8 test_string = STR8("Hello, world\n");
file_write(file, test_string.len, test_string.ptr);
file_close(file);
mem_arena_clear(mem_scratch());
}
@ -180,12 +195,17 @@ ORCA_EXPORT void OnFrameRefresh(void)
mg_matrix_push(transform);
mg_image_draw(paddleImage, paddle);
/*
mg_set_color(paddleColor);
mg_rectangle_fill(paddle.x, paddle.y, paddle.w, paddle.h);
*/
mg_image_draw(image, ball);
// mg_set_color(ballColor);
// mg_circle_fill(ball.x+ball.w/2, ball.y + ball.w/2, ball.w/2.);
mg_image_draw(ballImage, ball);
/*
mg_set_color(ballColor);
mg_circle_fill(ball.x+ball.w/2, ball.y + ball.w/2, ball.w/2.);
*/
mg_matrix_pop();

View File

@ -6,8 +6,8 @@
*
*****************************************************************/
#include"platform/orca_malloc.c"
#include"platform/orca_memory.c"
#include"platform/orca_malloc.c"
#include"platform/orca_strings.c"
#include"platform/orca_log.c"
#include"platform/platform_io_common.c"

View File

@ -456,7 +456,21 @@ void* orca_runloop(void* user)
//NOTE: call init handler
if(eventHandlers[G_EVENT_START])
{
m3_Call(eventHandlers[G_EVENT_START], 0, 0);
M3Result err = m3_Call(eventHandlers[G_EVENT_START], 0, 0);
if(err != NULL)
{
log_error("runtime error: %s\n", err);
str8 msg = str8_pushf(mem_scratch(), "Runtime error: %s\n", err);
const char* options[] = {"OK"};
mp_alert_popup("Error",
msg.ptr,
1,
options);
mp_request_quit();
return((void*)-1);
}
}
if(eventHandlers[G_EVENT_FRAME_RESIZE])