Added SDL_srand(), SDL_rand(), and SDL_rand_r() (thanks @JKaniarz!)

These are simple random functions that should not be used for serious random number generation.

Fixes https://github.com/libsdl-org/SDL/issues/4968
This commit is contained in:
Sam Lantinga
2024-06-16 07:20:11 -07:00
parent 9cb4bb92f6
commit d1d484ddbe
23 changed files with 203 additions and 89 deletions

View File

@@ -20,9 +20,6 @@
#include <emscripten/emscripten.h>
#endif
#include <stdlib.h>
#include <time.h>
#define NUM_OBJECTS 100
static SDLTest_CommonState *state;
@@ -75,8 +72,8 @@ static void DrawPoints(SDL_Renderer *renderer)
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
(Uint8)current_color, (Uint8)current_alpha);
x = (float)(rand() % viewport.w);
y = (float)(rand() % viewport.h);
x = (float)(SDL_rand() % viewport.w);
y = (float)(SDL_rand() % viewport.h);
SDL_RenderPoint(renderer, x, y);
}
}
@@ -123,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)((rand() % (viewport.w * 2)) - viewport.w);
x2 = (float)((rand() % (viewport.w * 2)) - viewport.w);
y1 = (float)((rand() % (viewport.h * 2)) - viewport.h);
y2 = (float)((rand() % (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);
}
}
@@ -168,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)(rand() % (viewport.h / 2));
rect.h = (float)(rand() % (viewport.h / 2));
rect.x = (float)((rand() % (viewport.w * 2) - viewport.w) - (rect.w / 2));
rect.y = (float)((rand() % (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);
}
}
@@ -293,8 +290,6 @@ int main(int argc, char *argv[])
SDL_RenderClear(renderer);
}
srand((unsigned int)time(NULL));
/* Main render loop */
frames = 0;
next_fps_check = SDL_GetTicks() + fps_check_delay;