diff --git a/share/grafx2/scripts/samples_2.4/picture/Tiler.lua b/share/grafx2/scripts/samples_2.4/picture/Tiler.lua index ae0ecd90..4119809c 100644 --- a/share/grafx2/scripts/samples_2.4/picture/Tiler.lua +++ b/share/grafx2/scripts/samples_2.4/picture/Tiler.lua @@ -14,29 +14,28 @@ -- Grid size -- TODO : get it from GrafX2 -xgrid = 8; -ygrid = 16; +xgrid = 17; +ygrid = 17; -- picture size -w, h = getsparepicturesize(); +w, h = getpicturesize(); -- We may need less if there are duplicates -setpicturesize(xgrid, w*h/xgrid); -clearpicture(255); +setsparepicturesize(xgrid, w*h/xgrid); tileid = 0; -- 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; for y = 0, height - 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 -function comparesparetopicture(srcx, srcy, dstx, dsty, width, height) +function comparesparewithpicture(srcx, srcy, dstx, dsty, width, height) local x,y,color for y = 0, height - 1, 1 do for x = 0, width - 1, 1 do @@ -58,7 +57,7 @@ function checksum(srcx, srcy, width, height) sum = 0; for y = 0, height - 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 @@ -78,7 +77,7 @@ for y = 0, h-1, ygrid do found = false; for id in pairs(tilemap[csum]) do -- 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 ! tilemap[csum][id] = tilemap[csum][id] + 1; found = true; @@ -88,7 +87,7 @@ for y = 0, h-1, ygrid do -- Add tile anyway if needed if not found then desty = tileid * ygrid; - blitsparetopicture(x, y, 0, desty, xgrid, ygrid); + blitpicturetospare(x, y, 0, desty, xgrid, ygrid); -- add it to the tilemap tilemap[csum][tileid] = 1; @@ -98,7 +97,7 @@ for y = 0, h-1, ygrid do else -- Copy to spare desty = tileid * ygrid; - blitsparetopicture(x, y, 0, desty, xgrid, ygrid); + blitpicturetospare(x, y, 0, desty, xgrid, ygrid); -- add it to the tilemap tilemap[csum] = {} @@ -109,6 +108,5 @@ for y = 0, h-1, ygrid do end end -finalizepicture(); -- resize from what we just did -setpicturesize(xgrid, (tileid-1)*ygrid) -updatescreen(); +setsparepicturesize(xgrid, (tileid-1)*ygrid) +--updatescreen(); diff --git a/src/factory.c b/src/factory.c index 1e509598..947ea03b 100644 --- a/src/factory.c +++ b/src/factory.c @@ -2,7 +2,7 @@ */ /* 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 modify it under the terms of the GNU General Public License @@ -308,6 +308,33 @@ int L_SetPictureSize(lua_State* L) 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; iPages->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) { 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 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 nb_args=lua_gettop(L); @@ -1529,6 +1592,7 @@ void Run_script(const char *script_subdirectory, const char *script_filename) // Drawing lua_register(L,"putbrushpixel",L_PutBrushPixel); lua_register(L,"putpicturepixel",L_PutPicturePixel); + lua_register(L,"putsparepicturepixel",L_PutSparePicturePixel); lua_register(L, "drawline",L_DrawLine); lua_register(L, "drawfilledrect",L_DrawFilledRect); lua_register(L, "drawcircle",L_DrawCircle); @@ -1547,6 +1611,7 @@ void Run_script(const char *script_subdirectory, const char *script_filename) // Sizes lua_register(L,"setbrushsize",L_SetBrushSize); lua_register(L,"setpicturesize",L_SetPictureSize); + lua_register(L,"setsparepicturesize",L_SetSparePictureSize); lua_register(L,"getbrushsize",L_GetBrushSize); 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,"finalizepicture",L_FinalizePicture); lua_register(L,"getfilename",L_GetFileName); + lua_register(L,"selectlayer",L_SelectLayer); // Load all standard libraries 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 // like a feedback off effect (convolution matrix comes to mind). Backup(); + Backup_the_spare(-1); Palette_has_changed=0; Brush_was_altered=0; diff --git a/src/graph.c b/src/graph.c index b31774e7..b95429a2 100644 --- a/src/graph.c +++ b/src/graph.c @@ -3067,6 +3067,10 @@ void Pixel_in_current_screen (word x,word y,byte color,int with_preview) #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) { diff --git a/src/graph.h b/src/graph.h index d27ec686..60d8b0aa 100644 --- a/src/graph.h +++ b/src/graph.h @@ -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 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); byte Read_pixel_from_current_screen (word x,word y); byte Read_pixel_from_current_layer(word x,word y);