Port to SDL2
This commit is contained in:
2
.vscode/c_cpp_properties.json
vendored
2
.vscode/c_cpp_properties.json
vendored
@@ -16,6 +16,7 @@
|
|||||||
"name": "Linux",
|
"name": "Linux",
|
||||||
"includePath": [
|
"includePath": [
|
||||||
"/usr/include",
|
"/usr/include",
|
||||||
|
"/usr/include/SDL2",
|
||||||
"${workspaceFolder}/src/"
|
"${workspaceFolder}/src/"
|
||||||
],
|
],
|
||||||
"intelliSenseMode": "clang-x64",
|
"intelliSenseMode": "clang-x64",
|
||||||
@@ -27,6 +28,7 @@
|
|||||||
"name": "Win32",
|
"name": "Win32",
|
||||||
"includePath": [
|
"includePath": [
|
||||||
"C:/msys64/mingw64/include",
|
"C:/msys64/mingw64/include",
|
||||||
|
"C:/msys64/mingw64/include/SDL2",
|
||||||
"${workspaceFolder}/src/"
|
"${workspaceFolder}/src/"
|
||||||
],
|
],
|
||||||
"defines": [
|
"defines": [
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
Feel free to customize this file to suit your needs
|
Feel free to customize this file to suit your needs
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <SDL/SDL.h>
|
#include <SDL.h>
|
||||||
#import <Cocoa/Cocoa.h>
|
#import <Cocoa/Cocoa.h>
|
||||||
|
|
||||||
@interface SDLMain : NSObject
|
@interface SDLMain : NSObject
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ Entity GameMapAux_CreateEnt(Entity ent, int i, int j, int res) {
|
|||||||
e = Entity_Copy(ent);
|
e = Entity_Copy(ent);
|
||||||
vec2_set(pos, (res / 2) + i * res, (res / 2) + j * res);
|
vec2_set(pos, (res / 2) + i * res, (res / 2) + j * res);
|
||||||
vec2_plus(e->pos, e->pos, pos);
|
vec2_plus(e->pos, e->pos, pos);
|
||||||
Entity_CalcBBox(e);
|
|
||||||
GameLib_AddEntity(e);
|
GameLib_AddEntity(e);
|
||||||
return (e);
|
return (e);
|
||||||
}
|
}
|
||||||
|
|||||||
8
Makefile
8
Makefile
@@ -21,8 +21,8 @@ ifeq ($(TARGET_ARCH),mingw)
|
|||||||
MKDIR := mkdir
|
MKDIR := mkdir
|
||||||
ECHO := echo
|
ECHO := echo
|
||||||
|
|
||||||
LIBS := -L/mingw/lib -lopengl32 -lSDL -lm
|
LIBS := -L/mingw/lib -lopengl32 -lm $(shell sdl2-config --libs)
|
||||||
CFLAGS := -g -mwindows -D_GNU_SOURCE=1 -DWIN32
|
CFLAGS := -g -mwindows -D_GNU_SOURCE=1 -DWIN32 $(shell sdl2-config --cflags)
|
||||||
LDFLAGS := -g -mwindows -D_GNU_SOURCE=1
|
LDFLAGS := -g -mwindows -D_GNU_SOURCE=1
|
||||||
|
|
||||||
RES_GAMELIB := libgame.a
|
RES_GAMELIB := libgame.a
|
||||||
@@ -36,8 +36,8 @@ ifeq ($(TARGET_ARCH),linux)
|
|||||||
MKDIR := mkdir
|
MKDIR := mkdir
|
||||||
ECHO := echo
|
ECHO := echo
|
||||||
|
|
||||||
LIBS := -lSDL -lpthread -L/usr/X11R6/lib -L/usr/lib -lm -lGL -lX11
|
LIBS := -lpthread -L/usr/X11R6/lib -L/usr/lib -lm -lGL -lX11 $(shell sdl2-config --libs)
|
||||||
CFLAGS := -Wall -g -I/usr/include/ -I/usr/include/SDL/ -I/usr/X11R6/include/
|
CFLAGS := -Wall -g -I/usr/include/ -I/usr/X11R6/include/ $(shell sdl2-config --cflags)
|
||||||
LDFLAGS :=
|
LDFLAGS :=
|
||||||
|
|
||||||
RES_GAMELIB := libgame.a
|
RES_GAMELIB := libgame.a
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ fi
|
|||||||
case "$uname" in
|
case "$uname" in
|
||||||
*MINGW* | *MSYS*)
|
*MINGW* | *MSYS*)
|
||||||
# Configuracion de Win32/Mingw
|
# Configuracion de Win32/Mingw
|
||||||
libs="-L/mingw/lib -lopengl32 -lSDL"
|
libs="-lopengl32 $(sdl2-config --libs)"
|
||||||
cflags="-g -mwindows -D_GNU_SOURCE=1 -DWIN32"
|
cflags="-g -mwindows -D_GNU_SOURCE=1 -DWIN32 $(sdl2-config --cflags)"
|
||||||
builddir="build-$gcctarget"
|
builddir="build-$gcctarget"
|
||||||
platform="$gcctarget"
|
platform="$gcctarget"
|
||||||
exeextension=".exe"
|
exeextension=".exe"
|
||||||
@@ -47,8 +47,8 @@ case "$uname" in
|
|||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
# Configuracion de Linux
|
# Configuracion de Linux
|
||||||
libs="-lSDL -lpthread -L/usr/X11R6/lib -L/usr/lib -lm -lGL -lX11"
|
libs="-lpthread -L/usr/X11R6/lib -L/usr/lib -lm -lGL -lX11 $(sdl2-config --libs)"
|
||||||
cflags="-Wall -g -I/usr/include/ -I/usr/include/SDL/ -I/usr/X11R6/include/"
|
cflags="-Wall -g -I/usr/include/ -I/usr/X11R6/include/ $(sdl2-config --cflags)"
|
||||||
builddir="build-$gcctarget"
|
builddir="build-$gcctarget"
|
||||||
platform="$gcctarget"
|
platform="$gcctarget"
|
||||||
exeextension=""
|
exeextension=""
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
Feel free to customize this file to suit your needs
|
Feel free to customize this file to suit your needs
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <SDL/SDL.h>
|
#include <SDL.h>
|
||||||
#import <Cocoa/Cocoa.h>
|
#import <Cocoa/Cocoa.h>
|
||||||
|
|
||||||
@interface SDLMain : NSObject
|
@interface SDLMain : NSObject
|
||||||
|
|||||||
15
src/Audio.c
15
src/Audio.c
@@ -8,7 +8,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <SDL/SDL.h>
|
#include <SDL.h>
|
||||||
|
|
||||||
#include "Audio.h"
|
#include "Audio.h"
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
@@ -50,6 +50,8 @@ struct TAudioChan {
|
|||||||
AudioChan _channels = NULL;
|
AudioChan _channels = NULL;
|
||||||
AudioChan _free_channels = NULL;
|
AudioChan _free_channels = NULL;
|
||||||
|
|
||||||
|
static SDL_AudioDeviceID _audioDeviceID = 0;
|
||||||
|
|
||||||
/////////////////////////////
|
/////////////////////////////
|
||||||
// Audio_Init
|
// Audio_Init
|
||||||
//
|
//
|
||||||
@@ -58,11 +60,7 @@ int Audio_Init() {
|
|||||||
SDL_AudioSpec as;
|
SDL_AudioSpec as;
|
||||||
SDL_AudioSpec as2;
|
SDL_AudioSpec as2;
|
||||||
|
|
||||||
// Initialize audio subsistem
|
// Initialize audio subsistem
|
||||||
#ifdef WIN32
|
|
||||||
// Force DSound Driver on win32
|
|
||||||
putenv("SDL_AUDIODRIVER=dsound");
|
|
||||||
#endif
|
|
||||||
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
|
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
|
||||||
Print("Audio_Init: Failure initializing SDL Audio.\n");
|
Print("Audio_Init: Failure initializing SDL Audio.\n");
|
||||||
Print("\tSDL Error: %s\n", SDL_GetError());
|
Print("\tSDL Error: %s\n", SDL_GetError());
|
||||||
@@ -75,7 +73,8 @@ int Audio_Init() {
|
|||||||
as.channels = 2;
|
as.channels = 2;
|
||||||
as.samples = 2048;
|
as.samples = 2048;
|
||||||
as.callback = Audio_MixerCallback;
|
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("Audio_Init: Failure opening audio.\n");
|
||||||
Print("\tSDL Error: %s\n", SDL_GetError());
|
Print("\tSDL Error: %s\n", SDL_GetError());
|
||||||
return (0);
|
return (0);
|
||||||
@@ -89,7 +88,7 @@ int Audio_Init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Unpause and ready to go
|
// Unpause and ready to go
|
||||||
SDL_PauseAudio(0);
|
SDL_PauseAudioDevice(_audioDeviceID, 0);
|
||||||
|
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|||||||
43
src/Draw.c
43
src/Draw.c
@@ -41,7 +41,7 @@
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
#include "lodepng.c"
|
#include "lodepng.c"
|
||||||
#include <SDL/SDL.h>
|
#include <SDL.h>
|
||||||
|
|
||||||
#ifndef GL_CLAMP_TO_EDGE
|
#ifndef GL_CLAMP_TO_EDGE
|
||||||
#define GL_CLAMP_TO_EDGE 0x812F
|
#define GL_CLAMP_TO_EDGE 0x812F
|
||||||
@@ -68,7 +68,8 @@ struct TDrawImage {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Globals
|
// Globals
|
||||||
SDL_Surface *_screen = NULL;
|
static SDL_Window *_window = NULL;
|
||||||
|
static SDL_GLContext _glcontext = NULL;
|
||||||
int _width;
|
int _width;
|
||||||
int _height;
|
int _height;
|
||||||
long long proc_t_frame = 33333;
|
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
|
// Initialize video mode
|
||||||
_screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_OPENGL);
|
_window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
|
||||||
if (_screen == NULL) {
|
SDL_WINDOWPOS_UNDEFINED, width, height,
|
||||||
|
SDL_WINDOW_OPENGL);
|
||||||
|
if (_window == NULL) {
|
||||||
Print("Draw_Init: Failure initializing video mode.\n");
|
Print("Draw_Init: Failure initializing video mode.\n");
|
||||||
Print("\tSDL Error: %s\n", SDL_GetError());
|
Print("\tSDL Error: %s\n", SDL_GetError());
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
SDL_WM_SetCaption(title, NULL);
|
_glcontext = SDL_GL_CreateContext(_window);
|
||||||
Draw_ShowInfo();
|
Draw_ShowInfo();
|
||||||
|
|
||||||
#if USE_OpenGL
|
#if USE_OpenGL
|
||||||
@@ -206,11 +209,11 @@ int Draw_Init(int width, int height, char *title, int pfps, int fps) {
|
|||||||
|
|
||||||
// Triplebuffer swap
|
// Triplebuffer swap
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
SDL_GL_SwapBuffers();
|
SDL_GL_SwapWindow(_window);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
SDL_GL_SwapBuffers();
|
SDL_GL_SwapWindow(_window);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
SDL_GL_SwapBuffers();
|
SDL_GL_SwapWindow(_window);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
glEnableClientState(GL_COLOR_ARRAY);
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
@@ -470,7 +473,7 @@ int Draw_LoopIteration() {
|
|||||||
Input_SetKey(InputKey_Exit, 1);
|
Input_SetKey(InputKey_Exit, 1);
|
||||||
}
|
}
|
||||||
if (event.type == SDL_KEYDOWN) {
|
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);
|
Input_SetKey(InputKey_Exit, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -517,7 +520,7 @@ int Draw_LoopIteration() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (event.type == SDL_KEYDOWN) {
|
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);
|
Input_SetKey(InputKey_Exit, 1);
|
||||||
if (!_draw_exitoverrided) {
|
if (!_draw_exitoverrided) {
|
||||||
_draw_looping = 0;
|
_draw_looping = 0;
|
||||||
@@ -541,22 +544,6 @@ int Draw_LoopIteration() {
|
|||||||
}
|
}
|
||||||
#endif
|
#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
|
// Process
|
||||||
if (_proc_func) {
|
if (_proc_func) {
|
||||||
if (_accTime > 100000) {
|
if (_accTime > 100000) {
|
||||||
@@ -574,7 +561,7 @@ int Draw_LoopIteration() {
|
|||||||
Audio_Frame();
|
Audio_Frame();
|
||||||
|
|
||||||
// Draw
|
// Draw
|
||||||
SDL_GL_SwapBuffers();
|
SDL_GL_SwapWindow(_window);
|
||||||
if (_draw_func) {
|
if (_draw_func) {
|
||||||
_draw_func(_data, (float)_accTime / (float)proc_t_frame);
|
_draw_func(_data, (float)_accTime / (float)proc_t_frame);
|
||||||
Draw_Flush();
|
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 = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0, 0, 0, 0);
|
||||||
surf->format->Amask = 0xFF000000;
|
surf->format->Amask = 0xFF000000;
|
||||||
surf->format->Ashift = 24;
|
surf->format->Ashift = 24;
|
||||||
SDL_SetAlpha(surf, SDL_SRCALPHA, 255);
|
//SDL_SetAlpha(surf, GL_SRC_ALPHA, 255);
|
||||||
SDL_LockSurface(surf);
|
SDL_LockSurface(surf);
|
||||||
memcpy(surf->pixels, data, width * height * 4);
|
memcpy(surf->pixels, data, width * height * 4);
|
||||||
SDL_UnlockSurface(surf);
|
SDL_UnlockSurface(surf);
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
||||||
|
|
||||||
#include <SDL/SDL.h>
|
#include <SDL.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "Anim.h"
|
#include "Anim.h"
|
||||||
#include "Audio.h"
|
#include "Audio.h"
|
||||||
@@ -410,6 +411,20 @@ void GameLib_DrawLoop(void *data, float f) {
|
|||||||
|
|
||||||
fdraw_count++;
|
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 &&
|
if (Input_GetKey(InputKey_DumpProfiling) == InputKey_Pressed &&
|
||||||
fproc_count > 0 && fdraw_count > 0) {
|
fproc_count > 0 && fdraw_count > 0) {
|
||||||
Print("Profiling:::::::::\n");
|
Print("Profiling:::::::::\n");
|
||||||
|
|||||||
23
src/Input.c
23
src/Input.c
@@ -1,6 +1,6 @@
|
|||||||
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
||||||
|
|
||||||
#include <SDL/SDL.h>
|
#include <SDL.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#ifdef EMSCRIPTEN
|
#ifdef EMSCRIPTEN
|
||||||
#define SDL_GetKeyState SDL_GetKeyboardState
|
#define SDL_GetKeyState SDL_GetKeyboardState
|
||||||
@@ -43,20 +43,21 @@ void Input_Frame() {
|
|||||||
Uint8 *keys;
|
Uint8 *keys;
|
||||||
|
|
||||||
// Get keyboard state
|
// Get keyboard state
|
||||||
keys = (Uint8 *)SDL_GetKeyState(NULL);
|
keys = (Uint8 *)SDL_GetKeyboardState(NULL);
|
||||||
|
|
||||||
// Process Keys
|
// Process Keys
|
||||||
Input_SetKey(InputKey_Action1, keys[SDLK_z] | keys[SDLK_o]);
|
Input_SetKey(InputKey_Action1, keys[SDL_SCANCODE_Z] | keys[SDL_SCANCODE_O]);
|
||||||
Input_SetKey(InputKey_Action2, keys[SDLK_x] | keys[SDLK_p]);
|
Input_SetKey(InputKey_Action2, keys[SDL_SCANCODE_X] | keys[SDL_SCANCODE_P]);
|
||||||
Input_SetKey(InputKey_Up, keys[SDLK_UP] | keys[SDLK_w]);
|
Input_SetKey(InputKey_Up, keys[SDL_SCANCODE_UP] | keys[SDL_SCANCODE_W]);
|
||||||
Input_SetKey(InputKey_Down, keys[SDLK_DOWN] | keys[SDLK_s]);
|
Input_SetKey(InputKey_Down, keys[SDL_SCANCODE_DOWN] | keys[SDL_SCANCODE_S]);
|
||||||
Input_SetKey(InputKey_Left, keys[SDLK_LEFT] | keys[SDLK_a]);
|
Input_SetKey(InputKey_Left, keys[SDL_SCANCODE_LEFT] | keys[SDL_SCANCODE_A]);
|
||||||
Input_SetKey(InputKey_Right, keys[SDLK_RIGHT] | keys[SDLK_d]);
|
Input_SetKey(InputKey_Right, keys[SDL_SCANCODE_RIGHT] | keys[SDL_SCANCODE_D]);
|
||||||
Input_SetKey(InputKey_Jump, keys[SDLK_SPACE]);
|
Input_SetKey(InputKey_Jump, keys[SDL_SCANCODE_SPACE]);
|
||||||
Input_SetKey(InputKey_Continue,
|
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]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////////////////////
|
/////////////////////////////
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ typedef enum {
|
|||||||
InputKey_Exit,
|
InputKey_Exit,
|
||||||
|
|
||||||
InputKey_DumpProfiling,
|
InputKey_DumpProfiling,
|
||||||
|
InputKey_Screenshot,
|
||||||
|
|
||||||
InputKey_Max
|
InputKey_Max
|
||||||
} InputKey;
|
} InputKey;
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user