Skip to content

Commit

Permalink
Merge pull request #1095 from ddelemeny/feature-chromakey-array
Browse files Browse the repository at this point in the history
Enable chromakey array on map and textri
  • Loading branch information
nesbox authored May 22, 2020
2 parents 6a73d37 + 631aa6d commit eca8ccd
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 43 deletions.
71 changes: 66 additions & 5 deletions src/jsapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,20 +403,51 @@ static duk_ret_t duk_map(duk_context* duk)
s32 h = duk_opt_int(duk, 3, TIC_MAP_SCREEN_HEIGHT);
s32 sx = duk_opt_int(duk, 4, 0);
s32 sy = duk_opt_int(duk, 5, 0);
u8 chromakey = duk_opt_int(duk, 6, -1);
s32 scale = duk_opt_int(duk, 7, 1);

static u8 colors[TIC_PALETTE_SIZE];
s32 count = 0;

{
if(!duk_is_null_or_undefined(duk, 6))
{
if(duk_is_array(duk, 6))
{
for(s32 i = 0; i < TIC_PALETTE_SIZE; i++)
{
duk_get_prop_index(duk, 6, i);
if(duk_is_null_or_undefined(duk, -1))
{
duk_pop(duk);
break;
}
else
{
colors[i] = duk_to_int(duk, -1);
count++;
duk_pop(duk);
}
}
}
else
{
colors[0] = duk_to_int(duk, 6);
count = 1;
}
}
}

tic_mem* tic = (tic_mem*)getDukMachine(duk);

if (duk_is_null_or_undefined(duk, 8))
tic_api_map(tic, &tic->ram.map, &tic->ram.tiles, x, y, w, h, sx, sy, chromakey, scale, NULL, NULL);
tic_api_map(tic, &tic->ram.map, &tic->ram.tiles, x, y, w, h, sx, sy, colors, count, scale, NULL, NULL);
else
{
void* remap = duk_get_heapptr(duk, 8);

RemapData data = {duk, remap};

tic_api_map((tic_mem*)getDukMachine(duk), &tic->ram.map, &tic->ram.tiles, x, y, w, h, sx, sy, chromakey, scale, remapCallback, &data);
tic_api_map((tic_mem*)getDukMachine(duk), &tic->ram.map, &tic->ram.tiles, x, y, w, h, sx, sy, colors, count, scale, remapCallback, &data);
}

return 0;
Expand Down Expand Up @@ -678,7 +709,37 @@ static duk_ret_t duk_textri(duk_context* duk)
pt[i] = (float)duk_to_number(duk, i);
tic_mem* tic = (tic_mem*)getDukMachine(duk);
bool use_map = duk_opt_boolean(duk, 12, false);
u8 chroma = duk_opt_int(duk, 13, 0xff);

static u8 colors[TIC_PALETTE_SIZE];
s32 count = 0;
{
if(!duk_is_null_or_undefined(duk, 13))
{
if(duk_is_array(duk, 13))
{
for(s32 i = 0; i < TIC_PALETTE_SIZE; i++)
{
duk_get_prop_index(duk, 13, i);
if(duk_is_null_or_undefined(duk, -1))
{
duk_pop(duk);
break;
}
else
{
colors[i] = duk_to_int(duk, -1);
count++;
duk_pop(duk);
}
}
}
else
{
colors[0] = duk_to_int(duk, 13);
count = 1;
}
}
}

tic_api_textri(tic, pt[0], pt[1], // xy 1
pt[2], pt[3], // xy 2
Expand All @@ -687,7 +748,7 @@ static duk_ret_t duk_textri(duk_context* duk)
pt[8], pt[9], // uv 2
pt[10], pt[11],// uv 3
use_map, // usemap
chroma); // chroma
colors, count); // chroma

return 0;
}
Expand Down
64 changes: 56 additions & 8 deletions src/luaapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,40 @@ static s32 lua_textri(lua_State* lua)
pt[i] = (float)lua_tonumber(lua, i + 1);

