Since you're in for learning things, here's how I would refactor the posted code:
Splash.hpp#ifndef SPLASH_HPP
#define SPLASH_HPP
#include <SFML/Graphics.hpp>
#include <iostream>
class Splash : public sf::Drawable
{
public:
Splash();
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
private:
sf::Sprite m_sprite;
sf::Texture m_texture;
sf::Vector2u m_screenSize;
sf::Vector2u m_windowSize;
};
#endif // SPLASH_HPP
Spalsh.cpp#include "Splash.hpp"
Splash::Splash()
: m_sprite(m_texture)
, m_screenSize(sf::VideoMode::getDesktopMode().width, sf::VideoMode::getDesktopMode().height)
, m_windowSize(800, 600)
{
m_sprite.setPosition(((m_screenSize.x / 2.f) - m_windowSize.x) / 2.f, (m_screenSize.y - m_windowSize.y) / 2.f);
if (!m_texture.loadFromFile("resources/Splash.png", sf::IntRect(0, 0, m_windowSize.x, m_windowSize.y)))
{
std::cout << "Could not load texture." << std::endl;
}
}
void Splash::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(m_sprite, states);
}
main.cpp
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include "Splash.hpp"
int main()
{
Splash splash;
sf::ContextSettings contextSettings;
contextSettings.depthBits = 24;
sf::Vector2u screenSize(sf::VideoMode::getDesktopMode().width, sf::VideoMode::getDesktopMode().height);
sf::RenderWindow mainWin(sf::VideoMode(screenSize.x / 2.f, screenSize.y), "Main Window", sf::Style::Default, contextSettings);
mainWin.setVerticalSyncEnabled(true);
mainWin.setPosition(sf::Vector2i(0, 0));
while (mainWin.isOpen())
{
sf::Event event;
while (mainWin.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed)
mainWin.close();
// Escape key: exit
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
mainWin.close();
if(event.type == sf::Event::MouseMoved)
std::cout << "x=" << event.mouseMove.x << ", y=" << event.mouseMove.y << std::endl;
}
// Clear the depth buffer
glClear(GL_DEPTH_BUFFER_BIT);
mainWin.clear(sf::Color(35,44,69,255));
mainWin.draw(splash);
// Finally, display the rendered frame on screen
mainWin.display();
}
}
Here a few comments on what I changed:
- Splash is a draw able, don't "extract" a sprite and draw that, but draw the Splash object directly.
- When you deal with two "connected" numbers, use a vector for them and pick the right type.
- Make use of a header and source file.
- Use the class' initialization list to initialize member variables.
- Use the constructor to directly load the texture and set the sprite's position
One thing I don't understand is your OpenGL code to clear the depth buffer. What's that for?