00001
00002
00003 #include <iostream>
00004 #include <string>
00005 #include <cstdio>
00006 #include <cstdarg>
00007
00008
00009 extern "C" {
00010 #include <lua.h>
00011 #include <lualib.h>
00012 #include <lauxlib.h>
00013
00014 #include "lua_config.h"
00015 }
00016
00017
00018 #include "mt19937.hpp"
00019
00020
00021 #ifdef USE_IRRKLANG
00022 #include "irrKlang.h"
00023 #endif
00024 #include "irrlicht.h"
00025
00026 #include "irrConsole/console.h"
00027 using namespace irr;
00028
00029
00030 #include "landscape.h"
00031 #include "cerberus.h"
00032 #include "menu.h"
00033 #include "game.h"
00034
00035
00036 #ifdef __APPLE__
00037 #include "CoreFoundation/CoreFoundation.h"
00038 #endif // __APPLE__
00039
00040
00041
00042 char* formatString( char* format, ... )
00043 {
00044 static char tmpBuffer[4096];
00045 tmpBuffer[0]='\0';
00046
00047 va_list args;
00048 va_start( args, format );
00049 vsnprintf( tmpBuffer, sizeof(tmpBuffer), format, args );
00050 va_end( args );
00051
00052 return tmpBuffer;
00053 }
00054
00055
00056 void test_mt19937( void )
00057 {
00058 const unsigned int no_bits = 23000000;
00059 FILE* fh;
00060
00061 fprintf( stdout, "Writing 23 million bits to the file 'mt19937.bit'.\n" );
00062 fprintf( stdout, "Run the diehard tests on this file.\n" );
00063
00064
00065
00066
00067 mt19937* rng = new mt19937( ((unsigned long)(0)+8749UL)*23095UL );
00068
00069
00070 fh=fopen( "mt19937.bit", "wb" );
00071
00072 unsigned long random_number;
00073 for( unsigned int i=0; i<no_bits; i++ ) {
00074 random_number = rng->genrand_int32();
00075 fwrite( &random_number, 4, 1, fh );
00076 }
00077
00078 fclose( fh );
00079 delete rng;
00080
00081 fprintf( stdout, "Done, exiting.\n" );
00082 }
00083
00084
00091 int main( int argc, char* argv[] )
00092 {
00093 unsigned int level=0;
00094 std::string mediaPath=".";
00095
00096 #ifdef __APPLE__
00097
00098 const size_t pathSize = 1024;
00099 char path[pathSize];
00100
00101 CFBundleRef mainBundle = CFBundleGetMainBundle();
00102 CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL( mainBundle );
00103 if( !CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, pathSize) ) {
00104 puts("Unable to determine Resources path in app bundle");
00105 exit(-1);
00106 }
00107 CFRelease(resourcesURL);
00108
00109 mediaPath = path;
00110 std::cout << "Media Path: " << mediaPath << std::endl;
00111 #endif // __APPLE__
00112
00113
00114
00115 if( argc==2 ) {
00116 if( strcmp(argv[1], "testrng")==0 ) {
00117 test_mt19937();
00118 exit( 0 );
00119 } else if( strcmp(argv[1], "iwantthelandscapecodesplease")==0 ) {
00120 FILE* fh;
00121
00122 fh=fopen( "landscape_codes.txt", "w" );
00123 mt19937* rng = new mt19937( 0 );
00124 for( int level=1; level<10000; level++ ) {
00125 rng->init( ((unsigned long)(level)+8749UL)*23095UL );
00126 fprintf( fh, "%04d %08d\n", level, (unsigned int)(rng->genrand_real1()*100000000.0) );
00127 }
00128 delete rng;
00129 fclose( fh );
00130
00131 exit( 0 );
00132 } else {
00133 fprintf( stderr, "Parameter '%s' unknown!\n", argv[1] );
00134 }
00135 } else if( argc==3 ) {
00136 if( strcmp(argv[1], "level")==0 ) {
00137 level = (unsigned int)atoi( argv[2] );
00138 if( level>9999 ) {
00139 fprintf( stderr, "'level'(=%s) must be a number from 0 to 9999!\n", argv[2] );
00140 exit( -1 );
00141 }
00142 } else {
00143 fprintf( stderr, "Parameter '%s' unknown!\n", argv[1] );
00144 }
00145 }
00146
00147
00148 lua_State* luaState = lua_open();
00149 luaopen_base( luaState );
00150 luaopen_io( luaState );
00151 luaopen_string( luaState );
00152 luaopen_math( luaState );
00153
00154
00155 cConfiguration mConfiguration;
00156 mConfiguration.loadFromFile( luaState, formatString("%s/%s", mediaPath.c_str(), "cerberus.cfg") );
00157 mConfiguration.max_fps=4000.;
00158 mConfiguration.fps_period=1000.0/mConfiguration.max_fps;
00159
00160
00161 video::E_DRIVER_TYPE driverType = video::EDT_SOFTWARE;
00162 switch( mConfiguration.driverType ) {
00163 case 2: driverType = video::EDT_BURNINGSVIDEO; break;
00164 case 3: driverType = video::EDT_DIRECT3D8; break;
00165 case 4: driverType = video::EDT_DIRECT3D9; break;
00166 case 5: driverType = video::EDT_OPENGL; break;
00167 default: break;
00168 }
00169
00170
00171 IrrlichtDevice* mDevice = createDevice( driverType, core::dimension2d<s32>(mConfiguration.width, mConfiguration.height),
00172 mConfiguration.screenDepth, mConfiguration.fullscreen,
00173 false, mConfiguration.vsync );
00174 if( mDevice == 0 ) {
00175 fprintf( stderr, "Couldn't create device\n" );
00176 exit( -1 );
00177 }
00178
00179 if( !mDevice->getFileSystem()->addZipFileArchive( formatString("%s/%s", mediaPath.c_str(), "base.pk3"), true, false ) ) {
00180 mDevice->getLogger()->log( "Can't open base.pk3", "base.pk3 must be in the same directory as the executable", ELL_ERROR );
00181 exit( -1 );
00182 }
00183
00184
00185 cAudioDevice mAudioDevice( mediaPath );
00186
00187
00188 IC_Console mConsole( luaState );
00189 mConsole.getConfig().dimensionRatios.Y = 0.6f;
00190 mConsole.getConfig().fontName = "media/console.bmp";
00191 mConsole.initializeConsole( mDevice );
00192 mConsole.loadDefaultCommands( mDevice );
00193
00194 while( true ) {
00195
00196 if( level==0 ) {
00197 cMenuScreen menu( mDevice, &mConfiguration, &mAudioDevice );
00198 if( !menu.run(&level) )
00199 break;
00200 }
00201
00202
00203 cGame game( mDevice, &mConfiguration, &mAudioDevice, &mConsole, level );
00204 game.run();
00205
00206 level=0;
00207 }
00208
00209
00210 mDevice->drop();
00211
00212
00213 lua_close( luaState );
00214
00215 return 0;
00216 }
00217
00218
00222 cAudioDevice::cAudioDevice( std::string mediaPath )
00223 {
00224 #ifdef USE_IRRKLANG
00225 irrKlang = irrklang::createIrrKlangDevice();
00226
00227 if( !irrKlang )
00228 exit( -1 );
00229
00230 std::string fileName=mediaPath + "/hisboyelroy-revolve.ogg";
00231
00232 irrklang::ISound* snd = irrKlang->play2D( fileName.c_str(), true, false, true );
00233 if( snd ) {
00234 snd->setVolume( 0.5f );
00235 snd->drop();
00236 }
00237
00238
00239
00240 #else
00241 #ifdef USE_SDL
00242 #endif
00243 #endif
00244 }
00245
00246
00250 cAudioDevice::~cAudioDevice( void )
00251 {
00252 #ifdef USE_IRRKLANG
00253 irrKlang->removeAllSoundSources();
00254 irrKlang->drop();
00255 #else
00256 #ifdef USE_SDL
00257 #endif
00258 #endif
00259 }
00260
00261
00265 void cAudioDevice::setListenerPosition( const irrklang::vec3df& pos, const irrklang::vec3df& lookdir,
00266 const irrklang::vec3df& velPerSecond, const irrklang::vec3df& upVector )
00267 {
00268 #ifdef USE_IRRKLANG
00269 irrKlang->setListenerPosition( pos, lookdir );
00270 #else
00271 #ifdef USE_SDL
00272
00273 #endif
00274 #endif
00275 }
00276
00277
00281 bool cConfiguration::loadFromFile( lua_State* L, const char* filename )
00282 {
00283
00284 if( luaL_loadfile(L, filename) || lua_pcall(L, 0, 0, 0) )
00285 printf( "cannot run configuration file: %s", lua_tostring(L, -1) );
00286
00287 width = get_number( L, "width", 640 );
00288 height = get_number( L, "height", 400 );
00289 fullscreen = get_boolean( L, "fullscreen", false );
00290 screenDepth = (u32)get_number( L, "screenDepth", 16 );
00291 vsync = get_boolean( L, "vsync", false );
00292 driverType = (size_t)get_number( L, "driverType", 1 );
00293
00294 return true;
00295 }
00296