Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: GM7/8 Problem with surfaces and views  (Read 8074 times)

0 Members and 1 Guest are viewing this topic.
GM7/8 Problem with surfaces and views
« on: March 19, 2020, 09:40:48 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 448
I have an object that is dealing with lighting effects. Basically it's creating a surface that covers the viewport, drawing a dark rectangle on it, and then essentially "poking out" holes by drawing circles and blending the colors together. At the moment Im trying to achieve the lantern effect. Everything seems to be working fine, except the surface wont follow the player object correctly. Either the dark rectangle is drawn over the viewport and the circle doesnt follow the player until youre close to the views x,y origin, OR the rectangle only draws at the rooms x,y origin not following the view but when you enter that area the circle follows you perfectly. I hope this makes sense. Heres the code.
Create
Code: [Select]
surf = surface_create(302,243);
color = make_color_rgb(8,8,40);

surface_set_target(surf);
draw_clear_alpha(c_black, 0);

surface_reset_target();
Step
Code: [Select]
if (surface_exists(surf)) {
    surface_set_target(surf);
   
    draw_set_color(color);
    draw_set_alpha(.8);
    draw_rectangle(0,0,302,243,0);
   
    draw_set_blend_mode(bm_subtract);
    draw_set_color(c_white);
   
    if global.item_obtained[0] = true {
    draw_circle(objLink.x+7,objLink.y+14,16,false);
    draw_set_alpha(.5)
    draw_circle(objLink.x+7,objLink.y+14,32,false);

    }
   
    draw_set_blend_mode(bm_normal);
    draw_set_alpha(1);
    surface_reset_target();
    } else {

    surf = surface_create(302, 243);
    surface_set_target(surf);
    draw_clear_alpha(c_black, 0);
    surface_reset_target();
   
    } 
Draw
Code: [Select]
if (!surface_exists(surf)) {
    surf = surface_create(302, 243);
} else {
    if (view_current == 0){
        draw_surface(surf, 0, 0);
        }
    }
The changes happen when I change x,y position of draw_surface in the Draw event, either 0,0, or view_xview+0,view_yview+0.
Logged
Re: GM7/8 Problem with surfaces and views
« Reply #1 on: March 19, 2020, 10:48:53 pm »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1626
When drawing something on a surface (outside of going through a viewport) always remember to draw relative to the surface. So instead of draw at position objLink.x use something like objLink.x - viewX1Position. You can determine the view x/y positioning with camera_get_view_x(view_current) I believe.
Logged
Re: GM7/8 Problem with surfaces and views
« Reply #2 on: March 19, 2020, 11:04:18 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 448
I don't believe that's a function in GM 7 or 8, I couldn't find it.
EDIT:
So in the create event I added 2 variables...
Code: [Select]
_x = x-view_xview[0];
_y = y-view_yview[0];
using them in the step event..
Code: [Select]
draw_circle(_x+7,_y+14,16,false);
and drawing the surface in view in draw event
Code: [Select]
draw_surface(surf, view_xview[0], view_yview[0]);
Which is better, this gets me the surface drawn within the view and the "light" is now consistently being drawn in top left corner following the view. Now it just needs to be drawn over the player object.   Using objLink.x-view_xview, objLink.y-view_yview didnt yield results :P
« Last Edit: March 19, 2020, 11:50:19 pm by ZoSo »
Logged
Re: GM7/8 Problem with surfaces and views
« Reply #3 on: March 20, 2020, 07:30:22 am »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1626
Sorry about the function. It's been ages since I programmed in the older versions.

If you can get the light in the top left corner of your surface already then you should be pretty much there already. Are you basing the _x variable on the view top left position? If so, can't you just replace x with objLink.x in that line of code?
« Last Edit: March 20, 2020, 09:41:38 am by Martijn dh »
Logged
Re: GM7/8 Problem with surfaces and views
« Reply #4 on: March 20, 2020, 11:57:41 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 448
I thought so, but apparently not  :huh: I was under the impression that x-view_xview, y-view_yview found the coordinates 0,0 on the view based on the surface. So I thought adding +objLink.x would essentially be 0+links x coordinate. But when I do this, the light circle only uses the players coordinate when you're in the rooms top left corner. The further away you travel from that corner the further away the light circle draws from the player position.
Logged
Re: GM7/8 Problem with surfaces and views
« Reply #5 on: March 20, 2020, 01:34:34 pm »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1626
Is your view 1:1 with the screen? If your view has a different size (for example a view of 240:320 is fitted to your larger monitor screen or visa versa) then you may need to determine the factor between both and then multiple (objLink - _x) by that amount.

Beyond that I'd need some screenshots to better understand your issue.
Logged
Re: GM7/8 Problem with surfaces and views
« Reply #6 on: March 20, 2020, 03:34:54 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 448
Yeah, view is fitted to the screen. I'll try and take a little video when I get home from work.
EDIT: ok here it is
https://i.imgur.com/lRALXFW.mp4
« Last Edit: March 20, 2020, 07:27:59 pm by ZoSo »
Logged
Re: GM7/8 Problem with surfaces and views
« Reply #7 on: March 20, 2020, 09:38:37 pm »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1626
Looks very similar to the problem I was thinking of: whenever your view moves then the light moves a constant factor extra on top of the view's movement itself.

I encountered a similar issue when drawing in the GUI draw event. My view of 320x240 was draw on screen as 640x480 pixels and I had to multiply all positions by the factor between that view and screen size. The reason is that a normal draw event will draw everything on a viewsurface of 320x240 (=the view settings) and gamemaker automatically resizes it for you. But if you draw directly to a surface that is screen sized (640x480) it will not. Also, if you draw in the GUI event you are also actually drawing to a screensized surface (640x480) so the same applies.

I solved my problem by calculating and applying the factor between the screen and view in order to adjust my draw function xposition, yposition, widthFactor and heightFactor. In GM:Studio I was able to calculate the factor with something like this:
Code: [Select]
var factorW = display_get_gui_width()/view_wview[0];
Logged
Re: GM7/8 Problem with surfaces and views
« Reply #8 on: March 20, 2020, 10:11:49 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 448
Edit: Solved.
Code: [Select]
draw_circle(objLink.x+12/2-view_xview,objLink.y+27/2-view_yview,16,false);Thanks for taking the time to help, I really appreciate it!
Keep an eye out for a demo coming up.
« Last Edit: March 21, 2020, 03:25:46 am by ZoSo »
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.327 seconds with 52 queries.

anything