src/video/dedicated_v.cpp
author rubidium
Tue, 30 Sep 2008 20:39:50 +0000
changeset 10207 c291a21b304e
parent 10201 4c181fbe8f2a
child 10208 72c00af5c95d
permissions -rw-r--r--
(svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2181
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2181
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 9001
diff changeset
     3
/** @file dedicated_v.cpp Dedicated server video 'driver'. */
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 9001
diff changeset
     4
2189
5cdc11ffeaa4 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
     5
#include "../stdafx.h"
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     6
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     7
#ifdef ENABLE_NETWORK
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     8
2195
ed135f2dc5f8 (svn r2710) Simplify dedicated server code a bit and don't compile it at all, if network support ist disabled
tron
parents: 2189
diff changeset
     9
#include "../openttd.h"
2189
5cdc11ffeaa4 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
    10
#include "../debug.h"
8123
ce31d2843a95 (svn r11684) -Codechange: split gfx.h in a type and functional header.
rubidium
parents: 8106
diff changeset
    11
#include "../gfx_func.h"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5406
diff changeset
    12
#include "../network/network.h"
8276
245f1b131d64 (svn r11840) -Codechange: split network.h so not everything in there needs to be included when wanting to know whether we are a server.
rubidium
parents: 8254
diff changeset
    13
#include "../network/network_internal.h"
9336
6baad5b3033d (svn r13228) -Codechange: split console.h.
rubidium
parents: 9111
diff changeset
    14
#include "../console_func.h"
2189
5cdc11ffeaa4 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
    15
#include "../variables.h"
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 2791
diff changeset
    16
#include "../genworld.h"
10039
1f236afd6cd1 (svn r14199) -Codechange: split fileio.h into fileio_type.h and fileio_func.h so not everything that includes saveload.h needs to include everything else too.
rubidium
parents: 9905
diff changeset
    17
#include "../fileio_type.h"
8151
4cefeef74c28 (svn r11713) -Codechange: Move some declarations and definitions in a more logical disposition
belugas
parents: 8131
diff changeset
    18
#include "../fios.h"
6937
40c760fcf1f6 (svn r10190) -Codechange: merged renderer and blitter to one single class API: blitter
truelight
parents: 6929
diff changeset
    19
#include "../blitter/factory.hpp"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
    20
#include "../core/alloc_func.hpp"
8254
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8171
diff changeset
    21
#include "../player_func.h"
8433
33899f3d6b5c (svn r12003) -Codechange: don't declare InteractiveRandom[Range] in multiple places.
rubidium
parents: 8276
diff changeset
    22
#include "../core/random_func.hpp"
2195
ed135f2dc5f8 (svn r2710) Simplify dedicated server code a bit and don't compile it at all, if network support ist disabled
tron
parents: 2189
diff changeset
    23
#include "dedicated_v.h"
781
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    24
2497
0fe349de34c7 (svn r3023) -Fix [BeOS] fixed compilation on BeOS R5 (MYOB)
bjarni
parents: 2261
diff changeset
    25
#ifdef BEOS_NET_SERVER
0fe349de34c7 (svn r3023) -Fix [BeOS] fixed compilation on BeOS R5 (MYOB)
bjarni
parents: 2261
diff changeset
    26
#include <net/socket.h>
0fe349de34c7 (svn r3023) -Fix [BeOS] fixed compilation on BeOS R5 (MYOB)
bjarni
parents: 2261
diff changeset
    27
#endif
0fe349de34c7 (svn r3023) -Fix [BeOS] fixed compilation on BeOS R5 (MYOB)
bjarni
parents: 2261
diff changeset
    28
781
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    29
#ifdef __OS2__
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    30
#	include <sys/time.h> /* gettimeofday */
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    31
#	include <sys/types.h>
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    32
#	include <unistd.h>
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    33
#	include <conio.h>
2181
c0ddc9695a14 (svn r2695) - Fix: OS/2 project update, add os2.h to dedicated_v.c
orudge
parents: 2180
diff changeset
    34
c0ddc9695a14 (svn r2695) - Fix: OS/2 project update, add os2.h to dedicated_v.c
orudge
parents: 2180
diff changeset
    35
#	define INCL_DOS
c0ddc9695a14 (svn r2695) - Fix: OS/2 project update, add os2.h to dedicated_v.c
orudge
parents: 2180
diff changeset
    36
#	include <os2.h>
c0ddc9695a14 (svn r2695) - Fix: OS/2 project update, add os2.h to dedicated_v.c
orudge
parents: 2180
diff changeset
    37
781
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    38
#	define STDIN 0  /* file descriptor for standard input */
810
a1494b19bd2a (svn r1281) -Fix: the OS/2 is now finished. Fixes:
truelight
parents: 796
diff changeset
    39
2261
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2228
diff changeset
    40
/**
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
    41
 * Switches OpenTTD to a console app at run-time, instead of a PM app
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
    42
 * Necessary to see stdout, etc. */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6033
diff changeset
    43
static void OS2_SwitchToConsoleMode()
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
    44
{
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
    45
	PPIB pib;
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
    46
	PTIB tib;
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
    47
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
    48
	DosGetInfoBlocks(&tib, &pib);
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
    49
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
    50
	// Change flag from PM to VIO
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
    51
	pib->pib_ultype = 3;
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
    52
}
781
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    53
#endif
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    54
6033
61682c6606c8 (svn r8756) [PSP] -Fix: made the dedicated code compile with PSP. It is almost UNIX, just it needs more includes ;)
truelight
parents: 5587
diff changeset
    55
