From 19227882b99ea652140994e31438c51e9e8c94d1 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 28 Jul 2015 22:33:59 +1000 Subject: [PATCH] Button is being drawn, if a little jenky; needs SDL2_ttf I tweaked the Image::Create() implementation, to act as a render target. --- src/application.cpp | 8 ++++ src/application.hpp | 1 + src/common/button.cpp | 106 ++++++++++++++++++++++++++++++++++++++++++ src/common/button.hpp | 61 ++++++++++++++++++++++++ src/common/image.cpp | 2 +- src/example_scene.cpp | 14 ++++++ src/example_scene.hpp | 5 ++ src/makefile | 2 +- 8 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 src/common/button.cpp create mode 100644 src/common/button.hpp diff --git a/src/application.cpp b/src/application.cpp index ab01deb..bc6b43d 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -57,6 +57,13 @@ void Application::Init(int argc, char* argv[]) { //set the hook for the renderer BaseScene::SetRenderer(renderer); + //setting up SDL2_ttf + if (TTF_Init()) { + std::ostringstream msg; + msg << "Failed to initialize SDL2_ttf: " << SDL_GetError(); + throw(std::runtime_error(msg.str())); + } + //setup lua lua = luaL_newstate(); if (!lua) { @@ -118,6 +125,7 @@ void Application::Proc() { void Application::Quit() { lua_close(lua); + TTF_Quit(); //clean up after the program BaseScene::SetRenderer(nullptr); diff --git a/src/application.hpp b/src/application.hpp index 73fa599..71d3017 100644 --- a/src/application.hpp +++ b/src/application.hpp @@ -26,6 +26,7 @@ #include "lua.hpp" #include "SDL2/SDL.h" +#include "SDL2/SDL_ttf.h" //TODO: do something with these constexpr int screenWidth = 800; diff --git a/src/common/button.cpp b/src/common/button.cpp new file mode 100644 index 0000000..22421f5 --- /dev/null +++ b/src/common/button.cpp @@ -0,0 +1,106 @@ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "button.hpp" + +#include + +void Button::DrawTo(SDL_Renderer* renderer) { + image.DrawTo(renderer, 0, 0); +} + +void Button::SetBackgroundTexture(SDL_Renderer* renderer, SDL_Texture* texture) { + //copy the given texture + image.Free(); + + //a blank 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); +} + +void Button::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string s, SDL_Color color) { + //make the surface (from SDL_ttf) + SDL_Surface* surf = TTF_RenderText_Solid(font, s.c_str(), color); + if (!surf) { + throw(std::runtime_error("Failed to create a TTF surface")); + } + + //convert to texture + SDL_Texture* text = SDL_CreateTextureFromSurface(renderer, surf); + SDL_FreeSurface(surf); + if (!text) { + throw(std::runtime_error("Failed to create a TTF texture")); + } + int w, h; + SDL_QueryTexture(text, nullptr, nullptr, &w, &h); + + //draw the text to the background + SDL_Rect src = {0, 0, w, h}; + SDL_Rect dst = { + (image.GetClipW() - w) / 2, + (image.GetClipH() - h) / 2, + w, h}; + SDL_SetRenderTarget(renderer, image.GetTexture()); + SDL_RenderCopy(renderer, text, &src, &dst); + SDL_SetRenderTarget(renderer, nullptr); + + //free the texture + SDL_DestroyTexture(text); +} + +void Button::SetX(int x) { + posX = x; +} + +void Button::SetY(int y) { + posY = y; +} + +void Button::MouseMotion(SDL_MouseMotionEvent const&) { + //TODO: empty +} + +void Button::MouseButtonDown(SDL_MouseButtonEvent const&) { + //TODO: empty +} + +void Button::MouseButtonUp(SDL_MouseButtonEvent const&) { + //TODO: empty +} + +void Button::SetOnPress(fptr arg) { + onPress = arg; +} + +void Button::SetOnRelease(fptr arg) { + onRelease = arg; +} diff --git a/src/common/button.hpp b/src/common/button.hpp new file mode 100644 index 0000000..5cae2b1 --- /dev/null +++ b/src/common/button.hpp @@ -0,0 +1,61 @@ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#pragma once + +#include "image.hpp" + +#include "SDL2/SDL_ttf.h" + +#include + +class Button { +public: + //types + typedef int (*fptr)(); + + //methods + Button() = default; + ~Button() = default; + + void DrawTo(SDL_Renderer*); + + //setup + void SetBackgroundTexture(SDL_Renderer*, SDL_Texture*); + void SetText(SDL_Renderer*, TTF_Font*, std::string, SDL_Color); + void SetX(int x); + void SetY(int y); + + //capture input + void MouseMotion(SDL_MouseMotionEvent const&); + void MouseButtonDown(SDL_MouseButtonEvent const&); + void MouseButtonUp(SDL_MouseButtonEvent const&); + + //responses + void SetOnPress(fptr); + void SetOnRelease(fptr); + +protected: + Image image; + int posX = 0, posY = 0; + fptr onPress = nullptr; + fptr onRelease = nullptr; +}; diff --git a/src/common/image.cpp b/src/common/image.cpp index bd9e662..f04c2e4 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -101,7 +101,7 @@ SDL_Texture* Image::Create(SDL_Renderer* renderer, Uint16 w, Uint16 h) { //make the texture texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, - SDL_TEXTUREACCESS_STATIC, + SDL_TEXTUREACCESS_TARGET, w, h); if (!texture) { diff --git a/src/example_scene.cpp b/src/example_scene.cpp index e6f600c..9b67520 100644 --- a/src/example_scene.cpp +++ b/src/example_scene.cpp @@ -23,6 +23,7 @@ #include #include +#include ExampleScene::ExampleScene(lua_State* L) { lua = L; @@ -46,6 +47,16 @@ ExampleScene::ExampleScene(lua_State* L) { camera.x = -3000; camera.y = -1500; camera.scale = 0.25; + + //DEBUG: testing buttons + font = TTF_OpenFont("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 12); + if (!font) { + std::ostringstream msg; + msg << "Failed to load a font: " << SDL_GetError(); + throw(std::runtime_error(msg.str())); + } + button.SetBackgroundTexture(GetRenderer(), tileSheet.GetTexture()); + button.SetText(GetRenderer(), font, "BUTTON TEXT", {255, 255, 255, 255}); } ExampleScene::~ExampleScene() { @@ -73,6 +84,9 @@ void ExampleScene::RenderFrame(SDL_Renderer* renderer) { //NOTE: Graphical scaling is done internally tileSheet.DrawRegionTo(renderer, &(*it), camera.x, camera.y, camera.scale, camera.scale); } + + //DEBUG: testing UI + button.DrawTo(renderer); } //------------------------- diff --git a/src/example_scene.hpp b/src/example_scene.hpp index c9ed5c7..afffeaf 100644 --- a/src/example_scene.hpp +++ b/src/example_scene.hpp @@ -22,10 +22,12 @@ #pragma once #include "base_scene.hpp" +#include "button.hpp" #include "region_pager_lua.hpp" #include "tile_sheet.hpp" #include "lua.hpp" +#include "SDL2/SDL_ttf.h" class ExampleScene : public BaseScene { public: @@ -60,4 +62,7 @@ private: int selection = 1; int layer = 0; + + TTF_Font* font = nullptr; + Button button; }; diff --git a/src/makefile b/src/makefile index 102623a..5f3b65f 100644 --- a/src/makefile +++ b/src/makefile @@ -7,7 +7,7 @@ LIBS+=libcommon.a libmap.a ifeq ($(OS),Windows_NT) LIBS+=-lmingw32 endif -LIBS+=-lSDL2main -lSDL2 -lSDL2_image -llua +LIBS+=-lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf -llua ifeq ($(shell uname), Linux) #I don't know what this does, but Ubuntu needs it (dynamic linking for lua) LIBS+=-ldl