Images turn into white squares after time

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.
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Images turn into white squares after time

Post by Vimm »

for some odd reason, whenever I run my program (not willing to call it a game yet) the image I have set to spawn will start spawning, and then after a few seconds(30 or so) the images will blank out and be replaced with white squares, can someone take a look at the code and see if they can find out what the problem is, because I have no idea.
Attachments
Asteroid.love
(24.49 KiB) Downloaded 192 times
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Images turn into white squares after time

Post by MadByte »

Seems to be a problem with your system. Can you post your specs, especially graphics card/chip and drivers.

btw. the way you use classes seems to be pretty weird. You create a new object table inside your Asteroid class to handle them but the sense of a class is to be able to create an instance of it - so if you set up an asteroid object it shouldn't create it's own table objects and update/draw them. You didn't asked for it but I want to suggest you to look at this piece here.
Asteroid.love
(24.44 KiB) Downloaded 171 times
Last edited by MadByte on Thu Mar 24, 2016 12:38 pm, edited 3 times in total.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Images turn into white squares after time

Post by pgimeno »

Works for me. I see you create a new image every time you add an asteroid. You should try to reuse the image as much as possible. Maybe you're running out of video memory?

On an unrelated note, I've noticed that this loop should go in reverse order:

Code: Select all

        for i, a in ipairs (asteroid) do
                a.x = a.x - (200 * dt)

                if a.x < -10 then
                        table.remove(asteroid, i)
                end
        end
That's because when you remove say element 5, what was element 6 becomes element 5, but you don't check element 5 again, so that element goes unchecked (i.e. it is skipped). If you go in reverse order, if you remove element 5, you don't care that 6 becomes 5, because 6 is already checked, and the next you will check is element 4.

In your case it probably doesn't matter because there will seldom be more than 1 asteroid removed per pass, but it's good to be trained in recognizing that kind of action and remedying it in general.

Here's an example of how to do that:

Code: Select all

        for i = #asteroid, 1, -1 do
                local a = asteroid[i]
                a.x = a.x - (200 * dt)

                if a.x < -10 then
                        table.remove(asteroid, i)
                end
        end
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: Images turn into white squares after time

Post by Vimm »

MadByte wrote:Seems to be a problem with your system. Can you post your specs, especially graphics card/chip and drivers.

btw. the way you use classes seems to be pretty weird. You create a new object table inside your Asteroid class to handle them but the sense of a class is to be able to create an instance of it - so if you set up an asteroid object it shouldn't create it's own table objects and update/draw them. You didn't asked for it but I want to suggest you to look at this piece here.
Asteroid.love
Yeah I figured it'd be a problem with my computer, but my specs are fine, or should be
GPU : AMD Radeon R9 270
intel core i5-4460 3.20 Ghz
1 8 GB DDR3 RAM

the computers been acting weird lately so Its possible some parts are broken.

also, not sure what you mean by
You create a new object table inside your Asteroid class to handle them but the sense of a class is to be able to create an instance of it - so if you set up an asteroid object it shouldn't create it's own table objects and update/draw them.
im still new to programming haha



Also about the other guys point
I see you create a new image every time you add an asteroid. You should try to reuse the image as much as possible.
how do I reuse the image? Again, fairly new
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Images turn into white squares after time

Post by MadByte »

how do I reuse the image? Again, fairly new
Actually you can see it by looking at my example above.

Code: Select all

...
local Asteroid = Object:extend()
local image    = love.graphics.newImage("assets/asteroid.png")

function Asteroid:new(x, y)
  self.image    = image
  ...
also, not sure what you mean by...
You create a new object table inside your Asteroid class to handle them but the sense of a class is to be able to create an instance of it - so if you set up an asteroid object it shouldn't create it's own table objects and update/draw them.
I mean this:

Code: Select all

function Asteroid:update(dt)
	createEnemyTimer = createEnemyTimer - 1 * dt
	if createEnemyTimer < 0 then
		createEnemyTimer = createEnemyTimerMax
		
		--random number
		randomNumber = math.random(128, love.window.getHeight() - 200)
		--end random number 
		
		newAsteroid = {x = asteroid.x, y = randomNumber, img = asteroid.img}
		table.insert(asteroid, newAsteroid)
	end
	
	for i, a in ipairs (asteroid) do
		a.x = a.x - (200 * dt)
		
		if a.x < -10 then
			table.remove(asteroid, i)
		end
	end
end

function Asteroid:draw(dt)
		for i,asteroid in ipairs(asteroid) do
			love.graphics.draw(asteroid.img, asteroid.x, asteroid.y)
	end
end
You use your Asteroid.lua as an "object handler" on it's own by letting it create new asteroids and then update and draw them inside the class. But this is the opposit to what a class is meant for. In my example the asteroid.lua creates a new asteroid and the update and draw process happens somewhere else. You need to make sure that your class just hold the object itself.

your specs shouldn't be the problem at all.
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: Images turn into white squares after time

Post by Vimm »

MadByte wrote:
how do I reuse the image? Again, fairly new
Actually you can see it by looking at my example above.

Code: Select all

...
local Asteroid = Object:extend()
local image    = love.graphics.newImage("assets/asteroid.png")

