Commit 84d11697 authored by Jack's avatar Jack
Browse files

implemented flip and width/height for sprite function

parent 18e83c57
......@@ -12,6 +12,8 @@ using namespace retro8;
extern retro8::Machine machine;
using real_t = float;
int pset(lua_State* L)
{
int args = lua_gettop(L);
......@@ -138,9 +140,19 @@ int spr(lua_State* L)
if (lua_gettop(L) > 3)
{
float w = 1.0f, h = 1.0f;
assert(lua_gettop(L) >= 5);
real_t w = lua_tonumber(L, 4);
real_t h = lua_tonumber(L, 5);
bool fx = false, fy = false;
machine.spr(idx, x, y);
if (lua_gettop(L) >= 6)
fx = lua_toboolean(L, 6);
if (lua_gettop(L) >= 7)
fy = lua_toboolean(L, 7);
machine.spr(idx, x, y, w, h, fx, fy);
}
else
/* optimized path */
......@@ -716,6 +728,25 @@ void Code::loadAPI()
}
void Code::printError(const char* where)
{
//for (int i = 1; i < lua_gettop(L); ++i)
{
std::cout << "Error on " << where << std::endl;
//luaL_traceback(L, L, NULL, 1);
//printf("%s\n", lua_tostring(L, -1));
if (lua_isstring(L, -1))
{
const char* message = lua_tostring(L, -1);
std::cout << message << std::endl;
}
}
getchar();
}
void Code::initFromSource(const std::string& code)
{
if (!L)
......@@ -726,22 +757,16 @@ void Code::initFromSource(const std::string& code)
registerFunctions(L);
if (luaL_loadstring(L, code.c_str()))
{
const char* message = lua_tostring(L, -1);
std::cout << "Error: on loadstring: " << message << std::endl;
getchar();
}
printError("luaL_loadString");
int error = lua_pcall(L, 0, 0, 0);
if (error)
{
const char* message = lua_tostring(L, -1);
printError("lua_pcall on init");
std::cout << "Error: script not loaded " << message << std::endl;
getchar();
}
lua_getglobal(L, "_update");
if (lua_isfunction(L, -1))
......@@ -781,12 +806,7 @@ void Code::callVoidFunction(const char* name)
int error = lua_pcall(L, 0, 0, 0);
if (error)
{
const char* message = lua_tostring(L, -1);
std::cout << "Error in " << name << " function: " << message << std::endl;
getchar();
}
printError(name);
}
void Code::update()
......
......@@ -23,6 +23,9 @@ namespace lua
~Code();
void loadAPI();
void printError(const char* where);
void initFromSource(const std::string& code);
void callVoidFunction(const char* name);
......
......@@ -168,7 +168,7 @@ void Machine::circfill(coord_t xc, coord_t yc, amount_t r, color_t color)
}
void Machine::spr(index_t idx, coord_t x, coord_t y)
{
{
const gfx::sprite_t* sprite = _memory.spriteAt(idx);
const gfx::palette_t* palette = _memory.paletteAt(gfx::DRAW_PALETTE_INDEX);
......@@ -176,10 +176,36 @@ void Machine::spr(index_t idx, coord_t x, coord_t y)
for (coord_t tx = 0; tx < gfx::SPRITE_WIDTH; ++tx)
{
color_t color = sprite->get(tx, ty);
if (!palette->transparent(color)) //TODO: manage real transparency through flags
pset(x + tx, y + ty, sprite->get(tx, ty));
if (!palette->transparent(color))
pset(x + tx, y + ty, color);
}
}
void Machine::spr(index_t idx, coord_t bx, coord_t by, float sw, float sh, bool flipX, bool flipY)
{
const gfx::palette_t* palette = _memory.paletteAt(gfx::DRAW_PALETTE_INDEX);
coord_t w = sw * gfx::SPRITE_WIDTH;
coord_t h = sh * gfx::SPRITE_HEIGHT;
/* we bypass spriteAt since we can use directly the address */
const gfx::color_byte_t* base = reinterpret_cast<const gfx::color_byte_t*>(_memory.spriteAt(idx));
for (coord_t y = 0; y < h; ++y)
{
for (coord_t x = 0; x < w; ++x)
{
coord_t fx = flipX ? (w - x - 1) : x;
coord_t fy = flipY ? (h - y - 1) : y;
//TODO: optimize by fetching only once if we need to read next pixel?
const gfx::color_byte_t pair = *(base + y * gfx::SPRITE_SHEET_WIDTH_IN_BYTES + x / gfx::PIXEL_TO_BYTE_RATIO);
const color_t color = pair.get(x);
if (!palette->transparent(color))
pset(bx + fx, by + fy, color);
}
}
}
// TODO: fix strange characters like symbols
......
......@@ -141,6 +141,7 @@ namespace retro8
void map(coord_t cx, coord_t cy, coord_t x, coord_t y, amount_t cw, amount_t ch, sprite_flags_t layer);
void spr(index_t idx, coord_t x, coord_t y);
void spr(index_t idx, coord_t x, coord_t y, float w, float h, bool flipX, bool flipY);
void print(const std::string& string, coord_t x, coord_t y, color_t color);
State& state() { return _state; }
......
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