mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-04-21 23:05:49 +00:00
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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user