Commit 74d11cd1 authored by Jack's avatar Jack
Browse files

integrated png load if path ends with png

parent 1b209238
#include "loader.h"
#include "stegano.h"
#include <fstream>
#include <algorithm>
#include <iterator>
......@@ -11,41 +13,50 @@ using namespace retro8::io;
int LoaderP8::valueForHexDigit(char c)
int Loader::valueForHexDigit(char c)
{
int v = (c >= 'A') ? (c >= 'a') ? (c - 'a' + 10) : (c - 'A' + 10) : (c - '0');
assert(v >= 0 && v <= 0xf);
return v;
}
color_t LoaderP8::colorFromDigit(char d)
color_t Loader::colorFromDigit(char d)
{
return static_cast<color_t>(valueForHexDigit(d));
}
sprite_index_t LoaderP8::spriteIndexFromString(const char* c)
sprite_index_t Loader::spriteIndexFromString(const char* c)
{
int h = valueForHexDigit(c[0]);
int l = valueForHexDigit(c[1]);
return (h << 4) | l;
}
uint8_t LoaderP8::valueForUint8(const char* c)
uint8_t Loader::valueForUint8(const char* c)
{
int h = valueForHexDigit(c[0]);
int l = valueForHexDigit(c[1]);
return (h << 4) | l;
}
retro8::sprite_flags_t LoaderP8::spriteFlagsFromString(const char* c)
retro8::sprite_flags_t Loader::spriteFlagsFromString(const char* c)
{
int h = valueForHexDigit(c[0]);
int l = valueForHexDigit(c[1]);
return (h << 4) | l;
}
void LoaderP8::load(const std::string& path, Machine& m)
{
void Loader::load(const std::string& path, Machine& m)
{
/* if it's a PNG we should use other loader */
if (path.length() >= 4 && path.substr(path.length() - 4) == ".png")
{
Stegano stegano;
stegano.load(path, m);
return;
}
std::vector<std::string> lines;
std::ifstream input(path);
......@@ -192,7 +203,7 @@ void LoaderP8::load(const std::string& path, Machine& m)
if (index < UNUSED_CHANNEL)
music->setSound(i, index);
else
assert(index == UNUSED_CHANNEL + i);
assert(index == UNUSED_CHANNEL + i + 1);
}
++msc;
......
......@@ -9,7 +9,7 @@ namespace retro8
{
namespace io
{
class LoaderP8
class Loader
{
private:
int valueForHexDigit(char c);
......
......@@ -103,3 +103,23 @@ void Stegano::load(const PngData& data, Machine& m)
m.code().initFromSource(code);
}
//TODO: remove SDL_image and use lighter library
#include <SDL_image.h>
void Stegano::load(const std::string& path, Machine& m)
{
SDL_Surface* surface = IMG_Load(path.c_str());
if (!surface)
{
printf("Error while loading PNG cart: %s\n", IMG_GetError());
assert(false);
}
PngData pngData = { static_cast<const uint32_t*>(surface->pixels), surface->h * surface->w };
assert(surface->pitch == IMAGE_WIDTH * sizeof(uint32_t));
assert(surface->format->BytesPerPixel == 4);
load(pngData, m);
SDL_FreeSurface(surface);
}
\ No newline at end of file
......@@ -24,6 +24,7 @@ namespace retro8
uint8_t assembleByte(const uint32_t v);
public:
void load(const PngData& data, Machine& dest);
void load(const std::string& path, Machine& dest);
};
}
}
\ No newline at end of file
......@@ -105,10 +105,10 @@ void GameView::render()
machine.code().loadAPI();
retro8::io::LoaderP8 loader;
retro8::io::Loader loader;
if (_path.empty())
_path = "breakout_hero.p8";
_path = "get-out.png";
loader.load(_path, machine);
......@@ -168,7 +168,7 @@ else
text(_path.c_str(), 10, 10);
char buffer[16];
sprintf(buffer, "%.0f", 1000.0f / manager->lastFrameTicks());
sprintf(buffer, "%.0f/%c0", 1000.0f / manager->lastFrameTicks(), machine.code().require60fps() ? '6' : '3');
text(buffer, 10, 22);
SDL_RenderCopy(renderer, _outputTexture, nullptr, &dest);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment