diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index af890e3..ff7961e 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -16,6 +16,7 @@ "name": "Linux", "includePath": [ "/usr/include", + "/usr/include/SDL2", "${workspaceFolder}/src/" ], "intelliSenseMode": "clang-x64", @@ -27,6 +28,7 @@ "name": "Win32", "includePath": [ "C:/msys64/mingw64/include", + "C:/msys64/mingw64/include/SDL2", "${workspaceFolder}/src/" ], "defines": [ diff --git a/Example.GameLib/macosx/SDLMain.m b/Example.GameLib/macosx/SDLMain.m index 8d3cda9..62bc171 100644 --- a/Example.GameLib/macosx/SDLMain.m +++ b/Example.GameLib/macosx/SDLMain.m @@ -5,7 +5,7 @@ Feel free to customize this file to suit your needs */ -#include +#include #import @interface SDLMain : NSObject diff --git a/Example.GameLib/src/GameMap.c b/Example.GameLib/src/GameMap.c index ff8acd3..147c3a8 100644 --- a/Example.GameLib/src/GameMap.c +++ b/Example.GameLib/src/GameMap.c @@ -39,7 +39,6 @@ Entity GameMapAux_CreateEnt(Entity ent, int i, int j, int res) { e = Entity_Copy(ent); vec2_set(pos, (res / 2) + i * res, (res / 2) + j * res); vec2_plus(e->pos, e->pos, pos); - Entity_CalcBBox(e); GameLib_AddEntity(e); return (e); } diff --git a/Makefile b/Makefile index b4043d2..343f59b 100644 --- a/Makefile +++ b/Makefile @@ -21,8 +21,8 @@ ifeq ($(TARGET_ARCH),mingw) MKDIR := mkdir ECHO := echo - LIBS := -L/mingw/lib -lopengl32 -lSDL -lm - CFLAGS := -g -mwindows -D_GNU_SOURCE=1 -DWIN32 + LIBS := -L/mingw/lib -lopengl32 -lm $(shell sdl2-config --libs) + CFLAGS := -g -mwindows -D_GNU_SOURCE=1 -DWIN32 $(shell sdl2-config --cflags) LDFLAGS := -g -mwindows -D_GNU_SOURCE=1 RES_GAMELIB := libgame.a @@ -36,8 +36,8 @@ ifeq ($(TARGET_ARCH),linux) MKDIR := mkdir ECHO := echo - 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/ + LIBS := -lpthread -L/usr/X11R6/lib -L/usr/lib -lm -lGL -lX11 $(shell sdl2-config --libs) + CFLAGS := -Wall -g -I/usr/include/ -I/usr/X11R6/include/ $(shell sdl2-config --cflags) LDFLAGS := RES_GAMELIB := libgame.a diff --git a/gamelib-config b/gamelib-config index 72ff9b7..11628d6 100644 --- a/gamelib-config +++ b/gamelib-config @@ -23,8 +23,8 @@ fi case "$uname" in *MINGW* | *MSYS*) # Configuracion de Win32/Mingw - libs="-L/mingw/lib -lopengl32 -lSDL" - cflags="-g -mwindows -D_GNU_SOURCE=1 -DWIN32" + libs="-lopengl32 $(sdl2-config --libs)" + cflags="-g -mwindows -D_GNU_SOURCE=1 -DWIN32 $(sdl2-config --cflags)" builddir="build-$gcctarget" platform="$gcctarget" exeextension=".exe" @@ -47,8 +47,8 @@ case "$uname" in ;; *) # Configuracion de Linux - 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/" + libs="-lpthread -L/usr/X11R6/lib -L/usr/lib -lm -lGL -lX11 $(sdl2-config --libs)" + cflags="-Wall -g -I/usr/include/ -I/usr/X11R6/include/ $(sdl2-config --cflags)" builddir="build-$gcctarget" platform="$gcctarget" exeextension="" diff --git a/macosx/SDLMain.m b/macosx/SDLMain.m index 8d3cda9..62bc171 100644 --- a/macosx/SDLMain.m +++ b/macosx/SDLMain.m @@ -5,7 +5,7 @@ Feel free to customize this file to suit your needs */ -#include +#include #import @interface SDLMain : NSObject diff --git a/src/Audio.c b/src/Audio.c index 2a0201f..32707b9 100644 --- a/src/Audio.c +++ b/src/Audio.c @@ -8,7 +8,7 @@ #include #include -#include +#include #include "Audio.h" #include "Util.h" @@ -50,6 +50,8 @@ struct TAudioChan { AudioChan _channels = NULL; AudioChan _free_channels = NULL; +static SDL_AudioDeviceID _audioDeviceID = 0; + ///////////////////////////// // Audio_Init // @@ -58,11 +60,7 @@ int Audio_Init() { SDL_AudioSpec as; SDL_AudioSpec as2; -// Initialize audio subsistem -#ifdef WIN32 - // Force DSound Driver on win32 - putenv("SDL_AUDIODRIVER=dsound"); -#endif + // Initialize audio subsistem if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { Print("Audio_Init: Failure initializing SDL Audio.\n"); Print("\tSDL Error: %s\n", SDL_GetError()); @@ -75,7 +73,8 @@ int Audio_Init() { as.channels = 2; as.samples = 2048; as.callback = Audio_MixerCallback; - if (SDL_OpenAudio(&as, &as2) < 0) { + _audioDeviceID = SDL_OpenAudioDevice(NULL, 0, &as, &as2, 0); + if (_audioDeviceID == 0) { Print("Audio_Init: Failure opening audio.\n"); Print("\tSDL Error: %s\n", SDL_GetError()); return (0); @@ -89,7 +88,7 @@ int Audio_Init() { } // Unpause and ready to go - SDL_PauseAudio(0); + SDL_PauseAudioDevice(_audioDeviceID, 0); return (1); } diff --git a/src/Draw.c b/src/Draw.c index 88dce08..5ee4256 100644 --- a/src/Draw.c +++ b/src/Draw.c @@ -41,7 +41,7 @@ #endif #endif #include "lodepng.c" -#include +#include #ifndef GL_CLAMP_TO_EDGE #define GL_CLAMP_TO_EDGE 0x812F @@ -68,7 +68,8 @@ struct TDrawImage { }; // Globals -SDL_Surface *_screen = NULL; +static SDL_Window *_window = NULL; +static SDL_GLContext _glcontext = NULL; int _width; int _height; long long proc_t_frame = 33333; @@ -186,13 +187,15 @@ int Draw_Init(int width, int height, char *title, int pfps, int fps) { } // Initialize video mode - _screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_OPENGL); - if (_screen == NULL) { + _window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, width, height, + SDL_WINDOW_OPENGL); + if (_window == NULL) { Print("Draw_Init: Failure initializing video mode.\n"); Print("\tSDL Error: %s\n", SDL_GetError()); return (0); } - SDL_WM_SetCaption(title, NULL); + _glcontext = SDL_GL_CreateContext(_window); Draw_ShowInfo(); #if USE_OpenGL @@ -206,11 +209,11 @@ int Draw_Init(int width, int height, char *title, int pfps, int fps) { // Triplebuffer swap glClear(GL_COLOR_BUFFER_BIT); - SDL_GL_SwapBuffers(); + SDL_GL_SwapWindow(_window); glClear(GL_COLOR_BUFFER_BIT); - SDL_GL_SwapBuffers(); + SDL_GL_SwapWindow(_window); glClear(GL_COLOR_BUFFER_BIT); - SDL_GL_SwapBuffers(); + SDL_GL_SwapWindow(_window); glClear(GL_COLOR_BUFFER_BIT); glEnableClientState(GL_COLOR_ARRAY); @@ -470,7 +473,7 @@ int Draw_LoopIteration() { Input_SetKey(InputKey_Exit, 1); } if (event.type == SDL_KEYDOWN) { - if (event.key.keysym.sym == SDLK_ESCAPE) { + if (event.key.keysym.sym == SDL_SCANCODE_ESCAPE) { Input_SetKey(InputKey_Exit, 1); } } @@ -517,7 +520,7 @@ int Draw_LoopIteration() { } } if (event.type == SDL_KEYDOWN) { - if (event.key.keysym.sym == SDLK_ESCAPE) { + if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { Input_SetKey(InputKey_Exit, 1); if (!_draw_exitoverrided) { _draw_looping = 0; @@ -541,22 +544,6 @@ int Draw_LoopIteration() { } #endif -#ifndef EMSCRIPTEN - // Process keys for Draw - Uint8 *keys; - keys = (Uint8 *)SDL_GetKeyState(NULL); - if (keys[SDLK_F12]) { - // Screenshot key - char strFile[255]; - int idx = -1; - do { - idx++; - snprintf(strFile, 255, "shot-%04d.png", idx); - } while (access(strFile, F_OK) != -1); - Draw_SaveScreenshoot(strFile); - } -#endif - // Process if (_proc_func) { if (_accTime > 100000) { @@ -574,7 +561,7 @@ int Draw_LoopIteration() { Audio_Frame(); // Draw - SDL_GL_SwapBuffers(); + SDL_GL_SwapWindow(_window); if (_draw_func) { _draw_func(_data, (float)_accTime / (float)proc_t_frame); Draw_Flush(); @@ -1079,7 +1066,7 @@ void Draw_SaveRGBAToBMP(char *filename, unsigned char *data, int width, surf = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0, 0, 0, 0); surf->format->Amask = 0xFF000000; surf->format->Ashift = 24; - SDL_SetAlpha(surf, SDL_SRCALPHA, 255); + //SDL_SetAlpha(surf, GL_SRC_ALPHA, 255); SDL_LockSurface(surf); memcpy(surf->pixels, data, width * height * 4); SDL_UnlockSurface(surf); diff --git a/src/GameLib.c b/src/GameLib.c index cff6094..aaaa408 100644 --- a/src/GameLib.c +++ b/src/GameLib.c @@ -1,10 +1,11 @@ // Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado) -#include +#include #include #include #include #include +#include #include "Anim.h" #include "Audio.h" @@ -410,6 +411,20 @@ void GameLib_DrawLoop(void *data, float f) { fdraw_count++; +#ifndef EMSCRIPTEN + if (Input_GetKey(InputKey_Screenshot) == InputKey_Pressed) { + // Screenshot key + char strFile[255]; + int idx = -1; + do { + idx++; + snprintf(strFile, 255, "shot-%04d.png", idx); + } while (access(strFile, F_OK) != -1); + Draw_SaveScreenshoot(strFile); + Print("Screenshot saved \"%s\"\n", strFile); + } +#endif // EMSCRIPTEN + if (Input_GetKey(InputKey_DumpProfiling) == InputKey_Pressed && fproc_count > 0 && fdraw_count > 0) { Print("Profiling:::::::::\n"); diff --git a/src/Input.c b/src/Input.c index 2ce4179..4cc6351 100644 --- a/src/Input.c +++ b/src/Input.c @@ -1,6 +1,6 @@ // Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado) -#include +#include #include #ifdef EMSCRIPTEN #define SDL_GetKeyState SDL_GetKeyboardState @@ -43,20 +43,21 @@ void Input_Frame() { Uint8 *keys; // Get keyboard state - keys = (Uint8 *)SDL_GetKeyState(NULL); + keys = (Uint8 *)SDL_GetKeyboardState(NULL); // Process Keys - Input_SetKey(InputKey_Action1, keys[SDLK_z] | keys[SDLK_o]); - Input_SetKey(InputKey_Action2, keys[SDLK_x] | keys[SDLK_p]); - Input_SetKey(InputKey_Up, keys[SDLK_UP] | keys[SDLK_w]); - Input_SetKey(InputKey_Down, keys[SDLK_DOWN] | keys[SDLK_s]); - Input_SetKey(InputKey_Left, keys[SDLK_LEFT] | keys[SDLK_a]); - Input_SetKey(InputKey_Right, keys[SDLK_RIGHT] | keys[SDLK_d]); - Input_SetKey(InputKey_Jump, keys[SDLK_SPACE]); + Input_SetKey(InputKey_Action1, keys[SDL_SCANCODE_Z] | keys[SDL_SCANCODE_O]); + Input_SetKey(InputKey_Action2, keys[SDL_SCANCODE_X] | keys[SDL_SCANCODE_P]); + Input_SetKey(InputKey_Up, keys[SDL_SCANCODE_UP] | keys[SDL_SCANCODE_W]); + Input_SetKey(InputKey_Down, keys[SDL_SCANCODE_DOWN] | keys[SDL_SCANCODE_S]); + Input_SetKey(InputKey_Left, keys[SDL_SCANCODE_LEFT] | keys[SDL_SCANCODE_A]); + Input_SetKey(InputKey_Right, keys[SDL_SCANCODE_RIGHT] | keys[SDL_SCANCODE_D]); + Input_SetKey(InputKey_Jump, keys[SDL_SCANCODE_SPACE]); Input_SetKey(InputKey_Continue, - keys[SDLK_RETURN] | keys[SDLK_KP_ENTER] | _pointerDown); + keys[SDL_SCANCODE_RETURN] | keys[SDL_SCANCODE_RETURN2] | keys[SDL_SCANCODE_KP_ENTER] | _pointerDown); - Input_SetKey(InputKey_DumpProfiling, keys[SDLK_m]); + Input_SetKey(InputKey_DumpProfiling, keys[SDL_SCANCODE_M]); + Input_SetKey(InputKey_Screenshot, keys[SDL_SCANCODE_F12]); } ///////////////////////////// diff --git a/src/Input.h b/src/Input.h index f6414fa..f8607bd 100644 --- a/src/Input.h +++ b/src/Input.h @@ -39,6 +39,7 @@ typedef enum { InputKey_Exit, InputKey_DumpProfiling, + InputKey_Screenshot, InputKey_Max } InputKey; diff --git a/src/Util.c b/src/Util.c index 1c9b98b..7520f24 100644 --- a/src/Util.c +++ b/src/Util.c @@ -4,7 +4,6 @@ #include #include #include -#include #include "Util.h"