4
« on: June 08, 2011, 04:03:54 pm »
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
#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
#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 :)