Class initialisation list problems (actually it's probably a variable scope problem)

Hi All

I have a class called GameManager and a class called Player.

The GameManager class has a method called gameLoop and within this gameLoop I'm instantiating a player object and printing out a few variables (playerRect.x, playerRect.y, playerRect.h, playerRect.w) which should be set by the Player constructor initialisation list (in Player.h) to 32 for w(idth) and h(eight) and 100 for x and y but I'm getting rubbish values instead and I'm not sure why.

The structure of my code could also be better I'm sure so any ideas on how / what to decouple would be appreciated.

Code can be found at: https://github.com/VDoggStudios/PuckCollector

Game.cpp - Entry point.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "GameManager.h"
#include "Player.h"

int main(int argc, char ** argv)
{
	{
		
		GameManager gameManager;
		gameManager.run();
	}

	std::cout << "Press return to exit..." << std::endl;
	std::cin.get();

	return 0;
}


GameManager.h
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
#pragma once

#ifndef GAMEMANAGER_H_
#define GAMEMANAGER_H_

#include <iostream>

#include <SDL\SDL.h>

#include "GameState.h"
#include "Player.h"

class GameManager
{
public:
	GameManager();
	~GameManager();
	void run();
	void handleInput();
	void draw(const SDL_Rect rect);
	
private:
	void initialiseSystem();
	void gameLoop();

	const char *_gameTitle = "Puck Collector";
	SDL_Window *_window;
	SDL_Renderer *_renderer;
	int _screenWidth;
	int _screenHeight;
	GameState _gameState;
	
};
#endif 


GameManager.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "GameManager.h"
#include "GameState.h"
#include "Player.h"

GameManager::GameManager() : _window(nullptr), _renderer(nullptr), _screenWidth(1024), _screenHeight(768) 
{
	std::cout << "GameManager created..." << std::endl;
	GameState _gameState(GameState::PLAY);
}

GameManager::~GameManager()
{
	std::cout << "GameManager destroyed..." << std::endl;
}

void GameManager::run()
{
	initialiseSystem();
	gameLoop();
}

void GameManager::initialiseSystem()
{
	// Initialise SDL
	SDL_Init(SDL_INIT_EVERYTHING);

	// Create an SDL window
	_window = SDL_CreateWindow(_gameTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _screenWidth, _screenHeight, SDL_WINDOW_SHOWN);

	// Create an SDL renderer
	_renderer = SDL_CreateRenderer(_window, -1, 0);

	/* Select the color for drawing. It is set to red here. */
	SDL_SetRenderDrawColor(_renderer, 255, 0, 0, 255);
	
	/* Clear the entire screen to our selected color. */
	SDL_RenderClear(_renderer);
}

void GameManager::gameLoop()
{

	std::cout << "gameLoop() running ..." << std::endl;

	Player player1;

	// print x position for debug
	std::cout << player1.playerRect.x << std::endl;
	std::cout << player1.playerRect.y << std::endl;
	std::cout << player1.playerRect.w << std::endl;
	std::cout << player1.playerRect.h << std::endl;

	player1.getPlayerRect();

	while (_gameState != GameState::EXIT) {
		
		// Handle input
		handleInput();
		
		// Move player, physics, etc

		// Draw
		//draw(player.getPlayerRect());

	}

	std::cout << "gameLoop() ended ..." << std::endl;
}


void GameManager::handleInput()
{
	// check for event, determine type and act accordingly
	SDL_Event evnt;

	while (SDL_PollEvent(&evnt))
	{
		switch (evnt.type) { 
			case SDL_QUIT: // user clicked x to close window
				SDL_Quit();
				_gameState = GameState::EXIT;
				break;
		}
	}

}

void GameManager::draw(const SDL_Rect rect)
{
	
	SDL_SetRenderDrawColor(_renderer, 255, 255, 255, 255);

	SDL_RenderDrawRect(_renderer, &rect);

	// Show back buffer
	SDL_RenderPresent(_renderer);
}


Player.h
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
#pragma once

#ifndef PLAYER_H_
#define PLAYER_H_

#include <iostream>

#include <SDL\SDL.h>

class Player
{
public:
	Player();
	~Player();
	void shout() { std::cout << "Hey!" << std::endl; };
	SDL_Rect getPlayerRect();


	SDL_Rect playerRect;
	

private:

	
	std::string _name;
	int _width;
	int _height;
	int _xPos;
	int _yPos;


};

#endif 


Player.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
#include "Player.h"

#include <SDL\SDL.h>
#include<iostream>

Player::Player() :_name("Player 1"),_width(32), _height(32), _xPos(0), _yPos(0)
{

	std::cout << "Player created..." << std::endl;

	// Create rectangle
	SDL_Rect playerRect;
	

	// Define rectangle
	playerRect.w = _width;
	playerRect.h = _height;
	playerRect.x = _xPos;
	playerRect.y = _yPos;


}

Player::~Player()
{
	std::cout << "Player destroyed..." << std::endl;
}

SDL_Rect Player::getPlayerRect() {

	std::cout << "This is from get player rect method: " << playerRect.x << std::endl << std::endl << std::endl;
	return playerRect;
}
Last edited on
player.cpp line 12: You're declaring a local variable named playerRect. This hides the member variable playerRect. The local playerRect (and the values you set) goes out of scope when your constructor exits.
Thanks!
Topic archived. No new replies allowed.