Commit 97f6b035 authored by Jack's avatar Jack
Browse files

preparing some work for p8.png decoding

parent e9fd1973
......@@ -130,6 +130,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\..\src\io\loader.cpp" />
<ClCompile Include="..\..\..\src\io\stegano.cpp" />
<ClCompile Include="..\..\..\src\lua\lapi.c" />
<ClCompile Include="..\..\..\src\lua\lauxlib.c" />
<ClCompile Include="..\..\..\src\lua\lbaselib.c" />
......@@ -176,6 +177,7 @@
<ItemGroup>
<ClInclude Include="..\..\..\src\common.h" />
<ClInclude Include="..\..\..\src\io\loader.h" />
<ClInclude Include="..\..\..\src\io\stegano.h" />
<ClInclude Include="..\..\..\src\lua\lapi.h" />
<ClInclude Include="..\..\..\src\lua\lauxlib.h" />
<ClInclude Include="..\..\..\src\lua\lcode.h" />
......
......@@ -135,6 +135,9 @@
<ClInclude Include="..\..\..\src\vm\sound.h">
<Filter>src\vm</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\io\stegano.h">
<Filter>src\io</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\src\main.cpp">
......@@ -266,5 +269,8 @@
<ClCompile Include="..\..\..\src\vm\sound.cpp">
<Filter>src\vm</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\io\stegano.cpp">
<Filter>src\io</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
......@@ -37,62 +37,6 @@ retro8::sprite_flags_t LoaderP8::spriteFlagsFromString(const char* c)
return (h << 4) | l;
}
//TODO: worst hacky solution ever, it's just a quick fix
void LoaderP8::fixOperators(std::string& line)
{
size_t t = 0;
size_t i0 = 0, i1 = 0;
enum class S { BEGIN, IDENT, INBETWEEN, OPERATOR } s = S::BEGIN;
while (t < line.length())
{
if (line[t] < 0)
break;
else if (isspace(line[t]))
{
if (s == S::BEGIN)
;
else if (s == S::INBETWEEN)
;
else if (s == S::IDENT)
{
i1 = t;
s = S::INBETWEEN;
}
else
break;
}
else if (isalnum(line[t]) || line[t] == '.' || line[t] == '_')
{
if (s == S::BEGIN)
{
s = S::IDENT;
i0 = t;
}
else if (s == S::IDENT)
;
else
break;
}
else if (line[t] == '*' || line[t] == '+' || line[t] == '-' || line[t] == '/' || line[t] == '%')
{
if (t < line.length() - 1 && line[t+1] == '=' && (s == S::IDENT || s == S::INBETWEEN))
{
if (s == S::IDENT)
i1 = t - 1;
// replace [i, t+1]
line = line.substr(0, i0) + line.substr(i0, i1 - i0) + " = " + line.substr(i0, i1 - i0) + ' ' + line[t] + ' ' + line.substr(t + 2);
break;
}
else
break;
}
++t;
}
}
void LoaderP8::load(const std::string& path, Machine& m)
{
std::vector<std::string> lines;
......
......@@ -12,8 +12,6 @@ namespace retro8
class LoaderP8
{
private:
void fixOperators(std::string& code);
int valueForHexDigit(char c);
retro8::color_t colorFromDigit(char d);
retro8::sprite_index_t spriteIndexFromString(const char* c);
......
#include "stegano.h"
#include <cassert>
using namespace retro8;
using namespace io;
constexpr size_t IMAGE_WIDTH = 160;
constexpr size_t IMAGE_HEIGHT = 205;
constexpr size_t RAW_DATA_LENGTH = 0x4300;
uint8_t Stegano::assembleByte(const uint32_t v)
{
constexpr uint32_t MASK_ALPHA = 0xff00000;
constexpr uint32_t MASK_RED = 0x000000ff;
constexpr uint32_t MASK_GREEN = 0x0000ff00;
constexpr uint32_t MASK_BLUE = 0x00ff0000;
constexpr uint32_t SHIFT_ALPHA = 24;
constexpr uint32_t SHIFT_RED = 0;
constexpr uint32_t SHIFT_GREEN = 8;
constexpr uint32_t SHIFT_BLUE = 16;
return
((((v & MASK_ALPHA) >> SHIFT_ALPHA) & 0b11) ) |
((((v & MASK_RED) >> SHIFT_RED) & 0b11) << 2) |
((((v & MASK_GREEN) >> SHIFT_GREEN) & 0b11) << 4) |
((((v & MASK_BLUE) >> SHIFT_BLUE) & 0b11) << 6);
}
void Stegano::load(const PngData& data, Machine& m)
{
constexpr size_t SPRITE_SHEET_SIZE = gfx::SPRITE_SHEET_HEIGHT * gfx::SPRITE_SHEET_WIDTH / gfx::PIXEL_TO_BYTE_RATIO;
constexpr size_t TILE_MAP_SIZE = gfx::TILE_MAP_WIDTH * gfx::TILE_MAP_HEIGHT * sizeof(sprite_index_t) / 2;
constexpr size_t SPRITE_FLAGS_SIZE = gfx::SPRITE_COUNT * sizeof(sprite_flags_t);
constexpr size_t MUSIC_SIZE = sfx::MUSIC_COUNT * sizeof(sfx::music_t);
constexpr size_t SOUND_SIZE = sfx::SOUND_COUNT * sizeof(sfx::sound_t);
static_assert(sizeof(sfx::music_t) == 4, "Must be 4 bytes");
static_assert(sizeof(sfx::sound_t) == 68, "Must be 68 bytes");
static_assert(RAW_DATA_LENGTH == SPRITE_SHEET_SIZE + TILE_MAP_SIZE + SPRITE_FLAGS_SIZE + MUSIC_SIZE + SOUND_SIZE, "Must be equal");
assert(data.length == IMAGE_WIDTH * IMAGE_HEIGHT);
/* first 0x4300 are read directly into the cart */
for (size_t i = 0; i < RAW_DATA_LENGTH; ++i)
m.memory().base()[i] = assembleByte(data.data[i]);
size_t o = RAW_DATA_LENGTH;
std::array<uint8_t, 4> magic;
std::array<uint8_t, 4> expected = { { ':', 'c', ':', '\0' } };
for (size_t i = 0; i < magic.size(); ++i)
magic[i] = assembleByte(data.data[o++]);
assert(magic == expected);
}
#pragma once
#include "common.h"
#include "vm/machine.h"
namespace retro8
{
namespace io
{
struct PngData
{
const uint32_t* data;
size_t length;
};
class Stegano
{
private:
uint8_t assembleByte(const uint32_t v);
public:
void load(const PngData& data, Machine& dest);
};
}
}
\ No newline at end of file
#include "main_view.h"
#include "io/loader.h"
#include "io/stegano.h"
using namespace ui;
namespace r8 = retro8;
retro8::Machine machine;
......@@ -103,7 +107,7 @@ void GameView::render()
retro8::io::LoaderP8 loader;
if (_path.empty())
_path = "breakout_hero.p8";
_path = "pico-man.p8";
loader.load(_path, machine);
......@@ -116,6 +120,12 @@ void GameView::render()
//machine.sound().init();
//machine.sound().resume();
/*SDL_Surface* surface = IMG_Load("pico-man.p8.png");
r8::io::PngData pngData = { static_cast<const uint32_t*>(surface->pixels), surface->h * surface->w };
assert(surface->format->BytesPerPixel == 4);
r8::io::Stegano stegano;
stegano.load(pngData, machine);*/
/*for (int i = 0; i < 32; ++i)
machine.circ(64, 64, i+1, (r8::color_t)(i % 15 + 1));*/
......
......@@ -11,8 +11,6 @@
#include "vm/machine.h"
#include "vm/lua_bridge.h"
#include "io/loader.h"
namespace ui
{
enum class Scale
......
......@@ -34,6 +34,7 @@ namespace retro8
static constexpr size_t SPRITES_PER_SPRITE_SHEET_ROW = 16;
static constexpr size_t SPRITE_SHEET_PITCH = SPRITE_SHEET_WIDTH / PIXEL_TO_BYTE_RATIO;
static constexpr size_t SPRITE_SHEET_HEIGHT = 128;
static constexpr size_t SPRITE_COUNT = (SPRITE_SHEET_WIDTH * SPRITE_SHEET_HEIGHT) / (SPRITE_WIDTH * SPRITE_HEIGHT);
static constexpr size_t FONT_GLYPHS_COLUMNS = 16;
static constexpr size_t FONT_GLYPHS_ROWS = 10;
......
......@@ -109,7 +109,7 @@ namespace retro8
private:
State _state;
Memory _memory;
sound::APU _sound;
sfx::APU _sound;
gfx::Font _font;
lua::Code _code;
SDL_Surface* _output;
......@@ -159,6 +159,6 @@ namespace retro8
Memory& memory() { return _memory; }
gfx::Font& font() { return _font; }
lua::Code& code() { return _code; }
sound::APU& sound() { return _sound; }
sfx::APU& sound() { return _sound; }
};
}
\ No newline at end of file
......@@ -3,7 +3,7 @@
#include <random>
using namespace retro8;
using namespace retro8::sound;
using namespace retro8::sfx;
inline void DSP::squareWave(uint32_t frequency, int16_t amplitude, int16_t offset, int32_t position, int16_t* dest, size_t samples)
......
......@@ -10,7 +10,7 @@
namespace retro8
{
namespace sound
namespace sfx
{
using volume_t = int32_t;
using pitch_t = int32_t;
......@@ -75,6 +75,17 @@ namespace retro8
uint8_t loopEnd;
};
struct Music
{
uint8_t dummy[4];
};
using sound_t = Sound;
using music_t = Music;
static constexpr size_t SOUND_COUNT = 64;
static constexpr size_t MUSIC_COUNT = 64;
struct SoundState
{
const Sound* sound;
......
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