Skip to content

Commit d69676f

Browse files
committed
Added sprite animation example
1 parent a71e743 commit d69676f

File tree

4 files changed

+160
-7
lines changed

4 files changed

+160
-7
lines changed

examples/textures_sprite_anim.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*******************************************************************************************
2+
*
3+
* raylib [textures] example - Sprite animation
4+
*
5+
* Example originally created with raylib 1.3, last time updated with raylib 1.3
6+
*
7+
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
8+
* BSD-like license that allows static linking with closed source software
9+
*
10+
* Copyright (c) 2014-2024 Ramon Santamaria (@raysan5)
11+
*
12+
********************************************************************************************/
13+
14+
#include "raylib.h"
15+
16+
void raylib_js_set_entry(void (*entry)(void));
17+
18+
#define MAX_FRAME_SPEED 15
19+
#define MIN_FRAME_SPEED 1
20+
const int screenWidth = 800;
21+
const int screenHeight = 450;
22+
23+
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
24+
Texture2D scarfy;
25+
26+
Vector2 position;
27+
Rectangle frameRec;
28+
int currentFrame = 0;
29+
int framesCounter = 0;
30+
int framesSpeed = 8; // Number of spritesheet frames shown by second
31+
32+
void GameFrame()
33+
{
34+
// Update
35+
//----------------------------------------------------------------------------------
36+
framesCounter++;
37+
38+
if (framesCounter >= (60/framesSpeed))
39+
{
40+
framesCounter = 0;
41+
currentFrame++;
42+
43+
if (currentFrame > 5) currentFrame = 0;
44+
45+
frameRec.x = (float)currentFrame*(float)scarfy.width/6;
46+
}
47+
48+
// Control frames speed
49+
if (IsKeyPressed(KEY_RIGHT)) framesSpeed++;
50+
else if (IsKeyPressed(KEY_LEFT)) framesSpeed--;
51+
52+
if (framesSpeed > MAX_FRAME_SPEED) framesSpeed = MAX_FRAME_SPEED;
53+
else if (framesSpeed < MIN_FRAME_SPEED) framesSpeed = MIN_FRAME_SPEED;
54+
//----------------------------------------------------------------------------------
55+
56+
// Draw
57+
//----------------------------------------------------------------------------------
58+
BeginDrawing();
59+
60+
ClearBackground(RAYWHITE);
61+
62+
DrawTexture(scarfy, 15, 40, WHITE);
63+
DrawRectangleLines(15, 40, scarfy.width, scarfy.height, LIME);
64+
DrawRectangleLines(15 + (int)frameRec.x, 40 + (int)frameRec.y, (int)frameRec.width, (int)frameRec.height, RED);
65+
66+
DrawText("FRAME SPEED: ", 165, 210, 10, DARKGRAY);
67+
DrawText(TextFormat("%02i FPS", framesSpeed), 575, 210, 10, DARKGRAY);
68+
DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 240, 10, DARKGRAY);
69+
70+
for (int i = 0; i < MAX_FRAME_SPEED; i++)
71+
{
72+
if (i < framesSpeed) DrawRectangle(250 + 21*i, 205, 20, 20, RED);
73+
DrawRectangleLines(250 + 21*i, 205, 20, 20, MAROON);
74+
}
75+
76+
DrawTextureRec(scarfy, frameRec, position, WHITE); // Draw part of the texture
77+
78+
DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, GRAY);
79+
80+
EndDrawing();
81+
//----------------------------------------------------------------------------------
82+
}
83+
84+
//------------------------------------------------------------------------------------
85+
// Program main entry point
86+
//------------------------------------------------------------------------------------
87+
int main(void)
88+
{
89+
// Initialization
90+
//--------------------------------------------------------------------------------------
91+
InitWindow(screenWidth, screenHeight, "raylib [texture] example - sprite anim");
92+
93+
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
94+
scarfy = LoadTexture("resources/scarfy.png"); // Texture loading
95+
96+
position = (Vector2){ 350.0f, 280.0f };
97+
frameRec = (Rectangle){ 0.0f, 0.0f, (float)scarfy.width/6, (float)scarfy.height };
98+
currentFrame = 0;
99+
100+
framesCounter = 0;
101+
framesSpeed = 8; // Number of spritesheet frames shown by second
102+
103+
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
104+
//--------------------------------------------------------------------------------------
105+
106+
#ifdef PLATFORM_WEB
107+
raylib_js_set_entry(GameFrame);
108+
#else
109+
// Main game loop
110+
while (!WindowShouldClose())
111+
{
112+
GameFrame();
113+
}
114+
115+
// De-Initialization
116+
//--------------------------------------------------------------------------------------
117+
UnloadTexture(scarfy); // Texture unloading
118+
119+
120+
CloseWindow(); // Close window and OpenGL context
121+
//--------------------------------------------------------------------------------------
122+
#endif
123+
124+
return 0;
125+
}

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@
7171
"core": ["core_basic_window", "core_basic_screen_manager", "core_input_keys", "core_input_mouse_wheel",],
7272
"shapes": ["shapes_colors_palette"],
7373
"text": ["text_writing_anim"],
74-
"textures": ["textures_logo_raylib"],
74+
"textures": ["textures_logo_raylib","texture_sprite_anim"],
75+
"camera": ["2d_camera_platformer"],
7576
}
7677
const defaultWasm = Object.values(wasmPaths)[0][0];
7778

raylib.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,15 @@ class RaylibJs {
248248
this.ctx.fillStyle = color;
249249
this.ctx.fillRect(x, y, w, h);
250250
}
251-
251+
DrawRectangleLines(x, y, w, h, color_ptr) {
252+
const buffer = this.wasm.instance.exports.memory.buffer;
253+
const color = getColorFromMemory(buffer, color_ptr);
254+
const lineThick = 2;
255+
this.ctx.strokeStyle = color;
256+
this.ctx.lineWidth = lineThick;
257+
this.ctx.strokeRect(x + lineThick/2, y + lineThick/2, w - lineThick, h - lineThick);
258+
}
259+
252260
DrawRectangleLinesEx(rec_ptr, lineThick, color_ptr) {
253261
const buffer = this.wasm.instance.exports.memory.buffer;
254262
const [x, y, w, h] = new Float32Array(buffer, rec_ptr, 4);
@@ -285,15 +293,22 @@ class RaylibJs {
285293
const buffer = this.wasm.instance.exports.memory.buffer;
286294
const filename = cstr_by_ptr(buffer, filename_ptr);
287295

288-
var result = new Uint32Array(buffer, result_ptr, 5)
289-
var img = new Image();
296+
const result = new Uint32Array(buffer, result_ptr, 5)
297+
const img = new Image();
290298
img.src = filename;
299+
300+
//wait for the image to load, busy loop no idea what the correct solution would be
301+
let maxWait = 1000;
302+
while(maxWait >= 0)
303+
maxWait--;
304+
305+
console.log("image loaded", filename, img.width, img.height);
291306
this.images.push(img);
292307

293308
result[0] = this.images.indexOf(img);
294-
// TODO: get the true width and height of the image
295-
result[1] = 256; // width
296-
result[2] = 256; // height
309+
// TODO: get the true width and height of the image because it sometimes isnt loaded yet a refresh helps because it's cached
310+
result[1] = img.width || 256; // width
311+
result[2] = img.height || 256; // height
297312
result[3] = 1; // mipmaps
298313
result[4] = 7; // format PIXELFORMAT_UNCOMPRESSED_R8G8B8A8
299314

@@ -309,6 +324,18 @@ class RaylibJs {
309324

310325
this.ctx.drawImage(this.images[id], posX, posY);
311326
}
327+
//void DrawTextureRec(Texture2D texture, Rectangle source, Vector2 position, Color tint)
328+
DrawTextureRec(texture_ptr, rec_ptr, position_ptr, color_ptr) {
329+
const buffer = this.wasm.instance.exports.memory.buffer;
330+
const [id, width, height, mipmaps, format] = new Uint32Array(buffer, texture_ptr, 5);
331+
const [x, y, w, h] = new Float32Array(buffer, rec_ptr, 4);
332+
const [px, py] = new Float32Array(buffer, position_ptr, 2);
333+
// // TODO: implement tinting for DrawTexture
334+
// const tint = getColorFromMemory(buffer, color_ptr);
335+
336+
this.ctx.drawImage(this.images[id], x, y, w, h, px, py, w, h);
337+
338+
}
312339

313340
// TODO: codepoints are not implemented
314341
LoadFontEx(result_ptr, fileName_ptr/*, fontSize, codepoints, codepointCount*/) {

resources/scarfy.png

10.2 KB
Loading

0 commit comments

Comments
 (0)