tic_mem* tic = (tic_mem*)getLuaMachine(lua);
u8 chroma = 0xff;
static u8 colors[TIC_PALETTE_SIZE];
s32 count = 0;
bool use_map = false;

// check for use map
if (top >= 13)
use_map = lua_toboolean(lua, 13);
// check for chroma
if (top >= 14)
chroma = (u8)getLuaNumber(lua, 14);
if(top >= 14)
{
if(lua_istable(lua, 14))
{
for(s32 i = 1; i <= TIC_PALETTE_SIZE; i++)
{
lua_rawgeti(lua, 14, i);
if(lua_isnumber(lua, -1))
{
colors[i-1] = getLuaNumber(lua, -1);
count++;
lua_pop(lua, 1);
}
else
{
lua_pop(lua, 1);
break;
}
}
}
else
{
colors[0] = getLuaNumber(lua, 14);
count = 1;
}
}

tic_api_textri(tic, pt[0], pt[1], // xy 1
pt[2], pt[3], // xy 2
Expand All @@ -320,7 +345,7 @@ static s32 lua_textri(lua_State* lua)
pt[8], pt[9], // uv 2
pt[10], pt[11], // uv 3
use_map, // use map
chroma); // chroma
colors, count); // chroma
}
else luaL_error(lua, "invalid parameters, textri(x1,y1,x2,y2,x3,y3,u1,v1,u2,v2,u3,v3,[use_map=false],[chroma=off])\n");
return 0;
Expand Down Expand Up @@ -561,8 +586,9 @@ static s32 lua_map(lua_State* lua)
s32 h = TIC_MAP_SCREEN_HEIGHT;
s32 sx = 0;
s32 sy = 0;
u8 chromakey = -1;
s32 scale = 1;
static u8 colors[TIC_PALETTE_SIZE];
s32 count = 0;

s32 top = lua_gettop(lua);

Expand All @@ -583,7 +609,29 @@ static s32 lua_map(lua_State* lua)

if(top >= 7)
{
chromakey = getLuaNumber(lua, 7);
if(lua_istable(lua, 7))
{
for(s32 i = 1; i <= TIC_PALETTE_SIZE; i++)
{
lua_rawgeti(lua, 7, i);
if(lua_isnumber(lua, -1))
{
colors[i-1] = getLuaNumber(lua, -1);
count++;
lua_pop(lua, 1);
}
else
{
lua_pop(lua, 1);
break;
}
}
}
else
{
colors[0] = getLuaNumber(lua, 7);
count = 1;
}

if(top >= 8)
{
Expand All @@ -599,7 +647,7 @@ static s32 lua_map(lua_State* lua)

tic_mem* tic = (tic_mem*)getLuaMachine(lua);

tic_api_map(tic, &tic->ram.map, &tic->ram.tiles, x, y, w, h, sx, sy, chromakey, scale, remapCallback, &data);
tic_api_map(tic, &tic->ram.map, &tic->ram.tiles, x, y, w, h, sx, sy, colors, count, scale, remapCallback, &data);

luaL_unref(lua, LUA_REGISTRYINDEX, data.reg);

Expand All @@ -614,7 +662,7 @@ static s32 lua_map(lua_State* lua)

tic_mem* tic = (tic_mem*)getLuaMachine(lua);

tic_api_map((tic_mem*)getLuaMachine(lua), &tic->ram.map, &tic->ram.tiles, x, y, w, h, sx, sy, chromakey, scale, NULL, NULL);
tic_api_map((tic_mem*)getLuaMachine(lua), &tic->ram.map, &tic->ram.tiles, x, y, w, h, sx, sy, colors, count, scale, NULL, NULL);

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ static void drawMapReg(Map* map)

tic_mem* tic = map->tic;
tic_api_map(tic, map->src, getBankTiles(), map->scroll.x / TIC_SPRITESIZE, map->scroll.y / TIC_SPRITESIZE,
TIC_MAP_SCREEN_WIDTH + 1, TIC_MAP_SCREEN_HEIGHT + 1, -scrollX, -scrollY, -1, 1, NULL, NULL);
TIC_MAP_SCREEN_WIDTH + 1, TIC_MAP_SCREEN_HEIGHT + 1, -scrollX, -scrollY, 0, 0, 1, NULL, NULL);

if(map->canvas.grid || map->scroll.active)
drawGrid(map);
Expand Down
63 changes: 55 additions & 8 deletions src/squirrelapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ static SQInteger squirrel_textri(HSQUIRRELVM vm)
}

