Updated GUI library
This commit is contained in:
@@ -28,6 +28,8 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
constexpr SDL_Color WHITE = {255, 255, 255, 255};
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Public access members
|
//Public access members
|
||||||
//-------------------------
|
//-------------------------
|
||||||
@@ -49,14 +51,16 @@ DisconnectedScreen::DisconnectedScreen() {
|
|||||||
|
|
||||||
//setup the button
|
//setup the button
|
||||||
backButton.SetBackgroundTexture(GetRenderer(), image.GetTexture());
|
backButton.SetBackgroundTexture(GetRenderer(), image.GetTexture());
|
||||||
backButton.SetText(GetRenderer(), font, "Back", COLOR_WHITE);
|
backButton.SetText(GetRenderer(), font, WHITE, "Back");
|
||||||
|
|
||||||
//set the button positions
|
//set the button positions
|
||||||
backButton.SetX(50);
|
backButton.SetX(50);
|
||||||
backButton.SetY(50);
|
backButton.SetY(50);
|
||||||
|
|
||||||
//set the disconnection message text
|
//set the disconnection message text
|
||||||
textLine.SetText(GetRenderer(), font, config["client.disconnectMessage"], {255, 255, 255, 255});
|
textLine.SetX(50);
|
||||||
|
textLine.SetY(30);
|
||||||
|
textLine.SetText(GetRenderer(), font, WHITE, config["client.disconnectMessage"]);
|
||||||
|
|
||||||
//full reset
|
//full reset
|
||||||
UDPNetworkUtility::GetSingleton().Unbind(Channels::SERVER);
|
UDPNetworkUtility::GetSingleton().Unbind(Channels::SERVER);
|
||||||
@@ -92,7 +96,7 @@ void DisconnectedScreen::FrameEnd() {
|
|||||||
|
|
||||||
void DisconnectedScreen::RenderFrame(SDL_Renderer* renderer) {
|
void DisconnectedScreen::RenderFrame(SDL_Renderer* renderer) {
|
||||||
backButton.DrawTo(renderer);
|
backButton.DrawTo(renderer);
|
||||||
textLine.DrawTo(renderer, 50, 30);
|
textLine.DrawTo(renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|||||||
@@ -27,6 +27,8 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
constexpr SDL_Color WHITE = {255, 255, 255, 255};
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Public access members
|
//Public access members
|
||||||
//-------------------------
|
//-------------------------
|
||||||
@@ -52,11 +54,11 @@ LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex):
|
|||||||
|
|
||||||
//setup the buttons
|
//setup the buttons
|
||||||
searchButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
searchButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
||||||
searchButton.SetText(GetRenderer(), font, "Search", COLOR_WHITE);
|
searchButton.SetText(GetRenderer(), font, WHITE, "Search");
|
||||||
joinButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
joinButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
||||||
joinButton.SetText(GetRenderer(), font, "Join", COLOR_WHITE);
|
joinButton.SetText(GetRenderer(), font, WHITE, "Join");
|
||||||
backButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
backButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
||||||
backButton.SetText(GetRenderer(), font, "Back", COLOR_WHITE);
|
backButton.SetText(GetRenderer(), font, WHITE, "Back");
|
||||||
|
|
||||||
//set the button positions (assumed)
|
//set the button positions (assumed)
|
||||||
searchButton.SetX(50);
|
searchButton.SetX(50);
|
||||||
@@ -67,6 +69,7 @@ LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex):
|
|||||||
backButton.SetY(90);
|
backButton.SetY(90);
|
||||||
|
|
||||||
//pseudo-list selection
|
//pseudo-list selection
|
||||||
|
//TODO: move this into the UI library?
|
||||||
boundingBox = {300, 50, 200, 12};
|
boundingBox = {300, 50, 200, 12};
|
||||||
|
|
||||||
//hacked together a highlight box
|
//hacked together a highlight box
|
||||||
@@ -121,8 +124,13 @@ void LobbyMenu::RenderFrame(SDL_Renderer* renderer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//draw the server's info
|
//draw the server's info
|
||||||
serverVector[i].nameImage.DrawTo(renderer, boundingBox.x, boundingBox.y + boundingBox.h * i);
|
serverVector[i].nameImage.SetX(boundingBox.x);
|
||||||
serverVector[i].playerCountImage.DrawTo(renderer, boundingBox.x+276, boundingBox.y + boundingBox.h * i);
|
serverVector[i].nameImage.SetY(boundingBox.y + boundingBox.h * i);
|
||||||
|
serverVector[i].nameImage.DrawTo(renderer);
|
||||||
|
|
||||||
|
serverVector[i].playerCountImage.SetX(boundingBox.x+276);
|
||||||
|
serverVector[i].playerCountImage.SetY(boundingBox.y + boundingBox.h * i);
|
||||||
|
serverVector[i].playerCountImage.DrawTo(renderer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,8 +256,8 @@ void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
//text graphics
|
//text graphics
|
||||||
serverVector.back().nameImage.SetText(GetRenderer(), font, newServer.name, color);
|
serverVector.back().nameImage.SetText(GetRenderer(), font, color, newServer.name);
|
||||||
serverVector.back().playerCountImage.SetText(GetRenderer(), font, itoa_base10(newServer.playerCount), color);
|
serverVector.back().playerCountImage.SetText(GetRenderer(), font, color, itoa_base10(newServer.playerCount));
|
||||||
}
|
}
|
||||||
|
|
||||||
void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) {
|
void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) {
|
||||||
|
|||||||
+15
-11
@@ -26,6 +26,8 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
constexpr SDL_Color WHITE = {255, 255, 255, 255};
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Public access members
|
//Public access members
|
||||||
//-------------------------
|
//-------------------------
|
||||||
@@ -46,11 +48,11 @@ MainMenu::MainMenu() {
|
|||||||
|
|
||||||
//setup the buttons
|
//setup the buttons
|
||||||
startButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
startButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
||||||
startButton.SetText(GetRenderer(), font, "Start", COLOR_WHITE);
|
startButton.SetText(GetRenderer(), font, WHITE, "Start");
|
||||||
optionsButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
optionsButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
||||||
optionsButton.SetText(GetRenderer(), font, "Options", COLOR_WHITE);
|
optionsButton.SetText(GetRenderer(), font, WHITE, "Options");
|
||||||
quitButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
quitButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
||||||
quitButton.SetText(GetRenderer(), font, "Quit", COLOR_WHITE);
|
quitButton.SetText(GetRenderer(), font, WHITE, "Quit");
|
||||||
|
|
||||||
//set the button positions
|
//set the button positions
|
||||||
startButton.SetX(50);
|
startButton.SetX(50);
|
||||||
@@ -61,9 +63,15 @@ MainMenu::MainMenu() {
|
|||||||
quitButton.SetY(50 + 20 * 2);
|
quitButton.SetY(50 + 20 * 2);
|
||||||
|
|
||||||
//text box
|
//text box
|
||||||
textBox.PushLine(GetRenderer(), font, "Thanks for playing!", {255, 255, 255, 255});
|
int h = -1;
|
||||||
textBox.PushLine(GetRenderer(), font, "You can get the latest version at: ", {255, 255, 255, 255});
|
SDL_RenderGetLogicalSize(GetRenderer(), nullptr, &h);
|
||||||
textBox.PushLine(GetRenderer(), font, "krgamestudios.com", {255, 255, 255, 255}); //TODO: (9) click to open the website/update
|
|
||||||
|
textBox.SetX(50);
|
||||||
|
textBox.SetY(h-100);
|
||||||
|
|
||||||
|
textBox.PushLine(GetRenderer(), font, WHITE, "Thanks for playing!");
|
||||||
|
textBox.PushLine(GetRenderer(), font, WHITE, "You can get the latest version at: ");
|
||||||
|
textBox.PushLine(GetRenderer(), font, WHITE, "krgamestudios.com"); //TODO: (9) click to open the website/update
|
||||||
|
|
||||||
//debug
|
//debug
|
||||||
//
|
//
|
||||||
@@ -93,11 +101,7 @@ void MainMenu::RenderFrame(SDL_Renderer* renderer) {
|
|||||||
startButton.DrawTo(renderer);
|
startButton.DrawTo(renderer);
|
||||||
optionsButton.DrawTo(renderer);
|
optionsButton.DrawTo(renderer);
|
||||||
quitButton.DrawTo(renderer);
|
quitButton.DrawTo(renderer);
|
||||||
|
textBox.DrawTo(renderer);
|
||||||
int h = -1;
|
|
||||||
SDL_RenderGetLogicalSize(GetRenderer(), nullptr, &h);
|
|
||||||
|
|
||||||
textBox.DrawTo(renderer, 50, h-50, -12);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|||||||
@@ -26,6 +26,8 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
constexpr SDL_Color WHITE = {255, 255, 255, 255};
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Public access members
|
//Public access members
|
||||||
//-------------------------
|
//-------------------------
|
||||||
@@ -46,14 +48,16 @@ OptionsMenu::OptionsMenu() {
|
|||||||
|
|
||||||
//setup the button
|
//setup the button
|
||||||
backButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
backButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
||||||
backButton.SetText(GetRenderer(), font, "Back", COLOR_WHITE);
|
backButton.SetText(GetRenderer(), font, WHITE, "Back");
|
||||||
|
|
||||||
//set the button positions
|
//set the button positions
|
||||||
backButton.SetX(50);
|
backButton.SetX(50);
|
||||||
backButton.SetY(50);
|
backButton.SetY(50);
|
||||||
|
|
||||||
//text line
|
//text line
|
||||||
textLine.SetText(GetRenderer(), font, "This code is fucking hard to refactor.", {255, 255, 255, 255});
|
textLine.SetX(50);
|
||||||
|
textLine.SetY(30);
|
||||||
|
textLine.SetText(GetRenderer(), font, WHITE, "Am I making any progress?");
|
||||||
}
|
}
|
||||||
|
|
||||||
OptionsMenu::~OptionsMenu() {
|
OptionsMenu::~OptionsMenu() {
|
||||||
@@ -78,7 +82,7 @@ void OptionsMenu::FrameEnd() {
|
|||||||
|
|
||||||
void OptionsMenu::RenderFrame(SDL_Renderer* renderer) {
|
void OptionsMenu::RenderFrame(SDL_Renderer* renderer) {
|
||||||
backButton.DrawTo(renderer);
|
backButton.DrawTo(renderer);
|
||||||
textLine.DrawTo(renderer, 50, 30);
|
textLine.DrawTo(renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|||||||
@@ -34,6 +34,8 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
constexpr SDL_Color WHITE = {255, 255, 255, 255};
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//static functions
|
//static functions
|
||||||
//-------------------------
|
//-------------------------
|
||||||
@@ -72,9 +74,9 @@ World::World(int* const argClientIndex, int* const argAccountIndex):
|
|||||||
|
|
||||||
//setup the buttons
|
//setup the buttons
|
||||||
disconnectButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
disconnectButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
||||||
disconnectButton.SetText(GetRenderer(), font, "Disconnect", COLOR_WHITE);
|
disconnectButton.SetText(GetRenderer(), font, WHITE, "Disconnect");
|
||||||
shutdownButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
shutdownButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
||||||
shutdownButton.SetText(GetRenderer(), font, "Shutdown", COLOR_WHITE);
|
shutdownButton.SetText(GetRenderer(), font, WHITE, "Shutdown");
|
||||||
|
|
||||||
//set the button positions
|
//set the button positions
|
||||||
disconnectButton.SetX(50);
|
disconnectButton.SetX(50);
|
||||||
@@ -261,12 +263,12 @@ void World::RenderFrame(SDL_Renderer* renderer) {
|
|||||||
shutdownButton.DrawTo(renderer);
|
shutdownButton.DrawTo(renderer);
|
||||||
|
|
||||||
//FPS
|
//FPS
|
||||||
fpsTextLine.DrawTo(renderer, 0, 0);
|
fpsTextLine.DrawTo(renderer);
|
||||||
int fpsRet = fps.Calculate();
|
int fpsRet = fps.Calculate();
|
||||||
if (fpsRet != -1) {
|
if (fpsRet != -1) {
|
||||||
std::ostringstream msg;
|
std::ostringstream msg;
|
||||||
msg << "FPS: " << fpsRet;
|
msg << "FPS: " << fpsRet;
|
||||||
fpsTextLine.SetText(renderer, font, msg.str(), {255, 255, 255, 255});
|
fpsTextLine.SetText(renderer, font, WHITE, msg.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+20
-20
@@ -21,6 +21,8 @@
|
|||||||
*/
|
*/
|
||||||
#include "button.hpp"
|
#include "button.hpp"
|
||||||
|
|
||||||
|
#include "render_text_texture.hpp"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
void Button::DrawTo(SDL_Renderer* renderer) {
|
void Button::DrawTo(SDL_Renderer* renderer) {
|
||||||
@@ -51,19 +53,9 @@ void Button::SetBackgroundTexture(SDL_Renderer* renderer, SDL_Texture* texture)
|
|||||||
image.SetClipH(image.GetClipH() / 3);
|
image.SetClipH(image.GetClipH() / 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Button::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string s, SDL_Color color) {
|
void Button::SetText(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string s) {
|
||||||
//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
|
//convert to texture
|
||||||
SDL_Texture* text = SDL_CreateTextureFromSurface(renderer, surf);
|
SDL_Texture* text = renderTextTexture(renderer, font, color, s);
|
||||||
SDL_FreeSurface(surf);
|
|
||||||
if (!text) {
|
|
||||||
throw(std::runtime_error("Failed to create a TTF texture"));
|
|
||||||
}
|
|
||||||
|
|
||||||
//get the dimensions & rects
|
//get the dimensions & rects
|
||||||
int x, y, w, h;
|
int x, y, w, h;
|
||||||
@@ -87,14 +79,6 @@ void Button::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string s, SDL_
|
|||||||
SDL_DestroyTexture(text);
|
SDL_DestroyTexture(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Button::SetX(int x) {
|
|
||||||
posX = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Button::SetY(int y) {
|
|
||||||
posY = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
Button::State Button::MouseMotion(SDL_MouseMotionEvent const& event) {
|
Button::State Button::MouseMotion(SDL_MouseMotionEvent const& event) {
|
||||||
//if out of bounds, exit
|
//if out of bounds, exit
|
||||||
if (!CheckBounds(event.x, event.y)) {
|
if (!CheckBounds(event.x, event.y)) {
|
||||||
@@ -156,6 +140,22 @@ Button::State Button::GetState() {
|
|||||||
return state;
|
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) {
|
bool Button::CheckBounds(int x, int y) {
|
||||||
//return if true (x, y) is within bounds, otherwise return false
|
//return if true (x, y) is within bounds, otherwise return false
|
||||||
return !(
|
return !(
|
||||||
|
|||||||
@@ -27,11 +27,6 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
constexpr SDL_Color COLOR_WHITE = {255, 255, 255, 255};
|
|
||||||
constexpr SDL_Color COLOR_RED = {255, 0, 0, 255};
|
|
||||||
constexpr SDL_Color COLOR_ORANGE = {255, 127, 0, 255};
|
|
||||||
constexpr SDL_Color COLOR_BLUE = {0, 0, 255, 255};
|
|
||||||
|
|
||||||
class Button {
|
class Button {
|
||||||
public:
|
public:
|
||||||
enum State {
|
enum State {
|
||||||
@@ -46,9 +41,7 @@ public:
|
|||||||
|
|
||||||
//setup
|
//setup
|
||||||
void SetBackgroundTexture(SDL_Renderer*, SDL_Texture*);
|
void SetBackgroundTexture(SDL_Renderer*, SDL_Texture*);
|
||||||
void SetText(SDL_Renderer*, TTF_Font*, std::string, SDL_Color);
|
void SetText(SDL_Renderer*, TTF_Font*, SDL_Color, std::string);
|
||||||
void SetX(int x);
|
|
||||||
void SetY(int y);
|
|
||||||
|
|
||||||
//capture input
|
//capture input
|
||||||
State MouseMotion(SDL_MouseMotionEvent const&);
|
State MouseMotion(SDL_MouseMotionEvent const&);
|
||||||
@@ -59,10 +52,16 @@ public:
|
|||||||
void SetState(State); //TODO: idle, busy or disabled
|
void SetState(State); //TODO: idle, busy or disabled
|
||||||
State GetState();
|
State GetState();
|
||||||
|
|
||||||
|
//accessors & mutators
|
||||||
|
int SetX(int x);
|
||||||
|
int SetY(int y);
|
||||||
|
int GetX() const;
|
||||||
|
int GetY() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool CheckBounds(int x, int y);
|
bool CheckBounds(int x, int y);
|
||||||
|
|
||||||
Image image;
|
Image image;
|
||||||
int posX = 0, posY = 0;
|
|
||||||
State state = State::IDLE;
|
State state = State::IDLE;
|
||||||
|
int posX = 0, posY = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#config
|
#config
|
||||||
INCLUDES+=.
|
INCLUDES+=. ../utilities
|
||||||
LIBS+=
|
LIBS+=
|
||||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
/* 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 <stdexcept>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
/* 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 <string>
|
||||||
|
|
||||||
|
SDL_Texture* renderTextTexture(SDL_Renderer*, TTF_Font*, SDL_Color color, std::string);
|
||||||
@@ -31,15 +31,18 @@ TextBox::~TextBox() {
|
|||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextBox::DrawTo(SDL_Renderer* renderer, int posX, int posY, int pointSize) {
|
void TextBox::DrawTo(SDL_Renderer* renderer) {
|
||||||
|
int renderY = posY;
|
||||||
for (std::list<TextLine>::iterator it = lineList.begin(); it != lineList.end(); it++) {
|
for (std::list<TextLine>::iterator it = lineList.begin(); it != lineList.end(); it++) {
|
||||||
it->DrawTo(renderer, posX, posY);
|
it->SetX(posX);
|
||||||
posY += pointSize;
|
it->SetY(renderY);
|
||||||
|
it->DrawTo(renderer);
|
||||||
|
renderY += it->GetPointHeight();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextBox::PushLine(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) {
|
void TextBox::PushLine(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string str) {
|
||||||
lineList.emplace_front(renderer, font, str, color);
|
lineList.emplace_back(renderer, font, color, str, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextBox::PopLine(int num) {
|
void TextBox::PopLine(int num) {
|
||||||
@@ -47,10 +50,26 @@ void TextBox::PopLine(int num) {
|
|||||||
num < lineList.size() ? num : lineList.size();
|
num < lineList.size() ? num : lineList.size();
|
||||||
|
|
||||||
for (int i = 0; i < num; ++i) {
|
for (int i = 0; i < num; ++i) {
|
||||||
lineList.pop_back();
|
lineList.pop_front();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextBox::ClearLines() {
|
void TextBox::ClearLines() {
|
||||||
lineList.clear();
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -34,12 +34,18 @@ public:
|
|||||||
TextBox();
|
TextBox();
|
||||||
~TextBox();
|
~TextBox();
|
||||||
|
|
||||||
void DrawTo(SDL_Renderer*, int posX, int posY, int pointSize);
|
void DrawTo(SDL_Renderer*);
|
||||||
|
|
||||||
void PushLine(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color);
|
void PushLine(SDL_Renderer*, TTF_Font*, SDL_Color color, std::string);
|
||||||
void PopLine(int num = 1);
|
void PopLine(int num = 1);
|
||||||
void ClearLines();
|
void ClearLines();
|
||||||
|
|
||||||
|
int SetX(int i);
|
||||||
|
int SetY(int i);
|
||||||
|
int GetX() const;
|
||||||
|
int GetY() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::list<TextLine> lineList;
|
std::list<TextLine> lineList;
|
||||||
|
int posX = 0, posY = 0;
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
/* 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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
/* 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 <string>
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
@@ -21,50 +21,58 @@
|
|||||||
*/
|
*/
|
||||||
#include "text_line.hpp"
|
#include "text_line.hpp"
|
||||||
|
|
||||||
|
#include "render_text_texture.hpp"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
SDL_Texture* renderTextTexture(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) {
|
|
||||||
//make the surface (from SDL_ttf)
|
|
||||||
SDL_Surface* surface = TTF_RenderText_Solid(font, str.c_str(), color);
|
|
||||||
if (!surface) {
|
|
||||||
throw(std::runtime_error("Failed to create a TTF surface"));
|
|
||||||
}
|
|
||||||
|
|
||||||
//convert to texture
|
|
||||||
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
|
|
||||||
|
|
||||||
//cleanup
|
|
||||||
SDL_FreeSurface(surface);
|
|
||||||
|
|
||||||
//check
|
|
||||||
if (!texture) {
|
|
||||||
throw(std::runtime_error("Failed to create a TTF texture"));
|
|
||||||
}
|
|
||||||
|
|
||||||
//NOTE: free the texture yourself
|
|
||||||
return texture;
|
|
||||||
}
|
|
||||||
|
|
||||||
TextLine::TextLine() {
|
TextLine::TextLine() {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
TextLine::~TextLine() {
|
||||||
ClearText();
|
ClearText();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextLine::DrawTo(SDL_Renderer* renderer, int posX, int posY) {
|
void TextLine::DrawTo(SDL_Renderer* renderer) {
|
||||||
SDL_Rect dclip = {posX, posY, 0, 0};
|
SDL_Rect dclip = {posX, posY, 0, 0};
|
||||||
SDL_QueryTexture(texture, nullptr, nullptr, &dclip.w, &dclip.h);
|
SDL_QueryTexture(texture, nullptr, nullptr, &dclip.w, &dclip.h);
|
||||||
SDL_RenderCopy(renderer, texture, nullptr, &dclip);
|
SDL_RenderCopy(renderer, texture, nullptr, &dclip);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextLine::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) {
|
void TextLine::SetText(SDL_Renderer* renderer, TTF_Font* font, SDL_Color color, std::string str) {
|
||||||
//just use the above global function
|
//just use the above global function
|
||||||
SDL_DestroyTexture(texture);
|
SDL_DestroyTexture(texture);
|
||||||
texture = renderTextTexture(renderer, font, str, color);
|
texture = renderTextTexture(renderer, font, color, str);
|
||||||
|
pointHeight = TTF_FontHeight(font);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextLine::ClearText() {
|
void TextLine::ClearText() {
|
||||||
SDL_DestroyTexture(texture);
|
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;
|
||||||
}
|
}
|
||||||
@@ -26,20 +26,28 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
SDL_Texture* renderTextTexture(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color);
|
|
||||||
|
|
||||||
class TextLine {
|
class TextLine {
|
||||||
public:
|
public:
|
||||||
TextLine();
|
TextLine();
|
||||||
TextLine(SDL_Renderer* r, TTF_Font* f, std::string s, SDL_Color c)
|
TextLine(SDL_Renderer*, TTF_Font*, SDL_Color, std::string, int x, int y);
|
||||||
{ SetText(r, f, s, c); }
|
|
||||||
virtual ~TextLine();
|
virtual ~TextLine();
|
||||||
|
|
||||||
void DrawTo(SDL_Renderer*, int posX, int posY);
|
void DrawTo(SDL_Renderer*);
|
||||||
|
|
||||||
void SetText(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color);
|
void SetText(SDL_Renderer*, TTF_Font*, SDL_Color, std::string);
|
||||||
void ClearText();
|
void ClearText();
|
||||||
|
|
||||||
|
//accessors & mutators
|
||||||
|
int SetX(int i);
|
||||||
|
int SetY(int i);
|
||||||
|
int GetX() const;
|
||||||
|
int GetY() const;
|
||||||
|
|
||||||
|
//utility
|
||||||
|
int GetPointHeight();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
SDL_Texture* texture = nullptr;
|
SDL_Texture* texture = nullptr;
|
||||||
|
int posX = 0, posY = 0;
|
||||||
|
int pointHeight = 0; //internal use for TextBox
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user