0 Members and 1 Guest are viewing this topic.
#include "state_main_menu.hpp"State_Main_Menu::Button::Button() {}State_Main_Menu::Button::Button(int cx, int cy, std::string filename) : center_x(cx), center_y(cy), image_filename(filename){ image.LoadFromFile(image_filename); sprite.SetImage(image);}State_Main_Menu::Button::~Button() {}std::string State_Main_Menu::Button::get_image_filename(){ return image_filename;}void State_Main_Menu::Button::set_image_filename(std::string new_filename){ image_filename = new_filename; image.LoadFromFile(image_filename);}bool State_Main_Menu::Button::is_mouse_over_button(int x, int y){ sf::Vector2f pos(sprite.GetPosition()); sf::Color color(sprite.GetPixel(x-pos.x, y-pos.y)); return (color.a > 0);}int State_Main_Menu::Button::get_x(){ return (center_x - image.GetWidth() / 2);}int State_Main_Menu::Button::get_y(){ return (center_y - image.GetHeight() / 2);}void State_Main_Menu::Button::draw_update(sf::RenderWindow* window){ sprite.SetX(get_x()); sprite.SetY(get_y()); window->Draw(sprite);}
#include "state_main_menu.hpp"namespace{ // Coordinates of the Buttons (define center point) const int button_p1_pos_x(269); const int button_p1_pos_y(126); const int button_p2_pos_x(309); const int button_p2_pos_y(126); const int button_opt_pos_x(263); const int button_opt_pos_y(173); const int button_inst_pos_x(309); const int button_inst_pos_y(173); const int button_exit_pos_x(389); const int button_exit_pos_y(220); // Filenames of the Buttons const std::string button_p1_filename("media/mainmenu_1pbutton.png"); const std::string button_p2_filename("media/mainmenu_2pbutton.png"); const std::string button_opt_filename("media/mainmenu_optionsbutton.png"); const std::string button_inst_filename("media/mainmenu_instructionsbutton.png"); const std::string button_exit_filename("media/mainmenu_endgamebutton.png");}State_Main_Menu::State_Main_Menu(AppManager& appmanager) : State_Base(appmanager){ button_list[0] = Button(button_p1_pos_x, button_p1_pos_y, button_p1_filename); // button_list[1] = Button(button_p2_pos_x, button_p2_pos_y, button_p2_filename); // button_list[2] = Button(button_opt_pos_x, button_opt_pos_y, button_opt_filename); // button_list[3] = Button(button_inst_pos_x, button_inst_pos_y, button_inst_filename); // button_list[4] = Button(button_exit_pos_x, button_exit_pos_y, button_exit_filename);}State_Main_Menu::~State_Main_Menu() {}void State_Main_Menu::update_event(){ sf::Event event; for (unsigned int n = 0; n < app_manager.event_list.size(); n++) { event = app_manager.event_list[n]; if (event.Type == sf::Event::Closed) exit_state(); }}void State_Main_Menu::update(){}void State_Main_Menu::draw(){ for (int n = 0; n<1; n++) { button_list[n].draw_update(app_manager.render_window); }}
#ifndef STATE_MAIN_MENU_HPP_INCLUDED#define STATE_MAIN_MENU_HPP_INCLUDED#include "state_base.hpp"class State_Main_Menu : public State_Base{ public: State_Main_Menu(AppManager& appmanager); ~State_Main_Menu(); void update_event(); void update(); void draw(); private: // ** Button ** class Button { public: Button(); Button(int cx, int cy, std::string filename); ~Button(); int center_x; int center_y; std::string get_image_filename(); void set_image_filename(std::string new_filename); bool is_mouse_over_button(int x, int y); void draw_update(sf::RenderWindow* window); private: int get_x(); int get_y(); std::string image_filename; sf::Sprite sprite; sf::Image image; }; // ** Button End ** Button button_list[5];};#endif // STATE_MAIN_MENU_HPP_INCLUDED
I think your problem is related to what is described at the end of the sprite tutorial.
Image and Sprite management?
As soon as you copy an image (which is what you do)
button_list[0] = Button(button_p1_pos_x, button_p1_pos_y, button_p1_filename);