Salut, j'ai lu le tuto sur le RAII, et j'aimerai donc l'appliquer à mon code. Cependant je ne sais pas si ce que j'ai fait est pertinent..
Engine.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /* -Engine.h- Classe du moteur permettant au jeu de fonctionner. //TODO */ #ifndef ENGINE #define ENGINE #pragma region Include #include "StateManager.h" #include "GameplayState.h" #include "TextureManager.h" #include "Exception.h" #include <SFML/Graphics.hpp> #pragma endregion class Engine { int m_LastExceptionCode; sf::RenderWindow* m_Window; TextureManager m_TextureManager; Exception m_EHandler; GameTime m_GameTime; StateManager m_StateManager; public: Engine::Engine(); Engine::~Engine(); void run(); void init(); void setLastExceptionCode(const int exceptionCode){m_LastExceptionCode = exceptionCode; }; void exit_now(); int const getLastExceptionCode(){return m_LastExceptionCode;}; void write_error(const char*, const char*); }; #endif |
Engine.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include "Engine.h" #include <cmath> Engine::Engine() { } void Engine::init() { setLastExceptionCode(0); m_EHandler.bind_engine(this); m_Window = new sf::RenderWindow(sf::VideoMode(800, 608), "Eugola"); //m_Window->setFramerateLimit(60); m_TextureManager.bind(&m_EHandler); m_StateManager.bind(m_Window, &m_TextureManager, &m_EHandler); State *g(0); g = new GameplayState(); m_StateManager.add_state(g); } Engine::~Engine() { delete m_Window; } |
Engine.cpp modifié
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | Engine::Engine() { try { setLastExceptionCode(0); m_EHandler.bind_engine(this); m_Window = new sf::RenderWindow(sf::VideoMode(800, 608), "Eugola"); //m_Window->setFramerateLimit(60); m_TextureManager.bind(&m_EHandler); m_StateManager.bind(m_Window, &m_TextureManager, &m_EHandler); State *g(0); g = new GameplayState(); m_StateManager.add_state(g); } catch (std::exception const& e) { write_error(typeid(e).name(), e.what()); MessageBoxA(NULL, LPCTSTR(std::string(std::string(typeid(e).name()) + std::string(" exception occured. Could not load the game.\n\n\nPlease send your log to the developer at guillaume.thivolet@gmail.com")).c_str()), "Eugola catched an exception", MB_OK | MB_ICONERROR); } } Engine::~Engine() { delete m_Window; } |
main.cpp normal:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include "main.h" using namespace std; int main(int argc, char* argv[]) { Engine engine; try { engine.init(); try { engine.run(); } catch (exception const& e) { engine.write_error(typeid(e).name(), e.what()); MessageBoxA(NULL, LPCTSTR(string(string(typeid(e).name()) + string(" exception occured.\n\n\nPlease send your log to the developer at guillaume.thivolet@gmail.com")).c_str()), "Eugola catched an exception", MB_OK | MB_ICONERROR); } } catch(exception const& e) { engine.write_error(typeid(e).name(), e.what()); MessageBoxA(NULL, LPCTSTR(string(string(typeid(e).name()) + string(" exception occured. Could not load the game.\n\n\nPlease send your log to the developer at guillaume.thivolet@gmail.com")).c_str()), "Eugola catched an exception", MB_OK | MB_ICONERROR); } return engine.getLastExceptionCode(); //0 by default } |
main.cpp modifié:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include "main.h" using namespace std; int main(int argc, char* argv[]) { Engine engine; try { engine.run(); } catch (exception const& e) { engine.write_error(typeid(e).name(), e.what()); MessageBoxA(NULL, LPCTSTR(string(string(typeid(e).name()) + string(" exception occured.\n\n\nPlease send your log to the developer at guillaume.thivolet@gmail.com")).c_str()), "Eugola catched an exception", MB_OK | MB_ICONERROR); } return engine.getLastExceptionCode(); //0 by default } |
Est-ce comme ça que l'on applique ce principe ?
+0
-0