#if defined(UNIX) || defined(PSP)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    56
#	include <sys/time.h> /* gettimeofday */
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    57
#	include <sys/types.h>
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    58
#	include <unistd.h>
702
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
    59
#	include <signal.h>
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    60
#	define STDIN 0  /* file descriptor for standard input */
6033
61682c6606c8 (svn r8756) [PSP] -Fix: made the dedicated code compile with PSP. It is almost UNIX, just it needs more includes ;)
truelight
parents: 5587
diff changeset
    61
#	if defined(PSP)
61682c6606c8 (svn r8756) [PSP] -Fix: made the dedicated code compile with PSP. It is almost UNIX, just it needs more includes ;)
truelight
parents: 5587
diff changeset
    62
#		include <sys/fd_set.h>
61682c6606c8 (svn r8756) [PSP] -Fix: made the dedicated code compile with PSP. It is almost UNIX, just it needs more includes ;)
truelight
parents: 5587
diff changeset
    63
#		include <sys/select.h>
61682c6606c8 (svn r8756) [PSP] -Fix: made the dedicated code compile with PSP. It is almost UNIX, just it needs more includes ;)
truelight
parents: 5587
diff changeset
    64
#	endif /* PSP */
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    65
702
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
    66
/* Signal handlers */
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    67
static void DedicatedSignalHandler(int sig)
702
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
    68
{
736
75bcef85daeb (svn r1188) -Fix: [Network] Fixed that CTRL+<key> could sometimes hang a dedicated
truelight
parents: 721
diff changeset
    69
	_exit_game = true;
75bcef85daeb (svn r1188) -Fix: [Network] Fixed that CTRL+<key> could sometimes hang a dedicated
truelight
parents: 721
diff changeset
    70
	signal(sig, DedicatedSignalHandler);
702
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
    71
}
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
    72
#endif
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
    73
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
    74
#if defined(WIN32)
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
    75
# include <windows.h> /* GetTickCount */
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
    76
# if !defined(WINCE)
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
    77
#  include <conio.h>
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
    78
# endif
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
    79
# include <time.h>
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
    80
# include <tchar.h>
4599
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
    81
static HANDLE _hInputReady, _hWaitForInputHandling;
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
    82
static HANDLE _hThread; // Thread to close
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
    83
static char _win_console_thread_buffer[200];
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
    84
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
    85
/* Windows Console thread. Just loop and signal when input has been received */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6033
diff changeset
    86
