• Content count

  • Joined

  • Last visited

Community Reputation

119 Neutral

About darraghdev

  • Rank
  1. OOP Newb: Am I doing this right?

    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.
  2. OOP Newb: Am I doing this right?

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