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

Author Topic: Avoiding the sf::String bug with an array  (Read 6534 times)

0 Members and 1 Guest are viewing this topic.

7eventh

  • Newbie
  • *
  • Posts: 9
    • MSN Messenger - jan.keromnes@free.fr
    • View Profile
    • http://7eventh.free.fr
Avoiding the sf::String bug with an array
« on: July 15, 2009, 08:48:18 pm »
Hullo.

I'm using the sf::String class, and I get the known Access Violation bug.
I know it comes from the default font destructor, so I'm searching a solution to avoid this problem.

The only thing that's making trouble is that I use an array :
Code: [Select]
sf::String text[100];

The constructor of my GameClass didn't seem to like for loops, so I decided to use pointers and allocation :

Code: [Select]
// This is the pointer array
sf::String* text[100];


// Later I allocate them :
sf::Font MyDefaultFont;
MyDefaultFont.LoadFromFile("default.ttf");
for(int i = 0; i < 100 ; i++)
{
text[i] = new sf::String("", MyDefaultFont);
}


// Everyting's fine this far, I do not forget to delete in the destructor of my GameClass :
for(int i = 0; i < 100 ; i++)
{
delete text[i];
}



The problem I have, and I'm sure it's a stupid one, is with the Draw function...

When I try window.Draw(text[0]), it says that I should use a reference and not a pointer, and when I try window.Draw(*text[0]), everything works fine... except the text is not drawn at all !

