1717
1818#include "raylib.h"
1919
20- #define MAX_COLORS 256
21- #define SCREEN_WIDTH 800
22- #define SCREEN_HEIGHT 450
23- #define SCALE_FACTOR 2
24- // buffer size at least for screenImage pixel count
25- #define INDEX_BUFFER_SIZE ((SCREEN_WIDTH * SCREEN_HEIGHT) / SCALE_FACTOR)
26- #define FLAME_WIDTH (SCREEN_WIDTH / SCALE_FACTOR)
20+ #include <stdlib.h> // Required for: calloc(), free()
2721
28- static void GeneretePalette ( Color * palette );
29- static void ClearIndexBuffer ( unsigned char * buffer , int count );
22+ #define MAX_COLORS 256
23+ #define SCALE_FACTOR 2
3024
3125//------------------------------------------------------------------------------------
3226// Program main entry point
@@ -35,31 +29,42 @@ int main(void)
3529{
3630 // Initialization
3731 //--------------------------------------------------------------------------------------
38- const int screenWidth = SCREEN_WIDTH ;
39- const int screenHeight = SCREEN_HEIGHT ;
40- const int pixelScale = SCALE_FACTOR ;
41- const int imageWidth = screenWidth / pixelScale ;
42- const int imageHeight = screenHeight / pixelScale ;
32+ const int screenWidth = 800 ;
33+ const int screenHeight = 450 ;
34+
4335 InitWindow (screenWidth , screenHeight , "raylib [textures] example - screen buffer" );
4436
45- Color palette [MAX_COLORS ] = {0 };
46- unsigned char indexBuffer [INDEX_BUFFER_SIZE ] = {0 };
47- unsigned char flameRootBuffer [FLAME_WIDTH ] = {0 };
37+ int imageWidth = screenWidth /SCALE_FACTOR ;
38+ int imageHeight = screenHeight /SCALE_FACTOR ;
39+ int flameWidth = screenWidth /SCALE_FACTOR ;
40+
41+ Color palette [MAX_COLORS ] = { 0 };
42+ unsigned char * indexBuffer = RL_CALLOC (imageWidth * imageWidth , sizeof (unsigned char ));
43+ unsigned char * flameRootBuffer = RL_CALLOC (flameWidth , sizeof (unsigned char ));
4844
4945 Image screenImage = GenImageColor (imageWidth , imageHeight , BLACK );
5046 Texture screenTexture = LoadTextureFromImage (screenImage );
51- GeneretePalette (palette );
52- ClearIndexBuffer (indexBuffer , INDEX_BUFFER_SIZE );
53- ClearIndexBuffer (flameRootBuffer , FLAME_WIDTH );
47+
48+ // Generate flame color palette
49+ for (int i = 0 ; i < MAX_COLORS ; i ++ )
50+ {
51+ float t = (float )i /(float )(MAX_COLORS - 1 );
52+ float hue = t * t ;
53+ float saturation = t ;
54+ float value = t ;
55+ palette [i ] = ColorFromHSV (250.0f + 150.0f * hue , saturation , value );
56+ }
5457
5558 SetTargetFPS (60 );
5659 //--------------------------------------------------------------------------------------
5760
5861 // Main game loop
5962 while (!WindowShouldClose ()) // Detect window close button or ESC key
6063 {
64+ // Update
65+ //----------------------------------------------------------------------------------
6166 // Grow flameRoot
62- for (int x = 2 ; x < FLAME_WIDTH ; ++ x )
67+ for (int x = 2 ; x < flameWidth ; x ++ )
6368 {
6469 unsigned short flame = flameRootBuffer [x ];
6570 if (flame == 255 ) continue ;
@@ -68,26 +73,26 @@ int main(void)
6873 flameRootBuffer [x ] = flame ;
6974 }
7075
71- // transfer flameRoot to indexBuffer
72- for (int x = 0 ; x < FLAME_WIDTH ; ++ x )
76+ // Transfer flameRoot to indexBuffer
77+ for (int x = 0 ; x < flameWidth ; x ++ )
7378 {
74- int i = x + (imageHeight - 1 ) * imageWidth ;
79+ int i = x + (imageHeight - 1 )* imageWidth ;
7580 indexBuffer [i ] = flameRootBuffer [x ];
7681 }
7782
7883 // Clear top row, because it can't move any higher
79- for (int x = 0 ; x < imageWidth ; ++ x )
84+ for (int x = 0 ; x < imageWidth ; x ++ )
8085 {
8186 if (indexBuffer [x ] == 0 ) continue ;
8287 indexBuffer [x ] = 0 ;
8388 }
8489
8590 // Skip top row, it is already cleared
86- for (int y = 1 ; y < imageHeight ; ++ y )
91+ for (int y = 1 ; y < imageHeight ; y ++ )
8792 {
88- for (int x = 0 ; x < imageWidth ; ++ x )
93+ for (int x = 0 ; x < imageWidth ; x ++ )
8994 {
90- unsigned i = x + y * imageWidth ;
95+ unsigned int i = x + y * imageWidth ;
9196 unsigned char colorIndex = indexBuffer [i ];
9297 if (colorIndex == 0 ) continue ;
9398
@@ -97,39 +102,44 @@ int main(void)
97102 int newX = x + moveX ;
98103 if (newX < 0 || newX >= imageWidth ) continue ;
99104
100- unsigned i_above = i - imageWidth + moveX ;
105+ unsigned int iabove = i - imageWidth + moveX ;
101106 int decay = GetRandomValue (0 , 3 );
102- colorIndex -= (decay < colorIndex ) ? decay : colorIndex ;
103- indexBuffer [i_above ] = colorIndex ;
107+ colorIndex -= (decay < colorIndex )? decay : colorIndex ;
108+ indexBuffer [iabove ] = colorIndex ;
104109 }
105110 }
106111
107112 // Update screenImage with palette colors
108- for (int y = 1 ; y < imageHeight ; ++ y )
113+ for (int y = 1 ; y < imageHeight ; y ++ )
109114 {
110- for (int x = 0 ; x < imageWidth ; ++ x )
115+ for (int x = 0 ; x < imageWidth ; x ++ )
111116 {
112- unsigned i = x + y * imageWidth ;
117+ unsigned int i = x + y * imageWidth ;
113118 unsigned char colorIndex = indexBuffer [i ];
114119 Color col = palette [colorIndex ];
115120 ImageDrawPixel (& screenImage , x , y , col );
116121 }
117122 }
118123
119124 UpdateTexture (screenTexture , screenImage .data );
125+ //----------------------------------------------------------------------------------
126+
120127 // Draw
121128 //----------------------------------------------------------------------------------
122129 BeginDrawing ();
123- const Vector2 origin = (Vector2 ){0 , 0 };
124- const float rotation = 0.f ;
125- DrawTextureEx (screenTexture , origin , rotation , pixelScale , WHITE );
130+
131+ ClearBackground (RAYWHITE );
132+
133+ DrawTextureEx (screenTexture , (Vector2 ){ 0 , 0 }, 0.0f , 2.0f , WHITE );
134+
126135 EndDrawing ();
127136 //----------------------------------------------------------------------------------
128137 }
129138
130139 // De-Initialization
131140 //--------------------------------------------------------------------------------------
132-
141+ RL_FREE (indexBuffer );
142+ RL_FREE (flameRootBuffer );
133143 UnloadTexture (screenTexture );
134144 UnloadImage (screenImage );
135145
@@ -138,24 +148,3 @@ int main(void)
138148
139149 return 0 ;
140150}
141-
142- static void GeneretePalette (Color * palette )
143- {
144- for (int i = 0 ; i < MAX_COLORS ; ++ i )
145- {
146- float t = (float )i /(float )(MAX_COLORS - 1 );
147- float hue = t * t ;
148- float saturation = t ;
149- float value = t ;
150- palette [i ] = ColorFromHSV (250.f + 150.f * hue , saturation , value );
151- }
152- }
153-
154- static void ClearIndexBuffer (unsigned char * buffer , int count )
155- {
156- // Use memset to set to ZERO, but for demonstration a plain for loop is used
157- for (int i = 0 ; i < count ; ++ i )
158- {
159- buffer [i ] = 0 ;
160- }
161- }
0 commit comments