Page 1 of 3

Images turn into white squares after time

Posted: Thu Mar 24, 2016 11:33 am
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.

Re: Images turn into white squares after time

Posted: Thu Mar 24, 2016 12:22 pm
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 183 times

Re: Images turn into white squares after time

Posted: Thu Mar 24, 2016 12:27 pm
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

Re: Images turn into white squares after time

Posted: Thu Mar 24, 2016 5:56 pm
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

Re: Images turn into white squares after time

Posted: Thu Mar 24, 2016 6:42 pm
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.

Re: Images turn into white squares after time

Posted: Thu Mar 24, 2016 7:43 pm
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, :/

Re: Images turn into white squares after time

Posted: Thu Mar 24, 2016 8:24 pm
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. :)

Re: Images turn into white squares after time

Posted: Thu Mar 24, 2016 8:51 pm
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.

Re: Images turn into white squares after time

Posted: Sat Mar 26, 2016 6:13 pm
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

Re: Images turn into white squares after time

Posted: Sat Mar 26, 2016 6:26 pm
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?