From d258cd9da373cdd18ea2834cfd6f97a421188f12 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Sun, 21 Apr 2024 17:11:39 +0200 Subject: [PATCH] Replace random() with example_random(), put definitions at start of scopes --- src/main.c | 50 +++++++++++++++++++++++++++++----------- src/platform/null/main.c | 2 +- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index 8365901..10e832a 100644 --- a/src/main.c +++ b/src/main.c @@ -3,17 +3,29 @@ #ifndef unigi_flag_exception #include -#define min(x, y) ((x) < (y) ? (x) : (y)) -#define max(x, y) ((x) > (y) ? (x) : (y)) +#ifndef min + #define min(x, y) ((x) < (y) ? (x) : (y)) +#endif + +#ifndef max + #define max(x, y) ((x) > (y) ? (x) : (y)) +#endif + +unigi_type_uint32 seed = 1337; +unigi_type_uint32 example_random() { + seed = (seed * 1103515245 + 12345) & 0x7FFFFFFF; + return seed; +} int main() { - if (unigi_init() > 0) { return 1; } - if (unigi_graphics_init() > 0) { return 1; } size_t resolution_index = 0; unigi_type_resolution_pixel_index smallestValidSize = 0 - 1; unigi_type_resolution smallestValidRes; smallestValidRes.width = 0; + if (unigi_init() > 0) { return 1; } + if (unigi_graphics_init() > 0) { return 1; } + while (1) { unigi_type_resolution_range * range = &unigi_status_resolutions[resolution_index]; if ( @@ -36,6 +48,7 @@ int main() { range->min->width <= 800 && range->min->height <= 600 ) { + unigi_type_resolution_pixel_index size; unigi_type_resolution_pixel_axis smallestWidth = min( max(range->min->width,320), min(range->max->width,800) @@ -48,7 +61,7 @@ int main() { printf("Found valid resolution: %ix%ix%i\n",smallestWidth,smallestHeight,range->min->depth); - unigi_type_resolution_pixel_index size = (unigi_type_resolution_pixel_index)smallestWidth * smallestHeight; + size = (unigi_type_resolution_pixel_index)smallestWidth * smallestHeight; if (size < smallestValidSize) { smallestValidRes.width = smallestWidth; smallestValidRes.height = smallestHeight; @@ -75,6 +88,15 @@ int main() { unigi_input_init(); while (1) { + unigi_type_resolution_pixel_axis startX; + unigi_type_resolution_pixel_axis startY; + unigi_type_resolution_pixel_axis width; + unigi_type_resolution_pixel_axis height; + unigi_type_resolution_pixel_index startIndex; + unigi_type_resolution_pixel_index stopIndex; + unigi_type_resolution_pixel_index prog; + unigi_type_resolution_pixel_axis ind; + while (1) { unigi_type_event * event = unigi_event_get(); if (event->id == unigi_enum_event_none) { break; } @@ -89,20 +111,20 @@ int main() { } // Draw some random squares on the screen - unigi_type_resolution_pixel_axis startX = random() % (unigi_status_resolution.width); - unigi_type_resolution_pixel_axis startY = random() % (unigi_status_resolution.height); - unigi_type_resolution_pixel_axis width = random() % (unigi_status_resolution.width - startX); - unigi_type_resolution_pixel_axis height = random() % (unigi_status_resolution.height - startY); - unigi_type_resolution_pixel_index startIndex = startX + (unigi_status_resolution.width * startY); - unigi_type_resolution_pixel_index stopIndex = startIndex + width; - unigi_graphics_draw_square(startIndex,stopIndex,height,random() % 65535); + startX = example_random() % (unigi_status_resolution.width); + startY = example_random() % (unigi_status_resolution.height); + width = example_random() % (unigi_status_resolution.width - startX); + height = example_random() % (unigi_status_resolution.height - startY); + startIndex = startX + (unigi_status_resolution.width * startY); + stopIndex = startIndex + width; + unigi_graphics_draw_square(startIndex,stopIndex,height,example_random() % 65535); // Draw a simple test pattern, and demonstrate how to use floats - unigi_type_resolution_pixel_index prog = unigi_float_2int(unigi_float_mul( + prog = unigi_float_2int(unigi_float_mul( unigi_float_make(unigi_status_resolution.width), unigi_float_div(unigi_float_make(1),unigi_float_make(40)) )); - unigi_type_resolution_pixel_axis ind = 0; + ind = 0; unigi_graphics_draw_square(prog * ind,prog * (ind + 1),unigi_status_resolution.height,0xF00F); ind++; unigi_graphics_draw_square(prog * ind,prog * (ind + 1),unigi_status_resolution.height,0xFF0F); diff --git a/src/platform/null/main.c b/src/platform/null/main.c index 83d3793..3bd95d2 100644 --- a/src/platform/null/main.c +++ b/src/platform/null/main.c @@ -10,4 +10,4 @@ unigi_type_error unigi_init() { } void unigi_close() { -} \ No newline at end of file +}