Skip to content

Commit ca1baca

Browse files
committed
REVIEWED: examples memory allocators, using raylib provided macros
1 parent 7e8aca0 commit ca1baca

5 files changed

Lines changed: 59 additions & 53 deletions

File tree

examples/core/core_random_sequence.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ int main(void)
111111

112112
// De-Initialization
113113
//--------------------------------------------------------------------------------------
114-
free(rectangles);
114+
RL_FREE(rectangles);
115115
CloseWindow(); // Close window and OpenGL context
116116
//--------------------------------------------------------------------------------------
117117

examples/shapes/shapes_ball_physics.c

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717

1818
#include "raylib.h"
1919

20-
#include <stdlib.h>
21-
#include <math.h>
20+
#include <stdlib.h> // Required for: malloc(), free()
21+
#include <math.h> // Required for: hypot()
2222

23-
#define MAX_BALLS 5000 // Maximum quantity of balls
23+
#define MAX_BALLS 5000 // Maximum quantity of balls
2424

25+
//----------------------------------------------------------------------------------
26+
// Types and Structures Definition
27+
//----------------------------------------------------------------------------------
28+
// Ball data type
2529
typedef struct Ball {
26-
Vector2 pos; // Position
27-
Vector2 vel; // Velocity
28-
Vector2 ppos; // Previous position
30+
Vector2 position;
31+
Vector2 speed;
32+
Vector2 prevPosition;
2933
float radius;
3034
float friction;
3135
float elasticity;
@@ -45,11 +49,13 @@ int main(void)
4549

4650
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - ball physics");
4751

48-
Ball *balls = (Ball*)malloc(sizeof(Ball)*MAX_BALLS);
52+
Ball *balls = (Ball*)RL_MALLOC(sizeof(Ball)*MAX_BALLS);
53+
54+
// Init first ball in the array
4955
balls[0] = (Ball){
50-
.pos = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f },
51-
.vel = { 200, 200 },
52-
.ppos = { 0 },
56+
.position = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f },
57+
.speed = { 200, 200 },
58+
.prevPosition = { 0 },
5359
.radius = 40,
5460
.friction = 0.99f,
5561
.elasticity = 0.9f,
@@ -58,12 +64,12 @@ int main(void)
5864
};
5965

6066
int ballCount = 1;
61-
Ball *grabbedBall = NULL; // A pointer to the current ball that is grabbed
62-
Vector2 pressOffset = { 0 }; // Mouse press offset relative to the ball that grabbedd
67+
Ball *grabbedBall = NULL; // A pointer to the current ball that is grabbed
68+
Vector2 pressOffset = { 0 }; // Mouse press offset relative to the ball that grabbedd
6369

64-
float gravity = 100; // World gravity
70+
float gravity = 100; // World gravity
6571

66-
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
72+
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
6773
//---------------------------------------------------------------------------------------
6874

