From 2f1c738d54fe55f774868e0526d58f06a7b94c0f Mon Sep 17 00:00:00 2001 From: noasakurajin Date: Fri, 14 Feb 2020 12:01:04 +0100 Subject: [PATCH] cleaned ip engine and world #20 --- src/engine.cpp | 29 ++++--------- src/engine.hpp | 48 +++++++++------------ src/main.cpp | 6 +-- src/world.cpp | 111 ++++++++++++++++++++----------------------------- src/world.hpp | 8 ++-- 5 files changed, 81 insertions(+), 121 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index ca82ea21..6384f293 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -63,17 +63,11 @@ void engine::init(){ //create an empty world activeWorld = std::shared_ptr (new world()); - //fill that world - if( ( errorCode = worldSetup(activeWorld) ) <= 0 ){ - return; - } - if(activeWorld == nullptr){ errorCode = -4; return; } - } std::shared_ptr engine::getActiveWorld(){ @@ -81,7 +75,7 @@ std::shared_ptr engine::getActiveWorld(){ return nullptr; } - return activeWorld; + return std::shared_ptr(activeWorld); } int engine::setActiveWorld(std::shared_ptr newWorld){ @@ -89,13 +83,13 @@ int engine::setActiveWorld(std::shared_ptr newWorld){ if(newWorld != nullptr){ activeWorld.reset(); - activeWorld = newWorld; + activeWorld = std::shared_ptr(newWorld); }else{ activeWorld = nullptr; - errorCode = -7; + stopGame(-7); } - return errorCode; + return -7; } @@ -112,12 +106,6 @@ int engine::getErrorCode(){ return errorCode; } -int engine::setFillWorldFunction( int fillFunction(std::shared_ptr) ){ - worldSetup = fillFunction; - - return errorCode; -} - int engine::setPhysicsLoopFunction(int loop(engine*)){ physicsLoopFunction = loop; @@ -152,13 +140,14 @@ void engine::stopGame(int error){ int engine::changeWorld(std::shared_ptr newWorld){ //if not a nullptr change world if(newWorld == nullptr){ - stopGame(); + stopGame(-5); return -5; } - errorCode = setActiveWorld(newWorld); - if(errorCode < 0){ - stopGame(); + + auto error = setActiveWorld(newWorld); + if(error < 0){ + stopGame(error); return -6; } } diff --git a/src/engine.hpp b/src/engine.hpp index 7eee9c1e..da343874 100644 --- a/src/engine.hpp +++ b/src/engine.hpp @@ -83,9 +83,6 @@ class engine{ ///The currently active world (nullptr or empty world if none) std::shared_ptr activeWorld; - ///a function which creates the initial world - std::function )> worldSetup; - ///The function which is executed on each physics tick std::function physicsLoopFunction; @@ -105,19 +102,13 @@ class engine{ /** * @brief Set the Config of the game engine to the given configuration - * *Describe the solution you'd like** -A clear and concise description of what you want to happen. - -**Describe alternatives you've considered** -A clear and concise description of any alternative solutions or features you've considered. - -**Additional context** -Add any other context or screenshots about the feature request here.ich sould be used (by default DEFAULT_ENGINE_CONFIG) + * @version Available since REDHAND_VERSION_NUMBER 1 */ void setConfig(engine_config conf); /** * @brief Get the current configuration of the engine + * @version Available since REDHAND_VERSION_NUMBER 1 * * @return engine_config The current configuration of the engine */ @@ -125,12 +116,14 @@ Add any other context or screenshots about the feature request here.ich sould be /** * @brief This function initilizes the engine like specified in the configuration. + * @version Available since REDHAND_VERSION_NUMBER 1 * */ void init(); /** * @brief Set the Active World object to the given world + * @version Available since REDHAND_VERSION_NUMBER 1 * * @param newWorld a pointer to the world that will be the new active world * @return int negative if someting went wrong @@ -139,14 +132,16 @@ Add any other context or screenshots about the feature request here.ich sould be /** * @brief change the world to the given new world + * @version Available since REDHAND_VERSION_NUMBER 1 * - * @param newWorld + * @param newWorld a shared pointer to the new world. An error will accour if it is a nullptr. * @return int */ int changeWorld(std::shared_ptr newWorld); /** * @brief Get the Active World object + * @version Available since REDHAND_VERSION_NUMBER 1 * * @return std::shared_ptr A pointer to the currently active world */ @@ -154,6 +149,8 @@ Add any other context or screenshots about the feature request here.ich sould be /** * @brief Get the Window object + * @warning will be deprecated in the future only giving access to feautures over methods of the engine + * @version Available since REDHAND_VERSION_NUMBER 1 * * @return GLFWwindow* a pointer to the currently active window */ @@ -161,6 +158,7 @@ Add any other context or screenshots about the feature request here.ich sould be /** * @brief this function returns the error code + * @version Available since REDHAND_VERSION_NUMBER 1 * * @return negative if something bad happened */ @@ -168,23 +166,18 @@ Add any other context or screenshots about the feature request here.ich sould be /** * @brief This functions clears the currently bound buffers. + * @version Available since REDHAND_VERSION_NUMBER 1 * */ void clearBuffers(); - /** - * @brief This function specifies which function to use to fill the initial world of the game - * - * @param fillFunction a function which should be used to fill the initial world of the game. It should a negative number is something went wrong and it needs a pointer to an empty world which will be filled as an argument. - * @return int a negative value if something went wrong - */ - int setFillWorldFunction( int fillFunction(std::shared_ptr) ); - /** * @brief Set the Loop Function of the engine. - * The loop function is the function responible for handeling all the inputs and calcualting all the physics + * @detail The loop function is the function responible for handeling all the inputs and calcualting all the physics + * @version Available since REDHAND_VERSION_NUMBER 1 * - * @param loop The loop function which returns a negative number if something went wrong and has three parameters, the currently active window, the currently active world and a pointer to the world which should be used next, which is a nullptr if there should be no change, + * @param loop The loop function which returns a negative number if something went wrong and has one parameter, which is a raw pointer to the engine object. + * @warning Do no delete the engine object or any of its members inside the physics loop, always modify them using the methods. * @return int the errorCode of the engine will be returned, negative if something bad happened */ int setPhysicsLoopFunction( int loop(engine*) ); @@ -192,6 +185,7 @@ Add any other context or screenshots about the feature request here.ich sould be /** * @brief This function runs the game, the engine handles all the logic to keep everything wunning for you. * @warning This function runs until the game is finished and the game should terminate. + * @version Available since REDHAND_VERSION_NUMBER 1 * * @return int negative if something bad happend, otherwise a positive return code */ @@ -199,6 +193,7 @@ Add any other context or screenshots about the feature request here.ich sould be /** * @brief returns true if the game is running + * @version Available since REDHAND_VERSION_NUMBER 1 * * @return true running * @return false not running @@ -207,17 +202,12 @@ Add any other context or screenshots about the feature request here.ich sould be /** * @brief stops the game when called + * @version Available since REDHAND_VERSION_NUMBER 1 * - * @param error the error code which the game should be set to. + * @param error the error code which the game should be set to, 0 if none is given. */ void stopGame(int error = 0); - /** - * @brief Get the Physics Function object - * - * @return std::function )> - */ - std::function )> getPhysicsFunction(); }; diff --git a/src/main.cpp b/src/main.cpp index d37469b8..e1693ae9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,9 +36,6 @@ int main(){ conf.title = "Redhand Test Game"; gameEngine->setConfig(conf); - //set the function which sets the initial state of the world - gameEngine->setFillWorldFunction(game_init); - //set the function which handles all the controls and physics computation gameEngine->setPhysicsLoopFunction(main_game_logic); @@ -50,6 +47,9 @@ int main(){ if(exitCode < 0){ gameEngine->stopGame(); }else{ + exitCode = game_init(gameEngine->getActiveWorld()); + if(exitCode < 0){return abs(exitCode);}; + //run the game exitCode = gameEngine->runGame(); } diff --git a/src/world.cpp b/src/world.cpp index f85ccf1c..7aa86a85 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -46,24 +46,21 @@ world::~world(){ } void world::tick(GLFWwindow* window){ - WorldObjectsMutex.lock_shared(); + std::shared_lock lock(WorldObjectsMutex); for(auto x : WorldObjects){ x->onLoop(window); } - WorldObjectsMutex.unlock_shared(); - } void world::draw(){ - WorldObjectsMutex.lock_shared(); + std::shared_lock lock(WorldObjectsMutex); for(auto x : WorldObjects){ x->draw(); } - WorldObjectsMutex.unlock_shared(); } void world::setWindowSize(int width, int height){ @@ -78,7 +75,7 @@ void world::setWindowSize(int width, int height){ WorldShaders.at(i)->setProjectionmatrix(projectionMatrix); } - std::scoped_lock lock(WorldObjectsMutex); + std::scoped_lock lock(WorldObjectsMutex); for(auto x:WorldObjects){ x->setScreenSize(width,height); @@ -99,6 +96,7 @@ int world::addShader(shader* shade){ WorldShaders.emplace_back(shade); if(WorldShaders.back() == shade){ shade->setProjectionmatrix(projectionMatrix); + shade->setCamera(cameraPosition[0], cameraPosition[1]); return 0; }else{ return -3; @@ -131,7 +129,7 @@ int world::addObject(game_object* obj){ return -2; } - std::scoped_lock lock(WorldObjectsMutex); + std::scoped_lock lock(WorldObjectsMutex); WorldObjects.emplace_back(obj); if(WorldObjects.back() == obj){ @@ -142,111 +140,94 @@ int world::addObject(game_object* obj){ } } -int world::removeShader(shader* shade){ - for(int i = 0; i < WorldShaders.size();i++){ - if(shade == WorldShaders.at(i)){ - WorldShaders.erase(WorldShaders.begin() + i); +int world::removeShader(std::string name){ + for(auto x:WorldShaders){ + if(x->getName().compare(name) == 0){ try{ - delete shade; + delete x; } catch(const std::exception& e){ - std::cout << e.what() << '\n'; + std::cerr << e.what() << '\n'; return -1; } return 0; - } + } } return 1; } -int world::removeTexture(texture2D* tex){ - for(int i = 0; i < WorldTextures.size();i++){ - if(tex == WorldTextures.at(i)){ - WorldTextures.erase(WorldTextures.begin() + i); +int world::removeTexture(std::string name){ + for(auto x:WorldTextures){ + if(x->getName().compare(name) == 0){ try{ - delete tex; + delete x; } catch(const std::exception& e){ - std::cout << e.what() << '\n'; + std::cerr << e.what() << '\n'; return -1; } return 0; - } + } } - return 1; + return 1; } -int world::removeObject(game_object* obj){ - std::scoped_lock lock(WorldObjectsMutex); +int world::removeObject(std::string name){ + std::scoped_lock lock(WorldObjectsMutex); - for(int i = 0; i < WorldObjects.size();i++){ - if(obj == WorldObjects.at(i)){ - WorldObjects.erase(WorldObjects.begin() + i); + for(auto x:WorldObjects){ + if(x->getName().compare(name) == 0){ try{ - delete obj; + delete x; } catch(const std::exception& e){ - std::cout << e.what() << '\n'; + std::cerr << e.what() << '\n'; return -1; } return 0; - } + } } - return 1; + + return 1; + } shader* world::getShaderByName(std::string name){ - int i = 0; - bool found = false; - while(i < WorldShaders.size()){ - if((WorldShaders.at(i)->getName()).compare(name) == 0){ - found = true; - break; - } - i++; + for(auto x:WorldShaders){ + if((x->getName()).compare(name) == 0){ + return x; + } } - if(found){ - return WorldShaders.at(i); - }else{ - return nullptr; - } + return nullptr; } texture2D* world::getTextureByName(std::string name){ - int i = 0; - bool found = false; - while(i < WorldTextures.size()){ - if((WorldTextures.at(i)->getName()).compare(name) == 0){ - found = true; - break; - } - i++; - } - if(found){ - return WorldTextures.at(i); - }else{ - return nullptr; + for(auto x:WorldTextures){ + if((x->getName()).compare(name) == 0){ + return x; + } } + + return nullptr; + } game_object* world::getObjectByName(std::string name){ - WorldObjectsMutex.lock_shared(); + std::shared_lock lock(WorldObjectsMutex); for(auto x:WorldObjects){ if(x->getName().compare(name) == 0){ - WorldObjectsMutex.unlock_shared(); return x; } } - WorldObjectsMutex.unlock_shared(); return nullptr; } @@ -255,8 +236,8 @@ void world::setCamera(float pos_x, float pos_y){ cameraPosition[0] = pos_x; cameraPosition[1] = pos_y; - for(int i = 0;i < WorldShaders.size();i++){ - WorldShaders.at(i)->setCamera(cameraPosition[0], cameraPosition[1]); + for(auto x : WorldShaders){ + x->setCamera(cameraPosition[0], cameraPosition[1]); } } @@ -264,7 +245,7 @@ void world::moveCamera(float delta_pos_x, float delta_pos_y){ cameraPosition[0] += delta_pos_x; cameraPosition[1] += delta_pos_y; - for(int i = 0;i < WorldShaders.size();i++){ - WorldShaders.at(i)->moveCamera(delta_pos_x, delta_pos_y); - } + for(auto x:WorldShaders){ + x->moveCamera(delta_pos_x, delta_pos_y); + } } \ No newline at end of file diff --git a/src/world.hpp b/src/world.hpp index 8f1677a8..9ecd3957 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -7,7 +7,7 @@ class world{ private: ///The mutex for allowing only one thread to acces the world objects at once - std::shared_timed_mutex WorldObjectsMutex; + std::shared_mutex WorldObjectsMutex; /// This vector holds all the game_objetcs in this world std::vector WorldObjects; @@ -66,21 +66,21 @@ class world{ * @param shade a pointer to the shader which should be removed * @return 0 if everything worked, 1 if no object was found, negative if something bad happened */ - int removeShader(shader* shade); + int removeShader(std::string name); /** * This function removes a texture from its world and delete the texture. * @param tex a pointer to the texture which should be removed * @return 0 if everything worked, 1 if no object was found, negative if something bad happened */ - int removeTexture(texture2D* tex); + int removeTexture(std::string name); /** * This function removes an object from its world and delete the object. * @param obj a pointer to the object which should be removed * @return 0 if everything worked, 1 if no object was found, negative if something bad happened */ - int removeObject(game_object* obj); + int removeObject(std::string name); /** * This function returns a pointer to the first shader with the given name