Page 1 of 2

An issue with a canvas

Posted: Thu Sep 19, 2013 5:32 am
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.

Re: An issue with a canvas

Posted: Thu Sep 19, 2013 6:17 am
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)

Re: An issue with a canvas

Posted: Thu Sep 19, 2013 4:49 pm
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

Re: An issue with a canvas

Posted: Thu Sep 19, 2013 5:02 pm
by tavuntu
By the way, What does mean obey? I see it in a lot of picture profile here in the forum (just curiosity).

Re: An issue with a canvas

Posted: Thu Sep 19, 2013 5:08 pm
by Nixola
It doesn't mean anything. Just ... OBEY.

Re: An issue with a canvas

Posted: Thu Sep 19, 2013 5:12 pm
by raidho36

Re: An issue with a canvas

Posted: Thu Sep 19, 2013 5:19 pm
by tavuntu
jajaja "There's nothing to "get". ^.^ Just ... OBEY. Get it?" fine then.

Re: An issue with a canvas

Posted: Thu Sep 19, 2013 5:23 pm
by tavuntu
And can I do that? or only party members? Wait.... can I even have a picture profile??

Re: An issue with a canvas

Posted: Thu Sep 19, 2013 5:25 pm
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 ^^

Re: An issue with a canvas

Posted: Thu Sep 19, 2013 5:31 pm
by raidho36
Sounds like you've never been to a phpBB forum.

Go to your user control panel and play around.