Witam.
Otóż mam pewien problem z wyświetlaniem sprite'ów w bibliotece sfml.
Tworzę sobie właśnie menu, mam w tym celu 2 klasy:
cMenu - klasa od menu
cButton - klasa od przycisków
Nie są one jak na razie zbyt rozwinięte, bo to dopiero początek.
W klasie cMenu mam metodę AddButton:
void cMenu::AddButton( short x, short y, Mode dest, const std::string &adres)
{
if ( dest == GameMode )
{
tab.push_back(cButton());
tab.back().SetButton( x, y, dest, adres);
}
}
Oto funkcja SetButton:
void cButton::SetButton( short x, short y, Mode dest, const std::string &adres )
{
i_gfx.LoadFromFile( adres );
gfx.SetImage(i_gfx);
gfx.SetPosition( x, y);
ButtonDest = dest;
}
Problem jest taki, iż gdy dodaję za pomocą AddButton np. 4 przyciski, poprawnie wyświetla się tylko ten dodany na końcu, reszta ma wymiary poprawne, lecz jest to puste, białe pole. Gdy próbowałem użyć w AddButton konstruktora z argumentami, który działał tak jak funkcja SetButton, wszystkie wtedy były pustymi polami.
Gdy przycisk jest dodawany standardowo, z konstruktora klasy cButton nie ma z nim żadnych problemów, ale gdy chcę dodać z poziomu klasy cMenu, wtedy pojawiają się problemy.
Teraz trochę kombinowałem i zauważyłem jedną rzecz.
Na początku dodaję tylko jeden przycisk(za pomocą AddButton). Oczywiście działa bardzo dobrze. Po wciśnięciu na ten przycisk sprawiłem, że inicjuję drugi.
Wynik: O ile na początku przycisk wyświetlał się normalnie, to po pojawieniu się nowego przycisku ten pierwszy zamienił się w puste pole.
Doszedłem do wniosku, iż błąd leży właśnie w metodzie AddButton, ale nie wiem co takiego robię źle.
Oto pełny kod:
//menu.h
#ifndef _MENU_H
#define _MENU_H
#include <iostream>
#include <SFML\Graphics.hpp>
extern sf::RenderWindow App;
extern sf::Event Event;
extern const sf::Input & Input;
enum Mode { M_MENU, M_GAME, M_SETTINGS, M_INFO };
enum Buttons{ B_MENU, B_GAME, B_SETTINGS, B_INFO };
class cButton
{
//private
short x, y;
sf::Image i_gfx;
sf::Sprite gfx;
Mode ButtonDest;
public:
cButton();
cButton( short x, short y, Mode dest, const std::string &adres );
void SetButton(short x, short y, Mode dest, const std::string &adres);
void DrawButton();
bool IsButtonPressed();
};
class cMenu
{
short tx, ty;
Mode tdest;
std::string tadres;
public:
std::vector <cButton> tab;// na czas testów jest publiczny, później będzie prywatny
void AddButton( short x, short y, Mode dest, const std::string &adres);
void DrawMenu();
cMenu();
};
#endif
//menu.cpp
#include "menu.h"
extern Mode GameMode;
cButton::cButton()
{
x;
y;
i_gfx;
gfx;
ButtonDest;
}
cButton::cButton( short x, short y, Mode dest, const std::string &adres )
{
//if (dest == GameMode)
//{
i_gfx.LoadFromFile( adres );
gfx.SetImage(i_gfx);
gfx.SetPosition( x, y);
//}
ButtonDest = dest;
}
void cButton::DrawButton()
{
if( ButtonDest == GameMode ) App.Draw(gfx);
}
bool cButton::IsButtonPressed()
{
if( Input.IsMouseButtonDown( sf::Mouse::Left) )
{
sf::Vector2f point = gfx.TransformToLocal( sf::Vector2f(( float ) Input.GetMouseX(),( float ) Input.GetMouseY() ) );
if( point.x >= 0 && point.y >= 0 && point.x <= i_gfx.GetWidth() && point.y <= i_gfx.GetHeight() )
{
return true;
}
}
}
void cButton::SetButton( short x, short y, Mode dest, const std::string &adres )
{
i_gfx.LoadFromFile( adres );
gfx.SetImage(i_gfx);
gfx.SetPosition( x, y);
ButtonDest = dest;
}
cMenu::cMenu()
{
std::vector <cButton> tab;
}
void cMenu::AddButton( short x, short y, Mode dest, const std::string &adres)
{
if ( dest == GameMode )
{
tab.push_back(cButton());
tab.back().SetButton( x, y, dest, adres);
}
}
void cMenu::DrawMenu()
{
std::vector <cButton>::iterator it;
for( it=tab.begin(); it!=tab.end(); ++it )
{
it->DrawButton();
}
}
///////////////////////////////////
// //
// Project :) //
// //
// //
// By blojayble //
// //
// main.cpp //
// //
///////////////////////////////////
#include <SFML\Graphics.hpp>
#include "menu.h"
sf::RenderWindow App(sf::VideoMode(960, 800, 32), "Junk Warz", sf::Style::Close);
sf::Event Event;
const sf::Input & Input = App.GetInput();
Mode GameMode = M_MENU;
void main()
{
App.SetFramerateLimit(60);
cMenu(Menu);
cButton(Button);
Menu.AddButton(350, 100, M_MENU , "Resources/Menu/gfxGame.PNG" );
//Menu.AddButton(350, 400, M_MENU , "Resources/Menu/gfxSettings.PNG" );
//Menu.AddButton(350, 550, M_MENU , "Resources/Menu/gfxInfo.PNG" );
//Menu.AddButton(350, 700, M_MENU , "Resources/Menu/gfxExit.PNG" );
while(App.IsOpened() )
{
App.Clear(sf::Color(255, 255, 2));
while(App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed) App.Close();
if ( Menu.tab[0].IsButtonPressed() == true)
{
Menu.AddButton(350, 700, M_MENU , "Resources/Menu/gfxExit.PNG" );
}
}
Menu.DrawMenu();
App.Display();
}
}
Być może napisałem to wszystko chaotycznie, ale jestem już nieco zmęczony.
Z góry dziękuję za wszelką pomoc.
Pozdrawiam.