added prng functions

This commit is contained in:
Andre Leiradella 2015-05-13 23:34:26 -03:00
parent 97703b4144
commit 3bfb14d0b1
4 changed files with 44 additions and 7 deletions

View File

@ -3,7 +3,7 @@ DEFINES=
CFLAGS=-m32 -O3 --std=c99 $(INCLUDES) $(DEFINES)
OBJS=rl_backgrnd.o rl_image.o rl_map.o rl_sound.o rl_sprite.o rl_tile.o rl_version.o
OBJS=rl_backgrnd.o rl_image.o rl_map.o rl_rand.o rl_sound.o rl_sprite.o rl_tile.o rl_version.o
%.o: %.c
gcc $(CFLAGS) -c $< -o $@

26
src/rl_rand.c Normal file
View File

@ -0,0 +1,26 @@
#include <rl_rand.h>
static uint64_t seed;
void rl_srand( uint64_t s )
{
seed = s;
}
uint32_t rl_rand( void )
{
/*
http://en.wikipedia.org/wiki/Linear_congruential_generator
Newlib parameters
*/
seed = 6364136223846793005ULL * seed + 1;
return seed >> 32;
}
int rl_random( int min, int max )
{
/* fixed point math */
uint64_t dividend = max - min + 1;
return (int)( ( ( dividend * rl_rand() ) >> 32 ) + min );
}

10
src/rl_rand.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef RL_RAND_H
#define RL_RAND_H
#include <stdint.h>
void rl_srand( uint64_t seed );
uint32_t rl_rand( void );
int rl_random( int min, int max );
#endif /* RL_RAND_H */

View File

@ -10,6 +10,7 @@
#include <rl_sprite.h>
#include <rl_sound.h>
#include <rl_map.h>
#include <rl_rand.h>
#include <rl_version.h>
#include <button_x.h>
@ -186,7 +187,7 @@ static int testscr_init( testscr_t* s )
goto error6;
}
srand( perf_cb.get_time_usec() );
rl_srand( perf_cb.get_time_usec() );
s->reset = 0;
rl_sound_play_ogg( sketch008_ogg, sketch008_ogg_len, 1, NULL );
@ -216,10 +217,10 @@ static void testscr_update( testscr_t* s )
//s->reset = WIDTH * 6;
s->count = 4;
// s->xx = rand() % WIDTH;
// s->yy = rand() % HEIGHT;
// s->dx = ( rand() & 1 ) * 2 - 1;
// s->dy = ( rand() & 1 ) * 2 - 1;
// s->xx = rl_random( 0, WIDTH - 1 );
// s->yy = rl_random( 0, HEIGHT - 1 );
// s->dx = ( rl_rand() & 1 ) * 2 - 1;
// s->dy = ( rl_rand() & 1 ) * 2 - 1;
s->xx = 123.0f;
s->yy = 73.0f;
s->dx = 0.25f;
@ -291,7 +292,7 @@ static void testscr_update( testscr_t* s )
}
}
fb[ ( height / 2 ) * pitch + width / 2 ] = ( rand() & 0x0f ) << 12 | ( rand() & 0x1f ) << 6 | ( rand() & 0x0f ) << 1;
fb[ ( height / 2 ) * pitch + width / 2 ] = ( rl_rand() & 0x0f ) << 12 | ( rl_rand() & 0x1f ) << 6 | ( rl_rand() & 0x0f ) << 1;
}
else
{