sdlbomber/utils.c

128 lines
1.8 KiB
C

#include "bomber.h"
#include "utils.h"
#include "gfx.h"
#include <arpa/inet.h>
int volatile hc=0;
char volatile interrupted=0;
static Uint32 cur_unique;
Uint32 network_unique;
Uint32 gtime(void) {
return SDL_GetTicks();
}
Uint32 longtime(void) {
return gtime()/1000;
}
/* random generator */
#define TAP1 250
#define TAP2 103
/*
#define TAP1 55
#define TAP2 31
*/
static unsigned char myrandblock[TAP1];
static int myrandtake;
static int myrand1(void) {
int i;
int val;
i=myrandtake-TAP2;
if(i<0) i+=TAP1;
val=myrandblock[myrandtake++]^=myrandblock[i];
if(myrandtake==TAP1) myrandtake=0;
return val;
}
int myrand(void) {
int v;
v=myrand1();
return (v<<8) | myrand1();
}
static void initmyrand(Uint32 unique) {
int i,j;
unsigned char *p;
int msb,msk;
myrandtake=0;
p=myrandblock;
j=12345 ^ unique;
i=TAP1;
while(i--) {
j=(j*1277)&0xffff;
*p++=j>>8;
}
p=myrandblock+14;
msk=0xff;
msb=0x80;
do {
*p&=msk;
*p|=msb;
p+=11;
msk>>=1;
msb>>=1;
} while(msk);
i=500;
while(i--) myrand();
}
void create_unique(void) {
set_unique(gtime());
}
void set_unique(Uint32 unique) {
cur_unique = unique;
network_unique = htonl(cur_unique);
initmyrand(cur_unique);
}
Uint32 get_unique(void) {
return cur_unique;
}
void nomem(char *str) {
printf("No memory!!![%s]\n",str);
exit(1);
}
void mypause(void) {
while(!interrupted) {
pollinput();
SDL_Delay(1);
}
interrupted=0;
}
static Uint32 sdlhandler(Uint32 time) {
#if defined (SDL_LATENCY)
outmsgs();
#endif
interrupted=1;
hc++;
return time;
}
void pulseon(void) {
SDL_SetTimer(40,sdlhandler);
}
void hexdump(unsigned char *p, int len) {
int i;
for (i = 0; i < len; i++) {
if (15 == len % 16)
fprintf(stderr, "0x%X\n", p[i]);
else
fprintf(stderr, "0x%X ", p[i]);
}
if (i % 16)
fprintf(stderr, "\n");
}