Sign in to follow this  
darraghdev

OOP Newb: Am I doing this right?

Recommended Posts

Hey guys, sorry this isn't really a question I'd just like some feedback. I started a program today to get a sprite moving and hopefully jumping (at some point tomorrow) tongue.png

 

Would anyone care to comment on this wall of code, am I doing anything really silly yet? Does it make sense to include a player object as a member of a game object and use a pointer to the sprite member of the player object to move it?

 

Main.cpp

#include "Program.h"

int main()
{
	Program::start();

	return 0;
}

 

Program.h

#ifndef PROGRAM_H
#define PROGRAM_H

#include "SFML/Graphics.hpp"

class Program {
	enum stateType { NotRunning, ShowingSplash, ShowingMenu, Playing, Closing };
	static stateType state;
	static sf::RenderWindow window;
	static const std::string NAME;
	static const unsigned short WIDTH;
	static const unsigned short HEIGHT;
	static const unsigned short BITS;
public:
	static void start();
	static void waitForInput();
};

#endif

 

Program.cpp

#include "Program.h"
#include "Splash.h"
#include "Menu.h"
#include "Game.h"

void Program::start() {
	if (state != NotRunning) {
		return;
	}

	window.create(sf::VideoMode(WIDTH, HEIGHT, BITS), NAME);

	Splash::load(window);

	waitForInput();

	Menu::load(window);

	waitForInput();

	Game newGame;
	newGame.load(window);
	newGame.play(window);
}

void Program::waitForInput() {
	sf::Event currentEvent;
	while (window.waitEvent(currentEvent)) {
		if (currentEvent.type == sf::Event::EventType::KeyPressed || 
			currentEvent.type == sf::Event::EventType::MouseButtonPressed) {
			return;
		}
	}
}

Program::stateType Program::state = NotRunning;
sf::RenderWindow Program::window;
const std::string Program::NAME = "Platformer";
const unsigned short Program::WIDTH = 800;
const unsigned short Program::HEIGHT = 600;
const unsigned short Program::BITS = 32;

 

Splash.h

#ifndef SPLASH_H
#define SPLASH_H

#include "SFML/Graphics.hpp"

class Splash {
public:
	static void load(sf::RenderWindow &);
};

#endif

 

Splash.cpp

#include "Splash.h"

void Splash::load(sf::RenderWindow &window) {
	window.clear(sf::Color::Color(255, 0, 0));
	window.display();
}

 

Menu.h

#ifndef MENU_H
#define MENU_H

#include "SFML/Graphics.hpp"

class Menu {
public:
	static void load(sf::RenderWindow &);
};

#endif

 

Menu.cpp

#include "Menu.h"

void Menu::load(sf::RenderWindow &window) {
	window.clear(sf::Color::Color(0, 255, 0));
	window.display();
}

 

Player.h

#ifndef PLAYER_H
#define PLAYER_H

#include "SFML/Graphics.hpp"

class Player {
	sf::Image image;
	sf::Texture texture;
	sf::Sprite sprite;
	bool isLoaded;
public:
	Player();
	sf::Sprite* getSpritePtr();
};

#endif

 

Player.cpp

#include "Player.h"

Player::Player() {
	if (image.loadFromFile("images/player.png")) {
		texture.loadFromImage(image);
		sprite.setTexture(texture);
		isLoaded = true;
	}
	else {
		isLoaded = false;
	}
}

sf::Sprite* Player::getSpritePtr() {
	return &sprite;
}

 

Game.h

#ifndef GAME_H
#define GAME_H

#include "SFML/Graphics.hpp"
#include "Player.h"

class Game {
	Player player;
public:
	void load(sf::RenderWindow &);
	void play(sf::RenderWindow &);
};

#endif

 

Game.cpp

#include "Game.h"

void Game::load(sf::RenderWindow &window) {
	const unsigned int WINDOW_WIDTH = window.getSize().x;
	const unsigned int WINDOW_HEIGHT = window.getSize().y;

	sf::Sprite* playerSpritePtr = player.getSpritePtr();

	const float PLAYER_WIDTH = playerSpritePtr->getScale().x;
	const float PLAYER_HEIGHT = playerSpritePtr->getScale().y;

	playerSpritePtr->setOrigin(PLAYER_WIDTH / 2, PLAYER_HEIGHT / 2);
	playerSpritePtr->setPosition(WINDOW_WIDTH / 2.0f, WINDOW_HEIGHT - 50.0f);

	window.clear(sf::Color::Color(0, 0, 0));
	window.draw(*playerSpritePtr);
	window.display();
}

void Game::play(sf::RenderWindow &window) {
	sf::Sprite* playerSpritePtr = player.getSpritePtr();
	while (true) {
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Q)) {
			return;
		}
		else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) {
			playerSpritePtr->move(-0.1, 0);
		}
		else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) {
			playerSpritePtr->move(0.1, 0);
		}
		window.clear(sf::Color::Color(0, 0, 0));
		window.draw(*playerSpritePtr);
		window.display();
	}
}

 

 

Share this post


Link to post
Share on other sites

This is just a general OOP guideline, but it fits this particular example very well. According to Law of Demeter (see http://en.wikipedia.org/wiki/Law_of_Demeter), the clients of your Player class (in this case, Game) should not know about the Sprite subcomponent. So in your Player class you would have a Move(float x, float y) method defined like this:

 

void Player::Move(float x, float y) {
    sprite.move(x, y)
}

 

Then, inside Game.cpp, instead of moving the player's sprite directly, you just tell the player to move, and the player will take care of moving its sprite.

 

Similarly, you should define the Player constructor to set the sprite's origin and initial position, instead of having Game.cpp do it.

 

 

Share this post


Link to post
Share on other sites

Strictly speaking, in object oriented programming an object should handle everything that deals with itself. In your code, you have the Game object handling movement as well as rendering the sprite of the player. These should be functions within the player class itself.

 

Also, it seems that you're mixing the logic portion of your game loop with input. All input from the player should be gathered first, and then logic (such as movement) should be done afterwards.

Share this post


Link to post
Share on other sites

Thanks guys, this stuff is really helpful. I thought it felt odd to encapsulate player members then provide a pointer to one, I'll implement move and jump operations in that class. Not sure what I was thinking with the statics, guess I didn't want to make an program instance because I only needed one, same with splash and menu. Also totally forgot about state.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this