diff --git a/include/asset.h b/include/asset.h new file mode 100644 index 0000000000000000000000000000000000000000..a2e1cd798d41dfef6da571748b0f202dbeb8a32c --- /dev/null +++ b/include/asset.h @@ -0,0 +1,15 @@ +#ifndef __ASSET_H__ +#define __ASSET_H__ + +#include "logger.h" + +class Asset +{ + public: + Asset(); + ~Asset(); + private: + Logger& _logger; +}; + +#endif // __ASSET_H__ diff --git a/include/game-engine.h b/include/game-engine.h index 4f92753c5626595e9dd54380754d710edaf696d0..8eaaf2aa474abe48dae1a962da38597b840ebc4d 100644 --- a/include/game-engine.h +++ b/include/game-engine.h @@ -4,10 +4,16 @@ #include <map> #include <string> #include <vector> +#include <chrono> +#include <ctime> +#include <SDL.h> + + // Return #include "logger.h" #include "entity-manager.h" #include "scene.h" +#include "window.h" class GameEngine { @@ -18,21 +24,28 @@ class GameEngine void run(); void update(); void close(); + void clean(); Scene* currentScene(); - void changeScene( Scene scene ); + // void changeScene( Scene scene ); // std::vector< Asset& > getAssets(); // Window& getWindow(); + std::vector<Asset> getAssets(); + void sUserInput(); + bool isRunning; + void handleInput(); private: + size_t _frame = 0; + std::time_t _frametime = 0; EntityManager _entityManager; Logger& _logger; std::string _currScene; - // Window* _window; + Window* _window; // std::vector<Asset> _assets; - std::map< std::string, Scene > _scenes; + // std::map< std::string, Scene > _scenes; /* Systems */ // sUserInput diff --git a/include/main.h b/include/main.h index b1e2153c3c449328bc6e3619b56851bdb9628725..bed9b6cc03b755adc9a12aed37f1fd0bd59b3d75 100644 --- a/include/main.h +++ b/include/main.h @@ -4,6 +4,8 @@ #include <iostream> #include <cstdint> +#define SDL_MAIN_HANDLED + enum GenerationMethod { Random, diff --git a/include/scene.h b/include/scene.h index 149f98d55d7c0700570d500b4a18273afef7155c..79747924cbe0c76576546ff48b4cfca9b373fb19 100644 --- a/include/scene.h +++ b/include/scene.h @@ -1,14 +1,33 @@ #ifndef __SCENE_H__ #define __SCENE_H__ +#include <map> +#include <string> +#include <vector> + #include "logger.h" +#include "asset.h" +#include "entities/entity-generic.h" +#include "entity-manager.h" class Scene{ public: Scene(); ~Scene(); + + virtual void update() = 0; + virtual void simulate() = 0; + virtual void doAction() = 0; + virtual void registerAction() = 0; private: + std::vector<Asset> _assets; Logger& _logger; + + EntityManager _entities; + size_t _frame; + + bool _hasEnded = false; + std::map< size_t, std::string> _actionMap; }; #endif // __SCENE_H__ diff --git a/include/window.h b/include/window.h index bf23aa48b1a02ed840d20ca37a59fd309b348f24..52f660f25c07623276a1c72139df189a0c75c0d9 100644 --- a/include/window.h +++ b/include/window.h @@ -1,7 +1,6 @@ #ifndef __WINDOW_H__ #define __WINDOW_H__ -#define SDL_MAIN_HANDLED #include <SDL.h> #include <SDL_image.h> diff --git a/makefile b/makefile index 3a19ac8ed9bab88ce76a62b0c06e46cc956c8d58..c628a4bbd9c52c5e8d4ef002957eec2502932562 100644 --- a/makefile +++ b/makefile @@ -68,7 +68,7 @@ $(TARGET): $(OBJ_FILES) $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp ${INC_DIR}/*.h @mkdir -p $(@D) - $(CXX) $(CXXFLAGS) -I${SDL_INCLUDE} -I$(INC_DIR) -c -o $@ $< + $(CXX) $(CXXFLAGS) $(LDFLAGS) -I${SDL_INCLUDE} -I$(INC_DIR) -c -o $@ $< clean: rm -rf $(BIN_DIR)/* $(BUILD_DIR)/* diff --git a/src/asset.cpp b/src/asset.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3b506fbf9964135bf6426579e2d5b7b9ac53510 --- /dev/null +++ b/src/asset.cpp @@ -0,0 +1,6 @@ +#include "asset.h" + +Asset::Asset() + : _logger( Logger::instance() ) +{ this->_logger.info( "New asset created" ); } +Asset::~Asset(){} diff --git a/src/game-engine.cpp b/src/game-engine.cpp index ea8cbdcb7f94b39c6400b8e5260f8b90af310c7d..e24ab037e23abe22760826ee30c1a53dc0195349 100644 --- a/src/game-engine.cpp +++ b/src/game-engine.cpp @@ -9,23 +9,84 @@ GameEngine::GameEngine() GameEngine::~GameEngine() { this->_logger.info( "Cleaning GameEngine Object" ); + this->clean(); } -void GameEngine::close(){} +void GameEngine::clean() +{ + delete this->_window; +} +void GameEngine::close(){ this->isRunning = false; } void GameEngine::init() { + this->_window = new Window(); + this->_window->spawn(); + this->_window->loadMedia(); + this->_window->update(); + this->isRunning = false; this->_currScene = ""; } -void GameEngine::changeScene( Scene scene ){} +// void GameEngine::changeScene( Scene scene ){} Scene* GameEngine::currentScene() { return nullptr; } void GameEngine::run() { + this->init(); this->isRunning = true; + this->_frame = 0; + std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); + std::time_t _frametime = std::chrono::system_clock::to_time_t(now); + while( this->isRunning ) { this->update(); } } + void GameEngine::update() { + // this->_window->processInput(); + this->handleInput(); + this->_entityManager.update(); + std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); + + if ( ++this->_frame % 1 == 0 ) { + std::time_t cTime = std::chrono::system_clock::to_time_t(now); + size_t delTime = cTime - this->_frametime; + this->_frametime = cTime; + printf( "Frame: %i at %.2f fps\n", this->_frame, delTime == 0 ? 0.0 : 1.0 / delTime ); + } + this->_window->update(); +} +void GameEngine::handleInput() +{ + SDL_Event e; + while( SDL_PollEvent( &e ) ) + { + // std::cout << "event" << std::endl; + if( e.type == SDL_QUIT ) this->close(); + else if( e.type == SDL_KEYDOWN ) + { + switch( e.key.keysym.sym ) + { + case SDL_QUIT: this->close(); break; + case SDLK_ESCAPE: this->close(); break; + // case SDLK_PAGEUP: + // this->_screenModulation.brighten( 5 ); + // break; + // case SDLK_PAGEDOWN: + // this->_screenModulation.darken( 5 ); + // break; + // case SDLK_PLUS: + // case SDLK_KP_PLUS: + // case SDLK_GREATER: + // if( this->_alpha < 0xFF ) this->_alpha += 5; + // break; + // case SDLK_LESS: + // case SDLK_MINUS: + // case SDLK_KP_MINUS: + // if( this->_alpha > 5 ) this->_alpha -= 5; + // break; + } + } + } } diff --git a/src/main.cpp b/src/main.cpp index 8f092e1306505b0cb477285c3c2c94a47ed6814f..bea7739e8da0f1833a8a6623621d6a064824af9a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,7 +19,6 @@ int main( int argc, char* argv[] ) { // Seed the random number generator srand(static_cast<unsigned int>(time(nullptr))); - std::vector< EntityGeneric* > entities; // Genrate a Random chunk @@ -39,43 +38,16 @@ int main( int argc, char* argv[] ) { // List Saves saveManager.list(); - // Initialise Window - Window* window = new Window(); - window->spawn(); - window->loadMedia( "assets/hello-world.bmp" ); - window->update(); - window->loadMedia(); GameEngine* game = new GameEngine(); // Main Loop - while( state != Exit ) - { - // if( ++tickCount > 1000000 ) - if( window->isClosed() ) - { - // Exit - state = Exit; - } - - // Update Entities - game->update(); - // Process Input - window->processInput(); - // Movement Updates - // Collision Updates - // Update Game State - // Render Game - window->update(); - - _frame++; - } + game->run(); userSettings.set( "exit", "teset" ); std::cout << userSettings; // Clean-up - delete window; - + while( entities.size() > 0 ) { EntityGeneric* entity = entities.back(); diff --git a/src/window.cpp b/src/window.cpp index b623414acfbf74033f9978b692e10faff791e8e4..abd0c53250e35d6028d798d8a12a8125cf1f6bbe 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -85,7 +85,7 @@ void Window::loadMedia() { this->_background->loadFromFile( "assets/background.png" ); this->_character->loadFromFile( "assets/person.png" ); - if( this->_water->loadFromFile( "assets/water_animated .png" ) ) + if( this->_water->loadFromFile( "assets/water_animated.png" ) ) { // Set Frame 1 this->_waterTicks[ 0 ].x = 0;