Renamed SDL_rand() to SDL_rand_bits() and updated tests

This commit is contained in:
John Kaniarz 2024-06-19 15:31:00 -04:00 committed by Sam Lantinga
parent 237bbfcb9d
commit 8f29f8cae5
12 changed files with 110 additions and 98 deletions

View file

@ -1260,25 +1260,27 @@ extern SDL_DECLSPEC int SDLCALL SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRI
extern SDL_DECLSPEC int SDLCALL SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2); extern SDL_DECLSPEC int SDLCALL SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
/** /**
* Seed the pseudo-random number generator. * Seeds the pseudo-random number generator.
* *
* Reusing the seed number will cause SDL_rand() to repeat the same stream of * Reusing the seed number will cause SDL_rand_*() to repeat the same stream
* 'random' numbers. * of 'random' numbers.
* *
* \param seed the value to use as a random number seed, or 0 to use * \param seed the value to use as a random number seed, or 0 to use
* SDL_GetPerformanceCounter(). * SDL_GetPerformanceCounter().
* *
* \threadsafety This should be called on the same thread that calls * \threadsafety This should be called on the same thread that calls
* SDL_rand() * SDL_rand*()
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_rand * \sa SDL_rand_n
* \sa SDL_rand_float
* \sa SDL_rand_bits
*/ */
extern SDL_DECLSPEC void SDLCALL SDL_srand(Uint64 seed); extern SDL_DECLSPEC void SDLCALL SDL_srand(Uint64 seed);
/** /**
* Get 32 pseudo-random bits. * Generates 32 pseudo-random bits.
* *
* You likely want to use SDL_rand_n() to get a psuedo-randum number instead. * You likely want to use SDL_rand_n() to get a psuedo-randum number instead.
* *
@ -1301,38 +1303,44 @@ extern SDL_DECLSPEC void SDLCALL SDL_srand(Uint64 seed);
* \sa SDL_rand_n * \sa SDL_rand_n
* \sa SDL_rand_float * \sa SDL_rand_float
*/ */
extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand(void); extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_bits(void);
/** /**
* Generates a pseudo-random number less than n * Generates a pseudo-random number less than n for positive n
* *
* The method used is faster and of better quality than `SDL_rand() % n`. * The method used is faster and of better quality than `rand() % n`.
* However just like with `SDL_rand() % n`, bias increases with larger n. Odds * Odds are roughly 99.9% even for n = 1 million. Evenness is better for
* are better than 99.9% even for n under 1 million. * smaller n, and much worse as n gets bigger.
* *
* Example: to simulate a d6 use `SDL_rand_n(6) + 1` The +1 converts 0..5 to * Example: to simulate a d6 use `SDL_rand_n(6) + 1` The +1 converts 0..5 to
* 1..6 * 1..6
* *
* If you want reproducible output, be sure to initialize with SDL_srand() first.
*
* There are no guarantees as to the quality of the random sequence produced, * There are no guarantees as to the quality of the random sequence produced,
* and this should not be used for security (cryptography, passwords) or where * and this should not be used for security (cryptography, passwords) or where
* money is on the line (loot-boxes, casinos). There are many random number * money is on the line (loot-boxes, casinos). There are many random number
* libraries available with different characteristics and you should pick one * libraries available with different characteristics and you should pick one
* of those to meet any serious needs. * of those to meet any serious needs.
* *
* \param n the number of possible values. * \param n the number of possible outcomes. n must be positive.
* \returns a random value in the range of [0 .. n-1]. *
* \returns a random value in the range of [0 .. n-1]
* *
* \threadsafety All calls should be made from a single thread * \threadsafety All calls should be made from a single thread
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_rand * \sa SDL_srand
* \sa SDL_rand_float
*/ */
extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_n(Uint32 n); extern SDL_DECLSPEC Sint32 SDLCALL SDL_rand_n(Sint32 n);
/** /**
* Generates a uniform pseudo-random floating point number less than 1.0 * Generates a uniform pseudo-random floating point number less than 1.0
* *
* If you want reproducible output, be sure to initialize with SDL_srand() first.
*
* There are no guarantees as to the quality of the random sequence produced, * There are no guarantees as to the quality of the random sequence produced,
* and this should not be used for security (cryptography, passwords) or where * and this should not be used for security (cryptography, passwords) or where
* money is on the line (loot-boxes, casinos). There are many random number * money is on the line (loot-boxes, casinos). There are many random number
@ -1345,7 +1353,8 @@ extern SDL_DECLSPEC Uint32 SDLCALL SDL_rand_n(Uint32 n);
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_rand * \sa SDL_srand
* \sa SDL_rand_n
*/ */
extern SDL_DECLSPEC float SDLCALL SDL_rand_float(void); extern SDL_DECLSPEC float SDLCALL SDL_rand_float(void);

View file

@ -955,8 +955,9 @@ SDL3_0.0.0 {
SDL_powf; SDL_powf;
SDL_qsort; SDL_qsort;
SDL_qsort_r; SDL_qsort_r;
SDL_rand; SDL_rand_bits;
SDL_rand_r; SDL_rand_float;
SDL_rand_n;
SDL_realloc; SDL_realloc;
SDL_round; SDL_round;
SDL_roundf; SDL_roundf;

View file

@ -980,7 +980,7 @@
#define SDL_powf SDL_powf_REAL #define SDL_powf SDL_powf_REAL
#define SDL_qsort SDL_qsort_REAL #define SDL_qsort SDL_qsort_REAL
#define SDL_qsort_r SDL_qsort_r_REAL #define SDL_qsort_r SDL_qsort_r_REAL
#define SDL_rand SDL_rand_REAL #define SDL_rand_bits SDL_rand_bits_REAL
#define SDL_rand_float SDL_rand_float_REAL #define SDL_rand_float SDL_rand_float_REAL
#define SDL_rand_n SDL_rand_n_REAL #define SDL_rand_n SDL_rand_n_REAL
#define SDL_realloc SDL_realloc_REAL #define SDL_realloc SDL_realloc_REAL

View file

@ -989,9 +989,9 @@ SDL_DYNAPI_PROC(double,SDL_pow,(double a, double b),(a,b),return)
SDL_DYNAPI_PROC(float,SDL_powf,(float a, float b),(a,b),return) SDL_DYNAPI_PROC(float,SDL_powf,(float a, float b),(a,b),return)
SDL_DYNAPI_PROC(void,SDL_qsort,(void *a, size_t b, size_t c, SDL_CompareCallback d),(a,b,c,d),) SDL_DYNAPI_PROC(void,SDL_qsort,(void *a, size_t b, size_t c, SDL_CompareCallback d),(a,b,c,d),)
SDL_DYNAPI_PROC(void,SDL_qsort_r,(void *a, size_t b, size_t c, SDL_CompareCallback_r d, void *e),(a,b,c,d,e),) SDL_DYNAPI_PROC(void,SDL_qsort_r,(void *a, size_t b, size_t c, SDL_CompareCallback_r d, void *e),(a,b,c,d,e),)
SDL_DYNAPI_PROC(Uint32,SDL_rand,(void),(),return) SDL_DYNAPI_PROC(Uint32,SDL_rand_bits,(void),(),return)
SDL_DYNAPI_PROC(float,SDL_rand_float,(void),(),return) SDL_DYNAPI_PROC(float,SDL_rand_float,(void),(),return)
SDL_DYNAPI_PROC(Uint32,SDL_rand_n,(Uint32 a),(a),return) SDL_DYNAPI_PROC(Sint32,SDL_rand_n,(Sint32 a),(a),return)
SDL_DYNAPI_PROC(void*,SDL_realloc,(void *a, size_t b),(a,b),return) SDL_DYNAPI_PROC(void*,SDL_realloc,(void *a, size_t b),(a,b),return)
SDL_DYNAPI_PROC(double,SDL_round,(double a),(a),return) SDL_DYNAPI_PROC(double,SDL_round,(double a),(a),return)
SDL_DYNAPI_PROC(float,SDL_roundf,(float a),(a),return) SDL_DYNAPI_PROC(float,SDL_roundf,(float a),(a),return)

View file

@ -34,7 +34,7 @@ void SDL_srand(Uint64 seed)
SDL_rand_initialized = SDL_TRUE; SDL_rand_initialized = SDL_TRUE;
} }
Uint32 SDL_rand(void) Uint32 SDL_rand_bits(void)
{ {
if(!SDL_rand_initialized) { if(!SDL_rand_initialized) {
SDL_srand(0); SDL_srand(0);
@ -61,14 +61,14 @@ Uint32 SDL_rand(void)
return (Uint32)(SDL_rand_state >> 32); return (Uint32)(SDL_rand_state >> 32);
} }
Uint32 SDL_rand_n(Uint32 n) Sint32 SDL_rand_n(Sint32 n)
{ {
// On 32-bit arch, the compiler will optimize to a single 32-bit multiply // On 32-bit arch, the compiler will optimize to a single 32-bit multiply
Uint64 val = (Uint64)SDL_rand() * n; Uint64 val = (Uint64)SDL_rand_bits() * n;
return (Uint32)(val >> 32); return (Sint32)(val >> 32);
} }
float SDL_rand_float(void) float SDL_rand_float(void)
{ {
return (SDL_rand() >> (32-24)) * 0x1p-24f; return (SDL_rand_bits() >> (32-24)) * 0x1p-24f;
} }

View file

@ -206,7 +206,7 @@ static void loop(void)
default: default:
break; break;
} }
SDL_Log("waiting new event\n"); SDL_Log("waiting new event\n");
} }
SDL_Log("exiting event loop\n"); SDL_Log("exiting event loop\n");
#ifdef SDL_PLATFORM_EMSCRIPTEN #ifdef SDL_PLATFORM_EMSCRIPTEN
@ -227,7 +227,7 @@ static int SDLCALL ping_thread(void *ptr)
sdlevent.type = SDL_EVENT_KEY_DOWN; sdlevent.type = SDL_EVENT_KEY_DOWN;
sdlevent.key.keysym.sym = SDLK_1; sdlevent.key.keysym.sym = SDLK_1;
SDL_PushEvent(&sdlevent); SDL_PushEvent(&sdlevent);
SDL_Delay(1000 + SDL_rand() % 1000); SDL_Delay(1000 + SDL_rand_n(1000));
} }
return cnt; return cnt;
} }