function Asteroid:new(x, y)
  self.image    = image
  ...
also, not sure what you mean by...
You create a new object table inside your Asteroid class to handle them but the sense of a class is to be able to create an instance of it - so if you set up an asteroid object it shouldn't create it's own table objects and update/draw them.
I mean this:

Code: Select all

function Asteroid:update(dt)
	createEnemyTimer = createEnemyTimer - 1 * dt
	if createEnemyTimer < 0 then
		createEnemyTimer = createEnemyTimerMax
		
		--random number
		randomNumber = math.random(128, love.window.getHeight() - 200)
		--end random number 
		
		newAsteroid = {x = asteroid.x, y = randomNumber, img = asteroid.img}
		table.insert(asteroid, newAsteroid)
	end
	
	for i, a in ipairs (asteroid) do
		a.x = a.x - (200 * dt)
		
		if a.x < -10 then
			table.remove(asteroid, i)
		end
	end
end

function Asteroid:draw(dt)
		for i,asteroid in ipairs(asteroid) do
			love.graphics.draw(asteroid.img, asteroid.x, asteroid.y)
	end
end
You use your Asteroid.lua as an "object handler" on it's own by letting it create new asteroids and then update and draw them inside the class. But this is the opposit to what a class is meant for. In my example the asteroid.lua creates a new asteroid and the update and draw process happens somewhere else. You need to make sure that your class just hold the object itself.

your specs shouldn't be the problem at all.

mkay well ill worry about all that some other time as im sure it doesnt have anything to do with the images disappearing lol.

and yeah my specs shouldnt be a problem but I think part of my computer are actually broken, so that might help XD The thing is though if I run the program, then click off the window so it isn't focused, this doesnt happen. It only happens when I have the window focused, :/
User avatar
slime
Solid Snayke
Posts: 3133
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Images turn into white squares after time

Post by slime »

If you create enough images that the graphics driver runs out of VRAM to use, it could cause instabilities and seemingly random graphics glitches (on top of the obvious performance issues). I'd worry about that now rather than later. :)
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: Images turn into white squares after time

Post by Vimm »

slime wrote:If you create enough images that the graphics driver runs out of VRAM to use, it could cause instabilities and seemingly random graphics glitches (on top of the obvious performance issues). I'd worry about that now rather than later. :)
well yeah im gonna worry about the creating new images thing, but not the other stuff they were talking about.
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: Images turn into white squares after time

Post by Vimm »

MadByte wrote:
how do I reuse the image? Again, fairly new
Actually you can see it by looking at my example above.

Code: Select all

...
local Asteroid = Object:extend()
local image    = love.graphics.newImage("assets/asteroid.png")

function Asteroid:new(x, y)
  self.image    = image
  ...
also, not sure what you mean by...
You create a new object table inside your Asteroid class to handle them but the sense of a class is to be able to create an instance of it - so if you set up an asteroid object it shouldn't create it's own table objects and update/draw them.
I mean this:

Code: Select all

function Asteroid:update(dt)
	createEnemyTimer = createEnemyTimer - 1 * dt
	if createEnemyTimer < 0 then
		createEnemyTimer = createEnemyTimerMax
		
		--random number
		randomNumber = math.random(128, love.window.getHeight() - 200)
		--end random number 
		
		newAsteroid = {x = asteroid.x, y = randomNumber, img = asteroid.img}
		table.insert(asteroid, newAsteroid)
	end
	
	for i, a in ipairs (asteroid) do
		a.x = a.x - (200 * dt)
		
		if a.x < -10 then
			table.remove(asteroid, i)
		end
	end
end

function Asteroid:draw(dt)
		for i,asteroid in ipairs(asteroid) do
			love.graphics.draw(asteroid.img, asteroid.x, asteroid.y)
	end
end
You use your Asteroid.lua as an "object handler" on it's own by letting it create new asteroids and then update and draw them inside the class. But this is the opposit to what a class is meant for. In my example the asteroid.lua creates a new asteroid and the update and draw process happens somewhere else. You need to make sure that your class just hold the object itself.

your specs shouldn't be the problem at all.

Am I still making more images in this example? cuz the whitebox thing still happens
Attachments
thing.love
(36.46 KiB) Downloaded 154 times
User avatar
Sulunia
Party member
Posts: 203
Joined: Tue Mar 22, 2016 1:10 pm
Location: SRS, Brazil

Re: Images turn into white squares after time

Post by Sulunia »

From what i see, you create a new image..

Code: Select all

playerImg = love.graphics.newImage("assets/player.png")
everytime you instantiate a "Player". So, if you create 300 players, you will also load the same file 300 times.

Try adding to your main love.load()

Code: Select all

function love.load()
	player = Player(0,0)
	playerImg = love.graphics.newImage("assets/player.png")
end
and when you draw the player, just use playerImg as you would.

Tl, dr: Make sure you load a texture just once (newImage) and then use the loaded texture everywhere you need it again.

Also, shouldnt this..

Code: Select all

function Player:new(x, y)
	speed = 500
end
be self.speed = 500? So each player has it's own individual speed?
Don't check my github! It contains thousands of lines of spaghetti code in many different languages cool software! :neko:
https://github.com/Sulunia
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 203 guests