Legacy GM [SOLVED] Problem with lighting engine - surface

P

ProExCurator

Guest
So I decided to use this lighting engine, but there is couple of problems with it.

For example when I'm trying to go fullscreen I get this:
Code:
##########################################################################################
ERROR in
action number 1
of  Step Event0
for object obj_control:

Trying to use non-existing surface.
 at gml_Object_obj_control_StepNormalEvent_1 (line 13) -   draw_surface_ext(sur, x - sprite_width / 2, y - sprite_height / 2, 1, 1, 0, image_blend, 1);
##########################################################################################
Lighting engine (Download - http://www.masterxy.bplaced.net/download.php?i=load&n=1):

script - light_change_scale
Code:
image_xscale = argument0;
image_yscale = argument1;

surface_free(sur);
sur = surface_create(sprite_width, sprite_height);

if !surface_exists(sur) {
sur = surface_create(sprite_width, sprite_height);
}
script - light_update
Code:
var xp, yp, lw, lh, imin, imax, i, pdmin, pdmax, minx, miny, maxx, maxy, a_min, a_max, a_test;

lw = sprite_width;
lh = sprite_height;

surface_set_target(sur);
draw_clear(c_black);
draw_sprite_ext(sprite_index, image_index, lw / 2, lh / 2, image_xscale, image_yscale, image_angle, c_white, 1);

with (obj_wall) {
  minx = px[0];
  miny = py[0];
  maxx = px[0];
  maxy = py[0];
 
  imin = -1;
  imax = -1;
  a_test = point_direction(other.x, other.y, x, y);
  a_min = -1;
  a_max = -1;
 
  for (i = 0; i < points; i++) {
    if (px[i] < minx)
      minx = px[i];
    if (px[i] > maxx)
      maxx = px[i];
    if (py[i] < miny)
      miny = py[i];
    if (py[i] > maxy)
      maxy = py[i];
 
    pdmin = (point_direction(other.x, other.y, x + px[i], y + py[i]) - a_test + 360) mod 360;
    pdmax = (a_test - point_direction(other.x, other.y, x + px[i], y + py[i]) + 360) mod 360;
    if (pdmin > a_min && pdmin <= 180) {
      a_min = pdmin;
      imin = i;
    }
    if (pdmax > a_max && pdmax <= 180) {
      a_max = pdmax;
      imax = i;
    }
  }
 
  if (rectangle_in_rectangle(x + minx, y + miny, x + maxx, y + maxy, other.x - lw / 2, other.y - lh / 2, other.x + lw / 2, other.y + lw / 2)) {
    i = imin;
    draw_primitive_begin(pr_trianglestrip);
    do {
      pd = point_direction(other.x, other.y, x + px[i], y + py[i]);
      draw_vertex_color(x + px[i] - other.x + other.sprite_width / 2, y + py[i] - other.y + other.sprite_height / 2, c_black, 1);
      draw_vertex_color(x + px[i] + lengthdir_x(height, pd) - other.x + other.sprite_width / 2, y + py[i] + lengthdir_y(height, pd) - other.y + other.sprite_height / 2, c_black, 1 - fade);
      i += 1;
      i = i mod points;
    } until (i == (imax + 1) mod points)
    draw_primitive_end();
  }
}

surface_reset_target();
Object - obj_control - Create Event:
Code:
// MUST HAVE: Create a surface for drawing all the lights
sur = surface_create(room_width, room_height);

// Create a light
m = instance_create(0, 0, obj_light);
with (m) {
  light_change_scale(3, 3);
  // Update the light, so it's surface is drawn correctly
  light_update();
}
Object - obj_control - Step Event:
Code:
// MUST HAVE: Draw all the lights to the surface
surface_set_target(sur);
draw_clear(c_black);
draw_set_blend_mode(bm_add);
with (obj_light) {
  draw_surface_ext(sur, x - sprite_width / 2, y - sprite_height / 2, 1, 1, 0, image_blend, 1);
}
draw_set_blend_mode(bm_normal);
surface_reset_target();
Object - obj_control - End Step:
Code:
// Update one light to follow the mouse
m.x = mouse_x;
m.y = mouse_y;

// Update this light to draw the correct shadows
with (m)
  light_update();
Object - obj_control - Draw Event:
Code:
// MUST HAVE: Draw the surface to the screen
if !surface_exists(sur){
sur = surface_create(room_width, room_height);
}

draw_set_blend_mode_ext(bm_dest_color, bm_zero);
draw_surface_ext(sur, 0, 0, 1, 1, 0, c_white, 1);
draw_set_blend_mode(bm_normal);

// Draw info
draw_set_color(c_white);
draw_text(0, 0, string(fps) + "fps#" + string(instance_number(obj_light)) + " lights#" + string(instance_number(obj_wall)) + " casters");
draw_set_color(c_black);
...

So I made a "research" and found out that I have to create the surface in the Draw event as well, since a surface can be errased any time in graphics memory.

and I did... but the problem, that it's not helping...
Code:
if !surface_exists(sur){
sur = surface_create(room_width, room_height);
}
 
Last edited by a moderator:
P

ProExCurator

Guest
Making sure a surface exists in a draw event won't stop an error in a step event.

Also, drawing in a step event doesn't draw anything.

View attachment 6867
Are you sure about it? Because removing it does affect light (there is no light), so it looks like it draws "something".
 

obscene

Member
Are you sure about it? Because removing it does affect light (there is no light), so it looks like it draws "something".
I'm never sure of anything. :p You might be able to draw TO a surface in a step event. I've never tested. But definitely the first point I mentioned will help your error.
 
P

ProExCurator

Guest
I'm never sure of anything. :p You might be able to draw TO a surface in a step event. I've never tested. But definitely the first point I mentioned will help your error.
Yes I pasted wrong code, now it's edited.
Now it's in draw event, but still don't stop error to appear while trying to go fullscreen...

Help? Anyone?
 
P

ProExCurator

Guest
If you make sure the surface exists directly before trying to draw it, it will not give error.
obj.control - "press key" - execute code
Code:
if !surface_exists(sur){
sur = surface_create(room_width, room_height);
}

window_set_fullscreen(true);
Like this?
 
P

ProExCurator

Guest
No... before you draw to it...

/// Pseudocode
if !surface_exists surface_create()
surface_draw(...)
My god it works! Sorry for being stupid ;]
Just everything here is new and complicted for me. I'm still learning ;]
 
Top