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
+ }
0 commit comments