#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;
}
#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
Default constructor.
This constructor doesn't actually create the window, use the other constructors or call Create to do so.
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:QuoteDefault 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:This is what you want:
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.
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. ;)
#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;
}
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. ;)
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. ;)
#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;
}