[Fftrader-cvs] fftrader/src engine.cpp,NONE,1.1 engine.h,NONE,1.1 py_baselib.cpp,NONE,1.1 py_baselib
Status: Alpha
Brought to you by:
tomt64
Update of /cvsroot/fftrader/fftrader/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25444/src Modified Files: guiapply.cpp guiapply.h main.cpp scenario.h Added Files: engine.cpp engine.h py_baselib.cpp py_baselib.h py_load.cpp py_load.h Log Message: Started adding Python to the engine Index: main.cpp =================================================================== RCS file: /cvsroot/fftrader/fftrader/src/main.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** main.cpp 19 Aug 2005 08:22:34 -0000 1.2 --- main.cpp 19 Aug 2005 22:39:52 -0000 1.3 *************** *** 10,13 **** --- 10,14 ---- SDL_gfx >= 2.0.8 physfs (with zip support) >= 0.1.9 + Python 2.4.x SDL and its libraries may be found easily through Google *************** *** 51,54 **** --- 52,56 ---- Scenario *mainScen; Unit *ship; + Engine *engine; Map *map; GUI *gui2; *************** *** 106,111 **** // We initalize the random number generator srand(time(NULL)); ! ! // We store the state of keys here SDL_Surface *screen=SDL_GetVideoSurface(); --- 108,112 ---- // We initalize the random number generator srand(time(NULL)); ! engine=new Engine(); SDL_Surface *screen=SDL_GetVideoSurface(); *************** *** 128,131 **** --- 129,133 ---- delete fftinput; fftinput=prefs->GetInput(); + engine->SetInput(fftinput); PHYSFS_addToSearchPath("fpk/base.fpk", 1); //load base packfile SCREEN_WIDTH = 640; *************** *** 144,147 **** --- 146,150 ---- // handle error } + engine->AddFont("font10",font); font2=LoadTTF("fonts/arial.ttf", 14); if(!font2) { *************** *** 149,152 **** --- 152,156 ---- // handle error } + engine->AddFont("font14",font2); Log::Write("Main Fonts loaded"); *************** *** 171,174 **** --- 175,179 ---- SDL_WM_SetCaption("Final Frontier Trader", "Final Frontier Trader"); SDL_Surface *icon = LoadICON("gfx/ico/fftico.png"); + engine->AddSurface("main icon",icon); SDL_WM_SetIcon(icon, NULL); Log::Write("Icon and caption in place"); *************** *** 180,183 **** --- 185,189 ---- SDL_Surface *bg = LoadIMG("gfx/startupbg.png"); + engine->AddSurface("splash screen",bg); DrawIMG(bg,0,0); SDL_Flip(screen); *************** *** 188,191 **** --- 194,199 ---- stars->Init(SCREEN_WIDTH, SCREEN_HEIGHT); stars2->Init(SCREEN_WIDTH, SCREEN_HEIGHT); + engine->AddStarfield(stars, false); + engine->AddStarfield(stars2, true); Log::Write("Beginning warp stars initialized"); *************** *** 207,214 **** --- 215,224 ---- ship = new Unit; ship->Init(0, 0, "gfx/ship/ship.png","base.fpk",(float)0.1); + engine->SetMainShip(ship); Log::Write("Main ship initialized"); map = new Map; map->Init(1); + engine->SetMap(map); Log::Write("Map initialized"); *************** *** 219,226 **** --- 229,239 ---- shot = LoadSound("sound/shoot.wav"); explode = LoadSound("sound/expl01.wav"); + engine->AddSound("shot", shot); + engine->AddSound("explode", explode); Log::Write("Audio initialized and sounds loaded"); gui2=new GUI; gui2->Init(); + engine->AddGUI("openmessage",gui2); SDL_Delay(1000); *************** *** 233,237 **** screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE|SDL_FULLSCREEN); } ! else exit(0); } else if (fullscreen) --- 246,250 ---- screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE|SDL_FULLSCREEN); } ! else engine->Quit(); } else if (fullscreen) *************** *** 241,245 **** screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN); } ! else exit(0); } else if (software) screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_SWSURFACE); --- 254,258 ---- screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN); } ! else engine->Quit(); } else if (software) screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_SWSURFACE); *************** *** 282,303 **** /* clean up */ ! SDL_FreeSurface(bg); ! delete fftinput; ! delete gui2; ! delete stars; ! delete stars2; ! delete ship; ! delete map; ! delete mainScen; ! Mix_FreeChunk(shot); ! Mix_FreeChunk(explode); ! Mix_CloseAudio(); ! SDL_FreeSurface(icon); ! TTF_CloseFont(font); ! TTF_CloseFont(font2); ! PHYSFS_deinit(); ! TTF_Quit(); ! SDL_Quit(); ! Log::Write("Thank you for playing!"); return 0; --- 295,299 ---- /* clean up */ ! engine->Quit(); return 0; *************** *** 334,353 **** // close button clicked case SDL_QUIT: - delete fftinput; - delete gui2; delete gui3; ! delete stars; ! delete stars2; ! delete ship; ! delete map; ! Mix_FreeChunk(shot); ! Mix_FreeChunk(explode); ! Mix_CloseAudio(); ! TTF_CloseFont(font); ! TTF_CloseFont(font2); ! PHYSFS_deinit(); ! TTF_Quit(); ! SDL_Quit(); ! exit(0); break; --- 330,335 ---- // close button clicked case SDL_QUIT: delete gui3; ! engine->Quit(); break; *************** *** 540,566 **** fpkships=LoadShipsFromScen(mainScen); int quit=NewShipOptions(SCREEN_WIDTH, SCREEN_HEIGHT, fpkships); ! if (quit==-1) ! { ! delete fftinput; ! delete gui2; ! delete stars; ! delete stars2; ! delete ship; ! delete map; ! delete mainScen; ! Mix_FreeChunk(shot); ! Mix_FreeChunk(explode); ! Mix_CloseAudio(); ! TTF_CloseFont(font); ! TTF_CloseFont(font2); ! PHYSFS_deinit(); ! TTF_Quit(); ! SDL_Quit(); ! exit(0); ! } ! if (quit==1) ! { ! return false; ! } Uint32 ticks=SDL_GetTicks(); Log::Write("Loading Game..."); --- 522,527 ---- fpkships=LoadShipsFromScen(mainScen); int quit=NewShipOptions(SCREEN_WIDTH, SCREEN_HEIGHT, fpkships); ! if (quit==-1) engine->Quit(); ! if (quit==1) return false; Uint32 ticks=SDL_GetTicks(); Log::Write("Loading Game..."); *************** *** 571,574 **** --- 532,536 ---- map = new Map; map->Init(zoom); + engine->SetMap(map); Log::Write("Setting map to scenario"); mainScen->SetMap(map); *************** *** 658,661 **** --- 620,624 ---- Log::Write("Variables set, displaying opening message"); TTF_Font *font9=TTF_OpenFont("fonts/arial.ttf", 9); + engine->AddFont("font9",font9); while(!done) { *************** *** 1078,1082 **** } } - TTF_CloseFont(font9); delete FPS; return done; --- 1041,1044 ---- Index: guiapply.h =================================================================== RCS file: /cvsroot/fftrader/fftrader/src/guiapply.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** guiapply.h 19 Aug 2005 07:32:29 -0000 1.1 --- guiapply.h 19 Aug 2005 22:39:52 -0000 1.2 *************** *** 2,12 **** #define __GUIAPPLY_H__ #include "gui.h" #include "graphics.h" #include "input.h" #include "fftfile.h" - #include "units.h" ! extern Unit *ship; extern FFTInput *fftinput; extern Preferences *prefs; --- 2,12 ---- #define __GUIAPPLY_H__ + #include "engine.h" #include "gui.h" #include "graphics.h" #include "input.h" #include "fftfile.h" ! extern Engine *engine; extern FFTInput *fftinput; extern Preferences *prefs; --- NEW FILE: engine.cpp --- #include "engine.h" Engine::Engine() { Log::Write("Initializing Engine"); mainship=0; mainscen=0; maininput=0; mainmap=0; fonts.clear(); guis.clear(); sounds.clear(); surfaces.clear(); starfields.clear(); Py_Initialize(); LoadPythonLibraries(); } Engine::~Engine() { this->Quit(); } void Engine::Quit() { int i; for(i=0; i<(int)sounds.size(); i++) { Mix_FreeChunk(sounds[i].sound); } for(i=0; i<(int)fonts.size(); i++) { TTF_CloseFont(fonts[i].font); } for(i=0; i<(int)surfaces.size(); i++) { SDL_FreeSurface(surfaces[i].surface); } delete mainmap; delete mainship; delete mainscen; delete maininput; fonts.clear(); guis.clear(); sounds.clear(); surfaces.clear(); starfields.clear(); Mix_CloseAudio(); Py_Finalize(); PHYSFS_deinit(); TTF_Quit(); SDL_Quit(); Log::Write("Thank you for playing!"); } Unit *Engine::GetMainShip() { return mainship; } void Engine::SetMainShip(Unit *ship) { mainship=ship; } Scenario *Engine::GetMainScenario() { return mainscen; } void Engine::SetMainScenario(Scenario *scen) { mainscen=scen; } FFTInput *Engine::GetInput() { return maininput; } void Engine::SetInput(FFTInput *input) { maininput=input; } Map *Engine::GetMap() { return mainmap; } void Engine::SetMap(Map* map) { mainmap=map; } TTF_Font *Engine::GetFont(std::string name) { int i; for(i=0; i<(int)fonts.size(); i++) { if (!strcmp(name.c_str(),fonts[i].name.c_str())) return fonts[i].font; } return 0; } void Engine::AddFont(std::string name, TTF_Font *font) { if(this->GetFont(name)) { FFTFont fftfont; fftfont.font=font; fftfont.name=name; fonts.push_back(fftfont); } else { int i; for(i=0; i<(int)fonts.size(); i++) { if (!strcmp(name.c_str(),fonts[i].name.c_str())) { TTF_CloseFont(fonts[i].font); fonts[i].font=font; } } } } Mix_Chunk *Engine::GetSound(std::string name) { int i; for(i=0; i<(int)sounds.size(); i++) { if (!strcmp(name.c_str(),sounds[i].name.c_str())) return sounds[i].sound; } return 0; } void Engine::AddSound(std::string name, Mix_Chunk *sound) { if(this->GetSound(name)) { FFTSound fftsound; fftsound.sound=sound; fftsound.name=name; sounds.push_back(fftsound); } else { int i; for(i=0; i<(int)sounds.size(); i++) { if (!strcmp(name.c_str(),sounds[i].name.c_str())) { Mix_FreeChunk(sounds[i].sound); sounds[i].sound=sound; } } } } GUI *Engine::GetGUI(std::string name) { int i; for(i=0; i<(int)guis.size(); i++) { if (!strcmp(name.c_str(),guis[i].name.c_str())) return guis[i].gui; } return 0; } void Engine::AddGUI(std::string name, GUI *gui) { if(this->GetSound(name)) { FFTGUI fftgui; fftgui.gui=gui; fftgui.name=name; guis.push_back(fftgui); } else { int i; for(i=0; i<(int)guis.size(); i++) { if (!strcmp(name.c_str(),guis[i].name.c_str())) { delete guis[i].gui; guis[i].gui=gui; } } } } SDL_Surface *Engine::GetSurface(std::string name) { int i; for(i=0; i<(int)surfaces.size(); i++) { if (!strcmp(name.c_str(),surfaces[i].name.c_str())) return surfaces[i].surface; } return 0; } void Engine::AddSurface(std::string name, SDL_Surface *surface) { if(this->GetSound(name)) { FFTSurface fftsurface; fftsurface.surface=surface; fftsurface.name=name; surfaces.push_back(fftsurface); } else { int i; for(i=0; i<(int)surfaces.size(); i++) { if (!strcmp(name.c_str(),surfaces[i].name.c_str())) { delete surfaces[i].surface; surfaces[i].surface=surface; } } } } void Engine::AddStarfield(StarField *stars, bool above) { FFTStars fftstars; fftstars.stars=stars; fftstars.above=above; starfields.push_back(fftstars); } --- NEW FILE: py_load.h --- #ifndef __PY_LOAD_H__ #define __PY_LOAD_H__ #include "Python.h" #include "log.h" void LoadPythonLibraries(); #endif /* __PY_LOAD_H__ */ --- NEW FILE: py_baselib.h --- #ifndef __PY_BASELIB_H__ #define __PY_BASELIB_H__ #include "Python.h" #include "guiapply.h" PyObject* fft_log(PyObject* self, PyObject* args); PyObject* fft_exit(PyObject* self, PyObject* args); #endif /* __PY_BASELIB_H__ */ --- NEW FILE: engine.h --- #ifndef __ENGINE_H__ #define __ENGINE_H__ #include <deque> #include <string> #include "SDL_mixer.h" #include "SDL_ttf.h" #include "gui.h" #include "log.h" #include "py_load.h" #include "scenario.h" #include "units.h" struct FFTFont { TTF_Font *font; std::string name; }; struct FFTGUI { GUI *gui; std::string name; }; struct FFTStars { StarField *stars; bool above; }; struct FFTSound { Mix_Chunk *sound; std::string name; }; struct FFTSurface { SDL_Surface *surface; std::string name; }; class Engine { public: Engine(); ~Engine(); void Quit(); Unit *GetMainShip(); void SetMainShip(Unit *ship); Scenario *GetMainScenario(); void SetMainScenario(Scenario *scen); FFTInput *GetInput(); void SetInput(FFTInput *input); Map *GetMap(); void SetMap(Map* map); TTF_Font *GetFont(std::string name); void AddFont(std::string name, TTF_Font *font); Mix_Chunk *GetSound(std::string name); void AddSound(std::string name, Mix_Chunk *sound); GUI *GetGUI(std::string name); void AddGUI(std::string name, GUI *gui); SDL_Surface *GetSurface(std::string name); void AddSurface(std::string name, SDL_Surface *surface); void AddStarfield(StarField *stars, bool above); private: Unit *mainship; Scenario *mainscen; FFTInput *maininput; Map *mainmap; std::deque<FFTFont> fonts; std::deque<FFTGUI> guis; std::deque<FFTSound> sounds; std::deque<FFTSurface> surfaces; std::deque<FFTStars> starfields; }; #endif /* __ENGINE_H__ */ Index: scenario.h =================================================================== RCS file: /cvsroot/fftrader/fftrader/src/scenario.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** scenario.h 19 Aug 2005 08:22:34 -0000 1.2 --- scenario.h 19 Aug 2005 22:39:52 -0000 1.3 *************** *** 2,11 **** #define __FFTSCEN_H__ - #include "Python.h" #include <string> #include "fftfile.h" #include "gui.h" #include "map.h" - #include "units.h" using aries::newNode; --- 2,9 ---- *************** *** 16,21 **** using aries::NodeList; - extern Unit *ship; - void LoadMap(Map *map, const std::string& filename, float zoom); --- 14,17 ---- Index: guiapply.cpp =================================================================== RCS file: /cvsroot/fftrader/fftrader/src/guiapply.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** guiapply.cpp 19 Aug 2005 07:32:29 -0000 1.1 --- guiapply.cpp 19 Aug 2005 22:39:52 -0000 1.2 *************** *** 47,51 **** // close button clicked case SDL_QUIT: ! done=-1; break; --- 47,55 ---- // close button clicked case SDL_QUIT: ! TTF_CloseFont(font); ! delete startingGUI; ! delete stars; ! delete stars2; ! engine->Quit(); break; *************** *** 705,717 **** // close button clicked case SDL_QUIT: - delete fftinput; delete gui3; ! delete ship; ! Mix_FreeChunk(explode); ! Mix_CloseAudio(); ! PHYSFS_deinit(); ! TTF_Quit(); ! SDL_Quit(); ! exit(0); break; --- 709,715 ---- // close button clicked case SDL_QUIT: delete gui3; ! SDL_FreeSurface(bg); ! engine->Quit(); break; *************** *** 805,817 **** // close button clicked case SDL_QUIT: - delete fftinput; delete gui3; ! delete ship; ! Mix_FreeChunk(explode); ! Mix_CloseAudio(); ! PHYSFS_deinit(); ! TTF_Quit(); ! SDL_Quit(); ! exit(0); break; --- 803,809 ---- // close button clicked case SDL_QUIT: delete gui3; ! SDL_FreeSurface(bg); ! engine->Quit(); break; *************** *** 904,916 **** // close button clicked case SDL_QUIT: - delete fftinput; delete gui3; ! delete ship; ! Mix_FreeChunk(explode); ! Mix_CloseAudio(); ! PHYSFS_deinit(); ! TTF_Quit(); ! SDL_Quit(); ! exit(0); break; --- 896,902 ---- // close button clicked case SDL_QUIT: delete gui3; ! SDL_FreeSurface(bg); ! engine->Quit(); break; *************** *** 992,1004 **** // close button clicked case SDL_QUIT: - delete fftinput; delete gui3; ! delete ship; ! Mix_FreeChunk(explode); ! Mix_CloseAudio(); ! PHYSFS_deinit(); ! TTF_Quit(); ! SDL_Quit(); ! exit(0); break; --- 978,984 ---- // close button clicked case SDL_QUIT: delete gui3; ! SDL_FreeSurface(bg); ! engine->Quit(); break; --- NEW FILE: py_load.cpp --- #include "py_load.h" extern PyMethodDef fftMethods[]; extern PyMethodDef catchMethods[]; void LoadPythonLibraries() { Log::Write("Loading FFTrader Python Library"); PyImport_AddModule("fft"); PyObject* module = Py_InitModule3("fft", fftMethods, "fft standard module.\n"); PyImport_AddModule("catch"); PyObject* catchmodule = Py_InitModule3("catch", catchMethods, "catch sysout and syserr.\n"); PyRun_SimpleString("import sys; sys.path.insert(0, '.')"); PyObject* sysModule = PyImport_ImportModule("sys"); PyObject_SetAttrString(sysModule, "stdout", catchmodule); PyObject_SetAttrString(sysModule, "stderr", catchmodule); } --- NEW FILE: py_baselib.cpp --- #include "py_baselib.h" extern int SCREEN_WIDTH; extern int SCREEN_HEIGHT; PyMethodDef fftMethods[] = { {"Exit", fft_exit, METH_VARARGS, "Exit(message)\nExits the current game displaying \"message\", then returns to the main menu\n"}, {0} }; PyMethodDef catchMethods[] = { {"write", fft_log, METH_VARARGS, "write function to override sysout and syserr"}, {0} }; PyObject* fft_log(PyObject* self, PyObject* args) { char* LogStr = NULL; if (!PyArg_ParseTuple(args, "s", &LogStr)) return NULL; Log::Writen(LogStr); Py_INCREF(Py_None); return Py_None; } PyObject* fft_exit(PyObject* self, PyObject* args) { char* ExitStr = NULL; if (!PyArg_ParseTuple(args, "s", &ExitStr)) return NULL; Log::Write("Exiting the game via fft.Exit() with message \"%s\"",ExitStr); ExitToMenuBox(SCREEN_WIDTH, SCREEN_HEIGHT, ExitStr); Py_INCREF(Py_None); return Py_None; } |