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

Author Topic: Problem with drawing outside of RenderWindow init function.  (Read 3399 times)

0 Members and 1 Guest are viewing this topic.

Code_Machine

  • Newbie
  • *
  • Posts: 13
    • View Profile
EDIT: the problem is segmentation faults... When I go over the box that is the sprites.
The sprites are simply 100x30 boxes that sit on each other. While I go over the box it draws the second box that's a different color, and I have another one that when I click it, it turns a different color.

Hi, I have a RenderWindow variable that's being pretty annoying, or it could, of course, be my fault. This variable is initialized in main(), but it's acting up when I try to use its functions outside of main.

I would like someone to look over it and see what's wrong. I'll print the two versions that I have -- outside of main and inside of main.

inside of main
Code: [Select]

#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
#define ONE "/home/ken/Documents/SFMLGOD/test/menu/clip4menu/bluetest.png"
#define TWO "/home/ken/Documents/SFMLGOD/test/menu/clip4menu/greentest.png"
#define THREE "/home/ken/Documents/SFMLGOD/test/menu/clip4menu/purpletest.png"
#define POSX 300
#define POSY 200




int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Test for the button etc");


sf::Image One;
sf::Image Two;
sf::Image Three;
std::vector<sf::Sprite> bSprite(3);


if(!One.LoadFromFile(ONE))
{
std::cout << ONE << std::endl;
return EXIT_FAILURE;
}
if(!Two.LoadFromFile(TWO))
{
std::cout << TWO << std::endl;
return EXIT_FAILURE;
}
if(!Three.LoadFromFile(THREE))
{
std::cout << THREE << std::endl;
return EXIT_FAILURE;
}


bSprite[0].SetImage(One);
bSprite[1].SetImage(Two);
bSprite[2].SetImage(Three);


bSprite[0].SetPosition(POSX, POSY);
bSprite[1].SetPosition(POSX, POSY);
bSprite[2].SetPosition(POSX, POSY);


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




App.Clear();


sf::Mouse::Button b;

if(App.GetInput().GetMouseX() >
bSprite[1].GetPosition().x &&
App.GetInput().GetMouseX() <
bSprite[1].GetPosition().x + 100 &&
App.GetInput().GetMouseY() >
bSprite[1].GetPosition().y  &&
App.GetInput().GetMouseY() <
bSprite[1].GetPosition().y + 30 &&
App.GetInput().IsMouseButtonDown(b) == true ) //end
App.Draw(bSprite[2]);


else if(App.GetInput().GetMouseX() > bSprite[1].GetPosition().x && App.GetInput().GetMouseX() < bSprite[1].GetPosition().x + 100 && App.GetInput().GetMouseY() > bSprite[1].GetPosition().y  && App.GetInput().GetMouseY() < bSprite[1].GetPosition().y + 30 )
App.Draw(bSprite[1]);


else
App.Draw(bSprite[0]);
App.Display();

}

return EXIT_SUCCESS;
}


outside of main
Code: [Select]

#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
#define ONE "/home/ken/Documents/SFMLGOD/test/menu/clip4menu/bluetest.png"
#define TWO "/home/ken/Documents/SFMLGOD/test/menu/clip4menu/greentest.png"
#define THREE "/home/ken/Documents/SFMLGOD/test/menu/clip4menu/purpletest.png"
#define POSX 300
#define POSY 200




void add(sf::Image*, int, std::vector<sf::Sprite> &);
int check(sf::RenderWindow*, std::vector<sf::Sprite> &);


int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Test for the button etc");


sf::Image One;
sf::Image Two;
sf::Image Three;
std::vector<sf::Sprite> bSprite(3);


if(!One.LoadFromFile(ONE))
{
std::cout << ONE << std::endl;
return EXIT_FAILURE;
}
if(!Two.LoadFromFile(TWO))
{
std::cout << TWO << std::endl;
return EXIT_FAILURE;
}
if(!Three.LoadFromFile(THREE))
{
std::cout << THREE << std::endl;
return EXIT_FAILURE;
}



add(&One, 0, bSprite);
add(&Two, 1, bSprite);
add(&Three, 2, bSprite);


bSprite[0].SetPosition(POSX, POSY);
bSprite[1].SetPosition(POSX, POSY);
bSprite[2].SetPosition(POSX, POSY);



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



