Added libcommon as a submodule
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "common"]
|
||||||
|
path = common
|
||||||
|
url = https://github.com/Ratstail91/Tortuga.git
|
||||||
Submodule
+1
Submodule common added at 68eddcfcbb
@@ -16,6 +16,7 @@ else ifeq ($(shell uname), Linux)
|
|||||||
find . -type f -name '*.o' -exec rm -f -r -v {} \;
|
find . -type f -name '*.o' -exec rm -f -r -v {} \;
|
||||||
find . -type f -name '*.a' -exec rm -f -r -v {} \;
|
find . -type f -name '*.a' -exec rm -f -r -v {} \;
|
||||||
rm -f -v $(OUT)
|
rm -f -v $(OUT)
|
||||||
|
find . -empty -type d -delete
|
||||||
endif
|
endif
|
||||||
|
|
||||||
rebuild: clean all
|
rebuild: clean all
|
||||||
|
|||||||
@@ -1,170 +0,0 @@
|
|||||||
/* 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()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
/* 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;
|
|
||||||
};
|
|
||||||
@@ -1,211 +0,0 @@
|
|||||||
/* 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;
|
|
||||||
}
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
/* 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;
|
|
||||||
};
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
#config
|
|
||||||
INCLUDES+=.
|
|
||||||
LIBS+=
|
|
||||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
|
||||||
|
|
||||||
#source
|
|
||||||
CXXSRC=$(wildcard *.cpp)
|
|
||||||
|
|
||||||
#objects
|
|
||||||
OBJDIR=obj
|
|
||||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
|
||||||
|
|
||||||
#output
|
|
||||||
OUTDIR=..
|
|
||||||
OUT=$(addprefix $(OUTDIR)/,libcommon.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 $@ $<
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
/* 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();
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
/* 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;
|
|
||||||
};
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
/* 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);
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
/* 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); }
|
|
||||||
~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;
|
|
||||||
};
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
#config
|
|
||||||
INCLUDES+=. ../common
|
|
||||||
LIBS+=
|
|
||||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
|
||||||
|
|
||||||
#source
|
|
||||||
CXXSRC=$(wildcard *.cpp)
|
|
||||||
|
|
||||||
#objects
|
|
||||||
OBJDIR=obj
|
|
||||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
|
||||||
|
|
||||||
#output
|
|
||||||
OUTDIR=..
|
|
||||||
OUT=$(addprefix $(OUTDIR)/,libmap.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 $@ $<
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
/* 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 "region.hpp"
|
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
#include <cstring>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
int snapToBase(int base, int x) {
|
|
||||||
return floor((double)x / base) * base;
|
|
||||||
}
|
|
||||||
|
|
||||||
Region::Region(int argX, int argY): x(argX), y(argY) {
|
|
||||||
if (x != snapToBase(REGION_WIDTH, x) || y != snapToBase(REGION_HEIGHT, y)) {
|
|
||||||
throw(std::invalid_argument("Region location is off grid"));
|
|
||||||
}
|
|
||||||
memset(tiles, 0, REGION_WIDTH*REGION_HEIGHT*REGION_DEPTH*sizeof(type_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
Region::Region(Region const& rhs): x(rhs.x), y(rhs.y) {
|
|
||||||
memcpy(tiles, rhs.tiles, REGION_WIDTH*REGION_HEIGHT*REGION_DEPTH*sizeof(type_t));
|
|
||||||
solid = rhs.solid;
|
|
||||||
}
|
|
||||||
|
|
||||||
Region::type_t Region::SetTile(int x, int y, int z, type_t v) {
|
|
||||||
if (x < 0 || y < 0 || z < 0 || x >= REGION_WIDTH || y >= REGION_HEIGHT || z >= REGION_DEPTH) {
|
|
||||||
throw(std::out_of_range("Region::SetTile() argument out of range"));
|
|
||||||
}
|
|
||||||
return tiles[x][y][z] = v;
|
|
||||||
}
|
|
||||||
|
|
||||||
Region::type_t Region::GetTile(int x, int y, int z) {
|
|
||||||
if (x < 0 || y < 0 || z < 0 || x >= REGION_WIDTH || y >= REGION_HEIGHT || z >= REGION_DEPTH) {
|
|
||||||
throw(std::out_of_range("Region::GetTile() argument out of range"));
|
|
||||||
}
|
|
||||||
return tiles[x][y][z];
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Region::SetSolid(int x, int y, bool b) {
|
|
||||||
if (x < 0 || y < 0 || x >= REGION_WIDTH || y >= REGION_HEIGHT) {
|
|
||||||
throw(std::out_of_range("Region::SetSolid() argument out of range"));
|
|
||||||
}
|
|
||||||
return solid[x * REGION_WIDTH + y] = b;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Region::GetSolid(int x, int y) {
|
|
||||||
if (x < 0 || y < 0 || x >= REGION_WIDTH || y >= REGION_HEIGHT) {
|
|
||||||
throw(std::out_of_range("Region::GetSolid() argument out of range"));
|
|
||||||
}
|
|
||||||
return solid[x * REGION_WIDTH + y];
|
|
||||||
}
|
|
||||||
|
|
||||||
int Region::GetX() const {
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Region::GetY() const {
|
|
||||||
return y;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::bitset<REGION_WIDTH*REGION_HEIGHT>* Region::GetSolidBitset() {
|
|
||||||
return &solid;
|
|
||||||
}
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
/* 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 <bitset>
|
|
||||||
|
|
||||||
//the region's storage format
|
|
||||||
constexpr int REGION_WIDTH = 20;
|
|
||||||
constexpr int REGION_HEIGHT = 20;
|
|
||||||
constexpr int REGION_DEPTH = 3;
|
|
||||||
|
|
||||||
//utility function
|
|
||||||
int snapToBase(int base, int x);
|
|
||||||
|
|
||||||
class Region {
|
|
||||||
public:
|
|
||||||
typedef unsigned char type_t;
|
|
||||||
|
|
||||||
Region() = delete;
|
|
||||||
Region(int x, int y);
|
|
||||||
Region(Region const&);
|
|
||||||
~Region() = default;
|
|
||||||
|
|
||||||
type_t SetTile(int x, int y, int z, type_t v);
|
|
||||||
type_t GetTile(int x, int y, int z);
|
|
||||||
|
|
||||||
bool SetSolid(int x, int y, bool b);
|
|
||||||
bool GetSolid(int x, int y);
|
|
||||||
|
|
||||||
//accessors
|
|
||||||
int GetX() const;
|
|
||||||
int GetY() const;
|
|
||||||
|
|
||||||
std::bitset<REGION_WIDTH*REGION_HEIGHT>* GetSolidBitset();
|
|
||||||
private:
|
|
||||||
friend class TileSheet;
|
|
||||||
|
|
||||||
const int x;
|
|
||||||
const int y;
|
|
||||||
|
|
||||||
type_t tiles[REGION_WIDTH][REGION_HEIGHT][REGION_DEPTH];
|
|
||||||
std::bitset<REGION_WIDTH*REGION_HEIGHT> solid;
|
|
||||||
};
|
|
||||||
@@ -1,99 +0,0 @@
|
|||||||
/* 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 "region_api.hpp"
|
|
||||||
|
|
||||||
#include "region.hpp"
|
|
||||||
|
|
||||||
static int setTile(lua_State* L) {
|
|
||||||
Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
|
|
||||||
int ret = region->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5));
|
|
||||||
lua_pushinteger(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getTile(lua_State* L) {
|
|
||||||
Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
|
|
||||||
int ret = region->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1);
|
|
||||||
lua_pushinteger(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int setSolid(lua_State* L) {
|
|
||||||
Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
|
|
||||||
bool ret = region->SetSolid(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_toboolean(L, 4));
|
|
||||||
lua_pushboolean(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getSolid(lua_State* L) {
|
|
||||||
Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
|
|
||||||
bool ret = region->GetSolid(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1);
|
|
||||||
lua_pushboolean(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getX(lua_State* L) {
|
|
||||||
Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
|
|
||||||
lua_pushinteger(L, region->GetX());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getY(lua_State* L) {
|
|
||||||
Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
|
|
||||||
lua_pushinteger(L, region->GetY());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getWidth(lua_State* L) {
|
|
||||||
lua_pushinteger(L, REGION_WIDTH);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getHeight(lua_State* L) {
|
|
||||||
lua_pushinteger(L, REGION_HEIGHT);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getDepth(lua_State* L) {
|
|
||||||
lua_pushinteger(L, REGION_DEPTH);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const luaL_Reg regionLib[] = {
|
|
||||||
{"SetTile",setTile},
|
|
||||||
{"GetTile",getTile},
|
|
||||||
{"SetSolid",setSolid},
|
|
||||||
{"GetSolid",getSolid},
|
|
||||||
{"GetX",getX},
|
|
||||||
{"GetY",getY},
|
|
||||||
|
|
||||||
//the global macros
|
|
||||||
{"GetWidth",getWidth},
|
|
||||||
{"GetHeight",getHeight},
|
|
||||||
{"GetDepth",getDepth},
|
|
||||||
{nullptr, nullptr}
|
|
||||||
};
|
|
||||||
|
|
||||||
LUAMOD_API int openRegionAPI(lua_State* L) {
|
|
||||||
luaL_newlib(L, regionLib);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
/* 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 "lua.hpp"
|
|
||||||
|
|
||||||
#define TORTUGA_REGION_API "region"
|
|
||||||
LUAMOD_API int openRegionAPI(lua_State* L);
|
|
||||||
@@ -1,172 +0,0 @@
|
|||||||
/* 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 "region_pager_api.hpp"
|
|
||||||
|
|
||||||
#include "region_pager_lua.hpp"
|
|
||||||
#include "region.hpp"
|
|
||||||
|
|
||||||
//DOCS: These glue functions simply wrap RegionPagerLua's methods
|
|
||||||
//NOTE: zero indexing is used here, but not in the region API
|
|
||||||
|
|
||||||
static int setTile(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
int ret = pager->SetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
|
|
||||||
lua_pushinteger(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getTile(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
int ret = pager->GetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4));
|
|
||||||
lua_pushinteger(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int setSolid(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
bool ret = pager->SetSolid(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_toboolean(L, 4));
|
|
||||||
lua_pushboolean(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getSolid(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
bool ret = pager->GetSolid(lua_tointeger(L, 2), lua_tointeger(L, 3));
|
|
||||||
lua_pushboolean(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getRegion(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3));
|
|
||||||
lua_pushlightuserdata(L, region);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int loadRegion(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
Region* region = pager->LoadRegion(lua_tointeger(L, 2), lua_tointeger(L, 3));
|
|
||||||
lua_pushlightuserdata(L, region);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int saveRegion(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
Region* region = pager->SaveRegion(lua_tointeger(L, 2), lua_tointeger(L, 3));
|
|
||||||
lua_pushlightuserdata(L, region);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int createRegion(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
Region* region = pager->CreateRegion(lua_tointeger(L, 2), lua_tointeger(L, 3));
|
|
||||||
lua_pushlightuserdata(L, region);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int unloadRegion(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
|
|
||||||
//two argument types: coords & the region itself
|
|
||||||
switch(lua_type(L, 2)) {
|
|
||||||
case LUA_TNUMBER:
|
|
||||||
pager->UnloadIf([&](Region const& region) -> bool {
|
|
||||||
int x = lua_tointeger(L, 2);
|
|
||||||
int y = lua_tointeger(L, 3);
|
|
||||||
return region.GetX() == x && region.GetY() == y;
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case LUA_TLIGHTUSERDATA:
|
|
||||||
pager->UnloadIf([&](Region const& region) -> bool {
|
|
||||||
return (®ion) == lua_touserdata(L, 2);
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int setOnLoad(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
luaL_unref(L, LUA_REGISTRYINDEX, pager->GetLoadReference());
|
|
||||||
pager->SetLoadReference(luaL_ref(L, LUA_REGISTRYINDEX));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int setOnSave(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
luaL_unref(L, LUA_REGISTRYINDEX, pager->GetSaveReference());
|
|
||||||
pager->SetSaveReference(luaL_ref(L, LUA_REGISTRYINDEX));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int setOnCreate(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
luaL_unref(L, LUA_REGISTRYINDEX, pager->GetCreateReference());
|
|
||||||
pager->SetCreateReference(luaL_ref(L, LUA_REGISTRYINDEX));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int setOnUnload(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
luaL_unref(L, LUA_REGISTRYINDEX, pager->GetUnloadReference());
|
|
||||||
pager->SetUnloadReference(luaL_ref(L, LUA_REGISTRYINDEX));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
//debugging
|
|
||||||
static int containerSize(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = static_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
lua_pushinteger(L, pager->GetContainer()->size());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const luaL_Reg regionPagerLib[] = {
|
|
||||||
//curry
|
|
||||||
{"SetTile", setTile},
|
|
||||||
{"GetTile", getTile},
|
|
||||||
{"SetSolid", setSolid},
|
|
||||||
{"GetSolid", getSolid},
|
|
||||||
|
|
||||||
//access and control
|
|
||||||
{"GetRegion", getRegion},
|
|
||||||
{"LoadRegion", loadRegion},
|
|
||||||
{"SaveRegion", saveRegion},
|
|
||||||
{"CreateRegion", createRegion},
|
|
||||||
{"UnloadRegion", unloadRegion},
|
|
||||||
|
|
||||||
//triggers
|
|
||||||
{"SetOnLoad",setOnLoad},
|
|
||||||
{"SetOnSave",setOnSave},
|
|
||||||
{"SetOnCreate",setOnCreate},
|
|
||||||
{"SetOnUnload",setOnUnload},
|
|
||||||
|
|
||||||
//debugging
|
|
||||||
{"ContainerSize", containerSize},
|
|
||||||
|
|
||||||
//sentinel
|
|
||||||
{nullptr, nullptr}
|
|
||||||
};
|
|
||||||
|
|
||||||
LUAMOD_API int openRegionPagerAPI(lua_State* L) {
|
|
||||||
luaL_newlib(L, regionPagerLib);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
/* 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 "lua.hpp"
|
|
||||||
|
|
||||||
#define TORTUGA_REGION_PAGER_API "region_pager"
|
|
||||||
LUAMOD_API int openRegionPagerAPI(lua_State* L);
|
|
||||||
@@ -1,106 +0,0 @@
|
|||||||
/* 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 "region_pager_base.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
RegionPagerBase::~RegionPagerBase() {
|
|
||||||
UnloadAll();
|
|
||||||
};
|
|
||||||
|
|
||||||
Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) {
|
|
||||||
Region* ptr = GetRegion(x, y);
|
|
||||||
return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v);
|
|
||||||
}
|
|
||||||
|
|
||||||
Region::type_t RegionPagerBase::GetTile(int x, int y, int z) {
|
|
||||||
Region* ptr = GetRegion(x, y);
|
|
||||||
return ptr->GetTile(x - ptr->GetX(), y - ptr->GetY(), z);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool RegionPagerBase::SetSolid(int x, int y, int b) {
|
|
||||||
Region* ptr = GetRegion(x, y);
|
|
||||||
return ptr->SetSolid(x - ptr->GetX(), y - ptr->GetY(), b);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool RegionPagerBase::GetSolid(int x, int y) {
|
|
||||||
Region* ptr = GetRegion(x, y);
|
|
||||||
return ptr->GetSolid(x - ptr->GetX(), y - ptr->GetY());
|
|
||||||
}
|
|
||||||
|
|
||||||
Region* RegionPagerBase::GetRegion(int x, int y) {
|
|
||||||
x = snapToBase(REGION_WIDTH, x);
|
|
||||||
y = snapToBase(REGION_HEIGHT, y);
|
|
||||||
|
|
||||||
//get the region by various means
|
|
||||||
Region* ptr = nullptr;
|
|
||||||
ptr = FindRegion(x, y);
|
|
||||||
if (ptr) return ptr;
|
|
||||||
ptr = LoadRegion(x, y);
|
|
||||||
if (ptr) return ptr;
|
|
||||||
return CreateRegion(x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
Region* RegionPagerBase::FindRegion(int x, int y) {
|
|
||||||
//find the region
|
|
||||||
std::list<Region>::iterator it = find_if(regionList.begin(), regionList.end(), [x, y](Region& region) -> bool {
|
|
||||||
return region.GetX() == x && region.GetY() == y;
|
|
||||||
});
|
|
||||||
return it != regionList.end() ? &(*it) : nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
Region* RegionPagerBase::PushRegion(Region* const ptr) {
|
|
||||||
//BUG: #45 Some regions are occasionally losing their tile data
|
|
||||||
regionList.push_front(*ptr);
|
|
||||||
return ®ionList.front();
|
|
||||||
}
|
|
||||||
|
|
||||||
Region* RegionPagerBase::LoadRegion(int x, int y) {
|
|
||||||
//EMPTY, intended for override
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
Region* RegionPagerBase::SaveRegion(int x, int y) {
|
|
||||||
//EMPTY, intended for override
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
Region* RegionPagerBase::CreateRegion(int x, int y) {
|
|
||||||
if (FindRegion(x, y)) {
|
|
||||||
throw(std::logic_error("Cannot overwrite an existing region"));
|
|
||||||
}
|
|
||||||
regionList.emplace_front(x, y);
|
|
||||||
return ®ionList.front();
|
|
||||||
}
|
|
||||||
|
|
||||||
void RegionPagerBase::UnloadIf(std::function<bool(Region const&)> fn) {
|
|
||||||
regionList.remove_if(fn);
|
|
||||||
}
|
|
||||||
|
|
||||||
void RegionPagerBase::UnloadAll() {
|
|
||||||
regionList.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::list<Region>* RegionPagerBase::GetContainer() {
|
|
||||||
return ®ionList;
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
/* 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 "region.hpp"
|
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
#include <list>
|
|
||||||
|
|
||||||
class RegionPagerBase {
|
|
||||||
public:
|
|
||||||
RegionPagerBase() = default;
|
|
||||||
virtual ~RegionPagerBase();
|
|
||||||
|
|
||||||
//tile manipulation
|
|
||||||
virtual Region::type_t SetTile(int x, int y, int z, Region::type_t v);
|
|
||||||
virtual Region::type_t GetTile(int x, int y, int z);
|
|
||||||
|
|
||||||
//solid manipulation
|
|
||||||
virtual bool SetSolid(int x, int y, int b);
|
|
||||||
virtual bool GetSolid(int x, int y);
|
|
||||||
|
|
||||||
//region manipulation
|
|
||||||
virtual Region* GetRegion(int x, int y);
|
|
||||||
virtual Region* FindRegion(int x, int y);
|
|
||||||
virtual Region* PushRegion(Region* const);
|
|
||||||
|
|
||||||
virtual Region* LoadRegion(int x, int y);
|
|
||||||
virtual Region* SaveRegion(int x, int y);
|
|
||||||
virtual Region* CreateRegion(int x, int y);
|
|
||||||
|
|
||||||
virtual void UnloadIf(std::function<bool(Region const&)> fn);
|
|
||||||
virtual void UnloadAll();
|
|
||||||
|
|
||||||
//accessors & mutators
|
|
||||||
std::list<Region>* GetContainer();
|
|
||||||
protected:
|
|
||||||
std::list<Region> regionList;
|
|
||||||
};
|
|
||||||
@@ -1,208 +0,0 @@
|
|||||||
/* 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 "region_pager_lua.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
RegionPagerLua::~RegionPagerLua() {
|
|
||||||
//unload all regions
|
|
||||||
UnloadAll();
|
|
||||||
//clear any stored functions
|
|
||||||
luaL_unref(lua, LUA_REGISTRYINDEX, loadRef);
|
|
||||||
luaL_unref(lua, LUA_REGISTRYINDEX, saveRef);
|
|
||||||
luaL_unref(lua, LUA_REGISTRYINDEX, createRef);
|
|
||||||
luaL_unref(lua, LUA_REGISTRYINDEX, unloadRef);
|
|
||||||
}
|
|
||||||
|
|
||||||
//return the loaded region, or nullptr on failure
|
|
||||||
Region* RegionPagerLua::LoadRegion(int x, int y) {
|
|
||||||
//get the pager's function from the registry
|
|
||||||
lua_rawgeti(lua, LUA_REGISTRYINDEX, loadRef);
|
|
||||||
|
|
||||||
//check if this function is available
|
|
||||||
if (lua_isnil(lua, -1)) {
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
//signal that there is no load function
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
//something to work on
|
|
||||||
Region tmpRegion(x, y);
|
|
||||||
lua_pushlightuserdata(lua, &tmpRegion);
|
|
||||||
|
|
||||||
//call the funtion, 1 arg, 1 return
|
|
||||||
if (lua_pcall(lua, 1, 1, 0) != LUA_OK) {
|
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
|
|
||||||
}
|
|
||||||
|
|
||||||
//check the return value, success or failure
|
|
||||||
if (lua_isboolean(lua, -1) && lua_toboolean(lua, -1)) {
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
//push and return the loaded region
|
|
||||||
regionList.push_front(tmpRegion);
|
|
||||||
return ®ionList.front();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
//signal a failure
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//NOTE: this return value seems strange; could replace it with a boolean
|
|
||||||
//return the saved region, or nullptr on failure
|
|
||||||
Region* RegionPagerLua::SaveRegion(int x, int y) {
|
|
||||||
//get the pager's function from the registry
|
|
||||||
lua_rawgeti(lua, LUA_REGISTRYINDEX, saveRef);
|
|
||||||
|
|
||||||
//check if this function is available
|
|
||||||
if (lua_isnil(lua, -1)) {
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
//signal that the region wasn't saved
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
//find the specified region
|
|
||||||
Region* ptr = FindRegion(x, y);
|
|
||||||
if (!ptr) {
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
//signal that there is no save function
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
lua_pushlightuserdata(lua, ptr);
|
|
||||||
|
|
||||||
//call the function, 1 arg, 1 return
|
|
||||||
if (lua_pcall(lua, 1, 1, 0) != LUA_OK) {
|
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
|
|
||||||
}
|
|
||||||
|
|
||||||
//check the return value, success or failure
|
|
||||||
if (lua_isboolean(lua, -1) && lua_toboolean(lua, -1)) {
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
//return the specified region that was saved
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
//signal a failure
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//DOCS: since this method is the last ditch call from GetRegion, it must return a valid region object, even if the create function hasn't been set.
|
|
||||||
//return a new region, throwing an error on failure
|
|
||||||
Region* RegionPagerLua::CreateRegion(int x, int y) {
|
|
||||||
if (FindRegion(x, y)) {
|
|
||||||
throw(std::logic_error("Cannot overwrite an existing region"));
|
|
||||||
}
|
|
||||||
|
|
||||||
//get the pager's function from the registry
|
|
||||||
lua_rawgeti(lua, LUA_REGISTRYINDEX, createRef);
|
|
||||||
|
|
||||||
//check if this function is available
|
|
||||||
if (lua_isnil(lua, -1)) {
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
//return an empty region object
|
|
||||||
regionList.emplace_front(x, y);
|
|
||||||
return ®ionList.front();
|
|
||||||
}
|
|
||||||
|
|
||||||
//something to work on
|
|
||||||
Region tmpRegion(x, y);
|
|
||||||
lua_pushlightuserdata(lua, &tmpRegion);
|
|
||||||
|
|
||||||
//call the function, 1 arg, 0 return
|
|
||||||
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
|
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
|
|
||||||
}
|
|
||||||
|
|
||||||
//return the new region
|
|
||||||
regionList.push_front(tmpRegion);
|
|
||||||
return ®ionList.front();
|
|
||||||
}
|
|
||||||
|
|
||||||
//no return
|
|
||||||
void RegionPagerLua::UnloadIf(std::function<bool(Region const&)> fn) {
|
|
||||||
//get the pager's function from the registry
|
|
||||||
lua_rawgeti(lua, LUA_REGISTRYINDEX, unloadRef);
|
|
||||||
|
|
||||||
//check if this function is available
|
|
||||||
if (lua_isnil(lua, -1)) {
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
//remove the regions anyway
|
|
||||||
regionList.remove_if(fn);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//run each region through this lambda
|
|
||||||
regionList.remove_if([&](Region& region) -> bool {
|
|
||||||
if (fn(region)) {
|
|
||||||
|
|
||||||
//push a copy of the function onto the stack with the region
|
|
||||||
lua_pushvalue(lua, -1);
|
|
||||||
lua_pushlightuserdata(lua, static_cast<void*>(®ion));
|
|
||||||
|
|
||||||
//call the function, 1 arg, 0 return
|
|
||||||
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
|
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
|
|
||||||
}
|
|
||||||
|
|
||||||
//signal to the container
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//signal to the container
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
|
|
||||||
//pop the base copy of the function
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void RegionPagerLua::UnloadAll() {
|
|
||||||
//get the pager's function from the registry
|
|
||||||
lua_rawgeti(lua, LUA_REGISTRYINDEX, unloadRef);
|
|
||||||
|
|
||||||
//check if this function is available
|
|
||||||
if (lua_isnil(lua, -1)) {
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
//remove the regions anyway
|
|
||||||
regionList.clear();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& it : regionList) {
|
|
||||||
//push a copy of the function onto the stack with the region
|
|
||||||
lua_pushvalue(lua, -1);
|
|
||||||
lua_pushlightuserdata(lua, &it);
|
|
||||||
|
|
||||||
//call the function, 1 arg, 0 return
|
|
||||||
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
|
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//pop the base copy of the function
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
|
|
||||||
//remove from memory
|
|
||||||
regionList.clear();
|
|
||||||
}
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
/* 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 "region_pager_base.hpp"
|
|
||||||
|
|
||||||
#include "lua.hpp"
|
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
//DOCS: set the lua hook before use
|
|
||||||
|
|
||||||
class RegionPagerLua : public RegionPagerBase {
|
|
||||||
public:
|
|
||||||
RegionPagerLua() = default;
|
|
||||||
~RegionPagerLua();
|
|
||||||
|
|
||||||
//region manipulation
|
|
||||||
Region* LoadRegion(int x, int y) override;
|
|
||||||
Region* SaveRegion(int x, int y) override;
|
|
||||||
Region* CreateRegion(int x, int y) override;
|
|
||||||
|
|
||||||
void UnloadIf(std::function<bool(Region const&)> fn) override;
|
|
||||||
void UnloadAll() override;
|
|
||||||
|
|
||||||
//accessors & mutators
|
|
||||||
lua_State* SetLuaState(lua_State* L) { return lua = L; }
|
|
||||||
lua_State* GetLuaState() { return lua; }
|
|
||||||
|
|
||||||
//utilities for the API
|
|
||||||
int SetLoadReference(int i) { return loadRef = i; }
|
|
||||||
int SetSaveReference(int i) { return saveRef = i; }
|
|
||||||
int SetCreateReference(int i) { return createRef = i; }
|
|
||||||
int SetUnloadReference(int i) { return unloadRef = i; }
|
|
||||||
|
|
||||||
int GetLoadReference() { return loadRef; }
|
|
||||||
int GetSaveReference() { return saveRef; }
|
|
||||||
int GetCreateReference() { return createRef; }
|
|
||||||
int GetUnloadReference() { return unloadRef; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
lua_State* lua = nullptr;
|
|
||||||
|
|
||||||
int loadRef = LUA_NOREF;
|
|
||||||
int saveRef = LUA_NOREF;
|
|
||||||
int createRef = LUA_NOREF;
|
|
||||||
int unloadRef = LUA_NOREF;
|
|
||||||
};
|
|
||||||
@@ -1,124 +0,0 @@
|
|||||||
/* 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 "tile_sheet.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
TileSheet& TileSheet::operator=(TileSheet const& rhs) {
|
|
||||||
//don't screw yourself
|
|
||||||
if (this == &rhs) {
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Free();
|
|
||||||
|
|
||||||
//Copy the other TileSheet's stuff
|
|
||||||
texture = rhs.texture;
|
|
||||||
clip = rhs.clip;
|
|
||||||
local = false;
|
|
||||||
countX = rhs.countX;
|
|
||||||
countY = rhs.countY;
|
|
||||||
}
|
|
||||||
|
|
||||||
TileSheet& TileSheet::operator=(TileSheet&& rhs) {
|
|
||||||
//don't screw yourself
|
|
||||||
if (this == &rhs) {
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Free();
|
|
||||||
|
|
||||||
//Copy the other TileSheet's stuff
|
|
||||||
texture = rhs.texture;
|
|
||||||
clip = rhs.clip;
|
|
||||||
local = false;
|
|
||||||
countX = rhs.countX;
|
|
||||||
countY = rhs.countY;
|
|
||||||
|
|
||||||
rhs.texture = nullptr;
|
|
||||||
rhs.clip = {0, 0, 0, 0};
|
|
||||||
rhs.local = false;
|
|
||||||
rhs.countX = 0;
|
|
||||||
rhs.countY = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TileSheet::Load(SDL_Renderer* renderer, std::string fname, int tileWidth, int tileHeight) {
|
|
||||||
Image::Load(renderer, fname);
|
|
||||||
countX = clip.w / tileWidth;
|
|
||||||
countY = clip.h / tileHeight;
|
|
||||||
clip.w = tileWidth;
|
|
||||||
clip.h = tileHeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Texture* TileSheet::SetTexture(SDL_Texture* ptr, int tileWidth, int tileHeight) {
|
|
||||||
Image::SetTexture(ptr);
|
|
||||||
countX = clip.w / tileWidth;
|
|
||||||
countY = clip.h / tileHeight;
|
|
||||||
clip.w = tileWidth;
|
|
||||||
clip.h = tileHeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TileSheet::Free() {
|
|
||||||
Image::Free();
|
|
||||||
countX = countY = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TileSheet::DrawLayerTo(SDL_Renderer* const renderer, Region* const region, int layer, int camX, int camY, double scaleX, double scaleY) {
|
|
||||||
//TODO: (2) empty
|
|
||||||
}
|
|
||||||
|
|
||||||
void TileSheet::DrawRegionTo(SDL_Renderer* const renderer, Region* const region, int camX, int camY, double scaleX, double scaleY) {
|
|
||||||
//NOTE: TileSheet is a friend class of Region
|
|
||||||
//reimplementing DrawTo() to improve performance (less indirection)
|
|
||||||
if (!texture) {
|
|
||||||
throw(std::logic_error("No image texture to draw"));
|
|
||||||
}
|
|
||||||
|
|
||||||
//the local variables
|
|
||||||
SDL_Rect sclip = {0, 0, clip.w, clip.h};
|
|
||||||
SDL_Rect dclip = {0, 0, Uint16(clip.w * scaleX), Uint16(clip.h * scaleY)};
|
|
||||||
Region::type_t tile = 0;
|
|
||||||
|
|
||||||
//for each tile
|
|
||||||
for (register int i = 0; i < REGION_WIDTH; ++i) {
|
|
||||||
for (register int j = 0; j < REGION_HEIGHT; ++j) {
|
|
||||||
for (register int k = 0; k < REGION_DEPTH; ++k) {
|
|
||||||
//get the value to skip expensive lookups
|
|
||||||
tile = region->tiles[i][j][k];
|
|
||||||
|
|
||||||
//0 is invisible
|
|
||||||
if (tile == 0) continue;
|
|
||||||
|
|
||||||
//set the sclip
|
|
||||||
sclip.x = (tile-1) % countX * clip.h;
|
|
||||||
sclip.y = (tile-1) / countX * clip.w;
|
|
||||||
|
|
||||||
//set the dclip
|
|
||||||
dclip.x = ((region->x + i) * clip.w - camX) * scaleX;
|
|
||||||
dclip.y = ((region->y + j) * clip.h - camY) * scaleY;
|
|
||||||
|
|
||||||
//draw
|
|
||||||
SDL_RenderCopy(renderer, texture, &sclip, &dclip);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
/* 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 "region.hpp"
|
|
||||||
|
|
||||||
#include "image.hpp"
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class TileSheet : public Image {
|
|
||||||
public:
|
|
||||||
TileSheet() = default;
|
|
||||||
TileSheet(TileSheet const& rhs) { *this = rhs; }
|
|
||||||
TileSheet(TileSheet&& rhs) { *this = std::move(rhs); }
|
|
||||||
TileSheet(SDL_Renderer* r, std::string fn, int tw, int th) { Load(r, fn, tw, th); }
|
|
||||||
TileSheet(SDL_Texture* p, int tw, int th) { SetTexture(p, tw, th); }
|
|
||||||
~TileSheet() = default;
|
|
||||||
|
|
||||||
TileSheet& operator=(TileSheet const&);
|
|
||||||
TileSheet& operator=(TileSheet&&);
|
|
||||||
|
|
||||||
void Load(SDL_Renderer*, std::string fname, int tileWidth, int tileHeight);
|
|
||||||
SDL_Texture* SetTexture(SDL_Texture*, int tileWidth, int tileHeight);
|
|
||||||
void Free() override;
|
|
||||||
|
|
||||||
void DrawLayerTo(SDL_Renderer* const renderer, Region* const region, int layer, int camX, int camY, double scaleX = 1.0, double scaleY = 1.0);
|
|
||||||
void DrawRegionTo(SDL_Renderer* const renderer, Region* const region, int camX, int camY, double scaleX = 1.0, double scaleY = 1.0);
|
|
||||||
|
|
||||||
//accessors
|
|
||||||
//DOCS: Reuse Image::clip for tile sizes
|
|
||||||
int GetCountX() { return countX; }
|
|
||||||
int GetCountY() { return countY; }
|
|
||||||
int GetTileW() { return clip.w; }
|
|
||||||
int GetTileH() { return clip.h; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
int countX = 0, countY = 0;
|
|
||||||
|
|
||||||
using Image::Load;
|
|
||||||
using Image::Create;
|
|
||||||
using Image::SetTexture;
|
|
||||||
using Image::SetClip;
|
|
||||||
using Image::SetClipX;
|
|
||||||
using Image::SetClipY;
|
|
||||||
using Image::SetClipW;
|
|
||||||
using Image::SetClipH;
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user