Skip to content

Commit

Permalink
Added the Image class and improved docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Campos Nunes committed Nov 17, 2015
1 parent 132d609 commit aeb3854
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/image.cpp
Original file line number Diff line number Diff line change
@@ -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<std::uint32_t, 3> 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<std::uint32_t, 3>& color_key)
{
return load_bmp(path, SDL_TRUE, color_key);
}

SDL_Surface* Image::load_bmp(const std::string &path)
{
std::array<std::uint32_t, 3> 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;
}


66 changes: 66 additions & 0 deletions src/image.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#ifndef IMAGE_HPP
#define IMAGE_HPP

#include <iostream>
#include <array>

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>


/**
* @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<std::uint32_t, 3> 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<std::uint32_t, 3>& 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
4 changes: 4 additions & 0 deletions src/random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

#include <random>

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

0 comments on commit aeb3854

Please sign in to comment.