int x;

App.Clear();
x = check(&App, bSprite);
if( x != 1)
{
App.Draw(bSprite[0]);
App.Display();
}
}

return EXIT_SUCCESS;
}



void add(sf::Image *b, int i, std::vector<sf::Sprite> & bSprite)
{


bSprite[i].SetImage(*b);

}

int check(sf::RenderWindow * g, std::vector<sf::Sprite> & bSprite)
{

sf::Mouse::Button b;

if(g->GetInput().GetMouseX() >
bSprite[1].GetPosition().x &&
g->GetInput().GetMouseX() <
bSprite[1].GetPosition().x + 100 &&
g->GetInput().GetMouseY() >
bSprite[1].GetPosition().y  &&
g->GetInput().GetMouseY() <
bSprite[1].GetPosition().y + 30 &&
g->GetInput().IsMouseButtonDown(b) == true )
{
g->Draw(bSprite[2]);
return 1;
}

if(g->GetInput().GetMouseX() > bSprite[1].GetPosition().x && g->GetInput().GetMouseX() < bSprite[1].GetPosition().x + 100 && g->GetInput().GetMouseY() > bSprite[1].GetPosition().y  && g->GetInput().GetMouseY() < bSprite[1].GetPosition().y + 30 )
{
g->Draw(bSprite[1]);
return 1;
}

return 0;
}

Thanks in advance :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with drawing outside of RenderWindow init function.
« Reply #1 on: June 08, 2011, 04:06:26 pm »
And what's your problem with the second code?
Laurent Gomila - SFML developer

Code_Machine

  • Newbie
  • *
  • Posts: 13
    • View Profile
Problem with drawing outside of RenderWindow init function.
« Reply #2 on: June 09, 2011, 07:50:44 am »
It seg faults. Could this be my machine?

Is it possible that it's because I'm not limiting my frames and sometimes achieves idk 1k fps?

If so, how do I manipulate max frame rate?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with drawing outside of RenderWindow init function.
« Reply #3 on: June 09, 2011, 07:53:53 am »
Quote
It seg faults

Where? (hint: use the debugger)
Laurent Gomila - SFML developer

Code_Machine

  • Newbie
  • *
  • Posts: 13
    • View Profile
Problem with drawing outside of RenderWindow init function.
« Reply #4 on: June 10, 2011, 10:43:03 am »
I'm not totally sure on how to use the debugger. My output does tell me where the problem lies... unfortunately I don't know what really to do with it.

Quote
(gdb) run
Starting program: /home/ken/Documents/SFMLGOD/test/menu/a.out
[Thread debugging using libthread_db enabled]

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff78af693 in sf::Input::IsMouseButtonDown(sf::Mouse::Button) const ()
   from /usr/local/lib/libsfml-window.so.1.6
(gdb) continue
Continuing.

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.


As you can see it's coming from the window library? On top of that, it's coming from my IsMouseButtonDown() function. I thought I used this properly. I'm a bit new to classes and their members and how to really manipulate/use them.

^obvious haha

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with drawing outside of RenderWindow init function.
« Reply #5 on: June 10, 2011, 01:35:33 pm »
After the crash, type "bt" in gdb, that will print the call stack.
Laurent Gomila - SFML developer

azurfire

  • Newbie
  • *
  • Posts: 6
    • View Profile
Problem with drawing outside of RenderWindow init function.
« Reply #6 on: June 11, 2011, 12:15:52 am »
I think the issue is that you declare "sf::Mouse::Button b", but you never initialize it before you use it, so it contains a garbage value that causes IsMouseButtonDown to crash.

You should instead do something like this:
Code: [Select]
sf::Mouse::Button b(sf::Mouse::Left);
or this:
Code: [Select]
...App.GetInput().IsMouseButtonDown(sf::Mouse::Left)

The reason it doesn't crash until you move your mouse over the sprite is because of "short-circuit evaluation".

Code_Machine

  • Newbie
  • *
  • Posts: 13
    • View Profile
Problem with drawing outside of RenderWindow init function.
« Reply #7 on: June 11, 2011, 10:38:12 pm »
Actually, not the solution to my problem. If you see the code that is using only main, that works. I thank that you showed me how to use that constructor. I will have more time tonight to actually mess around with the library, and just going to go str8 to other code I will get to the debugger too...

 

anything