Black rectangle drawn instead of image

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.
Post Reply
Sokolas
Prole
Posts: 2
Joined: Sun Mar 04, 2012 10:38 am

Black rectangle drawn instead of image

Post by Sokolas »

Hello.
I'm completely new to LOVE engine, and I decided to start with reading tutorials. I'm following the instructions, but when I try to draw an image, the black rectangle is drawn instead. It has the same size as the image. I tried different image editors and different formats but every time I got the same result.
Here's the code:

Code: Select all

function love.load()
	image1 = love.graphics.newImage("cake.jpg")
	local f = love.graphics.newFont(12)
	love.graphics.setFont(f)
	love.graphics.setColor(0, 0, 0, 255)
	love.graphics.setBackgroundColor(255, 255, 255)
	num = 0
	imgx = 10
	imgy = 30
end

function love.update(dt)
	if love.keyboard.isDown("up") then
		num = num + 100*dt
	end
end

function love.draw()
	love.graphics.print("cake", 10, 10)
	love.graphics.draw(image1, imgx, imgy)
end

function love.mousepressed(x, y, button)
	if button == 'l' then
		imgx = x
		imgy = y
	end
end
And here's what the result looks like:
bat5V.jpg
bat5V.jpg (13.86 KiB) Viewed 317 times
Everything else works fine: the text output and mouse events.
I'm using windows 7 x64, can it be the issue?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Black rectangle drawn instead of image

Post by bartbes »

Code: Select all

function love.load()
	--snip
	love.graphics.setColor(0, 0, 0, 255)
	--snip
end
--snip
function love.draw()
	love.graphics.print("cake", 10, 10)
	love.graphics.draw(image1, imgx, imgy)
end
--snip
You're setting the color to solid black, and then drawing the image with that, you probably want to do something like this:

Code: Select all

function love.draw()
    love.graphics.setColor(0, 0, 0) -- last argument is 255 by default
    love.graphics.print("cake", 10, 10)
    love.graphics.setColor(255, 255, 255) -- white, which would leave the image intact
    love.graphics.draw(image1, imgx, imgy)
end
Sokolas
Prole
Posts: 2
Joined: Sun Mar 04, 2012 10:38 am

Re: Black rectangle drawn instead of image

Post by Sokolas »

Thank you very much!
But why is image drawn in the solid color? The tutorial (https://love2d.org/wiki/Tutorial:Callback_Functions) says nothing about it.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Black rectangle drawn instead of image

Post by bartbes »

That has to do with the ColorMode, unfortunately I can't think of a way to really explain this, however.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests