SFML community forums

Help => General => Topic started by: JeZ-l-Lee on January 19, 2009, 02:11:00 pm

Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: JeZ-l-Lee on January 19, 2009, 02:11:00 pm
1st Demo Using SFML - Only Get Console Window - HELP

Hi,

I am new to SFML.
I am using Microsoft Vista OS and my C++ IDE is Code::Blocks.
I followed the instructions properly to set up SFML with Code::Blocks.
I entered this simple demo from SFML web site:

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

 int main()
 {
     // Create the main window
     sf::RenderWindow App(sf::VideoMode(640, 640), "SFML window");

     // Load a sprite to display
     sf::Image Image;
     if (!Image.LoadFromFile("cute_image.jpg"))
         return EXIT_FAILURE;
     sf::Sprite Sprite(Image);

     // Create a graphical string to display
     sf::Font Arial;
     if (!Arial.LoadFromFile("arial.ttf"))
         return EXIT_FAILURE;
     sf::String Text("Hello SFML", Arial, 50);

     // Load a music to play
     sf::Music Music;
     if (!Music.OpenFromFile("nice_music.ogg"))
         return EXIT_FAILURE;

     // Play the music
     Music.Play();

     // Start the game loop
     while (App.IsOpened())
     {
         // Process events
         sf::Event Event;
         while (App.GetEvent(Event))
         {
             // Close window : exit
             if (Event.Type == sf::Event::Closed)
                 App.Close();
         }

         // Clear screen
         App.Clear();

         // Draw the sprite
         App.Draw(Sprite);

         // Draw the string
         App.Draw(Text);

         // Update the window
         App.Display();
     }

     return EXIT_SUCCESS;
 }


It builds OK, but when I run it, I only get one console window with no 640x640 window. Also I see no test sprite, text, and OGG music playing? (I do have the test sprite JPG, font, and OGG in the directory of the executable.)

When I build above source in Code::Blocks under Windows Vista, I get the follow output:
Code: [Select]
-------------- Build: Debug in SFML_Beginning ---------------

Compiling: main.cpp
Linking console executable: bin\Debug\SFML_Beginning.exe
Info: resolving sf::Font::ourDefaultCharset      by linking to __imp___ZN2sf4Font17ourDefaultCharsetE (auto-import)
Info: resolving vtable for sf::Spriteby linking to __imp___ZTVN2sf6SpriteE (auto-import)
Info: resolving vtable for sf::Stringby linking to __imp___ZTVN2sf6StringE (auto-import)
C:\Program Files\CodeBlocks\MinGW\bin\ld.exe: warning: auto-importing has been activated without --enable-auto-import specified on the command line.
This should work unless it involves constant data structures referencing symbols from auto-imported DLLs.
Output size is 67.00 KB
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 1 warnings


Again I am new to SFML.
If you can help me make this work I would appreciate it greatly.


JeZ+Lee
SLNTHERO@aol.com
Silent Hero Productions(R)
Video Game Design Studio
http://www.SilentHeroProductions.com
Title: Re: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: dewyatt on January 21, 2009, 01:11:12 pm
I don't see anything really wrong with your code.
Although, I'm not sure this is doing what you think it is:
Quote from: "JeZ-l-Lee"

Code: [Select]

     // Start the game loop
     while (App.IsOpened())


See here (http://www.sfml-dev.org/documentation/1.4/classsf_1_1Window.htm#e7171f19a6adaf17347c9e64c87afb2a)
I think IsOpened will only return false when the window has not been created.
I would recommend something like this:
Code: [Select]

bool Done = false;
while (!Done)
{
    //Render...
}


Quote from: "JeZ-l-Lee"

Code: [Select]
-------------- Build: Debug in SFML_Beginning ---------------

Compiling: main.cpp
Linking console executable: bin\Debug\SFML_Beginning.exe
Info: resolving sf::Font::ourDefaultCharset      by linking to __imp___ZN2sf4Font17ourDefaultCharsetE (auto-import)
Info: resolving vtable for sf::Spriteby linking to __imp___ZTVN2sf6SpriteE (auto-import)
Info: resolving vtable for sf::Stringby linking to __imp___ZTVN2sf6StringE (auto-import)
C:\Program Files\CodeBlocks\MinGW\bin\ld.exe: warning: auto-importing has been activated without --enable-auto-import specified on the command line.
This should work unless it involves constant data structures referencing symbols from auto-imported DLLs.
Output size is 67.00 KB
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 1 warnings


This is what concerns me.
I've never seen these auto-import messages.
I'm wondering if you're using SFML DLLs and not defining SFML_DYNAMIC.
Is that it?
Title: Re: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: dabo on January 21, 2009, 04:41:01 pm
Quote from: "dewyatt"
I think IsOpened will only return false when the window has not been created.

Calling App.Close(); will make it false.
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: dewyatt on January 23, 2009, 05:14:55 am
Oh yeah. I wasn't very awake when I wrote all that...
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Liosan on January 26, 2009, 10:14:15 am
Any update on this subject?

I'm getting exactly the same problem - only the console window on a project that should contain a normal SFML window.

The problem occured after a migration from SFML 1.3 to 1.4. The project compiles fine with MinGW using g++ 4.0, I link against dynamic, non-debug libraries with the dll-s in place. I can't link against the static libraries, because I get linkage errors. Apart from that, everything works - except that the window doesn't show :)

The same project compiles and opens fine with Visual Studio.

Any insight?

EDIT: the window is created using:

Code: [Select]

    sf::VideoMode currentVideoMode = sf::VideoMode::GetDesktopMode();
    mRenderWindow = new sf::RenderWindow( sf::VideoMode(800, 600, currentVideoMode.BitsPerPixel), "Warsztat Game" );

    assert( mRenderWindow && "CGame::CGame(): unable to create RenderWindow\n" );
and the assertion doesn't fire.

Liosan
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: JeZ-l-Lee on February 04, 2009, 03:13:40 pm
Hi,

This is known bug in 1.4 version.
If you have a USB joystick plugged into computer then unplug it and rerun your SFML application, should work now!


JeZ+Lee
SLNTHERO@aol.com
Silent Hero Productions(R)
Video Game Design Studio
http://www.SilentHeroProductions.com
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Liosan on February 18, 2009, 01:17:16 pm
Well, ok. But I don't have one plugged in, I haven't even seen an USB joystick.

I do have several bluetooth HIDs (human interface devices) installed, but not plugged in (for example a Wii remote). Do you think this can be the issue?

Liosan
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Liosan on March 08, 2009, 06:48:26 pm
I'm sorry to double post... but is there any more insight on this?

Liosan
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Astrof on March 08, 2009, 07:00:39 pm
Quote from: "Liosan"
Well, ok. But I don't have one plugged in, I haven't even seen an USB joystick.

I do have several bluetooth HIDs (human interface devices) installed, but not plugged in (for example a Wii remote). Do you think this can be the issue?

Liosan


have you tried unplugging them?
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Bonafide on March 09, 2009, 04:17:05 am
Hey guys, I'm having a similar problem, but different (not the joystick issue). The difference is that my code will not run UNLESS I omit a few lines of code from it, and I don't see why those few lines would be a problem to the program. I've ran the same code in other instances (drafts, etc) and it has ran without problem.

I know there are two areas within the code that draws santa and the block twice, that isn't the problem, I've changed it around when trying to run the program - to no avail.

Here's the problematic code:
Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include "Collision.h"
#include "Map.h"

using namespace std;
using namespace sf;


int main()
{
//Load collision
Collision coll;  

// Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Window");

    // Load the sprite image from a file
    sf::Image imgBlock;
    if (!imgBlock.LoadFromFile("blocks.jpg"))
        return EXIT_FAILURE;

sf::Image imgSanta;
if (!imgSanta.LoadFromFile("santa.png"))
        return EXIT_FAILURE;

    // Create the sprite
    sf::Sprite block(imgBlock);
sf::Sprite santa(imgSanta);

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Get elapsed time
        float ElapsedTime = App.GetFrameTime();

/*///////////////////////////////////////////////
//Declare map & level variables
MAP map;

char level[1600][640];

//Open file stream;
ifstream file;

//Load
map.mapLoad(file, level);

//num holder
int num;

//Display
for(int y = 0; y < 1600; y++)
{
for(int x = 0; x < 640; x++)
{
num = level[y][x];

if(num == '1')
{
  block.SetPosition(x*32, y*32);
  App.Draw(block);
}
if(num == '2')
{
App.Draw(santa);
}
}
}
////////////////////////////////// */

if (App.GetInput().IsKeyDown(sf::Key::Left))
{
if(coll.detectCollision(santa, block) != true)
{
santa.Move(-100 * ElapsedTime, 0);
}
else
{
santa.SetPosition((santa.GetPosition().x + 3), (santa.GetPosition().y));
}
}
if (App.GetInput().IsKeyDown(sf::Key::Right))
{
if(coll.detectCollision(santa, block) != true)
{
santa.Move(+100 * ElapsedTime, 0);
}
else
{
santa.SetPosition((santa.GetPosition().x - 3), (santa.GetPosition().y));
}
}
if (App.GetInput().IsKeyDown(sf::Key::Up))
{
if(coll.detectCollision(santa, block) != true)
{
santa.Move(0, -100 * ElapsedTime);
}
else
{
santa.SetPosition((santa.GetPosition().x), (santa.GetPosition().y) + 3);
}
}
if (App.GetInput().IsKeyDown(sf::Key::Down))
{
if(coll.detectCollision(santa, block) != true)
{
santa.Move(0,  100 * ElapsedTime);
}
else
{
santa.SetPosition((santa.GetPosition().x), (santa.GetPosition().y) - 3);
}
}

        // Rotate the sprite
        if (App.GetInput().IsKeyDown(sf::Key::A)) santa.Rotate(- 100 * ElapsedTime);
        if (App.GetInput().IsKeyDown(sf::Key::D)) santa.Rotate(+ 100 * ElapsedTime);

        // Clear screen
        App.Clear();

        // Display sprite in our window
        App.Draw(santa);
App.Draw(block);

        // Display window contents on screen
        App.Display();
    }
    return EXIT_SUCCESS;
}


