Create RNG module
This commit is contained in:
2
build.sh
2
build.sh
@@ -8,4 +8,4 @@ defs="-D_POSIX_C_SOURCE=200809L"
|
|||||||
|
|
||||||
$cc $warn $flags $libs $defs \
|
$cc $warn $flags $libs $defs \
|
||||||
-o asteroids \
|
-o asteroids \
|
||||||
fb.c game.c input.c main.c maths.c renderer.c
|
fb.c game.c input.c main.c maths.c renderer.c rng.c
|
||||||
|
|||||||
14
game.c
14
game.c
@@ -2,12 +2,11 @@
|
|||||||
|
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
|
#include "rng.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <linux/input-event-codes.h>
|
#include <linux/input-event-codes.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/time.h>
|
|
||||||
|
|
||||||
#define FIRE_MEAN -0.15
|
#define FIRE_MEAN -0.15
|
||||||
#define FIRE_JITTER 0.01
|
#define FIRE_JITTER 0.01
|
||||||
@@ -204,11 +203,6 @@ static void remove_entity(unsigned id)
|
|||||||
--entity_count;
|
--entity_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
static float rand_plusminus()
|
|
||||||
{
|
|
||||||
return 2.0f * ((float)rand() / RAND_MAX - 0.5f);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ship_update()
|
static void ship_update()
|
||||||
{
|
{
|
||||||
entity_t *ship = entities + ship_entity_id;
|
entity_t *ship = entities + ship_entity_id;
|
||||||
@@ -221,7 +215,7 @@ static void ship_update()
|
|||||||
|
|
||||||
shape_t *fire = shapes + fire_shape_id;
|
shape_t *fire = shapes + fire_shape_id;
|
||||||
fire->visible = input.fwd != 0;
|
fire->visible = input.fwd != 0;
|
||||||
fire->verts[0].y = FIRE_MEAN + FIRE_JITTER * rand_plusminus();
|
fire->verts[0].y = FIRE_MEAN + FIRE_JITTER * rng_plusminus();
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_init(float _aspect)
|
void game_init(float _aspect)
|
||||||
@@ -230,10 +224,6 @@ void game_init(float _aspect)
|
|||||||
input_on_release(key_release_callback);
|
input_on_release(key_release_callback);
|
||||||
input_on_repeat(key_repeat_callback);
|
input_on_repeat(key_repeat_callback);
|
||||||
|
|
||||||
struct timeval tv;
|
|
||||||
gettimeofday(&tv, NULL);
|
|
||||||
srand(tv.tv_usec);
|
|
||||||
|
|
||||||
aspect = _aspect;
|
aspect = _aspect;
|
||||||
quit = false;
|
quit = false;
|
||||||
|
|
||||||
|
|||||||
3
main.c
3
main.c
@@ -1,6 +1,7 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
|
#include "rng.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
@@ -9,6 +10,8 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
rng_init();
|
||||||
|
|
||||||
const int input_fd = input_init();
|
const int input_fd = input_init();
|
||||||
|
|
||||||
const renderer_params_t renderer_params = renderer_init();
|
const renderer_params_t renderer_params = renderer_init();
|
||||||
|
|||||||
32
rng.c
Normal file
32
rng.c
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#include "rng.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
static uint32_t state;
|
||||||
|
|
||||||
|
void rng_init()
|
||||||
|
{
|
||||||
|
struct timeval tv;
|
||||||
|
const int res = gettimeofday(&tv, nullptr);
|
||||||
|
assert(res == 0);
|
||||||
|
state = tv.tv_usec;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t rng_uint32()
|
||||||
|
{
|
||||||
|
state ^= state << 13;
|
||||||
|
state ^= state >> 17;
|
||||||
|
state ^= state << 5;
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
float rng_canon()
|
||||||
|
{
|
||||||
|
return (float)rng_uint32() / UINT32_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
float rng_plusminus()
|
||||||
|
{
|
||||||
|
return 2.0 * rng_canon() - 1.0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user