get the bird to draw.

the bird now draws! i also got a font loaded too.
This commit is contained in:
Fries 2024-06-21 00:45:28 -07:00
parent 9f1b00d39d
commit 3362687710
12 changed files with 152 additions and 82 deletions

2
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,2 @@
{
}

BIN
data/PixelOperator.ttf Normal file

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 425 B

After

Width:  |  Height:  |  Size: 4.4 KiB

5
jsconfig.json Normal file
View file

@ -0,0 +1,5 @@
{
"compilerOptions": {
"target": "ESNext"
}
}

View file

@ -12,7 +12,7 @@ add_custom_command(
COMMENT "Creating data_generated.c from the data directory" COMMENT "Creating data_generated.c from the data directory"
) )
add_executable(WiiDuktape main.c grrlib_duk.c game.c data_generated.c) add_executable(WiiDuktape main.c grrlib_duk.c wii_duk.c game.c data_generated.c)
target_link_libraries(WiiDuktape duktape) target_link_libraries(WiiDuktape duktape)
target_link_libraries(WiiDuktape GRRLIB) target_link_libraries(WiiDuktape GRRLIB)
ogc_create_dol(WiiDuktape) ogc_create_dol(WiiDuktape)

View file

@ -1,3 +1,11 @@
/**
* returns an RGBA buffer
* @param {number} r red
* @param {number} g green
* @param {number} b blue
* @param {number} a alpha
* @returns {RGBA}
*/
function rgba(r, g, b, a) { function rgba(r, g, b, a) {
const buffer = Uint8Array.allocPlain(4); const buffer = Uint8Array.allocPlain(4);
buffer[0] = r; buffer[0] = r;
@ -21,7 +29,11 @@ function rgba_compare(rgba1, rgba2) {
} }
globalThis.rectangle_color = RED_COLOR; globalThis.rectangle_color = RED_COLOR;
/** @type {GRRLibTexture} */
globalThis.background = null; globalThis.background = null;
/** @type {GRRLibTexture} */
globalThis.bird = null;
function swap_colors() { function swap_colors() {
if (rgba_compare(rectangle_color, RED_COLOR)) { if (rgba_compare(rectangle_color, RED_COLOR)) {
@ -53,7 +65,6 @@ function update() {
} }
wii.grrlib.fill_screen(WHITE_COLOR); wii.grrlib.fill_screen(WHITE_COLOR);
// wii.grrlib.rectangle(0, 0, 320, 240, rectangle_color, true); wii.grrlib.draw_img(0, 0, background, 0, 1, 1, WHITE_COLOR);
wii.grrlib.draw_img(320, 0, background, 0, 1, 1, WHITE_COLOR);
wii.grrlib.draw_img(0, 0, bird, 0, 1, 1, WHITE_COLOR); wii.grrlib.draw_img(0, 0, bird, 0, 1, 1, WHITE_COLOR);
} }

View file

@ -45,6 +45,15 @@ static duk_ret_t grrlib_load_texture(duk_context *ctx) {
GRRLIB_texImg* texture = GRRLIB_LoadTexture(file_ptr); GRRLIB_texImg* texture = GRRLIB_LoadTexture(file_ptr);
if (texture->data == NULL) {
duk_push_string(ctx, "filename");
duk_get_prop(ctx, 0);
const char * filename = duk_to_string(ctx, -1);
duk_pop(ctx);
return duk_error(ctx, DUK_ERR_ERROR, "An error has occured while trying load the texture for the file %s.", filename);
}
duk_push_pointer(ctx, texture); duk_push_pointer(ctx, texture);
return 1; return 1;

View file

@ -1,94 +1,42 @@
#include <grrlib.h> #include <grrlib.h>
#include <duktape.h> #include <duktape.h>
#include <stdint.h> #include <stdio.h>
#include "grrlib_duk.h" #include <stdlib.h>
#include "data.h" #include "data.h"
#include "wii_duk.h"
extern const char * program; extern const char * program;
static bool running = true; static bool running = true;
static GRRLIB_ttfFont * font;
static duk_ret_t native_print(duk_context *ctx) { static unsigned int green = RGBA(0, 255, 0, 255);
GRRLIB_GeckoPrintf("%s\n", duk_to_string(ctx, 0));
return 0;
}
static duk_ret_t native_exit(duk_context *ctx) {
running = false;
return 0;
}
static void duk_fatal_error(void *udata, const char *msg) { static void duk_fatal_error(void *udata, const char *msg) {
GRRLIB_GeckoPrintf("an error has occured: %s\n", msg); const char * err_message_format = "an error has occured: %s";
size_t message_size = strlen(err_message_format) + strlen(msg) * sizeof(char *);
char * message = malloc(message_size);
snprintf(message, message_size, err_message_format, msg);
while (1) {
GRRLIB_PrintfTTF(0, 0, font, message, 12, green);
GRRLIB_Render();
} }
static duk_ret_t pad_scan_pads() {
PAD_ScanPads();
return 0;
}
static duk_ret_t pad_buttons_down(duk_context *ctx) {
u32 pressed = PAD_ButtonsDown(0);
duk_push_number(ctx, pressed);
return 1;
}
static duk_ret_t get_file(duk_context *ctx) {
const char* filename = duk_to_string(ctx, 0);
void* ptr;
size_t size;
if (get_file_pointer(filename, &ptr, &size)) {
duk_idx_t file_obj = duk_push_object(ctx);
duk_push_number(ctx, size);
duk_put_prop_string(ctx, file_obj, "size");
duk_push_pointer(ctx, ptr);
duk_put_prop_string(ctx, file_obj, "file_ptr");
return 1;
}
return duk_error(ctx, DUK_ERR_ERROR, "Error trying to get the file %s.", filename);
}
static void define_pad_object(duk_context *ctx) {
duk_idx_t pad_obj = duk_push_object(ctx);
duk_push_c_function(ctx, pad_scan_pads, 0);
duk_put_prop_string(ctx, pad_obj, "scan_pads");
duk_push_c_function(ctx, pad_buttons_down, 0);
duk_put_prop_string(ctx, pad_obj, "buttons_down");
}
static void define_wii_object(duk_context *ctx) {
duk_idx_t wii_obj = duk_push_object(ctx);
duk_push_c_function(ctx, native_print, 1);
duk_put_prop_string(ctx, wii_obj, "print");
duk_push_c_function(ctx, native_exit, 0);
duk_put_prop_string(ctx, wii_obj, "exit");
define_pad_object(ctx);
duk_put_prop_string(ctx, wii_obj, "pad");
define_grrlib_object(ctx);
duk_put_prop_string(ctx, wii_obj, "grrlib");
duk_push_c_function(ctx, get_file, 1);
duk_put_prop_string(ctx, wii_obj, "get_file");
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
GRRLIB_Init(); GRRLIB_Init();
PAD_Init(); PAD_Init();
// get the pixel operator font's data pointer and size
void * pixel_operator_font;
size_t pixel_operator_font_size;
get_file_pointer("PixelOperator.ttf", &pixel_operator_font, &pixel_operator_font_size);
font = GRRLIB_LoadTTF(pixel_operator_font, pixel_operator_font_size);
duk_context * ctx = duk_create_heap(NULL, NULL, NULL, NULL, duk_fatal_error); duk_context * ctx = duk_create_heap(NULL, NULL, NULL, NULL, duk_fatal_error);
define_wii_object(ctx); // create a wii object on the global object (globalThis)
define_wii_object(ctx, font, &running);
duk_put_global_string(ctx, "wii"); duk_put_global_string(ctx, "wii");
// evaluate functions from game.js // evaluate functions from game.js
@ -102,6 +50,7 @@ int main(int argc, char **argv) {
while (running) { while (running) {
PAD_ScanPads(); PAD_ScanPads();
// call update function
duk_get_global_string(ctx, "update"); duk_get_global_string(ctx, "update");
duk_call(ctx, 0); duk_call(ctx, 0);
duk_pop(ctx); duk_pop(ctx);
@ -109,10 +58,7 @@ int main(int argc, char **argv) {
GRRLIB_Render(); GRRLIB_Render();
} }
// printf("destroying heap\n");
duk_destroy_heap(ctx); duk_destroy_heap(ctx);
// printf("exit time\n");
GRRLIB_Exit(); GRRLIB_Exit();
return 0; return 0;

76
source/wii_duk.c Normal file
View file

@ -0,0 +1,76 @@
#include "wii_duk.h"
#include "data.h"
#include "grrlib_duk.h"
static GRRLIB_ttfFont * font;
static bool * running;
static unsigned int green = RGBA(0, 255, 0, 255);
static duk_ret_t native_print(duk_context *ctx) {
GRRLIB_PrintfTTF(0, 0, font, duk_to_string(ctx, 0), 32, green);
return 0;
}
static duk_ret_t native_exit(duk_context *ctx) {
*running = false;
return 0;
}
static duk_ret_t pad_buttons_down(duk_context *ctx) {
u32 pressed = PAD_ButtonsDown(0);
duk_push_number(ctx, pressed);
return 1;
}
static duk_ret_t get_file(duk_context *ctx) {
const char* filename = duk_to_string(ctx, 0);
void* ptr;
size_t size;
if (get_file_pointer(filename, &ptr, &size)) {
duk_idx_t file_obj = duk_push_object(ctx);
duk_push_number(ctx, size);
duk_put_prop_string(ctx, file_obj, "size");
duk_push_pointer(ctx, ptr);
duk_put_prop_string(ctx, file_obj, "file_ptr");
duk_push_string(ctx, filename);
duk_put_prop_string(ctx, file_obj, "filename");
return 1;
}
return duk_error(ctx, DUK_ERR_ERROR, "Error trying to get the file %s.", filename);
}
static void define_pad_object(duk_context *ctx) {
duk_idx_t pad_obj = duk_push_object(ctx);
duk_push_c_function(ctx, pad_buttons_down, 0);
duk_put_prop_string(ctx, pad_obj, "buttons_down");
}
void define_wii_object(duk_context *ctx, struct GRRLIB_Font * grrlib_font, bool * global_running) {
font = grrlib_font;
running = global_running;
duk_idx_t wii_obj = duk_push_object(ctx);
duk_push_c_function(ctx, native_print, 1);
duk_put_prop_string(ctx, wii_obj, "print");
duk_push_c_function(ctx, native_exit, 0);
duk_put_prop_string(ctx, wii_obj, "exit");
define_pad_object(ctx);
duk_put_prop_string(ctx, wii_obj, "pad");
define_grrlib_object(ctx);
duk_put_prop_string(ctx, wii_obj, "grrlib");
duk_push_c_function(ctx, get_file, 1);
duk_put_prop_string(ctx, wii_obj, "get_file");
}

6
source/wii_duk.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef WII_DUK_H
#define WII_DUK_H
#include <duktape.h>
#include <grrlib.h>
void define_wii_object(duk_context * ctx, struct GRRLIB_Font * grrlib_font, bool * global_running);
#endif

15
types.d.ts vendored Normal file
View file

@ -0,0 +1,15 @@
declare interface RGBA {}
declare interface FilePtr {}
declare interface GRRLibTexture {}
declare namespace wii {
function exit(): void
function get_file(filename: string) : FilePtr
namespace grrlib {
function load_texture(file: FilePtr) : GRRLibTexture
function fill_screen(color: RGBA): void
function draw_img(xPos: number, yPos: number, texture: GRRLibTexture, degrees: number, scaleX: number, scaleY: number, color: RGBA) : void
function rectangle(x: number, y: number, width: number, height: number, color: RGBA, filled: bool) : void
}
}

BIN
yellowbird-midflap.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 425 B