Why aren't my quads drawing?

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
Oonushi
Prole
Posts: 2
Joined: Thu Sep 17, 2020 12:30 am

Why aren't my quads drawing?

Post by Oonushi »

Hi All,

New member here. I just went through the tutorials at sheepolution without much issue, but now I am working on a small project and am stuck relatively at the outset. I'm doing a simple single-screen platformer type game and am using the final sheepolution tutorial as a basis, but trying to also integrate the tilesheet tutorial from earlier on in the same tutorials and this is where I'm getting stuck. My player and box image/texture render fine, but none of the walls render, however collision with the walls works as expected (though they're invisible). Every thing I can think to print to console to debug and verify looks fine. I know I must be missing something, but just can't figure it out.

Here's the relevant code:

Code: Select all

-- main.lua

local tileSheet
local tileMap = {
	{16,14,14,14,14,14,17, 1, 1,16,14,14,14,14,14,17},
	{19, 0, 0, 0, 0, 0,18, 1, 1,19, 0, 0, 0, 0, 0,18},
	{19, 0, 0, 0, 0, 0,11,14,14,12, 0, 0, 0, 0, 0,18},
	{19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,18},
	{21, 9, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 9,22},
	{ 1, 1,19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,18, 1, 1},
	{ 1, 1,19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,18, 1, 1},
	{16,14,12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,11,14,17},
	{19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,18},
	{19, 0, 0, 0, 0, 0, 6, 9, 9, 7, 0, 0, 0, 0, 0,18},
	{19, 0, 0, 0, 0, 0,18, 1, 1,19, 0, 0, 0, 0, 0,18},
	{21, 9, 9, 9, 9, 9,22, 1, 1,21, 9, 9, 9, 9, 9,22}
}

-- tables to contain collections of game entities
local objects, walls = {},{}

-- game entities
local player, box = {},{}

function love.load()

	player = Player(100, 100)
	box = Box(400, 150)

	table.insert(objects, player)
	table.insert(objects, box)

	tileSheet = TileSheet("assets/tiles/laboratory.png", 64, 5, 5, 1, 1)

	for y,v in ipairs(tileMap) do
		for x,tile in ipairs(v) do
			if tile > 0 then
				table.insert(walls, Wall((x-1)*tileSheet:getTileWidth(), (y-1)*tileSheet:getTileHeight(), tileSheet, tile))
			end
		end
	end
end

-- snip

-- drawing callback; called each frame after update()
function love.draw()

	love.graphics.setBackgroundColor(0, 64, 128)

	for i,v in ipairs(objects) do v:draw() end
	for i,v in ipairs(walls)   do v:draw() end

end

Code: Select all

-- TileSheet.lua

Object = require("lib.classic.classic")

TileSheet = Object:extend()

function TileSheet:new(imagePath, tileSize, sheetWidth, sheetHeight, tileGutter, tileScale)

	self.texture = love.graphics.newImage(imagePath)
	self.tileSize = tileSize
	self.tileGutter = tileGutter or 0
	self.tileScale = tileScale or 1
	self.tileWidth = self.tileSize * self.tileScale
	self.tileHeight = self.tileSize * self.tileScale
	self.tileQuads = {}

	-- build the tile sheet quads:
	for y=0,(sheetHeight-1) do
		for x=0,(sheetWidth-1) do
			table.insert(self.tileQuads, love.graphics.newQuad((x*self.tileWidth)+(x*self.tileGutter), (y*self.tileHeight)+(y*self.tileGutter), self.tileWidth, self.tileHeight, self.texture:getWidth(), self.texture:getHeight()))
		end
	end
end

function TileSheet:getTexture() return self.texture end
function TileSheet:getTileWidth() return self.tileWidth end
function TileSheet:getTileHeight() return self.tileHeight end
function TileSheet:getTileQuad(tile) return self.tileQuads[tile] end
function TileSheet:getTileScale() return self.tileScale end

Code: Select all

-- TileSheetEntity.lua

require("entity")
require("TileSheet")

TileSheetEntity = Entity:extend()

function TileSheetEntity:new(x, y, tileSheet, tile)

	TileSheetEntity.super.new(self, x, y)

	self.tileSheet = tileSheet
	self.tile = tile or 1
	self.width  = self.tileSheet:getTileWidth()
	self.height = self.tileSheet:getTileHeight()
	self.scale = self.tileSheet:getTileScale()
end

function TileSheetEntity:draw()

	love.graphics.draw(self.tileSheet:getTexture(), self.tileSheet:getTileQuad(self.tile), (self.x-1)*self.width, (self.y-1)*self.height, 0, self.scale, self.scale)

end

Code: Select all

-- Wall.lua

require("TileSheetEntity")

Wall = TileSheetEntity:extend()

function Wall:new(x, y, tileSheet, tile)

	Wall.super.new(self, x, y, tileSheet, tile)

	self.strength = 100
	self.weight = 0
	--self.gravity =  0
end

function Wall:draw()

	Wall.super.draw(self)

end
There is some other code of course, but somewhere in the above is my issue as everything else seems to be working fine, I just can't "see" my walls!

Thanks for looking and I hope someone out there can let me know what obvious thing I'm missing here.

I should mention that I am also new to lua, so it could equally be a lua language thing vs a love framework thing.
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: Why aren't my quads drawing?

Post by pgimeno »

Oonushi wrote: Thu Sep 17, 2020 12:45 am Every thing I can think to print to console to debug and verify looks fine.
So you have that luxury but you don't allow it to us... :cry:

I've made a runnable program out of your snippets, but next time please do that yourself. Help us help you. If you don't want to share all of your code, just make some stubs for the parts that you don't want to include, that will allow the program to run.

The problem is that you don't seem to stick to a single convention on what values must go in Wall.x and Wall.y. On creation, you pass pixels:

Code: Select all

				table.insert(walls, Wall((x-1)*tileSheet:getTileWidth(), (y-1)*tileSheet:getTileHeight(), ...
but when drawing you expect 1-based tile coordinates:

Code: Select all

	love.graphics.draw(..., (self.x-1)*self.width, (self.y-1)*self.height, ...
As a result, you're drawing the tile that should be drawn at (0, 0) in coordinates (-64, -64) = ((0-1)*64, (0-1)-64)), the tile at (64, 0) in coordinates (4032, -64) = ((64-1)*64, (0-1)*64), and so on, and no single tile is visible because they are all drawn out of the area of the screen.

Given that walls are being coded as generic entities, I'd say that their x and y should be in pixels, so that the entities in general have the maximum flexibility. This means that the table.insert is correct, and it's the draw call the one that needs changing, to just:

Code: Select all

	love.graphics.draw(..., self.x, self.y, ...
Attachments
Oonushi.love
(14.08 KiB) Downloaded 126 times
Oonushi
Prole
Posts: 2
Joined: Thu Sep 17, 2020 12:30 am

Re: Why aren't my quads drawing?

Post by Oonushi »

Hi,

Thanks for looking at this!

I ended up getting some help on the discord channel last night before this post was approved and figured this out - and yes, this was the correct answer. I was multiplying the x and y coordinates by the tile width an extra time making them drawn way off-screen.

Sorry for not uploading a runnable code sample (I did on discord) I didn't realize it was Ok to do that until after I had made the post.

I'm loving the Love framework and Lua as well - this is my first time working with it (I'm just a hobby programmer) and I love it. It reminds me a bit of python without the strict whitespace nonsense lol.

Thanks again pgimeno!
Post Reply

Who is online

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