Paint program

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Paint program

Post 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:

Code: Select all

a[1] = 5
This will work:

Code: Select all

a = {}
a[1] = 5
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

Code: Select all

print(a[12][45])
Setting the value works the same way.

This, by the way, is also the method to store tile-based maps.
User avatar
tavuntu
Citizen
Posts: 65
Joined: Mon Dec 24, 2012 6:56 am
Contact:

Re: Paint program

Post 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.
Last edited by tavuntu on Wed Oct 23, 2013 5:18 pm, edited 1 time in total.
User avatar
tavuntu
Citizen
Posts: 65
Joined: Mon Dec 24, 2012 6:56 am
Contact:

Re: Paint program

Post by tavuntu »

oops, a little later.
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Paint program

Post 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

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Paint program

Post 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
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Paint program

Post 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...

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Paint program

Post by DaedalusYoung »

The way is to use at least imageData. Why don't you want to use that?
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Paint program

Post by iPoisonxL »

I need someone to at least explain to me what imageData is.

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Paint program

Post 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.
User avatar
tavuntu
Citizen
Posts: 65
Joined: Mon Dec 24, 2012 6:56 am
Contact:

Re: Paint program

Post 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
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests