Ive talked about creating something that can create images and manipulate on a pixel level, with getPixel and setPixel support.
The following is a a bit of a hack… but maybe it will be useful for someone.
Here is a GFXClass … save it as GFXClass.lua
module( ..., package.seeall )
--====================================================================--
-- GFXCLASS
--[[ Version: 1.0
- Made by JTullin 2012
- This class is free to use, feel free to change.
--]]
local picView = display.newGroup()
local bitMap = {}
local \_wid = 100
local \_hei =100
local \_backColor = {255,255,255}
local \_maxWid = 1023
local \_maxHei = 767
local \_group
function createImage(caller,w,h,r,g,b)
if w and h then
if w \>=0 and w \<= 1024 and h \>=0 and h \<= \_maxHei then
\_wid = w;
\_hei = h;
\_backColor.r =r
\_backColor.g =g
\_backColor.b =b
else
return false
end
else
return false
end
end
function setPixel( caller,x,y, r,g,b)
if x\>= 0 and x\<=\_maxWid and y\>=0 and y\<=\_maxHei then
--range is OK
if not(bitMap[y\*\_wid + x]) then bitMap[y\*\_wid + x] ={r=0,g=0,b=0} end
local pixel
pixel = bitMap[y\*\_wid + x]
pixel.r = r
pixel.g = g
pixel.b = b
return true
else
return false
end
end
function getPixel( caller,x,y)
if x\>= 0 and x\<=\_wid and y\>=0 and y\<=\_hei then
--range is OK
if bitMap[y\*\_wid + x] then
--print("values",bitMap[y\*\_wid + x].r, bitMap[y\*\_wid + x].g, bitMap[y\*\_wid + x].b)
return bitMap[y\*\_wid + x].r, bitMap[y\*\_wid + x].g, bitMap[y\*\_wid + x].b
else
--print("returning background")
return \_backColor.r, \_backColor.g, \_backColor.b;
end
return bitMap[y\*\_wid + x].r, bitMap[y\*\_wid + x].g,bitMap[y\*\_wid + x].b;
else
--print("returning out of range")
return \_backColor.r,\_backColor.g,\_backColor.b;
end
end
function fillRect( caller,x,y, w,h,r,g,b)
if x\>= 0 and x\<=\_maxWid and y\>=0 and y\<=\_maxHei then
local xx,yy
local extentX,extentY
extentX = x + w
if extentX \>= \_wid then extentX = \_wid-1 end
extentY = y + h
if extentY \>= \_hei then extentY = \_hei-1 end
for xx = x, extentX,1 do
for yy = y,extentY,1 do
setPixel( caller,xx,yy, r,g,b)
end
end
end
end
function drawLine(caller,x0,y0,x1,y1,r,g,b )
local dx = math.abs(x1-x0)
local dy = math.abs(y1-y0)
if x0 \< x1 then
sx = 1
else
sx = -1
end
if y0 \< y1 then
sy = 1
else
sy = -1
end
local err = dx-dy
local z = 0
while z == 0 do
setPixel( caller,math.floor(x0),y0, r,g,b)
if ((x0 == x1) and (y0 == y1)) then
z = 10000
end
e2 = 2\*err
if e2 \> -dy then
err = err - dy
x0 = x0 + sx
end
if e2 \< dx then
err = err + dx
y0 = y0 + sy
end
end
end
function renderdisplayGroup( )
--returns a displayGroup
local xx,yy
local extentX,extentY
local aRect
local pixel
\_group = display.newGroup()
for xx = 0,\_wid,1 do
for yy = 0,\_hei,1 do
aRect = display.newRect(\_group,xx,yy,1,1)
if bitMap[yy\*\_wid + xx] then
pixel = bitMap[yy\*\_wid + xx]
aRect:setFillColor( pixel.r, pixel.g, pixel.b)
else
aRect:setFillColor( \_backColor.r,\_backColor.g,\_backColor.b)
end
end
end
return \_group
end
[import]uid: 108660 topic_id: 26731 reply_id: 326731[/import]