2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file sdl_v.cpp Implementation of the SDL video driver. */
|
2005-07-25 07:16:10 +00:00
|
|
|
|
|
|
|
#ifdef WITH_SDL
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
#include "../stdafx.h"
|
2005-07-25 07:16:10 +00:00
|
|
|
#include "../openttd.h"
|
2007-12-23 10:56:02 +00:00
|
|
|
#include "../gfx_func.h"
|
2005-07-25 07:16:10 +00:00
|
|
|
#include "../sdl.h"
|
|
|
|
#include "../variables.h"
|
2008-05-04 21:53:36 +00:00
|
|
|
#include "../rev.h"
|
2007-06-17 20:30:28 +00:00
|
|
|
#include "../blitter/factory.hpp"
|
2007-12-22 23:30:28 +00:00
|
|
|
#include "../network/network.h"
|
2008-05-06 22:17:12 +00:00
|
|
|
#include "../functions.h"
|
2009-10-15 17:41:06 +00:00
|
|
|
#include "../thread/thread.h"
|
2005-07-25 07:16:10 +00:00
|
|
|
#include "sdl_v.h"
|
2005-07-23 15:16:57 +00:00
|
|
|
#include <SDL.h>
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
static FVideoDriver_SDL iFVideoDriver_SDL;
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
static SDL_Surface *_sdl_screen;
|
|
|
|
static bool _all_modes;
|
|
|
|
|
2009-10-15 17:41:06 +00:00
|
|
|
/** Whether the drawing is/may be done in a separate thread. */
|
|
|
|
static bool _draw_threaded;
|
|
|
|
/** Thread used to 'draw' to the screen, i.e. push data to the screen. */
|
|
|
|
static ThreadObject *_draw_thread = NULL;
|
|
|
|
/** Mutex to keep the access to the shared memory controlled. */
|
|
|
|
static ThreadMutex *_draw_mutex = NULL;
|
|
|
|
/** Should we keep continue drawing? */
|
|
|
|
static volatile bool _draw_continue;
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
#define MAX_DIRTY_RECTS 100
|
|
|
|
static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS];
|
|
|
|
static int _num_dirty_rects;
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
void VideoDriver_SDL::MakeDirty(int left, int top, int width, int height)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
2005-07-29 10:16:49 +00:00
|
|
|
static void UpdatePalette(uint start, uint count)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2005-07-29 10:16:49 +00:00
|
|
|
SDL_Color pal[256];
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2009-01-23 16:05:58 +00:00
|
|
|
for (uint i = 0; i != count; i++) {
|
2005-07-29 10:16:49 +00:00
|
|
|
pal[i].r = _cur_palette[start + i].r;
|
|
|
|
pal[i].g = _cur_palette[start + i].g;
|
|
|
|
pal[i].b = _cur_palette[start + i].b;
|
2005-07-23 15:16:57 +00:00
|
|
|
pal[i].unused = 0;
|
|
|
|
}
|
|
|
|
|
2005-07-29 10:16:49 +00:00
|
|
|
SDL_CALL SDL_SetColors(_sdl_screen, pal, start, count);
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void InitPalette()
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
|
|
|
UpdatePalette(0, 256);
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void CheckPaletteAnim()
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2007-06-19 15:04:08 +00:00
|
|
|
if (_pal_count_dirty != 0) {
|
2007-06-19 15:18:26 +00:00
|
|
|
Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
|
|
|
|
|
2007-06-19 15:04:08 +00:00
|
|
|
switch (blitter->UsePaletteAnimation()) {
|
|
|
|
case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
|
|
|
|
UpdatePalette(_pal_first_dirty, _pal_count_dirty);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Blitter::PALETTE_ANIMATION_BLITTER:
|
|
|
|
blitter->PaletteAnimate(_pal_first_dirty, _pal_count_dirty);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Blitter::PALETTE_ANIMATION_NONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
_pal_count_dirty = 0;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void DrawSurfaceToScreen()
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
|
|
|
int n = _num_dirty_rects;
|
2009-01-23 16:05:58 +00:00
|
|
|
if (n == 0) return;
|
|
|
|
|
|
|
|
_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);
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-15 17:41:06 +00:00
|
|
|
static void DrawSurfaceToScreenThread(void *)
|
|
|
|
{
|
|
|
|
/* First wait till we 'may' start */
|
|
|
|
_draw_mutex->BeginCritical();
|
|
|
|
_draw_mutex->WaitForSignal();
|
|
|
|
|
|
|
|
while (_draw_continue) {
|
|
|
|
/* Then just draw and wait till we stop */
|
|
|
|
DrawSurfaceToScreen();
|
|
|
|
_draw_mutex->WaitForSignal();
|
|
|
|
}
|
|
|
|
|
|
|
|
_draw_mutex->EndCritical();
|
|
|
|
_draw_thread->Exit();
|
|
|
|
}
|
|
|
|
|
2009-01-23 15:58:34 +00:00
|
|
|
static const Dimension _default_resolutions[] = {
|
2005-07-23 15:16:57 +00:00
|
|
|
{ 640, 480},
|
|
|
|
{ 800, 600},
|
|
|
|
{1024, 768},
|
|
|
|
{1152, 864},
|
|
|
|
{1280, 800},
|
|
|
|
{1280, 960},
|
|
|
|
{1280, 1024},
|
|
|
|
{1400, 1050},
|
|
|
|
{1600, 1200},
|
|
|
|
{1680, 1050},
|
|
|
|
{1920, 1200}
|
|
|
|
};
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void GetVideoModes()
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2009-01-23 15:58:34 +00:00
|
|
|
SDL_Rect **modes = SDL_CALL SDL_ListModes(NULL, SDL_SWSURFACE | SDL_FULLSCREEN);
|
|
|
|
if (modes == NULL) usererror("sdl: no modes available");
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2009-01-23 15:58:34 +00:00
|
|
|
_all_modes = (SDL_CALL SDL_ListModes(NULL, SDL_SWSURFACE | (_fullscreen ? SDL_FULLSCREEN : 0)) == (void*)-1);
|
|
|
|
if (modes == (void*)-1) {
|
|
|
|
int n = 0;
|
|
|
|
for (uint i = 0; i < lengthof(_default_resolutions); i++) {
|
2009-01-23 17:32:01 +00:00
|
|
|
if (SDL_CALL SDL_VideoModeOK(_default_resolutions[i].width, _default_resolutions[i].height, 8, SDL_FULLSCREEN) != 0) {
|
2009-01-23 15:58:34 +00:00
|
|
|
_resolutions[n] = _default_resolutions[i];
|
|
|
|
if (++n == lengthof(_resolutions)) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_num_resolutions = n;
|
2005-07-23 15:16:57 +00:00
|
|
|
} else {
|
|
|
|
int n = 0;
|
2009-01-23 15:58:34 +00:00
|
|
|
for (int i = 0; modes[i]; i++) {
|
2009-06-27 20:53:45 +00:00
|
|
|
uint w = modes[i]->w;
|
|
|
|
uint h = modes[i]->h;
|
2009-01-23 15:58:34 +00:00
|
|
|
int j;
|
|
|
|
for (j = 0; j < n; j++) {
|
|
|
|
if (_resolutions[j].width == w && _resolutions[j].height == h) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j == n) {
|
|
|
|
_resolutions[j].width = w;
|
|
|
|
_resolutions[j].height = h;
|
|
|
|
if (++n == lengthof(_resolutions)) break;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_num_resolutions = n;
|
|
|
|
SortResolutions(_num_resolutions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-27 20:53:45 +00:00
|
|
|
static void GetAvailableVideoMode(uint *w, uint *h)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2009-01-23 16:05:58 +00:00
|
|
|
/* All modes available? */
|
2008-11-29 01:28:13 +00:00
|
|
|
if (_all_modes || _num_resolutions == 0) return;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2009-01-23 16:05:58 +00:00
|
|
|
/* Is the wanted mode among the available modes? */
|
|
|
|
for (int i = 0; i != _num_resolutions; i++) {
|
2008-06-16 19:38:41 +00:00
|
|
|
if (*w == _resolutions[i].width && *h == _resolutions[i].height) return;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
2009-01-23 16:05:58 +00:00
|
|
|
/* Use the closest possible resolution */
|
|
|
|
int best = 0;
|
2009-06-27 20:53:45 +00:00
|
|
|
uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
|
2009-01-23 16:05:58 +00:00
|
|
|
for (int i = 1; i != _num_resolutions; ++i) {
|
2009-06-27 20:53:45 +00:00
|
|
|
uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
|
2005-07-23 15:16:57 +00:00
|
|
|
if (newdelta < delta) {
|
|
|
|
best = i;
|
|
|
|
delta = newdelta;
|
|
|
|
}
|
|
|
|
}
|
2008-06-16 19:38:41 +00:00
|
|
|
*w = _resolutions[best].width;
|
|
|
|
*h = _resolutions[best].height;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
2006-08-13 08:18:11 +00:00
|
|
|
#ifndef ICON_DIR
|
|
|
|
#define ICON_DIR "media"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
/* Let's redefine the LoadBMP macro with because we are dynamically
|
|
|
|
* loading SDL and need to 'SDL_CALL' all functions */
|
|
|
|
#undef SDL_LoadBMP
|
|
|
|
#define SDL_LoadBMP(file) SDL_LoadBMP_RW(SDL_CALL SDL_RWFromFile(file, "rb"), 1)
|
|
|
|
#endif
|
|
|
|
|
2009-06-27 20:53:45 +00:00
|
|
|
static bool CreateMainSurface(uint w, uint h)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2006-08-13 08:18:11 +00:00
|
|
|
SDL_Surface *newscreen, *icon;
|
2005-07-23 15:16:57 +00:00
|
|
|
char caption[50];
|
2007-06-12 20:24:12 +00:00
|
|
|
int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
GetAvailableVideoMode(&w, &h);
|
|
|
|
|
2009-06-27 20:53:45 +00:00
|
|
|
DEBUG(driver, 1, "SDL: using mode %ux%ux%d", w, h, bpp);
|
2007-06-12 20:24:12 +00:00
|
|
|
|
2008-06-05 20:54:52 +00:00
|
|
|
if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2006-08-13 08:18:11 +00:00
|
|
|
/* Give the application an icon */
|
|
|
|
icon = SDL_CALL SDL_LoadBMP(ICON_DIR PATHSEP "openttd.32.bmp");
|
|
|
|
if (icon != NULL) {
|
|
|
|
/* Get the colourkey, which will be magenta */
|
|
|
|
uint32 rgbmap = SDL_CALL SDL_MapRGB(icon->format, 255, 0, 255);
|
2006-09-01 10:29:40 +00:00
|
|
|
|
2006-08-13 08:18:11 +00:00
|
|
|
SDL_CALL SDL_SetColorKey(icon, SDL_SRCCOLORKEY, rgbmap);
|
|
|
|
SDL_CALL SDL_WM_SetIcon(icon, NULL);
|
|
|
|
SDL_CALL SDL_FreeSurface(icon);
|
|
|
|
}
|
|
|
|
|
2009-01-23 16:05:58 +00:00
|
|
|
/* DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK */
|
2007-06-12 20:24:12 +00:00
|
|
|
newscreen = SDL_CALL SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE | SDL_HWPALETTE | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
|
2008-11-29 01:28:13 +00:00
|
|
|
if (newscreen == NULL) {
|
|
|
|
DEBUG(driver, 0, "SDL: Couldn't allocate a window to draw on");
|
2005-07-23 15:16:57 +00:00
|
|
|
return false;
|
2008-11-29 01:28:13 +00:00
|
|
|
}
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2009-10-15 17:41:06 +00:00
|
|
|
/* Delay drawing for this cycle; the next cycle will redraw the whole screen */
|
|
|
|
_num_dirty_rects = 0;
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
_screen.width = newscreen->w;
|
|
|
|
_screen.height = newscreen->h;
|
2007-06-12 20:24:12 +00:00
|
|
|
_screen.pitch = newscreen->pitch / (bpp / 8);
|
2005-07-23 15:16:57 +00:00
|
|
|
_sdl_screen = newscreen;
|
|
|
|
InitPalette();
|
|
|
|
|
|
|
|
snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
|
|
|
|
SDL_CALL SDL_WM_SetCaption(caption, caption);
|
|
|
|
SDL_CALL SDL_ShowCursor(0);
|
|
|
|
|
|
|
|
GameSizeChanged();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct VkMapping {
|
2005-07-23 15:16:57 +00:00
|
|
|
uint16 vk_from;
|
|
|
|
byte vk_count;
|
|
|
|
byte map_to;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
#define AS(x, z) {x, 0, z}
|
|
|
|
#define AM(x, y, z, w) {x, y - x, z}
|
|
|
|
|
|
|
|
static const VkMapping _vk_mapping[] = {
|
2007-07-23 16:48:19 +00:00
|
|
|
/* Pageup stuff + up/down */
|
2005-07-23 15:16:57 +00:00
|
|
|
AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
|
2006-08-28 18:53:03 +00:00
|
|
|
AS(SDLK_UP, WKC_UP),
|
|
|
|
AS(SDLK_DOWN, WKC_DOWN),
|
|
|
|
AS(SDLK_LEFT, WKC_LEFT),
|
|
|
|
AS(SDLK_RIGHT, WKC_RIGHT),
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2006-08-28 18:53:03 +00:00
|
|
|
AS(SDLK_HOME, WKC_HOME),
|
|
|
|
AS(SDLK_END, WKC_END),
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2006-08-28 18:53:03 +00:00
|
|
|
AS(SDLK_INSERT, WKC_INSERT),
|
|
|
|
AS(SDLK_DELETE, WKC_DELETE),
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2007-07-23 16:48:19 +00:00
|
|
|
/* Map letters & digits */
|
2005-07-23 15:16:57 +00:00
|
|
|
AM(SDLK_a, SDLK_z, 'A', 'Z'),
|
|
|
|
AM(SDLK_0, SDLK_9, '0', '9'),
|
|
|
|
|
2006-08-28 18:53:03 +00:00
|
|
|
AS(SDLK_ESCAPE, WKC_ESC),
|
|
|
|
AS(SDLK_PAUSE, WKC_PAUSE),
|
|
|
|
AS(SDLK_BACKSPACE, WKC_BACKSPACE),
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2006-08-28 18:53:03 +00:00
|
|
|
AS(SDLK_SPACE, WKC_SPACE),
|
|
|
|
AS(SDLK_RETURN, WKC_RETURN),
|
|
|
|
AS(SDLK_TAB, WKC_TAB),
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2007-07-23 16:48:19 +00:00
|
|
|
/* Function keys */
|
2005-07-23 15:16:57 +00:00
|
|
|
AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
|
|
|
|
|
2007-07-23 16:48:19 +00:00
|
|
|
/* Numeric part. */
|
2008-09-07 11:55:28 +00:00
|
|
|
AM(SDLK_KP0, SDLK_KP9, '0', '9'),
|
2006-08-28 18:53:03 +00:00
|
|
|
AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
|
|
|
|
AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
|
|
|
|
AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
|
|
|
|
AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
|
|
|
|
AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
|
2007-07-23 16:39:27 +00:00
|
|
|
AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
|
|
|
|
|
2007-07-23 16:48:19 +00:00
|
|
|
/* Other non-letter keys */
|
2007-07-23 16:39:27 +00:00
|
|
|
AS(SDLK_SLASH, WKC_SLASH),
|
|
|
|
AS(SDLK_SEMICOLON, WKC_SEMICOLON),
|
|
|
|
AS(SDLK_EQUALS, WKC_EQUALS),
|
|
|
|
AS(SDLK_LEFTBRACKET, WKC_L_BRACKET),
|
|
|
|
AS(SDLK_BACKSLASH, WKC_BACKSLASH),
|
|
|
|
AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
|
|
|
|
|
|
|
|
AS(SDLK_QUOTE, WKC_SINGLEQUOTE),
|
|
|
|
AS(SDLK_COMMA, WKC_COMMA),
|
|
|
|
AS(SDLK_MINUS, WKC_MINUS),
|
|
|
|
AS(SDLK_PERIOD, WKC_PERIOD)
|
2005-07-23 15:16:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static uint32 ConvertSdlKeyIntoMy(SDL_keysym *sym)
|
|
|
|
{
|
|
|
|
const VkMapping *map;
|
|
|
|
uint key = 0;
|
2006-09-01 10:29:40 +00:00
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
|
|
|
|
if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
|
|
|
|
key = sym->sym - map->vk_from + map->map_to;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-23 16:05:58 +00:00
|
|
|
/* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
|
2005-07-23 15:16:57 +00:00
|
|
|
#if defined(WIN32) || defined(__OS2__)
|
2006-11-16 16:50:54 +00:00
|
|
|
if (sym->scancode == 41) key = WKC_BACKQUOTE;
|
2005-07-23 15:16:57 +00:00
|
|
|
#elif defined(__APPLE__)
|
2006-11-16 16:50:54 +00:00
|
|
|
if (sym->scancode == 10) key = WKC_BACKQUOTE;
|
2005-07-23 15:16:57 +00:00
|
|
|
#elif defined(__MORPHOS__)
|
2006-11-16 16:50:54 +00:00
|
|
|
if (sym->scancode == 0) key = WKC_BACKQUOTE; // yes, that key is code '0' under MorphOS :)
|
2005-07-23 15:16:57 +00:00
|
|
|
#elif defined(__BEOS__)
|
2006-11-16 16:50:54 +00:00
|
|
|
if (sym->scancode == 17) key = WKC_BACKQUOTE;
|
2005-07-23 15:16:57 +00:00
|
|
|
#elif defined(__SVR4) && defined(__sun)
|
2006-11-16 16:50:54 +00:00
|
|
|
if (sym->scancode == 60) key = WKC_BACKQUOTE;
|
|
|
|
if (sym->scancode == 49) key = WKC_BACKSPACE;
|
2005-07-23 15:16:57 +00:00
|
|
|
#elif defined(__sgi__)
|
2006-11-16 16:50:54 +00:00
|
|
|
if (sym->scancode == 22) key = WKC_BACKQUOTE;
|
2005-07-23 15:16:57 +00:00
|
|
|
#else
|
2006-11-16 16:50:54 +00:00
|
|
|
if (sym->scancode == 49) key = WKC_BACKQUOTE;
|
2005-07-23 15:16:57 +00:00
|
|
|
#endif
|
|
|
|
|
2009-01-23 16:05:58 +00:00
|
|
|
/* META are the command keys on mac */
|
2006-09-01 10:29:40 +00:00
|
|
|
if (sym->mod & KMOD_META) key |= WKC_META;
|
2005-07-23 15:16:57 +00:00
|
|
|
if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
|
2006-09-01 10:29:40 +00:00
|
|
|
if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
|
|
|
|
if (sym->mod & KMOD_ALT) key |= WKC_ALT;
|
2009-01-23 16:05:58 +00:00
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
return (key << 16) + sym->unicode;
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static int PollEvent()
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
|
|
|
SDL_Event ev;
|
|
|
|
|
2006-09-01 10:29:40 +00:00
|
|
|
if (!SDL_CALL SDL_PollEvent(&ev)) return -2;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
switch (ev.type) {
|
2006-09-01 10:29:40 +00:00
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
if (_cursor.fix_at) {
|
|
|
|
int dx = ev.motion.x - _cursor.pos.x;
|
|
|
|
int dy = ev.motion.y - _cursor.pos.y;
|
|
|
|
if (dx != 0 || dy != 0) {
|
|
|
|
_cursor.delta.x += dx;
|
|
|
|
_cursor.delta.y += dy;
|
|
|
|
SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_cursor.delta.x = ev.motion.x - _cursor.pos.x;
|
|
|
|
_cursor.delta.y = ev.motion.y - _cursor.pos.y;
|
|
|
|
_cursor.pos.x = ev.motion.x;
|
|
|
|
_cursor.pos.y = ev.motion.y;
|
|
|
|
_cursor.dirty = true;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
2006-11-15 21:01:19 +00:00
|
|
|
HandleMouseEvents();
|
2006-09-01 10:29:40 +00:00
|
|
|
break;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2006-09-01 10:29:40 +00:00
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
2006-09-01 10:37:27 +00:00
|
|
|
if (_rightclick_emulate && SDL_CALL SDL_GetModState() & KMOD_CTRL) {
|
2006-09-01 10:29:40 +00:00
|
|
|
ev.button.button = SDL_BUTTON_RIGHT;
|
|
|
|
}
|
2006-03-24 17:00:55 +00:00
|
|
|
|
2006-09-01 10:29:40 +00:00
|
|
|
switch (ev.button.button) {
|
|
|
|
case SDL_BUTTON_LEFT:
|
|
|
|
_left_button_down = true;
|
|
|
|
break;
|
2006-03-24 00:42:35 +00:00
|
|
|
|
2006-09-01 10:29:40 +00:00
|
|
|
case SDL_BUTTON_RIGHT:
|
|
|
|
_right_button_down = true;
|
|
|
|
_right_button_clicked = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_BUTTON_WHEELUP: _cursor.wheel--; break;
|
|
|
|
case SDL_BUTTON_WHEELDOWN: _cursor.wheel++; break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
2006-11-15 21:01:19 +00:00
|
|
|
HandleMouseEvents();
|
2006-09-01 10:29:40 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
|
|
|
if (_rightclick_emulate) {
|
|
|
|
_right_button_down = false;
|
|
|
|
_left_button_down = false;
|
|
|
|
_left_button_clicked = false;
|
|
|
|
} else if (ev.button.button == SDL_BUTTON_LEFT) {
|
|
|
|
_left_button_down = false;
|
|
|
|
_left_button_clicked = false;
|
|
|
|
} else if (ev.button.button == SDL_BUTTON_RIGHT) {
|
|
|
|
_right_button_down = false;
|
|
|
|
}
|
2006-11-15 21:01:19 +00:00
|
|
|
HandleMouseEvents();
|
2006-09-01 10:29:40 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_ACTIVEEVENT:
|
|
|
|
if (!(ev.active.state & SDL_APPMOUSEFOCUS)) break;
|
|
|
|
|
|
|
|
if (ev.active.gain) { // mouse entered the window, enable cursor
|
|
|
|
_cursor.in_window = true;
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
2006-09-01 10:29:40 +00:00
|
|
|
UndrawMouseCursor(); // mouse left the window, undraw cursor
|
|
|
|
_cursor.in_window = false;
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2006-09-01 10:29:40 +00:00
|
|
|
break;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2009-01-23 16:05:58 +00:00
|
|
|
case SDL_QUIT:
|
|
|
|
HandleExitGameRequest();
|
|
|
|
break;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2009-01-23 16:05:58 +00:00
|
|
|
case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
|
2006-09-01 10:29:40 +00:00
|
|
|
if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) &&
|
|
|
|
(ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
|
|
|
|
ToggleFullScreen(!_fullscreen);
|
|
|
|
} else {
|
2006-11-15 19:35:52 +00:00
|
|
|
HandleKeypress(ConvertSdlKeyIntoMy(&ev.key.keysym));
|
2006-09-01 10:29:40 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_VIDEORESIZE: {
|
2008-04-18 21:49:38 +00:00
|
|
|
int w = max(ev.resize.w, 64);
|
|
|
|
int h = max(ev.resize.h, 64);
|
2006-09-01 10:29:40 +00:00
|
|
|
ChangeResInGame(w, h);
|
|
|
|
break;
|
|
|
|
}
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
const char *VideoDriver_SDL::Start(const char * const *parm)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
|
|
|
char buf[30];
|
|
|
|
|
|
|
|
const char *s = SdlOpen(SDL_INIT_VIDEO);
|
|
|
|
if (s != NULL) return s;
|
|
|
|
|
2009-06-30 12:36:24 +00:00
|
|
|
GetVideoModes();
|
|
|
|
if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
|
|
|
|
return SDL_CALL SDL_GetError();
|
|
|
|
}
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
SDL_CALL SDL_VideoDriverName(buf, 30);
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(driver, 1, "SDL: using driver '%s'", buf);
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
|
|
|
|
SDL_CALL SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
|
|
|
|
SDL_CALL SDL_EnableUNICODE(1);
|
2009-10-15 17:41:06 +00:00
|
|
|
|
|
|
|
_draw_threaded = GetDriverParam(parm, "no_threads") == NULL && GetDriverParam(parm, "no_thread") == NULL;
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
void VideoDriver_SDL::Stop()
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
|
|
|
SdlClose(SDL_INIT_VIDEO);
|
|
|
|
}
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
void VideoDriver_SDL::MainLoop()
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2007-01-10 15:00:20 +00:00
|
|
|
uint32 cur_ticks = SDL_CALL SDL_GetTicks();
|
2007-06-22 20:04:21 +00:00
|
|
|
uint32 last_cur_ticks = cur_ticks;
|
2007-01-10 15:00:20 +00:00
|
|
|
uint32 next_tick = cur_ticks + 30;
|
2005-07-23 15:16:57 +00:00
|
|
|
uint32 pal_tick = 0;
|
|
|
|
uint32 mod;
|
|
|
|
int numkeys;
|
|
|
|
Uint8 *keys;
|
|
|
|
|
2009-10-15 17:41:06 +00:00
|
|
|
if (_draw_threaded) {
|
|
|
|
/* Initialise the mutex first, because that's the thing we *need*
|
|
|
|
* directly in the newly created thread. */
|
|
|
|
_draw_mutex = ThreadMutex::New();
|
|
|
|
if (_draw_mutex == NULL) {
|
|
|
|
_draw_threaded = false;
|
|
|
|
} else {
|
|
|
|
_draw_mutex->BeginCritical();
|
|
|
|
_draw_continue = true;
|
|
|
|
|
|
|
|
_draw_threaded = ThreadObject::New(&DrawSurfaceToScreenThread, NULL, &_draw_thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the mutex if we won't be able to use it. */
|
|
|
|
if (!_draw_threaded) delete _draw_mutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(driver, 1, "SDL: using %sthreads", _draw_threaded ? "" : "no ");
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
for (;;) {
|
2007-01-10 15:00:20 +00:00
|
|
|
uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
|
2005-07-23 15:16:57 +00:00
|
|
|
InteractiveRandom(); // randomness
|
|
|
|
|
2006-11-15 19:49:16 +00:00
|
|
|
while (PollEvent() == -1) {}
|
2005-07-29 16:40:29 +00:00
|
|
|
if (_exit_game) return;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
mod = SDL_CALL SDL_GetModState();
|
|
|
|
keys = SDL_CALL SDL_GetKeyState(&numkeys);
|
|
|
|
#if defined(_DEBUG)
|
|
|
|
if (_shift_pressed)
|
|
|
|
#else
|
2007-01-01 19:37:53 +00:00
|
|
|
/* Speedup when pressing tab, except when using ALT+TAB
|
|
|
|
* to switch to another application */
|
|
|
|
if (keys[SDLK_TAB] && (mod & KMOD_ALT) == 0)
|
2005-07-23 15:16:57 +00:00
|
|
|
#endif
|
|
|
|
{
|
2006-09-03 22:07:28 +00:00
|
|
|
if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
|
2005-07-23 15:16:57 +00:00
|
|
|
} else if (_fast_forward & 2) {
|
|
|
|
_fast_forward = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_ticks = SDL_CALL SDL_GetTicks();
|
2009-05-06 15:06:57 +00:00
|
|
|
if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
|
2007-06-22 20:07:39 +00:00
|
|
|
_realtime_tick += cur_ticks - last_cur_ticks;
|
|
|
|
last_cur_ticks = cur_ticks;
|
2007-01-10 15:00:20 +00:00
|
|
|
next_tick = cur_ticks + 30;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2008-02-17 17:00:43 +00:00
|
|
|
bool old_ctrl_pressed = _ctrl_pressed;
|
|
|
|
|
2006-09-01 10:37:27 +00:00
|
|
|
_ctrl_pressed = !!(mod & KMOD_CTRL);
|
|
|
|
_shift_pressed = !!(mod & KMOD_SHIFT);
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2009-01-23 16:05:58 +00:00
|
|
|
/* determine which directional keys are down */
|
2005-07-23 15:16:57 +00:00
|
|
|
_dirkeys =
|
|
|
|
(keys[SDLK_LEFT] ? 1 : 0) |
|
|
|
|
(keys[SDLK_UP] ? 2 : 0) |
|
|
|
|
(keys[SDLK_RIGHT] ? 4 : 0) |
|
|
|
|
(keys[SDLK_DOWN] ? 8 : 0);
|
2008-02-17 17:00:43 +00:00
|
|
|
|
|
|
|
if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
|
|
|
|
|
2009-10-15 17:41:06 +00:00
|
|
|
/* The gameloop is the part that can run asynchroniously. The rest
|
|
|
|
* except sleeping can't. */
|
|
|
|
if (_draw_threaded) _draw_mutex->EndCritical();
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
GameLoop();
|
|
|
|
|
2009-10-15 17:41:06 +00:00
|
|
|
if (_draw_threaded) _draw_mutex->BeginCritical();
|
|
|
|
|
2007-06-12 20:24:12 +00:00
|
|
|
_screen.dst_ptr = _sdl_screen->pixels;
|
2005-07-23 15:16:57 +00:00
|
|
|
UpdateWindows();
|
|
|
|
if (++pal_tick > 4) {
|
|
|
|
CheckPaletteAnim();
|
|
|
|
pal_tick = 1;
|
|
|
|
}
|
2009-10-15 17:41:06 +00:00
|
|
|
|
|
|
|
/* End of the critical part. */
|
|
|
|
if (_draw_threaded) {
|
|
|
|
_draw_mutex->SendSignal();
|
|
|
|
} else {
|
|
|
|
/* Oh, we didn't have threads, then just draw unthreaded */
|
|
|
|
DrawSurfaceToScreen();
|
|
|
|
}
|
2005-07-23 15:16:57 +00:00
|
|
|
} else {
|
2009-10-15 17:41:06 +00:00
|
|
|
/* Release the thread while sleeping */
|
|
|
|
if (_draw_threaded) _draw_mutex->EndCritical();
|
|
|
|
CSleep(1);
|
|
|
|
if (_draw_threaded) _draw_mutex->BeginCritical();
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-15 17:41:06 +00:00
|
|
|
|
|
|
|
if (_draw_threaded) {
|
|
|
|
_draw_continue = false;
|
|
|
|
_draw_mutex->SendSignal();
|
|
|
|
_draw_mutex->EndCritical();
|
|
|
|
_draw_thread->Join();
|
|
|
|
|
|
|
|
delete _draw_mutex;
|
|
|
|
delete _draw_thread;
|
|
|
|
}
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
bool VideoDriver_SDL::ChangeResolution(int w, int h)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
|
|
|
return CreateMainSurface(w, h);
|
|
|
|
}
|
|
|
|
|
2008-01-01 14:20:48 +00:00
|
|
|
bool VideoDriver_SDL::ToggleFullscreen(bool fullscreen)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2007-07-05 12:23:54 +00:00
|
|
|
_fullscreen = fullscreen;
|
2005-07-23 15:16:57 +00:00
|
|
|
GetVideoModes(); // get the list of available video modes
|
2008-06-16 19:38:41 +00:00
|
|
|
if (_num_resolutions == 0 || !this->ChangeResolution(_cur_resolution.width, _cur_resolution.height)) {
|
2009-01-23 16:05:58 +00:00
|
|
|
/* switching resolution failed, put back full_screen to original status */
|
2006-09-01 10:29:40 +00:00
|
|
|
_fullscreen ^= true;
|
2008-01-01 14:20:48 +00:00
|
|
|
return false;
|
2006-09-01 10:29:40 +00:00
|
|
|
}
|
2008-01-01 14:20:48 +00:00
|
|
|
return true;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
2007-02-12 21:55:10 +00:00
|
|
|
#endif /* WITH_SDL */
|