From 4ced681c76717675610e5edec2dbc75c2b05371e Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 8 Jul 2015 03:09:50 +1000 Subject: [PATCH] Working on the ui, incomplete --- common/makefile | 2 +- common/ui/button.hpp | 75 +++++++++++---------------------------- common/ui/menu_bar.hpp | 1 - common/ui/raster_font.cpp | 60 ------------------------------- common/ui/raster_font.hpp | 51 -------------------------- makefile | 6 ++-- server/makefile | 2 +- 7 files changed, 26 insertions(+), 171 deletions(-) delete mode 100644 common/ui/raster_font.cpp delete mode 100644 common/ui/raster_font.hpp diff --git a/common/makefile b/common/makefile index 59bcb4f..e2d1029 100644 --- a/common/makefile +++ b/common/makefile @@ -4,5 +4,5 @@ all: $(MAKE) -C graphics $(MAKE) -C map $(MAKE) -C network -# $(MAKE) -C ui #TODO: reenable this + $(MAKE) -C ui $(MAKE) -C utilities diff --git a/common/ui/button.hpp b/common/ui/button.hpp index 4b93520..16b6cac 100644 --- a/common/ui/button.hpp +++ b/common/ui/button.hpp @@ -21,71 +21,38 @@ */ #pragma once +#include "bounding_box.hpp" #include "image.hpp" -#include "raster_font.hpp" + +#include "SDL2/SDL_ttf.h" #include -/* 3-phases, no toggle, centred text - * This class uses the size of the provided image as its bounds. Also, - * The provided image should be formatted correctly. - * - * The button's image should be divided into 3 sections virtucally, - * which act as the different button images. The clip width & height of the - * Image should be set manually, and the height should be 1/3 of the total - * graphical data. -*/ class Button { public: - enum State { - NORMAL = 0, HOVER = 1, PRESSED = 2 + //states available + enum class State { + NORMAL, HOVER, PRESSED }; + //methods Button() = default; ~Button() = default; - //handle input - State MouseMotion(SDL_MouseMotionEvent const&); - State MouseButtonDown(SDL_MouseButtonEvent const&); - State MouseButtonUp(SDL_MouseButtonEvent const&); + void RenderText(std::string s); + bool CaptureInput(int x, int y, bool pressed); + void DrawTo(SDL_Renderer*, int camX, int camY); - //yet another draw function - void DrawTo(SDL_Surface* const); + //accessors & mutators + int SetX(int x) { return posX = x; } + int SetY(int y) { return posY = y; } + int GetX() { return posX; } + int GetY() { return posY; } + Image* GetImage() { return ℑ } + BoundingBox* GetBoundingBox() { return &boundingBox; } - //accessors and mutators - Image* SetImage(Image* const ptr) { return image = ptr; } - Image* GetImage() { return image; } - RasterFont* SetFont(RasterFont* const ptr) { return font = ptr; } - RasterFont* GetFont() { return font; } - - Sint16 SetX(Sint16 i) { return x = i; } - Sint16 SetY(Sint16 i) { return y = i; } - Sint16 GetX() const { return x; } - Sint16 GetY() const { return y; } - - Sint16 SetTextX(Sint16 i) { return textX = i; } - Sint16 SetTextY(Sint16 i) { return textY = i; } - Sint16 GetTextX() const { return textX; } - Sint16 GetTextY() const { return textY; } - - State SetState(State s) { return state = s; } - State GetState() const { return state; } - - std::string SetText(std::string); - std::string GetText() const { return text; } - -private: - State CalcState(Sint16 x, Sint16 y, bool leftPressed); - - //point to the provided external objects - Image* image = nullptr; - RasterFont* font = nullptr; - - //positions - Sint16 x = 0, y = 0; - Sint16 textX = 0, textY = 0; - - // - State state = State::NORMAL; - std::string text; +protected: + Image image; + BoundingBox boundingBox; + int posX = 0, posY = 0; }; diff --git a/common/ui/menu_bar.hpp b/common/ui/menu_bar.hpp index 351259b..fa01f9c 100644 --- a/common/ui/menu_bar.hpp +++ b/common/ui/menu_bar.hpp @@ -22,7 +22,6 @@ #pragma once #include "image.hpp" -#include "raster_font.hpp" #include "button.hpp" #include diff --git a/common/ui/raster_font.cpp b/common/ui/raster_font.cpp deleted file mode 100644 index 8b48fd2..0000000 --- a/common/ui/raster_font.cpp +++ /dev/null @@ -1,60 +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 "raster_font.hpp" - -#include - -/* It might be more efficient to render to a different surface (like an Image) - * rather than calling this function with all of it's '%' and '/'. -*/ - -void RasterFont::DrawStringTo(std::string s, SDL_Surface* const dest, Sint16 x, Sint16 y) { - if (!image.GetSurface()) { - throw(std::runtime_error("RasterFont not loaded")); - } - const Uint16 w = image.GetClipW(); - const Uint16 h = image.GetClipH(); - for (int i = 0; i < s.size(); i++) { - image.SetClipX(s[i] % 16 * w); - image.SetClipY(s[i] / 16 * h); - image.DrawTo(dest, x + i * w, y); - } -} - -/* Note: This class can only take a raster font with 16*16 characters, and the - * indevidual characters must have the same dimensions. Overall this class is too - * restrictive; I suggest using a 3rd party library. -*/ - -SDL_Surface* RasterFont::LoadSurface(std::string fname) { - image.LoadSurface(fname); - image.SetClipW(image.GetSurface()->w/16); - image.SetClipH(image.GetSurface()->h/16); - return image.GetSurface(); -} - -SDL_Surface* RasterFont::SetSurface(SDL_Surface* p) { - image.SetSurface(p); - image.SetClipW(image.GetSurface()->w/16); - image.SetClipH(image.GetSurface()->h/16); - return image.GetSurface(); -} diff --git a/common/ui/raster_font.hpp b/common/ui/raster_font.hpp deleted file mode 100644 index 9000921..0000000 --- a/common/ui/raster_font.hpp +++ /dev/null @@ -1,51 +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" - -class RasterFont { -public: - RasterFont() = default; - RasterFont(RasterFont const& rhs) { *this = rhs; } - RasterFont(RasterFont&& rhs) { *this = std::move(rhs); } - RasterFont(std::string fname) { LoadSurface(fname); } - RasterFont(SDL_Surface* p) { SetSurface(p); } - ~RasterFont() = default; - - RasterFont& operator=(RasterFont const& rhs) { image = rhs.image; } - RasterFont& operator=(RasterFont&& rhs) { image = std::move(rhs.image); } - - void DrawStringTo(std::string, SDL_Surface* const, Sint16 x, Sint16 y); - - //Accessors and Mutators - SDL_Surface* LoadSurface(std::string); - SDL_Surface* SetSurface(SDL_Surface*); - SDL_Surface* GetSurface() const { return image.GetSurface(); } - void FreeSurface() { image.FreeSurface(); } - - Uint16 GetCharW() const { return image.GetClipW(); } - Uint16 GetCharH() const { return image.GetClipH(); } - -private: - Image image; -}; diff --git a/makefile b/makefile index 600f1a7..a9c9c5b 100644 --- a/makefile +++ b/makefile @@ -33,9 +33,9 @@ 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/client out/server + 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/server/makefile b/server/makefile index 2f8de96..fd5b942 100644 --- a/server/makefile +++ b/server/makefile @@ -9,7 +9,7 @@ ifeq ($(OS),Windows_NT) endif LIBS+=-lSDLmain -lSDL -llua -lsqlite3 ifeq ($(shell uname), Linux) - #I don't know what this does, but Ubuntu needs it + #I don't know what this does, but Ubuntu needs it (dynamic linking for lua) LIBS+=-ldl endif