(I tried many other ways, creating my own derivated MyString class, but I can't use a custom font because there is no such constructor like font("myverycoolfont.ttf") - which would be very cool by the way, so I tried to create a MyFont class, but still the font wont be loaded from a file within the constructor, etc., etc...)

dunce

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Avoiding the sf::String bug with an array
« Reply #1 on: July 16, 2009, 05:37:41 am »
AFAIK the problem with the default font was eliminated in SFML-2.
At least the code below works fine:

Code: [Select]
#include <SFML/Graphics.hpp>
 
int main()
{
  sf::RenderWindow App(sf::VideoMode(800, 600), "SFML window");
  sf::String* text[10];
 
  for(int i = 0; i < 10 ; i++)
  {
    text[i] = new sf::String("");
    text[i]->SetText("Abracadabra");
    text[i]->SetPosition(100, i * 30);
  }

  while (App.IsOpened())
  {
    sf::Event Event;

    while (App.GetEvent(Event))
    {
      if (Event.Type == sf::Event::Closed)
      {
        App.Close();
      }
    }

    App.Clear();

    for (int j = 0; j < 10; j++)
    {
      App.Draw(*text[j]);
    }

    App.Display();
  }

  for(int i = 0; i < 10 ; i++)
  {
    delete text[i];
  }  
 
  return EXIT_SUCCESS;
}


In previous versions just avoid using the default font.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Avoiding the sf::String bug with an array
« Reply #2 on: July 16, 2009, 07:51:07 am »
Quote
how close is the 2.0 branch to release? still quite a ways no?

Yes, at least a few months.
Laurent Gomila - SFML developer

7eventh

  • Newbie
  • *
  • Posts: 9
    • MSN Messenger - jan.keromnes@free.fr
    • View Profile
    • http://7eventh.free.fr
Avoiding the sf::String bug with an array
« Reply #3 on: July 16, 2009, 07:30:42 pm »
Thanks a lot for your answers, but my problem remains.
When I use the code you gave, dunce, everything compiles neatly whithout any error or warning, but when I launch the application, the texts don't show up.

I'll give you my C++ codes (some comments are in french, sorry about that). Here is my main.cpp :


Code: [Select]
//main.cpp


// Les Includes de SFML
#include <iostream>

// L'Include de Snake
#include "Game.h"


// La fonction principale du programe
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
//int main(int argc, char **argv)
//int main()
{
Game SnSt2Game;

SnSt2Game.init();

SnSt2Game.loop();

std::cout << "Loop ended !" << std::cout;

    return EXIT_SUCCESS;
}



Code: [Select]
//Game.h

// Defines
#define MAX_GAMEMODES 50

// Protection contre les inclusions multiples
#ifndef GAME_H
#define GAME_H

#include <iostream>
#include <fstream>
#include <SFML/Graphics.hpp>
#include "GameMode.h"



class Game
{

public:

// Méthodes
Game();
~Game();
void init();
void loop();

protected:

// Attributs
sf::RenderWindow window;
GameMode *gamemode[TOTALWORKINGMODES];
int active_gamemode;
};





#endif


Code: [Select]
//Game.cpp


#include "Game.h"





Game::Game() : window(sf::VideoMode::GetDesktopMode(), "SnakeSteaks 2", sf::Style::Fullscreen)
{
}


void Game::init()
{
for(int i = 0 ; i < TOTALWORKINGMODES ; i++)
{
gamemode[i] = new GameMode(window, i);
gamemode[i]->init();
}

active_gamemode = 0;
}


void Game::loop()
{
while(active_gamemode != EXITMODE)
{
std::cout << "Looping..." << std::endl;

window.Clear(sf::Color(255,255,255));

// Handle events
int new_mode = gamemode[active_gamemode]->handle_events();

if(new_mode != DEFAULTMODE && active_gamemode != EXITMODE)
{
active_gamemode = new_mode;
}

gamemode[active_gamemode]->update();

gamemode[active_gamemode]->refresh();

if(!(window.IsOpened()))active_gamemode = EXITMODE;

std::cout << "Game Mode : " << active_gamemode << std::endl;

window.Display();
}


}


Game::~Game()
{
for(int i = 0 ; i < TOTALWORKINGMODES ; i++)
{
delete gamemode[i];
}
}




Code: [Select]
//GameMode.h

// Defines
#define MAXSAMEOBJECTS 100
#define TOTALWORKINGMODES 1

#define EXITMODE -2
#define DEFAULTMODE -1
#define MENUMODE 0

#define WINDOW_X float(window.GetWidth())
#define WINDOW_Y float(window.GetHeight())
#define PERCENT_X  * float(window.GetWidth()) / 100.f
#define PERCENT_Y  * float(window.GetHeight()) / 100.f

// Protection contre les inclusions multiples
#ifndef GAMEMODE_H
#define GAMEMODE_H

#include <SFML/Graphics.hpp>
#include <iostream>
//#include "MyString.h"


class GameMode
{

public:

// Méthodes
GameMode(sf::RenderWindow &refwindow, int new_id);
void init();
int handle_events();
void handle_event(sf::Event &events);
void update();
void refresh();
~GameMode();

protected:

// Attributs
int mode_id;
int game_mode;
std::string name;
sf::Image img_fond;
sf::Sprite fond;
sf::Image image[MAXSAMEOBJECTS];
sf::Sprite sprite[MAXSAMEOBJECTS];
sf::String *texte[MAXSAMEOBJECTS];
sf::Font fonte[MAXSAMEOBJECTS];
sf::RenderWindow &window;

};





#endif




Code: [Select]
//GameMode.cpp


#include "GameMode.h"



GameMode::GameMode(sf::RenderWindow &refwindow, int new_id) : window(refwindow)
{
fonte[0].LoadFromFile("res/img/default.ttf");

for(int i = 0; i < MAXSAMEOBJECTS ; i++)
{
texte[i] = new sf::String("", fonte[0]);
}

switch(new_id)
{
case MENUMODE:
name = "Menu";
break;

default:
std::cout << "Error : Undefined mode " << new_id << std::endl;
name = "Error";
break;
}

mode_id = new_id;
}



void GameMode::init()
{

switch(mode_id)
{
case MENUMODE:
std::cout << "Initialising Menu" << std::endl;
// Loading resources
if (!img_fond.LoadFromFile("res/img/fond.jpg"))
{
std::cout << "Problem loading image." << std::endl;
}
else
{
std::cout << "Loading successful !" << std::endl;
}

// Using resources
fond.SetImage(img_fond);
fond.Resize(WINDOW_X, WINDOW_Y);
//fond.SetPosition(0.f, 0.f);

window.Draw(fond);

texte[0]->SetText("Solo");
texte[0]->SetSize(20);
texte[0]->SetColor(sf::Color::Black);
texte[0]->SetPosition((5 PERCENT_X),(10 PERCENT_Y));

texte[1]->SetText("Multi");
texte[1]->SetSize(20);
texte[1]->SetColor(sf::Color::Black);
texte[1]->SetPosition((5 PERCENT_X),(50 PERCENT_Y));
break;

default:
break;
}
}


int GameMode::handle_events()
{
game_mode = DEFAULTMODE;

sf::Event events;
    while (window.GetEvent(events) && game_mode != EXITMODE)
{
        if (events.Type == sf::Event::Closed)
game_mode = EXITMODE;

handle_event(events);
}

if(game_mode == EXITMODE)window.Close();
if(!(window.IsOpened()))game_mode = EXITMODE;

return game_mode;
}


void GameMode::handle_event(sf::Event &events)
{

switch(mode_id)
{
case MENUMODE:
if (events.Type == sf::Event::KeyPressed)
{
// Esc or Q : Quitter
if (events.Key.Code == sf::Key::Escape || events.Key.Code == sf::Key::Q)
game_mode = EXITMODE;
}
break;

default:
break;

}
}


void GameMode::update()
{

switch(mode_id)
{
case MENUMODE:
std::cout << "Nothing to update" << std::endl;
break;

default:
break;
}
}


void GameMode::refresh()
{

switch(mode_id)
{
case MENUMODE:
std::cout << "Drawing menu background..." << std::endl;

window.Draw(fond);

for(int i = 0 ; i < 2 ; i++)
{
window.Draw(*texte[i]);
}

window.Draw(*texte[0]);

break;

default:
break;
}
}



GameMode::~GameMode()
{
for(int i = 0 ; i < MAXSAMEOBJECTS ; i++)
{
delete texte[i];
}
}




Thanks a lot if you can help me !

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Avoiding the sf::String bug with an array
« Reply #4 on: July 16, 2009, 07:41:34 pm »
This is a lot of code -> give the less you can.
(Code minimal complet / compilable | sorry  :P )
SFML / OS X developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Avoiding the sf::String bug with an array
« Reply #5 on: July 16, 2009, 08:00:19 pm »
Quote
Thanks a lot for your answers, but my problem remains.
When I use the code you gave, dunce, everything compiles neatly whithout any error or warning, but when I launch the application, the texts don't show up.

Did you use SFML 2.0?
Laurent Gomila - SFML developer

7eventh

  • Newbie
  • *
  • Posts: 9
    • MSN Messenger - jan.keromnes@free.fr
    • View Profile
    • http://7eventh.free.fr
Avoiding the sf::String bug with an array
« Reply #6 on: July 17, 2009, 12:50:08 pm »
No, I use the 1.5, since no other releases are available.

I gave the whole code because I don't know where my problem comes from. The text objects don't throw any errors, but they're not visible on my window App...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Avoiding the sf::String bug with an array
« Reply #7 on: July 17, 2009, 01:16:35 pm »
Well, what dunce told you is that the problem is solved in SFML 2.0. You can it with SVN (see the tutorial and wiki for more explanations).

There is another workaround, wichi is very simple: link to the static libraries of SFML (-s suffix).
Laurent Gomila - SFML developer

7eventh

  • Newbie
  • *
  • Posts: 9
    • MSN Messenger - jan.keromnes@free.fr
    • View Profile
    • http://7eventh.free.fr
Avoiding the sf::String bug with an array
« Reply #8 on: July 17, 2009, 07:38:40 pm »
I've solved my particular problem of text not drawing itself. It was a pretty stupid error in fact, my "default.ttf" file wasn't located in the "res/img" folder, but in "res/ttf" one...

Well, thanks a lot for your time, and talking about SFML 2.0, I'll wait for the official release builds. I've a very bad experience with building my sources alone (endless days and nights trying to solve never ending building errors, and never once I got a thing correctly compiled...) So thank you very much for your hint, but I think I'll just be patient ! ^^

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Avoiding the sf::String bug with an array
« Reply #9 on: July 18, 2009, 10:09:42 am »
Quote
I've a very bad experience with building my sources alone (endless days and nights trying to solve never ending building errors, and never once I got a thing correctly compiled...)

This is with other libraries; with SFML it's as simple as pressing the "build" button in your favorite IDE ;)
Laurent Gomila - SFML developer