Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - larta

Pages: [1]
1
General / [Qt + SFML] Resize Qt main frame does nothing on SFML window
« on: February 06, 2014, 10:48:26 am »
Hi all
I'm actually having a problem. I use SFML 2.1 with Qt 5.

The problem is that i can't resize my SFML view. Let me explain :
When using only sfml, resizing the window change the window content size and position naturally.

But when using Qt, resizing the qt main frame does nothing on the sfml view (That the natural behavior, i know).
So i wanted to catch the resize event to resize the sfml view.
Well, what happend is not what i expected.

I can catch the resize event and give it to my sfml widget using an event filter. But i can't resize correctly the sfml window. I tried to put a view inside the window, then call setSize() on the view (and setView() on the window) but it doesn't change the actual window size.
It just change the inside view size, making my sprites smaller, but their position doesn't change.

To be clearer, i made a small project illustrating what i'm saying.
It just draw something on the screen. When resizing the window, the content should move and be resized, depending on the size of the main qt frame.

Link in attachement.
(I copy/pasted my project code and removed some things, just don't care about useless commented code x))
(It's just the basic Sfml + Qt code + some other things (and the event filter))

To sum up, i want to reproduce the resizing behavior of a sfml window, inside an sfml frame. That's to say, do something like calling a "native_resize" function on the sfml window.

Hope you can help me.
Thanks
(Edit : I forgot to say the project runs on Linux)


(Edit2 : Looks like i'm stupid.
This morning, right after poster i saw a post talking about a "setSize()" function on a sf::RenderWindow. Well, i didn't see this, and that actually works. Sorry, this post is now useless. It may be useless for other, who knows .. x))

2
Graphics / Re: [SFFont] Valgrind is not happy when loading fonts from file
« on: November 03, 2013, 09:38:50 pm »
Thanks for your answers.

I read about RAII, and I still don't get how it's supposed to be "usefull". I mean, how am i supposed to load my ressources only once and access it everywhere i need it with it ? (That's a stupid question in fact, RAII seems to be more a way to code than something to apply line by line)

I know, many things have to change in my conception if i want to do it, but i don't get how to do.
For me, the only way to store things, load it only once and access it everywhere in the code is singleton / global variables and i don't understand how RAII would help me here. Maybe i didn't understand what RAII is supposed to be use for.
I think i need some help here.

[EDIT :
And about manual memory management, i'm used to manage everything by hand, and delete what i need when it needs to be deleted. Well maybe with this concept it can save me some times, seeing your example code Nexus)

3
Graphics / [SFFont] Valgrind is not happy when loading fonts from file
« on: October 31, 2013, 04:13:24 pm »
Hi all

I'm using SFML (2.1) for a personal project and i want everything to be as good as possible.
I'm using a class to load all of my ressources (images, fonts, sounds) at the program starts.
This class is a singleton. The problem is not with the class itself, but with the "loadFromFile" method, in the sf::Font class.
Whenever i have a sf::Font attribute in my class, the code randomly crashes.

I already had this issue and I fixed it by doing something a little bit tricky (Here is the old code) :
class Blob
{
private:
sf::Font *_font;

public:
Blob();
virtual ~Blob();
};
// Now the CPP source file

Blob::Blob()
{
_font = new sf::Font();
font->loadFromFile("arial.ttf");
}
 
Doing this makes the code crash.
Here is the fix :
// Header file
class Blob
{
private:
static sf::Font _font;

public:
// Ctor / Dtor
void init();
};

// CPP Source code

Blob::Blob()
{
}

void Blob::init()
{
_font.loadFromFile("arial.ttf");
}

sf::Font Blob::_font;
 

Now, about the real problem.
I have a FontLoader class, which is supposed to load all the fonts needed in the whole program.
It contains a std::map<std::string, sf::Font>.
Then when i want a font, i just have to do "FontLoader->getFont("NAME"), what returns the case in the map corresponding to "NAME"

But seeing the problem i show before, this cannot work !
Actually, it doesnt. I'm having a malloc() memory corruption when trying to allcate an object of type sf::Font
See the code here :
(Singleton is just a way to make my class global, available for all the other class)

class                           SFFontLoader : public Singleton<SFFontLoader>
{
  friend class Singleton<SFFontLoader>;

private:
  std::map<std::string, sf::Font *>       _fontMap;

public:
  void                  loadFont(std::string const &path, std::string const &name);
  sf::Font              &getFont(std::string const &name) {return *_fontMap[name];};

private:
  SFFontLoader();
  virtual ~SFFontLoader();

};
 

And the CPP file
#include                        "SFFontLoader.hh"

SFFontLoader::SFFontLoader()
{
}

SFFontLoader::~SFFontLoader()
{
}

void                            SFFontLoader::loadFont(std::string const &path,
                                                       std::string const &name)
{
  _fontMap[name] = new sf::Font;
  if (!(_fontMap[name]->loadFromFile(path)))
    std::cerr << "Error : failed to load font \"" << path << "\"" << std::endl;
}
 

And valgrind's console :
==5936== Invalid write of size 8
==5936==    at 0x5266038: std::vector<unsigned char, std::allocator<unsigned char> >::_M_fill_insert(__gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char, std::allocator<unsigned char> > >, unsigned long, unsigned char const&) (in /usr/lib/libsfml-graphics.so.2.1)
==5936==    by 0x5264D75: sf::Font::loadGlyph(unsigned int, unsigned int, bool) const (in /usr/lib/libsfml-graphics.so.2.1)
==5936==    by 0x526540D: sf::Font::getGlyph(unsigned int, unsigned int, bool) const (in /usr/lib/libsfml-graphics.so.2.1)
==5936==    by 0x5285BE9: sf::Text::updateGeometry() (in /usr/lib/libsfml-graphics.so.2.1)
==5936==    by 0x40C079: SFLabel::setText(std::string const&) (SFLabel.cpp:25)
==5936==    by 0x403100: main (main.cpp:52)
==5936==  Address 0xce07828 is 0 bytes after a block of size 104 alloc'd
==5936==    at 0x4C2C7A7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5936==    by 0x40BAA8: SFFontLoader::loadFont(std::string const&, std::string const&) (SFFontLoader.cpp:24)
==5936==    by 0x40B6DE: SFRessourcesManager::loadDefaultRessources() (SFRessourcesManager.cpp:30)
==5936==    by 0x402DAD: main (main.cpp:27)
 
(Have fun reading this, the only usefull thing is at the top)

I have a solution to make this work :
Instead of map<string, sf::Font *>, juste do map<strring, sf::Font> then remove the new sf::Font line.
This works, the font appears, not memory corruption, but valgrinds yeld me some strange things (i think it tells me there's a problem when accessing to the _fontMap[name], well i actually have no idea why)

So, can anyone tell me why i cannot allocate an object of type sf::Font ?
Sorry, the post is a bit long but i wanted to be as accurate as possible.

Thanks for your help.

4
Graphics / Something wrong with sfml::Text ?
« on: July 05, 2013, 04:25:26 pm »
Hi all
I'm actually having a little (but really anoying) problem.

I have my own class to draw a sprite on sceen, instead of using the sf::Sprite. This works pretty fine.
I wanted to add some text on my screen, so i simply created a sfml::Text object then drew it, as i do with other drawables items.

This works, no problem. But i wanted to add something else :
Moving my scene. I working in a 2D scene (i mean, i don't use gluOrtho or whatever to draw in 2D, i just use the draw method of sf::RenderWindow)
So, to move the scene, i use glTranslatef(), wich works pretty fine, as far as i don't draw the text on the screen.
Well, it's a little bit hard to explain what's happening.

In one of my function, i first draw my sprite, then some text over it. Simply drawing the text after my own sprite makes glTranslatef do nothing.
And the most strange thing is that if i draw the text before my sprite, glTranslatef() works fine, and the world is moving (but the text is under my sprite so it doesn't appear)

What the hell ?

Here is the draw() function of my sprite class :
void                    FixedSprite::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
  if (_texture)
    {
        states.texture = _texture;
        states.transform *= getTransform();
        target.draw(_vertex, 4, sf::Quads, states);
    }
}
 
I have _texture wich is a pointer on a texture i load before.
_vertex is an array of 4 sf::Vertex

Do you have any idea why drawing the text after drawing the sprite make glTranslatef() do nothing ?
Or do you have a better way to move the whole scene, without having to go through all my objects and moving'em one by one ?

Thanks.

Pages: [1]