background image wont show up

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
xcrvaz
Prole
Posts: 7
Joined: Thu Nov 17, 2022 12:33 am

background image wont show up

Post by xcrvaz »

I'm brand new to love, and I tried adding a background image to my project, and nothing showed. A little help?

Code: Select all

local items = {
   "Gold bar",
   "Dagger of the forest",
   "Spirit Lock",
   "Book of knowledge",
   "Balloon dog",
   "Tomato",
   "Garbage bag",
   "Digested gum",
   "Lint Chunk",
   "Potion of Strength",
   "Walmart Shopping Cart"
}
local menu = love.audio.newSource("menu.mp3", "stream")
local ksong = love.audio.newSource("kenti village.mp3", "stream")
local swoovo = love.audio.newSource("swoovo.mp3", "stream")
local fsong = love.audio.newSource("peak's forest.mp3", "stream")
local inventory = {}
local coins = 0
local level = 1
local digit1 = 0
local digit2 = 0
local btforest = nil
local btkenti = false
local cttmarie = true
local zone = swoovo
swoovoday = love.graphics.newImage("swoovoback1.png")
swoovonight = love.graphics.newImage("swoovonight.png")
function background(image)
  local scaleX = 200/image:getWidth() -- 200 and 100 from rectangle width and height
  local scaleY = 100/image:getHeight() -- e.g. 100/200 -> scales by 0.5
  love.graphics.draw(image, 0,0, 0, scaleX, scaleY)
end
function wait(seconds)
   love.timer.sleep(seconds)
end
local function p(txt, x, y)
   love.graphics.print(txt, x, y)
end
menu:play()
background(swoovoday)
p("Welcome to goofy tading", 250, 250)
wait(1)
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: background image wont show up

Post by MrFariator »

From what I see, this

Code: Select all

background(swoovoday)
p("Welcome to goofy tading", 250, 250)
should become

Code: Select all

-- note: you'll only need a single instance of love.draw. 
-- Declaring another one will have one overwrite the other.
function love.draw ()
  background(swoovoday)
  p("Welcome to goofy tading", 250, 250)
end
Do note that in love2d, you don't want to call a wait function or equivalent while drawing your graphics, because this will halt the rendering (and rest of program execution) until that wait finishes. Such pausing can lead to erratic behavior, or even the application hanging up.

Additionally, consider the following:

Code: Select all

function background(image)
  -- if we're always going to draw the image at half-height, we don't need to call image:getWidth/Height.
  -- Instead, we can just plug in the desired scale factor directly.
  love.graphics.draw(image, 0,0, 0, 1.0, 0.5)
end
User avatar
zorg
Party member
Posts: 3435
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: background image wont show up

Post by zorg »

I would also look at the wiki and the listed callback functions, to understand how löve expects you to set things up: https://love2d.org/wiki/love#Callbacks
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests