background_delete crashes my game

kamiyasi

Member
Here's my creation code
Code:
tmex = background_create_colour(512, 512, c_gray);
tex = background_get_texture(tmex);
And here's the script I call when I want to change the background attached to the variable tmex
Code:
surf = surface_create(512, 512);
surface_set_target(surf);
draw_clear_alpha(c_black, 0);
draw_background_ext(bck_planet, 0, 0,1,1,0,color1,1);
draw_sprite_ext(spr_ring,0,0,0,1,1,0,color2,1);
surface_reset_target();
    background_delete(tmex);
    tmex = background_create_from_surface(surf, 0, 0, 512, 512, false, false);
    tex = background_get_texture(tmex);
It works just fine the first time, but for an additional time I need to execute the script, the game crashes when I try to delete the background using background_delete. If someone can help me figure out why I would appreciate it.
 
J

jackhigh24

Guest
might be better to show what the error is as you might be trying to draw that somewhere before its created a new one
 

TheouAegis

Member
Are you certain it's at that point of the game? Freezing typically implies an infinite loop.
And I don't see a 512x512 background causing a program to freeze up.
Is it freezing in Windows or on iOS/Android?
 

Ehsan

Pirates vs Clones
What version gm are you using? I had this issue if I remember correctly it was trying to delete background_create_gradient on android devices and it crashed somehow (or wouldn't sipmly work). Using draw_texture_flush did the job for me (if I remember correcly).

Bottom line, background_delete might not work for created backgrounds for version 1.3, it didn't for me. GMC was down so I never found out why exactlty...
 

kamiyasi

Member
I am using the most up to date version of GM Studio (1.4.1757)
I am positive that is the moment that causes the freeze.

It is not freezing if I create the background and then delete it. However, if I check if the background exists and if true, use background_delete before creating a new background, then it will freeze the game.
Obviously it's not going to work if I delete it after creating a background however as I need to use the background in my game. draw_texture_flush is not a suitable solution as there are many texture assets, many of a decent size, that need to load in and out of memory quite often.
 

kamiyasi

Member
Ok, so at the point I'm at right now here's the deal.

Code:
if !background_exists(tmex)
{
    tmex = background_create_colour(512, 512, c_gray);
    tex = background_get_texture(tmex);
}
   
background_delete(tmex);


//var surf;
surf = surface_create(512, 512);
surface_set_target(surf);
draw_clear_alpha(c_black, 0);
draw_background_ext(bck_planet, 0, 0,1,1,0,color1,1);
draw_sprite_ext(spr_ring,0,0,0,1,1,0,color2,1);
surface_reset_target();

    tmex = background_create_from_surface(surf, 0, 0, 512, 512, false, false);
    tex = background_get_texture(tmex);


d3d_start();

surface_free(surf);
At the point it's at now, executing this code causes an unrecoverable freeze.
But if you cut out this bit
Code:
if !background_exists(tmex)
{
    tmex = background_create_colour(512, 512, c_gray);
    tex = background_get_texture(tmex);
}
   
background_delete(tmex);
Or if you cut out this bit
Code:
tmex = background_create_from_surface(surf, 0, 0, 512, 512, false, false);
    tex = background_get_texture(tmex);
Then it will execute without freezing.

So when I delete the old background, and then recreate it, that's what causes the freeze.
I need to find a way to fix this because obviously creating a new background without deleting the old one is bad.
 

TheouAegis

Member
I originally was going to post this but then changed my mind. I'll just throw it out as a possibility:

Don't delete the old background. Save the id of the new background to a temporary variable. Use background_assign() to copy the new background to the old one, and then delete the new background.
 
Top