Lemmons Lemmons - 2 months ago 9
C++ Question

Window refuses to render

I recently got started trying to use SFML. For some reason my simple program will not render the window. I've tried throwing everything into main to see if there was an error in my code that had to do with multiple files etc. but to no avail.

I'll launch my program and nothing will appear.

What's the problem?

//main.h
#ifndef MAIN_H
#define MAIN_H

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

#include <iostream>
#include <fstream>

using namespace std;
using namespace sf;

class game
{
public:
void startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME);
void log(const string logging);

game()
{
QUIT = false;

pendingFile.open("Log.txt", ios::out);
pendingFile << "---Brain Bread Log---";
}

~game()
{
pendingFile.close();
}

private:
bool QUIT;
ofstream pendingFile;
};

#endif


//main.cpp
#include "main.h"

void game::log(const string logging)
{
pendingFile << logging;
}

void game::startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME)
{
Window Game(VideoMode(SCREEN_W, SCREEN_H, 32), SCREEN_NAME);
while(QUIT == false)
{
Game.Display();
}
}


int main(int argc, char* argv[])
{
game gameObj;

gameObj.startLoop(800, 600, "Brain Bread");

return 0;
}

Answer

I tried your code and it behaves exactly as I expect it to - that is to say that an iconless window with a black body pops up and it doesn't respond to events. Is that what you're getting? If not, you might need to rebuild SFML.

You might want to try introducing event-handling so that your startLoop looks more like this:

void game::startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME)
{
    // Init stuff

    while (Game.IsOpened())
    {
        sf::Event newEvent;

        while (Game.GetEvent(newEvent))
        {
            // Process event
        }

        // Do graphics stuff

        Game.Display();
    }
}
Comments