00001 #include <SdlArs.h>
00002
00003 #ifdef _DEBUG
00004 # pragma comment( lib , "sdld.lib" )
00005 # pragma comment( lib , "sdlmaind.lib" )
00006 # pragma comment( lib , "sdl_imaged.lib" )
00007 # pragma comment( lib , "sdl_mixerd.lib" )
00008 # pragma comment( lib , "smpegd.lib" )
00009
00010 #else
00011 # pragma comment( lib , "sdl.lib" )
00012 # pragma comment( lib , "sdlmain.lib" )
00013 # pragma comment( lib , "sdl_image.lib" )
00014 # pragma comment( lib , "sdl_mixer.lib" )
00015 # pragma comment( lib , "smpeg.lib" )
00016
00017 #endif
00018 #pragma comment( lib , "libfreetype.lib" )
00019 #pragma comment( lib , "libz.lib" )
00020
00021 ofstream tron;
00022
00023
00024
00025 Ars::RGBColor ArsText_color( 0, 0, 0 );
00026 Ars::RGBColor ArsBack_color( 0xc0, 0xc0, 0xc0 );
00027
00028 Ars::ArsApplication *_mainApp_;
00029
00030 int Ars::ArsApplication::scrW = 1280 , Ars::ArsApplication::scrH = 1024;
00031
00032 extern Ars::Window *cbObjCreate( const icstring &name );
00033
00035
00036 namespace Ars
00037 {
00038
00039 extern SDL_mutex *rsrcMutex;
00040
00041 map<icstring,void *> ArsApplication::lbGetter;
00042 map<icstring,unsigned long> ArsApplication::Mnemo_f , ArsApplication::Mnemo_s;
00043 vector<cbObjCreateType> ArsApplication::objCreate;
00044
00045 ArsApplication *ArsApplication::instance = 0;
00046
00047 ArsApplication::ArsApplication( const int Argc , char **Argv )
00048 : argc( Argc ) , argv( Argv ) , exitCode( EXIT_FAILURE ) , frame( 0 )
00049 , running( false ) , defaultFontEngine( 0 ) , keyFocusWindow( 0 )
00050 {
00051 rsrcMutex = SDL_CreateMutex();
00052
00053 instance = this;
00054
00055 if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
00056 tron << "ArsApplication::ArsApplication : Could not initialize SDL: " << SDL_GetError() << "\n";;
00057
00058
00059 if(SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL) == -1)
00060 tron << "ArsApplication::ArsApplication : Error setting SDL keyboard repeat rate.\n";
00061
00062 Mix_OpenAudio( MIX_DEFAULT_FREQUENCY , MIX_DEFAULT_FORMAT , 2 , 4096 );
00063
00064 Mnemo_f["F_NONE"] = F_NONE;
00065 Mnemo_f["F_SELECTABLE"] = F_SELECTABLE;
00066 Mnemo_f["F_EXIT"] = F_EXIT;
00067 Mnemo_f["F_EDITABLE"] = F_EDITABLE;
00068 Mnemo_f["F_HIDETREE"] = F_HIDETREE;
00069 Mnemo_f["F_INDIRECT"] = F_INDIRECT;
00070 Mnemo_f["F_PASSWORD"] = F_PASSWORD ;
00071 Mnemo_f["F_MULTISELECT"] = F_MULTISELECT;
00072 Mnemo_f["F_TABSTOP"] = F_TABSTOP;
00073 Mnemo_f["F_FOLLOWMOUSE"] = F_FOLLOWMOUSE;
00074
00075 Mnemo_s["S_NONE"] = S_NONE;
00076 Mnemo_s["S_SELECTED"] = S_SELECTED;
00077 Mnemo_s["S_DISABLED"] = S_DISABLED;
00078 Mnemo_s["S_CHECKED"] = S_CHECKED;
00079 Mnemo_s["S_OUTLINED"] = S_OUTLINED;
00080 Mnemo_s["S_SHADOWED"] = S_SHADOWED;
00081 Mnemo_s["S_BORDER"] = S_BORDER;
00082 Mnemo_s["S_FOCUS"] = S_FOCUS;
00083
00084 Mnemo_s["S_ALIGN_LEFT"] = S_ALIGN_LEFT;
00085 Mnemo_s["S_ALIGN_RIGHT"] = S_ALIGN_RIGHT;
00086 Mnemo_s["S_ALIGN_TOP"] = S_ALIGN_TOP;
00087 Mnemo_s["S_ALIGN_BOTTOM"] = S_ALIGN_BOTTOM;
00088
00089 Mnemo_s["S_RAISED"] = S_RAISED;
00090 Mnemo_s["S_SUNKEN"] = S_SUNKEN;
00091 Mnemo_s["S_TRANSPARENT"] = S_TRANSPARENT;
00092 Mnemo_s["S_HIGHLIGHT"] = S_HIGHLIGHT;
00093 Mnemo_s["S_INVERTONFOLLOW"] = S_INVERTONFOLLOW;
00094
00095 objCreate.push_back( (cbObjCreateType)::cbObjCreate );
00096
00097 atexit( SDL_Quit );
00098 }
00099
00100 ArsApplication::~ArsApplication(void)
00101 {
00102 SDL_DestroyMutex( rsrcMutex );
00103
00104 if( frame )
00105 delete frame;
00106 frame = 0;
00107
00108 for( std::map<std::pair<std::string, unsigned char>, FontEngine*>::iterator iter = begin() ; iter != end() ; ++iter )
00109 {
00110 if (iter->second)
00111 {
00112 delete iter->second;
00113 iter->second = 0;
00114 }
00115 }
00116 clear();
00117
00118 while( !Cfg::Empty() )
00119 Cfg::Pop();
00120
00121 Ars::Graf::ResetImage();
00122 Ars::Graf::ResetCursors();
00123 Ars::UnloadSounds();
00124
00125 }
00126
00127 void ArsApplication::GenDefaultCfg( void )
00128 {
00129 Cfg::Handler &h = Cfg::GetCfg();
00130
00131 h.SetCfgColorHex( "Ars" , "text_color" , ArsText_color );
00132 h.SetCfgColorHex( "Ars" , "back_color" , ArsBack_color );
00133
00134 h.SetCfgFile( "Paths" , "Fonts" , "./data/Fonts/Fonts.cfg" );
00135 h.SetCfgFile( "Paths" , "Dialogs" , "./data/dialogs.cfg" );
00136 h.SetCfgFile( "Paths" , "Menu" , "./data/menu.cfg" );
00137 h.SetCfgFile( "Paths" , "Images" , "./data/images/images.cfg" );
00138
00139 h.Flush();
00140 }
00141
00142 void ArsApplication::Init(void)
00143 {
00144 if( Cfg::GetCfg().GetSize() == 0 )
00145 ((ArsApplication *)this)->GenDefaultCfg();
00146
00147 Cfg::Push( "Ars.Cfg" );
00148
00149 MessageServer::Instance().RegisterMessageClient( this , Message::APP_EXIT );
00150 SDL_EnableUNICODE( 1 );
00151
00152 Cfg::Handler &h = Cfg::GetCfg();
00153
00154 ArsText_color = h.GetCfgColorHex( "Ars" , "text_color" , RGBColor( 0, 0, 0) );
00155 ArsBack_color = h.GetCfgColorHex( "Ars" , "back_color" , RGBColor(223, 223, 223) );
00156
00157 Cfg::Handler &fnt = Cfg::Push( h.GetCfgString( "Paths" , "Fonts" ) );
00158 SetDefaultFontEngine( GetFontEngine( fnt.GetCfgFile( "" , "Font0" ) , 16 ) );
00159 }
00160
00161 void ArsApplication::SetKeyFocus( MessageClient *pWindow )
00162 {
00163
00164 if( keyFocusWindow != pWindow )
00165 {
00166 if( keyFocusWindow )
00167 ArsPostMessage( CtrlMessage( Message::LOST_FOCUS , keyFocusWindow , pWindow ) );
00168 ArsPostMessage( Message( Message::SET_FOCUS , pWindow ) );
00169 }
00170 keyFocusWindow = pWindow;
00171 }
00172
00173 void ArsApplication::Exec(void)
00174 {
00176 frame->Init( EmptyString );
00177 while( !MessageServer::Instance().empty() )
00178 MessageServer::Instance().ProcessMessage();
00179
00180 running = true;
00181 frame->MainLoop();
00182 }
00183
00184 string ArsApplication::GetCoordStr( void )
00185 {
00186 char temp[10];
00187 string ans( itoa( scrW , temp , 10 ) );
00188 ans += "x";
00189 ans += itoa( scrH , temp , 10 );
00190 return ans;
00191 }
00192
00193 const bool ArsApplication::wmExit( const Message &msg )
00194 {
00195
00196
00197 SDL_Event user_event;
00198 user_event.type=SDL_USEREVENT;
00199 user_event.user.code=0;
00200 user_event.user.data1=0;
00201 user_event.user.data2=0;
00202
00203
00204 exitCode = EXIT_SUCCESS;
00205 running = false;
00206
00207 return true;
00208 }
00209
00210 FontEngine* ArsApplication::GetFontEngine( std::string sFontFileName , unsigned char iFontSize )
00211 {
00212 std::map< std::pair< std::string, unsigned char>, FontEngine *>::iterator iterFontEngine = find(std::make_pair(sFontFileName, iFontSize));
00213 FontEngine* pFontEngine = 0;
00214
00215 if (iterFontEngine == end())
00216 {
00217
00218 pFontEngine = new FontEngine(sFontFileName, iFontSize);
00219 insert(std::make_pair(std::make_pair(sFontFileName, iFontSize), pFontEngine));
00220 }
00221 else
00222 pFontEngine = iterFontEngine->second;
00223
00224 return pFontEngine;
00225 }
00226
00227 const bool ArsApplication::MainLoop( void )
00228 {
00229 SDL_Event event;
00230 if( IsRunning() )
00231 {
00232 while( SDL_PollEvent( &event ) )
00233 HandleSDLEvent( event );
00234
00235 while( MessageServer::Instance().empty() )
00236 {
00237 while( SDL_PollEvent( &event ) )
00238 HandleSDLEvent( event );
00239 SDL_Delay( 10 );
00240 }
00241 MessageServer::Instance().ProcessMessage();
00242
00243 if( !SDL_PollEvent( &event ) )
00244 {
00245 if( MessageServer::Instance().empty() )
00246 frame->IdleProcess();
00247 }
00248 else
00249 HandleSDLEvent( event );
00250 }
00251 return IsRunning();
00252 }
00253
00254 XPoint ArsApplication::mousexy;
00255
00256 void ArsApplication::HandleSDLEvent( SDL_Event Event )
00257 {
00258
00259 switch (Event.type)
00260 {
00261 case SDL_VIDEORESIZE:
00262 ArsPostMessage( ResizeMessage( Message::CTRL_RESIZE, 0, 0, XPoint(Event.resize.w, Event.resize.h ) ) );
00263 break;
00264 case SDL_KEYDOWN:
00265 ArsPostMessage( KeyboardMessage( Message::KEYBOARD_KEYDOWN, GetKeyFocus(),Event.key.keysym.scancode, Event.key.keysym.mod,Event.key.keysym.sym, Event.key.keysym.unicode ) );
00266 break;
00267 case SDL_KEYUP:
00268 ArsPostMessage( KeyboardMessage( Message::KEYBOARD_KEYUP, GetKeyFocus(),Event.key.keysym.scancode, Event.key.keysym.mod,Event.key.keysym.sym, Event.key.keysym.unicode ) );
00269 break;
00270 case SDL_MOUSEBUTTONDOWN:
00271 mousexy = XPoint(Event.button.x, Event.button.y);
00272 ArsPostMessage( MouseMessage( Message::MOUSE_BUTTONDOWN, 0, XPoint(Event.button.x, Event.button.y), XPoint() , MouseMessage::TranslateSDLButton( Event.button.button ) ) );
00273 break;
00274 case SDL_MOUSEBUTTONUP:
00275 mousexy = XPoint(Event.button.x, Event.button.y);
00276 ArsPostMessage( MouseMessage( Message::MOUSE_BUTTONUP, 0, XPoint(Event.button.x, Event.button.y), XPoint() ,MouseMessage::TranslateSDLButton(Event.button.button) ) );
00277 break;
00278 case SDL_MOUSEMOTION:
00279 mousexy = XPoint(Event.motion.x, Event.motion.y);
00280 ArsPostMessage( MouseMessage( Message::MOUSE_MOVE, 0, XPoint(Event.motion.x, Event.motion.y) , XPoint(Event.motion.xrel, Event.motion.yrel), MouseMessage::TranslateSDLButtonState(Event.motion.state) ) );
00281 break;
00282 case SDL_QUIT:
00283 ArsPostMessage( Message( Message::APP_EXIT, 0 ) );
00284 break;
00285 }
00286 }
00287
00288 }
00290 extern const int TofMain( const int argc , char **argv );
00291
00292 #ifdef LINUX
00293 # define SDL_main main
00294 #endif
00295
00296 int main(int argc, char **argv)
00297 {
00298 string s = argv[0];
00299 s = s.substr( 0 , s.find_last_of( '.' ) );
00300 s += ".log";
00301 tron.open( s.c_str() , ios::app );
00302
00303 time_t t;
00304 time( &t );
00305 struct tm *tm = localtime( &t );
00306 tron << endl << argv[0] << endl;
00307 s.assign( (int)strlen( argv[0] ) , '=' );
00308 tron << s << endl << "started at " << asctime( tm ) << endl << endl;
00309
00310 int iExitCode = EXIT_FAILURE;
00311 iExitCode = TofMain( argc , argv );
00312
00313 t;
00314 time( &t );
00315 tm = localtime( &t );
00316 tron << endl << "finished at " << asctime( tm ) << endl << endl;
00317
00318 exit(iExitCode);
00319 }