-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the Image class and improved docs.
- Loading branch information
Rafael Campos Nunes
committed
Nov 17, 2015
1 parent
132d609
commit aeb3854
Showing
3 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters