2012-09-19 15:15:01 +00:00
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
# ifdef TORQUE_SHARED
# ifdef WIN32
# include <windows.h>
2014-03-14 16:02:27 +00:00
# include <string>
2012-09-19 15:15:01 +00:00
extern " C "
{
2014-03-14 15:36:43 +00:00
int ( * torque_winmain ) ( HINSTANCE hInstance , HINSTANCE h , LPSTR lpszCmdLine , int nShow ) = NULL ;
2012-09-19 15:15:01 +00:00
} ;
2014-03-14 16:02:27 +00:00
bool getDllName ( std : : wstring & dllName )
2014-03-11 09:57:39 +00:00
{
2014-03-14 16:02:27 +00:00
wchar_t filenameBuf [ MAX_PATH ] ;
DWORD length = GetModuleFileNameW ( NULL , filenameBuf , MAX_PATH ) ;
if ( length = = 0 ) return false ;
dllName = std : : wstring ( filenameBuf ) ;
size_t dotPos = dllName . find_last_of ( L " . " ) ;
if ( dotPos = = std : : wstring : : npos ) return false ;
dllName . erase ( dotPos ) ;
dllName + = L " .dll " ;
return true ;
}
2014-03-14 15:36:43 +00:00
2014-03-14 16:02:27 +00:00
int PASCAL WinMain ( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpszCmdLine , int nCommandShow )
{
std : : wstring dllName = std : : wstring ( ) ;
if ( ! getDllName ( dllName ) )
{
MessageBoxW ( NULL , L " Unable to find game dll " , L " Error " , MB_OK | MB_ICONWARNING ) ;
return - 1 ;
}
2014-03-14 15:36:43 +00:00
2014-03-14 16:02:27 +00:00
HMODULE hGame = LoadLibraryW ( dllName . c_str ( ) ) ;
2014-03-14 15:36:43 +00:00
if ( ! hGame )
{
2014-03-14 16:02:27 +00:00
wchar_t error [ 4096 ] ;
_swprintf_l ( error , sizeof ( error ) , L " Unable to load game library: %s. Please make sure it exists and the latest DirectX is installed. " , _get_current_locale ( ) , dllName . c_str ( ) ) ;
MessageBoxW ( NULL , error , L " Error " , MB_OK | MB_ICONWARNING ) ;
2014-03-14 15:36:43 +00:00
return - 1 ;
}
2014-03-14 16:02:27 +00:00
torque_winmain = ( int ( * ) ( HINSTANCE hInstance , HINSTANCE h , LPSTR lpszCmdLine , int nShow ) ) GetProcAddress ( hGame , " torque_winmain " ) ;
2014-03-14 15:36:43 +00:00
if ( ! torque_winmain )
{
2014-03-14 16:02:27 +00:00
wchar_t error [ 4096 ] ;
_swprintf_l ( error , sizeof ( error ) , L " Missing torque_winmain export in game library: %s. Please make sure that it exists and the latest DirectX is installed. " , _get_current_locale ( ) , dllName . c_str ( ) ) ;
MessageBoxW ( NULL , error , L " Error " , MB_OK | MB_ICONWARNING ) ;
2014-03-14 15:36:43 +00:00
return - 1 ;
}
2014-03-14 16:02:27 +00:00
int ret = torque_winmain ( hInstance , hPrevInstance , lpszCmdLine , nCommandShow ) ;
2014-03-14 15:36:43 +00:00
FreeLibrary ( hGame ) ;
return ret ;
2012-09-19 15:15:01 +00:00
}
# endif // WIN32
# ifdef __MACOSX__
# include <dlfcn.h>
# include <stdio.h>
# include <unistd.h>
# include <Carbon/Carbon.h>
extern " C " {
2014-03-14 15:36:43 +00:00
int ( * torque_macmain ) ( int argc , const char * * argv ) = 0 ;
2012-09-19 15:15:01 +00:00
}
void GetBasePath ( const char * * cpath , const char * * cname )
{
2014-03-14 15:36:43 +00:00
static char path [ 2049 ] ;
static char name [ 2049 ] ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
ProcessSerialNumber PSN ;
ProcessInfoRec pinfo ;
FSSpec pspec ;
FSRef fsr ;
OSStatus err ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
path [ 0 ] = 0 ;
name [ 0 ] = 0 ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
* cpath = path ;
* cname = name ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
// set up process serial number
PSN . highLongOfPSN = 0 ;
PSN . lowLongOfPSN = kCurrentProcess ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
// set up info block
pinfo . processInfoLength = sizeof ( pinfo ) ;
pinfo . processName = NULL ;
pinfo . processAppSpec = & pspec ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
// grab the vrefnum and directory
err = GetProcessInformation ( & PSN , & pinfo ) ;
if ( ! err ) {
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
FSSpec fss2 ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
strcpy ( name , & pspec . name [ 1 ] ) ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
err = FSMakeFSSpec ( pspec . vRefNum , pspec . parID , 0 , & fss2 ) ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
if ( ! err ) {
err = FSpMakeFSRef ( & fss2 , & fsr ) ;
if ( ! err ) {
2012-09-19 15:15:01 +00:00
err = ( OSErr ) FSRefMakePath ( & fsr , ( UInt8 * ) path , 2048 ) ;
2014-03-14 15:36:43 +00:00
}
}
}
2012-09-19 15:15:01 +00:00
}
int main ( int argc , const char * * argv )
{
2014-03-14 15:36:43 +00:00
void * gameBundle = 0 ;
char gameBundleFilename [ 2049 ] ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
const char * basePath ;
const char * appName ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
// Get the path to our app binary and the app name
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
GetBasePath ( & basePath , & appName ) ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
if ( ! basePath [ 0 ] | | ! appName [ 0 ] )
return ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
char appNameNoDebug [ 2049 ] ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
strcpy ( appNameNoDebug , appName ) ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
int i = strlen ( appName ) ;
while ( i > 0 )
{
if ( ! strcmp ( & appName [ i ] , " _DEBUG " ) )
{
appNameNoDebug [ i ] = 0 ;
break ;
}
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
i - - ;
}
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
sprintf ( gameBundleFilename , " %s.app/Contents/Frameworks/%s Bundle.bundle/Contents/MacOS/%s Bundle " , appName , appNameNoDebug , appNameNoDebug ) ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
// first see if the current directory is set properly
gameBundle = dlopen ( gameBundleFilename , RTLD_LAZY | RTLD_LOCAL ) ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
if ( ! gameBundle )
{
// Couldn't load the game bundle... so, using the path to the bundle binary fix up the cwd
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
if ( basePath [ 0 ] ) {
chdir ( basePath ) ;
chdir ( " ../../../ " ) ;
}
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
// and try again
gameBundle = dlopen ( gameBundleFilename , RTLD_LAZY | RTLD_LOCAL ) ;
}
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
if ( ! gameBundle )
return - 1 ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
torque_macmain = ( int ( * ) ( int argc , const char * * argv ) ) dlsym ( gameBundle , " torque_macmain " ) ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
if ( ! torque_macmain )
return - 1 ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
return torque_macmain ( argc , argv ) ;
2012-09-19 15:15:01 +00:00
}
# endif // __MACOSX
# ifdef __linux__
# include <dlfcn.h>
# include <stdio.h>
# include <unistd.h>
# include <string.h>
extern " C "
{
2014-03-14 15:36:43 +00:00
int ( * torque_unixmain ) ( int argc , const char * * argv ) = NULL ;
void ( * setExePathName ) ( const char * exePathName ) = NULL ;
2012-09-19 15:15:01 +00:00
}
int main ( int argc , const char * * argv )
{
2014-03-14 15:36:43 +00:00
// assume bin name is in argv[0]
int len = strlen ( argv [ 0 ] ) ;
char * libName = new char [ len + 4 ] ; // len + .so + NUL
strcpy ( libName , argv [ 0 ] ) ;
strcat ( libName , " .so " ) ;
// try to load the game lib
void * gameLib = dlopen ( libName , RTLD_LAZY | RTLD_LOCAL ) ;
delete [ ] libName ;
if ( gameLib = = NULL )
{
printf ( " %s \n " , dlerror ( ) ) ;
return - 1 ;
}
// set the filename of the exe image
setExePathName = ( void ( * ) ( const char * ) ) dlsym ( gameLib , " setExePathName " ) ;
if ( setExePathName = = NULL )
{
printf ( " %s \n " , dlerror ( ) ) ;
return - 1 ;
}
setExePathName ( argv [ 0 ] ) ;
// try to load the lib entry point
torque_unixmain = ( int ( * ) ( int argc , const char * * argv ) ) dlsym ( gameLib , " torque_unixmain " ) ;
if ( torque_unixmain = = NULL )
{
printf ( " %s \n " , dlerror ( ) ) ;
return - 1 ;
}
// Go!
return torque_unixmain ( argc , argv ) ;
2012-09-19 15:15:01 +00:00
}
# endif // __linux__
# else //static exe build
# include "platform/platform.h"
# include "app/mainLoop.h"
# include "T3D/gameFunctions.h"
// Entry point for your game.
//
// This is build by default using the "StandardMainLoop" toolkit. Feel free
// to bring code over directly as you need to modify or extend things. You
// will need to merge against future changes to the SML code if you do this.
S32 TorqueMain ( S32 argc , const char * * argv )
{
2014-03-14 15:36:43 +00:00
// Some handy debugging code:
// if (argc == 1) {
// static const char* argvFake[] = { "dtest.exe", "-jload", "test.jrn" };
// argc = 3;
// argv = argvFake;
// }
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
// Memory::enableLogging("testMem.log");
// Memory::setBreakAlloc(104717);
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
// Initialize the subsystems.
StandardMainLoop : : init ( ) ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
// Handle any command line args.
if ( ! StandardMainLoop : : handleCommandLine ( argc , argv ) )
{
Platform : : AlertOK ( " Error " , " Failed to initialize game, shutting down. " ) ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
return 1 ;
}
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
// Main loop
while ( StandardMainLoop : : doMainLoop ( ) ) ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
// Clean everything up.
StandardMainLoop : : shutdown ( ) ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
// Do we need to restart?
if ( StandardMainLoop : : requiresRestart ( ) )
Platform : : restartInstance ( ) ;
2012-09-19 15:15:01 +00:00
2014-03-14 15:36:43 +00:00
// Return.
return 0 ;
2012-09-19 15:15:01 +00:00
}
# endif //TORQUE_SHARED