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!