Created a new branch to act as a submodule of common classes

This commit is contained in:
2015-08-14 22:14:26 +10:00
parent 2f86511254
commit b33fb66d2a
197 changed files with 15 additions and 10089 deletions
+170
View File
@@ -0,0 +1,170 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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>
void Button::DrawTo(SDL_Renderer* renderer) {
image.SetClipY(image.GetClipH() * state);
image.DrawTo(renderer, posX, posY);
}
void Button::SetBackgroundTexture(SDL_Renderer* renderer, SDL_Texture* texture) {
//copy the given texture
image.Free();
//a null texture can simply free the image
if (!texture) {
return;
}
//get the w & h, & create
int w = 0, h = 0;
SDL_QueryTexture(texture, nullptr, nullptr, &w, &h);
image.Create(renderer, w, h);
//copy
SDL_SetRenderTarget(renderer, image.GetTexture());
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_SetRenderTarget(renderer, nullptr);
//prune
image.SetClipH(image.GetClipH() / 3);
}
void Button::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string s, SDL_Color color) {
//make the surface (from SDL_ttf)
SDL_Surface* surf = TTF_RenderText_Solid(font, s.c_str(), color);
if (!surf) {
throw(std::runtime_error("Failed to create a TTF surface"));
}
//convert to texture
SDL_Texture* text = SDL_CreateTextureFromSurface(renderer, surf);
SDL_FreeSurface(surf);
if (!text) {
throw(std::runtime_error("Failed to create a TTF texture"));
}
//get the dimensions & rects
int x, y, w, h;
SDL_QueryTexture(text, nullptr, nullptr, &w, &h);
x = (image.GetClipW() - w) / 2;
y = (image.GetClipH() - h) / 2;
SDL_Rect src = {0, 0, w, h};
SDL_Rect dst;
//draw the text to the background
SDL_SetRenderTarget(renderer, image.GetTexture());
for (int i = 0; i < 3; i++) {
dst = {x, y + image.GetClipH() * i, w, h};
SDL_RenderCopy(renderer, text, &src, &dst);
}
SDL_SetRenderTarget(renderer, nullptr);
//free the texture
SDL_DestroyTexture(text);
//DEBUG: testing
// image.SetClipH(image.GetClipH() * 3);
}
void Button::SetX(int x) {
posX = x;
}
void Button::SetY(int y) {
posY = y;
}
Button::State Button::MouseMotion(SDL_MouseMotionEvent const& event) {
//if out of bounds, exit
if (!CheckBounds(event.x, event.y)) {
return state = State::IDLE;
}
//if in bounds, check button
if (event.state & SDL_BUTTON_LMASK && state == State::PRESSED) {
//stay pressed
// state = State::PRESSED;
}
else {
state = State::HOVER;
}
return state;
}
Button::State Button::MouseButtonDown(SDL_MouseButtonEvent const& event) {
//if out of bounds, exit
if (!CheckBounds(event.x, event.y)) {
return state = State::IDLE;
}
//if in bounds, check button
if (event.button == SDL_BUTTON_LEFT) {
return state = State::PRESSED;
}
//NOTE: if not left button down, ignore
return State::HOVER;
}
Button::State Button::MouseButtonUp(SDL_MouseButtonEvent const& event) {
//if out of bounds, exit
if (!CheckBounds(event.x, event.y)) {
return state = State::IDLE;
}
//if not left button up, ignore
if (event.button != SDL_BUTTON_LEFT) {
return state;
}
//if in bounds and left button up, send release signal
if (state == State::PRESSED) {
state = State::HOVER;
return State::RELEASED;
}
return state;
}
void Button::SetState(State s) {
state = s;
}
Button::State Button::GetState() {
return state;
}
bool Button::CheckBounds(int x, int y) {
//return if true (x, y) is within bounds, otherwise return false
return !(
x < posX ||
y < posY ||
x > posX + image.GetClipW() ||
y > posY + image.GetClipH()
);
}
+63
View File
@@ -0,0 +1,63 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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.
*/
#pragma once
#include "image.hpp"
#include "SDL2/SDL_ttf.h"
#include <string>
class Button {
public:
enum State {
IDLE = 0, HOVER = 1, PRESSED = 2, RELEASED = 3
};
//methods
Button() = default;
~Button() = default;
void DrawTo(SDL_Renderer*);
//setup
void SetBackgroundTexture(SDL_Renderer*, SDL_Texture*);
void SetText(SDL_Renderer*, TTF_Font*, std::string, SDL_Color);
void SetX(int x);
void SetY(int y);
//capture input
State MouseMotion(SDL_MouseMotionEvent const&);
State MouseButtonDown(SDL_MouseButtonEvent const&);
State MouseButtonUp(SDL_MouseButtonEvent const&);
//states
void SetState(State); //TODO: idle, busy or disabled
State GetState();
protected:
bool CheckBounds(int x, int y);
Image image;
int posX = 0, posY = 0;
State state = State::IDLE;
};
+211
View File
@@ -0,0 +1,211 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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"
#include "SDL2/SDL_image.h"
#include <sstream>
#include <stdexcept>
Image& Image::operator=(Image const& rhs) {
//don't screw yourself
if (this == &rhs) {
return *this;
}
Free();
//Copy the other Image's stuff
texture = rhs.texture;
clip = rhs.clip;
local = false;
}
Image& Image::operator=(Image&& rhs) {
//don't screw yourself
if (this == &rhs) {
return *this;
}
Free();
//Steal the other Image's stuff
texture = rhs.texture;
clip = rhs.clip;
local = rhs.local;
rhs.texture = nullptr;
rhs.clip = {0, 0, 0, 0};
rhs.local = false;
}
SDL_Texture* Image::Load(SDL_Renderer* renderer, std::string fname) {
Free();
//load the file into a surface
SDL_Surface* surface = IMG_Load(fname.c_str());
if (!surface) {
std::ostringstream msg;
msg << "Failed to load an image file: " << fname;
msg << "; " << IMG_GetError();
throw(std::runtime_error(msg.str()));
}
//create a texture from this surface
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
std::ostringstream msg;
msg << "Failed to convert a newly loaded image file: " << fname;
msg << "; " << SDL_GetError();
throw(std::runtime_error(msg.str()));
}
//set the metadata
clip.x = 0;
clip.y = 0;
if (SDL_QueryTexture(texture, nullptr, nullptr, &clip.w, &clip.h)) {
std::ostringstream msg;
msg << "Failed to record metadata for a newly loaded image file: " << fname;
msg << "; " << SDL_GetError();
throw(std::runtime_error(msg.str()));
}
local = true;
//free the surface & return
SDL_FreeSurface(surface);
return texture;
}
SDL_Texture* Image::Create(SDL_Renderer* renderer, Uint16 w, Uint16 h, SDL_Color blank) {
Free();
//make the texture
texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
w, h);
//check
if (!texture) {
std::ostringstream msg;
msg << "Failed to create a texture; " << SDL_GetError();
throw(std::runtime_error(msg.str()));
}
//set the metadata
clip.x = 0;
clip.y = 0;
if (SDL_QueryTexture(texture, nullptr, nullptr, &clip.w, &clip.h)) {
std::ostringstream msg;
msg << "Failed to record metadata for a newly created image";
msg << "; " << SDL_GetError();
throw(std::runtime_error(msg.str()));
}
local = true;
//blank (black) texture
SDL_SetRenderTarget(renderer, texture);
SDL_SetRenderDrawColor(renderer, blank.r, blank.g, blank.b, blank.a);
SDL_RenderFillRect(renderer, nullptr);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_SetRenderTarget(renderer, nullptr);
return texture;
}
SDL_Texture* Image::CopyTexture(SDL_Renderer* renderer, SDL_Texture* ptr) {
Free();
int w = 0, h = 0;
//get the info
SDL_QueryTexture(ptr, nullptr, nullptr, &w, &h);
//create a texture of (w, h) size (also sets the metadata)
Create(renderer, w, h);
//copy the argument texture to the local texture
SDL_SetRenderTarget(renderer, texture);
SDL_RenderCopy(renderer, ptr, nullptr, nullptr);
SDL_SetRenderTarget(renderer, nullptr);
//return the local texture
return texture;
}
SDL_Texture* Image::SetTexture(SDL_Texture* ptr) {
Free();
texture = ptr;
//set the metadata
clip.x = 0;
clip.y = 0;
if (SDL_QueryTexture(texture, nullptr, nullptr, &clip.w, &clip.h)) {
std::ostringstream msg;
msg << "Failed to record metadata for a newly image image";
msg << "; " << SDL_GetError();
throw(std::runtime_error(msg.str()));
}
local = false;
return texture;
}
SDL_Texture* Image::GetTexture() const {
return texture;
}
void Image::Free() {
if (local) {
SDL_DestroyTexture(texture);
local = false;
}
texture = nullptr;
clip = {0, 0, 0, 0};
}
void Image::DrawTo(SDL_Renderer* const renderer, Sint16 x, Sint16 y, double scaleX, double scaleY) {
if (!texture) {
throw(std::logic_error("No image texture to draw"));
}
SDL_Rect sclip = clip;
SDL_Rect dclip = {x, y, Uint16(clip.w * scaleX), Uint16(clip.h * scaleY)};
SDL_RenderCopy(renderer, texture, &sclip, &dclip);
}
void Image::SetAlpha(Uint8 a) {
if (SDL_SetTextureAlphaMod(texture, a)) {
std::ostringstream msg;
msg << "Failed to set alpha; " << SDL_GetError();
throw(std::runtime_error(msg.str()));
}
}
Uint8 Image::GetAlpha() {
Uint8 ret = 0;
if (SDL_GetTextureAlphaMod(texture, &ret)) {
std::ostringstream msg;
msg << "Failed to get alpha; " << SDL_GetError();
throw(std::runtime_error(msg.str()));
}
return ret;
}
+73
View File
@@ -0,0 +1,73 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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.
*/
#pragma once
#include "SDL2/SDL.h"
#include <string>
class Image {
public:
Image() = default;
Image(Image const& rhs) { *this = rhs; }
Image(Image&& rhs) { *this = std::move(rhs); }
Image(SDL_Renderer* r, std::string fname) { Load(r, fname); }
Image(SDL_Renderer* r, Uint16 w, Uint16 h) { Create(r, w, h); }
Image(SDL_Texture* p) { SetTexture(p); }
virtual ~Image() { Free(); }
Image& operator=(Image const&);
Image& operator=(Image&&);
SDL_Texture* Load(SDL_Renderer* renderer, std::string fname);
SDL_Texture* Create(SDL_Renderer* renderer, Uint16 w, Uint16 h, SDL_Color blank = {0, 0, 0, 255});
SDL_Texture* CopyTexture(SDL_Renderer* renderer, SDL_Texture* ptr);
SDL_Texture* SetTexture(SDL_Texture*);
SDL_Texture* GetTexture() const;
virtual void Free();
void DrawTo(SDL_Renderer* const, Sint16 x, Sint16 y, double scaleX = 1.0, double scaleY = 1.0);
void SetAlpha(Uint8 a);
Uint8 GetAlpha();
//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; }
bool GetLocal() const { return local; }
protected:
SDL_Texture* texture = nullptr;
SDL_Rect clip = {0, 0, 0, 0};
bool local = false;
};
+28
View File
@@ -0,0 +1,28 @@
#config
INCLUDES+=.
LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
#source
CXXSRC=$(wildcard *.cpp)
#objects
OBJDIR=obj
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
#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 $@ $<
+186
View File
@@ -0,0 +1,186 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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"
#include <sstream>
#include <stdexcept>
SpriteSheet& SpriteSheet::operator=(SpriteSheet const& rhs) {
//don't screw yourself
if (this == &rhs) {
return *this;
}
Free();
//Copy the other SpriteSheet's stuff
texture = rhs.texture;
clip = rhs.clip;
local = false;
countX = rhs.countX;
countY = rhs.countY;
indexX = rhs.indexX;
indexY = rhs.indexY;
delay = rhs.delay;
tick = rhs.tick;
}
SpriteSheet& SpriteSheet::operator=(SpriteSheet&& rhs) {
//don't screw yourself
if (this == &rhs) {
return *this;
}
Free();
//Steal the other SpriteSheet's stuff
texture = rhs.texture;
clip = rhs.clip;
local = rhs.local;
countX = rhs.countX;
countY = rhs.countY;
indexX = rhs.indexX;
indexY = rhs.indexY;
delay = rhs.delay;
tick = rhs.tick;
rhs.texture = nullptr;
rhs.clip = {0, 0, 0, 0};
rhs.local = false;
rhs.countX = 0;
rhs.countY = 0;
rhs.indexX = 0;
rhs.indexY = 0;
rhs.delay = 0.0;
rhs.tick = 0.0;
}
void SpriteSheet::Update(double delta) {
//if the delay has passed
if (delay && (tick += delta) >= delay) {
//if the index is out of bounds
if (++indexX >= countX) {
indexX = 0;
}
tick = 0;
}
//modify area drawn
clip.x = indexX * clip.w;
clip.y = indexX * clip.y;
}
SDL_Texture* SpriteSheet::Load(SDL_Renderer* r, std::string fname, Uint16 cx, Uint16 cy) {
//call the base function
Image::Load(r, fname);
//set the metadata
countX = cx;
countY = cy;
//assume clip.x and clip.y were set to the size of the texture
//reduce the w & h to the size of one cell
clip.w = clip.w / countX;
clip.h = clip.h / countY;
indexX = indexY = 0;
delay = tick = 0.0;
return texture;
}
SDL_Texture* SpriteSheet::Create(SDL_Renderer* r, Uint16 w, Uint16 h, Uint16 cx, Uint16 cy) {
//call the base function
Image::Create(r, w, h);
//set the metadata
countX = cx;
countY = cy;
//assume clip.x and clip.y were set to the size of the texture
//reduce the w & h to the size of one cell
clip.w = clip.w / countX;
clip.h = clip.h / countY;
indexX = indexY = 0;
delay = tick = 0.0;
return texture;
}
SDL_Texture* SpriteSheet::SetTexture(SDL_Texture* ptr, Uint16 cx, Uint16 cy) {
//call the base function
Image::SetTexture(ptr);
//set the metadata
countX = cx;
countY = cy;
//assume clip.x and clip.y were set to the size of the texture
//reduce the w & h to the size of one cell
clip.w = clip.w / countX;
clip.h = clip.h / countY;
indexX = indexY = 0;
delay = tick = 0.0;
return texture;
}
void SpriteSheet::Free() {
Image::Free();
countX = countY = 0;
indexX = indexY = 0;
delay = tick = 0.0;
}
Uint16 SpriteSheet::SetCountX(Uint16 i) {
indexX = 0;
return countX = i;
}
Uint16 SpriteSheet::SetCountY(Uint16 i) {
indexY = 0;
return countY = i;
}
Uint16 SpriteSheet::SetIndexX(Uint16 i) {
if (i > countX) {
std::ostringstream msg;
msg << "Cannot set index 'x' to " << i;
throw(std::out_of_range(msg.str()));
}
return indexX = i;
}
Uint16 SpriteSheet::SetIndexY(Uint16 i) {
if (i > countY) {
std::ostringstream msg;
msg << "Cannot set index 'y' to " << i;
throw(std::invalid_argument(msg.str()));
}
return indexY = i;
}
double SpriteSheet::SetDelay(double d) {
tick = 0;
return delay = d;
}
+70
View File
@@ -0,0 +1,70 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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.
*/
#pragma once
#include "image.hpp"
class SpriteSheet : public Image {
public:
SpriteSheet() = default;
SpriteSheet(SpriteSheet const& rhs) { *this = rhs; }
SpriteSheet(SpriteSheet&& rhs) { *this = std::move(rhs); }
SpriteSheet(SDL_Renderer* r, std::string fname, Uint16 cx, Uint16 cy)
{ Load(r, fname, cx, cy); }
SpriteSheet(SDL_Renderer* r, Uint16 w, Uint16 h, Uint16 cx, Uint16 cy)
{ Create(r, w, h, cx, cy); }
SpriteSheet(SDL_Texture* p, Uint16 cx, Uint16 cy)
{ SetTexture(p, cx, cy); }
~SpriteSheet() = default;
SpriteSheet& operator=(SpriteSheet const&);
SpriteSheet& operator=(SpriteSheet&&);
void Update(double delta);
SDL_Texture* Load(SDL_Renderer*, std::string fname, Uint16 cx, Uint16 cy);
SDL_Texture* Create(SDL_Renderer*, Uint16 w, Uint16 h, Uint16 cx, Uint16 cy);
SDL_Texture* SetTexture(SDL_Texture*, Uint16 cx, Uint16 cy);
void Free() override;
Uint16 SetCountX(Uint16);
Uint16 SetCountY(Uint16);
Uint16 SetIndexX(Uint16);
Uint16 SetIndexY(Uint16);
Uint16 GetCountX() const { return countX; }
Uint16 GetCountY() const { return countY; }
Uint16 GetIndexX() const { return indexX; }
Uint16 GetIndexY() const { return indexY; }
double SetDelay(double d);
double GetDelay() const { return delay; }
private:
Uint16 countX = 0, countY = 0, indexX = 0, indexY = 0;
double delay = 0.0, tick = 0.0;
//disable access
using Image::Load;
using Image::Create;
using Image::SetTexture;
};
+56
View File
@@ -0,0 +1,56 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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 "text_box.hpp"
#include <stdexcept>
TextBox::TextBox() {
//
}
TextBox::~TextBox() {
//
}
void TextBox::DrawTo(SDL_Renderer* renderer, int posX, int posY, int pointSize) {
for (std::list<TextLine>::iterator it = lineList.begin(); it != lineList.end(); it++) {
it->DrawTo(renderer, posX, posY);
posY -= pointSize;
}
}
void TextBox::PushLine(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) {
lineList.emplace_front(renderer, font, str, color);
}
void TextBox::PopLine(int num) {
//prevent underflow
num < lineList.size() ? num : lineList.size();
for (int i = 0; i < num; ++i) {
lineList.pop_back();
}
}
void TextBox::ClearLines() {
lineList.clear();
}
+45
View File
@@ -0,0 +1,45 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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.
*/
#pragma once
#include "text_line.hpp"
#include "SDL2/SDL.h"
#include "SDL2/SDL_ttf.h"
#include <string>
#include <list>
class TextBox {
public:
TextBox();
~TextBox();
void DrawTo(SDL_Renderer*, int posX, int posY, int pointSize);
void PushLine(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color);
void PopLine(int num = 1);
void ClearLines();
private:
std::list<TextLine> lineList;
};
+70
View File
@@ -0,0 +1,70 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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 "text_line.hpp"
#include <stdexcept>
SDL_Texture* renderTextTexture(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) {
//make the surface (from SDL_ttf)
SDL_Surface* surface = TTF_RenderText_Solid(font, str.c_str(), color);
if (!surface) {
throw(std::runtime_error("Failed to create a TTF surface"));
}
//convert to texture
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
//cleanup
SDL_FreeSurface(surface);
//check
if (!texture) {
throw(std::runtime_error("Failed to create a TTF texture"));
}
//NOTE: free the texture yourself
return texture;
}
TextLine::TextLine() {
//
}
TextLine::~TextLine() {
ClearText();
}
void TextLine::DrawTo(SDL_Renderer* renderer, int posX, int posY) {
SDL_Rect dclip = {posX, posY, 0, 0};
SDL_QueryTexture(texture, nullptr, nullptr, &dclip.w, &dclip.h);
SDL_RenderCopy(renderer, texture, nullptr, &dclip);
}
void TextLine::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) {
//just use the above global function
SDL_DestroyTexture(texture);
texture = renderTextTexture(renderer, font, str, color);
}
void TextLine::ClearText() {
SDL_DestroyTexture(texture);
}
+45
View File
@@ -0,0 +1,45 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* 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.
*/
#pragma once
#include "SDL2/SDL.h"
#include "SDL2/SDL_ttf.h"
#include <string>
SDL_Texture* renderTextTexture(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color);
class TextLine {
public:
TextLine();
TextLine(SDL_Renderer* r, TTF_Font* f, std::string s, SDL_Color c)
{ SetText(r, f, s, c); }
virtual ~TextLine();
void DrawTo(SDL_Renderer*, int posX, int posY);
void SetText(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color);
void ClearText();
protected:
SDL_Texture* texture = nullptr;
};