(20111222) 20:00
This commit is contained in:
197
GameLib.c
197
GameLib.c
@@ -15,8 +15,11 @@
|
||||
// Globals
|
||||
int _running;
|
||||
Entity **_entity=NULL;
|
||||
int *_entity_flag=NULL;
|
||||
int _n_entities=0;
|
||||
int _n_entities_res=0;
|
||||
int _entities_lock=0;
|
||||
int _entities_compactate=0;
|
||||
void (*_gameproc)()=NULL;
|
||||
void (*_gamepostproc)()=NULL;
|
||||
int _ft;
|
||||
@@ -55,6 +58,7 @@ int GameLib_Init(int w,int h,char *title,int fps){
|
||||
void GameLib_AddEntity(Entity *e){
|
||||
if(_n_entities>=_n_entities_res){
|
||||
Entity **entity_aux;
|
||||
int *entity_flag_aux;
|
||||
int i;
|
||||
|
||||
// Grow the array
|
||||
@@ -63,15 +67,22 @@ void GameLib_AddEntity(Entity *e){
|
||||
else
|
||||
_n_entities_res*=2;
|
||||
entity_aux=malloc(sizeof(Entity *)*_n_entities_res);
|
||||
for(i=0;i<_n_entities;i++)
|
||||
entity_flag_aux=malloc(sizeof(int)*_n_entities_res);
|
||||
for(i=0;i<_n_entities;i++){
|
||||
entity_aux[i]=_entity[i];
|
||||
if(_entity)
|
||||
entity_flag_aux[i]=_entity_flag[i];
|
||||
}
|
||||
if(_entity){
|
||||
free(_entity);
|
||||
free(_entity_flag);
|
||||
}
|
||||
_entity=entity_aux;
|
||||
_entity_flag=entity_flag_aux;
|
||||
}
|
||||
|
||||
// Add the entity
|
||||
_entity[_n_entities]=e;
|
||||
_entity_flag[_n_entities]=1;
|
||||
_n_entities++;
|
||||
}
|
||||
|
||||
@@ -84,11 +95,17 @@ int GameLib_UnrefEntity(Entity *e){
|
||||
int i;
|
||||
for(i=0;i<_n_entities;i++){
|
||||
if(e==_entity[i]){
|
||||
_entity[i]=NULL;
|
||||
return(1);
|
||||
if(_entities_lock){
|
||||
_entity_flag[i]=-2;
|
||||
}else{
|
||||
_entity[i]=NULL;
|
||||
_entity_flag[i]=0;
|
||||
}
|
||||
_entities_compactate=1;
|
||||
return(i);
|
||||
}
|
||||
}
|
||||
return(0);
|
||||
return(-1);
|
||||
}
|
||||
|
||||
|
||||
@@ -97,52 +114,90 @@ int GameLib_UnrefEntity(Entity *e){
|
||||
//
|
||||
// Adds an entity to the game.
|
||||
int GameLib_DelEntity(Entity *e){
|
||||
if(!GameLib_UnrefEntity(e)){
|
||||
int i;
|
||||
if((i=GameLib_UnrefEntity(e))==-1){
|
||||
return(0);
|
||||
}
|
||||
Entity_Destroy(e);
|
||||
if(_entities_lock){
|
||||
// Delete latter
|
||||
_entity[i]=e;
|
||||
_entity_flag[i]=-1;
|
||||
}else{
|
||||
// Delete now
|
||||
Entity_Destroy(e);
|
||||
}
|
||||
return(1);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_Compactate
|
||||
//
|
||||
//
|
||||
void GameLib_Compactate(){
|
||||
int i,j;
|
||||
j=0;
|
||||
if(!_entities_compactate)
|
||||
return;
|
||||
for(i=0;i<_n_entities;i++){
|
||||
if(!_entity[i] || _entity_flag[i]==-2)
|
||||
continue;
|
||||
if(_entity_flag[i]==-1){
|
||||
Entity_Destroy(_entity[i]);
|
||||
continue;
|
||||
}
|
||||
if(i>j){
|
||||
_entity[j]=_entity[i];
|
||||
_entity_flag[j]=_entity_flag[i];
|
||||
}
|
||||
j++;
|
||||
}
|
||||
_n_entities=j;
|
||||
_entities_compactate=0;
|
||||
_entities_lock=0;
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_ProcLoop
|
||||
//
|
||||
// Process the loop.
|
||||
long long t_proc;
|
||||
long long t_col;
|
||||
long long t_over;
|
||||
long long t_postproc;
|
||||
int f_count;
|
||||
int GameLib_ProcLoop(){
|
||||
int i,j;
|
||||
int repeat,count;
|
||||
long long time;
|
||||
|
||||
// Launch the method
|
||||
|
||||
// Process
|
||||
time=Time_GetTime();
|
||||
GameLib_Compactate();_entities_lock=1;
|
||||
if(_gameproc){
|
||||
_gameproc();
|
||||
}
|
||||
|
||||
// Process entities
|
||||
// vec2 grav;
|
||||
// vec2_set(grav,0,1);
|
||||
for(i=0;i<_n_entities;i++){
|
||||
if(!_entity[i])
|
||||
continue;
|
||||
Entity_Process(_entity[i],_ft);
|
||||
/*
|
||||
if(_entity[i]->mass>0.0f){
|
||||
vec2_plus(_entity[i]->vel,_entity[i]->vel,grav);
|
||||
}
|
||||
*/
|
||||
}
|
||||
GameLib_Compactate();
|
||||
t_proc+=Time_GetTime()-time;
|
||||
|
||||
// Process colisions between entities
|
||||
|
||||
// Colisions between entities
|
||||
time=Time_GetTime();
|
||||
GameLib_Compactate();_entities_lock=1;
|
||||
count=0;
|
||||
do{
|
||||
repeat=0;
|
||||
for(i=0;i<_n_entities-1;i++){
|
||||
if(!_entity[i])
|
||||
continue;
|
||||
if(!(_entity[i]->flags&EntityFlag_Collision))
|
||||
continue;
|
||||
for(j=i+1;j<_n_entities;j++){
|
||||
if(!_entity[j])
|
||||
if(!(_entity[j]->flags&EntityFlag_Collision))
|
||||
continue;
|
||||
if(Entity_Collide(_entity[i],_entity[j])){
|
||||
repeat=1;
|
||||
@@ -150,47 +205,39 @@ int GameLib_ProcLoop(){
|
||||
}
|
||||
}
|
||||
count++;
|
||||
}while(repeat && count<20);
|
||||
|
||||
}while(repeat && count<10);
|
||||
// Stop remaining collisions
|
||||
for(i=0;i<_n_entities-1;i++){
|
||||
if(!_entity[i])
|
||||
continue;
|
||||
if(!(_entity[i]->flags&EntityFlag_Collision))
|
||||
continue;
|
||||
for(j=i+1;j<_n_entities;j++){
|
||||
if(!_entity[j])
|
||||
if(count==10){
|
||||
for(i=0;i<_n_entities-1;i++){
|
||||
if(!(_entity[i]->flags&EntityFlag_Collision))
|
||||
continue;
|
||||
if(Entity_Collide(_entity[i],_entity[j])){
|
||||
vec2_set(_entity[i]->vel,0,0);
|
||||
vec2_set(_entity[j]->vel,0,0);
|
||||
for(j=i+1;j<_n_entities;j++){
|
||||
if(!(_entity[j]->flags&EntityFlag_Collision))
|
||||
continue;
|
||||
if(Entity_Collide(_entity[i],_entity[j])){
|
||||
vec2_set(_entity[i]->vel,0,0);
|
||||
vec2_set(_entity[j]->vel,0,0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
GameLib_Compactate();
|
||||
t_col+=Time_GetTime()-time;
|
||||
|
||||
// Process Overlaps
|
||||
time=Time_GetTime();
|
||||
GameLib_Compactate();_entities_lock=1;
|
||||
for(i=0;i<_n_entities-1;i++){
|
||||
if(!_entity[i])
|
||||
continue;
|
||||
if(!(_entity[i]->flags&EntityFlag_Overlap))
|
||||
continue;
|
||||
for(j=i+1;j<_n_entities;j++){
|
||||
if(!_entity[j])
|
||||
if(!(_entity[j]->flags&EntityFlag_Overlap))
|
||||
continue;
|
||||
Entity_Overlaps(_entity[i],_entity[j]);
|
||||
}
|
||||
}
|
||||
|
||||
// Compactate
|
||||
j=0;
|
||||
for(i=0;i<_n_entities;i++){
|
||||
if(!_entity[i])
|
||||
continue;
|
||||
if(i>j)
|
||||
_entity[j]=_entity[i];
|
||||
j++;
|
||||
}
|
||||
_n_entities=j;
|
||||
GameLib_Compactate();
|
||||
t_over+=Time_GetTime()-time;
|
||||
|
||||
// Sort
|
||||
int n,n2,swap;
|
||||
@@ -222,13 +269,15 @@ int GameLib_ProcLoop(){
|
||||
n=n2;
|
||||
}while(n>0);
|
||||
|
||||
|
||||
// PostProcess and draw entities
|
||||
time=Time_GetTime();
|
||||
GameLib_Compactate();_entities_lock=1;
|
||||
for(i=0;i<_n_entities;i++){
|
||||
Entity *e;
|
||||
Entity_PostProcess(_entity[i],_ft);
|
||||
if(!_entity[i])
|
||||
continue;
|
||||
|
||||
// FIXME: This is a hack
|
||||
e=_entity[i];
|
||||
if(e->pos[0]<(_game_pos[0]-128))
|
||||
continue;
|
||||
@@ -241,11 +290,13 @@ int GameLib_ProcLoop(){
|
||||
|
||||
Entity_Draw(e,-_game_pos[0],-_game_pos[1]);
|
||||
}
|
||||
|
||||
// Launch the method
|
||||
if(_gamepostproc){
|
||||
_gamepostproc();
|
||||
}
|
||||
GameLib_Compactate();
|
||||
t_postproc+=Time_GetTime()-time;
|
||||
|
||||
f_count++;
|
||||
|
||||
return(_running);
|
||||
}
|
||||
@@ -263,7 +314,18 @@ void GameLib_Loop(
|
||||
|
||||
_gameproc=gameproc;
|
||||
_gamepostproc=gamepostproc;
|
||||
t_proc=0;
|
||||
t_col=0;
|
||||
t_over=0;
|
||||
t_postproc=0;
|
||||
f_count=0;
|
||||
Draw_Loop(GameLib_ProcLoop);
|
||||
|
||||
printf("Profiling::::::::::::\n");
|
||||
printf("t_proc........:%6lld\n",t_proc/f_count);
|
||||
printf("t_col.........:%6lld\n",t_col/f_count);
|
||||
printf("t_over........:%6lld\n",t_over/f_count);
|
||||
printf("t_postprocdraw:%6lld\n",t_postproc/f_count);
|
||||
}
|
||||
|
||||
|
||||
@@ -276,23 +338,30 @@ void GameLib_BreakLoop(){
|
||||
}
|
||||
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_GetPos
|
||||
// GameLib_SetPos
|
||||
// GameLib_SetPos
|
||||
//
|
||||
//
|
||||
void GameLib_GetPos(int pos[2]){
|
||||
pos[0]=_game_pos[0];
|
||||
pos[1]=_game_pos[1];
|
||||
}
|
||||
|
||||
void GameLib_SetPos(int pos[2]){
|
||||
_game_pos[0]=pos[0];
|
||||
_game_pos[1]=pos[1];
|
||||
}
|
||||
|
||||
void GameLib_GetSize(int size[2]){
|
||||
size[0]=_game_size[0];
|
||||
size[1]=_game_size[1];
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_ForEachEn
|
||||
//
|
||||
// Deletes every entity.
|
||||
void GameLib_DelEnts(){
|
||||
int i;
|
||||
|
||||
@@ -305,7 +374,10 @@ void GameLib_DelEnts(){
|
||||
}
|
||||
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_ForEachEn
|
||||
//
|
||||
// Iterates every entity.
|
||||
void GameLib_ForEachEnt(int (*func)(Entity *ent)){
|
||||
int i;
|
||||
for(i=0;i<_n_entities;i++){
|
||||
@@ -318,10 +390,15 @@ void GameLib_ForEachEnt(int (*func)(Entity *ent)){
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_PlaySound
|
||||
//
|
||||
//
|
||||
void GameLib_PlaySound(AudioSnd snd,int x,int y){
|
||||
float vleft,vright,vcen;
|
||||
int r,cx,cy,off;
|
||||
|
||||
// Get the screen context
|
||||
cx=_game_pos[0]+_game_size[0]/2;
|
||||
cy=_game_pos[1]+_game_size[1]/2;
|
||||
if(_game_size[0]>_game_size[1]){
|
||||
@@ -332,26 +409,22 @@ void GameLib_PlaySound(AudioSnd snd,int x,int y){
|
||||
r=r*1.2f;
|
||||
off=r/10.0f;
|
||||
|
||||
vleft=vright=1.0f;
|
||||
|
||||
// Calculate volumes
|
||||
vcen=1.0f-(abs(y-cy)/(float)r);
|
||||
|
||||
vright=1.0f-(abs(x-(cx+off))/(float)r);
|
||||
vleft=1.0f-(abs(x-(cx-off))/(float)r);
|
||||
|
||||
|
||||
vright*=vcen;
|
||||
vleft*=vcen;
|
||||
|
||||
// Clamp to 0
|
||||
if(vleft<0.0f)
|
||||
vleft=0.0f;
|
||||
if(vright<0.0f)
|
||||
vright=0.0f;
|
||||
|
||||
if(vleft<=0.0f && vright<=0.0f){
|
||||
return;
|
||||
}
|
||||
|
||||
// PLAY!
|
||||
Audio_PlaySound(snd,vleft,vright);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user