diff --git a/GameLib/Audio.c b/GameLib/Audio.c index abacb5d..e51377e 100644 --- a/GameLib/Audio.c +++ b/GameLib/Audio.c @@ -4,6 +4,9 @@ #define _WIN32_WINNT 0x0501 #include #endif +#include +#include +#include #include #include "Audio.h" @@ -63,7 +66,11 @@ int Audio_Init(){ as.freq = 44100; as.format = AUDIO_S16SYS; as.channels = 2; +#ifdef EMSCRIPTEN + as.samples = 4096; +#else as.samples = 1024; +#endif as.callback = Audio_MixerCallback; if(SDL_OpenAudio(&as, &as2) < 0){ printf("Audio_Init: Failure opening audio.\n"); diff --git a/GameLib/Draw.c b/GameLib/Draw.c index 3d421cf..267ede6 100644 --- a/GameLib/Draw.c +++ b/GameLib/Draw.c @@ -2,6 +2,7 @@ #include #include +#include #include #ifdef WIN32 @@ -20,12 +21,17 @@ #include #endif #endif +#ifdef EMSCRIPTEN + #include +#endif #include "lodepng.c" #include #include "Time.h" #include "Util.h" #include "QuadArray2D.h" +#include "Audio.h" +#include "Input.h" #include "Draw.h" @@ -42,6 +48,7 @@ struct TDrawImage { }; + // Globals SDL_Surface *_screen=NULL; int _width; @@ -155,68 +162,96 @@ int Draw_Init(int width,int height,char *title,int pfps,int fps){ } +///////////////////////////// +// Draw_LoopIteration +// +// One iteracion of the loop updating the game window. +int (*_proc_func)()=NULL; +void (*_draw_func)(float f)=NULL; +long long _accTime; +int Draw_LoopIteration(){ + SDL_Event event; + Uint8* keys; + int done=0; + // Update screen + SDL_GL_SwapBuffers(); + + // Process Events + while(SDL_PollEvent(&event) ){ + if(event.type == SDL_QUIT ){ + done=1; + } + if(event.type == SDL_KEYDOWN ){ + if(event.key.keysym.sym == SDLK_ESCAPE ) { + done=1; + } + } + } + +#ifndef EMSCRIPTEN + // Process keys for Draw + keys=(Uint8 *)SDL_GetKeyState(NULL); + if(keys[SDLK_F12]){ + // Screenshot key + Draw_SaveScreenshoot("shot.bmp"); + } +#endif + + // Sound Frame + Audio_Frame(); + + // Process + if(_proc_func){ + if(_accTime>100000){ + _accTime=100000; + } + while(_accTime>=proc_t_frame && !done){ + Input_Frame(); + if(!_proc_func()){ + done=1; + } + _accTime-=proc_t_frame; + } + } + + // Draw + if(_draw_func){ + float frameFactor=0.0f; + frameFactor=(float)_accTime/(float)proc_t_frame; + _draw_func(frameFactor); + Draw_Flush(); + } + + return done; +} + +#ifdef EMSCRIPTEN +long long _procTime1; +long long _procTime2; +void Draw_LoopIterationAux(){ + Draw_LoopIteration(); + + // Update time + _procTime2=Time_GetTime(); + _accTime+=_procTime2-_procTime1; + _procTime1=_procTime2; +} +#endif ///////////////////////////// // Draw_Loop // // Loops updating the game window. void Draw_Loop(int (*proc)(),void (*draw)(float f)){ - int done=0; - SDL_Event event; - Uint8* keys; - long long newTime,accTime; + long long newTime; long long procTime1,procTime2,drawTime1,drawTime2,waitTime; - float frameFactor=0.0f; - accTime=proc_t_frame; + _proc_func=proc; + _draw_func=draw; +#ifndef EMSCRIPTEN + _accTime=proc_t_frame; procTime1=drawTime1=Time_GetTime(); - while(!done){ - - // Update screen - SDL_GL_SwapBuffers(); - - // Process Events - while(SDL_PollEvent(&event) ){ - if(event.type == SDL_QUIT ){ - done=1; - } - if(event.type == SDL_KEYDOWN ){ - if(event.key.keysym.sym == SDLK_ESCAPE ) { - done=1; - } - } - } - - // Process keys for Draw - keys=SDL_GetKeyState(NULL); - if(keys[SDLK_F12]){ - // Screenshot key - Draw_SaveScreenshoot("shot.bmp"); - } - - // Sound Frame - Audio_Frame(); - - // Process - if(proc){ - if(accTime>100000){ - accTime=100000; - } - while(accTime>=proc_t_frame && !done){ - Input_Frame(); - if(!proc()){ - done=1; - } - accTime-=proc_t_frame; - } - } - - // Draw - if(draw){ - frameFactor=(float)accTime/(float)proc_t_frame; - draw(frameFactor); - Draw_Flush(); - } + while(!Draw_LoopIteration()){ // Wait to round draw_t_frame drawTime2=Time_GetTime(); @@ -227,9 +262,14 @@ void Draw_Loop(int (*proc)(),void (*draw)(float f)){ // Update time procTime2=Time_GetTime(); - accTime+=procTime2-procTime1; + _accTime+=procTime2-procTime1; procTime1=procTime2; } +#else + _accTime=proc_t_frame; + _procTime1=Time_GetTime(); + emscripten_set_main_loop(Draw_LoopIterationAux, _fps, 1); +#endif } diff --git a/GameLib/GameLib.c b/GameLib/GameLib.c index 4c7c7c8..4e21348 100644 --- a/GameLib/GameLib.c +++ b/GameLib/GameLib.c @@ -1,6 +1,9 @@ // Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado) #include +#include +#include +#include #include #include "Time.h" diff --git a/GameLib/Input.c b/GameLib/Input.c index 33b2b77..2d48ae9 100644 --- a/GameLib/Input.c +++ b/GameLib/Input.c @@ -57,7 +57,7 @@ void Input_Frame(){ Uint8* keys; // Process Keys - keys=SDL_GetKeyState(NULL); + keys=(Uint8 *)SDL_GetKeyState(NULL); Input_SetKey(InputKey_Action1,keys[SDLK_z]); Input_SetKey(InputKey_Action2,keys[SDLK_x]); Input_SetKey(InputKey_Up,keys[SDLK_UP]); diff --git a/GameLib/Util.c b/GameLib/Util.c index 76df057..1e6f367 100644 --- a/GameLib/Util.c +++ b/GameLib/Util.c @@ -1,6 +1,8 @@ // Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado) #include +#include +#include #include "Util.h" @@ -187,7 +189,6 @@ int absmod(int v,int d){ return(v%d); } } - float fabsmod(float v,int d){ if(v<0){ v+=d*((((int)(v/d))*(-1))+1); @@ -199,6 +200,17 @@ float fabsmod(float v,int d){ } +///////////////////////////// +// IsBigEndian +// +int IsBigEndian(){ + union{ + unsigned int i; + char c[4]; + } bint={0x01020304}; + return bint.c[0]==1; +} + ///////////////////////////// // EndsWith diff --git a/GameLib/Util.h b/GameLib/Util.h index bc85c12..72224fa 100644 --- a/GameLib/Util.h +++ b/GameLib/Util.h @@ -65,6 +65,11 @@ int absmod(int v,int d); float fabsmod(float v,int d); +///////////////////////////// +// IsBigEndian +// +int IsBigEndian(); + ///////////////////////////// // EndsWith diff --git a/Makefile.common b/Makefile.common index a09558d..e71f767 100644 --- a/Makefile.common +++ b/Makefile.common @@ -50,8 +50,9 @@ clean: rm -f $(OBJS) $(BUILDDIR)/$(RESULT) run: $(BUILDDIR)/$(RESULT) - ./$(BUILDDIR)/$(RESULT) debug + $(LAUNCHER) ./$(BUILDDIR)/$(RESULT) debug +rebuild: clean all ################# # GameLib Rules # @@ -95,7 +96,7 @@ $(BUILDDIR)/main.o: main.c $(HEADS) ################ $(BUILDDIR)/$(RESULT): $(OBJS) - $(CC) -o $(BUILDDIR)/$(RESULT) $(OBJS) $(LIBS) $(CFLAGS) + $(CC) -o $(BUILDDIR)/$(RESULT) $(OBJS) $(LIBS) $(CFLAGS) $(LDFLAGS) diff --git a/Makefile.emscripten b/Makefile.emscripten new file mode 100644 index 0000000..c3ffb1f --- /dev/null +++ b/Makefile.emscripten @@ -0,0 +1,14 @@ +CC= python /c/Program\ Files/Emscripten/emscripten/1.7.8/emcc +LAUNCHER= start +RM=rm -rf + +LIBS= +CFLAGS= -s LEGACY_GL_EMULATION=1 -s ASM_JS=1 -O1 +LDFLAGS= --preload-file data + +RESULT=game.html +BUILDDIR=build-emscripten + +include Makefile.common + + diff --git a/Makefile.linux b/Makefile.linux index 3771781..bee462e 100644 --- a/Makefile.linux +++ b/Makefile.linux @@ -1,7 +1,10 @@ +CC=gcc +LAUNCHER= +RM=rm -rf + LIBS= -lSDL -lpthread -L/usr/X11R6/lib -L/usr/lib -lm -lGL -lX11 CFLAGS= -Wall -g -I/usr/include/ -I/usr/include/SDL/ -I/usr/X11R6/include/ -CC=gcc -RM=rm -rf +LDFLAGS= RESULT=game BUILDDIR=build-linux diff --git a/Makefile.mingw b/Makefile.mingw index ff4151e..58e8e56 100644 --- a/Makefile.mingw +++ b/Makefile.mingw @@ -1,7 +1,10 @@ +CC= i486-mingw32-gcc +LAUNCHER= +RM=rm -rf + LIBS= -L/usr/i486-mingw/lib -D_GNU_SOURCE=1 -Dmain=SDL_main -lopengl32 CFLAGS= -I/usr/i486-mingw/include -lmingw32 -lSDLmain -lSDL -mwindows -CC= i486-mingw32-gcc -RM=rm -rf +LDFLAGS= RESULT=game.exe BUILDDIR=build-mingw diff --git a/Makefile.win32 b/Makefile.win32 index 91f85a9..1ae5bf6 100644 --- a/Makefile.win32 +++ b/Makefile.win32 @@ -1,7 +1,10 @@ +CC=gcc +LAUNCHER= +RM=rm -rf + LIBS=-I/mingw/include/SDL -D_GNU_SOURCE=1 -Dmain=SDL_main -lopengl32 CFLAGS= -L/mingw/lib -lmingw32 -lSDLmain -lSDL -mwindows -g -CC=gcc -RM=rm -rf +LDFLAGS= RESULT=game.exe BUILDDIR=build-mingw diff --git a/main.c b/main.c index b68e9d6..7176f70 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ #include #include +#include #include #include