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

Author Topic: Getting an object from another file  (Read 2853 times)

0 Members and 1 Guest are viewing this topic.

jw6282000

  • Newbie
  • *
  • Posts: 2
    • View Profile
Getting an object from another file
« on: April 12, 2011, 03:48:19 am »
I am trying to make a window class and an input handling class. I created an instance of sf::RenderWindow in the Window.cpp. I need to use this object in my Input.cpp file. What would be the best way of doing this?
Window.cpp
Code: [Select]

#include "Window.hpp"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

namespace
{
       sf::RenderWindow window; //This is the object I need
       sf::Event Event;
}

Window::Window(int sizeX, int sizeY)
{
       width = sizeX;
       height = sizeY;
       scaleX = (scaleX * (width / 800));
       scaleY = (scaleY * (height / 600));
}


Input.cpp
Code: [Select]

#include "Input.hpp"
#include "Window.hpp"

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

namespace
{
       const sf::Input &input = window.GetInput(); //window not declared in this scope
}

Input::Input()
{
      while (input.IsKeyDown(sf::Key::Left)){
              Input::LeftKeyDown = true;}
      Input::LeftKeyDown = false;

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Re: Getting an object from another file
« Reply #1 on: April 12, 2011, 04:00:17 am »
I can think of three ways to do this, but there is probably more (google these):
 * Extern keyword
 * Singleton
 * Accessors to a pointer or reference


Here's how to use extern if I remember right...
Input.cpp
Code: [Select]
#include "Input.hpp"
#include "Window.hpp"

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

namespace
{
       extern sf::RenderWindow window;

       const sf::Input &input = window.GetInput(); //window not declared in this scope
}

Input::Input()
{
      while (input.IsKeyDown(sf::Key::Left)){
              Input::LeftKeyDown = true;}
      Input::LeftKeyDown = false;

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Getting an object from another file
« Reply #2 on: April 12, 2011, 10:01:48 am »
Externs are BAD! Real bad! Danger danger!
Pass around references. Don't over-complicate it for yourself.

Though how much experience do you have with C++?
Since you are saying "Pass a variable to another file", What you should be asking is "How do I send a variable to another object". The files doesn't exists in the executable after the application has compiled. Also the variables should be placed as instance variables in the class Window, not as global variables in a namespace.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

jw6282000

  • Newbie
  • *
  • Posts: 2
    • View Profile
Getting an object from another file
« Reply #3 on: April 14, 2011, 03:08:45 am »
I haven't known C++ for long. I am learning by making a game with SFML.

I tried making a pointer of the instance of the sf::RenderWindow class. I couldn't quite get it to work. I also tried declaring the instance inside of my Window class, but I couldn't access it in my Input.cpp, so I just put it inside of an unnamed namespace (learned that from someone else's code).

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Getting an object from another file
« Reply #4 on: April 14, 2011, 11:34:14 am »
How long have you come in your studies in C++? Are you learning from a teacher or by yourself?
What you want to do is pretty basic but I need to know how far you are so I know what level I need to explain things in.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Fred_FS

  • Newbie
  • *
  • Posts: 48
    • View Profile
Getting an object from another file
« Reply #5 on: April 14, 2011, 11:48:25 am »
Quote from: "jw6282000"
I haven't known C++ for long. I am learning by making a game with SFML.

I would rather not recommend that. C++ is a very complex programming language. So you should learn to programm C++ first before programming with SFML. There are languages you can learn while programming a little game, but C++ is definitely none of those. So it would also be better for you if you spend some time in learing C++.

I read in another forum about unnamed namespaces a few days ago, and I am supprised at the creativity of some programmers who don't want to use global variables(because everyone says that they are evil). So they are using singletons(but now everyone says singletons are evil, too) and obviously unnamed namespaces(I would say: evil ;)).
Just learn C++(use a good book and not bad source code^^) and you will know about classes, object oriented design and other good stuff and your programms will be much better :).