Simplified SDL random function names and added thread-safe versions

This commit is contained in:
Sam Lantinga committed 2024-06-24 11:49:15 -07:00
1 parent d013ac80ef
commit 96f2f23240
12 files changed
+167 -90

No files matched your search

+1 -1
View File
@@ -231,7 +231,7 @@ static int SDLCALL ping_thread(void *ptr)
sdlevent.type = SDL_EVENT_KEY_DOWN;
sdlevent.key.key = SDLK_1;
SDL_PushEvent(&sdlevent);
SDL_Delay(1000 + SDL_rand_n(1000));
SDL_Delay(1000 + SDL_rand(1000));
}
return cnt;
}
+10 -10
View File
@@ -72,8 +72,8 @@ static void DrawPoints(SDL_Renderer *renderer)
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
(Uint8)current_color, (Uint8)current_alpha);
x = (float)SDL_rand_n(viewport.w);
y = (float)SDL_rand_n(viewport.h);
x = (float)SDL_rand(viewport.w);
y = (float)SDL_rand(viewport.h);
SDL_RenderPoint(renderer, x, y);
}
}
@@ -120,10 +120,10 @@ static void DrawLines(SDL_Renderer *renderer)
SDL_RenderLine(renderer, 0.0f, (float)(viewport.h / 2), (float)(viewport.w - 1), (float)(viewport.h / 2));
SDL_RenderLine(renderer, (float)(viewport.w / 2), 0.0f, (float)(viewport.w / 2), (float)(viewport.h - 1));
} else {
x1 = (float)(SDL_rand_n(viewport.w * 2) - viewport.w);
x2 = (float)(SDL_rand_n(viewport.w * 2) - viewport.w);
y1 = (float)(SDL_rand_n(viewport.h * 2) - viewport.h);
y2 = (float)(SDL_rand_n(viewport.h * 2) - viewport.h);
x1 = (float)(SDL_rand(viewport.w * 2) - viewport.w);
x2 = (float)(SDL_rand(viewport.w * 2) - viewport.w);
y1 = (float)(SDL_rand(viewport.h * 2) - viewport.h);
y2 = (float)(SDL_rand(viewport.h * 2) - viewport.h);
SDL_RenderLine(renderer, x1, y1, x2, y2);
}
}
@@ -165,10 +165,10 @@ static void DrawRects(SDL_Renderer *renderer)
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
(Uint8)current_color, (Uint8)current_alpha);
rect.w = (float)SDL_rand_n(viewport.h / 2);
rect.h = (float)SDL_rand_n(viewport.h / 2);
rect.x = (float)((SDL_rand_n(viewport.w * 2) - viewport.w) - (rect.w / 2));
rect.y = (float)((SDL_rand_n(viewport.h * 2) - viewport.h) - (rect.h / 2));
rect.w = (float)SDL_rand(viewport.h / 2);
rect.h = (float)SDL_rand(viewport.h / 2);
rect.x = (float)((SDL_rand(viewport.w * 2) - viewport.w) - (rect.w / 2));
rect.y = (float)((SDL_rand(viewport.h * 2) - viewport.h) - (rect.h / 2));
SDL_RenderFillRect(renderer, &rect);
}
}
+4 -4
View File
@@ -1485,15 +1485,15 @@ int main(int argc, char *argv[])
SDL_Rect viewport;
SDL_GetRenderViewport(renderer, &viewport);
for (i = 0; i < num_sprites; ++i) {
positions[i].x = (float)SDL_rand_n(viewport.w - sprite_w);
positions[i].y = (float)SDL_rand_n(viewport.h - sprite_h);
positions[i].x = (float)SDL_rand(viewport.w - sprite_w);
positions[i].y = (float)SDL_rand(viewport.h - sprite_h);
positions[i].w = (float)sprite_w;
positions[i].h = (float)sprite_h;
velocities[i].x = 0.0f;
velocities[i].y = 0.0f;
while (velocities[i].x == 0.f || velocities[i].y == 0.f) {
velocities[i].x = (float)(SDL_rand_n(2 + 1) - 1);
velocities[i].y = (float)(SDL_rand_n(2 + 1) - 1);
velocities[i].x = (float)(SDL_rand(2 + 1) - 1);
velocities[i].y = (float)(SDL_rand(2 + 1) - 1);
}
}
+10 -10
View File
@@ -74,8 +74,8 @@ static void DrawPoints(SDL_Renderer *renderer)
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
(Uint8)current_color, (Uint8)current_alpha);
x = (float)SDL_rand_n(viewport.w);
y = (float)SDL_rand_n(viewport.h);
x = (float)SDL_rand(viewport.w);
y = (float)SDL_rand(viewport.h);
SDL_RenderPoint(renderer, x, y);
}
}
@@ -231,20 +231,20 @@ static void loop(void *arg)
break;
case SDLK_l:
add_line(
(float)SDL_rand_n(640),
(float)SDL_rand_n(480),
(float)SDL_rand_n(640),
(float)SDL_rand_n(480));
(float)SDL_rand(640),
(float)SDL_rand(480),
(float)SDL_rand(640),
(float)SDL_rand(480));
break;
case SDLK_R:
num_rects = 0;
break;
case SDLK_r:
add_rect(
(float)SDL_rand_n(640),
(float)SDL_rand_n(480),
(float)SDL_rand_n(640),
(float)SDL_rand_n(480));
(float)SDL_rand(640),
(float)SDL_rand(480),
(float)SDL_rand(640),
(float)SDL_rand(480));
break;
default:
break;
+4 -4
View File
@@ -189,15 +189,15 @@ int main(int argc, char *argv[])
quit(2);
}
for (i = 0; i < NUM_SPRITES; ++i) {
positions[i].x = (float)(SDL_rand_n(window_w - (int)sprite_w));
positions[i].y = (float)(SDL_rand_n(window_h - (int)sprite_h));
positions[i].x = (float)(SDL_rand(window_w - (int)sprite_w));
positions[i].y = (float)(SDL_rand(window_h - (int)sprite_h));
positions[i].w = sprite_w;
positions[i].h = sprite_h;
velocities[i].x = 0.0f;
velocities[i].y = 0.0f;
while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
velocities[i].x = (float)(SDL_rand_n(MAX_SPEED * 2 + 1) - MAX_SPEED);
velocities[i].y = (float)(SDL_rand_n(MAX_SPEED * 2 + 1) - MAX_SPEED);
velocities[i].x = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
velocities[i].y = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
}
}
+4 -4
View File
@@ -135,15 +135,15 @@ int main(int argc, char *argv[])
/* Initialize the sprite positions */
for (i = 0; i < NUM_SPRITES; ++i) {
positions[i].x = (float)SDL_rand_n(WINDOW_WIDTH - sprite_w);
positions[i].y = (float)SDL_rand_n(WINDOW_HEIGHT - sprite_h);
positions[i].x = (float)SDL_rand(WINDOW_WIDTH - sprite_w);
positions[i].y = (float)SDL_rand(WINDOW_HEIGHT - sprite_h);
positions[i].w = (float)sprite_w;
positions[i].h = (float)sprite_h;
velocities[i].x = 0.0f;
velocities[i].y = 0.0f;
while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
velocities[i].x = (float)(SDL_rand_n(MAX_SPEED * 2 + 1) - MAX_SPEED);
velocities[i].y = (float)(SDL_rand_n(MAX_SPEED * 2 + 1) - MAX_SPEED);
velocities[i].x = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
velocities[i].y = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
}
}
+4 -4
View File
@@ -97,15 +97,15 @@ static int InitSprites(void)
}
for (int i = 0; i < NUM_SPRITES; ++i) {
positions[i].x = (float)SDL_rand_n(WINDOW_WIDTH - sprite_w);
positions[i].y = (float)SDL_rand_n(WINDOW_HEIGHT - sprite_h);
positions[i].x = (float)SDL_rand(WINDOW_WIDTH - sprite_w);
positions[i].y = (float)SDL_rand(WINDOW_HEIGHT - sprite_h);
positions[i].w = (float)sprite_w;
positions[i].h = (float)sprite_h;
velocities[i].x = 0.0f;
velocities[i].y = 0.0f;
while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
velocities[i].x = (float)(SDL_rand_n(MAX_SPEED * 2 + 1) - MAX_SPEED);
velocities[i].y = (float)(SDL_rand_n(MAX_SPEED * 2 + 1) - MAX_SPEED);
velocities[i].x = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
velocities[i].y = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
}
}