class Renderer{
public:
void text_to_sdl_surface(Text text, Colour colour);
Font get_font(std::string font_name, int font_size);
private:
SDL_Surface * text_surface;
};
void Renderer::text_to_sdl_surface(Text text, Colour colour) {
glColor4f(1,1,1,1);
SDL_Color text_colour;
text_colour.r = Uint8(colour.get_colour_as_int()[0]);
text_colour.g = Uint8(colour.get_colour_as_int()[1]);
text_colour.b = Uint8(colour.get_colour_as_int()[2]);
TTF_Font * temp_font = get_font(text.get_fontname(), text.get_fontsize()).get_font();
text_surface = TTF_RenderText_Blended(temp_font, text.get_string().c_str(), text_colour);
}
I am using SDL_TTF to draw text and have a memory leak that I have narrowed down to the above function, specifically this line:
text_surface = TTF_RenderText_Blended(temp_font, text.get_string().c_str(), text_colour);
After using the surface in opengl I am calling SDL_FreeSurface before the function above is run again however it is still leaking.
Related
I'm using C++ with the SDL2 library to create a game. I'm using the SDL_ttf extension to be able to use ttf fonts and I'm trying to create my own class that would be more effective for multiple texts on the screen. The code I currently have starts out good, then crashes after about 15 seconds of running. I added more text and now it crashes after about 5 or 7 seconds. I'm looking for advice on how to solve this problem. my full Font class is as follows:
Font.h
#pragma once
#include "Graphics.h"
#include <string>
class Font
{
public:
Font(std::string path, SDL_Renderer* renderer);
~Font();
void FreeText();
void LoadText(int size, RGB_COLOR color, std::string text);
void Draw(int x, int y, Graphics& gfx, int size, RGB_COLOR color, std::string text);
private:
int width,height;
TTF_Font* font;
SDL_Texture* mTexture;
SDL_Renderer* renderer;
std::string path;
};
Font.cpp
#include "Font.h"
Font::Font(std::string path, SDL_Renderer* renderer)
:
font(NULL),
mTexture(NULL),
renderer(renderer),
path(path)
{
printf("Font con..\n");
}
Font::~Font()
{
}
void Font::LoadText(int size, RGB_COLOR color, std::string text)
{
font = TTF_OpenFont(path.c_str(), size);
SDL_Color c = {color.RED, color.GREEN, color.BLUE};
SDL_Surface* loadedSurface = TTF_RenderText_Solid(font, text.c_str(), c);
mTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
width = loadedSurface->w;
height = loadedSurface->h;
SDL_FreeSurface(loadedSurface);
}
void Font::FreeText()
{
SDL_DestroyTexture(mTexture);
mTexture = NULL;
}
void Font::Draw(int x, int y, Graphics& gfx, int size, RGB_COLOR color, std::string text)
{
FreeText();
LoadText(size, color, text);
SDL_Rect rect = {x, y, width * gfx.GetGameDims().SCALE, height * gfx.GetGameDims().SCALE};
gfx.DrawTexture(mTexture, NULL, &rect);
}
My Graphics class just handles the actual drawing as well as dimensions of the game (screen size, tile size, color struct, gamestates, etc) So when I'm calling gfx.Draw it calls SDL_RenderCopy function.
Within my Game class I have a pointer to my Font class. (its called in my Game constructor) Then font->Draw() is called every frame; which destroys the original SDL_Texture, Loads the new text, then renders it on the screen.
My ultimate goal is to have my font class set up to where I choose the color and size from my draw function. Not sure what to check from this point on..
Any suggestions? Ideas?
This is what I get (which is what I want) but then it crashes.
I've managed to get it working. After searching a little more on SDL_ttf, I realized that in my FreeFont() function I was clearing out the SDL_Texture, however I did nothing with the TTF_Font.
Adding these lines in that function did the trick:
TTF_CloseFont(font);
font = NULL;
I was following Lazy Foo' tutorial to create text using ttf font, and everything was fine, but I needed to create several text lines in several different places with different font size and color, so I decided to use vector. Here is my code of TextTexture (mostly copy of Lazy Foo tutorial):
#ifndef TEXT_TEXTURE_HPP
#define TEXT_TEXTURE_HPP
#include "graphics.hpp"
#include "vector2.hpp"
#include <SDL2/SDL_ttf.h>
#include <string>
class TextTexture {
public:
TextTexture(
Graphics& graphics,
TTF_Font* font,
std::string textureText,
SDL_Color textColor,
Vector2 coordinates
);
~TextTexture();
void draw( Graphics& graphics );
private:
SDL_Texture* mTexture;
int mWidth;
int mHeight;
int mX;
int mY;
};
#endif // TEXT_TEXTURE_HPP
And .cpp file for it:
#include "text_texture.hpp"
#include "vector2.hpp"
#include <iostream>
#include <unistd.h>
TextTexture::TextTexture (
Graphics& graphics,
TTF_Font* font,
std::string textureText,
SDL_Color textColor,
Vector2 coordinates
) :
mTexture(NULL),
mWidth(0),
mHeight(0),
mX(0),
mY(0)
{
//Render temp surface
SDL_Surface* tempSurface = TTF_RenderUTF8_Blended (font, textureText.c_str(), textColor);
if ( tempSurface == NULL ) {
std::cout << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << std::endl;
} else {
this -> mTexture = SDL_CreateTextureFromSurface(graphics.getRenderer(), tempSurface);
if ( this -> mTexture == NULL ) {
std::cout << "Unable to create texture from rendered text! SDL Error: " << SDL_GetError() << std::endl;
} else {
//Get image dimensions
mWidth = tempSurface -> w;
mHeight = tempSurface -> h;
// Get coordinates
this -> mX = coordinates.getX();
this -> mY = coordinates.getY();
}
SDL_FreeSurface (tempSurface);
tempSurface = NULL;
}
}
TextTexture::~TextTexture() {
//Free texture if it exists
if ( mTexture != NULL ) {
SDL_DestroyTexture( mTexture );
}
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
// FIXME somewhy affects previous dest rects
void TextTexture::draw (Graphics& graphics) {
//Set rendering space and render to screen
SDL_Rect destinationRectangle = { mX, mY, this -> mWidth, this -> mHeight };
//Render to screen
graphics.blitSurface( mTexture, NULL, &destinationRectangle );
}
I created simple Text Manager to handle vector of texts:
#ifndef TEXT_MANAGER_HPP
#define TEXT_MANAGER_HPP
#include "graphics.hpp"
#include "text_texture.hpp"
#include "vector2.hpp"
#include <string>
#include <vector>
enum fontSize {
SMALL = 16,
NORMAL = 32,
BIG = 48,
TITLE = 72
};
enum fontColor {
WHITE,
ORANGE,
BLACK
};
class TextManager {
public:
TextManager(Graphics& graphics);
~TextManager();
void addText(std::string, fontSize, fontColor, Vector2);
void draw();
void clearText();
private:
Graphics& graphics;
std::vector <TextTexture> gText;
};
#endif // TEXT_MANAGER_HPP
and .cpp file:
#include "text_manager.hpp"
#include <iostream>
TextManager::TextManager(Graphics& graphics) :
graphics(graphics)
{}
TextManager::~TextManager() {}
void TextManager::addText(std::string text, fontSize size, fontColor color, Vector2 coordinates) {
TTF_Font* tempFont = TTF_OpenFont( "resources/fonts/pixel.ttf", fontSize::TITLE );
SDL_Color tempColor = { 255, 255, 255 };
// Switch removed for shorter code
this -> gText.emplace_back(graphics, tempFont, text, tempColor, coordinates);
TTF_CloseFont(tempFont);
tempFont = NULL;
}
// FIXME
void TextManager::draw() {
std::vector<TextTexture>::iterator it;
for(it = gText.begin(); it != gText.end(); ++it) {
it -> draw(graphics);
}
}
void TextManager::clearText() {
gText.clear();
}
But when I start the application, I see something like this:
Second string is printed, but font and bonding rectangle of first line is saved, hovewer
Later I added input handler that added second line of text after pressing a button, and when there is only one line of text, everything fine, but when you add second, something weird is beginning - sometimes first text disappears, sometimes 'both' of them is shoved. As I understand, second surface of text somehow affects first one, by copying it texture on the place of the first's destination.
Here is my graphics.blitSurface, if it will help:
void Graphics::blitSurface(SDL_Texture* texture, SDL_Rect* sourceRectangle, SDL_Rect* destinationRectangle)
{
SDL_RenderCopy ( this -> _renderer, texture, sourceRectangle, destinationRectangle );
}
Where is my mistake? Sorry for bad english, I hope you will get my problem.
I figured it out somehow randomly. The thing is that when I adding object to vector, it's calls a destructor.
Here is why:
Why does my class's destructor get called when I add instances to a vector?
I trying to render an OSG scene into a image in my Qt program. Refer to the example of SnapImageDrawCallback(https://www.mail-archive.com/osg-users#lists.openscenegraph.org/msg45360.html).
class SnapImageDrawCallback : public osg::CameraNode::DrawCallback {
public:
SnapImageDrawCallback()
{
_snapImageOnNextFrame = false;
}
void setFileName(const std::string& filename) { _filename = filename; }
const std::string& getFileName() const { return _filename; }
void setSnapImageOnNextFrame(bool flag) { _snapImageOnNextFrame = flag;}
bool getSnapImageOnNextFrame() const { return _snapImageOnNextFrame; }
virtual void operator () (const osg::CameraNode& camera) const
{
if (!_snapImageOnNextFrame) return;
int x,y,width,height;
x = camera.getViewport()->x();
y = camera.getViewport()->y();
width = camera.getViewport()->width();
height = camera.getViewport()->height();
osg::ref_ptr<osg::Image> image = new osg::Image;
image->readPixels(x,y,width,height,GL_RGB,GL_UNSIGNED_BYTE);
if (osgDB::writeImageFile(*image,_filename))
{
std::cout << "Saved screen image to `"<<_filename
<<"`"<< std::endl;
}
_snapImageOnNextFrame = false;
}
protected:
std::string _filename;
mutable bool _snapImageOnNextFrame;
};
I set this as a the osg::Viewer's camera's FinalDrawCallback, but I failed with a blank image, and get this warning "Warning: detected OpenGL error 'invalid operation' at start of State::apply()" when invoke image->readPixels, My osgViewer::Viewer in embedded in QQuickFramebufferObject. Can any one give some suggestions?
Not sure to give you the right pointer, you should provide more details about your setup and what you're after.
As a general note, if you're trying to render with OSG into a QtQuick widget the best approach is to have osg to render to an FBO in a separate shared GL context, and copy the FBO contents back the qtquick widget.
I had tested this approach some times ago, see code here:
https://github.com/rickyviking/qmlosg
Another similar project here: https://github.com/podsvirov/osgqtquick
you can use pbo
ext->glGenBuffers(1, &pbo);
ext->glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
ext->glBufferData(GL_PIXEL_PACK_BUFFER_ARB, _width*_height*4, 0, GL_STREAM_READ);
glReadPixels(0, 0, _width, _height, _pixelFormat, _type, 0);
GLubyte* src = (GLubyte*)ext->glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB,
GL_READ_ONLY_ARB);
if(src)
{
memcpy(image->data(), src, _width*_height*4);
ext->glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
}
ext->glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
i'm new and still learning OOP and SDL for educational purpose.
so, i have a variable SDL_Renderer renderer. this variable needs to be initiated only once, and i initiate it in GameManager class.
and i have another class named Texture that needs that renderer.
this Texture will be used frequently.
so how do i pass this renderer? do i have to call GameManager in the Texture class? but if i do that, it means that i makeGameManager everytime i use the Texture right? or there is another way around?
thank you for helping me, i'm really sorry if my question is vague or not clear.
EDIT
this is Texture class
class Texture
{
public:
Texture();
~Texture();
int getWidth();
int getHeight();
bool loadFromFile(std::string path);
bool loadTextFromFile(std::string text, SDL_Color textColor, TTF_Font* font);
void render(int x, int y, SDL_Rect* clip = NULL);
void free();
bool lockTexture();
bool unlockTexture();
void* getPixels();
int getPitch();
private:
int vWidth;
int vHeight;
SDL_Texture* vTexture;
SDL_Renderer* renderer;
void* pPixels;
int pPitch;
};
this is the initiator
Texture::Texture()
{
vTexture = NULL;
vWidth = 0;
vHeight = 0;
renderer = GameManager::getRenderer();
}
this is GameManager class
class GameManager
{
public:
GameManager();
~GameManager();
bool intializeGame();
void gameLoop();
static SDL_Renderer* getRenderer();
private:
SDL_Window* window = NULL;
static SDL_Renderer* renderer;
TTF_Font* font = NULL;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
};
the getRenderer() just a getter to pass the renderer
and this is my Main
int main(int argc, char* args[])
{
GameManager gameManager;
gameManager.intializeGame();
Texture charTexture;
SDL_Rect rect;
bool text = charTexture.loadFromFile("foo.png");
if (!text)
{
printf("texture not loaded");
}
rect.x = 0;
rect.y = 0;
rect.w = charTexture.getWidth();
rect.h = charTexture.getHeight();
while (true)
{
SDL_SetRenderDrawColor(GameManager::getRenderer(), 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(GameManager::getRenderer());
charTexture.render(10, 10, &rect);
SDL_RenderPresent(GameManager::getRenderer());
}
return 0;
}
i hope it's not confusing.
Disclaimer : I've never used SDL. This might be terrible, but it's based on what you gave me.
The important thing is the ownership. This is a shared ownership example. It's quite simple and takes the burden of figuring out when to destroy SDL_Renderer off of you.
class GameManager {
//BLABGLABLA
public:
std::shared_ptr<SDL_Renderer> getRenderer();
private:
std::shared_ptr<SDL_Renderer> renderer
}
class Texture
{
public:
Texture(std::shared_ptr<SDL_Renderer> theRenderer, //some other args)
private:
std::shared_ptr<SDL_Renderer> renderer;
}
So based on the name of the classes alone, you probably want GameManager to own the renderer, but you also want Texture to have access to it.
One way you can do that is to have a shared_ptr member in both classes, and to pass the renderer to texture in its constructor.
Basically the renderer object you initialized in GameManager, will only be destroyed when the last shared_ptr pointing to it is destroyed.
I apologize if I give more details than necessary. I have a class Canvas that looks like this:
class Canvas : public QWidget
{
Q_OBJECT
public:
explicit Canvas(int width = 700, int height = 700, QWidget *parent = 0);
void setDelegate(CanvasDelegate *delegate);
private:
CanvasDelegate *delegate;
void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *resizeEvent);
[...]
};
The Canvas::paintEvent(QPaintEvent *) function is implemented like this:
void Canvas::paintEvent(QPaintEvent *)
{
delegate->redrawBuffer();
QPainter canvas_painter(this);
canvas_painter.drawImage(0, 0, *(delegate->getImage()));
}
And so the class CanvasDelegate looks like this:
class CanvasDelegate
{
friend class Canvas;
public:
CanvasDelegate(const Canvas *canvas);
~CanvasDelegate();
const QImage * getImage() const;
void drawPoint(const Complex &z, const QColor &color = "black", int width = 3);
[...]
virtual void redrawBuffer(const H2Isometry &mobius = H2Isometry::identity()) = 0;
virtual void mousePress(QMouseEvent * mouseEvent) = 0;
[...]
protected:
const Canvas *canvas;
int sizeX, sizeY;
[...]
QPen *pen;
QImage *image;
QPainter *painter;
void rescale(int sizeX, int sizeY);
};
The constructor of CanvasDelegate is as follows:
CanvasDelegate::CanvasDelegate(const Canvas *canvas) : canvas(canvas)
{
pen = new QPen;
image = new QImage(canvas->width(), canvas->height(), QImage::Format_RGB32);
painter = new QPainter(image);
[...]
}
I'm not sure this is the best design ever but this is not my question (any comments are welcome, though). My problem is what happens when the window (Canvas) is resized. Here is what my code looks like:
void Canvas::resizeEvent(QResizeEvent *resizeEvent)
{
QSize newSize = resizeEvent->size();
delegate->rescale(newSize.width(), newSize.height());
//update();
}
void CanvasDelegate::rescale(int sizeX, int sizeY)
{
*image = QImage(sizeX, sizeY, QImage::Format_RGB32);
painter->eraseRect(0, 0, sizeX, sizeY);
this->sizeX = sizeX;
this->sizeY = sizeY;
[...]
}
The problem is that when I run the program, it crashes. Apparently there is a segmentation fault when painter->eraseRect(0, 0, sizeX, sizeY); is called in void CanvasDelegate::rescale(int sizeX, int sizeY). I don't understand why, I don't see what the problem is.
In a previous version, I had written the following (which now seems to me more complicated than necessary):
void CanvasDelegate::rescale(int sizeX, int sizeY)
{
QImage * oldImage = image;
QImage * newImage = new QImage(sizeX, sizeY, QImage::Format_RGB32);
QPainter * oldPainter = painter;
QPainter * newPainter = new QPainter(newImage);
newPainter->eraseRect(0, 0, sizeX, sizeY);
newPainter->setPen(*pen);
image = newImage;
painter = newPainter;
delete oldImage;
delete oldPainter;
this->sizeX = sizeX;
this->sizeY = sizeY;
[...]
}
But that does not work: I get a Qt error QPaintDevice: Cannot destroy paint device that is being painted. If I remove delete oldImage; and delete oldPainter;, everything works fine but that is a disgusting memory leak, isn't it.
Does someone understand why what I have written does not work, and what I need to do?
Thank you very much for your attention.
I'm not exactly sure why painter->eraseRect(0, 0, sizeX, sizeY); segfaults, but it may be that when the paintdevice of a QPainter is an image, its size shoudn't change, and therefore *image = QImage(sizeX, sizeY, QImage::Format_RGB32); breaks this assumption.
Therefore, I would try, before resizing the image, to delete the QPainter, then resize the image, then allocate a new QPainter. In code:
void CanvasDelegate::rescale(int sizeX, int sizeY)
{
delete painter;
*image = QImage(sizeX, sizeY, QImage::Format_RGB32);
painter = new QPainter(image);
painter->eraseRect(0, 0, sizeX, sizeY);
painter->setPen(*pen);
this->sizeX = sizeX;
this->sizeY = sizeY;
[...]
}