Add non-animated SSSE3 blitter
[openttd/fttd.git] / src / video / dedicated_v.cpp
blob8e5b9ea91c7e42dfa017592190a2707bbb52cbff
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file dedicated_v.cpp Dedicated server video 'driver'. */
12 #include "../stdafx.h"
14 #ifdef ENABLE_NETWORK
16 #include "../gfx_func.h"
17 #include "../network/network.h"
18 #include "../network/network_internal.h"
19 #include "../console_func.h"
20 #include "../genworld.h"
21 #include "../fileio_type.h"
22 #include "../fios.h"
23 #include "../blitter/factory.hpp"
24 #include "../company_func.h"
25 #include "../core/random_func.hpp"
26 #include "../saveload/saveload.h"
27 #include "dedicated_v.h"
29 #ifdef BEOS_NET_SERVER
30 #include <net/socket.h>
31 #endif
33 #ifdef __OS2__
34 # include <sys/time.h> /* gettimeofday */
35 # include <sys/types.h>
36 # include <unistd.h>
37 # include <conio.h>
39 # define INCL_DOS
40 # include <os2.h>
42 # define STDIN 0 /* file descriptor for standard input */
44 /**
45 * Switches OpenTTD to a console app at run-time, instead of a PM app
46 * Necessary to see stdout, etc.
48 static void OS2_SwitchToConsoleMode()
50 PPIB pib;
51 PTIB tib;
53 DosGetInfoBlocks(&tib, &pib);
55 /* Change flag from PM to VIO */
56 pib->pib_ultype = 3;
58 #endif
60 #if defined(UNIX) || defined(PSP)
61 # include <sys/time.h> /* gettimeofday */
62 # include <sys/types.h>
63 # include <unistd.h>
64 # include <signal.h>
65 # define STDIN 0 /* file descriptor for standard input */
66 # if defined(PSP)
67 # include <sys/fd_set.h>
68 # include <sys/select.h>
69 # endif /* PSP */
71 /* Signal handlers */
72 static void DedicatedSignalHandler(int sig)
74 if (_game_mode == GM_NORMAL && _settings_client.gui.autosave_on_exit) DoExitSave();
75 _exit_game = true;
76 signal(sig, DedicatedSignalHandler);
78 #endif
80 #if defined(WIN32)
81 # include <windows.h> /* GetTickCount */
82 # if !defined(WINCE)
83 # include <conio.h>
84 # endif
85 # include <time.h>
86 # include <tchar.h>
87 static HANDLE _hInputReady, _hWaitForInputHandling;
88 static HANDLE _hThread; // Thread to close
89 static char _win_console_thread_buffer[200];
91 /* Windows Console thread. Just loop and signal when input has been received */
92 static void WINAPI CheckForConsoleInput()
94 #if defined(WINCE)
95 /* WinCE doesn't support console stuff */
96 return;
97 #else
98 DWORD nb;
99 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
100 for (;;) {
101 ReadFile(hStdin, _win_console_thread_buffer, lengthof(_win_console_thread_buffer), &nb, NULL);
102 /* Signal input waiting that input is read and wait for it being handled
103 * SignalObjectAndWait() should be used here, but it's unsupported in Win98< */
104 SetEvent(_hInputReady);
105 WaitForSingleObject(_hWaitForInputHandling, INFINITE);
107 #endif
110 static void CreateWindowsConsoleThread()
112 DWORD dwThreadId;
113 /* Create event to signal when console input is ready */
114 _hInputReady = CreateEvent(NULL, false, false, NULL);
115 _hWaitForInputHandling = CreateEvent(NULL, false, false, NULL);
116 if (_hInputReady == NULL || _hWaitForInputHandling == NULL) usererror("Cannot create console event!");
118 _hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput, NULL, 0, &dwThreadId);
119 if (_hThread == NULL) usererror("Cannot create console thread!");
121 DEBUG(driver, 2, "Windows console thread started");
124 static void CloseWindowsConsoleThread()
126 CloseHandle(_hThread);
127 CloseHandle(_hInputReady);
128 CloseHandle(_hWaitForInputHandling);
129 DEBUG(driver, 2, "Windows console thread shut down");
132 #endif
135 static void *_dedicated_video_mem;
137 /* Whether a fork has been done. */
138 bool _dedicated_forks;
140 extern bool SafeLoad(const char *filename, int mode, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL);
142 static FVideoDriver_Dedicated iFVideoDriver_Dedicated;
145 const char *VideoDriver_Dedicated::Start(const char * const *parm)
147 int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
148 _dedicated_video_mem = (bpp == 0) ? NULL : MallocT<byte>(_cur_resolution.width * _cur_resolution.height * (bpp / 8));
150 _screen.width = _screen.pitch = _cur_resolution.width;
151 _screen.height = _cur_resolution.height;
152 _screen.dst_ptr = _dedicated_video_mem;
153 ScreenSizeChanged();
154 BlitterFactory::GetCurrentBlitter()->PostResize();
156 #if defined(WINCE)
157 /* WinCE doesn't support console stuff */
158 #elif defined(WIN32)
159 /* For win32 we need to allocate a console (debug mode does the same) */
160 CreateConsole();
161 CreateWindowsConsoleThread();
162 SetConsoleTitle(_T("OpenTTD Dedicated Server"));
163 #endif
165 #ifdef _MSC_VER
166 /* Disable the MSVC assertion message box. */
167 _set_error_mode(_OUT_TO_STDERR);
168 #endif
170 #ifdef __OS2__
171 /* For OS/2 we also need to switch to console mode instead of PM mode */
172 OS2_SwitchToConsoleMode();
173 #endif
175 DEBUG(driver, 1, "Loading dedicated server");
176 return NULL;
179 void VideoDriver_Dedicated::Stop()
181 #ifdef WIN32
182 CloseWindowsConsoleThread();
183 #endif
184 free(_dedicated_video_mem);
187 void VideoDriver_Dedicated::MakeDirty(int left, int top, int width, int height) {}
188 bool VideoDriver_Dedicated::ChangeResolution(int w, int h) { return false; }
189 bool VideoDriver_Dedicated::ToggleFullscreen(bool fs) { return false; }
191 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
192 static bool InputWaiting()
194 struct timeval tv;
195 fd_set readfds;
197 tv.tv_sec = 0;
198 tv.tv_usec = 1;
200 FD_ZERO(&readfds);
201 FD_SET(STDIN, &readfds);
203 /* don't care about writefds and exceptfds: */
204 return select(STDIN + 1, &readfds, NULL, NULL, &tv) > 0;
207 static uint32 GetTime()
209 struct timeval tim;
211 gettimeofday(&tim, NULL);
212 return tim.tv_usec / 1000 + tim.tv_sec * 1000;
215 #else
217 static bool InputWaiting()
219 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
222 static uint32 GetTime()
224 return GetTickCount();
227 #endif
229 static void DedicatedHandleKeyInput()
231 static char input_line[1024] = "";
233 if (!InputWaiting()) return;
235 if (_exit_game) return;
237 #if defined(UNIX) || defined(__OS2__) || defined(PSP)
238 if (fgets(input_line, lengthof(input_line), stdin) == NULL) return;
239 #else
240 /* Handle console input, and signal console thread, it can accept input again */
241 assert_compile(lengthof(_win_console_thread_buffer) <= lengthof(input_line));
242 strecpy(input_line, _win_console_thread_buffer, lastof(input_line));
243 SetEvent(_hWaitForInputHandling);
244 #endif
246 /* strtok() does not 'forget' \r\n if the string starts with it,
247 * so we have to manually remove that! */
248 strtok(input_line, "\r\n");
249 for (char *c = input_line; *c != '\0'; c++) {
250 if (*c == '\n' || *c == '\r' || c == lastof(input_line)) {
251 *c = '\0';
252 break;
255 str_validate(input_line, lastof(input_line));
257 IConsoleCmdExec(input_line); // execute command
260 void VideoDriver_Dedicated::MainLoop()
262 uint32 cur_ticks = GetTime();
263 uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
265 /* Signal handlers */
266 #if defined(UNIX) || defined(PSP)
267 signal(SIGTERM, DedicatedSignalHandler);
268 signal(SIGINT, DedicatedSignalHandler);
269 signal(SIGQUIT, DedicatedSignalHandler);
270 #endif
272 /* Load the dedicated server stuff */
273 _is_network_server = true;
274 _network_dedicated = true;
275 _current_company = _local_company = COMPANY_SPECTATOR;
277 /* If SwitchMode is SM_LOAD_GAME, it means that the user used the '-g' options */
278 if (_switch_mode != SM_LOAD_GAME) {
279 StartNewGameWithoutGUI(GENERATE_NEW_SEED);
280 SwitchToMode(_switch_mode);
281 _switch_mode = SM_NONE;
282 } else {
283 _switch_mode = SM_NONE;
284 /* First we need to test if the savegame can be loaded, else we will end up playing the
285 * intro game... */
286 if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, BASE_DIR)) {
287 /* Loading failed, pop out.. */
288 DEBUG(net, 0, "Loading requested map failed, aborting");
289 _networking = false;
290 } else {
291 /* We can load this game, so go ahead */
292 SwitchToMode(SM_LOAD_GAME);
296 /* Done loading, start game! */
298 if (!_networking) {
299 DEBUG(net, 0, "Dedicated server could not be started, aborting");
300 return;
303 while (!_exit_game) {
304 uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
305 InteractiveRandom(); // randomness
307 if (!_dedicated_forks) DedicatedHandleKeyInput();
309 cur_ticks = GetTime();
310 _realtime_tick += cur_ticks - prev_cur_ticks;
311 if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks || _ddc_fastforward) {
312 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
314 GameLoop();
315 UpdateWindows();
318 /* Don't sleep when fast forwarding (for desync debugging) */
319 if (!_ddc_fastforward) CSleep(1);
323 #endif /* ENABLE_NETWORK */