Button is being drawn, if a little jenky; needs SDL2_ttf

I tweaked the Image::Create() implementation, to act as a render target.
This commit is contained in:
2015-07-28 22:33:59 +10:00
parent 4cb9e05841
commit 19227882b9
8 changed files with 197 additions and 2 deletions
+8
View File
@@ -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);
+1
View File
@@ -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;
+106
View File
@@ -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 <stdexcept>
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;
}
+61
View File
@@ -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 <string>
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;
};
+1 -1
View File
@@ -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) {
+14
View File
@@ -23,6 +23,7 @@
#include <iomanip>
#include <iostream>
#include <sstream>
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);
}
//-------------------------
+5
View File
@@ -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;
};
+1 -1
View File
@@ -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