Port to SDL2

This commit is contained in:
2022-04-04 02:36:36 +02:00
parent 7e94c5053d
commit 48583e0b08
12 changed files with 63 additions and 60 deletions

View File

@@ -8,7 +8,7 @@
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
#include <SDL.h>
#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);
}

View File

@@ -41,7 +41,7 @@
#endif
#endif
#include "lodepng.c"
#include <SDL/SDL.h>
#include <SDL.h>
#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);

View File

@@ -1,10 +1,11 @@
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
#include <SDL/SDL.h>
#include <SDL.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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");

View File

@@ -1,6 +1,6 @@
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
#include <SDL/SDL.h>
#include <SDL.h>
#include <math.h>
#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]);
}
/////////////////////////////

View File

@@ -39,6 +39,7 @@ typedef enum {
InputKey_Exit,
InputKey_DumpProfiling,
InputKey_Screenshot,
InputKey_Max
} InputKey;

View File

@@ -4,7 +4,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Util.h"