-
Notifications
You must be signed in to change notification settings - Fork 31
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
implement tinting #38
Open
AntonPieper
wants to merge
1
commit into
tsoding:main
Choose a base branch
from
AntonPieper:image-tint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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, RED); | ||
|
||
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
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 | ||
---|---|---|---|---|
|
@@ -61,8 +61,9 @@ class RaylibJs { | |||
} | ||||
|
||||
const canvas = document.getElementById(canvasId); | ||||
this.btx = document.createElement("canvas").getContext("2d"); | ||||
this.ctx = canvas.getContext("2d"); | ||||
if (this.ctx === null) { | ||||
if (this.ctx === null || this.btx === null) { | ||||
throw new Error("Could not create 2d canvas context"); | ||||
} | ||||
|
||||
|
@@ -109,6 +110,8 @@ class RaylibJs { | |||
InitWindow(width, height, title_ptr) { | ||||
this.ctx.canvas.width = width; | ||||
this.ctx.canvas.height = height; | ||||
this.btx.canvas.width = width; | ||||
this.btx.canvas.height = height; | ||||
const buffer = this.wasm.instance.exports.memory.buffer; | ||||
document.title = cstr_by_ptr(buffer, title_ptr); | ||||
} | ||||
|
@@ -301,13 +304,28 @@ class RaylibJs { | |||
} | ||||
|
||||
// RLAPI void DrawTexture(Texture2D texture, int posX, int posY, Color tint); | ||||
DrawTexture(texture_ptr, posX, posY, color_ptr) { | ||||
DrawTexture(texture_ptr, posX, posY, tint_ptr) { | ||||
/** @type {ArrayBuffer} */ | ||||
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. I don't think there's type hinting in any of the other JavaScript code here...
Suggested change
|
||||
const buffer = this.wasm.instance.exports.memory.buffer; | ||||
const [id, width, height, mipmaps, format] = new Uint32Array(buffer, texture_ptr, 5); | ||||
// // TODO: implement tinting for DrawTexture | ||||
// const tint = getColorFromMemory(buffer, color_ptr); | ||||
|
||||
this.ctx.drawImage(this.images[id], posX, posY); | ||||
let [id, width, height, mipmaps, format] = new Uint32Array(buffer, texture_ptr); | ||||
const tint = getColorFromMemory(buffer, tint_ptr); | ||||
const texture = this.images[id]; | ||||
if (texture === undefined) { | ||||
// TODO: Better error reporting | ||||
throw new Error(`textureID ${id} not found.`); | ||||
} | ||||
// TODO: actually use width / height from the passed struct | ||||
width = texture.width; | ||||
height = texture.height; | ||||
this.btx.clearRect(0, 0, width, height); | ||||
this.btx.drawImage(texture, 0, 0); | ||||
this.btx.fillStyle = tint; | ||||
this.btx.globalCompositeOperation = "multiply"; | ||||
this.btx.fillRect(0, 0, width, height); | ||||
this.btx.globalCompositeOperation = "destination-in"; | ||||
this.btx.drawImage(texture, 0, 0); | ||||
this.btx.globalCompositeOperation = "source-over"; | ||||
this.ctx.drawImage(this.btx.canvas, 0, 0, width, height, posX, posY, width, height); | ||||
} | ||||
|
||||
// TODO: codepoints are not implemented | ||||
|
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.
The only change in the
_tint
example versus the other example is the Tint color. This means there's a bunch of duplicated code for a 1-line change. Instead of having thetextures_logo_raylib_tint.c
example, I'd actually recommend porting over an official texture example that implements tint, like textures_bunnymark.c.