Sign in to follow this  

Random Surfaces in SDL?

This topic is 4394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

There's no such function in SDL. However, you can store SDL_Surface pointers in an array, vector or some other data structure with random access and use rand() to choose an index into that structure to get a random surface.

Share this post


Link to post
Share on other sites
Just place all of your cards in one Image
There are 52 cards, so make an image of 13*4 cards (so you have all the cards of the same color in the same row)

Just make one single surface, like SDL_Surface *cards;
And load that image with the 52 cards in it.

then you just make two integers, like card_col and card_val and make sure you get random numbers for them. For card_col a random number between 0 and 3 and for card_val a number between 0 and 12.

The next thing you have to do is make a rectangle, like SDL_Rect card_rect;
The only thing you have to do now, is to attach the 4 corners of the card in your rect: (you also have to know the width and heights of your cards)

card_rect.top = card_col * card_height;
card_rect.bottom = (card_col + 1) * card_height;
card_rect.left = card_val * card_width;
card_rect.right = (card_val + 1) * card_width;

And now you simply can draw that card with SDL_BlitSurface(), like:

SDL_BlitSurface(cards, &card_rect, Screen, &dest);
(where Screen is the screen surface and dest is the rectangle where you want to place the card on the screen.)



Hope this helps



PS: Make sure that you get every card just once, so that you dont have ace of spades twice and no king of harths, or something like that.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Don't forget the (ol' mighty) great standard template library.

A simple vector should do it.

Create a vector and push back all your cards. Then, randomly shuffle them:

std::random_shuffle(yourVec.begin(), yourVec.end());



C++

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Woops, you wanted to randomly 'choose' a card. Well, you can do that random_shuffle and then always just choose yourVec[0], as that will be different because of random_shuffle. Or, you can use:

std::vector<cards> allCards;

allCards.push_back(...);
...
...

std::random_shuffle(allCards.begin(), allCards.end());

card theCard = allCards[rand() % allCards.size()];



That should be extremely random.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Don't forget the (ol' mighty) great standard template library.

A simple vector should do it.

Create a vector and push back all your cards. Then, randomly shuffle them:
*** Source Snippet Removed ***

C++


Wow, talk about a cool standard function I've never heard of [lol].

Share this post


Link to post
Share on other sites
Quote:
Original post by Lasko
If you are making a card game, is there a code for SDL to randomly choose a card? I only know of rand() choosing numbers.


Remember that SDL is just for graphics (in this case). You should have a way of mapping the numbers 0-51 to your surfaces, or parts of a surface, or whatever, but the random part should have nothing to do with SDL. All your card manipulation apart from drawing should just use the card number. When you separate everything out like this, life becomes a lot simpler.

Share this post


Link to post
Share on other sites

This topic is 4394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this