Page 1 of 1

Text in front of Geometry

Posted: Thu Jan 03, 2019 3:07 am
by Pardaleco
Is there a way for me to get some text using love.graphics.print in front of rectangles created by love.graphics.rectangle ??

Example I created some recangles using

Code: Select all

world = {}

function CreateObject(x, y, w, h)
  return {position = vector2.new(x, y), size = vector2.new(w, h)}
end

function LoadWorld()
  world[1] = CreateObject(0,0,1400,200) --ceiling
  world[2] = CreateObject(0,800-200,1400,200) --floor
end

function DrawWorld(world)
  for i = 1, table.getn(world), 1 do
    love.graphics.rectangle("fill", world[i].position.x, world[i].position.y, world[i].size.x, world[i].size.y)
  end
end
And I want this next text which is a timer to be on top of the ceiling rectangle

Code: Select all

countdown = 5

function Countdown(dt)
  if countdown > 0 then
    countdown = countdown - dt
  end
  if countdown == 0 then
    countdown = countdown 
  end
  
end

function Drawtimer()
  love.graphics.setColor(0,0,0)
  love.graphics.setDefaultFilter("nearest", "nearest")
  local timerfont = love.graphics.newFont("edosz.ttf", 25)
  love.graphics.setFont(timerfont)
  local round = string.format("%.1f", countdown)
  love.graphics.print("time: " .. round, 60, 300)
  love.graphics.setColor(1,1,1)
end

Re: Text in front of Geometry

Posted: Thu Jan 03, 2019 4:48 am
by zorg
Without seeing the code that calls DrawWorld and Drawtimer, i'm assuming it looks somewhat like this; note the order:

Code: Select all

Drawtimer(w)
DrawWorld(w)
If that's the issue, then if you draw the timer last, it will be on top of everything that has been drawn before it; simple as that.


On the other hand, don't create new fonts each time you call something as repetitive as a function that runs every frame; just have timerfont defined and created elsewhere.

Re: Text in front of Geometry

Posted: Sat Jan 05, 2019 7:23 am
by Pardaleco
Thank you zorg! It was really that simple :s!

And again, thank for the advice on the font, I love efficiency so that helps!
Thank you again