6975
// Main game loop
@@ -80,8 +86,8 @@ int main(void)
8086
for (int i = ballCount - 1; i >= 0; i--)
8187
{
8288
Ball *ball = &balls[i];
83-
pressOffset.x = mousePos.x - ball->pos.x;
84-
pressOffset.y = mousePos.y - ball->pos.y;
89+
pressOffset.x = mousePos.x - ball->position.x;
90+
pressOffset.y = mousePos.y - ball->position.y;
8591

8692
// If the distance between the ball position and the mouse press position
8793
// is less than or equal to the ball radius, the event occurred inside the ball
@@ -110,9 +116,9 @@ int main(void)
110116
if (ballCount < MAX_BALLS)
111117
{
112118
balls[ballCount++] = (Ball){
113-
.pos = mousePos,
114-
.vel = { (float)GetRandomValue(-300, 300), (float)GetRandomValue(-300, 300) },
115-
.ppos = { 0 },
119+
.position = mousePos,
120+
.speed = { (float)GetRandomValue(-300, 300), (float)GetRandomValue(-300, 300) },
121+
.prevPosition = { 0 },
116122
.radius = 20.0f + (float)GetRandomValue(0, 30),
117123
.friction = 0.99f,
118124
.elasticity = 0.9f,
@@ -127,7 +133,7 @@ int main(void)
127133
{
128134
for (int i = 0; i < ballCount; i++)
129135
{
130-
if (!balls[i].grabbed) balls[i].vel = (Vector2){ (float)GetRandomValue(-2000, 2000), (float)GetRandomValue(-2000, 2000) };
136+
if (!balls[i].grabbed) balls[i].speed = (Vector2){ (float)GetRandomValue(-2000, 2000), (float)GetRandomValue(-2000, 2000) };
131137
}
132138
}
133139

@@ -143,49 +149,49 @@ int main(void)
143149
if (!ball->grabbed)
144150
{
145151
// Ball repositioning using the velocity
146-
ball->pos.x += ball->vel.x * delta;
147-
ball->pos.y += ball->vel.y * delta;
152+
ball->position.x += ball->speed.x * delta;
153+
ball->position.y += ball->speed.y * delta;
148154

149155
// Does the ball hit the screen right boundary?
150-
if ((ball->pos.x + ball->radius) >= screenWidth)
156+
if ((ball->position.x + ball->radius) >= screenWidth)
151157
{
152-
ball->pos.x = screenWidth - ball->radius; // Ball repositioning
153-
ball->vel.x = -ball->vel.x*ball->elasticity; // Elasticity makes the ball lose 10% of its velocity on hit
158+
ball->position.x = screenWidth - ball->radius; // Ball repositioning
159+
ball->speed.x = -ball->speed.x*ball->elasticity; // Elasticity makes the ball lose 10% of its velocity on hit
154160
}
155161
// Does the ball hit the screen left boundary?
156-
else if ((ball->pos.x - ball->radius) <= 0)
162+
else if ((ball->position.x - ball->radius) <= 0)
157163
{
158-
ball->pos.x = ball->radius;
159-
ball->vel.x = -ball->vel.x*ball->elasticity;
164+
ball->position.x = ball->radius;
165+
ball->speed.x = -ball->speed.x*ball->elasticity;
160166
}
161167

162168
// The same for y axis
163-
if ((ball->pos.y + ball->radius) >= screenHeight)
169+
if ((ball->position.y + ball->radius) >= screenHeight)
164170
{
165-
ball->pos.y = screenHeight - ball->radius;
166-
ball->vel.y = -ball->vel.y*ball->elasticity;
171+
ball->position.y = screenHeight - ball->radius;
172+
ball->speed.y = -ball->speed.y*ball->elasticity;
167173
}
168-
else if ((ball->pos.y - ball->radius) <= 0)
174+
else if ((ball->position.y - ball->radius) <= 0)
169175
{
170-
ball->pos.y = ball->radius;
171-
ball->vel.y = -ball->vel.y*ball->elasticity;
176+
ball->position.y = ball->radius;
177+
ball->speed.y = -ball->speed.y*ball->elasticity;
172178
}
173179

174180
// Friction makes the ball lose 1% of its velocity each frame
175-
ball->vel.x = ball->vel.x*ball->friction;
181+
ball->speed.x = ball->speed.x*ball->friction;
176182
// Gravity affects only the y axis
177-
ball->vel.y = ball->vel.y*ball->friction + gravity;
183+
ball->speed.y = ball->speed.y*ball->friction + gravity;
178184
}
179185
else
180186
{
181187
// Ball repositioning using the mouse position
182-
ball->pos.x = mousePos.x - pressOffset.x;
183-
ball->pos.y = mousePos.y - pressOffset.y;
188+
ball->position.x = mousePos.x - pressOffset.x;
189+
ball->position.y = mousePos.y - pressOffset.y;
184190

185191
// While the ball is grabbed, recalculates its velocity
186-
ball->vel.x = (ball->pos.x - ball->ppos.x)/delta;
187-
ball->vel.y = (ball->pos.y - ball->ppos.y)/delta;
188-
ball->ppos = ball->pos;
192+
ball->speed.x = (ball->position.x - ball->prevPosition.x)/delta;
193+
ball->speed.y = (ball->position.y - ball->prevPosition.y)/delta;
194+
ball->prevPosition = ball->position;
189195
}
190196
}
191197
//----------------------------------------------------------------------------------
@@ -198,8 +204,8 @@ int main(void)
198204

199205
for (int i = 0; i < ballCount; i++)
200206
{
201-
DrawCircleV(balls[i].pos, balls[i].radius, balls[i].color);
202-
DrawCircleLinesV(balls[i].pos, balls[i].radius, BLACK);
207+
DrawCircleV(balls[i].position, balls[i].radius, balls[i].color);
208+
DrawCircleLinesV(balls[i].position, balls[i].radius, BLACK);
203209
}
204210

205211
DrawText("grab a ball by pressing with the mouse and throw it by releasing", 10, 10, 10, DARKGRAY);
@@ -215,7 +221,7 @@ int main(void)
215221

216222
// De-Initialization
217223
//--------------------------------------------------------------------------------------
218-
free(balls);
224+
RL_FREE(balls);
219225
CloseWindow(); // Close window and OpenGL context
220226
//--------------------------------------------------------------------------------------
221227

examples/text/text_codepoints_loading.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int main(void)
6161
SetTextLineSpacing(20); // Set line spacing for multiline text (when line breaks are included '\n')
6262

6363
// Free codepoints, atlas has already been generated
64-
free(codepointsNoDups);
64+
RL_FREE(codepointsNoDups);
6565

6666
bool showFontAtlas = false;
6767

@@ -139,7 +139,7 @@ int main(void)
139139
static int *CodepointRemoveDuplicates(int *codepoints, int codepointCount, int *codepointsResultCount)
140140
{
141141
int codepointsNoDupsCount = codepointCount;
142-
int *codepointsNoDups = (int *)calloc(codepointCount, sizeof(int));
142+
int *codepointsNoDups = (int *)RL_CALLOC(codepointCount, sizeof(int));
143143
memcpy(codepointsNoDups, codepoints, codepointCount*sizeof(int));
144144

145145
// Remove duplicates

examples/textures/textures_bunnymark.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int main(void)
4747
// Load bunny texture
4848
Texture2D texBunny = LoadTexture("resources/raybunny.png");
4949

50-
Bunny *bunnies = (Bunny *)malloc(MAX_BUNNIES*sizeof(Bunny)); // Bunnies array
50+
Bunny *bunnies = (Bunny *)RL_MALLOC(MAX_BUNNIES*sizeof(Bunny)); // Bunnies array
5151

5252
int bunniesCount = 0; // Bunnies counter
5353

@@ -126,7 +126,7 @@ int main(void)
126126

127127
// De-Initialization
128128
//--------------------------------------------------------------------------------------
129-
free(bunnies); // Unload bunnies data array
129+
RL_FREE(bunnies); // Unload bunnies data array
130130

131131
UnloadTexture(texBunny); // Unload bunny texture
132132

examples/textures/textures_fog_of_war.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ int main(void)
5151
// NOTE: We can have up to 256 values for tile ids and for tile fog state,
5252
// probably we don't need that many values for fog state, it can be optimized
5353
// to use only 2 bits per fog state (reducing size by 4) but logic will be a bit more complex
54-
map.tileIds = (unsigned char *)calloc(map.tilesX*map.tilesY, sizeof(unsigned char));
55-
map.tileFog = (unsigned char *)calloc(map.tilesX*map.tilesY, sizeof(unsigned char));
54+
map.tileIds = (unsigned char *)RL_CALLOC(map.tilesX*map.tilesY, sizeof(unsigned char));
55+
map.tileFog = (unsigned char *)RL_CALLOC(map.tilesX*map.tilesY, sizeof(unsigned char));
5656

5757
// Load map tiles (generating 2 random tile ids for testing)
5858
// NOTE: Map tile ids should be probably loaded from an external map file
@@ -149,8 +149,8 @@ int main(void)
149149

150150
// De-Initialization
151151
//--------------------------------------------------------------------------------------
152-
free(map.tileIds); // Free allocated map tile ids
153-
free(map.tileFog); // Free allocated map tile fog state
152+
RL_FREE(map.tileIds); // Free allocated map tile ids
153+
RL_FREE(map.tileFog); // Free allocated map tile fog state
154154

155155
UnloadRenderTexture(fogOfWar); // Unload render texture
156156

0 commit comments

Comments
 (0)