• 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 draw sprites inside rectangle only

S

Stratos.la

Guest
i have a carousel of sprites that you can swipe left or right using a ds list. i have those inside a sprite object ( like a table ) . i have like 20 images i want to use but they get drawn across all of the screen and i want them to just get drawn inside the table! any ideas?
 
S

Stratos.la

Guest
well there is not much to show!

GML:
elements = ds_list_create();

ds_list_add(elements, sCardArcher);
ds_list_add(elements, sCardMage);
ds_list_add(elements, CardsIndex);
ds_list_add(elements, sCardMage);
ds_list_add(elements, CardsIndex);

selected_card = 0;

number_of_cards = ds_list_size(elements);
card_distance = 170
move_value =  -card_distance;
deck_x = 480;
deck_y = 1000;
and this is my draw event
GML:
for(i = 0; i < number_of_cards; i++)
    {
    var sc = 1;
    var alp = 1;
    var dst = abs(move_value+i*card_distance);

    draw_sprite_ext(elements[| i], image_index, move_value + deck_x + i*card_distance, deck_y, image_xscale*sc, image_yscale*sc, image_angle, c_white, alp);
}
this draws all of my cards in the screen . i want them to only draw in a specified rectangle! i though about surfaces to just draw them inside those coordinates but i dont really know much about them!
 
S

Stratos.la

Guest
i did try surfaces and ended up with this but i cant get it to work! . i have a few lines commented out trying to make it work. the whole idea is that the sprites inside the for loop are only visible inside that rectangle
GML:
draw_self()

if (!surface_exists(surf))
{
    surf = surface_create(550,180);
}
surface_set_target(surf);

draw_rectangle(895,223,895,735,1)
//gpu_set_blendmode(bm_subtract);

draw_set_colour(c_black)
//draw_set_alpha(1)

//draw_set_colour(c_black)
//draw_set_alpha(1)
for(i = 0; i < number_of_cards; i++)
    {
    var sc = 1;
    var alp = 1;
    var dst = abs(move_value+i*card_distance);

    draw_sprite_ext(elements[| i], image_index, move_value + deck_x + i*card_distance, deck_y, image_xscale*sc, image_yscale*sc, image_angle, c_white, alp);
}

//draw_set_alpha(1)

//gpu_set_blendmode(bm_normal)

surface_reset_target();
draw_surface(surf,200,900)
 
S

Stratos.la

Guest
the cards are drawn in the middle of the deck ( their x/y ) and depending on the ds list they go all the way to the right. outside i draw the rest of the game screen.
i thought id put a rectangle with the dimensions i want acting as a window and show the cards inside there!

cards.png

that card deck should only show 3 cards. the rest should be invisible due to the surface, well if i had made it right.
 

Xer0botXer0

Senpai
So you want to scale down the cards ? Im actually working on something similar which has background sprites and sprites of different dimensions on the foreground that will have to be scaled down.. although I worry about scaling it's really about reducing the workload from creating multiple sprites of various dimensions.

It actually looks kinda cool with the bowmans bow outside of the card.
From what simon mentioned you could scale it down to card size.. but that wont remove the issue with the foreground being offset..

This would then be the issue since the y coordinate is good according to you.

"move_value + deck_x + i*card_distance"

so it's incrementing more than it should every iteration.

But why aren't you drawing the foreground sprites with the background objects ? in sets rather than the background cards seperately to the foreground sprites ?
 

TailBit

Member
If there are only sprites, then you can use some trickery with draw_sprite_part even for smooth scrolling.

You can also do scrolling with nonmoving cards, so they suddenly change position as you scroll, but if you try to resort to surfaces then I guess this isn't what you want.
 
S

Stratos.la

Guest
well, i don't know to be honest im still working on it! my idea was to well create a rectangle area acting as a surface ( just the inside part of the deck and up until the cards top position )and since the cards are spawned inside it the rest won't be visible. i will be making it swipeable later on. i also have a hidden button in the center card that once you swipe you can select the card and so stuff. the issue now is as you can see that it doesn't look good like that. i have used the bm_substract before on a previous game but i just cant get it working in this one. I was hoping that since the surface would be a fixed size anything inside that would be shown only. but as im trying to do that i either disable everything including the deck or draw the whole screen black.cards.png

the surface in red is an actual representation of the size of the rectangle i tried to draw to act like a window
 

Xer0botXer0

Senpai
Oh i see.. so the first three cards are the available ones while the ones there after are hidden until the next round or something ?
how about an if statement..

GML:
for(i = 0; i < number_of_cards; i++)

    {

    var sc = 1;
    alp = 1;
    var dst = abs(move_value+i*card_distance);

    if deck_x < visible_area_x
   {
    draw_sprite_ext(elements[| i], image_index, move_value + deck_x + i*card_distance, deck_y, image_xscale*sc, image_yscale*sc, image_angle, c_white,alp);
   }
}
I assume deck_x is the x coordinate for the card behind a character..
so visible_area_x will be the x coordinate right after the third card.. that way the sprites there after wont even draw..


but you probably want them to kind of fade in ? when a card becomes visible ? not sure although with surfaces that may be what would have happened. In any case either the shader or surface idea could work and would only be necessary for the card about to become visible.
 
S

Stratos.la

Guest
yes deck_x and deck_y is the center of the deck. these two variables are for the cards. the center one is the one that can be activated. i will try your suggestion! is giving me a headache since this morning!
 
S

Stratos.la

Guest
made it work like this! thank you PhallicSpleen !
GML:
draw_self();

if (!surface_exists(clip_surface)) {
    clip_surface = surface_create(clip_width, clip_height);
}
// clear and start drawing to surface:
surface_set_target(clip_surface);
draw_clear_alpha(c_black, 0);

for(i=0; i<number_of_cards; i++)
{ 
  var sc = 1;
  var alp = 1;
  var dst = abs(move_value+i*card_distance);
  draw_sprite_ext(elements[| i], image_index, move_value + (middle_x - clip_x) + i*card_distance, (middle_y-clip_y), image_xscale*sc, image_yscale*sc, image_angle, c_white, alp);
}
// finish and draw the surface itself:
surface_reset_target();
draw_surface(clip_surface, clip_x, clip_y);
GML:
clip_surface = -1

clip_width = 545;
clip_height = 300;
clip_x = 265;
clip_y = 900;
 
Top