Something is wrong with my canvas. Can't spot a bug. Help? ;-;

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
BlackDiamondPL
Prole
Posts: 15
Joined: Tue Aug 08, 2017 8:38 pm

Something is wrong with my canvas. Can't spot a bug. Help? ;-;

Post by BlackDiamondPL »

I am re-making my game using OOP in Lua. Looking at my old code, canvases were taking big amounts of code. What have i done? Created class Cdraw. Made load and new functions. Load, loads, new,draws.
Started to create canvases that i need and start drawing them. First canvas is working fine, BUT whenever i try to load second canvas, i get a black screen. I don't know why it happens, something is wrong with load not the draw. Also i have put 'love.graphics.clear()', still nothing.
I can't spot the bug, so only thing i can do is ask for help (searched bug 2 days, so what?).

Here is the code:

Code: Select all

local canvases = require("common.canvases")
Cdraw = Class:create("Cdraw")
function Cdraw:new()
	love.graphics.draw(canvases.playerPanel,2,(love.graphics.getHeight() - canvases.playerPanel:getHeight()) + 2)
end
function Cdraw:load()
	--playerH()
	playerP()
end
function playerH()
	love.graphics.setCanvas(canvases.playerHealth)
	love.graphics.clear()
	love.graphics.setBlendMode("alpha","premultiplied")
	love.graphics.setColor(255,0,0,156)
	love.graphics.rectangle("fill",0,0,canvases.playerHealth:getWidth(),canvases.playerHealth:getHeight())
	love.graphics.setColor(255,255,255)
	love.graphics.printf({{255,255,255},data.player.hp,{255,255,255},"/",{255,255,255},data.player.maxHp},canvases.playerHealth:getWidth(),"center")
	love.graphics.setBlendMode("alpha")
	love.graphics.setCanvas()
end
function playerP()
	love.graphics.setCanvas(canvases.playerPanel)
	love.graphics.setBlendMode("alpha","premultiplied")
	love.graphics.setColor(237,245,100)
	love.graphics.rectangle("fill",0,0,canvases.playerPanel:getWidth()-5,canvases.playerPanel:getHeight()-5,5,5)
	love.graphics.setColor(51,51,0)
	love.graphics.setLineWidth(5)
	love.graphics.rectangle("line",0,0,canvases.playerPanel:getWidth()-5,canvases.playerPanel:getHeight()-5,5,5)
	love.graphics.setLineWidth(2)
	--love.graphics.draw(canvases.playerHealth,0,0)
	love.graphics.line(canvases.playerPanel:getWidth()/3,0,canvases.playerPanel:getWidth()/3,canvases.playerPanel:getHeight())
	love.graphics.setColor(255,255,255)
	love.graphics.setBlendMode("alpha")
	love.graphics.setCanvas()
end
return Cdraw
Canvas list:

Code: Select all

canvases = {
	enemyC = love.graphics.newCanvas(400,200),
	enemyHealth = love.graphics.newCanvas(300,25),
	playerPanel = love.graphics.newCanvas(800,160),
	--playerHealth = love.graphics.newCanvas(100,25)
}
canvases.playerHealth = love.graphics.newCanvas(canvases.playerPanel:getWidth()/3,25)
return canvases
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Something is wrong with my canvas. Can't spot a bug. Help? ;-;

Post by grump »

Can you show the part where you draw the canvas objects on the screen? Or even better, attach a love file that shows the problem?
BlackDiamondPL
Prole
Posts: 15
Joined: Tue Aug 08, 2017 8:38 pm

Re: Something is wrong with my canvas. Can't spot a bug. Help? ;-;

Post by BlackDiamondPL »

Still didn't figured out. :shock:
CdrawError.love
(79.67 KiB) Downloaded 259 times
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Something is wrong with my canvas. Can't spot a bug. Help? ;-;

Post by grump »

There is an errorneous call to love.graphics.printf in CDraw.lua, function playerH(). This cupid thing prevents you from seeing the error. Remove require("lib.cupid") from conf.lua to get your error messages back.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Something is wrong with my canvas. Can't spot a bug. Help? ;-;

Post by grump »

Btw, you shouldn't call math.randomseed() every frame. This has the opposite effect of what you want. Call it once at the start at the program.

That CDraw() call in love.draw also seems suspicious. Doesn't that create a new CDraw instance in every frame? Better give CDraw a draw() function and call that instead.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Something is wrong with my canvas. Can't spot a bug. Help? ;-;

Post by MadByte »

The problem is that you don't use classes as they meant to be used. In the main you are trying to draw the class itself what doesn't make sense. I recommend you to read a bit more about classes amd OOP in lua.

Here an example how it can look like (untested code):

Code: Select all

-- Player.lua --
local Player = Class:create("Player")

function Player:new(x, y)
  self.x = x or 0
  self.y = y or 0
  self.width = 32
  self.height = 64
end


function Player:update(dt) end


function Player:draw()
  love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end

return Player


-- Main.lua --
local Player = require("classes.player")
local player_instance

function love.load()
  player_instance = Player()
end


function love.update(dt)
  player_instance:update(dt)
end


function love.draw()
  player_instance:draw()
end
Something like that should be your base when using classes.
If you fix that and learn a bit more about OOP and classes / methods in lua then your canvases at least have a chance to work as intended.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Something is wrong with my canvas. Can't spot a bug. Help? ;-;

Post by grump »

MadByte wrote: Wed Aug 30, 2017 7:39 pm The problem is that you don't use classes as they meant to be used.
I agree that the code is a bit misstructured and needs work, but the actual problem that prevents him from seeing anything is the errorneous call to printf in combination with whatever that cupid thing is doing that prevents error messages from being shown. Fix/remove the printf and you'll at least see something.

What is that cupid thing even supposed to do? Why does it intercept errors and silently discard them?
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Something is wrong with my canvas. Can't spot a bug. Help? ;-;

Post by MadByte »

Nice find, you're right. Sorry about that. :nyu:
Anyway, I still think BlackDiamond should try to rethink his way of putting things togehter.
BlackDiamondPL
Prole
Posts: 15
Joined: Tue Aug 08, 2017 8:38 pm

Re: Something is wrong with my canvas. Can't spot a bug. Help? ;-;

Post by BlackDiamondPL »

Cupid is in game console. i usualy check and debug my games. So when i program something i can have love running and programming if i would call it 'in run'.
Well this time it failed. I thnk i won't use it again if something like that can happen next time :x.

I just started with classes i don't know them well. Standard structure of class is not needed for me. For example player. I have only one player and that's it, same goes for enemy. Please don't sue me for writing that
But just give me week or two to learn about it. :roll:
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Something is wrong with my canvas. Can't spot a bug. Help? ;-;

Post by Beelz »

You're missing out on the best part of OOP: inheritance. Even if your player and enemy classes are completely different, I'm sure they do have some similarities... For instance, they both probably use the same variables like x and y for position, and maybe one for health. You could make a base entity that has the variables and methods that they have in common, instead of copy and pasting it in every new entity type. Ex: (typed on phone, not tested)

Code: Select all

local entity = Class:extend()

function entity:init(x, y)
  self.x = x or 0
  self.y = y or 0
  self.health = 100
end

function entity:setPosition(x, y)
  self.x, self.y = x, y
end

function entity:hurt(dmg)
  self.health = self.health - dmg
end

function entity:isDead()
  return self.health <= 0
end

-- You can even include blank functions
function entity:update(dt) end
-- that way you can update all entities without first checking if it has the method and also not worry about a nil reference

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
Post Reply

Who is online

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