-
Notifications
You must be signed in to change notification settings - Fork 30
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ class RaylibJs { | |
this.currentPressedKeyState = new Set(); | ||
this.currentMouseWheelMoveState = 0; | ||
this.currentMousePosition = {x: 0, y: 0}; | ||
this.images = []; | ||
this.quit = false; | ||
} | ||
|
||
|
@@ -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 | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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 theresult_ptr
when theload
callback finishes in the general case, because theresult_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
).