tic_mem* tic = (tic_mem*)getSquirrelMachine(vm);
u8 chroma = 0xff;
static u8 colors[TIC_PALETTE_SIZE];
s32 count = 0;
bool use_map = false;

// check for use map
Expand All @@ -370,8 +371,30 @@ static SQInteger squirrel_textri(HSQUIRRELVM vm)
use_map = (b != SQFalse);
}
// check for chroma
if (top >= 15)
chroma = (u8)getSquirrelNumber(vm, 15);
if(OT_ARRAY == sq_gettype(vm, 15))
{
for(s32 i = 0; i < TIC_PALETTE_SIZE; i++)
{
sq_pushinteger(vm, (SQInteger)i);
sq_rawget(vm, 15);
if(sq_gettype(vm, -1) & (OT_FLOAT|OT_INTEGER))
{
colors[i-1] = getSquirrelNumber(vm, -1);
count++;
sq_poptop(vm);
}
else
{
sq_poptop(vm);
break;
}
}
}
else
{
colors[0] = getSquirrelNumber(vm, 15);
count = 1;
}

tic_api_textri(tic, pt[0], pt[1], // xy 1
pt[2], pt[3], // xy 2
Expand All @@ -380,7 +403,7 @@ static SQInteger squirrel_textri(HSQUIRRELVM vm)
pt[8], pt[9], // uv 2
pt[10], pt[11], // uv 3
use_map, // use map
chroma); // chroma
colors, count); // chroma
}
else return sq_throwerror(vm, "invalid parameters, textri(x1,y1,x2,y2,x3,y3,u1,v1,u2,v2,u3,v3,[use_map=false],[chroma=off])\n");
return 0;
Expand Down Expand Up @@ -644,8 +667,9 @@ static SQInteger squirrel_map(HSQUIRRELVM vm)
s32 h = TIC_MAP_SCREEN_HEIGHT;
s32 sx = 0;
s32 sy = 0;
u8 chromakey = -1;
s32 scale = 1;
static u8 colors[TIC_PALETTE_SIZE];
s32 count = 0;

SQInteger top = sq_gettop(vm);

Expand All @@ -666,7 +690,30 @@ static SQInteger squirrel_map(HSQUIRRELVM vm)

