SFML community forums
Help => General => Topic started 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:
#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:
-------------- 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
-
I don't see anything really wrong with your code.
Although, I'm not sure this is doing what you think it is:
// 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:
bool Done = false;
while (!Done)
{
//Render...
}
-------------- 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?
-
I think IsOpened will only return false when the window has not been created.
Calling App.Close(); will make it false.
-
Oh yeah. I wasn't very awake when I wrote all that...
-
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:
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
-
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
-
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
-
I'm sorry to double post... but is there any more insight on this?
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?
-
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:
#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):
/*///////////////////////////////////////////////
//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:
#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!
-
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.
-
The stack is certainly too small to contain 1600x640 char ;)
Try declaring your array at global scope, or dynamically (using a std::vector).
-
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.
-
I do have several bluetooth HIDs installed, but not plugged in ...
have you tried unplugging them?
... I haven't... any other ideas? :D
Liosan
-
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?
-
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 :)
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]
-
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?
-
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!
-
Probably, but as it's pretty weird I can't be sure, you should check it by yourself :)
-
Thanks for your answer, Laurent.
After posting I've used static linking and now it works all fine. It's really weird :|
-
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 :)
-
How can I avoid this bug? I mean that I can start my app with plugged usb-devices?
-
Nobody knows that? I think its a really bad bug, because many useres have usb hids.
-
You can download the latest sources from SVN, and recompile SFML.
Linking to the static libraries should work as well.