diff --git a/src/image.cpp b/src/image.cpp new file mode 100644 index 0000000..5ad79b4 --- /dev/null +++ b/src/image.cpp @@ -0,0 +1,79 @@ +#include "image.hpp" +#include "debug.hpp" + +Image::Image() +{ + if (!m_image_opened) { + if (IMG_Init(IMG_INIT_PNG) != 0) { + Debug::log_err("Image system couldn't be initialized. Due to:", + IMG_GetError()); + + m_image_opened = false; + } + else { + Debug::log("Image system initialized."); + m_image_opened = true; + } + } + else { + Debug::log("Image system initialized."); + } +} + +Image::~Image() +{ + +} + +bool Image::is_open() +{ + return m_image_opened; +} + +SDL_Surface* Image::load_bmp(const std::string& path, int color_key_state, + std::array color_key) +{ + SDL_Surface* image = SDL_LoadBMP(path.c_str()); + + if (image == nullptr) { + Debug::log_err("Failed to load the image! Due to: ", SDL_GetError()); + } + else { + SDL_SetColorKey(image, color_key_state, SDL_MapRGB( + image->format, + color_key[0], color_key[1], color_key[2])); + } + return image; +} + +SDL_Surface* Image::load_bmp(const std::string &path, + std::array& color_key) +{ + return load_bmp(path, SDL_TRUE, color_key); +} + +SDL_Surface* Image::load_bmp(const std::string &path) +{ + std::array color_key_{{ 0, 0, 0 }}; + + return load_bmp(path, SDL_FALSE, color_key_); +} + +SDL_Surface* Image::load_png(const std::string &path) +{ + SDL_Surface* image = nullptr; + SDL_RWops* rwop = nullptr; + + if (m_image_opened) { + rwop = SDL_RWFromFile(path.c_str(), "rb"); + + image = IMG_LoadPNG_RW(rwop); + + if (image == nullptr) { + Debug::log_err("Failed to load the image! Due to: ", SDL_GetError()); + } + } + return image; +} + + diff --git a/src/image.hpp b/src/image.hpp new file mode 100644 index 0000000..ee61e12 --- /dev/null +++ b/src/image.hpp @@ -0,0 +1,66 @@ +#ifndef IMAGE_HPP +#define IMAGE_HPP + +#include +#include + +#include +#include + + +/** + * @brief The Image class provide functions to load BMP and PNG images. + */ +class Image +{ +private: + // + bool m_image_opened; +public: + Image(); + ~Image(); + + /** + * @brief is_open + * @return A boolean represeting the image system state. + */ + bool is_open(); + + /** + * @brief Function to load BMP images and then return a pointer to the + * surface that contains the image. + * @param The path to where the image is located. + * @param The color to be interpreted as transparent. + * @return A pointer to a surface which contains the image. + */ + SDL_Surface* load_bmp(const std::string& path, int color_key_state, + std::array color_key); + + /** + * @brief Function to load BMP images and then return a pointer to the + * surface that contains the image. + * @param The path to where the image is located. + * @param The color to be interpreted as transparent. + * @return A pointer to a surface which contains the image. + */ + SDL_Surface* load_bmp(const std::string &path, + std::array& color_key); + + /** + * @brief Function to load BMP images and then return a pointer to the + * surface that contains the image. + * @param The path to where the image is located. + * @return A pointer to a surface which contains the image. + */ + SDL_Surface* load_bmp(const std::string& path); + + /** + * @brief Function to load PNG images and then return a pointer to the + * surface that contains the image. + * @param The path to where the image is located. + * @return A pointer to a surface which contains the image. + */ + SDL_Surface* load_png(const std::string& path); +}; + +#endif // IMAGE_HPP diff --git a/src/random.hpp b/src/random.hpp index c0f4922..fc1ea00 100755 --- a/src/random.hpp +++ b/src/random.hpp @@ -15,6 +15,10 @@ #include +/** + * @brief The Random class is responsible to abstract methods in which the + * programmer will use to obtain random numbers. + */ class Random { private: std::random_device m_random_device;