Implementación de texto.

parent 8a27233b
#ifndef FUENTE_H
#define FUENTE_H
#include <SDL2/SDL_ttf.h>
#include <stdexcept>
/// Encapsula un puntero TTF_Font* para tener RAII
class Fuente {
TTF_Font* fuente;
public:
Fuente(const char *camino, int tamano_pt) {
fuente = TTF_OpenFont(camino, tamano_pt);
if (fuente == nullptr) {
throw std::runtime_error(SDL_GetError());
}
};
TTF_Font *operator*() {
return fuente;
}
~Fuente() {
TTF_CloseFont(fuente);
}
};
#endif // FUENTE_H
#include "Texto.h"
SDL_Texture *Texto::get_textura(SDL_Renderer *renderer) {
if (texture == nullptr) {
texture = SDL_CreateTextureFromSurface(renderer, surface);
}
return texture;
}
void Texto::renderizar(SDL_Renderer *renderer, int x, int y) {
texture = get_textura(renderer);
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_QueryTexture(texture, nullptr, nullptr, &dest.w, &dest.h);
SDL_RenderCopy(renderer, texture, nullptr, &dest);
}
#ifndef TEXTO_H
#define TEXTO_H
#include "Fuente.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_ttf.h>
class Texto {
SDL_Surface *surface;
SDL_Texture *texture = nullptr;
SDL_Texture *get_textura(SDL_Renderer *renderer);
public:
Texto(Fuente &fuente, const char *texto, SDL_Color fg) {
surface = TTF_RenderUTF8_Solid(*fuente, texto, fg);
};
void renderizar(SDL_Renderer *Renderer, int x, int y);
~Texto() {
SDL_FreeSurface(surface);
if (texture) {
SDL_DestroyTexture(texture);
}
}
};
#endif // TEXTO_H
No preview for this file type
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
#include "SDL2/SDL_ttf.h" #include "SDL2/SDL_ttf.h"
#include <iostream> #include <iostream>
#include "Sprite.h" #include "Sprite.h"
#include "Texto.h"
#include "Fuente.h"
#include "Personaje.h" #include "Personaje.h"
void blit(SDL_Renderer *Renderer, SDL_Texture *Texture, SDL_Rect *Cut, int x, int y){ void blit(SDL_Renderer *Renderer, SDL_Texture *Texture, SDL_Rect *Cut, int x, int y){
...@@ -39,6 +41,12 @@ int main(){ ...@@ -39,6 +41,12 @@ int main(){
return -1; return -1;
} }
if (TTF_Init() < 0) {
std::cout << "Error at TTF_Init();"<<std::endl;
std::cout << "Error: " << SDL_GetError() << std::endl;
return -1;
}
SDL_Window *Window = SDL_CreateWindow( SDL_Window *Window = SDL_CreateWindow(
"SDL2 Window", "SDL2 Window",
SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,
...@@ -65,6 +73,8 @@ int main(){ ...@@ -65,6 +73,8 @@ int main(){
} }
Fuente opensans("assets/OpenSans-Italic.ttf", 100);
Texto texto(opensans, "AGAPITO 3D", {255, 255, 255});
Sprite Fondo1("assets/fondoHabitacion.jpg"); Sprite Fondo1("assets/fondoHabitacion.jpg");
Sprite Planta("assets/planta.png"); Sprite Planta("assets/planta.png");
Personaje Agapito; Personaje Agapito;
...@@ -100,6 +110,7 @@ int main(){ ...@@ -100,6 +110,7 @@ int main(){
Fondo1.renderizar(Renderer, 0, 0, 1920, 1080); Fondo1.renderizar(Renderer, 0, 0, 1920, 1080);
Planta.renderizar(Renderer, 10, 100, 50, 50); Planta.renderizar(Renderer, 10, 100, 50, 50);
Agapito.renderizar(Renderer); Agapito.renderizar(Renderer);
texto.renderizar(Renderer, 50, 50);
cambiar_buffers(Renderer); cambiar_buffers(Renderer);
SDL_Delay(5); SDL_Delay(5);
} }
......
CC = g++ CC = g++
CodeFiles = main.cpp Personaje.cpp Sprite.cpp CodeFiles = main.cpp Personaje.cpp Sprite.cpp Texto.cpp
Executable: $(CodeFiles) Executable: $(CodeFiles)
$(CC) $(CodeFiles) -w -lSDL2 -lSDL2_image -lSDL2_ttf -o Executable $(CC) $(CodeFiles) -w -lSDL2 -lSDL2_image -lSDL2_ttf -o Executable
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment