(20111219) 01:00
This commit is contained in:
168
GameLib.c
168
GameLib.c
@@ -18,6 +18,9 @@ int _n_entities_res=0;
|
||||
void (*_gameproc)()=NULL;
|
||||
void (*_gamepostproc)()=NULL;
|
||||
int _ft;
|
||||
int _game_size[2];
|
||||
int _game_pos[2];
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_Init
|
||||
@@ -32,6 +35,11 @@ int GameLib_Init(int w,int h,char *title,int fps){
|
||||
}
|
||||
Audio_Init();
|
||||
|
||||
_game_size[0]=w;
|
||||
_game_size[1]=h;
|
||||
_game_pos[0]=0;
|
||||
_game_pos[1]=0;
|
||||
|
||||
_ft=1000/fps;
|
||||
|
||||
return(1);
|
||||
@@ -109,8 +117,8 @@ int GameLib_ProcLoop(){
|
||||
}
|
||||
|
||||
// Process entities
|
||||
vec2 grav;
|
||||
vec2_set(grav,0,1);
|
||||
// vec2 grav;
|
||||
// vec2_set(grav,0,1);
|
||||
for(i=0;i<_n_entities;i++){
|
||||
if(!_entity[i])
|
||||
continue;
|
||||
@@ -127,8 +135,12 @@ int GameLib_ProcLoop(){
|
||||
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[i] || !_entity[j])
|
||||
if(!_entity[j])
|
||||
continue;
|
||||
if(Entity_Collide(_entity[i],_entity[j])){
|
||||
repeat=1;
|
||||
@@ -140,8 +152,12 @@ int GameLib_ProcLoop(){
|
||||
|
||||
// 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[i] || !_entity[j])
|
||||
if(!_entity[j])
|
||||
continue;
|
||||
if(Entity_Collide(_entity[i],_entity[j])){
|
||||
vec2_set(_entity[i]->vel,0,0);
|
||||
@@ -150,12 +166,17 @@ int GameLib_ProcLoop(){
|
||||
}
|
||||
}
|
||||
|
||||
// PostProcess and draw entities
|
||||
for(i=0;i<_n_entities;i++){
|
||||
// Process Overlaps
|
||||
for(i=0;i<_n_entities-1;i++){
|
||||
if(!_entity[i])
|
||||
continue;
|
||||
Entity_PostProcess(_entity[i],_ft);
|
||||
Entity_Draw(_entity[i]);
|
||||
if(!(_entity[i]->flags&EntityFlag_Overlap))
|
||||
continue;
|
||||
for(j=i+1;j<_n_entities;j++){
|
||||
if(!_entity[j])
|
||||
continue;
|
||||
Entity_Overlaps(_entity[i],_entity[j]);
|
||||
}
|
||||
}
|
||||
|
||||
// Compactate
|
||||
@@ -169,6 +190,56 @@ int GameLib_ProcLoop(){
|
||||
}
|
||||
_n_entities=j;
|
||||
|
||||
// Sort
|
||||
int n,n2,swap;
|
||||
n=_n_entities;
|
||||
do{
|
||||
n2=0;
|
||||
for(i=1;i<n;i++){
|
||||
swap=0;
|
||||
if(_entity[i-1]->zorder > _entity[i]->zorder){
|
||||
// Lower level
|
||||
swap=1;
|
||||
}else
|
||||
if(_entity[i-1]->zorder < _entity[i]->zorder){
|
||||
// Upper level
|
||||
}else{
|
||||
// Same level
|
||||
if(_entity[i-1]->pos[1] > _entity[i]->pos[1]){
|
||||
swap=1;
|
||||
}
|
||||
}
|
||||
if(swap){
|
||||
Entity *ent;
|
||||
ent=_entity[i];
|
||||
_entity[i]=_entity[i-1];
|
||||
_entity[i-1]=ent;
|
||||
n2=i;
|
||||
}
|
||||
}
|
||||
n=n2;
|
||||
}while(n>0);
|
||||
|
||||
// PostProcess and draw entities
|
||||
for(i=0;i<_n_entities;i++){
|
||||
Entity *e;
|
||||
Entity_PostProcess(_entity[i],_ft);
|
||||
if(!_entity[i])
|
||||
continue;
|
||||
|
||||
e=_entity[i];
|
||||
if(e->pos[0]<(_game_pos[0]-128))
|
||||
continue;
|
||||
if(e->pos[0]>(_game_pos[0]+_game_size[0]+128))
|
||||
continue;
|
||||
if(e->pos[1]<(_game_pos[1]-128))
|
||||
continue;
|
||||
if(e->pos[1]>(_game_pos[1]+_game_size[1]+128))
|
||||
continue;
|
||||
|
||||
Entity_Draw(e,-_game_pos[0],-_game_pos[1]);
|
||||
}
|
||||
|
||||
// Launch the method
|
||||
if(_gamepostproc){
|
||||
_gamepostproc();
|
||||
@@ -201,3 +272,84 @@ void GameLib_Loop(
|
||||
void GameLib_BreakLoop(){
|
||||
_running=0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
|
||||
void GameLib_DelEnts(){
|
||||
int i;
|
||||
|
||||
for(i=0;i<_n_entities;i++){
|
||||
if(!_entity[i])
|
||||
continue;
|
||||
Entity_Destroy(_entity[i]);
|
||||
}
|
||||
_n_entities=0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void GameLib_ForEachEnt(int (*func)(Entity *ent)){
|
||||
int i;
|
||||
for(i=0;i<_n_entities;i++){
|
||||
if(!_entity[i])
|
||||
continue;
|
||||
if(!func(_entity[i])){
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void GameLib_PlaySound(AudioSnd snd,int x,int y){
|
||||
float vleft,vright,vcen;
|
||||
int r,cx,cy,off;
|
||||
|
||||
cx=_game_pos[0]+_game_size[0]/2;
|
||||
cy=_game_pos[1]+_game_size[1]/2;
|
||||
if(_game_size[0]>_game_size[1]){
|
||||
r=_game_size[0]/2;
|
||||
}else{
|
||||
r=_game_size[1]/2;
|
||||
}
|
||||
r=r*1.2f;
|
||||
off=r/10.0f;
|
||||
|
||||
vleft=vright=1.0f;
|
||||
|
||||
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;
|
||||
|
||||
if(vleft<0.0f)
|
||||
vleft=0.0f;
|
||||
if(vright<0.0f)
|
||||
vright=0.0f;
|
||||
|
||||
if(vleft<=0.0f && vright<=0.0f){
|
||||
return;
|
||||
}
|
||||
|
||||
Audio_PlaySound(snd,vleft,vright);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user