Change main loop start and stop mechanisms

This commit is contained in:
2014-07-12 20:42:09 +02:00
committed by Valeriano A.R
parent 6f61665702
commit a386d256cb
5 changed files with 78 additions and 39 deletions

View File

@@ -438,24 +438,37 @@ void Draw_Clean(
// Draw_LoopIteration
//
// One iteracion of the loop updating the game window.
int (*_proc_func)()=NULL;
void (*_draw_func)(float f)=NULL;
void (*_proc_func)(void *data)=NULL;
void (*_draw_func)(void *data,float f)=NULL;
void *_data=NULL;
int _draw_looping=0;
int _draw_exitoverrided=0;
long long _accTime;
int Draw_LoopIteration(){
SDL_Event event;
Uint8* keys;
int done=0;
// Update screen
SDL_GL_SwapBuffers();
// Process Events
Input_SetKey(InputKey_Exit,0);
while(SDL_PollEvent(&event) ){
if(event.type == SDL_QUIT ){
done=1;
Input_SetKey(InputKey_Exit,1);
#ifndef EMSCRIPTEN
if(!_draw_exitoverrided){
_draw_looping=0;
}
#endif
}
if(event.type == SDL_KEYDOWN ){
if(event.key.keysym.sym == SDLK_ESCAPE ) {
done=1;
Input_SetKey(InputKey_Exit,1);
#ifndef EMSCRIPTEN
if(!_draw_exitoverrided){
_draw_looping=0;
}
#endif
}
}
}
@@ -477,11 +490,9 @@ int Draw_LoopIteration(){
if(_accTime>100000){
_accTime=100000;
}
while(_accTime>=proc_t_frame && !done){
while(_accTime>=proc_t_frame && _draw_looping){
Input_Frame();
if(!_proc_func()){
done=1;
}
_proc_func(_data);
_accTime-=proc_t_frame;
}
}
@@ -490,11 +501,11 @@ int Draw_LoopIteration(){
if(_draw_func){
float frameFactor=0.0f;
frameFactor=(float)_accTime/(float)proc_t_frame;
_draw_func(frameFactor);
_draw_func(_data,frameFactor);
Draw_Flush();
}
return done;
return _draw_looping;
}
#ifdef EMSCRIPTEN
@@ -514,16 +525,23 @@ void Draw_LoopIterationAux(){
// Draw_Loop
//
// Loops updating the game window.
void Draw_Loop(int (*proc)(),void (*draw)(float f)){
void Draw_Loop(
void (*proc)(void *data),
void (*draw)(void *data,float f),
void *data)
{
long long newTime;
long long procTime1,procTime2,drawTime1,drawTime2,waitTime;
_proc_func=proc;
_draw_func=draw;
_data=data;
if(_draw_looping){return;}
_draw_looping=1;
#ifndef EMSCRIPTEN
_accTime=proc_t_frame;
procTime1=drawTime1=Time_GetTime();
while(!Draw_LoopIteration()){
while(Draw_LoopIteration()){
// Wait to round draw_t_frame
drawTime2=Time_GetTime();
@@ -540,11 +558,35 @@ void Draw_Loop(int (*proc)(),void (*draw)(float f)){
#else
_accTime=proc_t_frame;
_procTime1=Time_GetTime();
emscripten_set_main_loop(Draw_LoopIterationAux, _fps, 1);
if(_fps<=50){
emscripten_set_main_loop(Draw_LoopIterationAux, _fps, 1);
}else{
emscripten_set_main_loop(Draw_LoopIterationAux, 0, 1);
}
#endif
}
/////////////////////////////
// Draw_BreakLoop
//
// Breaks the drawing loop
void Draw_BreakLoop(){
#ifndef EMSCRIPTEN
_draw_looping=0;
#endif
}
/////////////////////////////
// Draw_OverrideExit
//
// Overrides the default exit mechanism
void Draw_OverrideExit(int override){
_draw_exitoverrided=override;
}
/////////////////////////////
// Draw_CreateImage
//

View File

@@ -25,7 +25,24 @@ void Draw_Clean(
// Draw_Loop
//
// Loops updating the game window.
void Draw_Loop(int (*proc)(),void (*draw)(float f));
void Draw_Loop(
void (*proc)(void *data),
void (*draw)(void *data,float f),
void *data);
/////////////////////////////
// Draw_BreakLoop
//
// Breaks the drawing loop
void Draw_BreakLoop();
/////////////////////////////
// Draw_OverrideExit
//
// Overrides the default exit mechanism
void Draw_OverrideExit(int override);
/////////////////////////////

View File

@@ -17,7 +17,6 @@
#include "GameLib.h"
// Globals
int _running;
Entity *_entity=NULL;
int *_entity_flag=NULL;
int _n_entities=0;
@@ -188,7 +187,7 @@ void GameLib_Compactate(){
// GameLib_ProcLoop
//
// Process the loop.
int GameLib_ProcLoop(){
void GameLib_ProcLoop(void *data){
int i,j;
int repeat,count;
long long time;
@@ -324,8 +323,6 @@ int GameLib_ProcLoop(){
t_postproc+=Time_GetTime()-time;
fproc_count++;
return(_running);
}
@@ -333,7 +330,7 @@ int GameLib_ProcLoop(){
// GameLib_DrawLoop
//
//
void GameLib_DrawLoop(float f){
void GameLib_DrawLoop(void *data, float f){
long long time;
int i;
int game_pos[2];
@@ -414,8 +411,6 @@ void GameLib_Loop(
void (*gamepredraw)(float f),
void (*gamedraw)(float f))
{
_running=1;
_gameproc=gameproc;
_gamepostproc=gamepostproc;
_gamepredraw=gamepredraw;
@@ -427,16 +422,7 @@ void GameLib_Loop(
t_draw=0;
fproc_count=0;
fdraw_count=0;
Draw_Loop(GameLib_ProcLoop,GameLib_DrawLoop);
}
/////////////////////////////
// GameLib_BreakLoop
//
// Breaks the game loop.
void GameLib_BreakLoop(){
_running=0;
Draw_Loop(GameLib_ProcLoop,GameLib_DrawLoop,NULL);
}

View File

@@ -51,13 +51,6 @@ void GameLib_Loop(
void (*gamedraw)(float f));
/////////////////////////////
// GameLib_BreakLoop
//
// Breaks the game loop.
void GameLib_BreakLoop();
/////////////////////////////
// GameLib_GetPos
// GameLib_SetPos

View File

@@ -33,6 +33,7 @@ typedef enum {
InputKey_Right,
InputKey_Jump,
InputKey_Continue,
InputKey_Exit,
InputKey_DumpProfiling,