Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

textures_logo_raylib example #30

Merged
merged 5 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ clang -I./include/ -o build/core_input_keys ./examples/core_input_keys.c -L./lib
clang -I./include/ -o build/shapes_colors_palette ./examples/shapes_colors_palette.c -L./lib/ -lraylib -lm
clang -I./include/ -o build/game ./game.c -L./lib/ -lraylib -lm
clang -I./include/ -o ./build/core_input_mouse_wheel ./examples/core_input_mouse_wheel.c -L./lib/ -lraylib -lm
clang -I./include/ -o ./build/textures_logo_raylib ./examples/textures_logo_raylib.c -L./lib/ -lraylib -lm

clang --target=wasm32 -I./include --no-standard-libraries -Wl,--export-table -Wl,--no-entry -Wl,--allow-undefined -Wl,--export=main -o wasm/core_basic_window.wasm ./examples/core_basic_window.c -DPLATFORM_WEB
clang --target=wasm32 -I./include --no-standard-libraries -Wl,--export-table -Wl,--no-entry -Wl,--allow-undefined -Wl,--export=main -o wasm/core_basic_screen_manager.wasm ./examples/core_basic_screen_manager.c -DPLATFORM_WEB
clang --target=wasm32 -I./include --no-standard-libraries -Wl,--export-table -Wl,--no-entry -Wl,--allow-undefined -Wl,--export=main -o wasm/core_input_keys.wasm ./examples/core_input_keys.c -DPLATFORM_WEB
clang --target=wasm32 -I./include --no-standard-libraries -Wl,--export-table -Wl,--no-entry -Wl,--allow-undefined -Wl,--export=main -o wasm/shapes_colors_palette.wasm ./examples/shapes_colors_palette.c -DPLATFORM_WEB
clang --target=wasm32 -I./include --no-standard-libraries -Wl,--export-table -Wl,--no-entry -Wl,--allow-undefined -Wl,--export=main -o wasm/game.wasm game.c -DPLATFORM_WEB
clang --target=wasm32 -I./include --no-standard-libraries -Wl,--export-table -Wl,--no-entry -Wl,--allow-undefined -Wl,--export=main -o wasm/core_input_mouse_wheel.wasm ./examples/core_input_mouse_wheel.c -DPLATFORM_WEB
clang --target=wasm32 -I./include --no-standard-libraries -Wl,--export-table -Wl,--no-entry -Wl,--allow-undefined -Wl,--export=main -o wasm/textures_logo_raylib.wasm ./examples/textures_logo_raylib.c -DPLATFORM_WEB
70 changes: 70 additions & 0 deletions examples/textures_logo_raylib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*******************************************************************************************
*
* raylib [textures] example - Texture loading and drawing
*
* Example originally created with raylib 1.0, last time updated with raylib 1.0
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2014-2024 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"

void raylib_js_set_entry(void (*entry)(void));

const int screenWidth = 800;
const int screenHeight = 450;
Texture2D texture = {0};

void GameFrame() {
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------

// Draw
//----------------------------------------------------------------------------------
BeginDrawing();

ClearBackground(RAYWHITE);

DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2, WHITE);

DrawText("this IS a texture!", 360, 370, 10, GRAY);

EndDrawing();
}

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------

InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture loading and drawing");

// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
texture = LoadTexture("resources/raylib_logo.png"); // Texture loading
//---------------------------------------------------------------------------------------

#ifdef PLATFORM_WEB
raylib_js_set_entry(GameFrame);
#else
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
GameFrame();
}

// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
#endif
return 0;
}
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
const wasmPaths = {
"tsoding": ["game",],
"core": ["core_basic_window", "core_basic_screen_manager", "core_input_keys", "core_input_mouse_wheel",],
"shapes": ["shapes_colors_palette"]
"shapes": ["shapes_colors_palette"],
"textures": ["textures_logo_raylib"]
}

const raylibExampleSelect = document.getElementById("raylib-example-select");
Expand Down
31 changes: 31 additions & 0 deletions raylib.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class RaylibJs {
this.currentPressedKeyState = new Set();
this.currentMouseWheelMoveState = 0;
this.currentMousePosition = {x: 0, y: 0};
this.images = [];
this.quit = false;
}

Expand Down Expand Up @@ -235,6 +236,36 @@ class RaylibJs {
return this.ctx.measureText(text).width;
}

// RLAPI Texture2D LoadTexture(const char *fileName);
async LoadTexture(result_ptr, filename_ptr) {
const buffer = this.wasm.instance.exports.memory.buffer;
const filename = cstr_by_ptr(buffer, filename_ptr);

var result = new Uint32Array(buffer, result_ptr, 5)
var img = new Image();
img.src = filename;
this.images.push(img);

result[0] = this.images.indexOf(img);
result[1] = 256; // width
result[2] = 256; // height
Comment on lines +251 to +252
Copy link
Contributor Author

@jkisor jkisor Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to add an event listener load that provides the true width and height. I couldn't quite get it to work. I think due to async. Any tips?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async does not work unless you use asyncify (see #25 and #26). You sadly can't write to the result_ptr when the load callback finishes in the general case, because the result_ptr lives on the stack and might already have different data on it.

You could add the images to RaylibJs in a preload step (as an argument to start, or the constructor) from JavaScript (like emscripten --preload-file).

result[3] = 1; // mipmaps
result[4] = 7; // format PIXELFORMAT_UNCOMPRESSED_R8G8B8A8

return result;
}

// RLAPI void DrawTexture(Texture2D texture, int posX, int posY, Color tint);
DrawTexture(texture_ptr, posX, posY, color_ptr) {
const buffer = this.wasm.instance.exports.memory.buffer;
const [id, width, height, mipmaps, format] = new Uint32Array(buffer, texture_ptr, 5);
// const tint = getColorFromMemory(buffer, color_ptr);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no tinting yet


if(this.images[id]) {
this.ctx.drawImage(this.images[id], posX, posY);
}
}

raylib_js_set_entry(entry) {
this.entryFunction = this.wasm.instance.exports.__indirect_function_table.get(entry);
}
Expand Down
Binary file added resources/raylib_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wasm/textures_logo_raylib.wasm
Binary file not shown.