diff --git a/.gitmodules b/.gitmodules index 64106e6..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "bin"] - path = bin - url = https://github.com/Ratstail91/Tortuga.git diff --git a/bin b/bin deleted file mode 160000 index c653980..0000000 --- a/bin +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c65398019329fe3d28670cd57f84461b48082642 diff --git a/common/frameworks/network/makefile b/common/frameworks/network/makefile deleted file mode 100644 index 3bcac3d..0000000 --- a/common/frameworks/network/makefile +++ /dev/null @@ -1,29 +0,0 @@ -#config -INCLUDES+=. packet_types ../gameplay ../map ../utilities -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) - $(MAKE) -C packet_types - -$(OBJ): | $(OBJDIR) - -$(OUT): | $(OUTDIR) - -$(OBJDIR): - mkdir $(OBJDIR) - -$(OUTDIR): - mkdir $(OUTDIR) - -$(OBJDIR)/%.o: %.cpp - $(CXX) $(CXXFLAGS) -c -o $@ $< diff --git a/common/frameworks/scenes/application.cpp b/common/frameworks/scenes/application.cpp deleted file mode 100644 index 6e7aa93..0000000 --- a/common/frameworks/scenes/application.cpp +++ /dev/null @@ -1,188 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 "application.hpp" - -#include -#include -#include - -void Application::Init(int argc, char* argv[]) { - //create and check the window - window = SDL_CreateWindow( - "Example Caption", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - screenWidth, - screenHeight, - SDL_WINDOW_RESIZABLE); - - if (!window) { - std::ostringstream msg; - msg << "Failed to create the window: " << SDL_GetError(); - throw(std::runtime_error(msg.str())); - } - - //create and check the renderer - renderer = SDL_CreateRenderer(window, -1, 0); - - if (!renderer) { - std::ostringstream msg; - msg << "Failed to create the renderer: " << SDL_GetError(); - throw(std::runtime_error(msg.str())); - } - - //screen scaling - SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best"); - SDL_RenderSetLogicalSize(renderer, screenWidth, screenHeight); - - //set the hook for the renderer - BaseScene::SetRenderer(renderer); -} - -void Application::Proc() { - //load the first scene - ProcessSceneSignal(SceneSignal::FIRST); - - //fixed frame rate - typedef std::chrono::steady_clock Clock; - - Clock::time_point simTime = Clock::now(); - Clock::time_point realTime; - constexpr std::chrono::duration frameDelay(16); //~60FPS - - //the game loop continues until the scenes signal QUIT - while(activeScene->GetSceneSignal() != SceneSignal::QUIT) { - //switch scenes if necessary - if(activeScene->GetSceneSignal() != SceneSignal::CONTINUE) { - ProcessSceneSignal(activeScene->GetSceneSignal()); - continue; - } - - //update the current time - realTime = Clock::now(); - - //simulate the game or give the machine a break - if (simTime < realTime) { - while(simTime < realTime) { - //call the user defined functions - activeScene->FrameStart(); - ProcessEvents(); - activeScene->Update(); - activeScene->FrameEnd(); - - //step to the next frame - simTime += frameDelay; - } - } - else { - SDL_Delay(1); - } - - SDL_RenderClear(renderer); - activeScene->RenderFrame(renderer); - SDL_RenderPresent(renderer); - } - - //cleanup - ClearScene(); -} - -void Application::Quit() { - //clean up after the program - BaseScene::SetRenderer(nullptr); - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); -} - -//------------------------- -//Scene management -//------------------------- - -void Application::ProcessEvents() { - SDL_Event event; - while(SDL_PollEvent(&event)) { - switch(event.type) { - case SDL_QUIT: - activeScene->QuitEvent(); - break; - - case SDL_MOUSEMOTION: - activeScene->MouseMotion(event.motion); - break; - - case SDL_MOUSEBUTTONDOWN: - activeScene->MouseButtonDown(event.button); - break; - - case SDL_MOUSEBUTTONUP: - activeScene->MouseButtonUp(event.button); - break; - - case SDL_MOUSEWHEEL: - activeScene->MouseWheel(event.wheel); - break; - - case SDL_KEYDOWN: - activeScene->KeyDown(event.key); - break; - - case SDL_KEYUP: - activeScene->KeyUp(event.key); - break; - - //TODO: joystick and controller events - - //window events are handled internally - case SDL_WINDOWEVENT: - switch(event.window.event) { - case SDL_WINDOWEVENT_RESIZED: - SDL_RenderSetLogicalSize(renderer, event.window.data1, event.window.data2); - break; - } - break; - } - } -} - -//Add the custom scene headers here -#include "example_scene.hpp" - -void Application::ProcessSceneSignal(SceneSignal signal) { - ClearScene(); - - switch(signal) { - case SceneSignal::FIRST: - case SceneSignal::EXAMPLE_SCENE: - activeScene = new ExampleScene(); - break; - default: { - std::ostringstream msg; - msg << "Failed to recognize the scene signal: " << signal; - throw(std::logic_error(msg.str())); - } - } -} - -void Application::ClearScene() { - delete activeScene; - activeScene = nullptr; -} \ No newline at end of file diff --git a/common/frameworks/scenes/application.hpp b/common/frameworks/scenes/application.hpp deleted file mode 100644 index 04afe8b..0000000 --- a/common/frameworks/scenes/application.hpp +++ /dev/null @@ -1,54 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 "base_scene.hpp" -#include "scene_signal.hpp" - -#include "SDL2/SDL.h" - -//TODO: do something with these -constexpr int screenWidth = 800; -constexpr int screenHeight = 600; - -//DOCS: The Application class handles scene switching, utilizing only one window -class Application { -public: - Application() = default; - ~Application() = default; - - void Init(int argc, char* argv[]); - void Proc(); - void Quit(); - -private: - //scene management - void ProcessEvents(); - void ProcessSceneSignal(SceneSignal); - void ClearScene(); - - BaseScene* activeScene = nullptr; - - //TODO: build a "window" class? - SDL_Window* window = nullptr; - SDL_Renderer* renderer = nullptr; -}; \ No newline at end of file diff --git a/common/frameworks/scenes/base_scene.cpp b/common/frameworks/scenes/base_scene.cpp deleted file mode 100644 index 03d7f4c..0000000 --- a/common/frameworks/scenes/base_scene.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 "base_scene.hpp" - -SDL_Renderer* BaseScene::rendererHandle = nullptr; - -BaseScene::BaseScene() { - //EMPTY -} - -BaseScene::~BaseScene() { - //EMPTY -} - -void BaseScene::RenderFrame(SDL_Renderer* renderer) { - //EMPTY -} - -void BaseScene::SetRenderer(SDL_Renderer* r) { - rendererHandle = r; -} - -SDL_Renderer* BaseScene::GetRenderer() { - return rendererHandle; -} - -void BaseScene::SetSceneSignal(SceneSignal signal) { - sceneSignal = signal; -} - -SceneSignal BaseScene::GetSceneSignal() { - return sceneSignal; -} - -//------------------------- -//frame phases -//------------------------- - -void BaseScene::FrameStart() { - //EMPTY -} - -void BaseScene::Update() { - //EMPTY -} - -void BaseScene::FrameEnd() { - //EMPTY -} - -//------------------------- -//input events -//------------------------- - -void BaseScene::QuitEvent() { - sceneSignal = SceneSignal::QUIT; -} - -void BaseScene::MouseMotion(SDL_MouseMotionEvent const& event) { - //EMPTY -} - -void BaseScene::MouseButtonDown(SDL_MouseButtonEvent const& event) { - //EMPTY -} - -void BaseScene::MouseButtonUp(SDL_MouseButtonEvent const& event) { - //EMPTY -} - -void BaseScene::MouseWheel(SDL_MouseWheelEvent const& event) { - //EMPTY -} - -void BaseScene::KeyDown(SDL_KeyboardEvent const& event) { - //preference as a default - switch(event.keysym.sym) { - case SDLK_ESCAPE: - QuitEvent(); - break; - } -} - -void BaseScene::KeyUp(SDL_KeyboardEvent const& event) { - //EMPTY -} diff --git a/common/frameworks/scenes/base_scene.hpp b/common/frameworks/scenes/base_scene.hpp deleted file mode 100644 index 3c9c287..0000000 --- a/common/frameworks/scenes/base_scene.hpp +++ /dev/null @@ -1,61 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 "scene_signal.hpp" - -#include "SDL2/SDL.h" - -class BaseScene { -public: - BaseScene(); - virtual ~BaseScene(); - - virtual void RenderFrame(SDL_Renderer*); - static void SetRenderer(SDL_Renderer*); - SceneSignal GetSceneSignal(); - - //frame phases - virtual void FrameStart(); - virtual void Update(); - virtual void FrameEnd(); - - //input events - virtual void QuitEvent(); - virtual void MouseMotion(SDL_MouseMotionEvent const& event); - virtual void MouseButtonDown(SDL_MouseButtonEvent const& event); - virtual void MouseButtonUp(SDL_MouseButtonEvent const& event); - virtual void MouseWheel(SDL_MouseWheelEvent const& event); - virtual void KeyDown(SDL_KeyboardEvent const& event); - virtual void KeyUp(SDL_KeyboardEvent const& event); - - //TODO: joystick and controller events - -protected: - //control - static SDL_Renderer* GetRenderer(); - void SetSceneSignal(SceneSignal); - -private: - static SDL_Renderer* rendererHandle; - SceneSignal sceneSignal = SceneSignal::CONTINUE; -}; \ No newline at end of file diff --git a/common/frameworks/scenes/example_scene.cpp b/common/frameworks/scenes/example_scene.cpp deleted file mode 100644 index 5a5497e..0000000 --- a/common/frameworks/scenes/example_scene.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 "example_scene.hpp" - -ExampleScene::ExampleScene() { - // -} - -ExampleScene::~ExampleScene() { - // -} - -//------------------------- -//frame phases -//------------------------- - -void ExampleScene::FrameStart() { - // -} - -void ExampleScene::Update() { - // -} - -void ExampleScene::FrameEnd() { - // -} - -void ExampleScene::RenderFrame(SDL_Renderer* renderer) { - // -} - -//------------------------- -//input events -//------------------------- - -void ExampleScene::MouseMotion(SDL_MouseMotionEvent const& event) { - // -} - -void ExampleScene::MouseButtonDown(SDL_MouseButtonEvent const& event) { - // -} - -void ExampleScene::MouseButtonUp(SDL_MouseButtonEvent const& event) { - // -} - -void ExampleScene::MouseWheel(SDL_MouseWheelEvent const& event) { - // -} - -void ExampleScene::KeyDown(SDL_KeyboardEvent const& event) { - //preference as a default - switch(event.keysym.sym) { - case SDLK_ESCAPE: - QuitEvent(); - break; - } -} - -void ExampleScene::KeyUp(SDL_KeyboardEvent const& event) { - // -} diff --git a/common/frameworks/scenes/example_scene.hpp b/common/frameworks/scenes/example_scene.hpp deleted file mode 100644 index b644dd6..0000000 --- a/common/frameworks/scenes/example_scene.hpp +++ /dev/null @@ -1,46 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 "base_scene.hpp" - -class ExampleScene : public BaseScene { -public: - ExampleScene(); - ~ExampleScene(); - - void RenderFrame(SDL_Renderer* renderer) override; - -private: - //frame phases - void FrameStart() override; - void Update() override; - void FrameEnd() override; - - //input events - void MouseMotion(SDL_MouseMotionEvent const& event) override; - void MouseButtonDown(SDL_MouseButtonEvent const& event) override; - void MouseButtonUp(SDL_MouseButtonEvent const& event) override; - void MouseWheel(SDL_MouseWheelEvent const& event) override; - void KeyDown(SDL_KeyboardEvent const& event) override; - void KeyUp(SDL_KeyboardEvent const& event) override; -}; diff --git a/common/frameworks/scenes/main.cpp b/common/frameworks/scenes/main.cpp deleted file mode 100644 index 31a20d7..0000000 --- a/common/frameworks/scenes/main.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 "application.hpp" - -#include "SDL2/SDL.h" - -#include -#include - -int main(int argc, char** argv) { - std::cout << "Beginning " << argv[0] << std::endl; - try { - Application app; - app.Init(argc, argv); - app.Proc(); - app.Quit(); - } - catch(std::exception& e) { - std::cerr << "Fatal Error: " << e.what() << std::endl; - return 1; - } - std::cout << "Clean exit from " << argv[0] << std::endl; - return 0; -} \ No newline at end of file diff --git a/common/frameworks/scenes/makefile b/common/frameworks/scenes/makefile deleted file mode 100644 index a8f0ea0..0000000 --- a/common/frameworks/scenes/makefile +++ /dev/null @@ -1,56 +0,0 @@ -#include directories -INCLUDES+=. - -#libraries -#the order of the $(LIBS) is important, at least for MinGW -LIBS+= -ifeq ($(OS),Windows_NT) - LIBS+=-lmingw32 -endif -LIBS+=-lSDL2main -lSDL2 - -#flags -CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) -ifeq ($(shell uname), Linux) - #read data about the current install - CXXFLAGS+=$(shell sdl-config --cflags --static-libs) -endif - -#source -CXXSRC=$(wildcard *.cpp) - -#objects -OBJDIR=obj -OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) - -#output -OUTDIR=out -OUT=$(addprefix $(OUTDIR)/,scenes) - -#targets -all: $(OBJ) $(OUT) - $(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS) - -$(OBJ): | $(OBJDIR) - -$(OUT): | $(OUTDIR) - -$(OBJDIR): - mkdir $(OBJDIR) - -$(OUTDIR): - mkdir $(OUTDIR) - -$(OBJDIR)/%.o: %.cpp - $(CXX) $(CXXFLAGS) -c -o $@ $< - -clean: -ifeq ($(OS),Windows_NT) - $(RM) *.o *.a *.exe -else ifeq ($(shell uname), Linux) - find . -type f -name '*.o' -exec rm -f -r -v {} \; - find . -type f -name '*.a' -exec rm -f -r -v {} \; - rm -f -v $(OUT) -endif - -rebuild: clean all diff --git a/common/frameworks/scenes/scene_signal.hpp b/common/frameworks/scenes/scene_signal.hpp deleted file mode 100644 index 1322611..0000000 --- a/common/frameworks/scenes/scene_signal.hpp +++ /dev/null @@ -1,32 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 - -enum SceneSignal { - //reserved members for internal use - QUIT = -1, - CONTINUE = 0, - FIRST = 1, - - //custom scenes - EXAMPLE_SCENE -}; \ No newline at end of file diff --git a/common/graphics/button.cpp b/common/graphics/button.cpp deleted file mode 100644 index d58fed8..0000000 --- a/common/graphics/button.cpp +++ /dev/null @@ -1,167 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 "render_text_texture.hpp" - -#include - -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, SDL_Color color, std::string s) { - //convert to texture - SDL_Texture* text = renderTextTexture(renderer, font, color, s); - - //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); -} - -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; -} - -int Button::SetX(int i) { - return posX = i; -} - -int Button::SetY(int i) { - return posY = i; -} - -int Button::GetX() const { - return posX; -} - -int Button::GetY() const { - return posY; -} - -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() - ); -} \ No newline at end of file diff --git a/common/graphics/button.hpp b/common/graphics/button.hpp deleted file mode 100644 index 2d9da37..0000000 --- a/common/graphics/button.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 - -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*, SDL_Color, std::string); - - //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(); - - //accessors & mutators - int SetX(int x); - int SetY(int y); - int GetX() const; - int GetY() const; - -protected: - bool CheckBounds(int x, int y); - - Image image; - State state = State::IDLE; - int posX = 0, posY = 0; -}; diff --git a/common/graphics/image.cpp b/common/graphics/image.cpp deleted file mode 100644 index 7eadeaf..0000000 --- a/common/graphics/image.cpp +++ /dev/null @@ -1,211 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 -#include - -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 set 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; -} \ No newline at end of file diff --git a/common/graphics/image.hpp b/common/graphics/image.hpp deleted file mode 100644 index b4e62f8..0000000 --- a/common/graphics/image.hpp +++ /dev/null @@ -1,73 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 - -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; -}; \ No newline at end of file diff --git a/common/graphics/render_text_texture.cpp b/common/graphics/render_text_texture.cpp deleted file mode 100644 index bd02f48..0000000 --- a/common/graphics/render_text_texture.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 "render_text_texture.hpp" - -#include - -SDL_Texture* renderTextTexture(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string str) { - //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; -} \ No newline at end of file diff --git a/common/graphics/render_text_texture.hpp b/common/graphics/render_text_texture.hpp deleted file mode 100644 index 1aca1ed..0000000 --- a/common/graphics/render_text_texture.hpp +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 - -SDL_Texture* renderTextTexture(SDL_Renderer*, TTF_Font*, SDL_Color color, std::string); \ No newline at end of file diff --git a/common/graphics/text_box.cpp b/common/graphics/text_box.cpp deleted file mode 100644 index f597d73..0000000 --- a/common/graphics/text_box.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 - -TextBox::TextBox() { - // -} - -TextBox::~TextBox() { - // -} - -void TextBox::DrawTo(SDL_Renderer* renderer) { - int renderY = posY; - for (std::list::iterator it = lineList.begin(); it != lineList.end(); it++) { - it->SetX(posX); - it->SetY(renderY); - it->DrawTo(renderer); - renderY += it->GetPointHeight(); - } -} - -void TextBox::PushLine(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string str) { - lineList.emplace_back(renderer, font, color, str, 0, 0); -} - -void TextBox::PopLine(int num) { - //prevent underflow - num < lineList.size() ? num : lineList.size(); - - for (int i = 0; i < num; ++i) { - lineList.pop_front(); - } -} - -void TextBox::ClearLines() { - lineList.clear(); -} - -int TextBox::SetX(int i) { - return posX = i; -} - -int TextBox::SetY(int i) { - return posY = i; -} - -int TextBox::GetX() const { - return posX; -} - -int TextBox::GetY() const { - return posY; -} diff --git a/common/graphics/text_box.hpp b/common/graphics/text_box.hpp deleted file mode 100644 index 0e96bc8..0000000 --- a/common/graphics/text_box.hpp +++ /dev/null @@ -1,51 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 -#include - -class TextBox { -public: - TextBox(); - ~TextBox(); - - void DrawTo(SDL_Renderer*); - - void PushLine(SDL_Renderer*, TTF_Font*, SDL_Color color, std::string); - void PopLine(int num = 1); - void ClearLines(); - - int SetX(int i); - int SetY(int i); - int GetX() const; - int GetY() const; - -private: - std::list lineList; - int posX = 0, posY = 0; -}; \ No newline at end of file diff --git a/common/graphics/text_field.cpp b/common/graphics/text_field.cpp deleted file mode 100644 index c9056be..0000000 --- a/common/graphics/text_field.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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_field.hpp" - -#include "render_text_texture.hpp" - -TextField::TextField() { - // -} - -TextField::~TextField() { - SDL_DestroyTexture(texture); -} - -void TextField::DrawTo(SDL_Renderer* renderer) { - if (!texture) { - return; - } - SDL_Rect dclip = {posX, posY, 0, 0}; - SDL_QueryTexture(texture, nullptr, nullptr, &dclip.w, &dclip.h); - SDL_RenderCopy(renderer, texture, nullptr, &dclip); -} - -std::string TextField::PushText(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string s) { - text += s; - return SetText(renderer, font, color, text); -} - -std::string TextField::SetText(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string s) { - text = s; - SDL_DestroyTexture(texture); - if (text.size()) { - texture = renderTextTexture(renderer, font, color, text); - } - else { - texture = nullptr; - } - return text; -} - -std::string TextField::PopChars(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, int i) { - if (text.size() > 0) { - text.erase(text.size() - i); - } - return SetText(renderer, font, color, text); -} - -std::string TextField::GetText() { - return text; -} - -bool TextField::MouseButtonDown(SDL_MouseButtonEvent const& event) { - BoundingBox cursorBox = {event.x, event.y, 0, 0}; - BoundingBox fieldBox = bounds; - fieldBox.x += posX; - fieldBox.y += posY; - return focus = fieldBox.CheckOverlap(cursorBox); -} - -BoundingBox TextField::SetBounds(BoundingBox b) { - return bounds = b; -} - -BoundingBox TextField::GetBounds() { - return bounds; -} - -bool TextField::SetFocus(bool b) { - return focus = b; -} - -bool TextField::GetFocus() { - return focus; -} - -int TextField::SetX(int i) { - return posX = i; -} - -int TextField::SetY(int i) { - return posY = i; -} - -int TextField::GetX() const { - return posX; -} - -int TextField::GetY() const { - return posY; -} diff --git a/common/graphics/text_field.hpp b/common/graphics/text_field.hpp deleted file mode 100644 index 5bdbc21..0000000 --- a/common/graphics/text_field.hpp +++ /dev/null @@ -1,65 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 "bounding_box.hpp" - -#include "SDL2/SDL.h" -#include "SDL2/SDL_ttf.h" - -#include - -class TextField { -public: - TextField(); - ~TextField(); - - void DrawTo(SDL_Renderer*); - - //input - std::string PushText(SDL_Renderer*, TTF_Font*, SDL_Color color, std::string); - std::string SetText(SDL_Renderer*, TTF_Font*, SDL_Color color, std::string); - std::string PopChars(SDL_Renderer*, TTF_Font*, SDL_Color color, int i); - - std::string GetText(); - - bool MouseButtonDown(SDL_MouseButtonEvent const& event); - - BoundingBox SetBounds(BoundingBox b); - BoundingBox GetBounds(); - - bool SetFocus(bool b); - bool GetFocus(); - - //accessors & mutators - int SetX(int i); - int SetY(int i); - int GetX() const; - int GetY() const; - -private: - SDL_Texture* texture = nullptr; - std::string text; - BoundingBox bounds; - bool focus = false; - int posX = 0, posY = 0; -}; \ No newline at end of file diff --git a/common/graphics/text_line.cpp b/common/graphics/text_line.cpp deleted file mode 100644 index 865f07e..0000000 --- a/common/graphics/text_line.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 "render_text_texture.hpp" - -#include - -TextLine::TextLine() { - // -} - -TextLine::TextLine(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string str, int x, int y) { - SetText(renderer, font, color, str); - posX = x; - posY = y; -} - -TextLine::~TextLine() { - ClearText(); -} - -void TextLine::DrawTo(SDL_Renderer* renderer) { - 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, SDL_Color color, std::string str) { - //just use the above global function - SDL_DestroyTexture(texture); - texture = renderTextTexture(renderer, font, color, str); - pointHeight = TTF_FontHeight(font); -} - -void TextLine::ClearText() { - SDL_DestroyTexture(texture); - pointHeight = 0; -} - -int TextLine::SetX(int i) { - return posX = i; -} - -int TextLine::SetY(int i) { - return posY = i; -} - -int TextLine::GetX() const { - return posX; -} - -int TextLine::GetY() const { - return posY; -} - -int TextLine::GetPointHeight() { - return pointHeight; -} \ No newline at end of file diff --git a/common/graphics/text_line.hpp b/common/graphics/text_line.hpp deleted file mode 100644 index ffa6661..0000000 --- a/common/graphics/text_line.hpp +++ /dev/null @@ -1,53 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 - -class TextLine { -public: - TextLine(); - TextLine(SDL_Renderer*, TTF_Font*, SDL_Color, std::string, int x, int y); - virtual ~TextLine(); - - void DrawTo(SDL_Renderer*); - - void SetText(SDL_Renderer*, TTF_Font*, SDL_Color, std::string); - void ClearText(); - - //accessors & mutators - int SetX(int i); - int SetY(int i); - int GetX() const; - int GetY() const; - - //utility - int GetPointHeight(); - -protected: - SDL_Texture* texture = nullptr; - int posX = 0, posY = 0; - int pointHeight = 0; //internal use for TextBox -}; \ No newline at end of file diff --git a/common/map/makefile b/common/map/makefile deleted file mode 100644 index 7e6b897..0000000 --- a/common/map/makefile +++ /dev/null @@ -1,28 +0,0 @@ -#config -INCLUDES+=. ../graphics -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 $@ $< diff --git a/common/map/region.cpp b/common/map/region.cpp deleted file mode 100644 index 0a9631e..0000000 --- a/common/map/region.cpp +++ /dev/null @@ -1,82 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 -#include -#include - -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) const { - 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) const { - 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::GetSolidBitset() { - return &solid; -} \ No newline at end of file diff --git a/common/map/region.hpp b/common/map/region.hpp deleted file mode 100644 index a454a24..0000000 --- a/common/map/region.hpp +++ /dev/null @@ -1,62 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 - -//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) const; - - bool SetSolid(int x, int y, bool b); - bool GetSolid(int x, int y) const; - - //accessors - int GetX() const; - int GetY() const; - - std::bitset* GetSolidBitset(); -private: - friend class TileSheet; - - const int x; - const int y; - - type_t tiles[REGION_WIDTH][REGION_HEIGHT][REGION_DEPTH]; - std::bitset solid; -}; diff --git a/common/map/region_api.cpp b/common/map/region_api.cpp deleted file mode 100644 index 4fa49c9..0000000 --- a/common/map/region_api.cpp +++ /dev/null @@ -1,99 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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(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(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(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(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(lua_touserdata(L, 1)); - lua_pushinteger(L, region->GetX()); - return 1; -} - -static int getY(lua_State* L) { - Region* region = reinterpret_cast(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; -} \ No newline at end of file diff --git a/common/map/region_api.hpp b/common/map/region_api.hpp deleted file mode 100644 index 7e16202..0000000 --- a/common/map/region_api.hpp +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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); diff --git a/common/map/region_pager_api.cpp b/common/map/region_pager_api.cpp deleted file mode 100644 index 1500577..0000000 --- a/common/map/region_pager_api.cpp +++ /dev/null @@ -1,172 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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(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(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(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(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(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(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(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(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(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(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(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(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(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(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; -} \ No newline at end of file diff --git a/common/map/region_pager_api.hpp b/common/map/region_pager_api.hpp deleted file mode 100644 index 2374b6a..0000000 --- a/common/map/region_pager_api.hpp +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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); diff --git a/common/map/region_pager_base.cpp b/common/map/region_pager_base.cpp deleted file mode 100644 index 5446c66..0000000 --- a/common/map/region_pager_base.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 -#include - -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); -} - -//Bug Origin? -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::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) { - 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 fn) { - regionList.remove_if(fn); -} - -void RegionPagerBase::UnloadAll() { - regionList.clear(); -} - -std::list* RegionPagerBase::GetContainer() { - return ®ionList; -} \ No newline at end of file diff --git a/common/map/region_pager_base.hpp b/common/map/region_pager_base.hpp deleted file mode 100644 index ef43e52..0000000 --- a/common/map/region_pager_base.hpp +++ /dev/null @@ -1,58 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 -#include - -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 fn); - virtual void UnloadAll(); - - //accessors & mutators - std::list* GetContainer(); -protected: - std::list regionList; -}; diff --git a/common/map/region_pager_lua.cpp b/common/map/region_pager_lua.cpp deleted file mode 100644 index 889781e..0000000 --- a/common/map/region_pager_lua.cpp +++ /dev/null @@ -1,208 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 - -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 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(®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(); -} \ No newline at end of file diff --git a/common/map/region_pager_lua.hpp b/common/map/region_pager_lua.hpp deleted file mode 100644 index 4dc18b2..0000000 --- a/common/map/region_pager_lua.hpp +++ /dev/null @@ -1,68 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 -#include - -//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 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; -}; diff --git a/common/map/tile_sheet.cpp b/common/map/tile_sheet.cpp deleted file mode 100644 index 40e9114..0000000 --- a/common/map/tile_sheet.cpp +++ /dev/null @@ -1,124 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 - -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); - } - } - } -} diff --git a/common/map/tile_sheet.hpp b/common/map/tile_sheet.hpp deleted file mode 100644 index 145f7a7..0000000 --- a/common/map/tile_sheet.hpp +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013-2016 - * - * 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 - -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; -};