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

Author Topic: Tutorial "Opening a window" code doesn't work righ  (Read 9814 times)

0 Members and 1 Guest are viewing this topic.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Tutorial "Opening a window" code doesn't work righ
« Reply #15 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.

Bacu

  • Newbie
  • *
  • Posts: 21
    • View Profile
Tutorial "Opening a window" code doesn't work righ
« Reply #16 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.

Bacu

  • Newbie
  • *
  • Posts: 21
    • View Profile
Tutorial "Opening a window" code doesn't work righ
« Reply #17 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;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Tutorial "Opening a window" code doesn't work righ
« Reply #18 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.
Laurent Gomila - SFML developer

Bacu

  • Newbie
  • *
  • Posts: 21
    • View Profile
Tutorial "Opening a window" code doesn't work righ
« Reply #19 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.

Bacu

  • Newbie
  • *
  • Posts: 21
    • View Profile
Tutorial "Opening a window" code doesn't work righ
« Reply #20 on: November 11, 2011, 10:06:25 pm »
I was dividing by 0, duh. okay, problem solved.

Bacu

  • Newbie
  • *
  • Posts: 21
    • View Profile
Tutorial "Opening a window" code doesn't work righ
« Reply #21 on: November 11, 2011, 11:27:44 pm »
And now another question. How to statically link? all of the .lib are -d.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Tutorial "Opening a window" code doesn't work righ
« Reply #22 on: November 12, 2011, 10:57:34 am »
You must compile the static libs (disable BUILD_SHARED_LIBS in CMake).
Laurent Gomila - SFML developer

Bacu

  • Newbie
  • *
  • Posts: 21
    • View Profile
Tutorial "Opening a window" code doesn't work righ
« Reply #23 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.

sbroadfoot90

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Tutorial "Opening a window" code doesn't work righ
« Reply #24 on: November 17, 2011, 03:03:20 am »
unrecognised symbol usually means that you aren't linking to the necessary libraries

Bacu

  • Newbie
  • *
  • Posts: 21
    • View Profile
Tutorial "Opening a window" code doesn't work righ
« Reply #25 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.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Tutorial "Opening a window" code doesn't work righ
« Reply #26 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

Bacu

  • Newbie
  • *
  • Posts: 21
    • View Profile
Tutorial "Opening a window" code doesn't work righ
« Reply #27 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.

Bacu

  • Newbie
  • *
  • Posts: 21
    • View Profile
Tutorial "Opening a window" code doesn't work righ
« Reply #28 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.

Bacu

  • Newbie
  • *
  • Posts: 21
    • View Profile
Tutorial "Opening a window" code doesn't work righ
« Reply #29 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 &)'

 

anything