-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
189 lines (148 loc) · 4.45 KB
/
game.cpp
File metadata and controls
189 lines (148 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
game.cpp -- executable to run Xash Engine
Copyright (C) 2011 Uncle Mike
This program 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, either version 3 of the License, or
(at your option) any later version.
This program 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.
*/
#include "port.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#if defined(__APPLE__) || defined(__unix__) || defined(__HAIKU__)
#define XASHLIB "libxash." OS_LIB_EXT
#elif _WIN32
#if !__MINGW32__ && _MSC_VER >= 1200
#define USE_WINMAIN
#endif
#define XASHLIB "xash.dll"
#define dlerror() GetStringLastError()
#include <shellapi.h> // CommandLineToArgvW
#endif
#ifdef WIN32
extern "C"
{
// Enable NVIDIA High Performance Graphics while using Integrated Graphics.
__declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
// Enable AMD High Performance Graphics while using Integrated Graphics.
__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
}
#endif
#define E_GAME "XASH3D_GAME" // default env dir to start from
#define GAME_PATH "diffusion" // default dir to start from
typedef void (*pfnChangeGame)( const char *progname );
typedef int (*pfnInit)( int argc, char **argv, const char *progname, int bChangeGame, pfnChangeGame func );
typedef void (*pfnShutdown)( void );
static pfnInit Xash_Main;
static pfnShutdown Xash_Shutdown = NULL;
static char szGameDir[128]; // safe place to keep gamedir
static int szArgc;
static char **szArgv;
static HINSTANCE hEngine;
static void Xash_Error( const char *szFmt, ... )
{
static char buffer[16384]; // must support > 1k messages
va_list args;
va_start( args, szFmt );
vsnprintf( buffer, sizeof(buffer), szFmt, args );
va_end( args );
#if defined( _WIN32 )
MessageBoxA( NULL, buffer, "Xash Error", MB_OK );
#else
fprintf( stderr, "Xash Error: %s\n", buffer );
#endif
exit( 1 );
}
#ifdef _WIN32
static const char *GetStringLastError()
{
static char buf[1024];
FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ),
buf, sizeof( buf ), NULL );
return buf;
}
#endif
static void Sys_LoadEngine( void )
{
if(( hEngine = LoadLibrary( XASHLIB )) == NULL )
{
Xash_Error("Unable to load the " XASHLIB ": %s", dlerror() );
}
if(( Xash_Main = (pfnInit)GetProcAddress( hEngine, "Host_Main" )) == NULL )
{
Xash_Error( XASHLIB " missed 'Host_Main' export: %s", dlerror() );
}
// this is non-fatal for us but change game will not working
Xash_Shutdown = (pfnShutdown)GetProcAddress( hEngine, "Host_Shutdown" );
}
static void Sys_UnloadEngine( void )
{
if( Xash_Shutdown ) Xash_Shutdown( );
if( hEngine ) FreeLibrary( hEngine );
Xash_Main = NULL;
Xash_Shutdown = NULL;
}
static void Sys_ChangeGame( const char *progname )
{
if( !progname || !progname[0] )
Xash_Error( "Sys_ChangeGame: NULL gamedir" );
if( Xash_Shutdown == NULL )
Xash_Error( "Sys_ChangeGame: missed 'Host_Shutdown' export\n" );
strncpy_s( szGameDir, progname, sizeof( szGameDir ) - 1 );
Sys_UnloadEngine ();
Sys_LoadEngine ();
Xash_Main( szArgc, szArgv, szGameDir, 1, Sys_ChangeGame );
}
_inline int Sys_Start( void )
{
int ret;
pfnChangeGame changeGame = NULL;
Sys_LoadEngine();
#ifndef XASH_DISABLE_MENU_CHANGEGAME
if( Xash_Shutdown )
changeGame = Sys_ChangeGame;
#endif
const char *game = getenv( E_GAME );
if( !game )
game = GAME_PATH;
ret = Xash_Main( szArgc, szArgv, game, 0, changeGame );
Sys_UnloadEngine();
return ret;
}
#ifndef USE_WINMAIN
int main( int argc, char **argv )
{
szArgc = argc;
szArgv = argv;
return Sys_Start();
}
#else
//#pragma comment(lib, "shell32.lib")
int __stdcall WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int nShow )
{
LPWSTR* lpArgv;
int ret, i;
lpArgv = CommandLineToArgvW( GetCommandLineW(), &szArgc );
szArgv = ( char** )malloc( (szArgc + 1) * sizeof( char* ));
for( i = 0; i < szArgc; ++i )
{
int size = wcslen(lpArgv[i]) + 1;
szArgv[i] = ( char* )malloc( size );
wcstombs( szArgv[i], lpArgv[i], size );
}
szArgv[szArgc] = 0;
LocalFree( lpArgv );
ret = Sys_Start();
for( ; i < szArgc; ++i )
free( szArgv[i] );
free( szArgv );
return ret;
}
#endif