Jump to content

  • Log In with Google      Sign In   
  • Create Account


hkjsdhkjhflajhlaksjhdkhfsk

Member Since 26 Mar 2013
Offline Last Active Jun 20 2013 07:55 AM
-----

#5050412 OOP Newb: Am I doing this right?

Posted by hkjsdhkjhflajhlaksjhdkhfsk on 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();
	}
}

 

 




PARTNERS