View file

@ -72,8 +72,8 @@ static void DrawPoints(SDL_Renderer *renderer)
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color, SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
(Uint8)current_color, (Uint8)current_alpha); (Uint8)current_color, (Uint8)current_alpha);
x = (float)(SDL_rand() % viewport.w); x = (float)SDL_rand_n(viewport.w);
y = (float)(SDL_rand() % viewport.h); y = (float)SDL_rand_n(viewport.h);
SDL_RenderPoint(renderer, x, y); 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, 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)); SDL_RenderLine(renderer, (float)(viewport.w / 2), 0.0f, (float)(viewport.w / 2), (float)(viewport.h - 1));
} else { } else {
x1 = (float)((SDL_rand() % (viewport.w * 2)) - viewport.w); x1 = (float)(SDL_rand_n(viewport.w * 2) - viewport.w);
x2 = (float)((SDL_rand() % (viewport.w * 2)) - viewport.w); x2 = (float)(SDL_rand_n(viewport.w * 2) - viewport.w);
y1 = (float)((SDL_rand() % (viewport.h * 2)) - viewport.h); y1 = (float)(SDL_rand_n(viewport.h * 2) - viewport.h);
y2 = (float)((SDL_rand() % (viewport.h * 2)) - viewport.h); y2 = (float)(SDL_rand_n(viewport.h * 2) - viewport.h);
SDL_RenderLine(renderer, x1, y1, x2, y2); SDL_RenderLine(renderer, x1, y1, x2, y2);
} }
} }
@ -165,10 +165,10 @@ static void DrawRects(SDL_Renderer *renderer)
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color, SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
(Uint8)current_color, (Uint8)current_alpha); (Uint8)current_color, (Uint8)current_alpha);
rect.w = (float)(SDL_rand() % (viewport.h / 2)); rect.w = (float)SDL_rand_n(viewport.h / 2);
rect.h = (float)(SDL_rand() % (viewport.h / 2)); rect.h = (float)SDL_rand_n(viewport.h / 2);
rect.x = (float)((SDL_rand() % (viewport.w * 2) - viewport.w) - (rect.w / 2)); rect.x = (float)((SDL_rand_n(viewport.w * 2) - viewport.w) - (rect.w / 2));
rect.y = (float)((SDL_rand() % (viewport.h * 2) - viewport.h) - (rect.h / 2)); rect.y = (float)((SDL_rand_n(viewport.h * 2) - viewport.h) - (rect.h / 2));
SDL_RenderFillRect(renderer, &rect); SDL_RenderFillRect(renderer, &rect);
} }
} }

