diff --git a/Hearts/base_engine.h b/Hearts/base_engine.h index fe1b29d..7a50f22 100644 --- a/Hearts/base_engine.h +++ b/Hearts/base_engine.h @@ -103,16 +103,4 @@ namespace KAGE { }; } -#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 +#endif \ No newline at end of file diff --git a/Hearts/button.cpp b/Hearts/button.cpp new file mode 100644 index 0000000..b3c3833 --- /dev/null +++ b/Hearts/button.cpp @@ -0,0 +1,114 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "button.hpp" + +#include + +Button::Button(std::string bgname, std::string fname, std::string t, Sint16 _x, Sint16 _y) { + LoadSurface(bgname); + LoadFontSurface(fname); + SetText(t); + SetX(x); + SetY(y); +} + +Button::Button(SDL_Surface* bg, SDL_Surface* f, std::string t, Sint16 _x, Sint16 _y) { + SetSurface(bg); + SetFontSurface(f); + SetText(t); + SetX(x); + SetY(y); +} + +SDL_Surface* Button::LoadSurface(std::string s) { + image.LoadSurface(s); + image.SetClipH(image.GetClipH()/3); //3 phases, vertical storage + SetText(text); //reset textX & textY + return image.GetSurface(); +} + +SDL_Surface* Button::LoadFontSurface(std::string s) { + font.LoadSurface(s); + SetText(text); //reset textX & textY + return font.GetSurface(); +} + +SDL_Surface* Button::SetSurface(SDL_Surface* p) { + image.SetSurface(p); + image.SetClipH(image.GetClipH()/3); //3 phases, vertical storage + SetText(text); //reset textX & textY + return image.GetSurface(); +} + +SDL_Surface* Button::SetFontSurface(SDL_Surface* p) { + font.SetSurface(p); + SetText(text); //reset textX & textY + return font.GetSurface(); +} + +Button::State Button::MouseMotion(SDL_MouseMotionEvent const& motion) { + return CalcState(motion.x, motion.y, motion.state & SDL_BUTTON_LMASK); +} + +Button::State Button::MouseButtonDown(SDL_MouseButtonEvent const& button) { + if (button.button == SDL_BUTTON_LEFT) { + return CalcState(button.x, button.y, true); + } + return state; +} + +Button::State Button::MouseButtonUp(SDL_MouseButtonEvent const& button) { + if (button.button == SDL_BUTTON_LEFT) { + return CalcState(button.x, button.y, false); + } + return state; +} + +void Button::DrawTo(SDL_Surface* const dest) { + image.DrawTo(dest, x, y); + font.DrawStringTo(text, dest, textX + x, textY + y); +} + +std::string Button::SetText(std::string t) { + //one line, cache the position + text = t; + textX = (image.GetClipW() / 2) - (font.GetCharW() * text.size() / 2); + textY = (image.GetClipH() / 2) - (font.GetCharH() / 2); + return text; +} + +Button::State Button::CalcState(Sint16 i, Sint16 j, bool leftPressed) { + if (i < x || i > (x + image.GetClipW()) || + j < y || j > (y + image.GetClipH()) + ) { + image.SetClipY(0); + return state = State::NORMAL; + } + if (leftPressed) { + image.SetClipY(image.GetClipH()*2); + return state = State::PRESSED; + } + else { + image.SetClipY(image.GetClipH()); + return state = State::HOVER; + } +} diff --git a/Hearts/button.hpp b/Hearts/button.hpp new file mode 100644 index 0000000..f1bbda0 --- /dev/null +++ b/Hearts/button.hpp @@ -0,0 +1,91 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef BUTTON_HPP_ +#define BUTTON_HPP_ + +#include "image.hpp" +#include "raster_font.hpp" + +#include + +//3-phases, no toggle, centred text +class Button { +public: + enum class State { + NORMAL, HOVER, PRESSED + }; + + Button() = default; + Button(std::string bgname, std::string fontname, std::string t = "", Sint16 x = 0, Sint16 y = 0); + Button(SDL_Surface* background, SDL_Surface* font, std::string t = "", Sint16 x = 0, Sint16 y = 0); + ~Button() = default; + + //graphics + SDL_Surface* LoadSurface(std::string); + SDL_Surface* LoadFontSurface(std::string); + SDL_Surface* SetSurface(SDL_Surface*); + SDL_Surface* SetFontSurface(SDL_Surface*); + SDL_Surface* GetSurface() const { return image.GetSurface(); } + SDL_Surface* GetFontSurface() const { return font.GetSurface(); } + void FreeSurface() { image.FreeSurface(); } + void FreeFontSurface() { font.FreeSurface(); } + + //handle input + State MouseMotion(SDL_MouseMotionEvent const&); + State MouseButtonDown(SDL_MouseButtonEvent const&); + State MouseButtonUp(SDL_MouseButtonEvent const&); + State SetState(State s) { return state = s; } + State GetState() const { return state; } + + //yet another draw function + void DrawTo(SDL_Surface* const); + + //text string + std::string SetText(std::string); + std::string GetText() const { return text; } + + //position + Sint16 SetX(Sint16 i) { return x = i; } + Sint16 SetY(Sint16 i) { return y = i; } + Sint16 GetX() const { return x; } + Sint16 GetY() const { return y; } + + //OO Breakers + Image* GetImage() { return ℑ } + RasterFont* GetFont() { return &font; } + + Sint16 SetTextX(Sint16 i) { return textX = i; } + Sint16 SetTextY(Sint16 i) { return textY = i; } + Sint16 GetTextX() const { return textX; } + Sint16 GetTextY() const { return textY; } +private: + State CalcState(Sint16 x, Sint16 y, bool leftPressed); + + Sint16 x = 0, y = 0; + Sint16 textX = 0, textY = 0; //prevent recalc every loop + Image image; + RasterFont font; //TODO: cache this? + State state = State::NORMAL; + std::string text; +}; + +#endif diff --git a/Hearts/hearts_engine.cpp b/Hearts/hearts_engine.cpp index 5a45b14..76c557b 100644 --- a/Hearts/hearts_engine.cpp +++ b/Hearts/hearts_engine.cpp @@ -42,6 +42,13 @@ HeartsEngine::HeartsEngine() { deck.Init("rsc\\cards.bmp","rsc\\back.bmp"); + button.LoadSurface("rsc\\button.bmp"); + button.LoadFontSurface("rsc\\pk_white_8.bmp"); + font.LoadSurface("rsc\\pk_white_8.bmp"); + +// button.SetX(); +// button.SetY(); + player[0] = new PlayerUser(); player[1] = new PlayerAI(); player[2] = new PlayerAI(); diff --git a/Hearts/hearts_engine.h b/Hearts/hearts_engine.h index d07ab95..7c2752c 100644 --- a/Hearts/hearts_engine.h +++ b/Hearts/hearts_engine.h @@ -27,12 +27,16 @@ */ #ifndef KR_HEARTSENGINE_H_ #define KR_HEARTSENGINE_H_ + #include "base_engine.h" + #include "image.hpp" #include "deck.h" #include "player_ai.h" #include "player_user.h" #include "table.h" +#include "button.hpp" +#include "raster_font.hpp" class HeartsEngine : public KAGE::BaseEngine { public: @@ -72,6 +76,9 @@ private: }rotation; /* Game members */ + Button button; + RasterFont font; + Deck deck; Player *player[4]; Table table; @@ -88,6 +95,4 @@ private: void CalcScores(); }; -START(HeartsEngine) - #endif diff --git a/Hearts/main.cpp b/Hearts/main.cpp new file mode 100644 index 0000000..1a800ac --- /dev/null +++ b/Hearts/main.cpp @@ -0,0 +1,45 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "hearts_engine.h" + +#include +#include + +using namespace std; + +int main(int, char**) { +#ifdef DEBUG + cout << "Beginning program" << endl; +#endif + try { + HeartsEngine app; + app.Start(true); + } + catch(exception& e) { + cerr << "Fatal exception thrown: " << e.what() << endl; + return 1; + } +#ifdef DEBUG + cout << "Clean exit" << endl; +#endif + return 0; +} diff --git a/Hearts/raster_font.cpp b/Hearts/raster_font.cpp new file mode 100644 index 0000000..bde074d --- /dev/null +++ b/Hearts/raster_font.cpp @@ -0,0 +1,60 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "raster_font.hpp" + +#include + +/* It might be more efficient to render to a different surface (like an Image) + * rather than calling this function with all of it's '%' and '/'. +*/ + +void RasterFont::DrawStringTo(std::string s, SDL_Surface* const dest, Sint16 x, Sint16 y) { + if (!image.GetSurface()) { + throw(std::runtime_error("RasterFont not loaded")); + } + const Uint16 w = image.GetClipW(); + const Uint16 h = image.GetClipH(); + for (int i = 0; i < s.size(); i++) { + image.SetClipX(s[i] % 16 * w); + image.SetClipY(s[i] / 16 * h); + image.DrawTo(dest, x + i * w, y); + } +} + +/* Note: This class can only take a raster font with 16*16 characters, and the + * indevidual characters must have the same dimensions. Overall this class is too + * restrictive; I suggest using a 3rd party library. +*/ + +SDL_Surface* RasterFont::LoadSurface(std::string fname) { + image.LoadSurface(fname); + image.SetClipW(image.GetSurface()->w/16); + image.SetClipH(image.GetSurface()->h/16); + return image.GetSurface(); +} + +SDL_Surface* RasterFont::SetSurface(SDL_Surface* p) { + image.SetSurface(p); + image.SetClipW(image.GetSurface()->w/16); + image.SetClipH(image.GetSurface()->h/16); + return image.GetSurface(); +} diff --git a/Hearts/raster_font.hpp b/Hearts/raster_font.hpp new file mode 100644 index 0000000..e4e157e --- /dev/null +++ b/Hearts/raster_font.hpp @@ -0,0 +1,49 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef RASTERFONT_HPP_ +#define RASTERFONT_HPP_ + +#include "image.hpp" + +class RasterFont { +public: + RasterFont() = default; + RasterFont(std::string fname) { LoadSurface(fname); } + RasterFont(SDL_Surface* p) { SetSurface(p); } + ~RasterFont() = default; + + void DrawStringTo(std::string, SDL_Surface* const, Sint16 x, Sint16 y); + + //Accessors and Mutators + SDL_Surface* LoadSurface(std::string); + SDL_Surface* SetSurface(SDL_Surface*); + SDL_Surface* GetSurface() const { return image.GetSurface(); } + void FreeSurface() { image.FreeSurface(); } + + Uint16 GetCharW() const { return image.GetClipW(); } + Uint16 GetCharH() const { return image.GetClipH(); } + +private: + Image image; +}; + +#endif diff --git a/rsc/button.bmp b/rsc/button.bmp new file mode 100644 index 0000000..842f309 Binary files /dev/null and b/rsc/button.bmp differ diff --git a/rsc/pk_white_8.bmp b/rsc/pk_white_8.bmp new file mode 100644 index 0000000..edf2bd8 Binary files /dev/null and b/rsc/pk_white_8.bmp differ