static void WINAPI CheckForConsoleInput()
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
    87
{
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
    88
#if defined(WINCE)
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
    89
	/* WinCE doesn't support console stuff */
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
    90
	return;
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
    91
#else
6265
808ed372b237 (svn r9074) -Codechange: win32 dedicated console now doesn't need an extra 'enter' to fully quit
glx
parents: 6247
diff changeset
    92
	DWORD nb;
808ed372b237 (svn r9074) -Codechange: win32 dedicated console now doesn't need an extra 'enter' to fully quit
glx
parents: 6247
diff changeset
    93
	HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
    94
	while (true) {
6265
808ed372b237 (svn r9074) -Codechange: win32 dedicated console now doesn't need an extra 'enter' to fully quit
glx
parents: 6247
diff changeset
    95
		ReadFile(hStdin, _win_console_thread_buffer, lengthof(_win_console_thread_buffer), &nb, NULL);
4599
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
    96
		/* Signal input waiting that input is read and wait for it being handled
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
    97
		 * SignalObjectAndWait() should be used here, but it's unsupported in Win98< */
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
    98
		SetEvent(_hInputReady);
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
    99
		WaitForSingleObject(_hWaitForInputHandling, INFINITE);
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   100
	}
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
   101
#endif
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   102
}
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   103
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6033
diff changeset
   104
static void CreateWindowsConsoleThread()
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   105
{
1762
2441d8946237 (svn r2266) - Feature: it is now possible to start a dedicated server on Win98/95, for the few sadistic geeks that really want to do this (thanks for testing Hackykid). Also fix up another glitch in console output
Darkvater
parents: 1626
diff changeset
   106
	DWORD dwThreadId;
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   107
	/* Create event to signal when console input is ready */
4599
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
   108
	_hInputReady = CreateEvent(NULL, false, false, NULL);
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
   109
	_hWaitForInputHandling = CreateEvent(NULL, false, false, NULL);
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9336
diff changeset
   110
	if (_hInputReady == NULL || _hWaitForInputHandling == NULL) usererror("Cannot create console event!");
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   111
4599
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
   112
	_hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput, NULL, 0, &dwThreadId);
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9336
diff changeset
   113
	if (_hThread == NULL) usererror("Cannot create console thread!");
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   114
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5168
diff changeset
   115
	DEBUG(driver, 2, "Windows console thread started");
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   116
}
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   117
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6033
diff changeset
   118
static void CloseWindowsConsoleThread()
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   119
{
4599
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
   120
	CloseHandle(_hThread);
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
   121
	CloseHandle(_hInputReady);
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
   122
	CloseHandle(_hWaitForInputHandling);
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5168
diff changeset
   123
	DEBUG(driver, 2, "Windows console thread shut down");
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   124
}
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   125
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   126
#endif
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   127
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   128
6878
5cefd3ac59c7 (svn r10121) -Codechange: split renderer from rest of code; no longer any code directly accesses the video-buffer
truelight
parents: 6615
diff changeset
   129
static void *_dedicated_video_mem;
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   130
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6878
diff changeset
   131
