mirror of
https://github.com/Ratstail91/Hearts.git
synced 2025-11-29 02:24:28 +11:00
Imported stable version of my old Hearts game
Included the (now open source) KAGE files Slightly modified function calls for compatability Added personal note to README.md Included VC++ project in the repo for simplicity
This commit is contained in:
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
#Editor generated files
|
||||
#*.sln
|
||||
#*.vcproj
|
||||
*.suo
|
||||
*.ncb
|
||||
*.user
|
||||
|
||||
#Directories
|
||||
Release/
|
||||
Debug/
|
||||
20
Hearts.sln
Normal file
20
Hearts.sln
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 10.00
|
||||
# Visual C++ Express 2008
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Hearts", "Hearts\Hearts.vcproj", "{A3DAA0E4-CC3F-43F4-BCD1-348A090F753B}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Win32 = Debug|Win32
|
||||
Release|Win32 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{A3DAA0E4-CC3F-43F4-BCD1-348A090F753B}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{A3DAA0E4-CC3F-43F4-BCD1-348A090F753B}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{A3DAA0E4-CC3F-43F4-BCD1-348A090F753B}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{A3DAA0E4-CC3F-43F4-BCD1-348A090F753B}.Release|Win32.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
346
Hearts/Hearts.vcproj
Normal file
346
Hearts/Hearts.vcproj
Normal file
@@ -0,0 +1,346 @@
|
||||
<?xml version="1.0" encoding="Windows-1252"?>
|
||||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="9.00"
|
||||
Name="Hearts"
|
||||
ProjectGUID="{A3DAA0E4-CC3F-43F4-BCD1-348A090F753B}"
|
||||
RootNamespace="Hearts"
|
||||
Keyword="Win32Proj"
|
||||
TargetFrameworkVersion="196613"
|
||||
>
|
||||
<Platforms>
|
||||
<Platform
|
||||
Name="Win32"
|
||||
/>
|
||||
</Platforms>
|
||||
<ToolFiles>
|
||||
</ToolFiles>
|
||||
<Configurations>
|
||||
<Configuration
|
||||
Name="Debug|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="1"
|
||||
CharacterSet="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="0"
|
||||
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
|
||||
MinimalRebuild="true"
|
||||
BasicRuntimeChecks="3"
|
||||
RuntimeLibrary="3"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="3"
|
||||
DebugInformationFormat="4"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
LinkIncremental="2"
|
||||
GenerateDebugInformation="true"
|
||||
SubSystem="1"
|
||||
TargetMachine="1"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Release|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="1"
|
||||
CharacterSet="1"
|
||||
WholeProgramOptimization="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="2"
|
||||
EnableIntrinsicFunctions="true"
|
||||
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
|
||||
RuntimeLibrary="2"
|
||||
EnableFunctionLevelLinking="true"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="3"
|
||||
DebugInformationFormat="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
AdditionalDependencies="SDL.lib SDLmain.lib SDL_mixer.lib"
|
||||
LinkIncremental="1"
|
||||
GenerateDebugInformation="false"
|
||||
SubSystem="1"
|
||||
OptimizeReferences="2"
|
||||
EnableCOMDATFolding="2"
|
||||
TargetMachine="1"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
</Configurations>
|
||||
<References>
|
||||
</References>
|
||||
<Files>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
|
||||
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\animation.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\audio_manager.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\base_engine.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\card.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\card_list.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\deck.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\hearts_engine.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\image.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\image_manager.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\music.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\network_manager.cpp"
|
||||
>
|
||||
<FileConfiguration
|
||||
Name="Release|Win32"
|
||||
ExcludedFromBuild="true"
|
||||
>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\object.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\player.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\player_ai.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\player_user.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\sound.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\table.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Header Files"
|
||||
Filter="h;hpp;hxx;hm;inl;inc;xsd"
|
||||
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\animation.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\audio_manager.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\base_engine.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\card.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\card_list.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\deck.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\hearts_engine.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\image.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\image_manager.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\music.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\named_manager.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\network_manager.h"
|
||||
>
|
||||
<FileConfiguration
|
||||
Name="Release|Win32"
|
||||
ExcludedFromBuild="true"
|
||||
>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\object.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\player.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\player_ai.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\player_user.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\sound.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\table.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Resource Files"
|
||||
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
|
||||
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
|
||||
>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
</VisualStudioProject>
|
||||
85
Hearts/animation.cpp
Normal file
85
Hearts/animation.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
/* File Name: animation.cpp
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 9/7/2011
|
||||
* Version: 1, Stable
|
||||
* Description: The Animation class, animates spritesheets.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#include "animation.h"
|
||||
using namespace KAGE;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
Animation::Animation(int _stateCount, int _frameCount, float _speed) {
|
||||
stateCount = _stateCount;
|
||||
frameCount = _frameCount;
|
||||
state = 0;
|
||||
frame = 0;
|
||||
speed = _speed;
|
||||
}
|
||||
|
||||
void Animation::Process(int delta) {
|
||||
frame += delta * speed + speed;
|
||||
|
||||
if (frame >= frameCount)
|
||||
frame = 0;
|
||||
|
||||
SetCell();
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Accessors and mutators
|
||||
//-------------------------
|
||||
|
||||
int Animation::SetStateCount(int _stateCount) {
|
||||
frame = 0;
|
||||
return stateCount = _stateCount;
|
||||
}
|
||||
|
||||
int Animation::SetFrameCount(int _frameCount) {
|
||||
frame = 0;
|
||||
return frameCount = _frameCount;
|
||||
}
|
||||
|
||||
int Animation::SetState(int _state) {
|
||||
if (_state >= stateCount)
|
||||
return -1;
|
||||
|
||||
frame = 0;
|
||||
return state = _state;
|
||||
}
|
||||
|
||||
float Animation::SetFrame(float _frame) {
|
||||
if (_frame >= frameCount)
|
||||
return -1;
|
||||
|
||||
return frame = _frame;
|
||||
}
|
||||
|
||||
float Animation::SetSpeed(float _speed) {
|
||||
frame = 0;
|
||||
return speed = _speed;
|
||||
}
|
||||
|
||||
int Animation::GetStateCount() {
|
||||
return stateCount;
|
||||
}
|
||||
|
||||
int Animation::GetFrameCount() {
|
||||
return frameCount;
|
||||
}
|
||||
|
||||
int Animation::GetState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
float Animation::GetFrame() {
|
||||
return frame;
|
||||
}
|
||||
|
||||
float Animation::GetSpeed() {
|
||||
return speed;
|
||||
}
|
||||
55
Hearts/animation.h
Normal file
55
Hearts/animation.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/* File Name: animation.h
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 9/7/2011
|
||||
* Version: 1, Stable
|
||||
* Description: The Animation class, animates spritesheets.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#ifndef KR_KAGE_ANIMATION_H_
|
||||
#define KR_KAGE_ANIMATION_H_
|
||||
|
||||
/* Version information */
|
||||
#define ANIMATION_VERSION_MAJOR 1
|
||||
#define ANIMATION_VERSION_MINOR 0
|
||||
#define ANIMATION_VERSION_PATCH 0
|
||||
#define ANIMATION_VERSION "1.0.0"
|
||||
#define ANIMATION_VERSION_STRING "KAGE::Animation Version 1.0.0, Stable"
|
||||
|
||||
namespace KAGE {
|
||||
class Animation {
|
||||
public:
|
||||
/* Public access members */
|
||||
Animation(int stateCount, int frameCount, float speed = 0);
|
||||
|
||||
virtual void Process(int delta);
|
||||
|
||||
/* Accessors and mutators */
|
||||
virtual int SetStateCount (int stateCount);
|
||||
virtual int SetFrameCount (int frameCount);
|
||||
|
||||
virtual int SetState (int state);
|
||||
virtual float SetFrame (float frame);
|
||||
virtual float SetSpeed (float speed);
|
||||
|
||||
virtual int GetStateCount ();
|
||||
virtual int GetFrameCount ();
|
||||
|
||||
virtual int GetState ();
|
||||
virtual float GetFrame ();
|
||||
virtual float GetSpeed ();
|
||||
protected:
|
||||
/* Protected access members */
|
||||
virtual void SetCell()=0;
|
||||
private:
|
||||
/* Private access members */
|
||||
int stateCount;
|
||||
int frameCount;
|
||||
|
||||
int state;
|
||||
float frame;
|
||||
float speed;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
119
Hearts/audio_manager.cpp
Normal file
119
Hearts/audio_manager.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
/* File Name: audio_manager.cpp
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 21/6/2011
|
||||
* Version: 3, Stable
|
||||
* Description: The AudioManager class, used to manage sounds and music.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include "audio_manager.h"
|
||||
using namespace std;
|
||||
using namespace KAGE;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
AudioManager::~AudioManager() {
|
||||
while(Mix_Init(0))
|
||||
Mix_Quit();
|
||||
Mix_CloseAudio();
|
||||
}
|
||||
|
||||
int AudioManager::Init(int flags,int frequency,int format,int channels,int chunkSize) {
|
||||
if (Mix_OpenAudio(frequency,format,channels,chunkSize) != 0) {
|
||||
cout << "Error in AudioManager::AudioManager()" << endl;
|
||||
cout << "Failed to initialise SDL_mixer" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int init = Mix_Init(flags);
|
||||
|
||||
if (init != flags) {
|
||||
cout << "Error in AudioManager::AudioManager()" << endl;
|
||||
cout << "Mix_Init() failed to load all components" << endl;
|
||||
cout << "Is there a .DLL file missing?" << endl;
|
||||
cout << "flags: " << flags << "; init: " << init << endl;
|
||||
return -2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Sounds
|
||||
//-------------------------
|
||||
|
||||
Sound* AudioManager::Add(const char* name,const char* fName) {
|
||||
if (Exists(name)) {
|
||||
cout << "Error in AudioManager::Add(name,fName)" << endl;
|
||||
cout << "A Sound of that name already exists" << endl;
|
||||
cout << "name: " << name << endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return NamedManager::Add(name,new Sound(fName));
|
||||
}
|
||||
|
||||
int AudioManager::Play(const char* name,int channel,int loop) {
|
||||
if (!Exists(name)) {
|
||||
cout << "Error in AudioManager::Play(name,channel,loop)" << endl;
|
||||
cout << "No such Sound with that name" << endl;
|
||||
cout << "name: " << name << endl;
|
||||
return -2;
|
||||
}
|
||||
|
||||
return Get(name)->Play(channel,loop);
|
||||
}
|
||||
|
||||
int AudioManager::Volume(const char* name,int volume) {
|
||||
if (!Exists(name)) {
|
||||
cout << "Error in AudioManager::Volume(name,volume)" << endl;
|
||||
cout << "No such Sound with that name" << endl;
|
||||
cout << "name: " << name << endl;
|
||||
return -2;
|
||||
}
|
||||
|
||||
return Get(name)->Volume(volume);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Music
|
||||
//-------------------------
|
||||
|
||||
Music* AudioManager::Music() {
|
||||
return &music;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Channels
|
||||
//-------------------------
|
||||
|
||||
int AudioManager::ChannelAllocate(int num) {
|
||||
return Mix_AllocateChannels(num);
|
||||
}
|
||||
|
||||
int AudioManager::ChannelHalt(int channel) {
|
||||
return Mix_HaltChannel(channel);
|
||||
}
|
||||
|
||||
int AudioManager::ChannelPlaying(int channel) {
|
||||
return Mix_Playing(channel);
|
||||
}
|
||||
|
||||
void AudioManager::ChannelPause(int channel) {
|
||||
Mix_Pause(channel);
|
||||
}
|
||||
|
||||
void AudioManager::ChannelResume(int channel) {
|
||||
Mix_Resume(channel);
|
||||
}
|
||||
|
||||
int AudioManager::ChannelPaused(int channel) {
|
||||
return Mix_Paused(channel);
|
||||
}
|
||||
|
||||
int AudioManager::ChannelVolume(int volume,int channel) {
|
||||
return Mix_Volume(channel,volume);
|
||||
}
|
||||
57
Hearts/audio_manager.h
Normal file
57
Hearts/audio_manager.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/* File Name: audio_manager.h
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 21/6/2011
|
||||
* Version: 3, Stable
|
||||
* Description: The AudioManager class, used to manage sounds and music.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#ifndef KR_KAGE_AUDIOMANAGER_H_
|
||||
#define KR_KAGE_AUDIOMANAGER_H_
|
||||
|
||||
/* Version information */
|
||||
#define AUDIOMANAGER_VERSION_MAJOR 3
|
||||
#define AUDIOMANAGER_VERSION_MINOR 0
|
||||
#define AUDIOMANAGER_VERSION_PATCH 0
|
||||
#define AUDIOMANAGER_VERSION "3.0.0"
|
||||
#define AUDIOMAANGER_VERSION_STRING "KAGE::AudioManager Version 3, Stable"
|
||||
|
||||
#include "SDL_mixer.h"
|
||||
#include "named_manager.h"
|
||||
#include "sound.h"
|
||||
#include "music.h"
|
||||
|
||||
namespace KAGE {
|
||||
class AudioManager : public KAGE::NamedManager<KAGE::Sound> {
|
||||
public:
|
||||
/* Public access members */
|
||||
~AudioManager();
|
||||
|
||||
int Init(int flags,int frequency,int format,int channels,int chunkSize);
|
||||
|
||||
/* Sounds */
|
||||
Sound* Add (const char* name, const char* fName);
|
||||
int Play (const char* name, int channel = -1, int loop = 0);
|
||||
int Volume (const char* name, int volume = -1);
|
||||
|
||||
/* Music */
|
||||
KAGE::Music* Music();
|
||||
|
||||
/* Channels */
|
||||
int ChannelAllocate (int num = -1);
|
||||
|
||||
int ChannelHalt (int channel = -1);
|
||||
int ChannelPlaying (int channel = -1);
|
||||
|
||||
void ChannelPause (int channel = -1);
|
||||
void ChannelResume (int channel = -1);
|
||||
int ChannelPaused (int channel = -1);
|
||||
|
||||
int ChannelVolume (int volume = -1,int channel = -1);
|
||||
private:
|
||||
/* Private access members */
|
||||
KAGE::Music music;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
280
Hearts/base_engine.cpp
Normal file
280
Hearts/base_engine.cpp
Normal file
@@ -0,0 +1,280 @@
|
||||
/* File Name: base_engine.cpp
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 9/7/2011
|
||||
* Version: 3.2.2, Stable
|
||||
* Description: The BaseEngine class, a Simple DirectMedia Layer (SDL) based all purpose 2D game engine.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include "base_engine.h"
|
||||
using namespace std;
|
||||
using namespace KAGE;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
BaseEngine::BaseEngine(int _screenWidth, int _screenHeight, bool _fullscreen) {
|
||||
//default values for global variables
|
||||
gameRunning = true;
|
||||
|
||||
screenWidth = _screenWidth;
|
||||
screenHeight = _screenHeight;
|
||||
fullscreen = _fullscreen;
|
||||
|
||||
//FPS variables
|
||||
deltaTime = 0;
|
||||
deltaTick = 0;
|
||||
frameRate = 0;
|
||||
frameTick = 0;
|
||||
frameCount = 0;
|
||||
}
|
||||
|
||||
BaseEngine::~BaseEngine() {
|
||||
if (init)
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void BaseEngine::Start(bool _init) {
|
||||
Init(_init);
|
||||
AdditionalInit();
|
||||
EngineLoop();
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event functions
|
||||
//-------------------------
|
||||
|
||||
void BaseEngine::MouseMotion(SDL_MouseMotionEvent& motion) {
|
||||
MouseImage();
|
||||
}
|
||||
|
||||
void BaseEngine::MouseButtonDown(SDL_MouseButtonEvent& button) {
|
||||
MouseImage();
|
||||
}
|
||||
|
||||
void BaseEngine::MouseButtonUp(SDL_MouseButtonEvent& button) {
|
||||
MouseImage();
|
||||
}
|
||||
|
||||
void BaseEngine::KeyDown(SDL_KeyboardEvent& key) {
|
||||
KeyboardImage();
|
||||
switch (TranslateMods(key.keysym)) {
|
||||
case KMOD_NONE:
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
gameRunning = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BaseEngine::KeyUp(SDL_KeyboardEvent& key) {
|
||||
KeyboardImage();
|
||||
}
|
||||
|
||||
void BaseEngine::ActiveEvent(SDL_ActiveEvent& active) {
|
||||
//you can pause or unpause the game's routines here
|
||||
}
|
||||
|
||||
void BaseEngine::UserEvent(SDL_UserEvent& user) {
|
||||
//user defined event
|
||||
}
|
||||
|
||||
void BaseEngine::BeginLoop() {
|
||||
//beginning of the game loop
|
||||
}
|
||||
|
||||
void BaseEngine::EndLoop() {
|
||||
//end of the game loop
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Utility functions
|
||||
//-------------------------
|
||||
|
||||
void BaseEngine::MouseImage() {
|
||||
mouseButtons = SDL_GetMouseState(&mouseX,&mouseY);
|
||||
}
|
||||
|
||||
void BaseEngine::KeyboardImage() {
|
||||
keyboard = SDL_GetKeyState(NULL);
|
||||
}
|
||||
|
||||
void BaseEngine::SetScreen() {
|
||||
SetScreen(screenWidth,screenHeight,fullscreen);
|
||||
}
|
||||
|
||||
void BaseEngine::SetScreen(int _width,int _height,bool _fullscreen) {
|
||||
screenWidth = _width;
|
||||
screenHeight = _height;
|
||||
fullscreen = _fullscreen;
|
||||
|
||||
if (fullscreen)
|
||||
screen = SDL_SetVideoMode(screenWidth,screenHeight,32,SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
|
||||
else
|
||||
screen = SDL_SetVideoMode(screenWidth,screenHeight,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
|
||||
|
||||
if (screen == NULL) {
|
||||
cout << "Error in BaseEngine::SetScreen()" << endl;
|
||||
cout << "SDL_SetVideoMode() returned NULL" << endl;
|
||||
cout << "screenWidth: " << screenWidth << endl;
|
||||
cout << "screenHeight: " << screenHeight << endl;
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
int BaseEngine::ScreenWidth() {
|
||||
return screenWidth;
|
||||
}
|
||||
|
||||
int BaseEngine::ScreenWidth(int width) {
|
||||
screenWidth = width;
|
||||
SetScreen();
|
||||
return screenWidth;
|
||||
}
|
||||
|
||||
int BaseEngine::ScreenHeight() {
|
||||
return screenHeight;
|
||||
}
|
||||
|
||||
int BaseEngine::ScreenHeight(int height) {
|
||||
screenHeight = height;
|
||||
SetScreen();
|
||||
return screenHeight;
|
||||
}
|
||||
|
||||
bool BaseEngine::Fullscreen() {
|
||||
return fullscreen;
|
||||
}
|
||||
|
||||
bool BaseEngine::Fullscreen(bool fscreen) {
|
||||
fullscreen = fscreen;
|
||||
SetScreen();
|
||||
return fullscreen;
|
||||
}
|
||||
|
||||
bool BaseEngine::ToggleFullscreen() {
|
||||
fullscreen = !fullscreen;
|
||||
SetScreen();
|
||||
return fullscreen;
|
||||
}
|
||||
|
||||
void BaseEngine::RedrawScreen() {
|
||||
Render();
|
||||
}
|
||||
|
||||
void BaseEngine::AdditionalInit() {
|
||||
SDL_WM_SetCaption("KAGE: Kayne's All-purpose Game Engine, By Kayne Ruse",NULL);
|
||||
}
|
||||
|
||||
int BaseEngine::TranslateMods(SDL_keysym ks) {
|
||||
/* Translates key mods into a single integer.
|
||||
* This allows the program's keys to be used
|
||||
* from a switch statement, giving cleaner code
|
||||
*/
|
||||
|
||||
/* prevent duplicated calls caused by mod keys
|
||||
* & disables mod keys from use
|
||||
*/
|
||||
if (ks.sym == SDLK_LCTRL || ks.sym == SDLK_RCTRL ||
|
||||
ks.sym == SDLK_LALT || ks.sym == SDLK_RALT ||
|
||||
ks.sym == SDLK_LSHIFT || ks.sym == SDLK_RSHIFT)
|
||||
return -1;
|
||||
|
||||
//recast mod
|
||||
int mods = 0;
|
||||
|
||||
if (ks.mod&KMOD_CTRL) mods += KMOD_CTRL;
|
||||
if (ks.mod&KMOD_ALT) mods += KMOD_ALT;
|
||||
if (ks.mod&KMOD_SHIFT) mods += KMOD_SHIFT;
|
||||
|
||||
return mods;
|
||||
}
|
||||
|
||||
void BaseEngine::PushUserEvent(int code, void* data1, void* data2) {
|
||||
SDL_Event e;
|
||||
e.type = SDL_USEREVENT;
|
||||
e.user.code = code;
|
||||
e.user.data1 = data1;
|
||||
e.user.data2 = data2;
|
||||
SDL_PushEvent(&e);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Private access members
|
||||
//-------------------------
|
||||
|
||||
void BaseEngine::Init(bool _init) {
|
||||
init = _init;
|
||||
|
||||
if (init && SDL_Init(SDL_INIT_VIDEO) == -1) {
|
||||
cout << "Error in BaseEngine::Init()" << endl;
|
||||
cout << "Failed to initialise SDL" << endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
KeyboardImage();
|
||||
MouseImage();
|
||||
SetScreen();
|
||||
}
|
||||
|
||||
void BaseEngine::EngineLoop() {
|
||||
SDL_Event event;
|
||||
while(gameRunning) {
|
||||
BeginLoop();
|
||||
while(SDL_PollEvent(&event)) {
|
||||
switch(event.type) {
|
||||
case SDL_QUIT:
|
||||
gameRunning = false;
|
||||
break;
|
||||
case SDL_MOUSEMOTION:
|
||||
MouseMotion(event.motion);
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
MouseButtonDown(event.button);
|
||||
break;
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
MouseButtonUp(event.button);
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
KeyDown(event.key);
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
KeyUp(event.key);
|
||||
break;
|
||||
case SDL_ACTIVEEVENT:
|
||||
ActiveEvent(event.active);
|
||||
break;
|
||||
case SDL_USEREVENT:
|
||||
UserEvent(event.user);
|
||||
break;
|
||||
}
|
||||
}
|
||||
FrameRate();
|
||||
DeltaTime();
|
||||
Process();
|
||||
Render();
|
||||
EndLoop();
|
||||
}
|
||||
}
|
||||
|
||||
void BaseEngine::Render() {
|
||||
Draw();
|
||||
SDL_Flip(screen);
|
||||
}
|
||||
|
||||
void BaseEngine::FrameRate() {
|
||||
frameCount++;
|
||||
if (SDL_GetTicks() - frameTick >= 1000) {
|
||||
frameRate = frameCount;
|
||||
frameCount = 0;
|
||||
frameTick = SDL_GetTicks();
|
||||
}
|
||||
}
|
||||
|
||||
void BaseEngine::DeltaTime() {
|
||||
deltaTime = SDL_GetTicks() - deltaTick;
|
||||
deltaTick = SDL_GetTicks();
|
||||
}
|
||||
105
Hearts/base_engine.h
Normal file
105
Hearts/base_engine.h
Normal file
@@ -0,0 +1,105 @@
|
||||
/* File Name: base_engine.h
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 9/7/2011
|
||||
* Version: 3.2.2, Stable
|
||||
* Description: The BaseEngine class, a Simple DirectMedia Layer (SDL) based all purpose 2D game engine.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#ifndef KR_KAGE_BASEENGINE_H_
|
||||
#define KR_KAGE_BASEENGINE_H_
|
||||
|
||||
/* Version information */
|
||||
#define BASEENGINE_VERSION_MAJOR 3
|
||||
#define BASEENGINE_VERSION_MINOR 2
|
||||
#define BASEENGINE_VERSION_PATCH 2
|
||||
#define BASEENGINE_VERSION "3.2.2"
|
||||
#define BASEENGINE_VERSION_STRING "KAGE::BaseEngine Version 3.2.2, Stable"
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
namespace KAGE {
|
||||
class BaseEngine {
|
||||
public:
|
||||
/* Public access members */
|
||||
BaseEngine(int screenWidth = 800, int screenHeight = 600, bool fullscreen = false);
|
||||
~BaseEngine();
|
||||
|
||||
void Start(bool init = false);
|
||||
protected:
|
||||
/* Event functions */
|
||||
virtual void MouseMotion (SDL_MouseMotionEvent& motion);
|
||||
virtual void MouseButtonDown (SDL_MouseButtonEvent& button);
|
||||
virtual void MouseButtonUp (SDL_MouseButtonEvent& button);
|
||||
virtual void KeyDown (SDL_KeyboardEvent& key);
|
||||
virtual void KeyUp (SDL_KeyboardEvent& key);
|
||||
virtual void ActiveEvent (SDL_ActiveEvent& active);
|
||||
virtual void UserEvent (SDL_UserEvent& user);
|
||||
virtual void Process ()=0;
|
||||
virtual void Draw ()=0;
|
||||
virtual void BeginLoop ();
|
||||
virtual void EndLoop ();
|
||||
|
||||
/* Utility functions */
|
||||
void MouseImage ();
|
||||
void KeyboardImage ();
|
||||
|
||||
void SetScreen ();
|
||||
void SetScreen (int width,int height,bool fullscreen);
|
||||
int ScreenWidth ();
|
||||
int ScreenWidth (int width);
|
||||
int ScreenHeight ();
|
||||
int ScreenHeight (int height);
|
||||
bool Fullscreen ();
|
||||
bool Fullscreen (bool fullscreen);
|
||||
bool ToggleFullscreen ();
|
||||
void RedrawScreen ();
|
||||
|
||||
virtual void AdditionalInit ();
|
||||
virtual int TranslateMods (SDL_keysym);
|
||||
virtual void PushUserEvent (int code, void* data1, void* data2);
|
||||
|
||||
/* Global variables */
|
||||
bool gameRunning;
|
||||
SDL_Surface* screen;
|
||||
|
||||
Uint8* keyboard;
|
||||
Uint8 mouseButtons;
|
||||
int mouseX;
|
||||
int mouseY;
|
||||
|
||||
int frameRate;
|
||||
int deltaTime;
|
||||
private:
|
||||
/* Private access members */
|
||||
void Init (bool init);
|
||||
void EngineLoop ();
|
||||
void Render ();
|
||||
void FrameRate ();
|
||||
void DeltaTime ();
|
||||
|
||||
bool init;
|
||||
|
||||
int screenWidth;
|
||||
int screenHeight;
|
||||
bool fullscreen;
|
||||
|
||||
int deltaTick;
|
||||
int frameTick;
|
||||
int frameCount;
|
||||
};
|
||||
}
|
||||
|
||||
#ifndef START
|
||||
/* Creates SDL_main(). Place this after the derived engine's
|
||||
* declaration, and pass it the name of the derived engine.
|
||||
*/
|
||||
#define START(ENGINE) \
|
||||
int SDL_main(int,char**) { \
|
||||
ENGINE app; \
|
||||
app.Start(true); \
|
||||
return 0; \
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
86
Hearts/card.cpp
Normal file
86
Hearts/card.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
/* File Name: card.cpp
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: 5/6/2011
|
||||
* Version:
|
||||
* Description: Designed for Project Hearts, 4th try.
|
||||
*/
|
||||
#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)
|
||||
{
|
||||
SetFace(UP);
|
||||
SetWidth(73);
|
||||
SetHeight(98);
|
||||
next = NULL;
|
||||
}
|
||||
|
||||
int Card::Suit() {
|
||||
return suit;
|
||||
}
|
||||
|
||||
int Card::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) {
|
||||
return next = _next;
|
||||
}
|
||||
|
||||
Card* Card::GetNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
bool Card::operator>(Card& card) {
|
||||
if (Suit() > card.Suit())
|
||||
return true;
|
||||
|
||||
if (Suit() == card.Suit() &&
|
||||
Rank() > card.Rank())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Card::operator<(Card& card) {
|
||||
return card > *this;
|
||||
}
|
||||
72
Hearts/card.h
Normal file
72
Hearts/card.h
Normal file
@@ -0,0 +1,72 @@
|
||||
/* File Name: card.h
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: 5/6/2011
|
||||
* Version:
|
||||
* Description: Designed for Project Hearts, 4th try.
|
||||
*/
|
||||
#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 {
|
||||
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* SetNext(Card* next);
|
||||
Card* GetNext();
|
||||
|
||||
bool operator>(Card&);
|
||||
bool operator<(Card&);
|
||||
|
||||
enum {
|
||||
UP = 1, DOWN
|
||||
};
|
||||
|
||||
enum {
|
||||
CLUBS = 1, DIAMONDS, SPADES, HEARTS
|
||||
};
|
||||
|
||||
enum {
|
||||
TWO=1, THREE=2, FOUR=3, FIVE=4,
|
||||
SIX=5, SEVEN=6, EIGHT=7, NINE=8,
|
||||
TEN=9, JACK=10, QUEEN=11, KING=12, ACE=13
|
||||
};
|
||||
protected:
|
||||
/* Protected access members */
|
||||
const int suit;
|
||||
const int rank;
|
||||
|
||||
int face;
|
||||
SDL_Surface* faceSurface;
|
||||
SDL_Surface* backSurface;
|
||||
|
||||
Card* next;
|
||||
};
|
||||
|
||||
#endif
|
||||
244
Hearts/card_list.cpp
Normal file
244
Hearts/card_list.cpp
Normal file
@@ -0,0 +1,244 @@
|
||||
/* File Name: card_list.cpp
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: 5/6/2011
|
||||
* Version:
|
||||
* Description: Designed for Project Hearts, 4th try.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
#include "card_list.h"
|
||||
using namespace std;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
CardList::CardList() {
|
||||
headCard = NULL;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//List accessors
|
||||
//-------------------------
|
||||
|
||||
void CardList::Receive(Card* slabHead) {
|
||||
if (headCard == NULL)
|
||||
headCard = slabHead;
|
||||
else {
|
||||
iterator it = headCard;
|
||||
while(it->GetNext() != NULL)
|
||||
it = it->GetNext();
|
||||
it->SetNext(slabHead);
|
||||
}
|
||||
}
|
||||
|
||||
Card* CardList::Read(int index) {
|
||||
iterator it = headCard;
|
||||
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (it != NULL)
|
||||
it = it->GetNext();
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
Card* CardList::Pass(int index) {
|
||||
//special cases
|
||||
if (headCard == NULL)
|
||||
return NULL;
|
||||
|
||||
if (index == 0) {
|
||||
iterator ret = headCard;
|
||||
headCard = headCard->GetNext();
|
||||
ret->SetNext(NULL);
|
||||
return ret;
|
||||
}
|
||||
|
||||
//iterate through the list
|
||||
iterator it = headCard;
|
||||
iterator prev = NULL;
|
||||
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (it->GetNext() != NULL) {
|
||||
prev = it;
|
||||
it = it->GetNext();
|
||||
}
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//return this card
|
||||
prev->SetNext(it->GetNext());
|
||||
it->SetNext(NULL);
|
||||
return it;
|
||||
}
|
||||
|
||||
Card* CardList::PassSlab(int first, int count) {
|
||||
//special cases
|
||||
if (headCard == NULL)
|
||||
return NULL;
|
||||
|
||||
if (count < 2)
|
||||
return Pass(first);
|
||||
|
||||
//Find the first & last card
|
||||
iterator prevFirst = (first < 1) ? NULL : Read(first - 1);
|
||||
iterator prevLast = Read(first + count - 1);
|
||||
|
||||
//return this section of the list
|
||||
iterator ret = NULL;
|
||||
|
||||
if (prevFirst != NULL && prevLast != NULL) {
|
||||
//everything is normal
|
||||
ret = prevFirst->GetNext();
|
||||
prevFirst->SetNext(prevLast->GetNext());
|
||||
prevLast->SetNext(NULL);
|
||||
}
|
||||
|
||||
else if (prevFirst == NULL && prevLast != NULL) {
|
||||
//returning the first card
|
||||
ret = headCard;
|
||||
headCard = prevLast->GetNext();
|
||||
prevLast->SetNext(NULL);
|
||||
}
|
||||
|
||||
else if (prevFirst == NULL && prevLast == NULL) {
|
||||
ret = headCard;
|
||||
headCard = NULL;
|
||||
}
|
||||
|
||||
else if (prevFirst != NULL && prevLast == NULL) {
|
||||
ret = prevFirst->GetNext();
|
||||
prevFirst->SetNext(NULL);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Misc members
|
||||
//-------------------------
|
||||
|
||||
void CardList::Shuffle() {
|
||||
//New version
|
||||
srand((unsigned int)time(NULL));
|
||||
iterator shuffleHead = NULL;
|
||||
iterator prev = NULL;
|
||||
|
||||
do {
|
||||
//shuffling backwards
|
||||
prev = shuffleHead;
|
||||
shuffleHead = Pass(rand() % Size());
|
||||
shuffleHead->SetNext(prev);
|
||||
}
|
||||
while(headCard != NULL);
|
||||
|
||||
headCard = shuffleHead;
|
||||
}
|
||||
|
||||
void CardList::Sort() {
|
||||
//Sort the cards in the list
|
||||
if (headCard == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
iterator sortHead = Pass();
|
||||
iterator it = sortHead;
|
||||
|
||||
while(it != NULL && Size()) {
|
||||
//first card
|
||||
if (*it > *Read()) {
|
||||
//insert the new card at the start of the list
|
||||
it = Pass();
|
||||
it->SetNext(sortHead);
|
||||
sortHead = it;
|
||||
continue;
|
||||
}
|
||||
|
||||
while (it->GetNext() != NULL && *Read() > *it->GetNext()) {
|
||||
//place the iterator just before the position to insert the card
|
||||
it = it->GetNext();
|
||||
}
|
||||
|
||||
//insert the next card here
|
||||
iterator tmp = Pass();
|
||||
tmp->SetNext(it->GetNext());
|
||||
it->SetNext(tmp);
|
||||
|
||||
//reset
|
||||
it = sortHead;
|
||||
}
|
||||
|
||||
headCard = sortHead;
|
||||
}
|
||||
|
||||
void CardList::SortRank() {
|
||||
//Sort the cards in the list
|
||||
if (headCard == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
iterator sortHead = Pass();
|
||||
iterator it = sortHead;
|
||||
|
||||
while(it != NULL && Size()) {
|
||||
//first card
|
||||
if (it->Rank() > Read()->Rank()) {
|
||||
//insert the new card at the start of the list
|
||||
it = Pass();
|
||||
it->SetNext(sortHead);
|
||||
sortHead = it;
|
||||
continue;
|
||||
}
|
||||
|
||||
while (it->GetNext() != NULL && Read()->Rank() > it->GetNext()->Rank()) {
|
||||
//place the iterator just before the position to insert the card
|
||||
it = it->GetNext();
|
||||
}
|
||||
|
||||
//insert the next card here
|
||||
iterator tmp = Pass();
|
||||
tmp->SetNext(it->GetNext());
|
||||
it->SetNext(tmp);
|
||||
|
||||
//reset
|
||||
it = sortHead;
|
||||
}
|
||||
|
||||
headCard = sortHead;
|
||||
}
|
||||
|
||||
int CardList::Size() {
|
||||
int count = 0;
|
||||
for (iterator it = headCard; it != NULL; count++)
|
||||
it = it->GetNext();
|
||||
return count;
|
||||
}
|
||||
|
||||
Card* CardList::Head() {
|
||||
return headCard;
|
||||
}
|
||||
|
||||
void CardList::DrawAll(SDL_Surface* dest) {
|
||||
for (iterator it = headCard;it != NULL;it = it->GetNext())
|
||||
it->Draw(dest);
|
||||
}
|
||||
53
Hearts/card_list.h
Normal file
53
Hearts/card_list.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/* File Name: card_list.h
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: 5/6/2011
|
||||
* Version:
|
||||
* Description: Designed for Project Hearts, 4th try.
|
||||
*/
|
||||
#ifndef KR_CARDLIST_H_
|
||||
#define KR_CARDLIST_H_
|
||||
#include "card.h"
|
||||
|
||||
class CardList {
|
||||
public:
|
||||
/* Public access members */
|
||||
CardList();
|
||||
|
||||
/* List accessors */
|
||||
void Receive(Card* slabHead);
|
||||
Card* Read(int index = 0);
|
||||
Card* Pass(int index = 0);
|
||||
Card* PassSlab(int first, int count);
|
||||
|
||||
/* Misc members */
|
||||
void Shuffle();
|
||||
void Sort();
|
||||
void SortRank();
|
||||
int Size();
|
||||
Card* Head();
|
||||
|
||||
void DrawAll(SDL_Surface*);
|
||||
|
||||
typedef Card* iterator;
|
||||
private:
|
||||
/* Private access members */
|
||||
Card* headCard;
|
||||
};
|
||||
|
||||
#endif
|
||||
72
Hearts/deck.cpp
Normal file
72
Hearts/deck.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
/* File Name: deck.cpp
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: 5/6/2011
|
||||
* Version:
|
||||
* Description: Designed for Project Hearts, 4th try.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include "deck.h"
|
||||
using namespace std;
|
||||
|
||||
Deck::Deck() {
|
||||
faceSurface = NULL;
|
||||
backSurface = NULL;
|
||||
}
|
||||
|
||||
Deck::~Deck() {
|
||||
DeleteAll();
|
||||
if (faceSurface != NULL)
|
||||
SDL_FreeSurface(faceSurface);
|
||||
if (backSurface != NULL)
|
||||
SDL_FreeSurface(backSurface);
|
||||
}
|
||||
|
||||
void Deck::Init(const char* faceName, const char* backName) {
|
||||
//Create and setup the surfaces, with error checking
|
||||
faceSurface = SDL_LoadBMP(faceName);
|
||||
|
||||
if (faceSurface == NULL) {
|
||||
cerr << "Error in Deck::Init(faceName,backName)" << endl;
|
||||
cerr << "SDL_LoadBMP() returned NULL" << endl;
|
||||
cerr << "faceName: " << faceName << endl;
|
||||
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));
|
||||
}
|
||||
|
||||
void Deck::DeleteAll() {
|
||||
while(Head() != NULL)
|
||||
delete Pass();
|
||||
}
|
||||
42
Hearts/deck.h
Normal file
42
Hearts/deck.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/* File Name: deck.h
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: 5/6/2011
|
||||
* Version:
|
||||
* Description: Designed for Project Hearts, 4th try.
|
||||
*/
|
||||
#ifndef KR_DECK_H_
|
||||
#define KR_DECK_H_
|
||||
#include "SDL.h"
|
||||
#include "card_list.h"
|
||||
|
||||
class Deck : public CardList {
|
||||
public:
|
||||
/* Public access members */
|
||||
Deck();
|
||||
~Deck();
|
||||
|
||||
void Init(const char* faceName, const char* backName);
|
||||
void DeleteAll();
|
||||
private:
|
||||
/* Private access members */
|
||||
SDL_Surface* faceSurface;
|
||||
SDL_Surface* backSurface;
|
||||
};
|
||||
|
||||
#endif
|
||||
386
Hearts/hearts_engine.cpp
Normal file
386
Hearts/hearts_engine.cpp
Normal file
@@ -0,0 +1,386 @@
|
||||
/* File Name: hearts_engine.h
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: ...
|
||||
* Version: ...
|
||||
* Description: ...
|
||||
*/
|
||||
#include <iostream>
|
||||
#include "hearts_engine.h"
|
||||
using namespace std;
|
||||
|
||||
#define CASE break; case
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
HeartsEngine::HeartsEngine() {
|
||||
mImage.Add("heart","rsc\\heart.bmp",32,32,64,64);
|
||||
|
||||
mAudio.Init(0,MIX_DEFAULT_FREQUENCY,MIX_DEFAULT_FORMAT,1,1024);
|
||||
mAudio.Add("knock","rsc\\knock.wav");
|
||||
mAudio.Add("glass","rsc\\glass_break.wav");
|
||||
|
||||
deck.Init("rsc\\cards.bmp","rsc\\back.bmp");
|
||||
|
||||
player[0] = new PlayerUser();
|
||||
player[1] = new PlayerAI();
|
||||
player[2] = new PlayerAI();
|
||||
player[3] = new PlayerAI();
|
||||
|
||||
//game members
|
||||
gamePhase = SETUP;
|
||||
rotation = NONE;
|
||||
|
||||
//graphics
|
||||
table.SetPositions(364,301,314,251,364,201,414,251); //centered: 364,251,
|
||||
|
||||
player[0]->SetPositions(ScreenWidth()/2-156, ScreenHeight() - 98, 20,0);
|
||||
player[1]->SetPositions(0, ScreenHeight()/2 - 169, 0,20);
|
||||
player[2]->SetPositions(ScreenWidth()/2-156, 0, 20,0);
|
||||
player[3]->SetPositions(ScreenWidth()-73, ScreenHeight()/2 - 169, 0,20);
|
||||
}
|
||||
|
||||
HeartsEngine::~HeartsEngine() {
|
||||
CleanupPhase();
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
delete player[i];
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Engine members
|
||||
//-------------------------
|
||||
|
||||
void HeartsEngine::MouseButtonDown(SDL_MouseButtonEvent& button) {
|
||||
BaseEngine::MouseButtonDown(button);
|
||||
|
||||
switch(gamePhase) {
|
||||
CASE SWAP:
|
||||
((PlayerUser*)(player[0]))->SelectSwapCard(mouseX, mouseY);
|
||||
CASE TRICK:
|
||||
if (playPhase == PLAYER)
|
||||
PlayPlayerPhase();
|
||||
}
|
||||
}
|
||||
|
||||
void HeartsEngine::Process() {
|
||||
switch(gamePhase) {
|
||||
CASE SETUP: SetupPhase();
|
||||
CASE SWAP: SwapPhase();
|
||||
CASE TRICK: TrickPhase();
|
||||
CASE SCORE: ScorePhase();
|
||||
CASE CLEANUP: CleanupPhase();
|
||||
}
|
||||
}
|
||||
|
||||
void HeartsEngine::Draw() {
|
||||
SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,128,0));
|
||||
mImage.DrawAll(screen);
|
||||
|
||||
switch(gamePhase) {
|
||||
case SETUP:
|
||||
case SWAP:
|
||||
case TRICK:
|
||||
for (int i = 0; i < 4; i++)
|
||||
player[i]->DrawHand(screen);
|
||||
|
||||
table.Draw(screen,firstPlayer);
|
||||
break;
|
||||
case SCORE:
|
||||
for (int i = 0; i < 4; i++)
|
||||
player[i]->DrawTricks(screen);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Game phase members
|
||||
//-------------------------
|
||||
|
||||
void HeartsEngine::SetupPhase() {
|
||||
deck.Shuffle();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
player[i]->Hand()->Receive( deck.PassSlab(0,13) );
|
||||
player[i]->Hand()->Sort();
|
||||
}
|
||||
ResetPositions();
|
||||
ResetFaces();
|
||||
|
||||
switch(rotation) {
|
||||
CASE NONE: rotation = LEFT;
|
||||
CASE LEFT: rotation = RIGHT;
|
||||
CASE RIGHT: rotation = ACROSS;
|
||||
CASE ACROSS: rotation = NONE;
|
||||
}
|
||||
|
||||
playPhase = BEFORE;
|
||||
|
||||
firstPlayer = -1;
|
||||
trickCount = 0;
|
||||
heartsBroken = false;
|
||||
mImage["heart"]->SetClipX(0);
|
||||
|
||||
if (rotation == NONE) {
|
||||
CalcFirst();
|
||||
gamePhase = TRICK;
|
||||
}
|
||||
else {
|
||||
((PlayerAI*)(player[1]))->SelectSwapCards(20,0);
|
||||
((PlayerAI*)(player[2]))->SelectSwapCards(0,20);
|
||||
((PlayerAI*)(player[3]))->SelectSwapCards(-20,0);
|
||||
gamePhase = SWAP;
|
||||
}
|
||||
}
|
||||
|
||||
void HeartsEngine::SwapPhase() {
|
||||
if (!keyboard[SDLK_F1])
|
||||
return;
|
||||
|
||||
if (!player[0]->CheckSwapCards())
|
||||
return;
|
||||
|
||||
CardList cList[4];
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
cList[i].Receive( player[i]->PassSwapCards() );
|
||||
|
||||
int pIndex = 0;
|
||||
|
||||
for (int loop = 0; loop < 4; loop++) {
|
||||
pIndex = loop + rotation;
|
||||
if (pIndex >= 4) pIndex -= 4;
|
||||
if (pIndex < 0) pIndex += 4;
|
||||
|
||||
player[loop]->Hand()->Receive( cList[pIndex].PassSlab(0,3) );
|
||||
player[loop]->Hand()->Sort();
|
||||
}
|
||||
|
||||
ResetPositions();
|
||||
ResetFaces();
|
||||
CalcFirst();
|
||||
gamePhase = TRICK;
|
||||
}
|
||||
|
||||
void HeartsEngine::TrickPhase() {
|
||||
switch(playPhase) {
|
||||
CASE BEFORE:
|
||||
PlayBeforePhase();
|
||||
CASE AFTER:
|
||||
PlayAfterPhase();
|
||||
CASE DISPLAY:
|
||||
PlayDisplayPhase();
|
||||
}
|
||||
|
||||
if (trickCount == 13) {
|
||||
//round is finished
|
||||
timeTick = SDL_GetTicks();
|
||||
ResetPositions();
|
||||
CalcScores();
|
||||
gamePhase = SCORE;
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
cout << "Player " << i << ": " << player[i]->Score() << endl;//tmp...
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void HeartsEngine::ScorePhase() {
|
||||
//end of game; temporary functionality...
|
||||
if (player[0]->Score() >= 100 ||
|
||||
player[1]->Score() >= 100 ||
|
||||
player[2]->Score() >= 100 ||
|
||||
player[3]->Score() >= 100)
|
||||
{
|
||||
cout << "GAME OVER" << endl;
|
||||
int winner = 0;
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (player[i]->Score() < player[winner]->Score()) {
|
||||
winner = i;
|
||||
}
|
||||
}
|
||||
cout << "Winner is: " << winner << endl;
|
||||
cout << endl;
|
||||
|
||||
player[winner]->Wins(1);
|
||||
for (int i = 0; i < 4; i++)
|
||||
cout << "Player " << i << " wins: " << player[i]->Wins() << endl;
|
||||
|
||||
cout << endl;
|
||||
|
||||
//reset all values
|
||||
for (int i = 0; i < 4; i++)
|
||||
player[i]->Score( -player[i]->Score() );
|
||||
rotation = NONE;
|
||||
}
|
||||
|
||||
//10 second delay
|
||||
if (SDL_GetTicks() - timeTick > 10000 || keyboard[SDLK_F1])//button...
|
||||
gamePhase = CLEANUP;
|
||||
}
|
||||
|
||||
void HeartsEngine::CleanupPhase() {
|
||||
deck.Receive( table.Pass() );
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
deck.Receive( player[i]->Hand()->PassSlab(0,52) );
|
||||
deck.Receive( player[i]->Tricks()->PassSlab(0,52) );
|
||||
}
|
||||
|
||||
gamePhase = SETUP;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Play phase members
|
||||
//-------------------------
|
||||
|
||||
void HeartsEngine::PlayBeforePhase() {
|
||||
if (firstPlayer != 0) {
|
||||
mAudio.Play("knock");
|
||||
for (int i = firstPlayer; i < 4; i++) {
|
||||
bool heartSound = heartsBroken;
|
||||
Card* c = ((PlayerAI*)(player[i]))->PassPlayCard(table.GetLeadingSuit(firstPlayer),trickCount,heartsBroken);
|
||||
if (c != NULL)
|
||||
table.Receive(c, i);
|
||||
else {
|
||||
cerr << "Error in HeartsEngine::PlayBeforePhase()" << endl;
|
||||
cerr << "player[i]->PassPlayCard() returned NULL" << endl;
|
||||
cerr << "Player index: " << i << endl;
|
||||
}
|
||||
if (heartSound != heartsBroken) {
|
||||
mImage["heart"]->SetClipX(64);
|
||||
mAudio.Play("glass");
|
||||
}
|
||||
}
|
||||
}
|
||||
playPhase = PLAYER;
|
||||
}
|
||||
|
||||
void HeartsEngine::PlayPlayerPhase() {
|
||||
if (mouseButtons & SDL_BUTTON(1)) {
|
||||
bool heartSound = heartsBroken;
|
||||
Card* c = ((PlayerUser*)(player[0]))->PassPlayCard(mouseX,mouseY,table.GetLeadingSuit(firstPlayer),trickCount,heartsBroken);
|
||||
if (c != NULL) {
|
||||
table.Receive(c, 0);
|
||||
playPhase = AFTER;
|
||||
}
|
||||
if (heartSound != heartsBroken) {
|
||||
mImage["heart"]->SetClipX(64);
|
||||
mAudio.Play("glass");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HeartsEngine::PlayAfterPhase() {
|
||||
mAudio.Play("knock");
|
||||
int max = (firstPlayer == 0)? 4:firstPlayer;
|
||||
|
||||
for (int i = 1; i < max; i++) {
|
||||
bool heartSound = heartsBroken;
|
||||
Card* c = ((PlayerAI*)(player[i]))->PassPlayCard(table.GetLeadingSuit(firstPlayer),trickCount,heartsBroken);
|
||||
if (c != NULL)
|
||||
table.Receive(c, i);
|
||||
else {
|
||||
cerr << "Error in HeartsEngine::PlayAfterPhase()" << endl;
|
||||
cerr << "player[i]->PassPlayCard() returned NULL" << endl;
|
||||
cerr << "Player index: " << i << endl;
|
||||
}
|
||||
if (heartSound != heartsBroken) {
|
||||
mImage["heart"]->SetClipX(64);
|
||||
mAudio.Play("glass");
|
||||
}
|
||||
}
|
||||
|
||||
timeTick = SDL_GetTicks();
|
||||
playPhase = DISPLAY;
|
||||
}
|
||||
|
||||
void HeartsEngine::PlayDisplayPhase() {
|
||||
//2 second delay
|
||||
if (SDL_GetTicks() - timeTick > 2000) {
|
||||
firstPlayer = table.CalcWinner(firstPlayer);
|
||||
player[firstPlayer]->Tricks()->Receive( table.Pass() );
|
||||
|
||||
trickCount++;
|
||||
playPhase = BEFORE;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Utility members
|
||||
//-------------------------
|
||||
|
||||
void HeartsEngine::ResetPositions() {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
player[i]->SetHandPositions();
|
||||
player[i]->SetTrickPositions();
|
||||
}
|
||||
}
|
||||
|
||||
void HeartsEngine::ResetFaces() {
|
||||
player[0]->SetFaces(Card::UP);
|
||||
for (int i = 1; i < 4; i++)
|
||||
player[i]->SetFaces(Card::DOWN);
|
||||
}
|
||||
|
||||
void HeartsEngine::CalcFirst() {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (CardList::iterator it = player[i]->Hand()->Head(); it != NULL; it = it->GetNext()) {
|
||||
if (ISCARD(it,TWO,CLUBS)) {
|
||||
firstPlayer = i;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HeartsEngine::CalcScores() {
|
||||
//count the scores
|
||||
int tally;
|
||||
Card* card;
|
||||
|
||||
for (int p = 0; p < 4; p++) {
|
||||
tally = 0;
|
||||
card = NULL;
|
||||
|
||||
for (int c = 0; c < 52; c++) {
|
||||
card = player[p]->Tricks()->Read(c);
|
||||
|
||||
if (card == NULL)
|
||||
break;
|
||||
|
||||
if (card->Suit() == Card::HEARTS)
|
||||
tally++;
|
||||
|
||||
if (ISCARD(card,QUEEN,SPADES))
|
||||
tally += 13;
|
||||
}
|
||||
|
||||
if (tally != 26)
|
||||
player[p]->Score(tally);
|
||||
else {
|
||||
//YAY PORTAL REFERENCE!
|
||||
for (int i = 0; i < 4; i++)
|
||||
if (i != p)
|
||||
player[i]->Score(26);
|
||||
}
|
||||
}
|
||||
}
|
||||
90
Hearts/hearts_engine.h
Normal file
90
Hearts/hearts_engine.h
Normal file
@@ -0,0 +1,90 @@
|
||||
/* File Name: hearts_engine.h
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: ...
|
||||
* Version: ...
|
||||
* Description: ...
|
||||
*/
|
||||
#ifndef KR_HEARTSENGINE_H_
|
||||
#define KR_HEARTSENGINE_H_
|
||||
#include "base_engine.h"
|
||||
#include "image_manager.h"
|
||||
#include "audio_manager.h"
|
||||
#include "deck.h"
|
||||
#include "player_ai.h"
|
||||
#include "player_user.h"
|
||||
#include "table.h"
|
||||
|
||||
class HeartsEngine : public KAGE::BaseEngine {
|
||||
public:
|
||||
/* Public access members */
|
||||
HeartsEngine();
|
||||
~HeartsEngine();
|
||||
private:
|
||||
/* Engine members */
|
||||
void MouseButtonDown (SDL_MouseButtonEvent& button);
|
||||
void Process ();
|
||||
void Draw ();
|
||||
|
||||
/* KAGE Managers */
|
||||
KAGE::ImageManager mImage;
|
||||
KAGE::AudioManager mAudio;
|
||||
|
||||
/* Game phase members */
|
||||
void SetupPhase();
|
||||
void SwapPhase();
|
||||
void TrickPhase();
|
||||
void ScorePhase();
|
||||
void CleanupPhase();
|
||||
|
||||
/* Play phase members */
|
||||
void PlayBeforePhase();
|
||||
void PlayPlayerPhase();
|
||||
void PlayAfterPhase();
|
||||
void PlayDisplayPhase();
|
||||
|
||||
enum GamePhase {
|
||||
SETUP, SWAP, TRICK, SCORE, CLEANUP
|
||||
}gamePhase;
|
||||
enum PlayPhase {
|
||||
BEFORE, PLAYER, AFTER, DISPLAY
|
||||
}playPhase;
|
||||
enum Rotation {
|
||||
NONE = 0, LEFT = -1, RIGHT = 1, ACROSS = 2
|
||||
}rotation;
|
||||
|
||||
/* Game members */
|
||||
Deck deck;
|
||||
Player *player[4];
|
||||
Table table;
|
||||
|
||||
int firstPlayer;
|
||||
int trickCount;
|
||||
bool heartsBroken;
|
||||
int timeTick;
|
||||
|
||||
/* Utility members */
|
||||
void ResetPositions();
|
||||
void ResetFaces();
|
||||
void CalcFirst();
|
||||
void CalcScores();
|
||||
};
|
||||
|
||||
START(HeartsEngine)
|
||||
|
||||
#endif
|
||||
154
Hearts/image.cpp
Normal file
154
Hearts/image.cpp
Normal file
@@ -0,0 +1,154 @@
|
||||
/* File Name: image.cpp
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 20/6/2011
|
||||
* Version: 3.1.0, Stable
|
||||
* Description: The Image class, a wrapper for SDL_Surface and other drawing functionality.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include "image.h"
|
||||
using namespace std;
|
||||
using namespace KAGE;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
Image::Image(const char* fileName, int x, int y, int w, int h):
|
||||
localSurface(true)
|
||||
{
|
||||
//Create a local surface from a file
|
||||
surface = SDL_LoadBMP(fileName);
|
||||
|
||||
if (surface == NULL) {
|
||||
cout << "Error in Image::Image()" << endl;
|
||||
cout << "SDL_LoadBMP() returned NULL" << endl;
|
||||
cout << "fileName: " << fileName << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
SetColorKey(255,0,255);
|
||||
Init(x,y,0,0,w,h);
|
||||
}
|
||||
|
||||
Image::Image(SDL_Surface* surf, int x, int y, int w, int h):
|
||||
localSurface(false)
|
||||
{
|
||||
//Use a surface passed by the calling function
|
||||
surface = surf;
|
||||
Init(x,y,0,0,w,h);
|
||||
}
|
||||
|
||||
void Image::Init(int x, int y, int sx, int sy, int sw, int sh) {
|
||||
//Set up the variables
|
||||
dclip.x = x;
|
||||
dclip.y = y;
|
||||
sclip.x = sx;
|
||||
sclip.y = sy;
|
||||
sclip.w = (sw == 0) ? surface->w : sw;
|
||||
sclip.h = (sh == 0) ? surface->h : sh;
|
||||
}
|
||||
|
||||
Image::~Image() {
|
||||
if (localSurface)
|
||||
SDL_FreeSurface(surface);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Graphical members
|
||||
//-------------------------
|
||||
|
||||
void Image::Draw(SDL_Surface* dest, int camX, int camY) {
|
||||
SDL_Rect tmp = dclip;
|
||||
tmp.x += camX;
|
||||
tmp.y += camY;
|
||||
SDL_BlitSurface(surface,&sclip,dest,&tmp);
|
||||
}
|
||||
|
||||
int Image::SetColorKey(int red, int green, int blue) {
|
||||
if (localSurface)
|
||||
return SDL_SetColorKey(surface,SDL_SRCCOLORKEY,SDL_MapRGB(surface->format,red,green,blue));
|
||||
else
|
||||
return -2;
|
||||
}
|
||||
|
||||
int Image::ClearColorKey() {
|
||||
if (localSurface)
|
||||
return SDL_SetColorKey(surface,0,0);
|
||||
else
|
||||
return -2;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Accessors and mutators
|
||||
//-------------------------
|
||||
|
||||
void Image::SetPos(int x, int y) {
|
||||
dclip.x = x;
|
||||
dclip.y = y;
|
||||
}
|
||||
|
||||
int Image::SetX(int x) {
|
||||
return dclip.x = x;
|
||||
}
|
||||
|
||||
int Image::SetY(int y) {
|
||||
return dclip.y = y;
|
||||
}
|
||||
|
||||
int Image::SetClipX(int x) {
|
||||
return sclip.x = x;
|
||||
}
|
||||
|
||||
int Image::SetClipY(int y) {
|
||||
return sclip.y = y;
|
||||
}
|
||||
|
||||
int Image::SetWidth(int w) {
|
||||
return sclip.w = w;
|
||||
}
|
||||
|
||||
int Image::SetHeight(int h) {
|
||||
return sclip.h = h;
|
||||
}
|
||||
|
||||
int Image::GetX() {
|
||||
return dclip.x;
|
||||
}
|
||||
|
||||
int Image::GetY() {
|
||||
return dclip.y;
|
||||
}
|
||||
|
||||
int Image::GetClipX() {
|
||||
return sclip.x;
|
||||
}
|
||||
|
||||
int Image::GetClipY() {
|
||||
return sclip.y;
|
||||
}
|
||||
|
||||
int Image::GetWidth() {
|
||||
return sclip.w;
|
||||
}
|
||||
|
||||
int Image::GetHeight() {
|
||||
return sclip.h;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Protected access members
|
||||
//-------------------------
|
||||
|
||||
SDL_Surface* Image::SetSurface(SDL_Surface* surf) {
|
||||
return surface = surf;
|
||||
}
|
||||
|
||||
SDL_Surface* Image::GetSurface() {
|
||||
return surface;
|
||||
}
|
||||
|
||||
bool Image::LocalSurface() {
|
||||
return localSurface;
|
||||
}
|
||||
66
Hearts/image.h
Normal file
66
Hearts/image.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/* File Name: image.h
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 20/6/2011
|
||||
* Version: 3.1.0, Stable
|
||||
* Description: The Image class, a wrapper for SDL_Surface and other drawing functionality.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#ifndef KR_KAGE_IMAGE_H_
|
||||
#define KR_KAGE_IMAGE_H_
|
||||
|
||||
/* Version information */
|
||||
#define IMAGE_VERSION_MAJOR 3
|
||||
#define IMAGE_VERSION_MINOR 1
|
||||
#define IMAGE_VERSION_PATCH 0
|
||||
#define IMAGE_VERSION "3.1.0"
|
||||
#define IMAGE_VERSION_STRING "KAGE::Image Version 3.1.0, Stable"
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
namespace KAGE {
|
||||
class Image {
|
||||
public:
|
||||
/* Public access members */
|
||||
Image(const char* fileName, int x = 0, int y = 0, int w = 0, int h = 0);
|
||||
Image(SDL_Surface* surface, int x = 0, int y = 0, int w = 0, int h = 0);
|
||||
~Image();
|
||||
|
||||
/* Graphical members */
|
||||
virtual void Draw(SDL_Surface* dest, int camX = 0, int camY = 0);
|
||||
|
||||
int SetColorKey(int red, int green, int blue);
|
||||
int ClearColorKey();
|
||||
|
||||
/* Accessors and mutators */
|
||||
virtual void SetPos(int x, int y);
|
||||
virtual int SetX(int);
|
||||
virtual int SetY(int);
|
||||
virtual int SetClipX(int);
|
||||
virtual int SetClipY(int);
|
||||
virtual int SetWidth(int);
|
||||
virtual int SetHeight(int);
|
||||
|
||||
virtual int GetX();
|
||||
virtual int GetY();
|
||||
virtual int GetClipX();
|
||||
virtual int GetClipY();
|
||||
virtual int GetWidth();
|
||||
virtual int GetHeight();
|
||||
protected:
|
||||
/* Protected access members */
|
||||
void Init(int x, int y, int sx, int sy, int sw, int sh);
|
||||
|
||||
SDL_Surface* SetSurface (SDL_Surface* surf);
|
||||
SDL_Surface* GetSurface ();
|
||||
bool LocalSurface ();
|
||||
private:
|
||||
/* Private access members */
|
||||
SDL_Surface* surface;
|
||||
SDL_Rect sclip;
|
||||
SDL_Rect dclip;
|
||||
const bool localSurface;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
54
Hearts/image_manager.cpp
Normal file
54
Hearts/image_manager.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
/* File Name: image_manager.cpp
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 20/6/2011
|
||||
* Version: 4, Stable
|
||||
* Description: The Image Manager creates, organises and cleans up the Images in the game.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include "image_manager.h"
|
||||
using namespace std;
|
||||
using namespace KAGE;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
Image* ImageManager::Add(const char* name,const char* fName, int x, int y, int w, int h) {
|
||||
if (Exists(name)) {
|
||||
cout << "Error in ImageManager::Add(name,fName)" << endl;
|
||||
cout << "An Image of that name already exists" << endl;
|
||||
cout << "name: " << name << endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return NamedManager::Add(name,new Image(fName,x,y,w,h));
|
||||
}
|
||||
|
||||
Image* ImageManager::Add(const char* name,SDL_Surface* surface, int x, int y, int w, int h) {
|
||||
if (Exists(name)) {
|
||||
cout << "Error in ImageManager::Add(name,surface)" << endl;
|
||||
cout << "An Image of that name already exists" << endl;
|
||||
cout << "name: " << name << endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return NamedManager::Add(name,new Image(surface,x,y,w,h));
|
||||
}
|
||||
|
||||
Image* ImageManager::Add(const char *name,Image* image) {
|
||||
if (Exists(name)) {
|
||||
cout << "Error in ImageManager::Add(name,image)" << endl;
|
||||
cout << "An Image of that name already exists" << endl;
|
||||
cout << "name: " << name << endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return NamedManager::Add(name,image);
|
||||
}
|
||||
|
||||
void ImageManager::DrawAll(SDL_Surface* dest, int camX, int camY) {
|
||||
for (MapType::iterator it = mapList.begin(); it != mapList.end(); it++)
|
||||
it->second->Draw(dest,camX,camY);
|
||||
}
|
||||
34
Hearts/image_manager.h
Normal file
34
Hearts/image_manager.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/* File Name: image_manager.h
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 20/6/2011
|
||||
* Version: 4, Stable
|
||||
* Description: The Image Manager creates, organises and cleans up the Images in the game.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#ifndef KR_KAGE_IMAGEMANAGER_H_
|
||||
#define KR_KAGE_IMAGEMANAGER_H_
|
||||
|
||||
/* Version information */
|
||||
#define IMAGEMANAGER_VERSION_MAJOR 4
|
||||
#define IMAGEMANAGER_VERSION_MINOR 0
|
||||
#define IMAGEMANAGER_VERSION_PATCH 0
|
||||
#define IMAGEMANAGER_VERSION "4.0.0"
|
||||
#define IMAGEMANAGER_VERSION_STRING "KAGE::ImageManager Version 4, Stable"
|
||||
|
||||
#include "named_manager.h"
|
||||
#include "image.h"
|
||||
|
||||
namespace KAGE {
|
||||
class ImageManager : public NamedManager<Image> {
|
||||
public:
|
||||
/* Public access members */
|
||||
Image* Add(const char* name, const char* fileName, int x = 0, int y = 0, int w = 0, int h = 0);
|
||||
Image* Add(const char* name, SDL_Surface* surface, int x = 0, int y = 0, int w = 0, int h = 0);
|
||||
Image* Add(const char* name, Image* image);
|
||||
|
||||
void DrawAll(SDL_Surface* dest, int camX = 0, int camY = 0);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
96
Hearts/music.cpp
Normal file
96
Hearts/music.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
/* File Name: music.cpp
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 21/6/2011
|
||||
* Version: 1.0.1, Stable
|
||||
* Description: The Music class, a wrapper for Mix_Music and other sound functionality.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include "music.h"
|
||||
using namespace std;
|
||||
using namespace KAGE;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
Music::Music() {
|
||||
music = NULL;
|
||||
}
|
||||
|
||||
Music::~Music() {
|
||||
Free();
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Accessors and mutators
|
||||
//-------------------------
|
||||
|
||||
int Music::Load(const char* fName) {
|
||||
Free();
|
||||
|
||||
music = Mix_LoadMUS(fName);
|
||||
|
||||
if (music == NULL) {
|
||||
cout << "Error in Music::Load()" << endl;
|
||||
cout << "Mix_LoadMUS() returned NULL" << endl;
|
||||
cout << "fName: " << fName << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Music::Free() {
|
||||
if (music != NULL) {
|
||||
Mix_FreeMusic(music);
|
||||
music = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int Music::Play(int loop) {
|
||||
if (music == NULL) {
|
||||
cout << "Error in Music::Play()" << endl;
|
||||
cout << "There is no music loaded" << endl;
|
||||
return -2;
|
||||
}
|
||||
|
||||
return Mix_PlayMusic(music,loop);
|
||||
}
|
||||
|
||||
void Music::Halt() {
|
||||
Mix_HaltMusic();
|
||||
}
|
||||
|
||||
int Music::Playing() {
|
||||
return Mix_PlayingMusic();
|
||||
}
|
||||
|
||||
void Music::Pause() {
|
||||
Mix_PauseMusic();
|
||||
}
|
||||
|
||||
void Music::Resume() {
|
||||
Mix_ResumeMusic();
|
||||
}
|
||||
|
||||
int Music::Paused() {
|
||||
return Mix_PausedMusic();
|
||||
}
|
||||
|
||||
void Music::Rewind() {
|
||||
Mix_RewindMusic();
|
||||
}
|
||||
|
||||
int Music::Volume(int volume) {
|
||||
return Mix_VolumeMusic(volume);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Protected access members
|
||||
//-------------------------
|
||||
|
||||
Mix_Music* Music::GetMixMusic() {
|
||||
return music;
|
||||
}
|
||||
50
Hearts/music.h
Normal file
50
Hearts/music.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/* File Name: music.h
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 21/6/2011
|
||||
* Version: 1.0.1, Stable
|
||||
* Description: The Music class, a wrapper for Mix_Music and other sound functionality.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#ifndef KR_KAGE_MUSIC_H_
|
||||
#define KR_KAGE_MUSIC_H_
|
||||
|
||||
/* Version information */
|
||||
#define MUSIC_VERSION_MAJOR 1
|
||||
#define MUSIC_VERSION_MINOR 0
|
||||
#define MUSIC_VERSION_PATCH 1
|
||||
#define MUSIC_VERSION "1.0.1"
|
||||
#define MUSIC_VERSION_STRING "KAGE::Music Version 1.0.1, Stable"
|
||||
|
||||
#include "SDL_mixer.h"
|
||||
|
||||
namespace KAGE {
|
||||
class Music {
|
||||
public:
|
||||
/* Public access members */
|
||||
Music();
|
||||
~Music();
|
||||
|
||||
/* Accessors and mutators */
|
||||
int Load (const char* fName);
|
||||
void Free ();
|
||||
|
||||
int Play (int loop = -1);
|
||||
void Halt ();
|
||||
int Playing ();
|
||||
void Pause ();
|
||||
void Resume ();
|
||||
int Paused ();
|
||||
void Rewind ();
|
||||
|
||||
int Volume (int volume);
|
||||
protected:
|
||||
/* Protected access members */
|
||||
Mix_Music* GetMixMusic();
|
||||
private:
|
||||
/* Private access members */
|
||||
Mix_Music* music;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
117
Hearts/named_manager.h
Normal file
117
Hearts/named_manager.h
Normal file
@@ -0,0 +1,117 @@
|
||||
/* File Name: named_manager.h
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 21/6/2011
|
||||
* Version: 1.0.1, Stable
|
||||
* Description: The NamedManager template class acts as a parent for all of Kage's managers.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#ifndef KR_KAGE_NAMEDMANAGER_H_
|
||||
#define KR_KAGE_NAMEDMANAGER_H_
|
||||
|
||||
/* Version information */
|
||||
#define NAMEDMANAGER_VERSION_MAJOR 1
|
||||
#define NAMEDMANAGER_VERSION_MINOR 0
|
||||
#define NAMEDMANAGER_VERSION_PATCH 1
|
||||
#define NAMEDMANAGER_VERSION "1.0.1"
|
||||
#define NAMEDMANAGER_VERSION_STRING "KAGE::Manager Version 1.0.1, Stable"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace KAGE {
|
||||
template<typename T>
|
||||
class NamedManager {
|
||||
public:
|
||||
/* Public access members */
|
||||
~NamedManager ();
|
||||
|
||||
T* Get (const char* name);
|
||||
T* operator[] (const char* name);
|
||||
T* Remove (const char* name);
|
||||
void Delete (const char* name);
|
||||
void DeleteAll ();
|
||||
bool Exists (const char* name);
|
||||
int Size ();
|
||||
protected:
|
||||
/* Protected access members */
|
||||
T* Add (const char* name,T* item);
|
||||
|
||||
typedef std::map<std::string,T*> MapType;
|
||||
MapType mapList;
|
||||
};
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Protected access members
|
||||
//-------------------------
|
||||
|
||||
template<typename T>
|
||||
T* KAGE::NamedManager<T>::Add(const char* name,T* item) {
|
||||
if (Exists(name))
|
||||
return NULL;
|
||||
return mapList[name] = item;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
template<typename T>
|
||||
KAGE::NamedManager<T>::~NamedManager() {
|
||||
DeleteAll();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T* KAGE::NamedManager<T>::Get(const char* name) {
|
||||
MapType::const_iterator it = mapList.find(name);
|
||||
if (it != mapList.end())
|
||||
return it->second;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T* KAGE::NamedManager<T>::operator[](const char* name) {
|
||||
return Get(name);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T* KAGE::NamedManager<T>::Remove(const char* name) {
|
||||
MapType::const_iterator it = mapList.find(name);
|
||||
if (it == mapList.end())
|
||||
return NULL;
|
||||
|
||||
T* ret = it->second;
|
||||
mapList.erase(it);
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void KAGE::NamedManager<T>::Delete(const char* name) {
|
||||
MapType::const_iterator it = mapList.find(name);
|
||||
if (it != mapList.end()) {
|
||||
delete it->second;
|
||||
mapList.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void KAGE::NamedManager<T>::DeleteAll() {
|
||||
for (MapType::iterator it = mapList.begin(); it != mapList.end(); it++)
|
||||
delete it->second;
|
||||
mapList.clear();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool KAGE::NamedManager<T>::Exists(const char* name) {
|
||||
MapType::const_iterator it = mapList.find(name);
|
||||
return it != mapList.end();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int KAGE::NamedManager<T>::Size() {
|
||||
return mapList.size();
|
||||
}
|
||||
|
||||
#endif
|
||||
155
Hearts/network_manager.cpp
Normal file
155
Hearts/network_manager.cpp
Normal file
@@ -0,0 +1,155 @@
|
||||
/* File Name: network_manager.cpp
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 21/6/2011
|
||||
* Version: 3, Stable
|
||||
* Description: The Network Manager deals with the networking for your game.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include "network_manager.h"
|
||||
using namespace std;
|
||||
using namespace KAGE;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
NetworkManager::~NetworkManager() {
|
||||
SDLNet_FreePacket(packet);
|
||||
SDLNet_Quit();
|
||||
}
|
||||
|
||||
int NetworkManager::Init(int port, int packetSize) {
|
||||
if (SDLNet_Init() != 0) {
|
||||
cout << "Error in NetworkManager::Init()" << endl;
|
||||
cout << "Failed to initialise SDL_net" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet = SDLNet_AllocPacket(packetSize);
|
||||
if (packet == NULL) {
|
||||
cout << "Error in NetworkManager::Init()" << endl;
|
||||
cout << "SDLNet_AllocPacket() returned NULL" << endl;
|
||||
cout << "packetSize: " << packetSize << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
socket = SDLNet_UDP_Open(port);
|
||||
if (socket == NULL) {
|
||||
cout << "Error in NetworkManager::Init()" << endl;
|
||||
cout << "SDLNet_UDP_Open() returned NULL" << endl;
|
||||
cout << "port: " << port << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
IPaddress* NetworkManager::Add(const char* name, char* ip, int port) {
|
||||
if (Exists(name)) {
|
||||
cout << "Error in NetworkManager::Add()" << endl;
|
||||
cout << "An IPaddress of that name already exists" << endl;
|
||||
cout << "name: " << name << endl;
|
||||
cout << "ip: " << ip << endl;
|
||||
cout << "port: " << port << endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
IPaddress* add = new IPaddress;
|
||||
|
||||
if (SDLNet_ResolveHost(add,ip,port) != 0) {
|
||||
cout << "Error in NetworkManager::Add(name,ip,port)" << endl;
|
||||
cout << "Failed to resolve an IPaddress" << endl;
|
||||
cout << "name: " << name << endl;
|
||||
cout << "ip: " << ip << endl;
|
||||
cout << "port: " << port << endl;
|
||||
delete add;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return NamedManager::Add(name,add);
|
||||
}
|
||||
|
||||
IPaddress* NetworkManager::Add(const char* name, IPaddress* address) {
|
||||
if (Exists(name)) {
|
||||
cout << "Error in NetworkManager::Add()" << endl;
|
||||
cout << "An IPaddress of that name already exists" << endl;
|
||||
cout << "name: " << name << endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return NamedManager::Add(name,address);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Network members
|
||||
//-------------------------
|
||||
int NetworkManager::Send(const char* name, const char* data) {
|
||||
IPaddress* add = Get(name);
|
||||
if (add == NULL) {
|
||||
cout << "Error in NetworkManager::Send(name,data)" << endl;
|
||||
cout << "No such IPaddress with that name" << endl;
|
||||
cout << "name: " << name << endl;
|
||||
return -2;
|
||||
}
|
||||
|
||||
return Send(add,data);
|
||||
}
|
||||
|
||||
int NetworkManager::Send(IPaddress* address, const char* data) {
|
||||
//send this data to the address
|
||||
strcpy_s((char*)packet->data,512,data);
|
||||
packet->address = *address;
|
||||
|
||||
packet->len = strlen((char*)packet->data) + 1;
|
||||
SDLNet_UDP_Send(socket,-1,packet);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int NetworkManager::Send(char* ip, int port, const char* data) {
|
||||
IPaddress* add = new IPaddress;
|
||||
|
||||
if (SDLNet_ResolveHost(add,ip,port) != 0) {
|
||||
cout << "Error in NetworkManager::Send(ip,port,data)" << endl;
|
||||
cout << "Failed to resolve an IPaddress" << endl;
|
||||
cout << "ip: " << ip << endl;
|
||||
cout << "port: " << port << endl;
|
||||
cout << "data: " << data << endl;
|
||||
delete add;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = Send(add,data);
|
||||
delete add;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void NetworkManager::SendAll(const char* data) {
|
||||
for (MapType::iterator it = mapList.begin(); it != mapList.end(); it++)
|
||||
Send(it->second,data);
|
||||
}
|
||||
|
||||
int NetworkManager::Receive() {
|
||||
return SDLNet_UDP_Recv(socket, packet);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Accessors and mutators
|
||||
//-------------------------
|
||||
|
||||
const char* NetworkManager::Data() {
|
||||
return (char*)packet->data;
|
||||
}
|
||||
|
||||
bool NetworkManager::CheckData(const char* str) {
|
||||
return !strcmp((char*)packet->data,str);
|
||||
}
|
||||
|
||||
bool NetworkManager::CheckData(const char* str, int num) {
|
||||
return !strncmp((char*)packet->data,str,num);
|
||||
}
|
||||
|
||||
UDPpacket* NetworkManager::Packet() {
|
||||
return packet;
|
||||
}
|
||||
52
Hearts/network_manager.h
Normal file
52
Hearts/network_manager.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/* File Name: network_manager.h
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 21/6/2011
|
||||
* Version: 3, Stable
|
||||
* Description: The Network Manager deals with the networking for your game.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#ifndef KR_KAGE_NETWORKMANAGER_H_
|
||||
#define KR_KAGE_NETWORKMANAGER_H_
|
||||
|
||||
/* Version information */
|
||||
#define NETWORKMANAGER_VERSION_MAJOR 3
|
||||
#define NETWORKMANAGER_VERSION_MINOR 0
|
||||
#define NETWORKMANAGER_VERSION_PATCH 0
|
||||
#define NETWORKMANAGER_VERSION "3.0.0"
|
||||
#define NETWORKMANAGER_VERSION_STRING "KAGE::NetworkManager Version 3, Stable"
|
||||
|
||||
#include "SDL_net.h"
|
||||
#include "named_manager.h"
|
||||
|
||||
namespace KAGE {
|
||||
class NetworkManager : public NamedManager<IPaddress> {
|
||||
public:
|
||||
/* Public access members */
|
||||
~NetworkManager();
|
||||
|
||||
int Init(int port, int packetSize = 512);
|
||||
|
||||
IPaddress* Add(const char* name, char* ip, int port);
|
||||
IPaddress* Add(const char* name, IPaddress* address);
|
||||
|
||||
/* Network members */
|
||||
int Send(const char* name, const char* data);
|
||||
int Send(IPaddress* address, const char* data);
|
||||
int Send(char* ip, int port, const char* data);
|
||||
void SendAll(const char* data);
|
||||
int Receive();
|
||||
|
||||
/* Accessors and mutators */
|
||||
const char* Data();
|
||||
bool CheckData(const char* str);
|
||||
bool CheckData(const char* str, int num);
|
||||
UDPpacket* Packet();
|
||||
|
||||
/* Private access members */
|
||||
UDPsocket socket;
|
||||
UDPpacket* packet;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
197
Hearts/object.cpp
Normal file
197
Hearts/object.cpp
Normal file
@@ -0,0 +1,197 @@
|
||||
/* File Name: object.cpp
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 9/7/2011
|
||||
* Version: 4, Stable
|
||||
* Description: The Object class, with movement and collision functionality.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include "object.h"
|
||||
using namespace std;
|
||||
using namespace KAGE;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
Object::Object(float _realX, float _realY, int boxX, int boxY, int boxWidth, int boxHeight, float _radius) {
|
||||
realX = _realX;
|
||||
realY = _realY;
|
||||
motionX = 0;
|
||||
motionY = 0;
|
||||
boundingBox.x = boxX;
|
||||
boundingBox.y = boxY;
|
||||
boundingBox.w = boxWidth;
|
||||
boundingBox.h = boxHeight;
|
||||
radius = _radius;
|
||||
}
|
||||
|
||||
void Object::Process(int delta) {
|
||||
realX += delta * motionX + motionX;
|
||||
realY += delta * motionY + motionY;
|
||||
UpdateClip();
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Accessors and mutators
|
||||
//-------------------------
|
||||
|
||||
void Object::SetPosition(float _realX, float _realY) {
|
||||
realX = _realX;
|
||||
realY = _realY;
|
||||
UpdateClip();
|
||||
}
|
||||
|
||||
void Object::ShiftPosition(float _realX, float _realY) {
|
||||
realX += _realX;
|
||||
realY += _realY;
|
||||
UpdateClip();
|
||||
}
|
||||
|
||||
float Object::SetRealX(float _realX) {
|
||||
realX = _realX;
|
||||
UpdateClip();
|
||||
return realX;
|
||||
}
|
||||
|
||||
float Object::SetRealY(float _realY) {
|
||||
realY = _realY;
|
||||
UpdateClip();
|
||||
return realY;
|
||||
}
|
||||
|
||||
float Object::GetRealX() {
|
||||
return realX;
|
||||
}
|
||||
|
||||
float Object::GetRealY() {
|
||||
return realY;
|
||||
}
|
||||
|
||||
void Object::SetMotion(float _motionX, float _motionY) {
|
||||
motionX = _motionX;
|
||||
motionY = _motionY;
|
||||
}
|
||||
|
||||
float Object::SetMotionX(float _motionX) {
|
||||
return motionX = _motionX;
|
||||
}
|
||||
|
||||
float Object::SetMotionY(float _motionY) {
|
||||
return motionY = _motionY;
|
||||
}
|
||||
|
||||
void Object::ShiftMotion(float _motionX, float _motionY) {
|
||||
motionX += _motionX;
|
||||
motionY += _motionY;
|
||||
}
|
||||
|
||||
float Object::ShiftMotionX(float _motionX) {
|
||||
return motionX += _motionX;
|
||||
}
|
||||
|
||||
float Object::ShiftMotionY(float _motionY) {
|
||||
return motionY += _motionY;
|
||||
}
|
||||
|
||||
void Object::StopMotion() {
|
||||
motionX = 0;
|
||||
motionY = 0;
|
||||
}
|
||||
|
||||
float Object::GetMotionX() {
|
||||
return motionX;
|
||||
}
|
||||
|
||||
float Object::GetMotionY() {
|
||||
return motionY;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Collision code
|
||||
//-------------------------
|
||||
|
||||
SDL_Rect Object::SetBoundingBox(int x, int y, int w, int h) {
|
||||
boundingBox.x = x;
|
||||
boundingBox.y = y;
|
||||
boundingBox.w = w;
|
||||
boundingBox.h = h;
|
||||
return boundingBox;
|
||||
}
|
||||
|
||||
SDL_Rect Object::SetBoundingBox(SDL_Rect _boundingBox) {
|
||||
return boundingBox = _boundingBox;
|
||||
}
|
||||
|
||||
float Object::SetRadius(float _radius) {
|
||||
return radius = _radius;
|
||||
}
|
||||
|
||||
SDL_Rect* Object::GetBoundingBox() {
|
||||
return &boundingBox;
|
||||
}
|
||||
|
||||
SDL_Rect Object::GetWorldBoundingBox() {
|
||||
SDL_Rect ret = boundingBox;
|
||||
ret.x += (Sint16)realX;
|
||||
ret.y += (Sint16)realY;
|
||||
ret.w += (Uint16)realX;
|
||||
ret.h += (Uint16)realY;
|
||||
return ret;
|
||||
}
|
||||
|
||||
float Object::GetRadius() {
|
||||
return radius;
|
||||
}
|
||||
|
||||
bool Object::CheckWorldBoundingBox(SDL_Rect arg) {
|
||||
if (radius > 0) {
|
||||
cout << "Error in Object::CheckWorldBoundingBox()" << endl;
|
||||
cout << "Wrong collision type" << endl;
|
||||
cout << "radius: " << radius << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_Rect box = GetWorldBoundingBox();
|
||||
if ((int)arg.x < (int)box.w &&
|
||||
(int)arg.w > (int)box.x &&
|
||||
(int)arg.y < (int)box.h &&
|
||||
(int)arg.h > (int)box.y)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
inline float sqr(float f) {
|
||||
return f*f;
|
||||
}
|
||||
|
||||
bool Object::CheckRadius(float _realX, float _realY, float _radius) {
|
||||
if (radius <= 0) {
|
||||
cout << "Error in Object::CheckRadius()" << endl;
|
||||
cout << "Wrong collision type" << endl;
|
||||
cout << "radius: " << radius << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (sqr(radius + _radius) > sqr(realX - _realX) + sqr(realY - _realY))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Object::CheckPoint(float _realX, float _realY) {
|
||||
if (radius > 0) {
|
||||
cout << "Error in Object::CheckPoint()" << endl;
|
||||
cout << "Wrong collision type" << endl;
|
||||
cout << "radius: " << radius << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_Rect box = GetWorldBoundingBox();
|
||||
if ((int)box.x < (int)_realX &&
|
||||
(int)box.y < (int)_realY &&
|
||||
(int)box.w > (int)_realX &&
|
||||
(int)box.h > (int)_realY)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
78
Hearts/object.h
Normal file
78
Hearts/object.h
Normal file
@@ -0,0 +1,78 @@
|
||||
/* File Name: object.h
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 9/7/2011
|
||||
* Version: 4, Stable
|
||||
* Description: The Object class, with movement and collision functionality.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#ifndef KR_KAGE_OBJECT_H_
|
||||
#define KR_KAGE_OBJECT_H_
|
||||
|
||||
/* Version information */
|
||||
#define OBJECT_VERSION_MAJOR 4
|
||||
#define OBJECT_VERSION_MINOR 0
|
||||
#define OBJECT_VERSION_PATCH 0
|
||||
#define OBJECT_VERSION "4.0.0"
|
||||
#define OBJECT_VERSION_STRING "KAGE::Object Version 4, Stable"
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
namespace KAGE {
|
||||
class Object {
|
||||
public:
|
||||
/* Public access members */
|
||||
Object(float realX = 0, float realY = 0, int boxX = 0, int boxY = 0, int boxWidth = 0, int boxHeight = 0, float radius = 0);
|
||||
|
||||
virtual void Process(int delta);
|
||||
|
||||
/* Accessors and mutators */
|
||||
virtual void SetPosition (float realX, float realY);
|
||||
virtual void ShiftPosition (float realX, float realY);
|
||||
virtual float SetRealX (float realX);
|
||||
virtual float SetRealY (float realY);
|
||||
|
||||
virtual float GetRealX ();
|
||||
virtual float GetRealY ();
|
||||
|
||||
virtual void SetMotion (float motionX, float motionY);
|
||||
virtual float SetMotionX (float motionX);
|
||||
virtual float SetMotionY (float motionY);
|
||||
|
||||
virtual void ShiftMotion (float motionX, float motionY);
|
||||
virtual float ShiftMotionX (float motionX);
|
||||
virtual float ShiftMotionY (float motionY);
|
||||
|
||||
virtual void StopMotion ();
|
||||
|
||||
virtual float GetMotionX ();
|
||||
virtual float GetMotionY ();
|
||||
|
||||
/* Collision code */
|
||||
virtual SDL_Rect SetBoundingBox (int x, int y, int w, int h);
|
||||
virtual SDL_Rect SetBoundingBox (SDL_Rect boundingBox);
|
||||
virtual float SetRadius (float radius);
|
||||
|
||||
virtual SDL_Rect* GetBoundingBox ();
|
||||
virtual SDL_Rect GetWorldBoundingBox ();
|
||||
virtual float GetRadius ();
|
||||
|
||||
virtual bool CheckWorldBoundingBox (SDL_Rect boundingBox);
|
||||
virtual bool CheckRadius (float realX, float realY, float radius = 0);
|
||||
virtual bool CheckPoint (float realX, float realY);
|
||||
protected:
|
||||
/* Protected access members */
|
||||
virtual void UpdateClip()=0;
|
||||
private:
|
||||
/* Private access members */
|
||||
float realX;
|
||||
float realY;
|
||||
float motionX;
|
||||
float motionY;
|
||||
|
||||
SDL_Rect boundingBox;
|
||||
float radius;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
193
Hearts/player.cpp
Normal file
193
Hearts/player.cpp
Normal file
@@ -0,0 +1,193 @@
|
||||
/* File Name: player.cpp
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: 5/6/2011
|
||||
* Version:
|
||||
* Description: Designed for Project Hearts, 4th try.
|
||||
*/
|
||||
#include "player.h"
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
Player::Player() {
|
||||
for (int i = 0; i < 3; i++)
|
||||
swapCards[i] = -1;
|
||||
score = 0;
|
||||
wins = 0;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Card members
|
||||
//-------------------------
|
||||
|
||||
CardList* Player::Hand() {
|
||||
return &hand;
|
||||
}
|
||||
|
||||
CardList* Player::Tricks() {
|
||||
return &tricks;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Score members
|
||||
//-------------------------
|
||||
|
||||
int Player::Score(int _score) {
|
||||
return score += _score;
|
||||
}
|
||||
|
||||
int Player::Wins(int _wins) {
|
||||
return wins += _wins;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Swapping members
|
||||
//-------------------------
|
||||
|
||||
void BubbleSwap(int& a, int& b) {
|
||||
if (a > b) return;
|
||||
int tmp = a;
|
||||
a = b;
|
||||
b = tmp;
|
||||
}
|
||||
|
||||
Card* Player::PassSwapCards() {
|
||||
if (!CheckSwapCards())
|
||||
return NULL;
|
||||
|
||||
//bubble sort-ish
|
||||
BubbleSwap(swapCards[0],swapCards[1]);
|
||||
BubbleSwap(swapCards[1],swapCards[2]);
|
||||
BubbleSwap(swapCards[0],swapCards[1]);
|
||||
|
||||
CardList cList;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
cList.Receive(hand.Pass(swapCards[i]));
|
||||
swapCards[i] = -1;
|
||||
}
|
||||
|
||||
return cList.PassSlab(0,3);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Graphical members
|
||||
//-------------------------
|
||||
|
||||
void Player::ShiftCard(int index, int dirX, int dirY) {
|
||||
hand.Read(index)->SetPos(
|
||||
hand.Read(index)->GetX() + dirX,
|
||||
hand.Read(index)->GetY() + dirY
|
||||
);
|
||||
}
|
||||
|
||||
void Player::SetFaces(int face) {
|
||||
for (CardList::iterator it = hand.Head(); it != NULL; it = it->GetNext())
|
||||
it->SetFace(face);
|
||||
}
|
||||
|
||||
void Player::SetPositions(int _posX, int _posY, int _dirX, int _dirY) {
|
||||
posX = _posX;
|
||||
posY = _posY;
|
||||
dirX = _dirX;
|
||||
dirY = _dirY;
|
||||
}
|
||||
|
||||
void Player::SetHandPositions() {
|
||||
int x = posX, y = posY;
|
||||
|
||||
for (int i = 0; i < hand.Size(); i++) {
|
||||
hand.Read(i)->SetPos(x,y);
|
||||
x += dirX;
|
||||
y += dirY;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::SetTrickPositions() {
|
||||
int x = posX, y = posY;
|
||||
|
||||
tricks.Sort();
|
||||
|
||||
for (int i = 0; i < tricks.Size(); i++) {
|
||||
if (tricks.Read(i)->Suit() != Card::HEARTS && !ISCARD(tricks.Read(i),QUEEN,SPADES))
|
||||
continue;
|
||||
|
||||
tricks.Read(i)->SetPos(x,y);
|
||||
x += dirX;
|
||||
y += dirY;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::DrawHand(SDL_Surface* dest) {
|
||||
Hand()->DrawAll(dest);
|
||||
}
|
||||
|
||||
void Player::DrawTricks(SDL_Surface* dest) {
|
||||
tricks.Sort();
|
||||
|
||||
for (int i = 0; i < tricks.Size(); i++) {
|
||||
if (tricks.Read(i)->Suit() != Card::HEARTS && !ISCARD(tricks.Read(i),QUEEN,SPADES))
|
||||
continue;
|
||||
tricks.Read(i)->Draw(dest);
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Swapping members
|
||||
//-------------------------
|
||||
|
||||
void Player::AddSwapCards(int card) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (swapCards[i] == -1) {
|
||||
swapCards[i] = card;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Player::CheckSwapCards(int card) {
|
||||
for (int i = 0; i < 3; i++)
|
||||
if (card == swapCards[i])
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Player::CheckSwapCards() {
|
||||
for (int i = 0; i < 3; i++)
|
||||
if (swapCards[i] == -1)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Player::RemoveSwapCards(int card) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (swapCards[i] == card) {
|
||||
swapCards[i] = -1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int Player::CountSwapCards() {
|
||||
int count = 0;
|
||||
for (int i = 0; i < 3; i++)
|
||||
if (swapCards[i] != -1)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
71
Hearts/player.h
Normal file
71
Hearts/player.h
Normal file
@@ -0,0 +1,71 @@
|
||||
/* File Name: player.h
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: 5/6/2011
|
||||
* Version:
|
||||
* Description: Designed for Project Hearts, 4th try.
|
||||
*/
|
||||
#ifndef KR_PLAYER_H_
|
||||
#define KR_PLAYER_H_
|
||||
#include "SDL.h"
|
||||
#include "card_list.h"
|
||||
|
||||
class Player {
|
||||
public:
|
||||
/* Public access members */
|
||||
Player();
|
||||
|
||||
/* Card members */
|
||||
CardList* Hand();
|
||||
CardList* Tricks();
|
||||
|
||||
/* Score members */
|
||||
int Score(int score = 0);
|
||||
int Wins(int wins = 0);
|
||||
|
||||
/* Swapping members */
|
||||
Card* PassSwapCards();
|
||||
|
||||
/* Graphical members */
|
||||
void ShiftCard(int index, int dirX, int dirY);
|
||||
void SetFaces(int face);
|
||||
void SetPositions(int posX, int posY, int dirX, int dirY);
|
||||
void SetHandPositions();
|
||||
void SetTrickPositions();
|
||||
void DrawHand(SDL_Surface*);
|
||||
void DrawTricks(SDL_Surface*);
|
||||
|
||||
bool CheckSwapCards();
|
||||
protected:
|
||||
/* Swapping members */
|
||||
void AddSwapCards(int);
|
||||
bool CheckSwapCards(int);
|
||||
void RemoveSwapCards(int);
|
||||
int CountSwapCards();
|
||||
|
||||
int swapCards[3];
|
||||
private:
|
||||
CardList hand;
|
||||
CardList tricks;
|
||||
int score;
|
||||
int wins;
|
||||
|
||||
int posX, posY, dirX, dirY;
|
||||
};
|
||||
|
||||
#endif
|
||||
139
Hearts/player_ai.cpp
Normal file
139
Hearts/player_ai.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
/* File Name: player_ai.cpp
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: 6/6/2011
|
||||
* Version:
|
||||
* Description: Designed for Project Hearts, 4th try.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include "player_ai.h"
|
||||
using namespace std;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
void PlayerAI::SelectSwapCards(int dirX, int dirY) {
|
||||
for (int pass = 0; pass < 3; pass++) {
|
||||
int card = -1;
|
||||
|
||||
for (int i = Hand()->Size() - 1; i >= 0; i--) {
|
||||
if (Hand()->Read(i)->Rank() > Hand()->Read(card)->Rank() && !CheckSwapCards(i) ) {
|
||||
card = i;
|
||||
}
|
||||
}
|
||||
|
||||
//sloppy bugfix, doesn't select the best card to swap, rule of good-enough; I'd like to remake this one day
|
||||
while(CheckSwapCards(card))
|
||||
card++;
|
||||
|
||||
AddSwapCards(card);
|
||||
ShiftCard(card,dirX, dirY);
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Gameplay members
|
||||
//-------------------------
|
||||
|
||||
Card* PlayerAI::PassPlayCard(int leadingSuit, int trickCount, bool& heartsBroken) {
|
||||
//rules
|
||||
if (trickCount == 0)
|
||||
return PassPlayFirstTrick(leadingSuit);
|
||||
|
||||
Card* c = PassPlayFollowSuit(leadingSuit,heartsBroken);
|
||||
if (c != NULL)
|
||||
return c;
|
||||
|
||||
return PassPlayBreakSuit(heartsBroken);
|
||||
}
|
||||
|
||||
Card* PlayerAI::PassPlayFirstTrick(int leadingSuit) {
|
||||
//2 of clubs
|
||||
if (ISCARD(Hand()->Read(),TWO,CLUBS))
|
||||
return Hand()->Pass();
|
||||
|
||||
//follow suit
|
||||
for(int i = 0; i < Hand()->Size()-1; i++)
|
||||
if (Hand()->Read(i)->Suit() == leadingSuit)
|
||||
return Hand()->Pass(i);
|
||||
|
||||
//can't follow suit, no penalty cards
|
||||
Hand()->SortRank();
|
||||
Card* c = NULL;
|
||||
|
||||
for (int i = Hand()->Size() -1; i >= 0; i--) {//backwards, to choose the highest rank
|
||||
c = Hand()->Read(i);
|
||||
if (c->Suit() != Card::HEARTS && !ISCARD(c,QUEEN,SPADES) ) {
|
||||
c = Hand()->Pass(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Hand()->Sort();
|
||||
return c;
|
||||
}
|
||||
|
||||
Card* PlayerAI::PassPlayFollowSuit(int leadingSuit, bool heartsBroken) {
|
||||
//Leading the suit, play a card, depending on heartsBroken
|
||||
if (leadingSuit == -1) {
|
||||
Hand()->SortRank();
|
||||
Card* c = NULL;
|
||||
|
||||
if (heartsBroken) {
|
||||
c = Hand()->Pass();
|
||||
}
|
||||
else {
|
||||
for (int i = 0; i < Hand()->Size() -1; i++) {
|
||||
if (Hand()->Read(i)->Suit() != Card::HEARTS) {
|
||||
c = Hand()->Pass(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Hand()->Sort();
|
||||
return c;
|
||||
}
|
||||
|
||||
//play the lowest card of the suit
|
||||
for (int i = 0; i < Hand()->Size()-1; i++)
|
||||
if (Hand()->Read(i)->Suit() == leadingSuit)
|
||||
return Hand()->Pass(i);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Card* PlayerAI::PassPlayBreakSuit(bool& heartsBroken) {
|
||||
//queen of spades
|
||||
for (int i = 0; i < Hand()->Size() -1; i++)
|
||||
if ( ISCARD(Hand()->Read(i),QUEEN,SPADES) )
|
||||
return Hand()->Pass(i);
|
||||
|
||||
//if I have a heart
|
||||
if (Hand()->Read( Hand()->Size()-1 )->Suit() == Card::HEARTS) {
|
||||
heartsBroken = true;
|
||||
return Hand()->Pass( Hand()->Size()-1 );
|
||||
}
|
||||
|
||||
//highest ranked card
|
||||
Hand()->SortRank();
|
||||
Card* c = Hand()->Pass( Hand()->Size()-1 );
|
||||
Hand()->Sort();
|
||||
return c;
|
||||
}
|
||||
39
Hearts/player_ai.h
Normal file
39
Hearts/player_ai.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/* File Name: player_ai.h
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: 6/6/2011
|
||||
* Version:
|
||||
* Description: Designed for Project Hearts, 4th try.
|
||||
*/
|
||||
#ifndef KR_PLAYERAI_H_
|
||||
#define KR_PLAYERAI_H_
|
||||
#include "player.h"
|
||||
|
||||
class PlayerAI : public Player {
|
||||
public:
|
||||
/* Public access members */
|
||||
void SelectSwapCards(int dirX, int dirY);
|
||||
Card* PassPlayCard(int leadingSuit, int trickCount, bool& heartsBroken);
|
||||
protected:
|
||||
/* Protected access members */
|
||||
Card* PassPlayFirstTrick(int leadingSuit);
|
||||
Card* PassPlayFollowSuit(int leadingSuit, bool heartsBroken);
|
||||
Card* PassPlayBreakSuit(bool& heartsBroken);
|
||||
};
|
||||
|
||||
#endif
|
||||
143
Hearts/player_user.cpp
Normal file
143
Hearts/player_user.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
/* File Name: player_user.cpp
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: 5/6/2011
|
||||
* Version:
|
||||
* Description: Designed for Project Hearts, 4th try.
|
||||
*/
|
||||
#include "player_user.h"
|
||||
|
||||
void PlayerUser::SelectSwapCard(int mouseX, int mouseY) {
|
||||
//get the index of the card at (x,y)
|
||||
int card = -1;
|
||||
|
||||
for (int index = 0; index < Hand()->Size();index++) {
|
||||
if (mouseX < Hand()->Read(index)->GetX())
|
||||
continue;
|
||||
if (mouseY < Hand()->Read(index)->GetY())
|
||||
continue;
|
||||
if (mouseX > Hand()->Read(index)->GetX() + Hand()->Read(index)->GetWidth())
|
||||
continue;
|
||||
if (mouseY > Hand()->Read(index)->GetY() + Hand()->Read(index)->GetHeight())
|
||||
continue;
|
||||
|
||||
card = index;
|
||||
}
|
||||
|
||||
//select or deselect the card found
|
||||
if (card != -1 && !CheckSwapCards(card) && CountSwapCards() < 3) {
|
||||
AddSwapCards(card);
|
||||
ShiftCard(card,0,-20);
|
||||
}
|
||||
else if (card != -1 && CheckSwapCards(card)) {
|
||||
RemoveSwapCards(card);
|
||||
ShiftCard(card,0,20);
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Gameplay members
|
||||
//-------------------------
|
||||
|
||||
Card* PlayerUser::PassPlayCard(int mouseX, int mouseY, int leadingSuit, int trickCount, bool& heartsBroken) {
|
||||
//find the selected card
|
||||
int card = -1;
|
||||
|
||||
for (int index = 0; index < Hand()->Size();index++) {
|
||||
if (mouseX < Hand()->Read(index)->GetX()) continue;
|
||||
if (mouseY < Hand()->Read(index)->GetY()) continue;
|
||||
if (mouseX > Hand()->Read(index)->GetX() + Hand()->Read(index)->GetWidth()) continue;
|
||||
if (mouseY > Hand()->Read(index)->GetY() + Hand()->Read(index)->GetHeight()) continue;
|
||||
card = index;
|
||||
}
|
||||
|
||||
if (card == -1)
|
||||
return NULL;
|
||||
|
||||
//rules
|
||||
if (trickCount == 0)
|
||||
return PassPlayFirstTrick(card,leadingSuit);
|
||||
|
||||
Card* c = PassPlayFollowSuit(card, leadingSuit, heartsBroken);
|
||||
if (c != NULL)
|
||||
return c;
|
||||
|
||||
/* Cannot lead a heart exception, from PassPlayFollowSuit() */
|
||||
if (leadingSuit == -1)
|
||||
return NULL;
|
||||
|
||||
return PassPlayBreakSuit(card,leadingSuit,heartsBroken);
|
||||
}
|
||||
|
||||
Card* PlayerUser::PassPlayFirstTrick(int card, int leadingSuit) {
|
||||
//2 of clubs
|
||||
if ( ISCARD(Hand()->Read(),TWO,CLUBS) ) { //if I have the two of clubs
|
||||
if ( ISCARD(Hand()->Read(card),TWO,CLUBS) ) //and I've selected the two of clubs
|
||||
return Hand()->Pass();
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//follow suit
|
||||
if (Hand()->Read(card)->Suit() == leadingSuit) //if I've selected the correct suit
|
||||
return Hand()->Pass(card);
|
||||
|
||||
//if I haven't selected the correct suit
|
||||
for (int i = 0; i < Hand()->Size() -1; i++)
|
||||
if (Hand()->Read(i)->Suit() == leadingSuit) //The correct suit is in my hand
|
||||
return NULL;
|
||||
|
||||
//can't follow suit, no penalty cards
|
||||
if (Hand()->Read(card)->Suit() != Card::HEARTS && !ISCARD(Hand()->Read(card),QUEEN,SPADES))
|
||||
return Hand()->Pass(card);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Card* PlayerUser::PassPlayFollowSuit(int card, int leadingSuit, bool heartsBroken) {
|
||||
//The Queen of spades CAN be played first, and CAN follow suit without breaking hearts
|
||||
//leading the suit
|
||||
if (leadingSuit == -1) {
|
||||
if (heartsBroken)
|
||||
return Hand()->Pass(card);
|
||||
else {
|
||||
if (Hand()->Read(card)->Suit() != Card::HEARTS)
|
||||
return Hand()->Pass(card);
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
//if the player is following suit
|
||||
if (Hand()->Read(card)->Suit() == leadingSuit)
|
||||
return Hand()->Pass(card);
|
||||
|
||||
//when the card does not follow suit
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Card* PlayerUser::PassPlayBreakSuit(int card, int leadingSuit, bool& heartsBroken) {
|
||||
//if the player CAN still follow suit
|
||||
for (int i = 0; i < Hand()->Size() -1; i++)
|
||||
if (Hand()->Read(i)->Suit() == leadingSuit)
|
||||
return NULL;
|
||||
|
||||
if (Hand()->Read(card)->Suit() == Card::HEARTS)
|
||||
heartsBroken = true;
|
||||
return Hand()->Pass(card);
|
||||
}
|
||||
39
Hearts/player_user.h
Normal file
39
Hearts/player_user.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/* File Name: player_user.h
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: 5/6/2011
|
||||
* Version:
|
||||
* Description: Designed for Project Hearts, 4th try.
|
||||
*/
|
||||
#ifndef KR_PLAYERUSER_H_
|
||||
#define KR_PLAYERUSER_H_
|
||||
#include "player.h"
|
||||
|
||||
class PlayerUser : public Player {
|
||||
public:
|
||||
/* Public access members */
|
||||
void SelectSwapCard(int mouseX, int mouseY);
|
||||
Card* PassPlayCard(int mouseX, int mouseY, int leadingSuit, int trickCount, bool& heartsBroken);
|
||||
protected:
|
||||
/* Protected access members */
|
||||
Card* PassPlayFirstTrick(int card, int leadingSuit);
|
||||
Card* PassPlayFollowSuit(int card, int leadingSuit, bool heartsBroken);
|
||||
Card* PassPlayBreakSuit(int card, int leadingSuit, bool& heartsBroken);
|
||||
};
|
||||
|
||||
#endif
|
||||
50
Hearts/sound.cpp
Normal file
50
Hearts/sound.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
/* File Name: sound.cpp
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 13/6/2011
|
||||
* Version: 1, Stable
|
||||
* Description: The Sound class, a wrapper for Mix_Chunk and other sound functionality.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include "sound.h"
|
||||
using namespace std;
|
||||
using namespace KAGE;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
Sound::Sound(const char* fName) {
|
||||
chunk = Mix_LoadWAV(fName);
|
||||
|
||||
if (chunk == NULL) {
|
||||
cout << "Error in Sound::Sound()" << endl;
|
||||
cout << "Mix_LoadWAV() returned NULL" << endl;
|
||||
cout << "fName: " << fName << endl;
|
||||
}
|
||||
}
|
||||
|
||||
Sound::~Sound() {
|
||||
Mix_FreeChunk(chunk);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Accessors and mutators
|
||||
//-------------------------
|
||||
|
||||
int Sound::Play(int channel,int loop) {
|
||||
return Mix_PlayChannel(channel,chunk,loop);
|
||||
}
|
||||
|
||||
int Sound::Volume(int volume) {
|
||||
return Mix_VolumeChunk(chunk,volume);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Protected access members
|
||||
//-------------------------
|
||||
|
||||
Mix_Chunk* Sound::GetMixChunk() {
|
||||
return chunk;
|
||||
}
|
||||
40
Hearts/sound.h
Normal file
40
Hearts/sound.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/* File Name: sound.h
|
||||
* Copyright: (c) Kayne Ruse, all rights reserved.
|
||||
* Author: Kayne Ruse
|
||||
* Date: 13/6/2011
|
||||
* Version: 1, Stable
|
||||
* Description: The Sound class, a wrapper for Mix_Chunk and other sound functionality.
|
||||
* Part of the KAGE Game Engine.
|
||||
*/
|
||||
#ifndef KR_KAGE_SOUND_H_
|
||||
#define KR_KAGE_SOUND_H_
|
||||
|
||||
/* Version information */
|
||||
#define SOUND_VERSION_MAJOR 1
|
||||
#define SOUND_VERSION_MINOR 0
|
||||
#define SOUND_VERSION_PATCH 0
|
||||
#define SOUND_VERSION "1.0.0"
|
||||
#define SOUND_VERSION_STRING "KAGE::Sound Version 1, Stable"
|
||||
|
||||
#include "SDL_mixer.h"
|
||||
|
||||
namespace KAGE {
|
||||
class Sound {
|
||||
public:
|
||||
/* Public access members */
|
||||
Sound(const char* fName);
|
||||
~Sound();
|
||||
|
||||
/* Accessors and mutators */
|
||||
int Play(int channel = -1,int loop = 0);
|
||||
int Volume(int volume = -1);
|
||||
protected:
|
||||
/* Protected access members */
|
||||
Mix_Chunk* GetMixChunk();
|
||||
private:
|
||||
/* Private access members */
|
||||
Mix_Chunk* chunk;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
106
Hearts/table.cpp
Normal file
106
Hearts/table.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
/* File Name: table.cpp
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: 9/6/2011
|
||||
* Version:
|
||||
* Description: Designed for Project Hearts, 4th try.
|
||||
*/
|
||||
#include "table.h"
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
Table::Table() {
|
||||
for (int i = 0; i < 4; i++)
|
||||
cards[i] = NULL;
|
||||
}
|
||||
|
||||
Card* Table::Pass() {
|
||||
CardList cList;
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
cList.Receive(cards[i]);
|
||||
cards[i] = NULL;
|
||||
}
|
||||
|
||||
return cList.PassSlab(0,4);
|
||||
}
|
||||
|
||||
void Table::Receive(Card* card,int position) {
|
||||
//position is the player's pos
|
||||
if (position < 0 || position > 4)
|
||||
return;
|
||||
|
||||
cards[position] = card;
|
||||
cards[position]->SetPos(
|
||||
cardPositions[position].x,
|
||||
cardPositions[position].y
|
||||
);
|
||||
cards[position]->SetFace(Card::UP);
|
||||
}
|
||||
|
||||
int Table::CalcWinner(int first) {
|
||||
if (first < 0 || first > 3)
|
||||
return -1;
|
||||
|
||||
int suit = cards[first]->Suit();
|
||||
int winner = -1;
|
||||
int highest = -1;
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (cards[i]->Suit() == suit &&
|
||||
cards[i]->Rank() > highest)
|
||||
{
|
||||
highest = cards[i]->Rank();
|
||||
winner = i;
|
||||
}
|
||||
}
|
||||
|
||||
return winner;
|
||||
}
|
||||
|
||||
int Table::GetLeadingSuit(int first) {
|
||||
if (cards[first] == NULL)
|
||||
return -1;
|
||||
return cards[first]->Suit();
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Graphical members
|
||||
//-------------------------
|
||||
|
||||
void Table::SetPositions(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3) {
|
||||
cardPositions[0].x = x0;
|
||||
cardPositions[0].y = y0;
|
||||
cardPositions[1].x = x1;
|
||||
cardPositions[1].y = y1;
|
||||
cardPositions[2].x = x2;
|
||||
cardPositions[2].y = y2;
|
||||
cardPositions[3].x = x3;
|
||||
cardPositions[3].y = y3;
|
||||
}
|
||||
|
||||
void Table::Draw(SDL_Surface* dest, int first) {
|
||||
if (first == -1)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
if (cards[(i + first) % 4] != NULL)
|
||||
cards[(i + first) % 4]->Draw(dest);
|
||||
}
|
||||
48
Hearts/table.h
Normal file
48
Hearts/table.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/* File Name: table.h
|
||||
* Copyright: (c) Kayne Ruse 2011, 2012
|
||||
* This file is part of Hearts.
|
||||
*
|
||||
* Hearts is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Hearts is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Hearts. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Kayne Ruse
|
||||
* Date: 9/6/2011
|
||||
* Version:
|
||||
* Description: Designed for Project Hearts, 4th try.
|
||||
*/
|
||||
#ifndef KR_TABLE_H_
|
||||
#define KR_TABLE_H_
|
||||
#include "SDL.h"
|
||||
#include "card_list.h"
|
||||
|
||||
class Table {
|
||||
public:
|
||||
/* Public access members */
|
||||
Table();
|
||||
|
||||
Card* Pass();
|
||||
void Receive(Card* card,int position);
|
||||
|
||||
int CalcWinner(int first);
|
||||
int GetLeadingSuit(int first);
|
||||
|
||||
/* Graphical members */
|
||||
void SetPositions(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3);
|
||||
void Draw(SDL_Surface* dest, int first);
|
||||
private:
|
||||
/* Private access members */
|
||||
Card* cards[4];
|
||||
SDL_Rect cardPositions[4];
|
||||
};
|
||||
|
||||
#endif
|
||||
41
README.md
Normal file
41
README.md
Normal file
@@ -0,0 +1,41 @@
|
||||
# Project Hearts version 1.0, 22nd June 2011
|
||||
|
||||
#### Introduction
|
||||
|
||||
This project is over 1 1/2 years old, so it doesn't reflect my current programming skills. Regardless, it's a pretty good demonstration of my development. From the KAGE engine, you can see what my thought processes at the time were, and you can probably guess that this evolved into my [Codebase](https://github.com/Ratstail91/Codebase). This has a sound system, and some aspects are different, but it's not a terrible start. I<>ve also included all of the unused modules including NetworkManager, which aren<65>t used here, but are simply part of KAGE.
|
||||
|
||||
I'd say that my biggest problem when I was learning was my overreliance on inheritance. You can see this from Card being descended from Image, a common habit in my old code. Also, the presence of NamedManager, which was essentially a wrapper for std::map, and was used to create several different Manager classes isn<73>t something I<>d do today.
|
||||
|
||||
Finally, my biggest mistake I believe, was that I made the KAGE engine closed source. This is a bad idea, since having code visible shows off what you can do. Whether it<69>s to a potential employer, or to someone critiquing your code, it<69>s always a good idea to show what you can do. That<61>s exactly why I<>m uploading this old project. Maybe if I had someone to guide me on the engine<6E>s design at the time, I<>d have a radically different Codebase and coding style today.
|
||||
|
||||
I don<6F>t consider this project complete, since it<69>s still missing a score display and a card passing button, but the scores are shown in the console window (stdout) and the F1 button compensates for a lack of graphical buttons. I<>d like to remake this, using the existing game logic to rely on my current Codebase version. If I do, I<>ll release the modified version under the zlib license.
|
||||
|
||||
####Instructions
|
||||
|
||||
To select a card, click on it
|
||||
To pass three cards at the start of the round, select them and press F1
|
||||
To speed up the wait after a round, press F1
|
||||
|
||||
####License
|
||||
|
||||
Built using Simple DirectMedia Layer (SDL) API. See http://www.libsdl.org for more information.
|
||||
|
||||
Built using Kayne's All-purpose Game Engine (KAGE). See KAGE documentation for more information (no longer available).
|
||||
|
||||
Programming by Kayne Ruse
|
||||
Freeware Graphics
|
||||
Freeware Audio
|
||||
|
||||
Hearts is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Hearts is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Hearts. If not, see http://www.gnu.org/licenses/.
|
||||
|
||||
Reference in New Issue
Block a user