From a97438a81bfb4c509c0d5248c86a05ea0b7b3442 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 6 Sep 2013 17:04:46 +1000 Subject: [PATCH] Moved main(), imported Button class & resources --- Hearts/base_engine.h | 14 +---- Hearts/button.cpp | 114 +++++++++++++++++++++++++++++++++++++++ Hearts/button.hpp | 91 +++++++++++++++++++++++++++++++ Hearts/hearts_engine.cpp | 7 +++ Hearts/hearts_engine.h | 9 +++- Hearts/main.cpp | 45 ++++++++++++++++ Hearts/raster_font.cpp | 60 +++++++++++++++++++++ Hearts/raster_font.hpp | 49 +++++++++++++++++ rsc/button.bmp | Bin 0 -> 90054 bytes rsc/pk_white_8.bmp | Bin 0 -> 33846 bytes 10 files changed, 374 insertions(+), 15 deletions(-) create mode 100644 Hearts/button.cpp create mode 100644 Hearts/button.hpp create mode 100644 Hearts/main.cpp create mode 100644 Hearts/raster_font.cpp create mode 100644 Hearts/raster_font.hpp create mode 100644 rsc/button.bmp create mode 100644 rsc/pk_white_8.bmp 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 0000000000000000000000000000000000000000..842f309b0649f609bdb26af70858b8e335edd771 GIT binary patch literal 90054 zcmeI*!Hr`@5P;#?1UM4{5fA`34#YqxNJPMi1RxV6G^9fU!-HOxEN{tFy{h9kvaNQz zx(}az(B9s6Km7UQr?-FqzP;YxT<`bm{pbQKg9ivqe@#B@~%=qe~I76o**i0P^j&{a@aEDGpq5z|#6psS#;SQOCJBBrZC zKvzLwu_&OcMNC(PfUbhVVo^X>iPV1auV? z7K;M9TEui!2=LO@qRVX-Kn zt3^y#g@CSt!eUWCSBsdg3ISaOg~g(Pt`;#}6#}{n3X4SnT`gj|Dg<;D6c&pDx?03^ zRS4)RC@dBQbhU`-su0jsP*^Mq=xPzuRUz=$)wPa&3j((Vo}xNmZ{Ox1flJ^qs;5VK zJ^o}(;GTd-5Z-fh>V-f=<$qZ(l>F2ch_2rL{_rBOB5)$S(mJ=DukSyD4?q$pyR24u2L48Ljhe40Ub{Tbd|E$ z917@a2@w=qhEgITX;<5YX{dKvyY?&7pv+|8^@l$KO9H3OE3I?i`TFI*#Ox5L3p}=TtwmL*Gx=H| z(mGwe9`-ANy1*$)S9LLy6#-pUl+1Jmbk!9zUlGt%MafK8Kv!Kc^A!PIRg}zh1$5OF zGhY$VRYl26S3p-?G4mAxT~(CKbOm(P6*FHE&{ajrOjkfxT`}_&0bNy;%yb2G)fF>e z5ztjd$xK&3S6wml6#-pUl+1Jmbk!9zUlGt%MafK8Kv!Kc^A!PIRg}zh1$5OFGhY$V zRYl26S3p-?G4mAxT~(CKbOm(P6*FHE&{ajrOjkfxT`}_&0bNy;%yb2G)fF>e5ztjd z$xK&3S6wml6#-pUl+1Jmbk!9zUlGt%MafK8Kv!Kc^A!PIRg}zh1$5OFGhY$VRYl26 zS3p-?G4mCH$F8nNnN*ILA@Ew@3Msle3u(O;^XvZ#oChtPoSq)(b$;?m;GV!0)%E8Q MgvY_>qQHsjAN!htu>b%7 literal 0 HcmV?d00001 diff --git a/rsc/pk_white_8.bmp b/rsc/pk_white_8.bmp new file mode 100644 index 0000000000000000000000000000000000000000..edf2bd80c3c4116f0dc62e95b4b2e34b65f32d04 GIT binary patch literal 33846 zcmeHPJCfzd4J<_{Vr1zIGQHCs?K?aYozG9-42_WnD-s|6} zyF;_Z?j#B*0Eq&Ne2+aF``5qy^7mxNFMmwwFBoNNf8g(*I5NXO|C|yr9@(99wiuo< zOr?}m{$0{<|N2Kt+YTH6CbknOz?IUTfDOQ4KY$FJKmo3ljs$GL4nR3FZ~_Imf_H=5 zfdk0E2^8Q;3Bz=5zz!Th22P*=7m7^42JFBAWZ(n}aHWLbbZx*696$z6pa56e6Us~* zz@G!izzG!K@}+GXcHy2>cHy2>cHy2>cHy2>cHy2>cHy2>cHy2>cHy2>cHy2>cHy2>cHy2>cHy2>cHy2 z>cHy2>cHy2>cHy2>cHy2>cHy2>cHy2>cHy2>cHy2>cD4p;M=$F`2D)$>Dzbwu48pz zbzpU1b>RQK1LKFu=Y6_-3*sk}(98hM4Ikdtr}z{DAGn~If$X32@g{zk;+OpJ<<}4K z;EzxB&*QHJ_)^jI&*}TecXlzX4y+EW4y+EW4y+EW4*X6!;9n&5EiSarTRZ;u2KHyE z_!cR3DxiPc>Bp1XOKQBW^lni8 zKX|#hCx6xWonfrR7iQ0eg77;Pbzk=9A_o6CWP8 z)*H7w2Bt$~2p`Z*2A?*AMcW62e+h6#JL6{a`^|HC0y}?izUEo;S+@H5XOBD9!N*5P zW|UqZqfdwnHfOYRSZr!|4(oYMFQVmhe%W7=m7k4)v;3Q#7HUCn9@P2cUcGeoTf1lJ z?cg)#nBf92R4(^)Wadd9t-ztLN$bIjk{*_vG~ZRqZxftO}i|c8^)tb65V_@pfVD!PXI))BUUM z*%|FOs`5$Jyipelv*oee@QFLzGx;o6=Xy741}(nxV%;p8ryj~98z$W^_@&Fwf-S$f z^`mAmtDj_qv>{Hh9%Xb7o(W9a5-DsZguLOzW#I6*Zcq(29VfVZKP$hd<^J=4cIK!- z#6GX<(s;vu$1w1-@1EBJ>^j&w{dy?8@v4U4N0qi)wE0etP&3F3S;j-5U|+)s6CuiEydANLESbV~V< zo*JgxJ>m0@?9Gok`571SH~c<7`yE=C=qr}Rjd3U`FxUU$t|G7gA!~dcR3;zj*IB7VaC!D@@d;o zYD~MY$0W@Dn3aE9Y49^J3U%of!T))FZj2!=YLne1e8MmK_IiG+gB(#Is|$`W_LCaR zIi%FCk%I|#*JVYuRxs_b(X6S5{C59!HL1=hceww$be1pKlOXSYs3-WMe%p6QMyWi5q#5gAJPz_Bfp18gfV2ELOPA11YFgLoHrog+4{ViZ$QR#Z zY%-j@jc?#LLO+sg4(xhN zdh>stw#`PqX82i0+Ad$UK3wVdP$TMnfiqd`?`p=*#rQV&WWny%O;0WD!Bg@#@w|Ya9!BKAQfc$Ea~~vQ!EmJm8uWb{&+R6`w#vV>HT#&f4dLfpXoi3y6&vvEAD{!?;MQ# eEBN&Xc|3$3{h{VPZGA&btUB(4Hw1pB>wf{-UlCIP literal 0 HcmV?d00001