ZombieSquirrel
Member
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...
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: