1
1
Fork 0
Browse Source

make get_rand() reentrant

master
alistair 3 years ago
parent
commit
84f997a3c1
  1. 16
      src/environment.c
  2. 20
      src/game.c
  3. 3
      src/game.h
  4. 8
      src/main.c

16
src/environment.c

@ -67,9 +67,10 @@ struct colour_pallete get_pallete (int seed) { @@ -67,9 +67,10 @@ struct colour_pallete get_pallete (int seed) {
if (high_contrast_mode)
return get_pallete_high_contrast();
int red = get_rand(0) % 255;
int blue = get_rand(0) % 255;
int green = get_rand(0) % 255;
int n = seed;
int red = get_rand(&n) % 255;
int blue = get_rand(&n) % 255;
int green = get_rand(&n) % 255;
base.r = red;
base.g = green;
@ -134,7 +135,8 @@ struct environment get_scene_at(Vect coordinate, int seed) { @@ -134,7 +135,8 @@ struct environment get_scene_at(Vect coordinate, int seed) {
bit.y = 100 * seed;
Vect pos;
get_rand(seed);
int n = seed;
get_rand(&n);
Vect node;
node.y = 0;
@ -144,9 +146,9 @@ struct environment get_scene_at(Vect coordinate, int seed) { @@ -144,9 +146,9 @@ struct environment get_scene_at(Vect coordinate, int seed) {
bit.y = seed;
node.x = bit.x;
int r1 = 1.5*(get_rand(0) % 500) - (get_rand(0) % 100);
int r2 = 1.5*(get_rand(0) % 200) - (get_rand(0) % 200);
int r3 = (get_rand(0) % 100) - (get_rand(0) % 500);
int r1 = 1.5*(get_rand(&n) % 500) - (get_rand(&n) % 100);
int r2 = 1.5*(get_rand(&n) % 200) - (get_rand(&n) % 200);
int r3 = (get_rand(&n) % 100) - (get_rand(&n) % 500);
int r4 = 50;
int r[4] = {r1, r2, r3, r4};

20
src/game.c

@ -241,9 +241,10 @@ FloorPoly* generate_floor_simple(int num_polys, bool extend_down, int st_height) @@ -241,9 +241,10 @@ FloorPoly* generate_floor_simple(int num_polys, bool extend_down, int st_height)
last.x = 10;
last.y = st_height;
int n = 1;
for (int i = 0; i < num_polys; i++) {
double run = (get_rand(0) % 900);
double rise = (get_rand(0) % 100) - (get_rand(0) % 100);
double run = (get_rand(&n) % 900);
double rise = (get_rand(&n) % 100) - (get_rand(&n) % 100);
next.x = last.x + run;
next.y = last.y + rise;
@ -1663,7 +1664,8 @@ void handle_input_event(SDL_Event event) { @@ -1663,7 +1664,8 @@ void handle_input_event(SDL_Event event) {
} if (sc == input_map.mute) {
mute = !mute;
} if (sc == input_map.pause) {
game_paused = !game_paused;
if (in_game)
game_paused = !game_paused;
}
break;
case SDL_KEYUP:
@ -1709,19 +1711,15 @@ void handle_input_event(SDL_Event event) { @@ -1709,19 +1711,15 @@ void handle_input_event(SDL_Event event) {
}
int get_rand(int seed) {
/* temporary storage variable *n should be initialised to 1 */
int get_rand(int *n) {
const unsigned c = 11;
const unsigned m = (1 << 31) - 1;
const unsigned initial_n = 1;
const unsigned a = 48271;
static unsigned n = initial_n;
if (seed != 0) {
n = seed ;
}
n = (a * n + c) % m;
return n;
*n = (a * *n + c) % m;
return *n;
}

3
src/game.h

@ -22,7 +22,8 @@ void add_motor(Body *thing, double x, double y); @@ -22,7 +22,8 @@ void add_motor(Body *thing, double x, double y);
bool get_motor_active(Body *thing, int i);
int get_rand(int seed);
/* temporary storage variable *n should be initialised to the seed value */
int get_rand(int *n);
void next_level();

8
src/main.c

@ -150,7 +150,13 @@ int main (int argc, char** argv) { @@ -150,7 +150,13 @@ int main (int argc, char** argv) {
#ifdef __linux__
mkdirat(AT_FDCWD, "saves", 0777);
#elif WIN32
mkdir("saves");
if (access("saves", 0) == 0) {
struct stat status;
stat("saves", &status );
if ((status.st_mode & S_IFDIR) == 0) {
mkdir("saves");
}
}
#endif
game();

Loading…
Cancel
Save