Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
cleaned ip engine and world #20
Browse files Browse the repository at this point in the history
  • Loading branch information
noah1510 committed Feb 14, 2020
1 parent dd3dabe commit 2f1c738
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 121 deletions.
29 changes: 9 additions & 20 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,33 @@ void engine::init(){
//create an empty world
activeWorld = std::shared_ptr<world> (new world());

//fill that world
if( ( errorCode = worldSetup(activeWorld) ) <= 0 ){
return;
}

if(activeWorld == nullptr){
errorCode = -4;
return;
}


}

std::shared_ptr<world> engine::getActiveWorld(){
if(errorCode < 0){
return nullptr;
}

return activeWorld;
return std::shared_ptr<world>(activeWorld);
}

int engine::setActiveWorld(std::shared_ptr<world> newWorld){

if(newWorld != nullptr){
activeWorld.reset();

activeWorld = newWorld;
activeWorld = std::shared_ptr<world>(newWorld);
}else{
activeWorld = nullptr;
errorCode = -7;
stopGame(-7);
}

return errorCode;
return -7;

}

Expand All @@ -112,12 +106,6 @@ int engine::getErrorCode(){
return errorCode;
}

int engine::setFillWorldFunction( int fillFunction(std::shared_ptr<world>) ){
worldSetup = fillFunction;

return errorCode;
}

int engine::setPhysicsLoopFunction(int loop(engine*)){
physicsLoopFunction = loop;

Expand Down Expand Up @@ -152,13 +140,14 @@ void engine::stopGame(int error){
int engine::changeWorld(std::shared_ptr<world> 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;
}
}
Expand Down
48 changes: 19 additions & 29 deletions src/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ class engine{
///The currently active world (nullptr or empty world if none)
std::shared_ptr<world> activeWorld;

///a function which creates the initial world
std::function <int(std::shared_ptr<world>)> worldSetup;

///The function which is executed on each physics tick
std::function <int(engine*)> physicsLoopFunction;

Expand All @@ -105,32 +102,28 @@ 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
*/
engine_config getConfig();

/**
* @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
Expand All @@ -139,66 +132,68 @@ 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<world> newWorld);

/**
* @brief Get the Active World object
* @version Available since REDHAND_VERSION_NUMBER 1
*
* @return std::shared_ptr<world> A pointer to the currently active world
*/
std::shared_ptr<world> getActiveWorld();

/**
* @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
*/
GLFWwindow* getWindow();

/**
* @brief this function returns the error code
* @version Available since REDHAND_VERSION_NUMBER 1
*
* @return negative if something bad happened
*/
int getErrorCode();

/**
* @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<world>) );

/**
* @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*) );

/**
* @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
*/
int runGame();

/**
* @brief returns true if the game is running
* @version Available since REDHAND_VERSION_NUMBER 1
*
* @return true running
* @return false not running
Expand All @@ -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 <int(std::shared_ptr<engine>)>
*/
std::function <int(std::shared_ptr<engine>)> getPhysicsFunction();
};


6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();
}
Expand Down
Loading

0 comments on commit 2f1c738

Please sign in to comment.