mirror of
https://github.com/Ratstail91/Hearts.git
synced 2025-11-29 02:24:28 +11:00
Moved main(), imported Button class & resources
This commit is contained in:
@@ -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
|
||||
114
Hearts/button.cpp
Normal file
114
Hearts/button.cpp
Normal file
@@ -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 <stdexcept>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
91
Hearts/button.hpp
Normal file
91
Hearts/button.hpp
Normal file
@@ -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 <string>
|
||||
|
||||
//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
|
||||
@@ -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();
|
||||
|
||||
@@ -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
|
||||
|
||||
45
Hearts/main.cpp
Normal file
45
Hearts/main.cpp
Normal file
@@ -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 <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
60
Hearts/raster_font.cpp
Normal file
60
Hearts/raster_font.cpp
Normal file
@@ -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 <stdexcept>
|
||||
|
||||
/* 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();
|
||||
}
|
||||
49
Hearts/raster_font.hpp
Normal file
49
Hearts/raster_font.hpp
Normal file
@@ -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
|
||||
BIN
rsc/button.bmp
Normal file
BIN
rsc/button.bmp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 88 KiB |
BIN
rsc/pk_white_8.bmp
Normal file
BIN
rsc/pk_white_8.bmp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
Reference in New Issue
Block a user