Hi. I am working on making a game with the SFML library. I was wondering how I could use an instance of a class from a different file. My code looks something like this:
Window.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
namespace
{
sf::RenderWindow window; //This is the object I want to use
sf::Event Event;
}
Window::Window(int sizeX, int sizeY)
{
width = sizeX;
height = sizeY;
scaleX = (scaleX * (width / 800));
scaleY = (scaleY * (height / 600));
}
Input.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include "Input.hpp"
#include "Window.hpp"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
namespace
{
const sf::Input &input = window.GetInput(); //This returns "'window' was not declared in this scope"
}
Input::Input()
{
while (input.IsKeyDown(sf::Key::Left)){
Input::LeftKeyDown = true;}
Input::LeftKeyDown = false;
...
This section of the code just provides it to the other files. Now you have to actually create the variable. Create the variable normally inside of your Main.cpp, but make sure you create it globally.