Here's the code that won't make the program run unless omitted (its in the code above):
Code: [Select]

/*///////////////////////////////////////////////
//Declare map & level variables
MAP map;

char level[1600][640];

//Open file stream;
ifstream file;

//Load
map.mapLoad(file, level);

//num holder
int num;

//Display
for(int y = 0; y < 1600; y++)
{
for(int x = 0; x < 640; x++)
{
num = level[y][x];

if(num == '1')
{
  block.SetPosition(x*32, y*32);
  App.Draw(block);
}
if(num == '2')
{
App.Draw(santa);
}
}
}
////////////////////////////////// */


Map class, if that's the issue:
Code: [Select]

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

using namespace sf;
using namespace std;

#define HEIGHT 1600;
#define WIDTH 600;



class MAP
{
public:
void mapLoad(ifstream& file, char level[1600][640]);

};

void MAP::mapLoad(ifstream &file, char level[1600][640])
{
//Input map file
file.open("level.txt");

//Holds the information line by line
std::string line;

//Height of the file
int fileHeight = 0;

while(std::getline(file, line))
{
for(int fileWidth = 0; fileWidth < 10; fileWidth++)
{    
  level[fileHeight][fileWidth] = line.at(fileWidth);
}    
fileHeight++;
}

//Close file stream
file.close();
}


All help is appreciated!
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Astrof on March 09, 2009, 05:22:01 am
have you tried using a debugger or manually debugging? (using debug couts and printfs to try to figure out where exactly the problem is occurring? ) it could be a problem in the file.
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Laurent on March 09, 2009, 07:47:55 am
The stack is certainly too small to contain 1600x640 char ;)

Try declaring your array at global scope, or dynamically (using a std::vector).
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Bonafide on March 09, 2009, 07:23:39 pm
Thanks, it's working now.

The problem was that I forgot to change the array dimensions to the results of 1600/32 and 640/32, which is 50 and 20 respectively.
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Liosan on March 11, 2009, 08:10:31 pm
Quote from: "Astrof"
Quote from: "Liosan"
I do have several bluetooth HIDs installed, but not plugged in ...


have you tried unplugging them?


... I haven't... any other ideas? :D

Liosan
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Astrof on March 11, 2009, 10:07:09 pm
I would try unplugging them and then trying it, or downloading the latest source through the SVN.  

but you said that the program works fine in Visual Studio?
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Liosan on March 13, 2009, 12:10:57 pm
Quote from: "Astrof"
I would try unplugging them and then trying it,


I'll quote myself again: not plugged in. So, no, I will not unplug them, because it seems impossible :)

Quote from: "Astrof"
but you said that the program works fine in Visual Studio?


Yes, it does. But I want to work with MinGW/g++4, not VS.  Although this does spoil the joystick theory, doesn't it? VS and g++ use separate libs, so I suppose there might be some sort of bug in the g++ version.

Liosan[/b]
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Astrof on March 13, 2009, 02:24:19 pm
Sorry, when you said "I haven't"  I thought you meant you had not unplugged them.  My bad.  

Have you tried using the latest source through the SVN?
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: drpitch on March 16, 2009, 06:04:59 pm
Hi,

First of all, congratulation to Laurent, you are doing a great job!

Now, I'm getting exactly the same problem. Only the console window is opened when running samples.

I have an usb wheel plugged into computer. As you said, if I unplug it and rerun samples, they work. Moreover, there's a strange behaviour:
- When mouse is moved in opengl sample, the performance of the rendering slows down dramatically. But if I plug the USB joystick when I'm running the sample, the scene runs smooth again, even when if I move the mouse.

Are these bugs fixed on the SVN version?

Thanks!
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Laurent on March 16, 2009, 08:17:16 pm
Probably, but as it's pretty weird I can't be sure, you should check it by yourself :)
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: drpitch on March 16, 2009, 09:31:13 pm
Thanks for your answer, Laurent.

After posting I've used static linking and now it works all fine. It's really weird :|
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Liosan on March 22, 2009, 07:57:47 pm
Heh, talk about weird, I can't link against static, I get some weird errors...

Liosan

PS. I'll check the repository version when I find the time to :)
Title: Bug
Post by: WorldRacer on May 21, 2009, 05:42:18 pm
How can I avoid this bug? I mean that I can start my app with plugged usb-devices?
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: WorldRacer on May 22, 2009, 07:40:47 pm
Nobody knows that? I think its a really bad bug, because many useres have usb hids.
Title: 1st Demo Using SFML - Only Get Console Window - HELP
Post by: Laurent on May 22, 2009, 07:57:59 pm
You can download the latest sources from SVN, and recompile SFML.

Linking to the static libraries should work as well.