Page 2 of 3
Re: Paint program
Posted: Wed Oct 23, 2013 2:43 pm
by micha
Sure.
The snippet creates a table of tables. Recall that you need to create a table before you can fill it:
This causes an error:
This will work:
Now an 1D-array is a table with integer number as indices:
Code: Select all
a[1] = 1
a[2] = 5
a[3] = 7
...
a[100] = -1
To create an 1D-array with zero in each entry you can do this with a loop:
Code: Select all
a = {}
for i = 1,100 do
a[i] = 0
end
Next, a 2D array is a 1D array, whose entries are 1D arrays.
So we next the above array-creation-code into itsself (and replace one of the loop variables by another one to avoid conflicts):
Code: Select all
a = {}
for i=1,100 do
a[i] = {}
for j = 1,50 do
a[i][j] = 0
end
end
This creates a 2D array of size 100x50. To access the entry in column 12 and row 45 you can use
Setting the value works the same way.
This, by the way, is also the method to store tile-based maps.
Re: Paint program
Posted: Wed Oct 23, 2013 5:14 pm
by tavuntu
Hi, that piece of code creates
n empty rows, where
n=width and for each row creates
m pixels, where
m=height, something like this (with width=3 and height=3):
Code: Select all
-- First iteration (i=1,j=1):
{0}
-- 2nd iteration (i=1,j=2):
{0,0}
-- 3rd iteration (i=1,j=3):
{0,0,0}
--...
-- 6th iteration (i=2,j=3):
{0,0,0},
{0,0,0}
--.........
-- last iteration (i=3,j=3):
{0,0,0},
{0,0,0},
{0,0,0}
-- at the end the pixel table =
{{0,0,0},{0,0,0},{0,0,0}}
I hope I've helped, I could be wrong though.
Re: Paint program
Posted: Wed Oct 23, 2013 5:17 pm
by tavuntu
oops, a little later.
Re: Paint program
Posted: Thu Oct 24, 2013 12:27 am
by iPoisonxL
micha wrote:
This, by the way, is also the method to store tile-based maps.
Yeah, I've tried working with tilemaps before, I never really grasped the concept correctly. Thanks for explaining this to me! Now, to color the pixels white, I'd simply loop through all the array entries and check if their values are, let's say, the string "white"? And if yes, then color that pixel white?
Code: Select all
for i=1,#pixels do
for j=1,#pixels[i] do
if(pixels[i][j]=="white")then
love.graphics.point(...) --this is the part where I can't figure out. I'm thinking pixels[i][j], but that doesn't work, since it's not a coordinate.
end
end
end
EDIT:
tavuntu wrote:oops, a little later.
Thanks for helping nonetheless!
DOUBLE EDIT:
I tried something out, but it has huge performance issues in the love.draw, and it's not even drawing.
Code: Select all
g = love.graphics
m = love.mouse
function love.load()
pixels = {}
for i=1,600 do
pixels[i]={}
for j=1,800 do
pixels[i][j]={}
end
end
g.setColor(255,255,255,255)
g.setCaption("p-a-int")
width = g.getWidth()
height = g.getHeight()
end
function love.update()
pos = {m.getX(), m.getY()}
if(m.isDown("r"))then
pixels = {}
end
if(m.isDown("l"))then
for i,v in pairs(pixels) do
for j,z in pairs(v) do
z = {1, pos}
end
end
end
end
function love.draw()
for i,v in pairs(pixels) do
for j,z in pairs(v) do
if(z[1]==1)then
g.point(z[2][1], z[2][2])
end
end
end
g.print(love.timer.getFPS(), 10, 10)
--g.print("Amount of pixels being drawn: "..table.length(pixels), 10, 30) to be remade later
end
Re: Paint program
Posted: Thu Oct 24, 2013 1:29 am
by Ref
No surprise that you aren't drawing anything.
Try replacing:
Code: Select all
pixels = {}
for i=1,600 do
pixels[i]={}
for j=1,800 do
pixels[i][j]={}
end
end
with:
Code: Select all
pixels = {}
for y = 1, 600 do
pixels[y] = {}
for x = 1, 800 do
pixels[y][x] = 0
end
end
and
Code: Select all
if(m.isDown("l"))then
for i,v in pairs(pixels) do
for j,z in pairs(v) do
z = {1, pos}
end
end
end
with
Code: Select all
if m.isDown('l') then pixels[m.getY()][m.getX()] = 1 end
and
Code: Select all
for i,v in pairs(pixels) do
for j,z in pairs(v) do
if(z[1]==1)then
g.point(z[2][1], z[2][2])
end
end
with
Code: Select all
for y = 1, 600 do
for x = 1, 800 do
if pixels[y][x] = 1 then g.point(x,y) end
end
end
I think that should work - maybe.
Best
Re: Paint program
Posted: Thu Oct 24, 2013 4:30 pm
by iPoisonxL
It works, but it has huge performance issues... I'm trying to do this without any imageData or canvases, is there any way to boost up performance? I understand that it goes through a 2D array of 480,000 entries every frame to check whether it's a 0 or a 1, but there must be a way...
Re: Paint program
Posted: Thu Oct 24, 2013 5:16 pm
by DaedalusYoung
The way is to use at least imageData. Why don't you want to use that?
Re: Paint program
Posted: Thu Oct 24, 2013 5:26 pm
by iPoisonxL
I need someone to at least explain to me what imageData is.
Re: Paint program
Posted: Thu Oct 24, 2013 7:25 pm
by DaedalusYoung
I think you can view ImageData as a sort of virtual image file. It basically works the same as a 2D array, but much more optimised. You can't draw ImageData directly, just as you can't draw an image file without loading it first, so you'll need to create a new image using the ImageData as source every time it changes. Then simply draw that image as you would with any other image.
Re: Paint program
Posted: Thu Oct 24, 2013 8:28 pm
by tavuntu
Your performace will always be by soil if you're using ImageData and manipulating it in real time (at least you use a low resolution or have a monster machine), even worse if you make a real image an draw it each time you draw a pixel on screen.
Look this, I'm using a 2D array here to draw in real time over the screen, I use ImageData only when loading/saving the image in/from a real file:
https://www.dropbox.com/s/xb4dbzjqtartt ... ditor.love