Where do i put 'sf::RenderWindow?'

Question is in the comments section of my code. But in short i don't know where to my sf::RenderWindow

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  #include "stdafx.h"
#include "EventHandling.h"

EventHandling Events;

int main()
{
	std::ifstream openfile("map.txt");

	sf::Texture tileTexture;
	sf::Sprite tiles;

	sf::Vector2i map[100][100];
	sf::Vector2i loadCounter = sf::Vector2i(0, 0);
	
	// Load textures into map
	if (openfile.is_open())
	{
		std::string tileLocation;
		openfile >> tileLocation;
		tileTexture.loadFromFile(tileLocation);
		tiles.setTexture(tileTexture);

		while (!openfile.eof())
		{
			std::string str;
			openfile >> str;
			char x = str[0], y = str[2];
			
			if (!isdigit(x) || !isdigit(y))
				map[loadCounter.x][loadCounter.y] = sf::Vector2i(-1, -1);
			else
				map[loadCounter.x][loadCounter.y] = sf::Vector2i(x - '0', y - '0');

			if (openfile.peek() == '\n')
			{
				loadCounter.x = 0;
				loadCounter.y++;
			}
			else
				loadCounter.x++;
		}

		loadCounter.y++;
	}

	//	Create Window (Where do i put this? Because if i take it from main 'this' isn't recongnised)
	sf::RenderWindow window(sf::VideoMode(1280, 720, 32), "Loading Maps[easy]", sf::Style::Fullscreen);

	Events.HandleEvents();

		// this 
		window.clear();

		for (int i = 0; i < loadCounter.x; i++)
		{
			for (int j = 0; j < loadCounter.y; j++)
			{
				if (map[i][j].x != -1 && map[i][j].y != -1)
				{
					tiles.setPosition(i * 32, j * 32);
					tiles.setTextureRect(sf::IntRect(map[i][j].x * 32, map[i][j].y * 32, 32, 32));
					window.draw(tiles);
				}
			}
		}

		window.display();
	}
}


stdafx.h
1
2
3
4
5
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <cctype>
#include <string> 


EventHandling.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "stdafx.h"
#include "EventHandling.h"

EventHandling::EventHandling()
{
}

EventHandling::~EventHandling()
{
}

void EventHandling::HandleEvents()
{
	// But while it is in main, this dosen't work
	while (window.IsOpen())
	{
		//	Event Handling
		sf::Event event;
		while (window.pollEvent(event))
		{
			switch (event.type)
			{
			case sf::Event::Closed:
				window.close();
				break;

			case sf::Event::KeyPressed:
				switch (event.key.code)
				{
				case sf::Keyboard::Escape:
					window.close();
					break;
				}

			}
		}
	}
}


EventHandling.h
1
2
3
4
5
6
7
8
9
10
#pragma once

class EventHandling
{
public:
	EventHandling();
	~EventHandling();

	void HandleEvents();
};


Appreciate any help :)
Last edited on
Put it at the very top.

1
2
3
int WIDTH = 1200;
int HEIGHT = 900;
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "SFML works!");


The first thing you want to do is open/render the window.
Thanks for the quick reply

The top at which file?

main.cpp
1
2
3
4
//here
#include "stdafx.h"
#include "EventHandling.h"
//here? 
Last edited on
Sorry I should have been more clear. Inside main, at the top

1
2
3
4
5
6
int main()
{
	
	int WIDTH = 1200;
	int HEIGHT = 900;
	sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "SFML works!");


Here is a small example on the SFML website - http://www.sfml-dev.org/tutorials/2.3/start-vc.php

All you want to do, is create the window, which is what this does. Then nothing more, the window is there until you close it, that's why you put it as basically the very first thing.
Last edited on
It's no problem :)

I fixed the errors in main.cpp, but still have this problem in EventHandling (there is a comment at the problem):

EventHandling.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "stdafx.h"
#include "EventHandling.h"

EventHandling::EventHandling()
{
}

EventHandling::~EventHandling()
{
}

void EventHandling::HandleEvents()
{
	// But while it is in main, this dosen't work
	while (window.IsOpen())
	{
		//	Event Handling
		sf::Event event;
		while (window.pollEvent(event))
		{
			switch (event.type)
			{
			case sf::Event::Closed:
				window.close();
				break;

			case sf::Event::KeyPressed:
				switch (event.key.code)
				{
				case sf::Keyboard::Escape:
					window.close();
					break;
				}

			}
		}
	}
}


Error 2 error C2065: 'window' : undeclared identifier c:\users\dylan\documents\visual studio 2013\projects\tile mapping game\eventhandling.cpp 14 1 Tile Mapping Game
Error 3 error C2228: left of '.IsOpen' must have class/struct/union c:\users\dylan\documents\visual studio 2013\projects\tile mapping game\eventhandling.cpp 14 1 Tile Mapping Game
Error 5 IntelliSense: identifier "window" is undefined c:\Users\Dylan\Documents\Visual Studio 2013\Projects\Tile Mapping Game\EventHandling.cpp 14 9 Tile Mapping Game
Last edited on
Ofc your code doesnt work. Window is not defined in that class. You're gonna have to send it in via the function parameters to be able to access it in your function.
Last edited on
sorry, still really new to programming, I didn't think of function parameters. Thanks for all your help :) I'll let you know If I fix it
Last edited on
I changed my code, this got rid of my errors. Now I get these two errors:

 
void EventHandling::HandleEvents(sf::RenderWindow window)


 
	Events.HandleEvents(window);


Error 2 error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable' c:\users\dylan\documents\visual studio 2013\projects\tile mapping game\sfml-2.3.2\include\sfml\window\window.hpp 521 1 Tile Mapping Game
Error 3 error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable' c:\users\dylan\documents\visual studio 2013\projects\tile mapping game\sfml-2.3.2\include\sfml\graphics\rendertarget.hpp 419 1 Tile Mapping Game
That's probably because The window is not copyable and there for can't be passed by value. Try sending it as a reference.

void EventHandling::HandleEvents(sf::RenderWindow& window) // change to this
Last edited on
Thank you so much, I appreciate it :)
You're very welcome :)
Topic archived. No new replies allowed.