Jump to content

  • Log In with Google      Sign In   
  • Create Account


OOP Newb: Am I doing this right?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 hkjsdhkjhflajhlaksjhdkhfsk   Members   -  Reputation: 118

Like
1Likes
Like

Posted 05 April 2013 - 02:41 PM

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();
	}
}

 

 



Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6974

Like
1Likes
Like

Posted 05 April 2013 - 03:04 PM

You never set state to anything other than NotRunning.

 

Other than that, I don't see anything that sets off any red flags.


[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#3 Wenzil   Members   -  Reputation: 592

Like
4Likes
Like

Posted 05 April 2013 - 06:49 PM

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.

 

 



#4 Ludus   Members   -  Reputation: 970

Like
1Likes
Like

Posted 05 April 2013 - 09:59 PM

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.



#5 EWClay   Members   -  Reputation: 659

Like
1Likes
Like

Posted 06 April 2013 - 12:30 AM

There's no reason for all those statics in Program. It can just be a class with normal members like any other.

#6 hkjsdhkjhflajhlaksjhdkhfsk   Members   -  Reputation: 118

Like
0Likes
Like

Posted 06 April 2013 - 04:28 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS