SFML community forums

Help => General => Topic started by: The Illusionist Mirage on August 08, 2013, 05:51:46 pm

Title: [Solved]Game window not working
Post by: The Illusionist Mirage on August 08, 2013, 05:51:46 pm
Hello

I installed SFML 2.0 after a lot of struggle in Code::Blocks with the MinGW 4.7.2 compiler. When I compile the follwoing code, it compiles and runs flawlessly:

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

const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const int WINDOW_BPP = 32;
const char *WINDOW_CAPTION = "MY_SFML";

int main()
{
    sf::RenderWindow Window;
    Window.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP),
                  WINDOW_CAPTION, sf::Style::Close);

    while(Window.isOpen())
    {
        sf::Event Event;

        while(Window.pollEvent(Event))
        {
            switch(Event.type)
            {
                case sf::Event::Closed:
                    {
                        Window.close();
                    } break;
                case sf::Event::KeyPressed:
                    {
                        if(Event.key.code == sf::Keyboard::Escape)
                        {
                            Window.close();
                        }
                    } break;
            }
        }

        Window.display();
    }
}
 
But if i am trying to display a circle or rectangle(as in the following code), then the code doesn't compile:

#include <SFML/Graphics.hpp>

const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
const char *WINDOW_CAPTION = "MY_SFML";

const float CIRCLE_RADIUS = 100.0f;

int main()
{
        system("cls");

        sf::RenderWindow GameWindow(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT),
                WINDOW_CAPTION, sf::Style::Close);

        sf::CircleShape Circle(CIRCLE_RADIUS);
        Circle.setFillColor(sf::Color::Yellow);

        while(GameWindow.isOpen())
        {
                sf::Event GameEvent;

                if(GameWindow.pollEvent(GameEvent))
                {
                        if(GameEvent.type == sf::Event::Closed)
                        {
                                GameWindow.close();
                        }

                        GameWindow.clear();
                        GameWindow.draw(Circle);
                        GameWindow.display();
                }
        }

        return 0;
}

So where's the problem?

Thanks
Title: Re: Game window not working
Post by: zsbzsb on August 08, 2013, 06:19:40 pm
Maybe if you posted why the code doesn't compile we might be able to help you. Just saying code doesn't compile without any error message of any sort won't help you out.
Title: Re: Game window not working
Post by: AlejandroCoria on August 08, 2013, 06:29:52 pm
(google translate)

the code
GameWindow.clear();
GameWindow.draw(Circle);
GameWindow.display();
is inside the "if" (which really should be "while").
if(GameWindow.pollEvent(GameEvent))

the code would have to be inside the loop
while(GameWindow.isOpen())
Title: Re: Game window not working
Post by: The Illusionist Mirage on August 09, 2013, 08:44:10 am
Maybe if you posted why the code doesn't compile we might be able to help you. Just saying code doesn't compile without any error message of any sort won't help you out.

The compiler down't display any error message. As soon as I hit Run, the SFML window appeares and then another dialog pops up telling that mysfmlprogram.exe has stopped working and windows is looking for a solution. After that nothing happens and process terminates with status -1073741819.
Title: Re: Game window not working
Post by: Ixrec on August 09, 2013, 12:01:05 pm
He's talking about the code that you said didn't compile (the second part), not the code that you said did compile.
Title: Re: Game window not working
Post by: zsbzsb on August 09, 2013, 01:28:31 pm
The compiler down't display any error message. As soon as I hit Run, the SFML window appeares and then another dialog pops up telling that mysfmlprogram.exe has stopped working and windows is looking for a solution. After that nothing happens and process terminates with status -1073741819.

Sounds like you have compiler/linker settings messed up somewhere. Are you sure your not mixing debug and release and that you have the correct libs for your compiler?
Title: Re: Game window not working
Post by: The Illusionist Mirage on August 09, 2013, 03:23:37 pm
Quote
Sounds like you have compiler/linker settings messed up somewhere. Are you sure your not mixing debug and release and that you have the correct libs for your compiler?

I followed this tutorial to set up SFML:
http://www.youtube.com/watch?v=U1zN_QRSwxw (http://www.youtube.com/watch?v=U1zN_QRSwxw)

As far as I can make out,  I created a SFML folder in my C: drive (C:\SFML\SFML 2.0) and I compiled the SFML source using Cmake(both Debug and Release). Though I did generate some files(sfml-graphics.dll ,etc) the tutorial didn't mention to use them.

Though I could set up SFML with Visual Studio 12(followed this tut:)http://www.youtube.com/watch?v=VcPRJE6Rt0o (http://www.youtube.com/watch?v=VcPRJE6Rt0o),
I have never successfuly done that for Code::Blocks.
Title: Re: Game window not working
Post by: The Illusionist Mirage on August 10, 2013, 12:16:58 pm
Problem still persisting. I am doing fine with SFML 2.0 in Visual Studio 12 but still couldn't figure out what's wrong with my Code::Blocks setup and why am i able to display a SFML 2.0 window but not draw anything to it.
Title: Re: Game window not working
Post by: eXpl0it3r on August 10, 2013, 05:48:03 pm
So the following code doesn't work with Code::Blocks?

#include <SFML/Graphics.hpp>

const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
const char *WINDOW_CAPTION = "MY_SFML";

const float CIRCLE_RADIUS = 100.0f;

int main()
{
    sf::RenderWindow GameWindow(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), WINDOW_CAPTION, sf::Style::Close);

    sf::CircleShape Circle(CIRCLE_RADIUS);
    Circle.setFillColor(sf::Color::Yellow);

    while(GameWindow.isOpen())
    {
        sf::Event GameEvent;

        if(GameWindow.pollEvent(GameEvent))
        {
            if(GameEvent.type == sf::Event::Closed)
            {
                GameWindow.close();
            }
        }

        GameWindow.clear();
        GameWindow.draw(Circle);
        GameWindow.display();
    }
}
Title: Re: Game window not working
Post by: lockandstrike on August 10, 2013, 09:50:30 pm
It's already been said, but in your code, where there is a "if(GameWindow.pollEvent(GameEvent))" statement there should be a "while(GameWindow.pollEvent(GameEvent))" statement. Change the code and post the result of that.
Title: Re: Game window not working
Post by: The Illusionist Mirage on August 11, 2013, 02:31:23 pm
Quote
@lockandstrike

It's already been said, but in your code, where there is a "if(GameWindow.pollEvent(GameEvent))" statement there should be a "while(GameWindow.pollEvent(GameEvent))" statement. Change the code and post the result of that.

I changed it to while and its still not working. I've recompiled SFML 2.0 again from the beginning and tried to make it work with Code::Blocks.

Here's what I have been doing:

First, I downloaded the SFML source from here https://github.com/SFML/SFML/tarball/master (https://github.com/SFML/SFML/tarball/master)

Then, extracted the files from the zip.

Next I compiled them using Cmake using the command line as per as the instructions on this site : http://sfmlcoder.wordpress.com/2011/06/16/building-sfml-2-0-with-mingw-make/ (http://sfmlcoder.wordpress.com/2011/06/16/building-sfml-2-0-with-mingw-make/)

After compiling everything (Debug and Release), I opened up Code::Blocks 12.11 and went to the compiler settings and added the include and lib directories of SFML 2.0(I added those lib files which I compiled).

Then, I created a new console app, went to its project build options and in the linker setting tab added these:
for debug,
sfml-graphics-d
sfml-system-d
sfml-window-d

for release,
sfml-graphics
sfml-system
sfml-window


Then I added this code:


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

const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const int WINDOW_BPP = 32;
const char *WINDOW_CAPTION = "MY_SFML";

int main()
{
    sf::RenderWindow Window;
    Window.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP),
                  WINDOW_CAPTION, sf::Style::Close);

    while(Window.isOpen())
    {
        sf::Event Event;

        while(Window.pollEvent(Event))
        {
            switch(Event.type)
            {
                case sf::Event::Closed:
                    {
                        Window.close();
                    } break;
                case sf::Event::KeyPressed:
                    {
                        if(Event.key.code == sf::Keyboard::Escape)
                        {
                            Window.close();
                        }
                    } break;
            }
        }

        Window.display();
    }
}


Uptill this, everything went on as expected. But when I added a few lines to display a circle:

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

const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const int WINDOW_BPP = 32;
const char *WINDOW_CAPTION = "MY_SFML";

const float CIRCLE_RADIUS = 100.0f;

int main()
{
    sf::RenderWindow Window;
    Window.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP),
                  WINDOW_CAPTION, sf::Style::Close);

    sf::Time Time;
    sf::Clock Clock;

    sf::CircleShape Circle(CIRCLE_RADIUS);
    Circle.setFillColor(sf::Color::Green);

    while(Window.isOpen())
    {
        sf::Event Event;

        // Now I've also put pollEvent in a while loop as members
        // AlejandroCoria and lockandstrike pointed out
        while(Window.pollEvent(Event))  
        {
            switch(Event.type)
            {
                case sf::Event::Closed:
                    {
                        Window.close();
                    } break;
                case sf::Event::KeyPressed:
                    {
                        if(Event.key.code == sf::Keyboard::Escape)
                        {
                            Window.close();
                        }
                    } break;
            }
        }

        Time = Clock.getElapsedTime();

        std::cout << Time.asSeconds() << std::endl;

        Window.draw(Circle);                   // problem started after //
        Window.display();
        Window.clear();                        // adding these two lines //
    }
}
 

The pgrogram is building, but when I run it, the console window and sfml window appear and then the sfml window turns white and a windows dialog saying mysfmlprogram.exe has stopped working and windows is looking for a solution. After that nothing happens until I close it.


EDIT:
quoted lockandstrike's reply
Title: Re: Game window not working
Post by: binary1248 on August 11, 2013, 03:26:07 pm
So.... what happens when you debug it? If you don't know how to debug your application in Code::Blocks, I'm sure you'll be able to find another tutorial for that...
Title: Re: Game window not working
Post by: The Hatchet on August 11, 2013, 03:28:24 pm
Did you remember to move the DLL files over to your working directory as well?  These are needed if you link the dynamic version vs static.
Title: Re: Game window not working
Post by: The Illusionist Mirage on August 11, 2013, 04:36:08 pm
Quote
@binary1248
So.... what happens when you debug it? If you don't know how to debug your application in Code::Blocks, I'm sure you'll be able to find another tutorial for that...

