* Add lua stuff to write to spare page

* Use it in Tiler to put the tilemap in the spare.


git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1834 416bcca6-2ee7-4201-b75f-2eb2f807beb1
This commit is contained in:
Adrien Destugues 2011-09-25 20:37:45 +00:00
parent 6e30a87f4c
commit 22d3f19df3
4 changed files with 86 additions and 16 deletions

View File

@ -14,29 +14,28 @@
-- Grid size -- Grid size
-- TODO : get it from GrafX2 -- TODO : get it from GrafX2
xgrid = 8; xgrid = 17;
ygrid = 16; ygrid = 17;
-- picture size -- picture size
w, h = getsparepicturesize(); w, h = getpicturesize();
-- We may need less if there are duplicates -- We may need less if there are duplicates
setpicturesize(xgrid, w*h/xgrid); setsparepicturesize(xgrid, w*h/xgrid);
clearpicture(255);
tileid = 0; tileid = 0;
-- blit part of the spare to picture -- blit part of the spare to picture
function blitsparetopicture(srcx, srcy, dstx, dsty, width, height) function blitpicturetospare(srcx, srcy, dstx, dsty, width, height)
local x,y; local x,y;
for y = 0, height - 1, 1 do for y = 0, height - 1, 1 do
for x = 0, width - 1, 1 do for x = 0, width - 1, 1 do
putpicturepixel(dstx+x, dsty+y, getsparepicturepixel(srcx + x, srcy + y)); putsparepicturepixel(dstx+x, dsty+y, getpicturepixel(srcx + x, srcy + y));
end end
end end
end end
function comparesparetopicture(srcx, srcy, dstx, dsty, width, height) function comparesparewithpicture(srcx, srcy, dstx, dsty, width, height)
local x,y,color local x,y,color
for y = 0, height - 1, 1 do for y = 0, height - 1, 1 do
for x = 0, width - 1, 1 do for x = 0, width - 1, 1 do
@ -58,7 +57,7 @@ function checksum(srcx, srcy, width, height)
sum = 0; sum = 0;
for y = 0, height - 1, 1 do for y = 0, height - 1, 1 do
for x = 0, width - 1, 1 do for x = 0, width - 1, 1 do
sum = sum + getsparepicturepixel(srcx+x, srcy+y); sum = sum + getpicturepixel(srcx+x, srcy+y);
end end
end end
@ -78,7 +77,7 @@ for y = 0, h-1, ygrid do
found = false; found = false;
for id in pairs(tilemap[csum]) do for id in pairs(tilemap[csum]) do
-- is it a match ? -- is it a match ?
if comparesparetopicture(x,y,0,id*ygrid, xgrid, ygrid) then if comparesparewithpicture(x,y,0,id*ygrid, xgrid, ygrid) then
-- found it ! -- found it !
tilemap[csum][id] = tilemap[csum][id] + 1; tilemap[csum][id] = tilemap[csum][id] + 1;
found = true; found = true;
@ -88,7 +87,7 @@ for y = 0, h-1, ygrid do
-- Add tile anyway if needed -- Add tile anyway if needed
if not found then if not found then
desty = tileid * ygrid; desty = tileid * ygrid;
blitsparetopicture(x, y, 0, desty, xgrid, ygrid); blitpicturetospare(x, y, 0, desty, xgrid, ygrid);
-- add it to the tilemap -- add it to the tilemap
tilemap[csum][tileid] = 1; tilemap[csum][tileid] = 1;
@ -98,7 +97,7 @@ for y = 0, h-1, ygrid do
else else
-- Copy to spare -- Copy to spare
desty = tileid * ygrid; desty = tileid * ygrid;
blitsparetopicture(x, y, 0, desty, xgrid, ygrid); blitpicturetospare(x, y, 0, desty, xgrid, ygrid);
-- add it to the tilemap -- add it to the tilemap
tilemap[csum] = {} tilemap[csum] = {}
@ -109,6 +108,5 @@ for y = 0, h-1, ygrid do
end end
end end
finalizepicture(); -- resize from what we just did setsparepicturesize(xgrid, (tileid-1)*ygrid)
setpicturesize(xgrid, (tileid-1)*ygrid) --updatescreen();
updatescreen();

View File

