#include "Stdafx.h"
#include "Player.h"
#include "Game.h"
Game::Game()
{
}
void Game::HandleEvents(sf::RenderWindow& window, Player player)
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::Resized:
windowSize = window.getSize();
window.setSize(sf::Vector2u(windowSize.x, windowSize.y));
case sf::Event::KeyPressed:
switch (event.key.code)
{
case sf::Keyboard::Escape:
window.close();
break;
// Player Movement
case sf::Keyboard::W:
player.SetMovingUp(true);
case sf::Keyboard::S:
player.SetMovingDown(true);
case sf::Keyboard::A:
player.SetMovingLeft(true);
case sf::Keyboard::D:
player.SetMovingRight(true);
}
case sf::Event::KeyReleased:
switch (event.key.code)
{
// Player Movement
case sf::Keyboard::W:
player.SetMovingUp(false);
case sf::Keyboard::S:
player.SetMovingDown(false);
case sf::Keyboard::A:
player.SetMovingLeft(false);
case sf::Keyboard::D:
player.SetMovingRight(false);
}
}
}
}
void Game::LoadSpriteIntoStack(sf::Sprite sprite)
{
stackOfSprites.push_back(sprite);
}
void Game::DrawStackOfSprites(sf::RenderWindow& window)
{
for (unsignedint i = 0; i < stackOfSprites.size(); i++)
{
window.draw(stackOfSprites[i]);
}
}
Game.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#pragma once
class Game
{
public:
Game();
// Handle Events
void HandleEvents(sf::RenderWindow& window, Player player);
void LoadSpriteIntoStack(sf::Sprite sprite);
void DrawStackOfSprites(sf::RenderWindow& window);
private:
sf::Vector2u windowSize;
std::vector<sf::Sprite> stackOfSprites;
};
Player.cpp (I took out the code with no relevence)
Your game object holds sprite objects. No sprite held by the game object is in any way connected or linked to the playerSprite in the player object. The only sprites that are drawn in this code are those contained in the game object, so updating the playerSprite in the player object has no effect.
If you want the a Game object to refer to an existing sprite, it should not store a copy of the sprite. It should store some sort of reference (in a general sense - a pointer would work) to a sprite.
So instead of std::vector<sf::Sprite> stackOfSprites; I should have std::vector<sf::Sprite*> stackOfSprites;
However i now get errors here:
1 2 3 4 5 6 7 8 9 10 11 12
void Game::LoadSpriteIntoStack(sf::Sprite* sprite)
{
stackOfSprites->push_back(sprite);
}
void Game::DrawStackOfSprites(sf::RenderWindow& window)
{
for (unsignedint i = 0; i < stackOfSprites.size(); i++)
{
window->draw(stackOfSprites[i]);
}
}
Error 1 error C2819: type 'std::vector<sf::Sprite *,std::allocator<_Ty>>' does not have an overloaded member 'operator ->' c:\users\dylan\documents\visual studio 2013\projects\sfml game\helpwitharrow\trunk\game.cpp 60 1 SFML Game
Error 2 error C2232: '->std::vector<sf::Sprite *,std::allocator<_Ty>>::push_back' : left operand has 'class' type, use '.' c:\users\dylan\documents\visual studio 2013\projects\sfml game\helpwitharrow\trunk\game.cpp 60 1 SFML Game
Error 3 error C2819: type 'sf::RenderWindow' does not have an overloaded member 'operator ->' c:\users\dylan\documents\visual studio 2013\projects\sfml game\helpwitharrow\trunk\game.cpp 67 1 SFML Game
Error 4 error C2232: '->sf::RenderTarget::draw' : left operand has 'class' type, use '.' c:\users\dylan\documents\visual studio 2013\projects\sfml game\helpwitharrow\trunk\game.cpp 67 1 SFML Game
5 IntelliSense: expression must have pointer type c:\Users\Dylan\Documents\Visual Studio 2013\Projects\SFML Game\helpwitharrow\trunk\Game.cpp 60 2 SFML Game
6 IntelliSense: expression must have pointer type c:\Users\Dylan\Documents\Visual Studio 2013\Projects\SFML Game\helpwitharrow\trunk\Game.cpp 67 3 SFML Game
Not sure if that is all I would have to change. But I would like to fix these errors before I continue :)