extern bool SafeSaveOrLoad(const char *filename, int mode, int newgm, Subdirectory subdir);
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   132
extern void SwitchMode(int new_mode);
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   133
7170
923946ec324f (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6937
diff changeset
   134
static FVideoDriver_Dedicated iFVideoDriver_Dedicated;
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   135
7170
923946ec324f (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6937
diff changeset
   136
923946ec324f (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6937
diff changeset
   137
const char *VideoDriver_Dedicated::Start(const char * const *parm)
1301
bf64ba5b6774 (svn r1805) Teach the driver layer a few things about const correctness
tron
parents: 1299
diff changeset
   138
{
6878
5cefd3ac59c7 (svn r10121) -Codechange: split renderer from rest of code; no longer any code directly accesses the video-buffer
truelight
parents: 6615
diff changeset
   139
	int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
5cefd3ac59c7 (svn r10121) -Codechange: split renderer from rest of code; no longer any code directly accesses the video-buffer
truelight
parents: 6615
diff changeset
   140
	if (bpp == 0) _dedicated_video_mem = NULL;
9533
e8b86b70c5f6 (svn r13537) -Fix [FS#2090](r13523): QSortT won't work this way, use Dimension instead of uint16[2] for resolutions
smatz
parents: 9470
diff changeset
   141
	else          _dedicated_video_mem = MallocT<byte>(_cur_resolution.width * _cur_resolution.height * (bpp / 8));
6878
5cefd3ac59c7 (svn r10121) -Codechange: split renderer from rest of code; no longer any code directly accesses the video-buffer
truelight
parents: 6615
diff changeset
   142
9533
e8b86b70c5f6 (svn r13537) -Fix [FS#2090](r13523): QSortT won't work this way, use Dimension instead of uint16[2] for resolutions
smatz
parents: 9470
diff changeset
   143
	_screen.width  = _screen.pitch = _cur_resolution.width;
e8b86b70c5f6 (svn r13537) -Fix [FS#2090](r13523): QSortT won't work this way, use Dimension instead of uint16[2] for resolutions
smatz
parents: 9470
diff changeset
   144
	_screen.height = _cur_resolution.height;
9001
1827d939372d (svn r12796) -Fix: the dedicated blitter did segfault too, like the null blitter did.
rubidium
parents: 8433
diff changeset
   145
	ScreenSizeChanged();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   146
5406
2df85542fee4 (svn r7602) -Fix (r7565): MSVC2003 and lower don't support variadic macros, so work around
Darkvater
parents: 5380
diff changeset
   147
	SetDebugString("net=6");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   148
7408
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
   149
#if defined(WINCE)
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
   150
	/* WinCE doesn't support console stuff */
605b333582d5 (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight
parents: 7170
diff changeset
   151
#elif defined(WIN32)
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 4891
diff changeset
   152
	// For win32 we need to allocate a console (debug mode does the same)
626
35294912464a (svn r1056) -Fix: [Network] Give the dedicated-server always a console in windows (sign_de)
truelight
parents: 543
diff changeset
   153
	CreateConsole();
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   154
	CreateWindowsConsoleThread();
5168
e206899169c4 (svn r7278) -Codechange: [win32] Add UNICODE support so it should compile on OS's using UNICODE
Darkvater
parents: 4891
diff changeset
   155
	SetConsoleTitle(_T("OpenTTD Dedicated Server"));
626
35294912464a (svn r1056) -Fix: [Network] Give the dedicated-server always a console in windows (sign_de)
truelight
parents: 543
diff changeset
   156
#endif
35294912464a (svn r1056) -Fix: [Network] Give the dedicated-server always a console in windows (sign_de)
truelight
parents: 543
diff changeset
   157
810
a1494b19bd2a (svn r1281) -Fix: the OS/2 is now finished. Fixes:
truelight
parents: 796
diff changeset
   158
#ifdef __OS2__
a1494b19bd2a (svn r1281) -Fix: the OS/2 is now finished. Fixes:
truelight
parents: 796
diff changeset
   159
	// For OS/2 we also need to switch to console mode instead of PM mode
a1494b19bd2a (svn r1281) -Fix: the OS/2 is now finished. Fixes:
truelight
parents: 796
diff changeset
   160
	OS2_SwitchToConsoleMode();
a1494b19bd2a (svn r1281) -Fix: the OS/2 is now finished. Fixes:
truelight
parents: 796
diff changeset
   161
#endif
a1494b19bd2a (svn r1281) -Fix: the OS/2 is now finished. Fixes:
truelight
parents: 796
diff changeset
   162
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5168
diff changeset
   163
	DEBUG(driver, 1, "Loading dedicated server");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   164
	return NULL;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   165
}
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   166
7170
923946ec324f (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6937
diff changeset
   167
void VideoDriver_Dedicated::Stop()
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   168
{
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   169
#ifdef WIN32
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   170
	CloseWindowsConsoleThread();
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   171
#endif
1109
ecb98f43ba2c (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1090
diff changeset
   172
	free(_dedicated_video_mem);
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   173
}
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   174
7170
923946ec324f (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6937
diff changeset
   175
void VideoDriver_Dedicated::MakeDirty(int left, int top, int width, int height) {}
923946ec324f (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6937
diff changeset
   176
bool VideoDriver_Dedicated::ChangeResolution(int w, int h) { return false; }
8171
3fb9d1f8ac3b (svn r11734) -Change: Allow ToggleFullScreen to return the result of the operation' attempt. Previously, only visual clues were available.
belugas
parents: 8151
diff changeset
   177
bool VideoDriver_Dedicated::ToggleFullscreen(bool fs) { return false; }
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   178
6033
61682c6606c8 (svn r8756) [PSP] -Fix: made the dedicated code compile with PSP. It is almost UNIX, just it needs more includes ;)
truelight
parents: 5587
diff changeset
   179
#if defined(UNIX) || defined(__OS2__) || defined(PSP)
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6033
diff changeset
   180
static bool InputWaiting()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   181
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   182
	struct timeval tv;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   183
	fd_set readfds;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   184
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   185
	tv.tv_sec = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   186
	tv.tv_usec = 1;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   187
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   188
	FD_ZERO(&readfds);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   189
	FD_SET(STDIN, &readfds);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   190
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   191
	/* don't care about writefds and exceptfds: */
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   192
	return select(STDIN + 1, &readfds, NULL, NULL, &tv) > 0;
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   193
}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   194
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6033
diff changeset
   195
static uint32 GetTime()
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   196
{
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   197
	struct timeval tim;
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   198
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   199
	gettimeofday(&tim, NULL);
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   200
	return tim.tv_usec / 1000 + tim.tv_sec * 1000;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   201
}
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   202
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   203
#else
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   204
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6033
diff changeset
   205
static bool InputWaiting()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   206
{
4599
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
   207
	return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   208
}
1109
ecb98f43ba2c (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1090
diff changeset
   209
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6033
diff changeset
   210
static uint32 GetTime()
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   211
{
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   212
	return GetTickCount();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   213
}
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   214
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   215
#endif
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   216
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6033
diff changeset
   217
static void DedicatedHandleKeyInput()
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   218
{
9905
591248f0d341 (svn r14055) -Codechange: increase buffers for (dedicated) consoles as they couldn't use the full "extent" of the new chat message limit.
rubidium
parents: 9848
diff changeset
   219
	static char input_line[1024] = "";
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   220
4599
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
   221
	if (!InputWaiting()) return;
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   222
4599
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
   223
	if (_exit_game) return;
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   224
6033
61682c6606c8 (svn r8756) [PSP] -Fix: made the dedicated code compile with PSP. It is almost UNIX, just it needs more includes ;)
truelight
parents: 5587
diff changeset
   225
#if defined(UNIX) || defined(__OS2__) || defined(PSP)
4891
90fed43ba450 (svn r6835) - Fix: Pressing ^D (EOF) at a dedicated console caused it to repeat the last command, instead of doing nothing.
peter1138
parents: 4848
diff changeset
   226
	if (fgets(input_line, lengthof(input_line), stdin) == NULL) return;
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   227
#else
4599
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
   228
	/* Handle console input, and singal console thread, it can accept input again */
10201
4c181fbe8f2a (svn r14414) -Fix: replace instances of strncpy with strecpy as strncpy doesn't guarantee the resulting string is '\0'-terminated.
rubidium
parents: 10039
diff changeset
   229
	assert_compile(lengthof(_win_console_thread_buffer) <= lengthof(input_line));
4c181fbe8f2a (svn r14414) -Fix: replace instances of strncpy with strecpy as strncpy doesn't guarantee the resulting string is '\0'-terminated.
rubidium
parents: 10039
diff changeset
   230
	strcpy(input_line, _win_console_thread_buffer);
4599
79009a39da03 (svn r6449) -Fix (Win32): The dedicated server could overwrite the keyboard input buffer
Darkvater
parents: 4300
diff changeset
   231
	SetEvent(_hWaitForInputHandling);
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   232
#endif
736
75bcef85daeb (svn r1188) -Fix: [Network] Fixed that CTRL+<key> could sometimes hang a dedicated
truelight
parents: 721
diff changeset
   233
9848
95b1e335da61 (svn r13992) -Fix [FS#2189]: the dedicated console removed any character that was not a printable ASCII character instead. Now it allows UTF8 formated strings too.
rubidium
parents: 9533
diff changeset
   234
	/* strtok() does not 'forget' \r\n if the string starts with it,
95b1e335da61 (svn r13992) -Fix [FS#2189]: the dedicated console removed any character that was not a printable ASCII character instead. Now it allows UTF8 formated strings too.
rubidium
parents: 9533
diff changeset
   235
	 * so we have to manually remove that! */
95b1e335da61 (svn r13992) -Fix [FS#2189]: the dedicated console removed any character that was not a printable ASCII character instead. Now it allows UTF8 formated strings too.
rubidium
parents: 9533
diff changeset
   236
	strtok(input_line, "\r\n");
95b1e335da61 (svn r13992) -Fix [FS#2189]: the dedicated console removed any character that was not a printable ASCII character instead. Now it allows UTF8 formated strings too.
rubidium
parents: 9533
diff changeset
   237
	for (char *c = input_line; *c != '\0'; c++) {
95b1e335da61 (svn r13992) -Fix [FS#2189]: the dedicated console removed any character that was not a printable ASCII character instead. Now it allows UTF8 formated strings too.
rubidium
parents: 9533
diff changeset
   238
		if (*c == '\n' || *c == '\r' || c == lastof(input_line)) {
95b1e335da61 (svn r13992) -Fix [FS#2189]: the dedicated console removed any character that was not a printable ASCII character instead. Now it allows UTF8 formated strings too.
rubidium
parents: 9533
diff changeset
   239
			*c = '\0';
95b1e335da61 (svn r13992) -Fix [FS#2189]: the dedicated console removed any character that was not a printable ASCII character instead. Now it allows UTF8 formated strings too.
rubidium
parents: 9533
diff changeset
   240
			break;
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   241
		}
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   242
	}
9848
95b1e335da61 (svn r13992) -Fix [FS#2189]: the dedicated console removed any character that was not a printable ASCII character instead. Now it allows UTF8 formated strings too.
rubidium
parents: 9533
diff changeset
   243
	str_validate(input_line);
1046
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   244
f1d46abf7d35 (svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
darkvater
parents: 915
diff changeset
   245
	IConsoleCmdExec(input_line); // execute command
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   246
}
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   247
7170
923946ec324f (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6937
diff changeset
   248
void VideoDriver_Dedicated::MainLoop()
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   249
{
5581
cf2b8ba6556b (svn r8028) -Fix: overflow of ticks was not handled properly, possibly resulting a non-reacting gameserver/gameclient.
rubidium
parents: 5475
diff changeset
   250
	uint32 cur_ticks = GetTime();
cf2b8ba6556b (svn r8028) -Fix: overflow of ticks was not handled properly, possibly resulting a non-reacting gameserver/gameclient.
rubidium
parents: 5475
diff changeset
   251
	uint32 next_tick = cur_ticks + 30;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   252
770
c2eacca29601 (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 736
diff changeset
   253
	/* Signal handlers */
6033
61682c6606c8 (svn r8756) [PSP] -Fix: made the dedicated code compile with PSP. It is almost UNIX, just it needs more includes ;)
truelight
parents: 5587
diff changeset
   254
#if defined(UNIX) || defined(PSP)
702
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
   255
	signal(SIGTERM, DedicatedSignalHandler);
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
   256
	signal(SIGINT, DedicatedSignalHandler);
736
75bcef85daeb (svn r1188) -Fix: [Network] Fixed that CTRL+<key> could sometimes hang a dedicated
truelight
parents: 721
diff changeset
   257
	signal(SIGQUIT, DedicatedSignalHandler);
702
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
   258
#endif
5e80e4d69057 (svn r1152) -Add: [Network] Added signal handling (GeniusDex)
truelight
parents: 626
diff changeset
   259
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   260
	// Load the dedicated server stuff
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   261
	_is_network_server = true;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   262
	_network_dedicated = true;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10201
diff changeset
   263
	_network_playas = COMPANY_SPECTATOR;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10201
diff changeset
   264
	_local_company = COMPANY_SPECTATOR;
1414
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   265
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   266
	/* If SwitchMode is SM_LOAD, it means that the user used the '-g' options */
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   267
	if (_switch_mode != SM_LOAD) {
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 2791
diff changeset
   268
		StartNewGameWithoutGUI(GENERATE_NEW_SEED);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 2791
diff changeset
   269
		SwitchMode(_switch_mode);
1414
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   270
		_switch_mode = SM_NONE;
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   271
	} else {
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   272
		_switch_mode = SM_NONE;
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   273
		/* First we need to test if the savegame can be loaded, else we will end up playing the
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   274
		 *  intro game... */
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6878
diff changeset
   275
		if (!SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, BASE_DIR)) {
1414
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   276
			/* Loading failed, pop out.. */
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5168
diff changeset
   277
			DEBUG(net, 0, "Loading requested map failed, aborting");
1414
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   278
			_networking = false;
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   279
		} else {
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   280
			/* We can load this game, so go ahead */
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   281
			SwitchMode(SM_LOAD);
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   282
		}
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   283
	}
e7aa3a34712e (svn r1918) -Fix: [ 1101874 ] Dedicated server now accepts '-g' (load game) as param
truelight
parents: 1406
diff changeset
   284
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   285
	// Done loading, start game!
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   286
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   287
	if (!_networking) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5168
diff changeset
   288
		DEBUG(net, 0, "Dedicated server could not be started, aborting");
2228
93cdde0f85ba (svn r2748) Remove unused cruft from the main loop
tron
parents: 2210
diff changeset
   289
		return;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   290
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   291
2228
93cdde0f85ba (svn r2748) Remove unused cruft from the main loop
tron
parents: 2210
diff changeset
   292
	while (!_exit_game) {
5581
cf2b8ba6556b (svn r8028) -Fix: overflow of ticks was not handled properly, possibly resulting a non-reacting gameserver/gameclient.
rubidium
parents: 5475
diff changeset
   293
		uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   294
		InteractiveRandom(); // randomness
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   295
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   296
		if (!_dedicated_forks)
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
   297
			DedicatedHandleKeyInput();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   298
2180
c93b74122221 (svn r2694) Various smaller changes: eol-style, static, code simplification
tron
parents: 2177
diff changeset
   299
		cur_ticks = GetTime();
5581
cf2b8ba6556b (svn r8028) -Fix: overflow of ticks was not handled properly, possibly resulting a non-reacting gameserver/gameclient.
rubidium
parents: 5475
diff changeset
   300
		if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks) {
cf2b8ba6556b (svn r8028) -Fix: overflow of ticks was not handled properly, possibly resulting a non-reacting gameserver/gameclient.
rubidium
parents: 5475
diff changeset
   301
			next_tick = cur_ticks + 30;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   302
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   303
			GameLoop();
6615
74ec6e68e831 (svn r9835) -Codechange: use Pixel typedef instead of byte where ever possible
truelight
parents: 6265
diff changeset
   304
			_screen.dst_ptr = _dedicated_video_mem;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   305
			UpdateWindows();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   306
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   307
		CSleep(1);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   308
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   309
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   310
2195
ed135f2dc5f8 (svn r2710) Simplify dedicated server code a bit and don't compile it at all, if network support ist disabled
tron
parents: 2189
diff changeset
   311
#endif /* ENABLE_NETWORK */