@ -2,7 +2,7 @@
*/ */
/* Grafx2 - The Ultimate 256-color bitmap paint program /* Grafx2 - The Ultimate 256-color bitmap paint program
Copyright 2009 Adrien Destugues Copyright 2009-2011 Adrien Destugues
Grafx2 is free software; you can redistribute it and/or Grafx2 is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License modify it under the terms of the GNU General Public License
@ -308,6 +308,33 @@ int L_SetPictureSize(lua_State* L)
return 0; return 0;
} }
int L_SetSparePictureSize(lua_State* L)
{
int w;
int h;
int nb_args=lua_gettop(L);
int i;
LUA_ARG_LIMIT (2, "setsparepicturesize");
LUA_ARG_NUMBER(1, "setsparepicturesize", w, 1, 9999);
LUA_ARG_NUMBER(2, "setsparepicturesize", h, 1, 9999);
Backup_and_resize_the_spare(w, h);
// part of Resize_image() : the pixel copy part.
for (i=0; i<Spare_backups->Pages->Nb_layers; i++)
{
Copy_part_of_image_to_another(
Spare_backups->Pages->Next->Image[i],0,0,Min(Spare_backups->Pages->Next->Width,Spare_image_width),
Min(Spare_backups->Pages->Next->Height,Spare_image_height),Spare_backups->Pages->Next->Width,
Spare_backups->Pages->Image[i],0,0,Spare_image_width);
}
Redraw_spare_image();
return 0;
}
int L_GetPictureSize(lua_State* L) int L_GetPictureSize(lua_State* L)
{ {
lua_pushinteger(L, Main_image_width); lua_pushinteger(L, Main_image_width);
@ -355,6 +382,29 @@ int L_PutPicturePixel(lua_State* L)
} }
int L_PutSparePicturePixel(lua_State* L)
{
int x;
int y;
int c;
int nb_args=lua_gettop(L);
LUA_ARG_LIMIT (3, "putsparepicturepixel");
LUA_ARG_NUMBER(1, "putsparepicturepixel", x, INT_MIN, INT_MAX);
LUA_ARG_NUMBER(2, "putsparepicturepixel", y, INT_MIN, INT_MAX);
LUA_ARG_NUMBER(3, "putsparepicturepixel", c, INT_MIN, INT_MAX);
// Bound check
if (x<0 || y<0 || x>=Spare_image_width || y>=Spare_image_height)
{
// Silently ignored
return 0;
}
Pixel_in_spare(x, y, c);
return 0; // no values returned for lua
}
int L_DrawLine(lua_State* L) int L_DrawLine(lua_State* L)
{ {
int x1, y1, x2, y2, c; int x1, y1, x2, y2, c;
@ -1292,6 +1342,19 @@ int L_StatusMessage(lua_State* L)
} }
int L_SelectLayer(lua_State* L)
{
int nb_args=lua_gettop(L);
LUA_ARG_LIMIT (1, "selectlayer");
LUA_ARG_NUMBER(1, "selectlayer", Main_current_layer, 0, Main_backups->Pages->Nb_layers - 1);
// TODO create layer if it doesn't exist yet ?
return 0;
}
int L_FinalizePicture(lua_State* L) int L_FinalizePicture(lua_State* L)
{ {
int nb_args=lua_gettop(L); int nb_args=lua_gettop(L);
@ -1529,6 +1592,7 @@ void Run_script(const char *script_subdirectory, const char *script_filename)
// Drawing // Drawing
lua_register(L,"putbrushpixel",L_PutBrushPixel); lua_register(L,"putbrushpixel",L_PutBrushPixel);
lua_register(L,"putpicturepixel",L_PutPicturePixel); lua_register(L,"putpicturepixel",L_PutPicturePixel);
lua_register(L,"putsparepicturepixel",L_PutSparePicturePixel);
lua_register(L, "drawline",L_DrawLine); lua_register(L, "drawline",L_DrawLine);
lua_register(L, "drawfilledrect",L_DrawFilledRect); lua_register(L, "drawfilledrect",L_DrawFilledRect);
lua_register(L, "drawcircle",L_DrawCircle); lua_register(L, "drawcircle",L_DrawCircle);
@ -1547,6 +1611,7 @@ void Run_script(const char *script_subdirectory, const char *script_filename)
// Sizes // Sizes
lua_register(L,"setbrushsize",L_SetBrushSize); lua_register(L,"setbrushsize",L_SetBrushSize);
lua_register(L,"setpicturesize",L_SetPictureSize); lua_register(L,"setpicturesize",L_SetPictureSize);
lua_register(L,"setsparepicturesize",L_SetSparePictureSize);
lua_register(L,"getbrushsize",L_GetBrushSize); lua_register(L,"getbrushsize",L_GetBrushSize);
lua_register(L,"getpicturesize",L_GetPictureSize); lua_register(L,"getpicturesize",L_GetPictureSize);
@ -1582,6 +1647,7 @@ void Run_script(const char *script_subdirectory, const char *script_filename)
lua_register(L,"updatescreen",L_UpdateScreen); lua_register(L,"updatescreen",L_UpdateScreen);
lua_register(L,"finalizepicture",L_FinalizePicture); lua_register(L,"finalizepicture",L_FinalizePicture);
lua_register(L,"getfilename",L_GetFileName); lua_register(L,"getfilename",L_GetFileName);
lua_register(L,"selectlayer",L_SelectLayer);
// Load all standard libraries // Load all standard libraries
luaL_openlibs(L); luaL_openlibs(L);
@ -1602,6 +1668,7 @@ void Run_script(const char *script_subdirectory, const char *script_filename)
// The backup also allows the script to read from it to make something // The backup also allows the script to read from it to make something
// like a feedback off effect (convolution matrix comes to mind). // like a feedback off effect (convolution matrix comes to mind).
Backup(); Backup();
Backup_the_spare(-1);
Palette_has_changed=0; Palette_has_changed=0;
Brush_was_altered=0; Brush_was_altered=0;

View File

@ -3067,6 +3067,10 @@ void Pixel_in_current_screen (word x,word y,byte color,int with_preview)
#endif #endif
} }
void Pixel_in_spare(word x,word y, byte color)
{
*((y)*Spare_image_width+(x)+Spare_backups->Pages->Image[Spare_current_layer])=color;
}
void Pixel_in_current_layer(word x,word y, byte color) void Pixel_in_current_layer(word x,word y, byte color)
{ {

View File

@ -121,6 +121,7 @@ void Update_part_of_screen(short x, short y, short width, short height);
void Redraw_grid(short x, short y, unsigned short w, unsigned short h); void Redraw_grid(short x, short y, unsigned short w, unsigned short h);
void Pixel_in_current_screen (word x,word y,byte color,int with_preview); void Pixel_in_current_screen (word x,word y,byte color,int with_preview);
void Pixel_in_spare(word x,word y, byte color);
void Pixel_in_current_layer(word x,word y, byte color); void Pixel_in_current_layer(word x,word y, byte color);
byte Read_pixel_from_current_screen (word x,word y); byte Read_pixel_from_current_screen (word x,word y);
byte Read_pixel_from_current_layer(word x,word y); byte Read_pixel_from_current_layer(word x,word y);