Random Surfaces in SDL?

Started by
6 comments, last by Kylotan 18 years, 4 months ago
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.
Advertisement
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.
Thanks man, i appreciate it :D, though i still don't know to do all of that, but ill find a way
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.
Spippo is magic,is magic!!!!!Oh ho hoo!!!!Spippo is magic
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++
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.
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].
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.

This topic is closed to new replies.

Advertisement