Yes, I know how to debug. Set build target to debug and then build it, right?

Quote
@The Hatchet
Did you remember to move the DLL files over to your working directory as well?  These are needed if you link the dynamic version vs static.

Yes I've put the DLLs into the project folder.
Title: Re: Game window not working
Post by: binary1248 on August 11, 2013, 04:54:14 pm
Yes, I know how to debug. Set build target to debug and then build it, right?
No, it is a bit more involved than that. Look for a guide, it's too much to explain here.
Title: Re: Game window not working
Post by: The Illusionist Mirage on August 11, 2013, 06:32:43 pm
Quote
@binary1248

No, it is a bit more involved than that. Look for a guide, it's too much to explain here.

I tried here: http://wiki.codeblocks.org/index.php?title=Debugging_with_Code::Blocks (http://wiki.codeblocks.org/index.php?title=Debugging_with_Code::Blocks). But unfortunately still the same problem is persisting. I am in a fix cause thiugh the project is building, its not running.
Title: Re: Game window not working
Post by: Ixrec on August 11, 2013, 08:56:57 pm
Debugging doesn't mean building a debug version of your program. Debugging means doing things like setting breakpoints so that when you build and run the debug version, you can see what's going on at specific points in the code (and which ones it even gets to), step through the code one line at a time, check the call stack and the values of all your variables at each step, and so on.

Since the code is short, what you probably want to do to debug this problem is set a breakpoint on the first line of main, run a debug build, then step through the program one line at a time from there until you see what line is causing the problem.
Title: Re: Game window not working
Post by: lockandstrike on August 11, 2013, 10:34:54 pm

Then, I created a new console app, went to its project build options and in the linker setting tab added these:
for debug,

sfml-graphics-d
sfml-system-d
sfml-window-d

for release,
sfml-graphics
sfml-system
sfml-window


The gcc toolchain requires that libs are declared in a certain order. Because of sfml-graphics lib depending on sfml-sytem and sfml-window try to add the libs in this order:

For debug:

sfml-system-d
sfml-window-d
sfml-graphics-d

For release:

sfml-system
sfml-window
sfml-graphics


And also i don't know how to do it in Code::Blocks but add the 'SFML_DYNAMIC' preprocessor to the project settings.
Title: Re: Game window not working
Post by: The Illusionist Mirage on August 12, 2013, 02:49:44 pm
Quote
@ Ixrec

You said:
Debugging doesn't mean building a debug version of your program. Debugging means doing things like setting breakpoints so that when you build and run the debug version, you can see what's going on at specific points in the code (and which ones it even gets to), step through the code one line at a time, check the call stack and the values of all your variables at each step, and so on.


After  I did that I get this:
(http://4.bp.blogspot.com/-SxgZsbBGucg/UgjYzfO3ScI/AAAAAAAAAK4/_c5Uc02aTX4/s1600/Capture8.png)

(http://3.bp.blogspot.com/-ivSNH8XmEws/UgjYzTln4fI/AAAAAAAAAK0/ZlV6jthXowU/s1600/Capture9.png)

Quote
@ lockandstrike

The gcc toolchain requires that libs are declared in a certain order. Because of sfml-graphics lib depending on sfml-sytem and sfml-window try to add the libs in this order:

For debug:

sfml-system-d
sfml-window-d
sfml-graphics-d

For release:

sfml-system
sfml-window
sfml-graphics


Yes, I tried it but unfortunately no progress.
Title: Re: Game window not working
Post by: The Illusionist Mirage on August 12, 2013, 02:57:21 pm
@ All those who tried to help me (zsbzsb, AlejandroCoria, Ixrec, eXpl0it3r, lockandstrike, binary1248, The Hatchet)

Thanks for trying to help me out guys. I've tried a lot but now I think all options have run out. I'll try to set up SFML with Code::Blocks some other time because I have little knowledge about my compiler, its possible that I've been messing with something that's going unnoticed, and I am pestering you guys ever since.

Right now I'll use VS 12 to learn and program using SFML 2.0 and after I am comletely familiar with Code::Blocks, I'll try to set up SFML in it.

THANKS :)
Title: [Solved] Game window not working
Post by: The Illusionist Mirage on August 14, 2013, 03:16:37 pm
Hello Everyone!

I figured out what was wrong. Now I've successfully installed SFML 2.0 in Code::Blocks 12.11 using the MinGW 4.7.2!


:D  ;D ;)
Title: Re: [Solved]Game window not working
Post by: eXpl0it3r on August 14, 2013, 03:30:46 pm
Glad you could resolve the issue.

Two tips for using the forum:
Title: Re: [Solved]Game window not working
Post by: The Illusionist Mirage on August 14, 2013, 04:07:15 pm
Thanks Sir! I'll remember these two useful tips.