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

Author Topic: Sprite not displaying when used in a class  (Read 3962 times)

0 Members and 1 Guest are viewing this topic.

Bumrang

  • Newbie
  • *
  • Posts: 3
    • View Profile
Sprite not displaying when used in a class
« on: November 23, 2012, 06:07:02 am »
Hello!

I'm making a small pong-breakout crossover in pure OpenGL and in SFML, and I've hit a problem in both that has stopped my development for roughly a month already.

I first prototyped my ball entirely in main, so I had all of my updating and drawing in main. Everything worked perfectly so I created a Ball class and just copy pasted all of it with a few minor adjustments (I also did this for my OpenGL version of my game).

Suddenly, the ball wouldn't display. I added a sf::RectangleShape into my Ball class, and that did display, so I have narrowed it down to the texture not displaying. The same exact thing happened in my OpenGL program, and the StackOverflow thread has all of the information about that, if that would help.

Tthe source can be found here.

This is what it currently looks like:


note: that white square is the rectangle, not the ball

-warning, pagestretch-
Source:

Main.cpp
#include "Includes.hpp"

unsigned int Width = 800;
unsigned int Height = 600;

int main()
{    
         sf::RenderWindow Window (sf::VideoMode (Width, Height), "PongOut");
     
     sf::Font font;
     if (!font.loadFromFile("font.ttf"))
         return EXIT_FAILURE;
     sf::Text text("test", font, 12);
     text.setPosition (0 , Height - 15);
     
     Ball ball (&Window , Width, Height);
     
     
     sf::Clock clock;
     
     while (Window.isOpen())
     {
                 sf::Time DT = clock.restart();
                 float DeltaTime = DT.asSeconds();

                  // Process events
         sf::Event event;
         while (Window.pollEvent(event))
         {
             // Close window : exit
             if (sf::Keyboard::isKeyPressed (sf::Keyboard::Escape))
                 Window.close();
         }
         
         ball.Update(DeltaTime);
 
         // Clear screen
         Window.clear();
         
         Window.draw(text);
         
         ball.Draw();
         
         //display
         Window.display();
         }

         return EXIT_SUCCESS;
}
 

Ball.hpp:
#ifndef BALL
#define BALL

class Ball
{
        private:
        int x, y, vx, vy;
        sf::Texture texture;
        sf::Sprite sprite;

        sf::RectangleShape rect;

        sf::RenderWindow* Window_;

        unsigned int Width, Height;

        public:
        Ball(sf::RenderWindow* , unsigned int width, unsigned int height);
        void Update(float DeltaTime);
        void Draw();
};

#endif
 

Ball.cpp:
#include "Includes.hpp"

Ball::Ball(sf::RenderWindow* win , unsigned int width, unsigned int height)
{
        Window_ = win;

        Width = width; Height = height;

     if (!texture.loadFromFile("img/ball.png"))
         std::cout << "\nCould not load the ball texture.";
     sprite.setTexture (texture);
     
     float x = Width / 2;
     float y = Height / 2;
     
     int vx = 200;
     int vy = 200;
     
     rect.setSize (sf::Vector2f (50 , 50));
     rect.setPosition (0 , 0);
}

void Ball::Update(float DeltaTime)
{
                 sprite.setPosition (x , y);
}

void Ball::Draw()
{
             // Draw the sprite
         Window_->draw(sprite);
         Window_->draw(rect);
}
 

If this is too much of a page stretch just say so and I will link it instead  :)

Some general information:
  • Arch Linux 64-bit
  • I am using SFML 2 release candidate, straight from the Arch AUR.

Thank you for reading!

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Sprite not displaying when used in a class
« Reply #1 on: November 23, 2012, 07:58:13 am »
In the constructor:
     float x = Width / 2;
     float y = Height / 2;

These local variables go out of scope so later, when you do:

void Ball::Update(float DeltaTime)
{
         sprite.setPosition (x , y);
}

The x and y in the class have not been set to anything meaningful.

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Sprite not displaying when used in a class
« Reply #2 on: November 23, 2012, 08:14:15 am »
And there is more stuff that you define in the constructor but get lost when the constructor ends. This is a C++ related question, this means that google helps you better than we can :)
Here is something to help you on your way. Also www.cplusplus.com provides almost everything you should know about C++ ;)

Bumrang

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sprite not displaying when used in a class
« Reply #3 on: November 23, 2012, 08:29:18 am »
Wow, I'm completely shocked. How the hell did I miss that?

I'm completely ashamed of myself  :-[

Though that doesn't explain why my OpenGL version doesn't draw, but since that is not SFML related I'd rather not discuss that here.

Thank you!  ;D

(also I know what scope is, I'm not a complete beginner :D)
« Last Edit: November 23, 2012, 08:35:58 am by Bumrang »

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Sprite not displaying when used in a class
« Reply #4 on: November 23, 2012, 09:07:45 am »
Oh I'm sorry, didn't read through all your code and missed some things. Don't worry about asking C++ related questions here, if it's really not working out we will gladly help you out. I just wanted to say that google is quite efficient in such questions ;)

Correct what cire said. Non-defined variables get semi-random values. And don't forget that what is drawn last is on top.

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Sprite not displaying when used in a class
« Reply #5 on: November 24, 2012, 05:31:28 am »
Quote
Though that doesn't explain why my OpenGL version doesn't draw, but since that is not SFML related I'd rather not discuss that here.

Try adding the push/popGLstates. It's a need to have those when you use both SFML and OpenGL. However I saw nothing drawn with OpenGL in the sources.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

Bumrang

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sprite not displaying when used in a class
« Reply #6 on: November 24, 2012, 09:01:27 am »
Quote
Though that doesn't explain why my OpenGL version doesn't draw, but since that is not SFML related I'd rather not discuss that here.

Try adding the push/popGLstates. It's a need to have those when you use both SFML and OpenGL. However I saw nothing drawn with OpenGL in the sources.

void Ball::Draw()
{

        glUseProgram (shader);
                glUniformMatrix4fv(MVPid, 1, GL_FALSE, &Camera.GetMVP()[0][0]); // Send MVP to shader

                glActiveTexture (GL_TEXTURE0); // bind the ball texture
                glBindTexture (GL_TEXTURE_2D, ballTex);
                glUniform1i (sampler , 0);
                        glEnableVertexAttribArray (coord); // sending the vertex data
                                glBindBuffer(GL_ARRAY_BUFFER, vertBuf);
                                        glVertexAttribPointer(
                                                coord, // The attribute we want to configure
                                                2,                  // size
                                                GL_FLOAT,           // type
                                                GL_FALSE,           // normalized?
                                                0,                  // stride
                                                (void*)0            // array buffer offset
                                                );

                        glEnableVertexAttribArray (uv);
                                glBindBuffer (GL_ARRAY_BUFFER , uvBuf);
                                        glVertexAttribPointer (uv, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);

                // Draw the triangle !
                                glDrawArrays(GL_TRIANGLES, 0, ballVert.size());

                        glDisableVertexAttribArray(coord);
                        glDisableVertexAttribArray(uv);
}

?

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Re: Sprite not displaying when used in a class
« Reply #7 on: November 24, 2012, 09:51:51 am »
Your OpenGL version(from your stackoverflow link) doesn't seem to use SFML at all, so you can ignore masskillers advice.

As he says, it's only needed when mixing SFML and OpenGL calls(push/popGLstates is part of the SFML library).

 

anything