#include "stdafx.h"
#if defined(WITH_SDL)
#include "ttd.h"
#include "gfx.h"
#include "sound.h"
#include "window.h"
#include <SDL.h>
#include "player.h"
#include "hal.h"
#ifdef UNIX
#include <signal.h>
#ifdef __MORPHOS__
#define SIG_DFL (void (*)(int))0
#endif
#define DYNAMICALLY_LOADED_SDL
static SDL_Surface *_sdl_screen;
static int _sdl_usage;
static bool _all_modes;
#define MAX_DIRTY_RECTS 100
static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS];
static int _num_dirty_rects;
#define SDL_CALL
#if defined(DYNAMICALLY_LOADED_SDL) && defined(WIN32)
bool LoadLibraryList(void **proc, const char *dll);
typedef struct {
int (SDLCALL *SDL_Init)(Uint32);
int (SDLCALL *SDL_InitSubSystem)(Uint32);
char *(SDLCALL *SDL_GetError)();
void (SDLCALL *SDL_QuitSubSystem)(Uint32);
void (SDLCALL *SDL_UpdateRect)(SDL_Surface *, Sint32, Sint32, Uint32, Uint32);
void (SDLCALL *SDL_UpdateRects)(SDL_Surface *, int, SDL_Rect *);
int (SDLCALL *SDL_SetColors)(SDL_Surface *, SDL_Color *, int, int);
void (SDLCALL *SDL_WM_SetCaption)(const char *, const char *);
int (SDLCALL *SDL_ShowCursor)(int);
void (SDLCALL *SDL_FreeSurface)(SDL_Surface *);
int (SDLCALL *SDL_PollEvent)(SDL_Event *);
void (SDLCALL *SDL_WarpMouse)(Uint16, Uint16);
uint32 (SDLCALL *SDL_GetTicks)();
int (SDLCALL *SDL_OpenAudio)(SDL_AudioSpec *, SDL_AudioSpec*);
void (SDLCALL *SDL_PauseAudio)(int);
void (SDLCALL *SDL_CloseAudio)();
int (SDLCALL *SDL_LockSurface)(SDL_Surface*);
void (SDLCALL *SDL_UnlockSurface)(SDL_Surface*);
SDLMod (SDLCALL *SDL_GetModState)();
void (SDLCALL *SDL_Delay)(Uint32);
void (SDLCALL *SDL_Quit)();
SDL_Surface *(SDLCALL *SDL_SetVideoMode)(int, int, int, Uint32);
int (SDLCALL *SDL_EnableKeyRepeat)(int, int);
void (SDLCALL *SDL_EnableUNICODE)(int);
void (SDLCALL *SDL_VideoDriverName)(char *, int);
SDL_Rect **(SDLCALL *SDL_ListModes)(void *, int);
Uint8 *(SDLCALL *SDL_GetKeyState)(int *);
} SDLProcs;
#define M(x) x "\0"
static const char sdl_files[] =
M("sdl.dll")
M("SDL_Init")
M("SDL_InitSubSystem")
M("SDL_GetError")
M("SDL_QuitSubSystem")
M("SDL_UpdateRect")
M("SDL_UpdateRects")
M("SDL_SetColors")
M("SDL_WM_SetCaption")
M("SDL_ShowCursor")
M("SDL_FreeSurface")
M("SDL_PollEvent")
M("SDL_WarpMouse")
M("SDL_GetTicks")
M("SDL_OpenAudio")
M("SDL_PauseAudio")
M("SDL_CloseAudio")
M("SDL_LockSurface")
M("SDL_UnlockSurface")
M("SDL_GetModState")
M("SDL_Delay")
M("SDL_Quit")
M("SDL_SetVideoMode")
M("SDL_EnableKeyRepeat")
M("SDL_EnableUNICODE")
M("SDL_VideoDriverName")
M("SDL_ListModes")
M("SDL_GetKeyState")
M("")
;
#undef M
static SDLProcs _proc;
static char *LoadSdlDLL(void)
{
if (_proc.SDL_Init != NULL)
return NULL;
if (!LoadLibraryList((void**)&_proc, sdl_files))
return "Unable to load sdl.dll";
}
#undef SDL_CALL
#define SDL_CALL _proc.
static void SdlAbort(int sig)
/* Own hand-made parachute for the cases of failed assertions. */
SDL_CALL SDL_Quit();
static char *SdlOpen(uint32 x)
char *s = LoadSdlDLL();
if (s != NULL) return s;
if (_sdl_usage++ == 0) {
if (SDL_CALL SDL_Init(x) == -1)
return SDL_CALL SDL_GetError();
} else if (x != 0) {
if (SDL_CALL SDL_InitSubSystem(x) == -1)
signal(SIGABRT, SdlAbort);
static void SdlClose(uint32 x)
if (x != 0)
SDL_CALL SDL_QuitSubSystem(x);
if (--_sdl_usage == 0) {
#ifndef __MORPHOS__
signal(SIGABRT, SIG_DFL);
#else
signal(SIGABRT, (void (*)(int))0);
static void SdlVideoMakeDirty(int left, int top, int width, int height)
// printf("(%d,%d)-(%d,%d)\n", left, top, width, height);
// _pixels_redrawn += width*height;
if (_num_dirty_rects < MAX_DIRTY_RECTS) {
_dirty_rects[_num_dirty_rects].x = left;
_dirty_rects[_num_dirty_rects].y = top;
_dirty_rects[_num_dirty_rects].w = width;
_dirty_rects[_num_dirty_rects].h = height;
_num_dirty_rects++;
static SDL_Color pal[256];
static void UpdatePalette(uint start, uint end)
uint i;
byte *b;
for (i = start, b = _cur_palette + start * 3; i != end; i++, b += 3) {
pal[i].r = b[0];
pal[i].g = b[1];
pal[i].b = b[2];
pal[i].unused = b[3];
SDL_CALL SDL_SetColors(_sdl_screen, pal, start, end);
static void InitPalette(void)
UpdatePalette(0, 256);
static void CheckPaletteAnim(void)
if(_pal_last_dirty != -1) {
UpdatePalette(_pal_first_dirty, _pal_last_dirty + 1);
_pal_last_dirty = -1;
static void DrawSurfaceToScreen(void)
int n = _num_dirty_rects;
if (n != 0) {
_num_dirty_rects = 0;
if (n > MAX_DIRTY_RECTS)
SDL_CALL SDL_UpdateRect(_sdl_screen, 0, 0, 0, 0);
else
SDL_CALL SDL_UpdateRects(_sdl_screen, n, _dirty_rects);
static int CDECL compare_res(const void *pa, const void *pb)
int x = ((const uint16*)pa)[0] - ((const uint16*)pb)[0];
if (x != 0) return x;
return ((const uint16*)pa)[1] - ((const uint16*)pb)[1];
static const uint16 default_resolutions[][2] = {
{ 640, 480},
{ 800, 600},
{1024, 768},
{1152, 864},
{1280, 960},
{1280, 1024},
{1400, 1050},
{1600, 1200}
};
static void GetVideoModes(void)
int i;
SDL_Rect **modes;
modes = SDL_CALL SDL_ListModes(NULL, SDL_SWSURFACE + (_fullscreen ? SDL_FULLSCREEN : 0));
if (modes == NULL)
error("sdl: no modes available");
_all_modes = (modes == (void*)-1);
if (_all_modes) {
// all modes available, put some default ones here
memcpy(_resolutions, default_resolutions, sizeof(default_resolutions));
_num_resolutions = lengthof(default_resolutions);
} else {
int n = 0;
for (i = 0; modes[i]; i++) {
Status change: