SFML community forums

Help => General => Topic started by: Bacu on November 06, 2011, 09:17:13 am

Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 06, 2011, 09:17:13 am
Everything compiles and links properly, but when the application runs, all I get is an empty console window, and no SFLM window. Any ideas? The .dlls are in the same directory as the .exe. I get a bunch of "Cannot find or open the PDB file" in the debug when I run. Not sure if this is problem.

Any ideas?
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Laurent on November 06, 2011, 09:56:53 am
SFML 1.6? ATI graphics card?
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 06, 2011, 07:03:21 pm
Quote from: "Laurent"
SFML 1.6? ATI graphics card?

Yes and yes.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Laurent on November 06, 2011, 08:03:46 pm
=> Known bug ;)
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 06, 2011, 08:21:36 pm
Quote from: "Laurent"
=> Known bug ;)

Well, what can I do now? Is the library useless to me until it's fixed or what?
Title: Tutorial "Opening a window" code doesn't work righ
Post by: OniLinkPlus on November 06, 2011, 08:58:44 pm
Quote from: "Bacu"
Quote from: "Laurent"
=> Known bug ;)

Well, what can I do now? Is the library useless to me until it's fixed or what?
Use SFML2.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 06, 2011, 09:07:25 pm
Quote from: "OniLink10"
Quote from: "Bacu"
Quote from: "Laurent"
=> Known bug ;)

Well, what can I do now? Is the library useless to me until it's fixed or what?
Use SFML2.

I assume that all of the tutorials that are for 1.6 will more or less work for 2.0?
Title: Tutorial "Opening a window" code doesn't work righ
Post by: OniLinkPlus on November 06, 2011, 09:41:34 pm
Quote from: "Bacu"
Quote from: "OniLink10"
Quote from: "Bacu"
Quote from: "Laurent"
=> Known bug ;)

Well, what can I do now? Is the library useless to me until it's fixed or what?
Use SFML2.

I assume that all of the tutorials that are for 1.6 will more or less work for 2.0?
Yes, and when they don't, it's probably because the name of something may have changed. For example, sf::Image has split into sf::Texture (what you want to use for rendering) and sf::Image (stored in memory only)
Title: Tutorial "Opening a window" code doesn't work righ
Post by: slotdev on November 06, 2011, 11:02:28 pm
Laurent, I know SFML 2 hasn't officially been released as yet, but why not take down the 1.6 link? The more people who use 2, the better, and it's so stable now anyway.

It would also help stop problems like this :-)
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Laurent on November 06, 2011, 11:53:48 pm
SFML 1.6 is still the latest official version, I can't hide it.

Regarding the initial problem, static linking should also solve it ;)
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 07, 2011, 01:57:12 am
Oh boy, more problems. I followed the instructions on using Cmake to put everything together, but I get this error:
Code: [Select]
CMake Error at CMakeLists.txt:131 (add_subdirectory):
  add_subdirectory given source "src/SFML" which is not an existing
  directory.


CMake Error at examples/ftp/CMakeLists.txt:8 (sfml_add_example):
  Unknown CMake command "sfml_add_example".


Configuring incomplete, errors occurred!

groan.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Laurent on November 07, 2011, 07:53:12 am
Seems like your SFML directory is messed up. Try to download it again.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 07, 2011, 04:42:21 pm
Quote from: "Laurent"
Seems like your SFML directory is messed up. Try to download it again.

Something was off with the download. The first time I extracted the .rar, it gave me an error. I downloaded and extracted over again but it kept happening; but if I extracted, got the error, then extracted and replaced all, I didn't get the error and Cmake did its job with no trouble.

But noooooooooow I've built the example and the same thing still happens. When I run the program, it also asks for sfml-window.dll and sfml-system.dll in addition to sfml-window-2.dll and sfml-system-2.dll. I tried using 1.6 dlls to no avail. Copying and renaming the -2.dlls gave errors.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Laurent on November 07, 2011, 04:45:17 pm
Something is seriously messed up, it's like you're mixing SFML 1.6 and 2.0.

Remove everything related to SFML and try again.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 07, 2011, 09:25:54 pm
Quote from: "Laurent"
Something is seriously messed up, it's like you're mixing SFML 1.6 and 2.0.

Remove everything related to SFML and try again.

alright, getting closer. Builds and runs and I get a window, but the window's 'X' close button doesn't seem to work, nor does the minimize, nor can I drag the window around. I can only close it by closing the console it's running from. The debug information says that it doesn't exit with "0" as its return value.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Tex Killer on November 07, 2011, 10:01:36 pm
That tutorial doesn't treat events, so your window's close event is not used and the window isn't closed. Advance on the tutorials and you shall solve your problem.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 07, 2011, 11:03:18 pm
Quote from: "Tex Killer"
That tutorial doesn't treat events, so your window's close event is not used and the window isn't closed. Advance on the tutorials and you shall solve your problem.

So it seems. Missed that line. Oh well. Time to move forward.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 09, 2011, 07:19:00 pm
Alright, been smooth so far. The differences between the tutorial and the 2.0 library are actually proving to be a good learning tool, gotta dig to figure it out.

anyway, having an issue with sf::Image.load. Getting "Failed to load image "sprite.png". Reason : Unable to open file". sprite.png is in the same directory as the executable. What am I doing wrong?

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

int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Events");
sf::Image Image;
if(!Image.LoadFromFile("sprite.png"))
{
//std::cout<<"Failed to load";
//return EXIT_FAILURE;
}
    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.PollEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Escape))
                App.Close();
        }

//if(sf::Keyboard::IsKeyPressed(sf::Keyboard::A)) std::cout<<"A IS PRESSED";

App.Clear();
        App.Display();
    }

    return EXIT_SUCCESS;
}
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Laurent on November 09, 2011, 08:15:04 pm
The working directory is not always the same directory as the executable, especially when you run the app from the IDE.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 11, 2011, 09:43:54 pm
Trying to play around with this stuff. This bit of code is supposed to create Triangles at the center of the screen, give them random velocity and spin, then display their movement.

Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>
#include <math.h>
#include <time.h>
#include <vector>

sf::Vector2f origin;
sf::Vector2f gravity;
int frames;

float getrand(float a, float b)
{
return ((b-a)*((float)rand()/RAND_MAX))+a;
}


struct Triangle
{
sf::Shape Tri;
sf::Vector2f Pos;
sf::Vector2f Vel;
float Spin;

Triangle()
{
Tri.AddPoint(0,10);
Tri.AddPoint(20,0);
Tri.AddPoint(20,20);
Tri.SetOrigin(10,10);
Tri.SetPosition(origin);
Pos = Tri.GetPosition();
Spin = getrand(-360.f,360.f);
Vel.x = getrand(-.5f,.5f);
Vel.y = getrand(-.5f,.5f);
}

void Draw(sf::RenderWindow &app)
{
app.Draw(Tri);
}
void Physics()
{
Vel = sf::Vector2f(Vel.x + gravity.x/frames ,Vel.y + gravity.y/frames);
Pos = sf::Vector2f(Pos.x + Vel.x/frames ,Pos.y + Vel.y/frames);
Tri.SetPosition(Pos);
Tri.Rotate(Spin/frames);
}
};

int main()
{
srand((unsigned)time(0));
origin.x = 400;
origin.y = 300;
gravity.x = 0;
gravity.y = 3;
std::vector<Triangle*> triangles;

sf::RenderWindow App(sf::VideoMode(800,600,32),"oh man");
int frames = 60;
App.SetFramerateLimit(frames);

    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.PollEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
            // Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Escape))
                App.Close();
        }

App.Clear();
Triangle* ptr = new Triangle;
triangles.push_back(ptr);
for (unsigned long x = 0; x < triangles.size(); x++)
{
std::cout<<triangles.at(0)->Pos.x<<" "<<triangles.at(0)->Pos.y<<"\n";
triangles.at(x)->Physics();
triangles.at(x)->Draw(App);
}
App.Display();
    }

    return EXIT_SUCCESS;
}


