Added some structuring.
The game runs but only a black window is shown.
This commit is contained in:
parent
f132916a0f
commit
d0893cba3a
16 changed files with 296 additions and 88 deletions
|
@ -6,13 +6,59 @@
|
|||
#include <stdexcept>
|
||||
#include <cassert>
|
||||
#include <ciso646>
|
||||
#include <png.h>
|
||||
|
||||
namespace cloonel {
|
||||
namespace {
|
||||
///---------------------------------------------------------------------
|
||||
///---------------------------------------------------------------------
|
||||
SDL_Surface* LoadNewSurface (const std::string& parPath) {
|
||||
assert(false);
|
||||
enum GraphicFormat {
|
||||
GraphicFormat_Unknown,
|
||||
GraphicFormat_Png
|
||||
};
|
||||
struct GraphicFormatItem {
|
||||
const char* extension;
|
||||
size_t length;
|
||||
GraphicFormat enumvalue;
|
||||
};
|
||||
|
||||
const GraphicFormatItem g_graphicFormatItems[] = {
|
||||
{".png", 4, GraphicFormat_Png}
|
||||
};
|
||||
|
||||
GraphicFormat GuessGraphicFormatFromName (const std::string& parPath) __attribute__((pure));
|
||||
|
||||
///--------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------
|
||||
GraphicFormat GuessGraphicFormatFromName (const std::string& parPath) {
|
||||
const size_t dotPos = parPath.find_last_of('.');
|
||||
if (parPath.npos == dotPos) {
|
||||
return GraphicFormat_Unknown;
|
||||
}
|
||||
|
||||
const size_t extLen = parPath.size() - dotPos;
|
||||
for (size_t z = 0; z < sizeof(g_graphicFormatItems) / sizeof(g_graphicFormatItems[0]); ++z) {
|
||||
const GraphicFormatItem& currItem = g_graphicFormatItems[z];
|
||||
if (currItem.length == extLen and parPath.compare(dotPos, currItem.length, currItem.extension) == 0) {
|
||||
return currItem.enumvalue;
|
||||
}
|
||||
}
|
||||
return GraphicFormat_Unknown;
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------
|
||||
SDL_Surface* LoadNewPngSurface (const std::string& parPath) {
|
||||
PhysicsFSFile rawfile(parPath.c_str(), PhysicsFSFile::OpenMode_Read, "graphics");
|
||||
unsigned char header[8];
|
||||
assert(rawfile.IsOpen());
|
||||
|
||||
rawfile.Read(header, 8, 1);
|
||||
if (png_sig_cmp(header, 0, 8))
|
||||
return nullptr;
|
||||
|
||||
png_structp pngptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
||||
if (not pngptr)
|
||||
return nullptr;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
@ -46,8 +92,18 @@ namespace cloonel {
|
|||
///------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
void Texture::Reload() {
|
||||
const GraphicFormat fmt = GuessGraphicFormatFromName(m_path);
|
||||
Destroy();
|
||||
SDL_Surface* surf = SDL_LoadBMP(m_path.c_str());
|
||||
|
||||
SDL_Surface* surf = nullptr;
|
||||
switch (fmt) {
|
||||
case GraphicFormat_Png:
|
||||
surf = LoadNewPngSurface(m_path.c_str());
|
||||
|
||||
default:
|
||||
throw std::runtime_error(std::string("Unsupported file format for \"") + m_path + "\"");
|
||||
}
|
||||
|
||||
if (nullptr == surf)
|
||||
throw std::runtime_error(GetFullErrorMessage(__PRETTY_FUNCTION__, m_path));
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue