As the title says I would like to know how to reload a TTF_SDL Text during runtime in C++.
What I want to do is to update all the values of the text each frame so I can, to example change the font size and it will instantly change it in-game as well without needing to restart the entire game.
How would I do this?
Note : I'm using textures to render the text, using the SDL_CreateTextureFromSurface() function.
This is the way I'm currently trying to update the TTF Text :
void Text::Update(SDL_Renderer* renderer)
{
SDL_FreeSurface(m_surface);
SDL_DestroyTexture(m_texture);
m_surface = new SDL_Surface();
m_surface = TTF_RenderText_Solid(m_font, m_text.c_str(), m_color_fg);
m_texture = SDL_CreateTextureFromSurface(renderer, m_surface);
}
And in the header file the two variables look like this :
SDL_Surface* m_surface = nullptr;
SDL_Texture* m_texture = nullptr;
And I currently get an error at SDL_FreeSurface(m_surface); which looks like this :
Thread 1: EXC_BAD_ACCESS (code=EXC_l386_GPFLT)
However, the code can also be 1 with a address that seem to change every time I run my application
However I sometimes get another error on this line :
m_surface = TTF_RenderText_Solid(m_font, m_text.c_str(), m_color_fg);
Instead, which has the following error code :
Thread 1: EXC_BAD_ACCESS (code=1, adress=0x11)
But the address on the error codes are not the same every time I run it.
(On both lines of code)
**Full sprite class code :**
Sprite.hpp :
//
// Text.hpp
// SDL_Project
//
// Created by Someone... on 2016-03-18.
// Copyright © 2016 Someone... All rights reserved.
//
#ifndef Text_hpp
#define Text_hpp
#include <stdio.h>
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2_ttf/SDL_ttf.h>
class Text {
public:
Text(std::string __m_text, int x, int y, SDL_Renderer* renderer);
void Render(SDL_Renderer* renderer);
void Update(SDL_Renderer* renderer);
void SetText(std::string text, SDL_Renderer* renderer);
void Destroy();
private:
SDL_Rect* m_rect = nullptr;
SDL_Surface* m_surface = nullptr;
SDL_Texture* m_texture = nullptr;
SDL_Color m_color_fg;
TTF_Font* m_font;
std::string m_text;
};
#endif /* Text_hpp */
Sprite.cpp :
//
// Text.cpp
// SDL_Project
//
// Created by Still made by someone on 2016-03-18.
// Copyright © 2016 :). All rights reserved.
//
#include "Text.hpp"
Text::Text(std::string __m_text, int x, int y, SDL_Renderer* renderer)
{
m_text = __m_text;
m_font = TTF_OpenFont("arial.ttf", 24);
m_rect = new SDL_Rect();
m_rect->x = x;
m_rect->y = y;
m_surface = TTF_RenderText_Solid(m_font, m_text.c_str(), m_color_fg);
if (m_surface == nullptr)
{
std::cout << "Failed to create text surface : " << SDL_GetError() << std::endl;
}
m_texture = SDL_CreateTextureFromSurface(renderer, m_surface);
if (m_texture == nullptr)
{
std::cout << "Failed to create text texture : " << SDL_GetError() << std::endl;
}
TTF_CloseFont(m_font);
SDL_FreeSurface(m_surface);
}
void Text::Render(SDL_Renderer* renderer)
{
SDL_RenderCopy(renderer, m_texture, NULL, m_rect);
}
void Text::Update(SDL_Renderer* renderer)
{
// This part throws me a error and makes the game crash.
std::cout << "1 : " << SDL_GetError() << std::endl;
SDL_FreeSurface(m_surface);
SDL_DestroyTexture(m_texture);
std::cout << "2 : " << SDL_GetError() << std::endl;
m_surface = TTF_RenderText_Solid(m_font, m_text.c_str(), m_color_fg);
m_texture = SDL_CreateTextureFromSurface(renderer, m_surface);
}
void Text::SetText(std::string text, SDL_Renderer* renderer)
{
m_text = text;
Update(renderer);
}
void Text::Destroy()
{
}