• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

(SOLVED) Bizarre lag problem: Surface problem, Camera problem, or something else?

Hi everyone.
For the past year I've been coding my own game.
It's becoming quite large, at least for a beginner like me.
However I've always ran across a problem: lag.

At first I thought it was the number of objects and the code in them.
I learned how to optimize, I learned how to make sure objects don't cause this.
It took me a while and I've learned a lot, in fact, object lag is no longer an issue in my game,
even with high object counts.

But the lag endured… At least "sometimes".
So I went about making sure my texture groups are optimized etc,...
And that helped.
In fact, the lag was seemingly gone after that.

But!

...But the lag endured O_O

This time, I have become 100% sure the lag ONLY happens after MINIMIZING the game, then maximizing it again.
I have NO IDEA why this happens.
I guess, just guess, it has to do with surfaces or something...
I am seeking more professional help here, this doesn't seem a beginner/intermediate problem anymore.

For those of you interested in this puzzle, I will include the code of the one object
that handles surfaces and shaders: my camera object.
Why the camera? I only use one camera and it always follows the player.
So i'll just use that to handle very simple shader lighting and surface drawing.
Plus, all other objects are pretty much optimized and I followed them in the profiler in debug mode. They are not the cause.
Also, the game tells me a high FPS count, sometimes even around 300,
while the game lags like it's going at 10 fps, all after minimizing the game window.
Weird huh?

So here we go: CAMERA CODE

CREATE EVENT:
--------------------------
Scale = 4;
Display_Height = display_get_height()/Scale;
Display_Width = display_get_width()/Scale;
surface_resize(application_surface, display_get_width(), display_get_height());
display_set_gui_size(Display_Width, Display_Height);
Camera = camera_create();
camera_set_view_size(Camera, Display_Width, Display_Height);
camera_set_view_pos(Camera, x - (Display_Width / 2), y - (Display_Height / 2));

+ some shader color variables that I know can't be the issue, so I'll leave them out of this (basically stuff like redvalue = number or contrast = number).


ROOM START EVENT:
--------------------------
view_enabled = true;
view_visible[0] = true;
view_set_camera(0, Camera);

x = OBJ_Player.x;
y = OBJ_Player.y;


STEP EVENT:
-------------------
camera_set_view_pos(Camera, x - (Display_Width / 2), y - (Display_Height / 2));
if (OBJ_Player.x < room_width - Display_Width / 2) && (OBJ_Player.x > Display_Width/2)
{x = OBJ_Player.x}
if (OBJ_Player.y < room_height - Display_Height / 2) && (OBJ_Player.y > Display_Height/2)
{y = OBJ_Player.y}
if (OBJ_Player.x >= room_width - Display_Width / 2)
{x = room_width - Display_Width / 2}
if (OBJ_Player.x <= Display_Width / 2)
{x = Display_Width / 2}
if (OBJ_Player.y >= room_height - Display_Height / 2)
{y = room_height - Display_Height / 2}
if (OBJ_Player.y <= Display_Height / 2)
{y = Display_Height / 2}




POST-DRAW EVENT
-------------------------
This is might be a bit tricky, as it contains some variables defined in the create event and manipulated in the step
event that aren't shown here. That code is however, not a part of this problem. It's basically how to mix colors, brightness,
and contrast and are very simple calculations that in no way could contribute to a problem related to minimizing the game.


if (!surface_exists(srf_lights))
{
srf_lights = surface_create(surface_get_width(application_surface), surface_get_height(application_surface));
tex_lights = surface_get_texture(srf_lights);
}
surface_set_target(srf_lights);

draw_clear(c_black);
gpu_set_blendmode(bm_add);
gpu_set_tex_filter(true);

var lightsstrength = lightsalpha;
var vx = camera_get_view_x(Camera);
var vy = camera_get_view_y(Camera);

if (instance_exists(OBJ_PlayerLight)) //imagine a light disc around the player to light up his surrounding area in a dark dungeon
{
with(OBJ_PlayerLight)
{
draw_sprite_ext(sprite_index, image_index, OBJ_Camera.Scale*(x-vx), OBJ_Camera.Scale*(y-vy),
OBJ_Camera.Scale*2, OBJ_Camera.Scale*2,
image_angle,
image_blend,
image_alpha*1/* *lightsstrength */);
}
}

gpu_set_blendmode(bm_normal);
gpu_set_tex_filter(false);
surface_reset_target();


shader_set(SHD_DayNightWeather);

shader_set_uniform_f(udaycolorred, colormixred);
shader_set_uniform_f(udaycolorgreen,colormixgreen);
shader_set_uniform_f(udaycolorblue, colormixblue);
shader_set_uniform_f(ucontrast, conmix);
shader_set_uniform_f(usaturation, satmix);
shader_set_uniform_f(ubrightness, brightmix);
shader_set_uniform_f(upop, popmix);
shader_set_uniform_f(utres, tresmix);

texture_set_stage(s_lights, tex_lights);

(these were the variables i talked about in the create event)


application_surface_draw_enable(false);
if (surface_exists(application_surface))
{
draw_surface(application_surface, 0, 0);
}


shader_reset();




CLEANUP EVENT
-----------------------
if surface_exists(srf_lights)
{
surface_free(srf_lights);
}
surface_free(application_surface);




Now I will admit I don't fully understand everything that is happening here. I understand most of it but not everything. I
followed tutorials for this.



So for those crazy enough to have read all that, let me remind you of the question:
The game lags ONLY after minimizing and then maximizing the game.

In the Game options I have this enabled:
Allow fullscreen switching
Allow window resize
Keep aspect ratio


And that's it. That's the problem.
If I just play the game, no lag happens AT ALL.
If i play it and minimize, maximize, lag happens and it fluctuates randomly between severe and annoying.



Also one extra weird thing:
Sometimes when I build and start the game from the engine, the game starts in a windowed mode and refuses to go fullscreen.


I hope someone can help, because it's giving me a headache...
 
Last edited:
R

robproctor83

Guest
Please don't post code directly, it should be wrapped with code tags for readability.

First, do you have vsync on? Try enabling/disabling and see if that works. Do a Google search for how to set it if your not sure.

Next, what is you real fps? Enable debug overlay and see what it gives you. This will also tell you about what types of calls are consuming the fps, ie draw, step, etc.[/code]
 
Vsync is off, and FPS is around 200-300. After minimizing and maximizing the window, it drops to about 120 - 200 (it varies).
However it's in no way that, as the game lags well below 60 fps.

Maybe the game has trouble maximizing? Like it's stuck in windowed mode or something? I really don't know anymore...
 
R

robproctor83

Guest
Did you try enabling vsync to see if it fixed it? Even at high fps without vsync it can appear as lag. My game runs normal between 200 and 500fps, but once it drops around 200 I start noticing stuttering from the tearing.

You should also try looking at debugger and seeing if it suddenly starts using more memory. You should also look at the graphics tab in debugger and inspect your surfaces. Perhaps when you tab away one of them is getting lost and when you tab back your not properly recreating it.

Also, might be worth looking at your os process manager (windows task manager) and see if it's eating up your cpu/gpu or what exactly.

You may also want to try a few things to see if it helps. When you tab back try enabling/disabling fullscreen to see if that helps. How are you setting fullscreen? You might also want to try updating your graphics drivers/os drivers to be safe.

Finally, should mention I have seen this pop-up in the forums before, though I don't remember the solution, and I couldn't find it with a quick search, but maybe try searching Google as I know this has come up before.
 
You should also look at the graphics tab in debugger and inspect your surfaces. Perhaps when you tab away one of them is getting lost and when you tab back your not properly recreating it.

Also, might be worth looking at your os process manager (windows task manager) and see if it's eating up your cpu/gpu or what exactly.

You may also want to try a few things to see if it helps. When you tab back try enabling/disabling fullscreen to see if that helps. How are you setting fullscreen? You might also want to try updating your graphics drivers/os drivers to be safe.
I never thought about such a simple thing actually.
And guess what... No more lag.

I only had to add ONE line of code in the STEP event of my camera:
window_set_fullscreen(true)


So testing the game, and pressing ctrl+esc to back out of it, then clicking the icon on the taskbar to maximize, caused big lag spikes.

But thinking about "fullscreen" made me wonder. After maximizing the game, i just pressed alt+enter 2 times to make it windowed and then fullscreen again.
This made the lag dissapear so I placed window_set_fullscreen(true) in my camera object and it seem to have solved it.

Which leaves me to wonder exactly WHY the game had trouble with "a windowed mode that appeared like a fullscreen, but wasn't"
...
 
Top