diff --git a/Hearts/card.cpp b/Hearts/card.cpp index b0c5e7b..3162d6e 100644 --- a/Hearts/card.cpp +++ b/Hearts/card.cpp @@ -1,7 +1,7 @@ /* File Name: card.cpp * Author: Kayne Ruse - * Date (dd/mm/yyyy): 05/06/2011 - * Copyright: (c) Kayne Ruse 2011, 2012 + * Date (dd/mm/yyyy): 21/01/2013 + * Copyright: (c) Kayne Ruse 2011, 2012, 2013 * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages @@ -23,50 +23,50 @@ * distribution. * * Description: - * Designed for Project Hearts, 4th try. + * A basic playing card. This also has the graphics built in. */ #include "card.h" -Card::Card(int _suit, int _rank, SDL_Surface* _faceSurface, SDL_Surface* _backSurface): - Image(_faceSurface), - suit(_suit), - rank(_rank), - faceSurface(_faceSurface), - backSurface(_backSurface) +#include + +//------------------------- +//Public access members +//------------------------- + +Card::Card(int s, int r, SDL_Surface* faceSurface, SDL_Surface* backSurface): + suit(s), + rank(r) { - SetFace(UP); - SetWidth(73); - SetHeight(98); + //images + face.SetSurface(faceSurface); + back.SetSurface(backSurface); + + //set the face's clip + face.SetClipX((rank - 1) * 73); + face.SetClipY((suit - 1) * 98); + face.SetClipW(73); + face.SetClipH(98); + + SetFaceState(UP); + + x = 0; + y = 0; + next = NULL; } -int Card::Suit() { +//------------------------- +//Card game members +//------------------------- + +int Card::GetSuit() { return suit; } -int Card::Rank() { +int Card::GetRank() { return rank; } -int Card::SetFace(int _face) { - face = _face; - if (face == UP) { - SetSurface(faceSurface); - SetClipX((rank - 1) * 73); - SetClipY((suit - 1) * 98); - } - else { - SetSurface(backSurface); - SetClipX(0); - SetClipY(0); - } - return face; -} - -int Card::GetFace() { - return face; -} - Card* Card::SetNext(Card* _next) { return next = _next; } @@ -76,11 +76,10 @@ Card* Card::GetNext() { } bool Card::operator>(Card& card) { - if (Suit() > card.Suit()) + if (suit > card.suit) return true; - if (Suit() == card.Suit() && - Rank() > card.Rank()) + if (suit == card.suit && rank > card.rank) return true; return false; @@ -89,3 +88,45 @@ bool Card::operator>(Card& card) { bool Card::operator<(Card& card) { return card > *this; } + +//------------------------- +//Graphics members +//------------------------- + +int Card::SetFaceState(int face) { + if (face != UP && face != DOWN) + throw(std::invalid_argument("Unknown face value")); + return faceState = face; +} + +int Card::GetFaceState() { + return faceState; +} + +void Card::SetPos(Sint16 newX, Sint16 newY) { + x = newX; + y = newY; +} + +Sint16 Card::SetX(Sint16 newX) { + return x = newX; +} + +Sint16 Card::SetY(Sint16 newY) { + return y = newY; +} + +Sint16 Card::GetX() { + return x; +} + +Sint16 Card::GetY() { + return y; +} + +void Card::DrawTo(SDL_Surface* const dest) { + if (faceState == UP) + face.DrawTo(dest, x, y); + else if (faceState == DOWN) + back.DrawTo(dest, x, y); +} diff --git a/Hearts/card.h b/Hearts/card.h index 0858038..1253423 100644 --- a/Hearts/card.h +++ b/Hearts/card.h @@ -1,7 +1,7 @@ /* File Name: card.h * Author: Kayne Ruse - * Date (dd/mm/yyyy): 05/06/2011 - * Copyright: (c) Kayne Ruse 2011, 2012 + * Date (dd/mm/yyyy): 21/01/2013 + * Copyright: (c) Kayne Ruse 2011, 2012, 2013 * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages @@ -23,25 +23,25 @@ * distribution. * * Description: - * Designed for Project Hearts, 4th try. + * A basic playing card. This also has the graphics built in. */ #ifndef KR_CARD_H_ #define KR_CARD_H_ + #include "SDL.h" + #include "image.h" #define ISCARD(CARD,RANK,SUIT) (CARD->Suit() == Card::SUIT && CARD->Rank() == Card::RANK) -class Card : public KAGE::Image { +class Card { public: /* Public access members */ Card(int suit, int rank, SDL_Surface* faceSurface, SDL_Surface* backSurface); - int Suit(); - int Rank(); - - int SetFace(int face); - int GetFace(); + /* Card game memebers */ + int GetSuit(); + int GetRank(); Card* SetNext(Card* next); Card* GetNext(); @@ -49,6 +49,19 @@ public: bool operator>(Card&); bool operator<(Card&); + /* Graphics members */ + int SetFaceState(int face); + int GetFaceState(); + + void SetPos(Sint16 x, Sint16 y); + Sint16 SetX(Sint16); + Sint16 SetY(Sint16); + Sint16 GetX(); + Sint16 GetY(); + + void DrawTo(SDL_Surface* const); + + /* Macros */ enum { UP = 1, DOWN }; @@ -63,15 +76,17 @@ public: TEN=9, JACK=10, QUEEN=11, KING=12, ACE=13 }; protected: - /* Protected access members */ + /* Card game members */ const int suit; const int rank; - int face; //TODO: "facestate" - SDL_Surface* faceSurface; //TODO: Image - SDL_Surface* backSurface; - Card* next; + + /* Graphics members */ + int faceState; + Image face; + Image back; + Sint16 x, y; }; #endif diff --git a/Hearts/card_list.cpp b/Hearts/card_list.cpp index 57f380d..d5329c4 100644 --- a/Hearts/card_list.cpp +++ b/Hearts/card_list.cpp @@ -1,7 +1,7 @@ /* File Name: card_list.cpp * Author: Kayne Ruse - * Date (dd/mm/yyyy): 05/06/2011 - * Copyright: (c) Kayne Ruse 2011, 2012 + * Date (dd/mm/yyyy): 21/01/2013 + * Copyright: (c) Kayne Ruse 2011, 2012, 2013 * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages @@ -23,12 +23,11 @@ * distribution. * * Description: - * Designed for Project Hearts, 4th try. + * A linked list of playing cards. */ -#include -#include #include "card_list.h" -using namespace std; + +#include //------------------------- //Public access members @@ -144,8 +143,9 @@ Card* CardList::PassSlab(int first, int count) { //------------------------- void CardList::Shuffle() { + //TODO: rewrite this to avoid continuous calls to Size() + //New version - srand((unsigned int)time(NULL)); //TODO: Remove this, initiate randomization in the Scene (and shuffle 3 times ;) ) iterator shuffleHead = NULL; iterator prev = NULL; @@ -207,7 +207,7 @@ void CardList::SortRank() { while(it != NULL && Size()) { //first card - if (it->Rank() > Read()->Rank()) { + if (it->GetRank() > Read()->GetRank()) { //insert the new card at the start of the list it = Pass(); it->SetNext(sortHead); @@ -215,7 +215,7 @@ void CardList::SortRank() { continue; } - while (it->GetNext() != NULL && Read()->Rank() > it->GetNext()->Rank()) { + while (it->GetNext() != NULL && Read()->GetRank() > it->GetNext()->GetRank()) { //place the iterator just before the position to insert the card it = it->GetNext(); } @@ -243,7 +243,7 @@ Card* CardList::Head() { return headCard; } -void CardList::DrawAll(SDL_Surface* dest) { +void CardList::DrawTo(SDL_Surface* dest) { for (iterator it = headCard;it != NULL;it = it->GetNext()) - it->Draw(dest); + it->DrawTo(dest); } diff --git a/Hearts/card_list.h b/Hearts/card_list.h index ce6b4e4..c9b58c0 100644 --- a/Hearts/card_list.h +++ b/Hearts/card_list.h @@ -1,7 +1,7 @@ /* File Name: card_list.h * Author: Kayne Ruse - * Date (dd/mm/yyyy): 05/06/2011 - * Copyright: (c) Kayne Ruse 2011, 2012 + * Date (dd/mm/yyyy): 21/01/2013 + * Copyright: (c) Kayne Ruse 2011, 2012, 2013 * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages @@ -23,7 +23,7 @@ * distribution. * * Description: - * Designed for Project Hearts, 4th try. + * A linked list of playing cards. */ #ifndef KR_CARDLIST_H_ #define KR_CARDLIST_H_ @@ -47,7 +47,7 @@ public: int Size(); Card* Head(); - void DrawAll(SDL_Surface*); + void DrawTo(SDL_Surface*); typedef Card* iterator; private: diff --git a/Hearts/deck.cpp b/Hearts/deck.cpp index de892d9..734a4b3 100644 --- a/Hearts/deck.cpp +++ b/Hearts/deck.cpp @@ -1,7 +1,7 @@ /* File Name: deck.cpp * Author: Kayne Ruse - * Date (dd/mm/yyyy): 05/06/2011 - * Copyright: (c) Kayne Ruse 2011, 2012 + * Date (dd/mm/yyyy): 21/01/2013 + * Copyright: (c) Kayne Ruse 2011, 2012, 2013 * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages @@ -23,55 +23,43 @@ * distribution. * * Description: - * Designed for Project Hearts, 4th try. + * Create and destroy the cards in the game, and report if any are missing. */ -#include #include "deck.h" -using namespace std; + +#include Deck::Deck() { - faceSurface = NULL; - backSurface = NULL; + count = 0; } Deck::~Deck() { - DeleteAll(); - if (faceSurface != NULL) - SDL_FreeSurface(faceSurface); - if (backSurface != NULL) - SDL_FreeSurface(backSurface); + Quit(); } void Deck::Init(const char* faceName, const char* backName) { - //Create and setup the surfaces, with error checking - faceSurface = SDL_LoadBMP(faceName); + face.LoadSurface(faceName); + back.LoadSurface(backName); - if (faceSurface == NULL) { - cerr << "Error in Deck::Init(faceName,backName)" << endl; - cerr << "SDL_LoadBMP() returned NULL" << endl; - cerr << "faceName: " << faceName << endl; + //exit if there are already cards in the game + if (count != 0) return; - } - - backSurface = SDL_LoadBMP(backName); - - if (backSurface == NULL) { - cerr << "Error in Deck::Init(faceName,backName)" << endl; - cerr << "SDL_LoadBMP() returned NULL" << endl; - cerr << "backName: " << backName << endl; - return; - } - - SDL_SetColorKey(faceSurface,SDL_SRCCOLORKEY,SDL_MapRGB(faceSurface->format,255,0,255)); - SDL_SetColorKey(backSurface,SDL_SRCCOLORKEY,SDL_MapRGB(backSurface->format,255,0,255)); //Create the cards - for (int s = 1; s <= 4; s++) - for (int r = 1; r <= 13; r++) - Receive(new Card(s,r,faceSurface,backSurface)); + for (int s = 1; s <= 4; s++) { + for (int r = 1; r <= 13; r++) { + Receive(new Card(s,r,face.GetSurface(),back.GetSurface())); + count++; + } + } } -void Deck::DeleteAll() { - while(Head() != NULL) +void Deck::Quit() { + while(Head() != NULL) { delete Pass(); + count--; + } + + if (count != 0) + throw(std::runtime_error("Memory leak: Some cards are unnacounted for")); } diff --git a/Hearts/deck.h b/Hearts/deck.h index 9cf57f5..540eda1 100644 --- a/Hearts/deck.h +++ b/Hearts/deck.h @@ -1,7 +1,7 @@ /* File Name: deck.h * Author: Kayne Ruse - * Date (dd/mm/yyyy): 05/06/2011 - * Copyright: (c) Kayne Ruse 2011, 2012 + * Date (dd/mm/yyyy): 21/01/2013 + * Copyright: (c) Kayne Ruse 2011, 2012, 2013 * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages @@ -23,11 +23,15 @@ * distribution. * * Description: - * Designed for Project Hearts, 4th try. + * Create and destroy the cards in the game, and report if any are missing. */ #ifndef KR_DECK_H_ #define KR_DECK_H_ + #include "SDL.h" + +#include "image.h" + #include "card_list.h" class Deck : public CardList { @@ -37,11 +41,13 @@ public: ~Deck(); void Init(const char* faceName, const char* backName); - void DeleteAll(); + void Quit(); private: /* Private access members */ - SDL_Surface* faceSurface; - SDL_Surface* backSurface; + Image face; + Image back; + + int count; }; #endif