Two problems. The first is Triangle::Physics(); turns Vel and Pos into
Code: [Select]
+ Pos {x=-1.#IND000 y=1.#INF000 } sf::Vector2<float>
+ Vel {x=-1.#IND000 y=1.#INF000 } sf::Vector2<float>

Am I doing something wrong here?

Second, if I change Triangle::Physics to only increment Pos every time it's called, The call to Triangle::Draw(App); never ends up producing anything onscreen. If I only create one Triangle and never call Physics, it will display at the middle of the screen.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 11, 2011, 10:06:25 pm
I was dividing by 0, duh. okay, problem solved.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 11, 2011, 11:27:44 pm
And now another question. How to statically link? all of the .lib are -d.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Laurent on November 12, 2011, 10:57:34 am
You must compile the static libs (disable BUILD_SHARED_LIBS in CMake).
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 17, 2011, 02:51:28 am
today's problem. I've #included <SFML\Graphics>, but for some reason if I try to make an sf::RenderWindow or any other thing, it says that it's an unrecognized symbol. My autocomplete doesn't show up when I type the sf:: namespace, either. But when I tell VC to open the header files, it has no problems opening them, but apparently something's wrong here.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: sbroadfoot90 on November 17, 2011, 03:03:20 am
unrecognised symbol usually means that you aren't linking to the necessary libraries
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 17, 2011, 03:05:48 am
Quote from: "sbroadfoot90"
unrecognised symbol usually means that you aren't linking to the necessary libraries

I've linked to all of the -s-d.libs in the proper order. I'm fairly certain I copied all of the settings over from the project file that worked properly.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Tex Killer on November 17, 2011, 03:32:09 am
Quote from: "Bacu"
today's problem. I've #included <SFML\Graphics>...


It should be SFML/Graphics.hpp
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 17, 2011, 04:07:44 am
Quote from: "Tex Killer"
Quote from: "Bacu"
today's problem. I've #included <SFML\Graphics>...


It should be SFML/Graphics.hpp
That's what I meant.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 17, 2011, 04:40:22 am
problem routed. I was being a stupid ameteur and including .cpp files that ended up making some knotted wreath of infinite self-reference.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 18, 2011, 04:42:40 am
Peculiar error. no clue on this one.

my code all other source files compile and link properly.
Code: [Select]
#include <vector>
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>

#include "core.h"
#include "state.h"
#include "state_game.h"

Core::Core()
{
WIDTH = 800;
HEIGHT = 600;
DEPTH = 32;
TITLE = "SpaceGame";
Window.Create(sf::VideoMode(WIDTH, HEIGHT, DEPTH), TITLE);
states.push_back(new State_Game(this));
}

int Core::run()
{
while (Window.IsOpened())
{
while (Window.PollEvent(Event))
{
if (Event.Type == sf::Event::Closed) Window.Close();
}

if (states.empty())
{
return 1;
}

states.back()->process();
states.back()->render(Window);

Window.Display();
}
return 0;
}

void Core::popState()
{
Core::states.pop_back();
}

void Core::pushState(State* newState)
{
Core::states.push_back(newState);
}



Error output
Code: [Select]
1>c:\sfml\sfml2\include\sfml\window\window.hpp(479): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>          c:\sfml\sfml2\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>          c:\sfml\sfml2\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable'
1>          This diagnostic occurred in the compiler generated function 'sf::Window::Window(const sf::Window &)'
1>c:\sfml\sfml2\include\sfml\graphics\rendertarget.hpp(304): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>          c:\sfml\sfml2\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>          c:\sfml\sfml2\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable'
1>          This diagnostic occurred in the compiler generated function 'sf::RenderTarget::RenderTarget(const sf::RenderTarget &)'
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Laurent on November 18, 2011, 07:36:56 am
You're trying to copy a RenderWindow instance somewhere (this is probably implicit/hidden), which is not allowed.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: sbroadfoot90 on November 18, 2011, 08:46:41 am
Probably this line here.

Code: [Select]
     states.back()->render(Window);

I'm guessing the signature for render is

Code: [Select]
void render(sf::RenderWindow Window);

or something like that. Change it to

Code: [Select]
void render(const sf::RenderWindow& Window);
Title: Tutorial "Opening a window" code doesn't work righ
Post by: Bacu on November 18, 2011, 08:02:10 pm
Quote from: "sbroadfoot90"
Probably this line here.

Code: [Select]
     states.back()->render(Window);

I'm guessing the signature for render is

Code: [Select]
void render(sf::RenderWindow Window);

or something like that. Change it to

Code: [Select]
void render(const sf::RenderWindow& Window);

winner winner. that was it.
Title: Tutorial "Opening a window" code doesn't work righ
Post by: sbroadfoot90 on November 18, 2011, 11:23:58 pm
Actually, the methods you are likely to call on the RenderWindow in your render method are likely going to be Draw, and Display, both of which are not const methods. Thus, you should not pass a reference to const, but just a reference, as a reference to const will not allow you to call any methods that are not const-qualified.

Use

Code: [Select]
void render(sf::RenderWindow& Window);

basically any time you want to pass a non-integral type (not an int, float, etc) for example (std::vector, sf::RenderWindow, sf::Texture, etc), you should be passing by reference, not by copy. When you write

Code: [Select]
void someMethod(sf::Texture Texture);

you are copying ALL of the data that is held in the instance you are passing as a parameter, and you should opt to pass by reference


Code: [Select]
void someMethod(sf::Texture& Texture);

However, passing by reference allows you to change the original object that was passed in. To promise you wont change anything, you can pass a reference to const

Code: [Select]
void someMethod(const sf::Texture& Texture);

but then you are limited to using methods of sf::Texture that are const qualified for example

Code: [Select]
unsigned int GetWidth () const

however you would not be allowed to call

Code: [Select]
void Update (const Uint8 *pixels)