Libraries are building correctly
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
/* 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():
|
||||
Button(0,0, nullptr, nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
Button::Button(Sint16 i, Sint16 j, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string s) {
|
||||
x = i;
|
||||
y = j;
|
||||
state = State::NORMAL;
|
||||
|
||||
SetSurfaces(imageSurface, fontSurface);
|
||||
|
||||
SetText(s);
|
||||
}
|
||||
|
||||
Button::State Button::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
if (motion.state & SDL_BUTTON_LMASK) {
|
||||
return CalcState(motion.x, motion.y, true);
|
||||
}
|
||||
else {
|
||||
return CalcState(motion.x, motion.y, false);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void Button::SetSurfaces(SDL_Surface* imageSurface, SDL_Surface* fontSurface) {
|
||||
//graphical stuff
|
||||
image.SetSurface(imageSurface);
|
||||
image.SetClipH(image.GetClipH() / 3); //3 phases
|
||||
font.SetSurface(fontSurface);
|
||||
|
||||
//reset textX & textY
|
||||
SetText(text);
|
||||
}
|
||||
|
||||
std::string Button::SetText(std::string s) {
|
||||
//one line
|
||||
text = s;
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/* 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();
|
||||
Button(Sint16 x, Sint16 y, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string text = "");
|
||||
|
||||
//return the current state
|
||||
State MouseMotion(SDL_MouseMotionEvent const&);
|
||||
State MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||
State MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
State GetState() const {
|
||||
return state;
|
||||
}
|
||||
|
||||
//yet another draw function
|
||||
void DrawTo(SDL_Surface* const);
|
||||
|
||||
//simple accessors and mutators
|
||||
Sint16 SetX(Sint16 i) { return x = i; }
|
||||
Sint16 SetY(Sint16 i) { return y = i; }
|
||||
Sint16 GetX() const { return x; }
|
||||
Sint16 GetY() const { return y; }
|
||||
|
||||
void SetSurfaces(SDL_Surface* image, SDL_Surface* font);
|
||||
|
||||
std::string SetText(std::string s);
|
||||
std::string GetText() const { return text; }
|
||||
|
||||
//raw access, be careful
|
||||
Image* GetImage() { return ℑ }
|
||||
RasterFont* GetFont() { return &font; }
|
||||
|
||||
//debug
|
||||
Sint16 GetTextX() const { return textX; }
|
||||
Sint16 GetTextY() const { return textY; }
|
||||
private:
|
||||
State CalcState(Sint16 x, Sint16 y, bool leftPressed);
|
||||
|
||||
Sint16 x, y;
|
||||
Sint16 textX, textY; //prevent recalc every loop
|
||||
Image image;
|
||||
RasterFont font;
|
||||
State state;
|
||||
std::string text;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,76 @@
|
||||
/* 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 "config_utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <fstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void ConfigUtility::Load(string fname) {
|
||||
ifstream is(fname);
|
||||
|
||||
if (!is.is_open()) {
|
||||
throw(runtime_error("Failed to open config file"));
|
||||
}
|
||||
|
||||
string key, val;
|
||||
|
||||
for (;;) { //forever
|
||||
//eat whitespace
|
||||
while(isspace(is.peek()))
|
||||
is.ignore();
|
||||
|
||||
//end of file
|
||||
if (is.eof())
|
||||
break;
|
||||
|
||||
//skip comment lines
|
||||
if (is.peek() == '#') {
|
||||
while(is.peek() != '\n' && !is.eof()) {
|
||||
is.ignore();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
//read in the pair
|
||||
getline(is, key,'=');
|
||||
getline(is, val);
|
||||
|
||||
//trim the strings at the start & end
|
||||
while(key.size() && isspace(*key.begin())) key.erase(0, 1);
|
||||
while(val.size() && isspace(*val.begin())) val.erase(0, 1);
|
||||
|
||||
while(key.size() && isspace(*(key.end()-1))) key.erase(key.end() - 1);
|
||||
while(val.size() && isspace(*(val.end()-1))) val.erase(val.end() - 1);
|
||||
|
||||
//allow empty/wiped values
|
||||
if (key.size() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//save the pair
|
||||
table[key] = val;
|
||||
}
|
||||
|
||||
is.close();
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/* 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 CONFIGUTILITY_HPP_
|
||||
#define CONFIGUTILITY_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
|
||||
class ConfigUtility {
|
||||
public:
|
||||
ConfigUtility() {}
|
||||
ConfigUtility(std::string s) { Load(s); }
|
||||
|
||||
void Load(std::string fname);
|
||||
|
||||
std::string String(std::string s) {
|
||||
return table[s];
|
||||
}
|
||||
const char* CString(std::string s) {
|
||||
return table[s].c_str();
|
||||
}
|
||||
int Integer(std::string s) {
|
||||
return atoi(table[s].c_str());
|
||||
}
|
||||
double Double(std::string s) {
|
||||
return atof(table[s].c_str());
|
||||
}
|
||||
bool Boolean(std::string s) {
|
||||
return table[s] == "true";
|
||||
}
|
||||
|
||||
std::string& operator[](std::string s) {
|
||||
return table[s];
|
||||
}
|
||||
int Int(std::string s) {
|
||||
return Integer(s);
|
||||
}
|
||||
int Bool(std::string s) {
|
||||
return Boolean(s);
|
||||
}
|
||||
std::map<std::string, std::string>* GetMap() {
|
||||
return &table;
|
||||
}
|
||||
private:
|
||||
std::map<std::string, std::string> table;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
/* 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 "frame_rate.hpp"
|
||||
|
||||
int FrameRate::frameCount = 0;
|
||||
int FrameRate::lastFrameRate = 0;
|
||||
FrameRate::Clock::time_point FrameRate::tick = FrameRate::Clock::now();
|
||||
@@ -0,0 +1,48 @@
|
||||
/* 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 FRAMERATE_HPP_
|
||||
#define FRAMERATE_HPP_
|
||||
|
||||
#include <chrono>
|
||||
|
||||
class FrameRate {
|
||||
public:
|
||||
typedef std::chrono::high_resolution_clock Clock;
|
||||
|
||||
FrameRate() = delete;
|
||||
static int Calculate() {
|
||||
frameCount++;
|
||||
if (Clock::now() - tick >= std::chrono::duration<int>(1)) {
|
||||
lastFrameRate = frameCount;
|
||||
frameCount = 0;
|
||||
tick = Clock::now();
|
||||
}
|
||||
return lastFrameRate;
|
||||
}
|
||||
static int GetFrameRate() { return lastFrameRate; }
|
||||
private:
|
||||
static int frameCount;
|
||||
static int lastFrameRate;
|
||||
static Clock::time_point tick;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
/* 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 "image.hpp"
|
||||
|
||||
Image::Image(SDL_Surface* p) {
|
||||
SetSurface(p);
|
||||
}
|
||||
|
||||
Image::Image(SDL_Surface* p, SDL_Rect r) {
|
||||
SetSurface(p, r);
|
||||
}
|
||||
|
||||
SDL_Surface* Image::SetSurface(SDL_Surface* p) {
|
||||
if (!(surface = p)) {
|
||||
clip = {0, 0, 0, 0};
|
||||
}
|
||||
else {
|
||||
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
|
||||
}
|
||||
return surface;
|
||||
}
|
||||
|
||||
SDL_Surface* Image::SetSurface(SDL_Surface* const p, SDL_Rect r) {
|
||||
surface = p;
|
||||
clip = r;
|
||||
return surface;
|
||||
}
|
||||
|
||||
void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) {
|
||||
if (!surface) {
|
||||
return;
|
||||
}
|
||||
SDL_Rect sclip = clip, dclip = {x,y};
|
||||
SDL_BlitSurface(surface, &sclip, dest, &dclip);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/* 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 IMAGE_HPP_
|
||||
#define IMAGE_HPP_
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
class Image {
|
||||
public:
|
||||
Image() = default;
|
||||
Image(SDL_Surface*);
|
||||
Image(SDL_Surface*, SDL_Rect);
|
||||
~Image() = default;
|
||||
|
||||
SDL_Surface* SetSurface(SDL_Surface*);
|
||||
SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect);
|
||||
SDL_Surface* GetSurface() const { return surface; }
|
||||
|
||||
void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y);
|
||||
|
||||
//Clip handlers
|
||||
SDL_Rect SetClip(SDL_Rect r) { return clip = r; }
|
||||
SDL_Rect GetClip() const { return clip; }
|
||||
|
||||
Sint16 SetClipX(Sint16 x) { return clip.x = x; }
|
||||
Sint16 SetClipY(Sint16 y) { return clip.y = y; }
|
||||
Uint16 SetClipW(Uint16 w) { return clip.w = w; }
|
||||
Uint16 SetClipH(Uint16 h) { return clip.h = h; }
|
||||
|
||||
Sint16 GetClipX() const { return clip.x; }
|
||||
Sint16 GetClipY() const { return clip.y; }
|
||||
Uint16 GetClipW() const { return clip.w; }
|
||||
Uint16 GetClipH() const { return clip.h; }
|
||||
protected:
|
||||
SDL_Surface* surface = nullptr;
|
||||
SDL_Rect clip = {0, 0, 0, 0};
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,37 @@
|
||||
#config
|
||||
INCLUDES=
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
LIB=
|
||||
|
||||
#source
|
||||
SRC=$(wildcard *.cpp)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ=$(addprefix $(OBJDIR)/,$(SRC:.cpp=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../out
|
||||
OUT=$(addprefix $(OUTDIR)/,Codebase.a)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
ar -crs $(OUT) $(OBJ)
|
||||
|
||||
$(OBJ): | $(OBJDIR)
|
||||
|
||||
$(OUT): | $(OUTDIR)
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
$(OUTDIR):
|
||||
mkdir $(OUTDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
rebuild: clean all
|
||||
@@ -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.
|
||||
*/
|
||||
#include "raster_font.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
RasterFont::RasterFont(SDL_Surface* p) {
|
||||
SetSurface(p);
|
||||
}
|
||||
|
||||
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] % w * w);
|
||||
image.SetClipY(s[i] / h * h);
|
||||
image.DrawTo(dest, x + i * w, y);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Surface* RasterFont::SetSurface(SDL_Surface* p) {
|
||||
if (image.SetSurface(p)) {
|
||||
image.SetClipW(image.GetSurface()->w/16);
|
||||
image.SetClipH(image.GetSurface()->h/16);
|
||||
}
|
||||
return image.GetSurface();
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/* 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"
|
||||
|
||||
#include <string>
|
||||
|
||||
class RasterFont {
|
||||
public:
|
||||
RasterFont() = default;
|
||||
RasterFont(SDL_Surface* p);
|
||||
~RasterFont() = default;
|
||||
|
||||
void DrawStringTo(std::string, SDL_Surface* const, Sint16 x, Sint16 y);
|
||||
|
||||
//Accessors and Mutators
|
||||
SDL_Surface* SetSurface(SDL_Surface*);
|
||||
SDL_Surface* GetSurface() const { return image.GetSurface(); }
|
||||
Uint16 GetCharW() { return image.GetClipW(); }
|
||||
Uint16 GetCharH() { return image.GetClipH(); }
|
||||
private:
|
||||
Image image;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,44 @@
|
||||
/* 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 "sprite_sheet.hpp"
|
||||
|
||||
SpriteSheet::SpriteSheet(SDL_Surface* s, Uint16 w, Uint16 h) {
|
||||
SetSurface(s, w, h);
|
||||
}
|
||||
|
||||
void SpriteSheet::Update(double delta) {
|
||||
if (interval && (ticks += delta) >= interval) {
|
||||
if (++currentFrame >= maxFrames) {
|
||||
currentFrame = 0;
|
||||
}
|
||||
ticks = 0;
|
||||
}
|
||||
image.SetClipX(currentFrame * image.GetClipW());
|
||||
image.SetClipY(currentStrip * image.GetClipH());
|
||||
}
|
||||
|
||||
SDL_Surface* SpriteSheet::SetSurface(SDL_Surface* const s, Uint16 w, Uint16 h) {
|
||||
image.SetSurface(s, {0, 0, w, h});
|
||||
currentFrame = 0; maxFrames = image.GetSurface()->w / image.GetClipW();
|
||||
currentStrip = 0; maxStrips = image.GetSurface()->h / image.GetClipH();
|
||||
interval = ticks = 0;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/* 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 SPRITESHEET_HPP_
|
||||
#define SPRITESHEET_HPP_
|
||||
|
||||
#include "image.hpp"
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
class SpriteSheet {
|
||||
public:
|
||||
SpriteSheet() = default;
|
||||
SpriteSheet(SDL_Surface*, Uint16 w, Uint16 h);
|
||||
~SpriteSheet() = default;
|
||||
|
||||
void Update(double delta);
|
||||
|
||||
SDL_Surface* SetSurface(SDL_Surface* const, Uint16 w, Uint16 h);
|
||||
SDL_Surface* GetSurface() const {
|
||||
return image.GetSurface();
|
||||
}
|
||||
|
||||
void DrawTo(SDL_Surface* const dest, Sint16 x, Sint16 y) {
|
||||
image.DrawTo(dest, x, y);
|
||||
}
|
||||
|
||||
//Accessors and Mutators
|
||||
double SetInterval(double i) { return interval = i; }
|
||||
double GetInterval() const { return interval; }
|
||||
|
||||
int SetCurrentFrame(int i) { return currentFrame = i; }
|
||||
int SetCurrentStrip(int i) { return currentStrip = i; }
|
||||
|
||||
Uint16 GetFrameWidth() const { return image.GetClipW(); }
|
||||
Uint16 GetFrameHeight() const { return image.GetClipH(); }
|
||||
int GetCurrentFrame() const { return currentFrame; };
|
||||
int GetCurrentStrip() const { return currentStrip; };
|
||||
int GetMaxFrames() const { return maxFrames; }
|
||||
int GetMaxStrips() const { return maxStrips; }
|
||||
private:
|
||||
Image image;
|
||||
int currentFrame = 0, maxFrames = 0;
|
||||
int currentStrip = 0, maxStrips = 0;
|
||||
double interval = 0, ticks = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,85 @@
|
||||
/* 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 "surface_manager.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
SurfaceManager::~SurfaceManager() noexcept {
|
||||
FreeAll();
|
||||
}
|
||||
|
||||
SDL_Surface* SurfaceManager::Load(std::string key, std::string fname) {
|
||||
MapType::iterator it = surfaceMap.find(key);
|
||||
if (it != surfaceMap.end()) {
|
||||
throw(std::runtime_error(std::string("Surface already loaded: ") + key + std::string(", ") + fname));
|
||||
}
|
||||
return LoadSurface(key, fname);
|
||||
}
|
||||
|
||||
SDL_Surface* SurfaceManager::Reload(std::string key, std::string fname) {
|
||||
MapType::iterator it = surfaceMap.find(key);
|
||||
if (it != surfaceMap.end()) {
|
||||
SDL_FreeSurface(it->second);
|
||||
surfaceMap.erase(it);
|
||||
}
|
||||
return LoadSurface(key, fname);
|
||||
}
|
||||
|
||||
SDL_Surface* SurfaceManager::Get(std::string key) {
|
||||
MapType::iterator it = surfaceMap.find(key);
|
||||
if (it == surfaceMap.end()) {
|
||||
throw(std::runtime_error(std::string("Could not find key: ") + key));
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
SDL_Surface* SurfaceManager::Set(std::string key, SDL_Surface* ptr) {
|
||||
MapType::iterator it = surfaceMap.find(key);
|
||||
if (it != surfaceMap.end()) {
|
||||
throw(std::runtime_error(std::string("Key already exists: ") + key));
|
||||
}
|
||||
return surfaceMap[key] = ptr;
|
||||
}
|
||||
|
||||
void SurfaceManager::Free(std::string key) {
|
||||
MapType::iterator it = surfaceMap.find(key);
|
||||
if (it != surfaceMap.end()) {
|
||||
SDL_FreeSurface(it->second);
|
||||
surfaceMap.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
void SurfaceManager::FreeAll() {
|
||||
for (auto it : surfaceMap) {
|
||||
SDL_FreeSurface(it.second);
|
||||
}
|
||||
surfaceMap.clear();
|
||||
}
|
||||
|
||||
SDL_Surface* SurfaceManager::LoadSurface(std::string key, std::string fname) {
|
||||
SDL_Surface* ptr = SDL_LoadBMP(fname.c_str());
|
||||
if (!ptr) {
|
||||
throw(std::runtime_error(std::string("Failed to load file: ") + fname));
|
||||
}
|
||||
SDL_SetColorKey(ptr, SDL_SRCCOLORKEY, SDL_MapRGB(ptr->format, 255, 0, 255)); //default
|
||||
return surfaceMap[key] = ptr;
|
||||
}
|
||||
@@ -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 SURFACEMANAGER_HPP_
|
||||
#define SURFACEMANAGER_HPP_
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
class SurfaceManager {
|
||||
public:
|
||||
SurfaceManager() = default;
|
||||
~SurfaceManager() noexcept;
|
||||
|
||||
SDL_Surface* Load(std::string key, std::string fname);
|
||||
SDL_Surface* Reload(std::string key, std::string fname);
|
||||
SDL_Surface* Get(std::string key);
|
||||
SDL_Surface* Set(std::string key, SDL_Surface* ptr);
|
||||
void Free(std::string key);
|
||||
void FreeAll();
|
||||
|
||||
SDL_Surface* operator[](std::string key) { return Get(key); };
|
||||
private:
|
||||
SDL_Surface* LoadSurface(std::string key, std::string fname);
|
||||
typedef std::map<std::string, SDL_Surface*> MapType;
|
||||
MapType surfaceMap;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,148 @@
|
||||
/* 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 "udp_network_utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
void UDPNetworkUtility::Open(int port, int packSize) {
|
||||
if (!(socket = SDLNet_UDP_Open(port))) {
|
||||
Close();
|
||||
throw(std::runtime_error("Failed to open a UDP socket"));
|
||||
}
|
||||
|
||||
if (!(packOut = SDLNet_AllocPacket(packSize))) {
|
||||
Close();
|
||||
throw(std::runtime_error("Failed to allocate the out packet"));
|
||||
}
|
||||
|
||||
if (!(packIn = SDLNet_AllocPacket(packSize))) {
|
||||
Close();
|
||||
throw(std::runtime_error("Failed to allocate the in packet"));
|
||||
}
|
||||
}
|
||||
|
||||
void UDPNetworkUtility::Close() {
|
||||
SDLNet_UDP_Close(socket);
|
||||
SDLNet_FreePacket(packOut);
|
||||
SDLNet_FreePacket(packIn);
|
||||
socket = nullptr;
|
||||
packOut = nullptr;
|
||||
packIn = nullptr;
|
||||
}
|
||||
|
||||
int UDPNetworkUtility::Bind(const char* ip, int port, int channel) {
|
||||
IPaddress add;
|
||||
if (SDLNet_ResolveHost(&add, ip, port) == -1) {
|
||||
throw(std::runtime_error("Failed to resolve a host"));
|
||||
}
|
||||
|
||||
return Bind(&add, channel);
|
||||
}
|
||||
|
||||
int UDPNetworkUtility::Bind(IPaddress* add, int channel) {
|
||||
int ret = SDLNet_UDP_Bind(socket, channel, add);
|
||||
|
||||
if (ret == -1) {
|
||||
throw(std::runtime_error("Failed to bind to a channel"));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void UDPNetworkUtility::Unbind(int channel) {
|
||||
SDLNet_UDP_Unbind(socket, channel);
|
||||
}
|
||||
|
||||
int UDPNetworkUtility::Send(const char* ip, int port, void* data, int len) {
|
||||
IPaddress add;
|
||||
if (SDLNet_ResolveHost(&add, ip, port) == -1) {
|
||||
throw(std::runtime_error("Failed to resolve a host"));
|
||||
}
|
||||
|
||||
Send(&add, data, len);
|
||||
}
|
||||
|
||||
int UDPNetworkUtility::Send(IPaddress* add, void* data, int len) {
|
||||
if (len > packOut->maxlen) {
|
||||
throw(std::runtime_error("Failed to copy the data into the packet"));
|
||||
}
|
||||
memset(packOut->data, 0, packOut->maxlen);
|
||||
memcpy(packOut->data, data, len);
|
||||
packOut->len = len;
|
||||
packOut->address = *add;
|
||||
|
||||
int ret = SDLNet_UDP_Send(socket, -1, packOut);
|
||||
|
||||
if (ret <= 0) {
|
||||
throw(std::runtime_error("Failed to send a packet"));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int UDPNetworkUtility::Send(int channel, void* data, int len) {
|
||||
if (len > packOut->maxlen) {
|
||||
throw(std::runtime_error("Failed to copy the data into the packet"));
|
||||
}
|
||||
memset(packOut->data, 0, packOut->maxlen);
|
||||
memcpy(packOut->data, data, len);
|
||||
packOut->len = len;
|
||||
|
||||
int ret = SDLNet_UDP_Send(socket, channel, packOut);
|
||||
|
||||
if (ret <= 0) {
|
||||
throw(std::runtime_error("Failed to send a packet"));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int UDPNetworkUtility::SendAll(void* data, int len) {
|
||||
if (len > packOut->maxlen) {
|
||||
throw(std::runtime_error("Failed to copy the data into the packet"));
|
||||
}
|
||||
memset(packOut->data, 0, packOut->maxlen);
|
||||
memcpy(packOut->data, data, len);
|
||||
packOut->len = len;
|
||||
|
||||
int sent = 0;
|
||||
|
||||
//send to all bound channels
|
||||
for (int i = 0; i < SDLNET_MAX_UDPCHANNELS; i++) {
|
||||
if (SDLNet_UDP_GetPeerAddress(socket, i)) {
|
||||
sent += SDLNet_UDP_Send(socket, i, packOut);
|
||||
}
|
||||
}
|
||||
|
||||
return sent;
|
||||
}
|
||||
|
||||
int UDPNetworkUtility::Receive() {
|
||||
memset(packIn->data, 0, packIn->maxlen);
|
||||
int ret = SDLNet_UDP_Recv(socket, packIn);
|
||||
|
||||
if (ret < 0) {
|
||||
throw(std::runtime_error("Unknown network error occured"));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/* 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 UDPNETWORKUTILITY_HPP_
|
||||
#define UDPNETWORKUTILITY_HPP_
|
||||
|
||||
#include "SDL_net/SDL_net.h"
|
||||
|
||||
class UDPNetworkUtility {
|
||||
public:
|
||||
UDPNetworkUtility() = default;
|
||||
~UDPNetworkUtility() = default;
|
||||
|
||||
void Open(int port, int packSize);
|
||||
void Close();
|
||||
|
||||
//bind to an available channel
|
||||
int Bind(const char* ip, int port) {
|
||||
Bind(ip, port, -1);
|
||||
}
|
||||
int Bind(IPaddress* add) {
|
||||
Bind(add, -1);
|
||||
}
|
||||
|
||||
//bind to certain channel
|
||||
int Bind(const char* ip, int port, int channel);
|
||||
int Bind(IPaddress* add, int channel);
|
||||
void Unbind(int channel);
|
||||
|
||||
IPaddress* GetIPAddress(int channel) {
|
||||
return SDLNet_UDP_GetPeerAddress(socket, channel);
|
||||
}
|
||||
|
||||
int Send(const char* ip, int port, void* data, int len);
|
||||
int Send(IPaddress* add, void* data, int len);
|
||||
int Send(int channel, void* data, int len);
|
||||
int SendAll(void* data, int len);
|
||||
int Receive();
|
||||
|
||||
void* GetOutData() const {
|
||||
return reinterpret_cast<void*>(packOut->data);
|
||||
};
|
||||
void* GetInData() const {
|
||||
return reinterpret_cast<void*>(packIn->data);
|
||||
};
|
||||
UDPpacket* GetOutPacket() const {
|
||||
return packOut;
|
||||
}
|
||||
UDPpacket* GetInPacket() const {
|
||||
return packIn;
|
||||
}
|
||||
UDPsocket GetSocket() const {
|
||||
return socket;
|
||||
}
|
||||
private:
|
||||
UDPsocket socket = nullptr;
|
||||
UDPpacket* packOut = nullptr;
|
||||
UDPpacket* packIn = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,77 @@
|
||||
/* 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 VECTOR2_HPP_
|
||||
#define VECTOR2_HPP_
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cmath>
|
||||
|
||||
class Vector2 {
|
||||
public:
|
||||
double x = 0, y = 0;
|
||||
Vector2() = default;
|
||||
Vector2(double i, double j) {
|
||||
x = i; y = j;
|
||||
}
|
||||
double Length() const {
|
||||
return sqrt(x*x+y*y);
|
||||
}
|
||||
double SquaredLength() const {
|
||||
return x*x+y*y;
|
||||
}
|
||||
|
||||
double operator[](size_t i) {
|
||||
if (i >= 2)
|
||||
throw(std::runtime_error("Out of range"));
|
||||
return *(&x+i);
|
||||
}
|
||||
|
||||
//Arithmetic operators
|
||||
Vector2 operator+(Vector2 v) const { return Vector2(x + v.x, y + v.y); }
|
||||
Vector2 operator-(Vector2 v) const { return Vector2(x - v.x, y - v.y); }
|
||||
Vector2 operator*(Vector2 v) const { return Vector2(x * v.x, y * v.y); }
|
||||
Vector2 operator*(double d) const { return Vector2(x * d, y * d); }
|
||||
|
||||
Vector2 operator/(Vector2 v) {
|
||||
if (!v.x || !v.y)
|
||||
throw(std::runtime_error("Divide by zero"));
|
||||
return Vector2(x / v.x, y / v.y);
|
||||
}
|
||||
Vector2 operator/(double d) {
|
||||
if (!d)
|
||||
throw(std::runtime_error("Divide by zero"));
|
||||
return Vector2(x / d, y / d);
|
||||
}
|
||||
|
||||
bool operator==(Vector2 v) { return (x == v.x && y == v.y); }
|
||||
bool operator!=(Vector2 v) { return (x != v.x || y != v.y); }
|
||||
|
||||
//templates
|
||||
template<typename T> Vector2 operator+=(T t) { return *this = *this + t; }
|
||||
template<typename T> Vector2 operator-=(T t) { return *this = *this - t; }
|
||||
template<typename T> Vector2 operator*=(T t) { return *this = *this * t; }
|
||||
template<typename T> Vector2 operator/=(T t) { return *this = *this / t; }
|
||||
template<typename T> bool operator==(T t) { return (x == t && y == t); }
|
||||
template<typename T> bool operator!=(T t) { return (x != t || y != t); }
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user