7 Commits

Author SHA1 Message Date
Ray
ca1baca7c2 REVIEWED: examples memory allocators, using raylib provided macros 2026-03-23 11:53:57 +01:00
Ray
7e8aca00b6 Update textures_raw_data.c 2026-03-23 11:53:31 +01:00
Ray
c1dbfc87f3 REVIEWED: PR #5679 2026-03-23 11:52:01 +01:00
Ray
644e4adbe2 Update rcore.c 2026-03-23 11:51:37 +01:00
1e74aba7c3 WebRGFW: remapping mouse/touch position to canvas pixel coordinates (#5679)
* RGFW also requires RGBA8 images as window icons, as raylib already reports in raylib.h

* LibraryConfigurations.cmake: exchanged MATCHES -> STREQUAL in platform choosing if-statements

* WebRGFW: remapping mouse/touch position to canvas pixel coordinates

* fix 'typo'

* has to be done like that because of += in case of captured mouse
2026-03-23 11:31:06 +01:00
83e35ba170 [rcore] refactor possible data loss on FileMove() (#5682) 2026-03-23 11:30:28 +01:00
22cc2554b1 Fix Memory out of bounds in [shapes] example - ball physics (#5683)
Update screenshot to shapes_bouncing_ball.png
2026-03-23 11:28:28 +01:00
9 changed files with 104 additions and 73 deletions

View File

@ -111,7 +111,7 @@ int main(void)
// De-Initialization
//--------------------------------------------------------------------------------------
free(rectangles);
RL_FREE(rectangles);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

View File

@ -17,15 +17,19 @@
#include "raylib.h"
#include <stdlib.h>
#include <math.h>
#include <stdlib.h> // Required for: malloc(), free()
#include <math.h> // Required for: hypot()
#define MAX_BALLS 5000 // Maximum quantity of balls
#define MAX_BALLS 5000 // Maximum quantity of balls
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
// Ball data type
typedef struct Ball {
Vector2 pos; // Position
Vector2 vel; // Velocity
Vector2 ppos; // Previous position
Vector2 position;
Vector2 speed;
Vector2 prevPosition;
float radius;
float friction;
float elasticity;
@ -45,24 +49,27 @@ int main(void)
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - ball physics");
Ball balls[MAX_BALLS] = {{
.pos = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f },
.vel = { 200, 200 },
.ppos = { 0 },
Ball *balls = (Ball*)RL_MALLOC(sizeof(Ball)*MAX_BALLS);
// Init first ball in the array
balls[0] = (Ball){
.position = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f },
.speed = { 200, 200 },
.prevPosition = { 0 },
.radius = 40,
.friction = 0.99f,
.elasticity = 0.9f,
.color = BLUE,
.grabbed = false
}};
};
int ballCount = 1;
Ball *grabbedBall = NULL; // A pointer to the current ball that is grabbed
Vector2 pressOffset = { 0 }; // Mouse press offset relative to the ball that grabbedd
Ball *grabbedBall = NULL; // A pointer to the current ball that is grabbed
Vector2 pressOffset = { 0 }; // Mouse press offset relative to the ball that grabbedd
float gravity = 100; // World gravity
float gravity = 100; // World gravity
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//---------------------------------------------------------------------------------------
// Main game loop
@ -79,8 +86,8 @@ int main(void)
for (int i = ballCount - 1; i >= 0; i--)
{
Ball *ball = &balls[i];
pressOffset.x = mousePos.x - ball->pos.x;
pressOffset.y = mousePos.y - ball->pos.y;
pressOffset.x = mousePos.x - ball->position.x;
pressOffset.y = mousePos.y - ball->position.y;
// If the distance between the ball position and the mouse press position
// is less than or equal to the ball radius, the event occurred inside the ball
@ -109,9 +116,9 @@ int main(void)
if (ballCount < MAX_BALLS)
{
balls[ballCount++] = (Ball){
.pos = mousePos,
.vel = { (float)GetRandomValue(-300, 300), (float)GetRandomValue(-300, 300) },
.ppos = { 0 },
.position = mousePos,
.speed = { (float)GetRandomValue(-300, 300), (float)GetRandomValue(-300, 300) },
.prevPosition = { 0 },
.radius = 20.0f + (float)GetRandomValue(0, 30),
.friction = 0.99f,
.elasticity = 0.9f,
@ -126,7 +133,7 @@ int main(void)
{
for (int i = 0; i < ballCount; i++)
{
if (!balls[i].grabbed) balls[i].vel = (Vector2){ (float)GetRandomValue(-2000, 2000), (float)GetRandomValue(-2000, 2000) };
if (!balls[i].grabbed) balls[i].speed = (Vector2){ (float)GetRandomValue(-2000, 2000), (float)GetRandomValue(-2000, 2000) };
}
}
@ -142,49 +149,49 @@ int main(void)
if (!ball->grabbed)
{
// Ball repositioning using the velocity
ball->pos.x += ball->vel.x * delta;
ball->pos.y += ball->vel.y * delta;
ball->position.x += ball->speed.x * delta;
ball->position.y += ball->speed.y * delta;
// Does the ball hit the screen right boundary?
if ((ball->pos.x + ball->radius) >= screenWidth)
if ((ball->position.x + ball->radius) >= screenWidth)
{
ball->pos.x = screenWidth - ball->radius; // Ball repositioning
ball->vel.x = -ball->vel.x*ball->elasticity; // Elasticity makes the ball lose 10% of its velocity on hit
ball->position.x = screenWidth - ball->radius; // Ball repositioning
ball->speed.x = -ball->speed.x*ball->elasticity; // Elasticity makes the ball lose 10% of its velocity on hit
}
// Does the ball hit the screen left boundary?
else if ((ball->pos.x - ball->radius) <= 0)
else if ((ball->position.x - ball->radius) <= 0)
{
ball->pos.x = ball->radius;
ball->vel.x = -ball->vel.x*ball->elasticity;
ball->position.x = ball->radius;
ball->speed.x = -ball->speed.x*ball->elasticity;
}
// The same for y axis
if ((ball->pos.y + ball->radius) >= screenHeight)
if ((ball->position.y + ball->radius) >= screenHeight)
{
ball->pos.y = screenHeight - ball->radius;
ball->vel.y = -ball->vel.y*ball->elasticity;
ball->position.y = screenHeight - ball->radius;
ball->speed.y = -ball->speed.y*ball->elasticity;
}
else if ((ball->pos.y - ball->radius) <= 0)
else if ((ball->position.y - ball->radius) <= 0)
{
ball->pos.y = ball->radius;
ball->vel.y = -ball->vel.y*ball->elasticity;
ball->position.y = ball->radius;
ball->speed.y = -ball->speed.y*ball->elasticity;
}
// Friction makes the ball lose 1% of its velocity each frame
ball->vel.x = ball->vel.x*ball->friction;
ball->speed.x = ball->speed.x*ball->friction;
// Gravity affects only the y axis
ball->vel.y = ball->vel.y*ball->friction + gravity;
ball->speed.y = ball->speed.y*ball->friction + gravity;
}
else
{
// Ball repositioning using the mouse position
ball->pos.x = mousePos.x - pressOffset.x;
ball->pos.y = mousePos.y - pressOffset.y;
ball->position.x = mousePos.x - pressOffset.x;
ball->position.y = mousePos.y - pressOffset.y;
// While the ball is grabbed, recalculates its velocity
ball->vel.x = (ball->pos.x - ball->ppos.x)/delta;
ball->vel.y = (ball->pos.y - ball->ppos.y)/delta;
ball->ppos = ball->pos;
ball->speed.x = (ball->position.x - ball->prevPosition.x)/delta;
ball->speed.y = (ball->position.y - ball->prevPosition.y)/delta;
ball->prevPosition = ball->position;
}
}
//----------------------------------------------------------------------------------
@ -197,8 +204,8 @@ int main(void)
for (int i = 0; i < ballCount; i++)
{
DrawCircleV(balls[i].pos, balls[i].radius, balls[i].color);
DrawCircleLinesV(balls[i].pos, balls[i].radius, BLACK);
DrawCircleV(balls[i].position, balls[i].radius, balls[i].color);
DrawCircleLinesV(balls[i].position, balls[i].radius, BLACK);
}
DrawText("grab a ball by pressing with the mouse and throw it by releasing", 10, 10, 10, DARKGRAY);
@ -214,6 +221,7 @@ int main(void)
// De-Initialization
//--------------------------------------------------------------------------------------
RL_FREE(balls);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 9.3 KiB

View File

@ -61,7 +61,7 @@ int main(void)
SetTextLineSpacing(20); // Set line spacing for multiline text (when line breaks are included '\n')
// Free codepoints, atlas has already been generated
free(codepointsNoDups);
RL_FREE(codepointsNoDups);
bool showFontAtlas = false;
@ -139,7 +139,7 @@ int main(void)
static int *CodepointRemoveDuplicates(int *codepoints, int codepointCount, int *codepointsResultCount)
{
int codepointsNoDupsCount = codepointCount;
int *codepointsNoDups = (int *)calloc(codepointCount, sizeof(int));
int *codepointsNoDups = (int *)RL_CALLOC(codepointCount, sizeof(int));
memcpy(codepointsNoDups, codepoints, codepointCount*sizeof(int));
// Remove duplicates

View File

@ -47,7 +47,7 @@ int main(void)
// Load bunny texture
Texture2D texBunny = LoadTexture("resources/raybunny.png");
Bunny *bunnies = (Bunny *)malloc(MAX_BUNNIES*sizeof(Bunny)); // Bunnies array
Bunny *bunnies = (Bunny *)RL_MALLOC(MAX_BUNNIES*sizeof(Bunny)); // Bunnies array
int bunniesCount = 0; // Bunnies counter
@ -126,7 +126,7 @@ int main(void)
// De-Initialization
//--------------------------------------------------------------------------------------
free(bunnies); // Unload bunnies data array
RL_FREE(bunnies); // Unload bunnies data array
UnloadTexture(texBunny); // Unload bunny texture

View File

@ -51,8 +51,8 @@ int main(void)
// NOTE: We can have up to 256 values for tile ids and for tile fog state,
// probably we don't need that many values for fog state, it can be optimized
// to use only 2 bits per fog state (reducing size by 4) but logic will be a bit more complex
map.tileIds = (unsigned char *)calloc(map.tilesX*map.tilesY, sizeof(unsigned char));
map.tileFog = (unsigned char *)calloc(map.tilesX*map.tilesY, sizeof(unsigned char));
map.tileIds = (unsigned char *)RL_CALLOC(map.tilesX*map.tilesY, sizeof(unsigned char));
map.tileFog = (unsigned char *)RL_CALLOC(map.tilesX*map.tilesY, sizeof(unsigned char));
// Load map tiles (generating 2 random tile ids for testing)
// NOTE: Map tile ids should be probably loaded from an external map file
@ -149,8 +149,8 @@ int main(void)
// De-Initialization
//--------------------------------------------------------------------------------------
free(map.tileIds); // Free allocated map tile ids
free(map.tileFog); // Free allocated map tile fog state
RL_FREE(map.tileIds); // Free allocated map tile ids
RL_FREE(map.tileFog); // Free allocated map tile fog state
UnloadRenderTexture(fogOfWar); // Unload render texture

View File

@ -17,8 +17,6 @@
#include "raylib.h"
#include <stdlib.h> // Required for: malloc() and free()
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
@ -39,26 +37,31 @@ int main(void)
UnloadImage(fudesumiRaw); // Unload CPU (RAM) image data
// Generate a checked texture by code
int width = 960;
int height = 480;
int imWidth = 960;
int imHeight = 480;
// Dynamic memory allocation to store pixels data (Color type)
Color *pixels = (Color *)malloc(width*height*sizeof(Color));
// WARNING: Using raylib provided MemAlloc() that uses default raylib
// internal memory allocator, so this data can be freed using UnloadImage()
// that also uses raylib internal memory de-allocator
Color *pixels = (Color *)MemAlloc(imWidth*imHeight*sizeof(Color));
for (int y = 0; y < height; y++)
for (int y = 0; y < imHeight; y++)
{
for (int x = 0; x < width; x++)
for (int x = 0; x < imWidth; x++)
{
if (((x/32+y/32)/1)%2 == 0) pixels[y*width + x] = ORANGE;
else pixels[y*width + x] = GOLD;
if (((x/32+y/32)/1)%2 == 0) pixels[y*imWidth + x] = ORANGE;
else pixels[y*imWidth + x] = GOLD;
}
}
// Load pixels data into an image structure and create texture
// NOTE: We can assign pixels directly to data because Color is R8G8B8A8
// data structure defining that pixelformat, format must be set properly
Image checkedIm = {
.data = pixels, // We can assign pixels directly to data
.width = width,
.height = height,
.data = pixels,
.width = imWidth,
.height = imHeight,
.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
.mipmaps = 1
};

View File

@ -1447,15 +1447,35 @@ void PollInputEvents(void)
} break;
case RGFW_mousePosChanged:
{
float mouseX = 0.0f;
float mouseY = 0.0f;
if (RGFW_window_isCaptured(platform.window))
{
CORE.Input.Mouse.currentPosition.x += (float)rgfw_event.mouse.vecX;
CORE.Input.Mouse.currentPosition.y += (float)rgfw_event.mouse.vecY;
mouseX = (float)rgfw_event.mouse.vecX;
mouseY = (float)rgfw_event.mouse.vecY;
}
else
{
CORE.Input.Mouse.currentPosition.x = (float)rgfw_event.mouse.x;
CORE.Input.Mouse.currentPosition.y = (float)rgfw_event.mouse.y;
mouseX = (float)rgfw_event.mouse.x;
mouseY = (float)rgfw_event.mouse.y;
}
#if defined(__EMSCRIPTEN__)
double canvasWidth = 0.0;
double canvasHeight = 0.0;
emscripten_get_element_css_size("#canvas", &canvasWidth, &canvasHeight);
mouseX *= ((float)GetScreenWidth()/(float)canvasWidth);
mouseY *= ((float)GetScreenHeight()/(float)canvasHeight);
#endif
if (RGFW_window_isCaptured(platform.window))
{
CORE.Input.Mouse.currentPosition.x += event_x;
CORE.Input.Mouse.currentPosition.y += event_y;
}
else
{
CORE.Input.Mouse.currentPosition.x = event_x;
CORE.Input.Mouse.currentPosition.y = event_y;
}
CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;

View File

@ -2274,14 +2274,14 @@ int FileCopy(const char *srcPath, const char *dstPath)
// NOTE: If dst directories do not exists they are created
int FileMove(const char *srcPath, const char *dstPath)
{
int result = 0;
int result = -1;
if (FileExists(srcPath))
{
FileCopy(srcPath, dstPath);
FileRemove(srcPath);
if (FileCopy(srcPath, dstPath) == 0) result = FileRemove(srcPath);
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to copy file to [%s]", srcPath, dstPath);
}
else result = -1;
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Source file does not exist", srcPath);
return result;
}