Skip to content

Commit

Permalink
Merge pull request #5 from oddbookworm/main
Browse files Browse the repository at this point in the history
Made RenderWindow a true singleton
  • Loading branch information
durkisneer1 authored Jan 10, 2024
2 parents 7f560e4 + 3c1a964 commit 6832653
Show file tree
Hide file tree
Showing 2 changed files with 315 additions and 161 deletions.
200 changes: 144 additions & 56 deletions include/RenderWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,66 +17,154 @@

namespace kn {

/// @brief The renderer context.
/// @warning This class must be a singleton.
/**
* @brief The renderer context.
*
* @warning This class must be a singleton.
*/
class RenderWindow final {
public:
/// @brief Create a window.
/// @param title The title of the window.
/// @param scale The scale of the window.
/// @param fullscreen Whether to make the window fullscreen.
RenderWindow(const std::string &title = "Kraken Window", int scale = 1, bool fullscreen = false);
~RenderWindow();

/// @brief Clear the screen.
/// @param color The color to clear the screen.
void cls(SDL_Color color = { 0, 0, 0, 255 });

/// @brief Flip the render frame buffer.
void flip();

/// @brief Draw a texture using rects.
/// @param texture The texture to draw.
/// @param crop The rectangle to draw from.
/// @param rect The rectangle to draw to.
void blit(const std::shared_ptr<Texture>& texture, Rect crop, Rect rect);

/// @brief Draw a texture to a position.
/// @param texture The texture to draw.
/// @param position The position to draw at.
void blit(const std::shared_ptr<Texture>& texture, const math::Vec2& position);

/// @brief Draw a texture using rects.
/// @param texture The texture to draw.
/// @param crop The rectangle to draw from.
/// @param rect The rectangle to draw to.
/// @param angle The angle to draw the texture.
/// @param flipX Whether to flip the texture on the x-axis.
/// @param flipY Whether to flip the texture on the y-axis.
void blitEx(const std::shared_ptr<Texture>& texture, Rect crop, Rect rect, double angle = 0.0, bool flipX = false, bool flipY = false);

/// @brief Draw a texture to a position.
/// @param texture The texture to draw.
/// @param position The position to draw at.
/// @param angle The angle to draw the texture.
/// @param flipX Whether to flip the texture on the x-axis.
/// @param flipY Whether to flip the texture on the y-axis.
void blitEx(const std::shared_ptr<Texture>& texture, const math::Vec2& position, double angle = 0.0, bool flipX = false, bool flipY = false);

/// @brief Get the window renderer.
/// @return The window renderer.
SDL_Renderer* getRenderer() { return renderer; }

/// @brief Get user events.
/// @return The user events.
const std::vector<KN_Event>& getEvents();

/**
* @brief Get a reference to the instance of the singleton
*
* @return reference to the instance
*/
static RenderWindow& getInstance();

/**
* @brief Clear the screen.
*
* @param color The color to clear the screen.
*/
void cls(SDL_Color color = { 0, 0, 0, 255 });

/**
* @brief Flip the render frame buffer.
*/
void flip();

/**
* @brief Draw a texture using rects.
*
* @param texture The texture to draw.
* @param crop The rectangle to draw from.
* @param rect The rectangle to draw to.
*/
void blit(const std::shared_ptr<Texture>& texture, Rect crop, Rect rect);

/**
* @brief Draw a texture to a position.
* @param texture The texture to draw.
* @param position The position to draw at.
*/
void blit(const std::shared_ptr<Texture>& texture, const math::Vec2& position);

/**
* @brief Draw a texture using rects.
* @param texture The texture to draw.
* @param crop The rectangle to draw from.
* @param rect The rectangle to draw to.
* @param angle The angle to draw the texture.
* @param flipX Whether to flip the texture on the x-axis.
* @param flipY Whether to flip the texture on the y-axis.
*/
void blitEx(const std::shared_ptr<Texture>& texture, Rect crop, Rect rect, double angle = 0.0, bool flipX = false, bool flipY = false);

/**
* @brief Draw a texture to a position.
* @param texture The texture to draw.
* @param position The position to draw at.
* @param angle The angle to draw the texture.
* @param flipX Whether to flip the texture on the x-axis.
* @param flipY Whether to flip the texture on the y-axis.
*/
void blitEx(const std::shared_ptr<Texture>& texture, const math::Vec2& position, double angle = 0.0, bool flipX = false, bool flipY = false);

/**
* @brief Get the window renderer.
* @return The window renderer.
*/
SDL_Renderer* getRenderer() { return m_renderer; }

/**
* @brief Get user events.
* @return The user events.
*/
const std::vector<KN_Event>& getEvents();

/**
* @brief Get the title of the window
*
* @return the title
*/
const std::string getTitle() const;

/**
* @brief Get whether the window is fullscreen or not
*
* @return true if fullscreen
* @return false otherwise
*/
bool getFullscreen() const;

/**
* @brief Get the scale of the window
*
* @return the scale
*/
static int getScale();

/**
* @brief Set the title of the window
*
* @param newTitle the new title
*/
void setTitle(const std::string& newTitle);

/**
* @brief Set whether the window is fullscreen or not
*
* @param fullscreen true if setting fullscreen, false otherwise
*/
void setFullscreen(bool fullscreen);

/**
* @brief Set the scale of the window. Only valid before instantiating
* RenderWindow for the first time
*
* @param newScale the scale of the window
*
* @return true if successfully set scale
* @return false otherwise
*/
static bool setScale(int newScale);

private:
SDL_Renderer* renderer = nullptr;
SDL_Window* window = nullptr;
/**
* @brief Private constructor for singleton design pattern
*/
RenderWindow();

/**
* @brief Destructor
*/
~RenderWindow();

/**
* @brief Deleted copy constructor to prevent copy
*/
RenderWindow(const RenderWindow& other) = delete;

/**
* @brief Deleted assignment operator to prevent assignment
*/
RenderWindow& operator=(const RenderWindow& rhs) = delete;

SDL_Renderer* m_renderer = nullptr;
SDL_Window* m_window = nullptr;

KN_Event event;
std::vector<SDL_Event> events;
KN_Event m_event;
std::vector<SDL_Event> m_events;
};

}
Loading

0 comments on commit 6832653

Please sign in to comment.