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

Author Topic: [Solved]Game window not working  (Read 11278 times)

0 Members and 1 Guest are viewing this topic.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
[Solved]Game window not working
« 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
« Last Edit: August 14, 2013, 03:17:30 pm by The illusionist mirage »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Game window not working
« Reply #1 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.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: Game window not working
« Reply #2 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())

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Game window not working
« Reply #3 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.
« Last Edit: August 09, 2013, 08:46:10 am by The illusionist mirage »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Game window not working
« Reply #4 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.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Game window not working
« Reply #5 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?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Game window not working
« Reply #6 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:


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:),
I have never successfuly done that for Code::Blocks.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Game window not working
« Reply #7 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Game window not working
« Reply #8 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();
    }
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lockandstrike

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: Game window not working
« Reply #9 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.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Game window not working
« Reply #10 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

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/

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
« Last Edit: August 11, 2013, 02:39:32 pm by The illusionist mirage »

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Game window not working
« Reply #11 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...
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Game window not working
« Reply #12 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.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Game window not working
« Reply #13 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.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Game window not working
« Reply #14 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.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

 

anything