ICT290 / src / engine / engine.h
engine.h
Raw
#pragma once
#include "../glIncludes.h"

#include "resources.h"
#include "stateManager.h"

/**
 * @class Engine
 * @brief The engine class that initialises the SDL2 and OpenGL context as well
 * as all of its managers. Contains the main game loop and a stateManager to
 * control the sequence of events on a per scene basis.
 *
 * @author Chase Percy
 *
 */
class Engine {
   public:
    /**
     * Responsible for starting all the managers and other aspects of the
     * engine. Used instead of constructors to ensure correct load order of
     * managers.
     * @return true if all managers start, else false.
     */
    Engine();

    /**
     * Responsible for stopping all the managers and other aspects of the
     * engine. Used instead of destructors to ensure correct closing order of
     * managers.
     * @return true if all managers stopped, else false.
     */
    ~Engine();

    /**
     * Once engine has started up successfully this is called to run the game
     * loop.
     */
    void run();

    /**
     * Sets the view port via glViewport.
     * @param width the width of the screen to set.
     * @param height the height of the screen to set.
     */
    void setViewport(int width, int height);

   private:
    /**
     * @struct DeltaTime
     * @brief A small struct to store the delta time components and a update
     * function to calculate the time delta between frames.
     *
     * @author Chase Percy
     *
     */
    struct DeltaTime {
        float current{0};                    /// The current time
        float delta{0};                      /// The delta time
        float previous{0};                   /// The previous time
        int fps{0};                          /// The current Frames Per Second
        float fpsDelta{0};                   /// Adds delta up to 1 second
        float update(Resources& resources);  /// Updates the delta every frame
    };
    SDL_Window* m_window{nullptr};       /// The client window
    SDL_GLContext m_GLContext{nullptr};  /// The OpenGL context
    SDL_Event m_event{0};                /// The event queue
    bool m_engineRunning{false};         /// Engine running flag
    StateManager m_stateManager;         /// The state manager
    Resources m_resources;               /// The game resources and managers
    DeltaTime deltaTime;                 /// The delta time between frames

    /**
     * Initialises OpenGL.
     * @return true if initialised successfully, else false
     */
    void startOpenGL();

    /**
     * Stops OpenGL and frees associated memory.
     */
    void stopOpenGL();

    /**
     * Initialises SDL2.
     * @return true if initialised successfully, else false.
     */
    void startSDL();

    /**
     * Stops SDL2 and frees associated memory.
     */
    void stopSDL();

    /**
     * Starts all the managers to be used during run time.
     * @return true if initialised successfully, else false.
     */
    void startManagers();

    /**
     * Updates the settings for the current state if the settings in resources
     * have been changed.
     */
    void updateSettings();

    /**
     * Load settings from a file. If the file cant be found or fails default
     * settings will be used.
     * @param filename the file with settings to load
     * @return true if settings were loaded, else false
     */
    bool loadSettings(const std::string& filename);

    /**
     * Save settings to a file
     * @param filename the file to save the settings to
     * @return true if settings were saved, else false
     */
    bool saveSettings(const std::string& filename);
};