SFML community forums

Help => General => Topic started by: Munchor on August 28, 2012, 04:51:31 pm

Title: No window appears
Post by: Munchor on August 28, 2012, 04:51:31 pm
Hi there everyone, first day doing SFML, here's my Game.cpp and my Game.hpp:

Game.cpp
Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

#include "Game.hpp"

#include <stdio.h>

void Game::setup() {
  title = "s";
  sf::RenderWindow window(sf::VideoMode(800, 600, 32), title.c_str());
  sf::CircleShape shape(100.f);
  shape.setFillColor(sf::Color::Green);

  start();
}

void Game::start()
{
  /* Handle game-related variables here */
 
  run();
}

void Game::run() {
  running = true;

  while (running) {
    update();
    render();
  }
}

void Game::update() {
  while (window.isOpen())
  {
    sf::Event event;
    while (window.pollEvent(event))
    {
      if (event.type == sf::Event::Closed)
        window.close();
    }
  }
}

void Game::render() {
  printf("Starting render...\n");
  window.clear();
  window.draw(shape);
  window.display();
}

int main(int argc, char *argv[]) {
  Game game;
  game.start();
  game.run();

  return 0;
}

Game.hpp
Code: [Select]
#ifndef PAXLURE_HPP
#define PAXLURE_HPP

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

#include <string>

using namespace std;

class Game {
  private:
    string title;
    sf::RenderWindow window;
    sf::CircleShape shape;

    bool running;

  public:
    void setup();
    void start();
    void run();
    void update();
    void render();
};

#endif

For some reason the window is not being drawn, but the ::render() function is being called. I run the program and the window just doesn't show up.

Any ideas why? Thank you in advance.
Title: Re: No window appears
Post by: eXpl0it3r on August 28, 2012, 04:57:30 pm
In setup() you create a temporary sf::RenderWindow object, thus you initialize the member window with the default constructor which doesn't really create the window.
See documentation:
Quote
Default constructor.

This constructor doesn't actually create the window, use the other constructors or call Create to do so.
Title: Re: No window appears
Post by: Munchor on August 28, 2012, 05:03:47 pm
In setup() you create a temporary sf::RenderWindow object, thus you initialize the member window with the default constructor which doesn't really create the window.
See documentation:
Quote
Default constructor.

This constructor doesn't actually create the window, use the other constructors or call Create to do so.

Mhm, I understand what's wrong and how to fix it, but I'm not really succeeding:

I tried

window.create(sf::VideoMode(800, 600, 32), title.c_str());
window = sf:Window(sf::VideoMode(800, 600, 32), title.c_str());

And more things, but not luck.

sf::Window window;

Also, in the Game.hpp file, should I declare the window as sf::Window or sf::RenderWindow? Thank you.
Title: Re: No window appears
Post by: eXpl0it3r on August 28, 2012, 05:12:30 pm
Mhm, I understand what's wrong and how to fix it, but I'm not really succeeding:
This is what you want:
window.create(sf::VideoMode(800, 600, 32), title.c_str());
The creation an other window doesn't make sense.

Also, in the Game.hpp file, should I declare the window as sf::Window or sf::RenderWindow? Thank you.
Depends what you want to do, but I guess you want to use all the features SFML provides thus sf::RenderWindow is what you want to use.

Edit: The next problem will be, now that your window isOpen the application will get stuck in the update() function at the while(window.isOpen()) loop and never reach the render function. ;)
Title: Re: No window appears
Post by: Munchor on August 28, 2012, 05:23:50 pm
Mhm, I understand what's wrong and how to fix it, but I'm not really succeeding:
This is what you want:
window.create(sf::VideoMode(800, 600, 32), title.c_str());
The creation an other window doesn't make sense.

Also, in the Game.hpp file, should I declare the window as sf::Window or sf::RenderWindow? Thank you.
Depends what you want to do, but I guess you want to use all the features SFML provides thus sf::RenderWindow is what you want to use.

Edit: The next problem will be, now that your window isOpen the application will get stuck in the update() function at the while(window.isOpen()) loop and never reach the render function. ;)

Thanks a lot, but that didn't really work:

Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

#include "Game.hpp"

#include <stdio.h>

void Game::setup() {
  title = "s";
  window.create(sf::VideoMode(800, 600, 32), title.c_str());
  sf::CircleShape shape(100.f);
  shape.setFillColor(sf::Color::Green);

  start();
}

void Game::start()
{
  /* Handle game-related variables here */
 
  run();
}

void Game::run() {
  running = true;

  while (running) {
    update();
    render();
  }
}

void Game::update() {
  while (window.isOpen())
  {
    sf::Event event;
    while (window.pollEvent(event))
    {
      if (event.type == sf::Event::Closed)
        window.close();
    }
  }
}

void Game::render() {
  printf("Starting render...\n");
  window.clear();
  window.draw(shape);
  window.display();
}

int main(int argc, char *argv[]) {
  Game game;
  game.start();
  game.run();

  return 0;
}

The window is still not showing up :S
Title: Re: No window appears
Post by: eXpl0it3r on August 28, 2012, 05:27:28 pm
Edit: The next problem will be, now that your window isOpen the application will get stuck in the update() function at the while(window.isOpen()) loop and never reach the render function. ;)
Title: Re: No window appears
Post by: Munchor on August 28, 2012, 05:42:11 pm
Edit: The next problem will be, now that your window isOpen the application will get stuck in the update() function at the while(window.isOpen()) loop and never reach the render function. ;)

I actually had thought of that already, but I knew *that* was not the problem. I am debug printing on render(), it is reaching there lots of times.

Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

#include "Game.hpp"

#include <stdio.h>

void Game::setup() {
  title = "Explosonoid";
  window.create(sf::VideoMode(800, 600, 32), title.c_str());
  sf::CircleShape shape(100.f);
  shape.setFillColor(sf::Color::Green);

  start();
}

void Game::start()
{
  /* Handle game-related variables here */
 
  run();
}

void Game::run() {
  running = true;

  while (running) {
    update();
    render();
  }
}

void Game::update() {
  sf::Event event;
  while (window.pollEvent(event))
  {
    if (event.type == sf::Event::Closed)
      window.close();
  }
}

void Game::render() {
  printf("Starting render...\n");
  window.clear();
  window.draw(shape);
  window.display();
}

int main(int argc, char *argv[]) {
  Game game;
  game.start();
  game.run();

  return 0;
}

Either way, I still fixed it to make sure, it's not working yet. Thank you for all the attention.