View file

@ -26,9 +26,9 @@
#include <libswscale/swscale.h> #include <libswscale/swscale.h>
#ifdef HAVE_EGL #ifdef HAVE_EGL
#include <SDL3/SDL_egl.h>
#include <SDL3/SDL_opengl.h> #include <SDL3/SDL_opengl.h>
#include <SDL3/SDL_opengles2.h> #include <SDL3/SDL_opengles2.h>
#include <SDL3/SDL_egl.h>
#include <libavutil/hwcontext_drm.h> #include <libavutil/hwcontext_drm.h>
@ -43,20 +43,20 @@
#endif #endif
#endif #endif
#define DRM_FORMAT_MOD_VENDOR_NONE 0 #define DRM_FORMAT_MOD_VENDOR_NONE 0
#define DRM_FORMAT_RESERVED ((1ULL << 56) - 1) #define DRM_FORMAT_RESERVED ((1ULL << 56) - 1)
#define fourcc_mod_get_vendor(modifier) \ #define fourcc_mod_get_vendor(modifier) \
(((modifier) >> 56) & 0xff) (((modifier) >> 56) & 0xff)
#define fourcc_mod_is_vendor(modifier, vendor) \ #define fourcc_mod_is_vendor(modifier, vendor) \
(fourcc_mod_get_vendor(modifier) == DRM_FORMAT_MOD_VENDOR_## vendor) (fourcc_mod_get_vendor(modifier) == DRM_FORMAT_MOD_VENDOR_##vendor)
#define fourcc_mod_code(vendor, val) \ #define fourcc_mod_code(vendor, val) \
((((Uint64)DRM_FORMAT_MOD_VENDOR_## vendor) << 56) | ((val) & 0x00ffffffffffffffULL)) ((((Uint64)DRM_FORMAT_MOD_VENDOR_##vendor) << 56) | ((val) & 0x00ffffffffffffffULL))
#define DRM_FORMAT_MOD_INVALID fourcc_mod_code(NONE, DRM_FORMAT_RESERVED) #define DRM_FORMAT_MOD_INVALID fourcc_mod_code(NONE, DRM_FORMAT_RESERVED)
#define DRM_FORMAT_MOD_LINEAR fourcc_mod_code(NONE, 0) #define DRM_FORMAT_MOD_LINEAR fourcc_mod_code(NONE, 0)
#ifdef SDL_PLATFORM_APPLE #ifdef SDL_PLATFORM_APPLE
#include <CoreVideo/CoreVideo.h> #include <CoreVideo/CoreVideo.h>
@ -71,7 +71,6 @@
#include "icon.h" #include "icon.h"
static SDL_Texture *sprite; static SDL_Texture *sprite;
static SDL_FRect *positions; static SDL_FRect *positions;
static SDL_FRect *velocities; static SDL_FRect *velocities;
@ -228,7 +227,8 @@ static SDL_bool CreateWindowAndRenderer(SDL_WindowFlags window_flags, const char
return SDL_TRUE; return SDL_TRUE;
} }
static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned int len, int *w, int *h) { static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned int len, int *w, int *h)
{
SDL_Texture *texture = NULL; SDL_Texture *texture = NULL;
SDL_Surface *surface; SDL_Surface *surface;
SDL_IOStream *src = SDL_IOFromConstMem(data, len); SDL_IOStream *src = SDL_IOFromConstMem(data, len);
@ -624,7 +624,7 @@ static SDL_bool GetTextureForMemoryFrame(AVFrame *frame, SDL_Texture **texture)
uint8_t *pixels[4]; uint8_t *pixels[4];
int pitch[4]; int pitch[4];
if (SDL_LockTexture(*texture, NULL, (void **)&pixels[0], &pitch[0]) == 0) { if (SDL_LockTexture(*texture, NULL, (void **)&pixels[0], &pitch[0]) == 0) {
sws_scale(sws_container->context, (const uint8_t * const *)frame->data, frame->linesize, 0, frame->height, pixels, pitch); sws_scale(sws_container->context, (const uint8_t *const *)frame->data, frame->linesize, 0, frame->height, pixels, pitch);
SDL_UnlockTexture(*texture); SDL_UnlockTexture(*texture);
} }
} else { } else {
@ -636,12 +636,12 @@ static SDL_bool GetTextureForMemoryFrame(AVFrame *frame, SDL_Texture **texture)
case SDL_PIXELFORMAT_IYUV: case SDL_PIXELFORMAT_IYUV:
if (frame->linesize[0] > 0 && frame->linesize[1] > 0 && frame->linesize[2] > 0) { if (frame->linesize[0] > 0 && frame->linesize[1] > 0 && frame->linesize[2] > 0) {
SDL_UpdateYUVTexture(*texture, NULL, frame->data[0], frame->linesize[0], SDL_UpdateYUVTexture(*texture, NULL, frame->data[0], frame->linesize[0],
frame->data[1], frame->linesize[1], frame->data[1], frame->linesize[1],
frame->data[2], frame->linesize[2]); frame->data[2], frame->linesize[2]);
} else if (frame->linesize[0] < 0 && frame->linesize[1] < 0 && frame->linesize[2] < 0) { } else if (frame->linesize[0] < 0 && frame->linesize[1] < 0 && frame->linesize[2] < 0) {
SDL_UpdateYUVTexture(*texture, NULL, frame->data[0] + frame->linesize[0] * (frame->height - 1), -frame->linesize[0], SDL_UpdateYUVTexture(*texture, NULL, frame->data[0] + frame->linesize[0] * (frame->height - 1), -frame->linesize[0],
frame->data[1] + frame->linesize[1] * (AV_CEIL_RSHIFT(frame->height, 1) - 1), -frame->linesize[1], frame->data[1] + frame->linesize[1] * (AV_CEIL_RSHIFT(frame->height, 1) - 1), -frame->linesize[1],
frame->data[2] + frame->linesize[2] * (AV_CEIL_RSHIFT(frame->height, 1) - 1), -frame->linesize[2]); frame->data[2] + frame->linesize[2] * (AV_CEIL_RSHIFT(frame->height, 1) - 1), -frame->linesize[2]);
} }
break; break;
default: default:
@ -895,7 +895,7 @@ static SDL_bool GetTextureForDRMFrame(AVFrame *frame, SDL_Texture **texture)
for (i = 0; i < desc->nb_layers; ++i) { for (i = 0; i < desc->nb_layers; ++i) {
const AVDRMLayerDescriptor *layer = &desc->layers[i]; const AVDRMLayerDescriptor *layer = &desc->layers[i];
for (j = 0; j < layer->nb_planes; ++j) { for (j = 0; j < layer->nb_planes; ++j) {
static const uint32_t formats[ 2 ] = { DRM_FORMAT_R8, DRM_FORMAT_GR88 }; static const uint32_t formats[2] = { DRM_FORMAT_R8, DRM_FORMAT_GR88 };
const AVDRMPlaneDescriptor *plane = &layer->planes[j]; const AVDRMPlaneDescriptor *plane = &layer->planes[j];
const AVDRMObjectDescriptor *object = &desc->objects[plane->object_index]; const AVDRMObjectDescriptor *object = &desc->objects[plane->object_index];
@ -906,10 +906,10 @@ static SDL_bool GetTextureForDRMFrame(AVFrame *frame, SDL_Texture **texture)
attr[k++] = formats[i]; attr[k++] = formats[i];
attr[k++] = EGL_WIDTH; attr[k++] = EGL_WIDTH;
attr[k++] = frames->width / ( image_index + 1 ); /* half size for chroma */ attr[k++] = frames->width / (image_index + 1); /* half size for chroma */
attr[k++] = EGL_HEIGHT; attr[k++] = EGL_HEIGHT;
attr[k++] = frames->height / ( image_index + 1 ); attr[k++] = frames->height / (image_index + 1);
attr[k++] = EGL_DMA_BUF_PLANE0_FD_EXT; attr[k++] = EGL_DMA_BUF_PLANE0_FD_EXT;
attr[k++] = object->fd; attr[k++] = object->fd;
@ -922,7 +922,7 @@ static SDL_bool GetTextureForDRMFrame(AVFrame *frame, SDL_Texture **texture)
if (has_EGL_EXT_image_dma_buf_import_modifiers) { if (has_EGL_EXT_image_dma_buf_import_modifiers) {
attr[k++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT; attr[k++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
attr[k++] = (object->format_modifier >> 0) & 0xFFFFFFFF; attr[k++] = (object->format_modifier >> 0) & 0xFFFFFFFF;
attr[k++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT; attr[k++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
attr[k++] = (object->format_modifier >> 32) & 0xFFFFFFFF; attr[k++] = (object->format_modifier >> 32) & 0xFFFFFFFF;
@ -1227,7 +1227,7 @@ static void InterleaveAudio(AVFrame *frame, const SDL_AudioSpec *spec)
for (c = 0; c < spec->channels; ++c) { for (c = 0; c < spec->channels; ++c) {
const Uint8 *src = frame->data[c]; const Uint8 *src = frame->data[c];
Uint8 *dst = data + c * samplesize; Uint8 *dst = data + c * samplesize;
for (n = frame->nb_samples; n--; ) { for (n = frame->nb_samples; n--;) {
SDL_memcpy(dst, src, samplesize); SDL_memcpy(dst, src, samplesize);
src += samplesize; src += samplesize;
dst += framesize; dst += framesize;
@ -1251,7 +1251,7 @@ static void HandleAudioFrame(AVFrame *frame)
} }
} }
static void av_log_callback(void* avcl, int level, const char *fmt, va_list vl) static void av_log_callback(void *avcl, int level, const char *fmt, va_list vl)
{ {
const char *pszCategory = NULL; const char *pszCategory = NULL;
char *message; char *message;
@ -1290,7 +1290,8 @@ static void av_log_callback(void* avcl, int level, const char *fmt, va_list vl)
SDL_free(message); SDL_free(message);
} }
static void print_usage(SDLTest_CommonState *state, const char *argv0) { static void print_usage(SDLTest_CommonState *state, const char *argv0)
{
static const char *options[] = { "[--verbose]", "[--sprites N]", "[--audio-codec codec]", "[--video-codec codec]", "[--software]", "video_file", NULL }; static const char *options[] = { "[--verbose]", "[--sprites N]", "[--audio-codec codec]", "[--video-codec codec]", "[--software]", "video_file", NULL };
SDLTest_CommonLogUsage(state, argv0, options); SDLTest_CommonLogUsage(state, argv0, options);
} }
@ -1325,7 +1326,7 @@ int main(int argc, char *argv[])
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
/* Log ffmpeg messages */ /* Log ffmpeg messages */
av_log_set_callback( av_log_callback ); av_log_set_callback(av_log_callback);
/* Parse commandline */ /* Parse commandline */
for (i = 1; i < argc;) { for (i = 1; i < argc;) {
@ -1336,14 +1337,14 @@ int main(int argc, char *argv[])
if (SDL_strcmp(argv[i], "--verbose") == 0) { if (SDL_strcmp(argv[i], "--verbose") == 0) {
verbose = SDL_TRUE; verbose = SDL_TRUE;
consumed = 1; consumed = 1;
} else if (SDL_strcmp(argv[i], "--sprites") == 0 && argv[i+1]) { } else if (SDL_strcmp(argv[i], "--sprites") == 0 && argv[i + 1]) {
num_sprites = SDL_atoi(argv[i+1]); num_sprites = SDL_atoi(argv[i + 1]);
consumed = 2; consumed = 2;
} else if (SDL_strcmp(argv[i], "--audio-codec") == 0 && argv[i+1]) { } else if (SDL_strcmp(argv[i], "--audio-codec") == 0 && argv[i + 1]) {
audio_codec_name = argv[i+1]; audio_codec_name = argv[i + 1];
consumed = 2; consumed = 2;
} else if (SDL_strcmp(argv[i], "--video-codec") == 0 && argv[i+1]) { } else if (SDL_strcmp(argv[i], "--video-codec") == 0 && argv[i + 1]) {
video_codec_name = argv[i+1]; video_codec_name = argv[i + 1];
consumed = 2; consumed = 2;
} else if (SDL_strcmp(argv[i], "--software") == 0) { } else if (SDL_strcmp(argv[i], "--software") == 0) {
software_only = SDL_TRUE; software_only = SDL_TRUE;
@ -1369,7 +1370,7 @@ int main(int argc, char *argv[])
goto quit; goto quit;
} }
if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0) { if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0) {
return_code = 2; return_code = 2;
goto quit; goto quit;
} }
@ -1484,15 +1485,15 @@ int main(int argc, char *argv[])
SDL_Rect viewport; SDL_Rect viewport;
SDL_GetRenderViewport(renderer, &viewport); SDL_GetRenderViewport(renderer, &viewport);
for (i = 0; i < num_sprites; ++i) { for (i = 0; i < num_sprites; ++i) {
positions[i].x = (float)(SDL_rand() % (viewport.w - sprite_w)); positions[i].x = (float)SDL_rand_n(viewport.w - sprite_w);
positions[i].y = (float)(SDL_rand() % (viewport.h - sprite_h)); positions[i].y = (float)SDL_rand_n(viewport.h - sprite_h);
positions[i].w = (float)sprite_w; positions[i].w = (float)sprite_w;
positions[i].h = (float)sprite_h; positions[i].h = (float)sprite_h;
velocities[i].x = 0.0f; velocities[i].x = 0.0f;
velocities[i].y = 0.0f; velocities[i].y = 0.0f;
while (velocities[i].x == 0.f || velocities[i].y == 0.f) { while (velocities[i].x == 0.f || velocities[i].y == 0.f) {
velocities[i].x = (float)((SDL_rand() % (2 + 1)) - 1); velocities[i].x = (float)(SDL_rand_n(2 + 1) - 1);
velocities[i].y = (float)((SDL_rand() % (2 + 1)) - 1); velocities[i].y = (float)(SDL_rand_n(2 + 1) - 1);
} }
} }

View file

@ -12,8 +12,8 @@
/* Simple program: draw as many random objects on the screen as possible */ /* Simple program: draw as many random objects on the screen as possible */
#include <SDL3/SDL_test_common.h>
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
#include <SDL3/SDL_test_common.h>
#ifdef SDL_PLATFORM_EMSCRIPTEN #ifdef SDL_PLATFORM_EMSCRIPTEN
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
@ -74,8 +74,8 @@ static void DrawPoints(SDL_Renderer *renderer)
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color, SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
(Uint8)current_color, (Uint8)current_alpha); (Uint8)current_color, (Uint8)current_alpha);
x = (float)(SDL_rand() % viewport.w); x = (float)SDL_rand_n(viewport.w);
y = (float)(SDL_rand() % viewport.h); y = (float)SDL_rand_n(viewport.h);
SDL_RenderPoint(renderer, x, y); SDL_RenderPoint(renderer, x, y);
} }
} }
@ -206,7 +206,7 @@ static void loop(void *arg)
{ {
int i; int i;
SDL_Event event; SDL_Event event;
int *done = (int*)arg; int *done = (int *)arg;
/* Check for events */ /* Check for events */
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
@ -231,10 +231,10 @@ static void loop(void *arg)
num_lines = 0; num_lines = 0;
} else { } else {
add_line( add_line(
(float)(SDL_rand() % 640), (float)SDL_rand_n(640),
(float)(SDL_rand() % 480), (float)SDL_rand_n(480),
(float)(SDL_rand() % 640), (float)SDL_rand_n(640),
(float)(SDL_rand() % 480)); (float)SDL_rand_n(480));
} }
break; break;
case 'r': case 'r':
@ -242,10 +242,10 @@ static void loop(void *arg)
num_rects = 0; num_rects = 0;
} else { } else {
add_rect( add_rect(
(float)(SDL_rand() % 640), (float)SDL_rand_n(640),
(float)(SDL_rand() % 480), (float)SDL_rand_n(480),
(float)(SDL_rand() % 640), (float)SDL_rand_n(640),
(float)(SDL_rand() % 480)); (float)SDL_rand_n(480));
} }
break; break;
default: default:
@ -287,7 +287,7 @@ int main(int argc, char *argv[])
int done; int done;
/* Initialize parameters */ /* Initialize parameters */
num_objects = -1; /* -1 means not initialized */ num_objects = -1; /* -1 means not initialized */
/* Initialize test framework */ /* Initialize test framework */
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);

View file

@ -188,15 +188,15 @@ int main(int argc, char *argv[])
quit(2); quit(2);
} }
for (i = 0; i < NUM_SPRITES; ++i) { for (i = 0; i < NUM_SPRITES; ++i) {
positions[i].x = (float)(SDL_rand() % (window_w - (int)sprite_w)); positions[i].x = (float)(SDL_rand_n((window_w - (int)sprite_w)));
positions[i].y = (float)(SDL_rand() % (window_h - (int)sprite_h)); positions[i].y = (float)(SDL_rand_n((window_h - (int)sprite_h)));
positions[i].w = sprite_w; positions[i].w = sprite_w;
positions[i].h = sprite_h; positions[i].h = sprite_h;
velocities[i].x = 0.0f; velocities[i].x = 0.0f;
velocities[i].y = 0.0f; velocities[i].y = 0.0f;
while (velocities[i].x == 0.f && velocities[i].y == 0.f) { while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
velocities[i].x = (float)((SDL_rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED); velocities[i].x = (float)((SDL_rand_n((MAX_SPEED * 2 + 1))) - MAX_SPEED);
velocities[i].y = (float)((SDL_rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED); velocities[i].y = (float)((SDL_rand_n((MAX_SPEED * 2 + 1))) - MAX_SPEED);
} }
} }

View file

@ -33,7 +33,8 @@ static int sprite_w, sprite_h;
static SDL_Renderer *renderer; static SDL_Renderer *renderer;
static int done; static int done;
static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned int len, int *w, int *h) { static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned int len, int *w, int *h)
{
SDL_Texture *texture = NULL; SDL_Texture *texture = NULL;
SDL_Surface *surface; SDL_Surface *surface;
SDL_IOStream *src = SDL_IOFromConstMem(data, len); SDL_IOStream *src = SDL_IOFromConstMem(data, len);
@ -134,15 +135,15 @@ int main(int argc, char *argv[])
/* Initialize the sprite positions */ /* Initialize the sprite positions */
for (i = 0; i < NUM_SPRITES; ++i) { for (i = 0; i < NUM_SPRITES; ++i) {
positions[i].x = (float)(SDL_rand() % (WINDOW_WIDTH - sprite_w)); positions[i].x = (float)SDL_rand_n(WINDOW_WIDTH - sprite_w);
positions[i].y = (float)(SDL_rand() % (WINDOW_HEIGHT - sprite_h)); positions[i].y = (float)SDL_rand_n(WINDOW_HEIGHT - sprite_h);
positions[i].w = (float)sprite_w; positions[i].w = (float)sprite_w;
positions[i].h = (float)sprite_h; positions[i].h = (float)sprite_h;
velocities[i].x = 0.0f; velocities[i].x = 0.0f;
velocities[i].y = 0.0f; velocities[i].y = 0.0f;
while (velocities[i].x == 0.f && velocities[i].y == 0.f) { while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
velocities[i].x = (float)((SDL_rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED); velocities[i].x = (float)(SDL_rand_n(MAX_SPEED * 2 + 1) - MAX_SPEED);
velocities[i].y = (float)((SDL_rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED); velocities[i].y = (float)(SDL_rand_n(MAX_SPEED * 2 + 1) - MAX_SPEED);
} }
} }

View file

@ -97,15 +97,15 @@ static int InitSprites(void)
} }
for (int i = 0; i < NUM_SPRITES; ++i) { for (int i = 0; i < NUM_SPRITES; ++i) {
positions[i].x = (float)(SDL_rand() % (WINDOW_WIDTH - sprite_w)); positions[i].x = (float)SDL_rand_n(WINDOW_WIDTH - sprite_w);
positions[i].y = (float)(SDL_rand() % (WINDOW_HEIGHT - sprite_h)); positions[i].y = (float)SDL_rand_n(WINDOW_HEIGHT - sprite_h);
positions[i].w = (float)sprite_w; positions[i].w = (float)sprite_w;
positions[i].h = (float)sprite_h; positions[i].h = (float)sprite_h;
velocities[i].x = 0.0f; velocities[i].x = 0.0f;
velocities[i].y = 0.0f; velocities[i].y = 0.0f;
while (velocities[i].x == 0.f && velocities[i].y == 0.f) { while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
velocities[i].x = (float)((SDL_rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED); velocities[i].x = (float)(SDL_rand_n(MAX_SPEED * 2 + 1) - MAX_SPEED);
velocities[i].y = (float)((SDL_rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED); velocities[i].y = (float)(SDL_rand_n(MAX_SPEED * 2 + 1) - MAX_SPEED);
} }
} }