Resources > Coding

GM7/8 Problem with surfaces and views

(1/2) > >>

ZoSo:
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: ---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();
--- End code ---
Step

--- Code: ---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();
   
    } 
--- End code ---
Draw

--- Code: ---if (!surface_exists(surf)) {
    surf = surface_create(302, 243);
} else {
    if (view_current == 0){
        draw_surface(surf, 0, 0);
        }
    }
--- End code ---
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.

Martijn dh:
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.

ZoSo:
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: ---_x = x-view_xview[0];
_y = y-view_yview[0];

--- End code ---
using them in the step event..

--- Code: ---draw_circle(_x+7,_y+14,16,false);

--- End code ---
and drawing the surface in view in draw event

--- Code: ---draw_surface(surf, view_xview[0], view_yview[0]);

--- End code ---
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

Martijn dh:
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?

ZoSo:
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.

Navigation

[0] Message Index

[#] Next page


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



Go to full version