An issue with a canvas

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

An issue with a canvas

Post by tavuntu »

Hi people, I have this code and I want to draw a canvas with a grid full of gray and white squares (to appreciate the transparency of colors)
but I only get one "row" :( What am I doing wrong?

Code: Select all

function love.load()
	gr=love.graphics
	kb=love.keyboard
	ms=love.mouse
	ANCHO_BARRA_HERR=100
	ancho_p=gr.getWidth()-- Ancho de la pantalla.
	alto_p=gr.getHeight()-- Alto de la pantalla.
	lienzo={}-- Lienzo y sus dimensiones.
	-- Dimensiones por default (pixeles).
	lienzo.ancho=16
	lienzo.alto=16
	lienzo.tam_pixel=20-- Tamano de pixel (20 pixeles reales).
	--Lienzo de fondo:
	fondo=gr.newCanvas(lienzo.ancho*lienzo.tam_pixel,lienzo.alto*lienzo.tam_pixel)
	gr.setCanvas(fondo)
	local tcf=8-- Cuadrito de fondo.
	local x,y=0,0
	for i=1,lienzo.alto*lienzo.tam_pixel,tcf do
		y=y+1 -- THIS LINE SEEMS NOT TO WORK.
		for j=1,lienzo.ancho*lienzo.tam_pixel,tcf do
			x=x+1
			if (x+y)%2==0 then
				gr.setColor(255,255,255)
			else
				gr.setColor(127,127,127)
			end
			gr.rectangle("fill",(x-1)*tcf,(y-1)*tcf,tcf,tcf)
		end
	end
	gr.setCanvas()
	-- Crear la imagen inicial:
	m={}-- Matriz.
	for y=1,lienzo.alto do
		-- Crear fila:
		m[y]={}
		for x=1,lienzo.ancho do
			-- Crear un pixel blanco y con tranparencia de 100%:
			local pixel={}
			pixel.r=r()--255-- Red.
			pixel.g=r()--255-- Green.
			pixel.b=r()--255-- Blue.
			pixel.o=50-- Opacity (opacidad).
			-- Guardar pixel:
			m[y][x]=pixel
		end
	end
end
function r( ... )return math.random(0,255) end
function love.update(dt)
	
end
function love.draw()
	local x=x or ancho_p/2-lienzo.ancho*lienzo.tam_pixel/2
	local y=y or alto_p/2-lienzo.alto*lienzo.tam_pixel/2+ANCHO_BARRA_HERR/2
	--Dibujar fondo del lienzo:
	gr.setColor(255,255,255)
	gr.draw(fondo,x,y)
	-- Dibujar imagen:
	for i=1,lienzo.alto do
		for j=1,lienzo.ancho do
			gr.setColor(m[j][i].r,m[j][i].g,m[j][i].b,m[j][i].o)
			gr.rectangle("fill",
				x+(j-1)*lienzo.tam_pixel,
				y+(i-1)*lienzo.tam_pixel,
				lienzo.tam_pixel,
				lienzo.tam_pixel)
		end
	end
end
Many thanks.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: An issue with a canvas

Post by micha »

Code: Select all

   local x,y=0,0
   for i=1,lienzo.alto*lienzo.tam_pixel,tcf do
      y=y+1 -- THIS LINE SEEMS NOT TO WORK.
      for j=1,lienzo.ancho*lienzo.tam_pixel,tcf do
         x=x+1
         if (x+y)%2==0 then
            gr.setColor(255,255,255)
         else
            gr.setColor(127,127,127)
         end
         gr.rectangle("fill",(x-1)*tcf,(y-1)*tcf,tcf,tcf)
      end
   end
The error is that you do not reset x to 0 once you have drawn a line. So the second row is drawn, but so far to the right that you cannot see it. Besides that you can greatly simplify you code:
You don't need four variables x,y,i and j. Two are enough to traverse a 2d-array. So you can shorten it.

Code: Select all

for x = 0,xmax do
  for y = 0,ymax do
    gr.setColor(127,127,127)
    if (x+y)%2 == 0 then
      gr.setColor(255,255,255)
    end
    gr.rectangle('fill',x*tcf,y*tcf,tcf,tcf)
  end
end
This is shorter and more elegant (and resets x correctly).

Secondly, here is how you can do quick and dirty debugging in such cases: Print the values of important variables. So for example in the line where you draw the rectangle you could add

Code: Select all

print('x = ' .. x .. ', y = ' .. y)
To see the actual values, when the rectangle is drawn. (You need to start your game from console to see the printed text)
User avatar
tavuntu
Citizen
Posts: 65
Joined: Mon Dec 24, 2012 6:56 am
Contact:

Re: An issue with a canvas

Post by tavuntu »

I can't believe how dumb I am :) many thanks. About the 2 couples (i,j and x,y), I did this because I have to control not only the gray or white color, I want the canvas to suitin a defined rectangle, Many thanks again, micha, I was half asleep when I wrote the code, maybe that helped :3
User avatar
tavuntu
Citizen
Posts: 65
Joined: Mon Dec 24, 2012 6:56 am
Contact:

Re: An issue with a canvas

Post by tavuntu »

By the way, What does mean obey? I see it in a lot of picture profile here in the forum (just curiosity).
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: An issue with a canvas

Post by Nixola »

It doesn't mean anything. Just ... OBEY.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: An issue with a canvas

Post by raidho36 »

User avatar
tavuntu
Citizen
Posts: 65
Joined: Mon Dec 24, 2012 6:56 am
Contact:

Re: An issue with a canvas

Post by tavuntu »

jajaja "There's nothing to "get". ^.^ Just ... OBEY. Get it?" fine then.
User avatar
tavuntu
Citizen
Posts: 65
Joined: Mon Dec 24, 2012 6:56 am
Contact:

Re: An issue with a canvas

Post by tavuntu »

And can I do that? or only party members? Wait.... can I even have a picture profile??
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: An issue with a canvas

Post by Nixola »

tavuntu wrote:And can I do that? or only party members? Wait.... can I even have a picture profile??
Of course you can ^^
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: An issue with a canvas

Post by raidho36 »

Sounds like you've never been to a phpBB forum.

Go to your user control panel and play around.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 9 guests