if(top >= 8)
{
chromakey = getSquirrelNumber(vm, 8);
if(OT_ARRAY == sq_gettype(vm, 8))
{
for(s32 i = 0; i < TIC_PALETTE_SIZE; i++)
{
sq_pushinteger(vm, (SQInteger)i);
sq_rawget(vm, 8);
if(sq_gettype(vm, -1) & (OT_FLOAT|OT_INTEGER))
{
colors[i-1] = getSquirrelNumber(vm, -1);
count++;
sq_poptop(vm);
}
else
{
sq_poptop(vm);
break;
}
}
}
else
{
colors[0] = getSquirrelNumber(vm, 8);
count = 1;
}

if(top >= 9)
{
Expand All @@ -684,7 +731,7 @@ static SQInteger squirrel_map(HSQUIRRELVM vm)

tic_mem* tic = (tic_mem*)getSquirrelMachine(vm);

tic_api_map(tic, &tic->ram.map, &tic->ram.tiles, x, y, w, h, sx, sy, chromakey, scale, remapCallback, &data);
tic_api_map(tic, &tic->ram.map, &tic->ram.tiles, x, y, w, h, sx, sy, colors, count, scale, remapCallback, &data);

//luaL_unref(lua, LUA_REGISTRYINDEX, data.reg);
sq_release(vm, &data.reg);
Expand All @@ -700,7 +747,7 @@ static SQInteger squirrel_map(HSQUIRRELVM vm)

tic_mem* tic = (tic_mem*)getSquirrelMachine(vm);

tic_api_map((tic_mem*)getSquirrelMachine(vm), &tic->ram.map, &tic->ram.tiles, x, y, w, h, sx, sy, chromakey, scale, NULL, NULL);
tic_api_map((tic_mem*)getSquirrelMachine(vm), &tic->ram.map, &tic->ram.tiles, x, y, w, h, sx, sy, colors, count, scale, NULL, NULL);

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/start.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void reset(Start* start)

for(s32 i = 0; i < sizeof(tic_tile); i++) tile[i] = val;

tic_api_map(start->tic, &start->tic->ram.map, &start->tic->ram.tiles, 0, 0, TIC_MAP_SCREEN_WIDTH, TIC_MAP_SCREEN_HEIGHT + (TIC80_HEIGHT % TIC_SPRITESIZE ? 1 : 0), 0, 0, -1, 1, NULL, NULL);
tic_api_map(start->tic, &start->tic->ram.map, &start->tic->ram.tiles, 0, 0, TIC_MAP_SCREEN_WIDTH, TIC_MAP_SCREEN_HEIGHT + (TIC80_HEIGHT % TIC_SPRITESIZE ? 1 : 0), 0, 0, 0, 0, 1, NULL, NULL);
}

static void drawHeader(Start* start)
Expand Down Expand Up @@ -104,4 +104,4 @@ void initStart(Start* start, tic_mem* tic)
void freeStart(Start* start)
{
free(start);
}
}
6 changes: 3 additions & 3 deletions src/system/sdlgpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ static void initTouchKeyboard()

tic_api_cls(tic, 0);
tic_api_map(tic, &platform.studio->config()->cart->bank0.map,
&platform.studio->config()->cart->bank0.tiles, 8, 0, Cols, Rows, 0, 0, -1, 1, NULL, NULL);
&platform.studio->config()->cart->bank0.tiles, 8, 0, Cols, Rows, 0, 0, 0, 0, 1, NULL, NULL);

drawKeyboardLabels(0);

Expand All @@ -289,7 +289,7 @@ static void initTouchKeyboard()
memcpy(tic->ram.vram.palette.data, platform.studio->config()->cart->bank0.palette.data, sizeof(tic_palette));

tic_api_map(tic, &platform.studio->config()->cart->bank0.map,
&platform.studio->config()->cart->bank0.tiles, TIC_MAP_SCREEN_WIDTH+8, 0, Cols, Rows, 0, 0, -1, 1, NULL, NULL);
&platform.studio->config()->cart->bank0.tiles, TIC_MAP_SCREEN_WIDTH+8, 0, Cols, Rows, 0, 0, 0, 0, 1, NULL, NULL);

drawKeyboardLabels(2);

Expand All @@ -314,7 +314,7 @@ static void initTouchGamepad()
if(!platform.gamepad.pixels)
{
tic_api_map(platform.studio->tic, &platform.studio->config()->cart->bank0.map,
&platform.studio->config()->cart->bank0.tiles, 0, 0, TIC_MAP_SCREEN_WIDTH, TIC_MAP_SCREEN_HEIGHT, 0, 0, -1, 1, NULL, NULL);
&platform.studio->config()->cart->bank0.tiles, 0, 0, TIC_MAP_SCREEN_WIDTH, TIC_MAP_SCREEN_HEIGHT, 0, 0, 0, 0, 1, NULL, NULL);

platform.gamepad.pixels = SDL_malloc(TEXTURE_SIZE * TEXTURE_SIZE * sizeof(u32));

Expand Down
Loading

0 comments on commit eca8ccd

Please sign in to comment.