00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #if defined( _WIN32 ) && defined(_MSC_VER)
00026 # include <windows.h>
00027 # include <io.h>
00028 # include <fcntl.h>
00029 #endif
00030 #include "Teddy/TeddyConfig.h"
00031 #include "Teddy/Application/BaseAudioManager.h"
00032 #include "Teddy/Application/BaseRoot.h"
00033 #include "Teddy/Graphics/View.h"
00034 #include "Teddy/Materials/Material.h"
00035 #include "Teddy/SysSupport/Messages.h"
00036 #include "Teddy/Teddy.h"
00037 #include "Teddy/SysSupport/StdSDL.h"
00038 #ifndef SWIG
00039 #include <cstdlib>
00040 #include <cstdio>
00041 #include <cstdlib>
00042 #include <cstring>
00043 #endif
00044 using namespace Teddy::SysSupport;
00045 using namespace Teddy::Graphics;
00046
00047
00048 namespace Teddy {
00049 namespace Application {
00050
00051
00052 BaseRoot::BaseRoot(){
00053 }
00054
00055
00056 BaseRoot::~BaseRoot(){
00057 }
00058
00059
00060 void BaseRoot::start( int argc, char **argv ){
00061 this->argc = argc;
00062 this->argv = argv;
00063 init_msg ();
00064 setDefaultOptions();
00065 parseOptions ();
00066
00067 # if defined(_WIN32) && defined(_MSC_VER) //&& defined(DEBUG_CONSOLE)
00068 if( hasOpt("--con") ){
00069 int hCrt;
00070 FILE *hf;
00071
00072 AllocConsole();
00073 hCrt = _open_osfhandle(
00074 (long) GetStdHandle(STD_OUTPUT_HANDLE),
00075 _O_TEXT
00076 );
00077 hf = _fdopen( hCrt, "w" );
00078 *stdout = *hf;
00079 setvbuf( stdout, NULL, _IONBF, 0 );
00080 }
00081 # endif
00082
00083 unsigned long sdl_init_flags = 0;
00084 sdl_init_flags |= SDL_INIT_VIDEO;
00085 sdl_init_flags |= SDL_INIT_AUDIO;
00086 sdl_init_flags |= SDL_INIT_TIMER;
00087 sdl_init_flags |= SDL_INIT_NOPARACHUTE;
00088 #if defined( HAVE_SDL_JOYSTICKOPEN )
00089 sdl_init_flags |= SDL_INIT_JOYSTICK;
00090 #endif
00091
00092 if( SDL_Init(sdl_init_flags) < 0 ){
00093 fmsg( M_FATAL, MSG_HEAD "Unable to initialize SDL: %s\n", SDL_GetError() );
00094 }else{
00095
00096
00097 atexit( SDL_Quit );
00098 }
00099
00100 Teddy::Materials::Material::init();
00101 Teddy::Graphics ::View ::init();
00102
00103 }
00104
00105
00106 void BaseRoot::setDefaultOptions(){
00107 screen_width = 640;
00108 screen_height = 480;
00109
00110 screen_options.setOptions( View::OPT_FRAME | View::OPT_MULTI_WINDOW | View::OPT_SEPARATE_SPECULAR_COLOR );
00111 #if !defined( DISABLE_AUDIO )
00112 audio_options .setOptions( BaseAudioManager::OPT_SAMPLES );
00113 #else
00114 audio_options .setOptions( 0 );
00115 #endif
00116
00117 }
00118
00119
00120 void BaseRoot::parseOptions(){
00121 if( hasOpt("--width") ) screen_width = getOptInt("--width");
00122 if( hasOpt("--height") ) screen_height = getOptInt("--height");
00123 if( hasOpt("--fullscreen") ) screen_options.enable ( View::OPT_FULLSCREEN );
00124 if( hasOpt("--no-fullscreen") ) screen_options.disable( View::OPT_FULLSCREEN );
00125 if( hasOpt("--multi-window") ) screen_options.enable ( View::OPT_MULTI_WINDOW );
00126 if( hasOpt("--no-multi-window") ) screen_options.disable( View::OPT_MULTI_WINDOW );
00127 if( hasOpt("--frame") ) screen_options.enable ( View::OPT_FRAME );
00128 if( hasOpt("--no-frame") ) screen_options.disable( View::OPT_FRAME );
00129 if( hasOpt("--ssc") ) screen_options.enable ( View::OPT_SEPARATE_SPECULAR_COLOR );
00130 if( hasOpt("--no-ssc") ) screen_options.disable( View::OPT_SEPARATE_SPECULAR_COLOR );
00131 if( hasOpt("--font-fix") ) screen_options.enable ( View::OPT_FONT_FIX );
00132 if( hasOpt("--no-font-fix") ) screen_options.disable( View::OPT_FONT_FIX );
00133
00134 #if !defined( DISABLE_AUDIO )
00135 if( hasOpt("--audio-samples") ) audio_options.enable ( BaseAudioManager::OPT_SAMPLES );
00136 if( hasOpt("--no-audio-samples") ) audio_options.disable ( BaseAudioManager::OPT_SAMPLES );
00137 if( hasOpt("--audio-midi") ) audio_options.enable ( BaseAudioManager::OPT_MIDI );
00138 if( hasOpt("--no-audio-midi") ) audio_options.disable ( BaseAudioManager::OPT_MIDI );
00139 #endif
00140
00141 if( hasOpt("--debug-init") ) enable_msg ( M_INIT );
00142 if( hasOpt("--debug-gl" ) ) enable_msg ( M_GL );
00143 if( hasOpt("--debug-wm" ) ) enable_msg ( M_WM );
00144 if( hasOpt("--debug-wmd" ) ) enable_msg ( M_WMD );
00145 if( hasOpt("--debug-wme" ) ) enable_msg ( M_WME );
00146 if( hasOpt("--debug-wml" ) ) enable_msg ( M_WML );
00147 if( hasOpt("--debug-mat" ) ) enable_msg ( M_MAT );
00148 if( hasOpt("--debug-mod" ) ) enable_msg ( M_MOD );
00149 if( hasOpt("--debug-lwo" ) ) enable_msg ( M_LWO );
00150 if( hasOpt("--debug-lws" ) ) enable_msg ( M_LWS );
00151 if( hasOpt("--debug-tmap") ) enable_msg ( M_TMAP );
00152 if( hasOpt("--debug-vert") ) enable_msg ( M_VERT );
00153 if( hasOpt("--debug-ffe" ) ) enable_msg ( M_FFE );
00154 if( hasOpt("--debug-net" ) ) enable_msg ( M_NET );
00155 if( hasOpt("--debug-scn" ) ) enable_msg ( M_SCN );
00156 if( hasOpt("--debug-aud" ) ) enable_msg ( M_AUDIO );
00157
00158 if( hasOpt("--no-debug-init") ) disable_msg ( M_INIT );
00159 if( hasOpt("--no-debug-gl" ) ) disable_msg ( M_GL );
00160 if( hasOpt("--no-debug-wm" ) ) disable_msg ( M_WM );
00161 if( hasOpt("--no-debug-wmd" ) ) disable_msg ( M_WMD );
00162 if( hasOpt("--no-debug-wme" ) ) disable_msg ( M_WME );
00163 if( hasOpt("--no-debug-wml" ) ) disable_msg ( M_WML );
00164 if( hasOpt("--no-debug-mat" ) ) disable_msg ( M_MAT );
00165 if( hasOpt("--no-debug-mod" ) ) disable_msg ( M_MOD );
00166 if( hasOpt("--no-debug-lwo" ) ) disable_msg ( M_LWO );
00167 if( hasOpt("--no-debug-lws" ) ) disable_msg ( M_LWS );
00168 if( hasOpt("--no-debug-tmap") ) disable_msg ( M_TMAP );
00169 if( hasOpt("--no-debug-vert") ) disable_msg ( M_VERT );
00170 if( hasOpt("--no-debug-ffe" ) ) disable_msg ( M_FFE );
00171 if( hasOpt("--no-debug-net" ) ) disable_msg ( M_NET );
00172 if( hasOpt("--no-debug-scn" ) ) disable_msg ( M_SCN );
00173 if( hasOpt("--no-debug-aud" ) ) disable_msg ( M_AUDIO );
00174 }
00175
00176
00177 Options &BaseRoot::getAudioOptions(){
00178 return audio_options;
00179 }
00180
00181
00182 Options &BaseRoot::getScreenOptions(){
00183 return screen_options;
00184 }
00185
00186
00187 const char *BaseRoot::getOptString( char *key ){
00188 const char *val = "";
00189 for( int i=1; i<argc; i++ ){
00190 if( strcmp(argv[i],key) == 0 ){
00191 if( argc > i+1 ){
00192 val = argv[i+1];
00193 }
00194 }
00195 }
00196 return val;
00197 }
00198
00199
00200 int BaseRoot::getOptInt( char *key ){
00201 int i_val = 0;
00202 for( int i=1; i<argc; i++ ){
00203 if( strcmp(argv[i],key) == 0 ){
00204 if( argc > i+1 ){
00205 i_val = atoi( argv[i+1] );
00206 }
00207 }
00208 }
00209 return i_val;
00210 }
00211
00212
00213 float BaseRoot::getOptFloat( char *key ){
00214 float f_val = 0;
00215 for( int i=1; i<argc; i++ ){
00216 if( strcmp(argv[i],key) == 0 ){
00217 if( argc > i+1 ){
00218 f_val = float( atof( argv[i+1] ) );
00219 }
00220 }
00221 }
00222 return f_val;
00223 }
00224
00225
00226 const char *BaseRoot::getArg( int i ){
00227 return ( argc>=i ) ? argv[i] : "";
00228 }
00229
00230
00231 bool BaseRoot::hasOpt( char *key ){
00232 for( int i=1; i<argc; i++ ){
00233 if( strcmp(argv[i],key) == 0 ){
00234 return true;
00235 }
00236 }
00237 return false;
00238 }
00239
00240
00241 int BaseRoot::getScreenWidth(){
00242 return screen_width;
00243 }
00244
00245
00246 int BaseRoot::getScreenHeight(){
00247 return screen_height;
00248 }
00249
00250
00251 };
00252 };
00253