i'm trying create classes containing textures&sprites using sfml. when create object of class in sfml library in class - cannot use object in main. wonder how display sprite in main (e.g.):
class mainmenu { public: int drawmenu(){ sf::texture texture; if (!texture.loadfromfile("idle.png")) return exit_failure; sf::sprite spritemenu(texture); return 0; } }; int main() { // create main window sf::renderwindow app(sf::videomode(800, 600), "sfml window"); // load sprite display sf::texture texture; if (!texture.loadfromfile("cb.bmp")) return exit_failure; sf::sprite sprite(texture); mainmenu menu; // start game loop while (app.isopen()) { // process events sf::event event; while (app.pollevent(event)) { // close window : exit if (event.type == sf::event::closed) app.close(); } // clear screen app.clear(); // draw sprite app.draw(sprite); menu.drawmenu(); app.draw(spritemenu); // update window app.display(); } return exit_success; }
your drawmenu function name lying. doesn't draw anything. loads texture , sprite.
drawmenu should draw menu sprite:
void drawmenu(sf::renderwindow& window) { // window = draw menu window.draw(spritemenu); } now load spritemenu? stays same through lifetime of mainmenu, should naturally instantiated in constructor of mainmenu:
class mainmenu { public: mainmenu() { if (!texture.loadfromfile("cb.bmp")) abort(); // or throw exception spritemenu.settexture(texture); // "initialize" spritemenu texture } … private: sf::texture texture; // store these member data, sf::sprite spritemenu; // kept alive through lifetime of mainmenu. }; now when mainmenu menu; texture , sprite initialized once, , not every time call draw function.
and when need draw menu, call menu.drawmenu(app); instead of menu.drawmenu(); app.draw(spritemenu);.
Comments
Post a Comment