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

Author Topic: Sprite is drawn as a rect  (Read 2912 times)

0 Members and 1 Guest are viewing this topic.

Wurstinator

  • Newbie
  • *
  • Posts: 33
    • View Profile
Sprite is drawn as a rect
« on: August 12, 2010, 01:56:12 pm »
Hi,
I have a problem with SFML 1.6.
I have this image:


Which is loaded and used with a Sprite. But for some reason, the image is not displayed as it is but as a white rect:
http://img64.imageshack.us/img64/7966/screenxe.png

My class:
Code: [Select]
#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);
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sprite is drawn as a rect
« Reply #1 on: August 12, 2010, 02:16:42 pm »
We need to see more code, and particularly how the Button instances are stored. I think your problem is related to what is described at the end of the sprite tutorial.
Laurent Gomila - SFML developer

Wurstinator

  • Newbie
  • *
  • Posts: 33
    • View Profile
Sprite is drawn as a rect
« Reply #2 on: August 12, 2010, 04:01:31 pm »
Okay, here are the header and cpp files where Button is used:
Code: [Select]
#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);
    }
}


Code: [Select]
#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


Quote
I think your problem is related to what is described at the end of the sprite tutorial.

Image and Sprite management?  :?:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sprite is drawn as a rect
« Reply #3 on: August 12, 2010, 04:07:10 pm »
Quote
Image and Sprite management?

Absolutely ;)

As soon as you copy an image (which is what you do), the sprites that are using this image need to be updated to point to the new copy rather than keeping a reference to the old one, which is probably already destroyed.
Laurent Gomila - SFML developer

Wurstinator

  • Newbie
  • *
  • Posts: 33
    • View Profile
Sprite is drawn as a rect
« Reply #4 on: August 12, 2010, 04:50:56 pm »
Quote
As soon as you copy an image (which is what you do)

:shock: In what method?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sprite is drawn as a rect
« Reply #5 on: August 12, 2010, 04:54:21 pm »
Here:
Code: [Select]
button_list[0] = Button(button_p1_pos_x, button_p1_pos_y, button_p1_filename);
You construct a temporary Button instance and you copy (assign) it to button_list[0]. And copying a Button instance copies all its members, including the image.
Laurent Gomila - SFML developer

Wurstinator

  • Newbie
  • *
  • Posts: 33
    • View Profile
Sprite is drawn as a rect
« Reply #6 on: August 12, 2010, 09:51:29 pm »
Oh, ok. I forgot that assigning values to an array also copies the objects.

(I'm one of these "n00bz" in C++ ;)