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

Author Topic: simple question from a noob, why doesn't my window appear?  (Read 172 times)

0 Members and 1 Guest are viewing this topic.

alex_h

  • Newbie
  • *
  • Posts: 1
    • View Profile
simple question from a noob, why doesn't my window appear?
« on: December 22, 2024, 02:18:01 pm »
Hello, 

I'm starting to learn C++ and SFML.

I just start  a new program, no errors during compilation, but no window appears during the execution.
Normally, a window with red background should appear.
I don't understand why this isn't the case.

What am I doing wrong?
Thanks for your help !

This my code:

game.hpp

#ifndef GAME_HPP
#define GAME_HPP

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

class Game
{
private:
  sf::RenderWindow *window;

  void initWindow();

public:
  Game();

  virtual ~Game();

  void run();

  void update();
  void render();
};

#endif // GAME_HPP
 

game.cpp
#include "game.hpp"

Game::Game()
{
  this->initWindow();
};

Game::~Game()
{
  delete this->window;
};

// Private functions
void Game::initWindow()
{
  this->window = new sf::RenderWindow(sf::VideoMode(800, 600), "Game", sf::Style::Close | sf::Style::Titlebar);
}

// Public functions
void Game::run()
{
  while (this->window->isOpen())
  {
    this->update();
    this->render();
  }
}

void Game::update()
{
}

void Game::render()
{
  // Clear
  this->window->clear(sf::Color::Red);

  // Display
  this->window->display();
}
 

And on main
#include "game.hpp"

int main()
{
  Game game;
  game.run();

  return EXIT_SUCCESS;
}

 


Deedolith

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Hello,

After compiling your code (Visual Studio 2022, C++20 enabled), the window show up as expected.
Are you sure your window isn't showing up on the background, or out of the screen or on a secondary screen ?

Additional remarks:
In modern C++, we do not like raw pointers, they are very problematic (a simple search on Google will tell you why).
Use automatic instances instead (I do not see any reasons to use pointers in the sample you provided) and initialiser lists in your constructor (the initWindow() member function is unneeded, so is the destructor).
Also, unless you need to disambiguate, the this pointer is superfluous and harm readability.
Finally, prefer brace initialisation as it provide stronger types checking from the compiler.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11053
    • View Profile
    • development blog
    • Email
I also don't see anything wrong with the given code either.

If you use shared libraries, make sure that the DLLs are put next to the executable.
Maybe try to start the application from Explorer, if a DLL is missing, you should get a proper error message there.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything