mirror of
https://github.com/Ratstail91/Hearts.git
synced 2025-11-29 02:24:28 +11:00
Revamped the card system to use the newer graphics system
This commit is contained in:
113
Hearts/card.cpp
113
Hearts/card.cpp
@@ -1,7 +1,7 @@
|
|||||||
/* File Name: card.cpp
|
/* File Name: card.cpp
|
||||||
* Author: Kayne Ruse
|
* Author: Kayne Ruse
|
||||||
* Date (dd/mm/yyyy): 05/06/2011
|
* Date (dd/mm/yyyy): 21/01/2013
|
||||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
* Copyright: (c) Kayne Ruse 2011, 2012, 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -23,50 +23,50 @@
|
|||||||
* distribution.
|
* distribution.
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Designed for Project Hearts, 4th try.
|
* A basic playing card. This also has the graphics built in.
|
||||||
*/
|
*/
|
||||||
#include "card.h"
|
#include "card.h"
|
||||||
|
|
||||||
Card::Card(int _suit, int _rank, SDL_Surface* _faceSurface, SDL_Surface* _backSurface):
|
#include <stdexcept>
|
||||||
Image(_faceSurface),
|
|
||||||
suit(_suit),
|
//-------------------------
|
||||||
rank(_rank),
|
//Public access members
|
||||||
faceSurface(_faceSurface),
|
//-------------------------
|
||||||
backSurface(_backSurface)
|
|
||||||
|
Card::Card(int s, int r, SDL_Surface* faceSurface, SDL_Surface* backSurface):
|
||||||
|
suit(s),
|
||||||
|
rank(r)
|
||||||
{
|
{
|
||||||
SetFace(UP);
|
//images
|
||||||
SetWidth(73);
|
face.SetSurface(faceSurface);
|
||||||
SetHeight(98);
|
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;
|
next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Card::Suit() {
|
//-------------------------
|
||||||
|
//Card game members
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
int Card::GetSuit() {
|
||||||
return suit;
|
return suit;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Card::Rank() {
|
int Card::GetRank() {
|
||||||
return rank;
|
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) {
|
Card* Card::SetNext(Card* _next) {
|
||||||
return next = _next;
|
return next = _next;
|
||||||
}
|
}
|
||||||
@@ -76,11 +76,10 @@ Card* Card::GetNext() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Card::operator>(Card& card) {
|
bool Card::operator>(Card& card) {
|
||||||
if (Suit() > card.Suit())
|
if (suit > card.suit)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (Suit() == card.Suit() &&
|
if (suit == card.suit && rank > card.rank)
|
||||||
Rank() > card.Rank())
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -89,3 +88,45 @@ bool Card::operator>(Card& card) {
|
|||||||
bool Card::operator<(Card& card) {
|
bool Card::operator<(Card& card) {
|
||||||
return card > *this;
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/* File Name: card.h
|
/* File Name: card.h
|
||||||
* Author: Kayne Ruse
|
* Author: Kayne Ruse
|
||||||
* Date (dd/mm/yyyy): 05/06/2011
|
* Date (dd/mm/yyyy): 21/01/2013
|
||||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
* Copyright: (c) Kayne Ruse 2011, 2012, 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -23,25 +23,25 @@
|
|||||||
* distribution.
|
* distribution.
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Designed for Project Hearts, 4th try.
|
* A basic playing card. This also has the graphics built in.
|
||||||
*/
|
*/
|
||||||
#ifndef KR_CARD_H_
|
#ifndef KR_CARD_H_
|
||||||
#define KR_CARD_H_
|
#define KR_CARD_H_
|
||||||
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
|
|
||||||
#include "image.h"
|
#include "image.h"
|
||||||
|
|
||||||
#define ISCARD(CARD,RANK,SUIT) (CARD->Suit() == Card::SUIT && CARD->Rank() == Card::RANK)
|
#define ISCARD(CARD,RANK,SUIT) (CARD->Suit() == Card::SUIT && CARD->Rank() == Card::RANK)
|
||||||
|
|
||||||
class Card : public KAGE::Image {
|
class Card {
|
||||||
public:
|
public:
|
||||||
/* Public access members */
|
/* Public access members */
|
||||||
Card(int suit, int rank, SDL_Surface* faceSurface, SDL_Surface* backSurface);
|
Card(int suit, int rank, SDL_Surface* faceSurface, SDL_Surface* backSurface);
|
||||||
|
|
||||||
int Suit();
|
/* Card game memebers */
|
||||||
int Rank();
|
int GetSuit();
|
||||||
|
int GetRank();
|
||||||
int SetFace(int face);
|
|
||||||
int GetFace();
|
|
||||||
|
|
||||||
Card* SetNext(Card* next);
|
Card* SetNext(Card* next);
|
||||||
Card* GetNext();
|
Card* GetNext();
|
||||||
@@ -49,6 +49,19 @@ public:
|
|||||||
bool operator>(Card&);
|
bool operator>(Card&);
|
||||||
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 {
|
enum {
|
||||||
UP = 1, DOWN
|
UP = 1, DOWN
|
||||||
};
|
};
|
||||||
@@ -63,15 +76,17 @@ public:
|
|||||||
TEN=9, JACK=10, QUEEN=11, KING=12, ACE=13
|
TEN=9, JACK=10, QUEEN=11, KING=12, ACE=13
|
||||||
};
|
};
|
||||||
protected:
|
protected:
|
||||||
/* Protected access members */
|
/* Card game members */
|
||||||
const int suit;
|
const int suit;
|
||||||
const int rank;
|
const int rank;
|
||||||
|
|
||||||
int face; //TODO: "facestate"
|
|
||||||
SDL_Surface* faceSurface; //TODO: Image
|
|
||||||
SDL_Surface* backSurface;
|
|
||||||
|
|
||||||
Card* next;
|
Card* next;
|
||||||
|
|
||||||
|
/* Graphics members */
|
||||||
|
int faceState;
|
||||||
|
Image face;
|
||||||
|
Image back;
|
||||||
|
Sint16 x, y;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/* File Name: card_list.cpp
|
/* File Name: card_list.cpp
|
||||||
* Author: Kayne Ruse
|
* Author: Kayne Ruse
|
||||||
* Date (dd/mm/yyyy): 05/06/2011
|
* Date (dd/mm/yyyy): 21/01/2013
|
||||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
* Copyright: (c) Kayne Ruse 2011, 2012, 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -23,12 +23,11 @@
|
|||||||
* distribution.
|
* distribution.
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Designed for Project Hearts, 4th try.
|
* A linked list of playing cards.
|
||||||
*/
|
*/
|
||||||
#include <iostream>
|
|
||||||
#include <time.h>
|
|
||||||
#include "card_list.h"
|
#include "card_list.h"
|
||||||
using namespace std;
|
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Public access members
|
//Public access members
|
||||||
@@ -144,8 +143,9 @@ Card* CardList::PassSlab(int first, int count) {
|
|||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void CardList::Shuffle() {
|
void CardList::Shuffle() {
|
||||||
|
//TODO: rewrite this to avoid continuous calls to Size()
|
||||||
|
|
||||||
//New version
|
//New version
|
||||||
srand((unsigned int)time(NULL)); //TODO: Remove this, initiate randomization in the Scene (and shuffle 3 times ;) )
|
|
||||||
iterator shuffleHead = NULL;
|
iterator shuffleHead = NULL;
|
||||||
iterator prev = NULL;
|
iterator prev = NULL;
|
||||||
|
|
||||||
@@ -207,7 +207,7 @@ void CardList::SortRank() {
|
|||||||
|
|
||||||
while(it != NULL && Size()) {
|
while(it != NULL && Size()) {
|
||||||
//first card
|
//first card
|
||||||
if (it->Rank() > Read()->Rank()) {
|
if (it->GetRank() > Read()->GetRank()) {
|
||||||
//insert the new card at the start of the list
|
//insert the new card at the start of the list
|
||||||
it = Pass();
|
it = Pass();
|
||||||
it->SetNext(sortHead);
|
it->SetNext(sortHead);
|
||||||
@@ -215,7 +215,7 @@ void CardList::SortRank() {
|
|||||||
continue;
|
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
|
//place the iterator just before the position to insert the card
|
||||||
it = it->GetNext();
|
it = it->GetNext();
|
||||||
}
|
}
|
||||||
@@ -243,7 +243,7 @@ Card* CardList::Head() {
|
|||||||
return headCard;
|
return headCard;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CardList::DrawAll(SDL_Surface* dest) {
|
void CardList::DrawTo(SDL_Surface* dest) {
|
||||||
for (iterator it = headCard;it != NULL;it = it->GetNext())
|
for (iterator it = headCard;it != NULL;it = it->GetNext())
|
||||||
it->Draw(dest);
|
it->DrawTo(dest);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/* File Name: card_list.h
|
/* File Name: card_list.h
|
||||||
* Author: Kayne Ruse
|
* Author: Kayne Ruse
|
||||||
* Date (dd/mm/yyyy): 05/06/2011
|
* Date (dd/mm/yyyy): 21/01/2013
|
||||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
* Copyright: (c) Kayne Ruse 2011, 2012, 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
* distribution.
|
* distribution.
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Designed for Project Hearts, 4th try.
|
* A linked list of playing cards.
|
||||||
*/
|
*/
|
||||||
#ifndef KR_CARDLIST_H_
|
#ifndef KR_CARDLIST_H_
|
||||||
#define KR_CARDLIST_H_
|
#define KR_CARDLIST_H_
|
||||||
@@ -47,7 +47,7 @@ public:
|
|||||||
int Size();
|
int Size();
|
||||||
Card* Head();
|
Card* Head();
|
||||||
|
|
||||||
void DrawAll(SDL_Surface*);
|
void DrawTo(SDL_Surface*);
|
||||||
|
|
||||||
typedef Card* iterator;
|
typedef Card* iterator;
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/* File Name: deck.cpp
|
/* File Name: deck.cpp
|
||||||
* Author: Kayne Ruse
|
* Author: Kayne Ruse
|
||||||
* Date (dd/mm/yyyy): 05/06/2011
|
* Date (dd/mm/yyyy): 21/01/2013
|
||||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
* Copyright: (c) Kayne Ruse 2011, 2012, 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -23,55 +23,43 @@
|
|||||||
* distribution.
|
* distribution.
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Designed for Project Hearts, 4th try.
|
* Create and destroy the cards in the game, and report if any are missing.
|
||||||
*/
|
*/
|
||||||
#include <iostream>
|
|
||||||
#include "deck.h"
|
#include "deck.h"
|
||||||
using namespace std;
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
Deck::Deck() {
|
Deck::Deck() {
|
||||||
faceSurface = NULL;
|
count = 0;
|
||||||
backSurface = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Deck::~Deck() {
|
Deck::~Deck() {
|
||||||
DeleteAll();
|
Quit();
|
||||||
if (faceSurface != NULL)
|
|
||||||
SDL_FreeSurface(faceSurface);
|
|
||||||
if (backSurface != NULL)
|
|
||||||
SDL_FreeSurface(backSurface);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Deck::Init(const char* faceName, const char* backName) {
|
void Deck::Init(const char* faceName, const char* backName) {
|
||||||
//Create and setup the surfaces, with error checking
|
face.LoadSurface(faceName);
|
||||||
faceSurface = SDL_LoadBMP(faceName);
|
back.LoadSurface(backName);
|
||||||
|
|
||||||
if (faceSurface == NULL) {
|
//exit if there are already cards in the game
|
||||||
cerr << "Error in Deck::Init(faceName,backName)" << endl;
|
if (count != 0)
|
||||||
cerr << "SDL_LoadBMP() returned NULL" << endl;
|
|
||||||
cerr << "faceName: " << faceName << endl;
|
|
||||||
return;
|
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
|
//Create the cards
|
||||||
for (int s = 1; s <= 4; s++)
|
for (int s = 1; s <= 4; s++) {
|
||||||
for (int r = 1; r <= 13; r++)
|
for (int r = 1; r <= 13; r++) {
|
||||||
Receive(new Card(s,r,faceSurface,backSurface));
|
Receive(new Card(s,r,face.GetSurface(),back.GetSurface()));
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Deck::DeleteAll() {
|
void Deck::Quit() {
|
||||||
while(Head() != NULL)
|
while(Head() != NULL) {
|
||||||
delete Pass();
|
delete Pass();
|
||||||
|
count--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count != 0)
|
||||||
|
throw(std::runtime_error("Memory leak: Some cards are unnacounted for"));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/* File Name: deck.h
|
/* File Name: deck.h
|
||||||
* Author: Kayne Ruse
|
* Author: Kayne Ruse
|
||||||
* Date (dd/mm/yyyy): 05/06/2011
|
* Date (dd/mm/yyyy): 21/01/2013
|
||||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
* Copyright: (c) Kayne Ruse 2011, 2012, 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -23,11 +23,15 @@
|
|||||||
* distribution.
|
* distribution.
|
||||||
*
|
*
|
||||||
* Description:
|
* 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_
|
#ifndef KR_DECK_H_
|
||||||
#define KR_DECK_H_
|
#define KR_DECK_H_
|
||||||
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
|
|
||||||
|
#include "image.h"
|
||||||
|
|
||||||
#include "card_list.h"
|
#include "card_list.h"
|
||||||
|
|
||||||
class Deck : public CardList {
|
class Deck : public CardList {
|
||||||
@@ -37,11 +41,13 @@ public:
|
|||||||
~Deck();
|
~Deck();
|
||||||
|
|
||||||
void Init(const char* faceName, const char* backName);
|
void Init(const char* faceName, const char* backName);
|
||||||
void DeleteAll();
|
void Quit();
|
||||||
private:
|
private:
|
||||||
/* Private access members */
|
/* Private access members */
|
||||||
SDL_Surface* faceSurface;
|
Image face;
|
||||||
SDL_Surface* backSurface;
|
Image back;
|
||||||
|
|
||||||
|
int count;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user