2
\$\begingroup\$

I'm trying to mimic the SFML library to be suitable for text-based games. The library called MSLIB in GitHub here.

Here is a sample of the library implementation. It prints gray square onto the screen and it can be controlled by arrow keys to move it up, down, right and left.

How can I improve MSLIB?

Game.h

#ifndef GAME_H
#define GAME_H

#include <MSLIB\Image.h>
#include <MSLIB\RenderConsole.h>
#include <MSLIB\Time.h>
#include <MSLIB\Clock.h>

class Game : public ms::NonCopyable
{
public:
    Game();
    void                    run();

private:
    void                    processEvents();
    void                    update(ms::Time elapsedTime);
    void                    render();

    void                    updateStatistics(ms::Time elapsedTime);
    void                    handlePlayerInput(ms::Keyboard::Key key, bool isPressed);


private:
    static const float      PlayerSpeed;
    static const ms::Time   TimePerFrame;



    ms::RenderConsole mConsole;
    ms::Image mPlayer;

    bool isRunning;

    std::string             mStatisticsText;
    std::string             mStatisticsText1;
    ms::Time                mStatisticsUpdateTime;

    std::size_t             mStatisticsNumFrames;
    bool                    mIsMovingUp;
    bool                    mIsMovingDown;
    bool                    mIsMovingRight;
    bool                    mIsMovingLeft;
};

#endif // GAME_H

Game.cpp

#include "Game.h"

const float Game::PlayerSpeed = 10.f;
const ms::Time Game::TimePerFrame = ms::seconds(1.f / 60.f);

Game::Game()
    : mConsole(350, 50, 80, 50, "Simple Game")
    , mPlayer()
    , isRunning(true)
    , mStatisticsText()
    , mStatisticsText1()
    , mStatisticsUpdateTime()
    , mStatisticsNumFrames(0)
    , mIsMovingUp(false)
    , mIsMovingDown(false)
    , mIsMovingRight(false)
    , mIsMovingLeft(false)
{
    mPlayer.setFillCharacter(ms::Character::Sold);
    mPlayer.setFillColor(ms::Color::Gray);
    mPlayer.setPosition(40, 30);
    mPlayer.setSize(5, 3);
}

void Game::run()
{
    ms::Clock clock;
    ms::Time timeSinceLastUpdate = ms::Time::Zero;
    while (isRunning)
    {
        ms::Time elapsedTime = clock.restart();
        timeSinceLastUpdate += elapsedTime;

        while (timeSinceLastUpdate > TimePerFrame)
        {
            timeSinceLastUpdate -= TimePerFrame;

            processEvents();
            update(TimePerFrame);
        }

        updateStatistics(elapsedTime);

        render();
    }
}

void Game::processEvents()
{
    ms::Event event;
    while (mConsole.pollEvent(event))
    {
        switch (event.type)
        {
        case ms::Event::KeyPressed:
            if (event.key.code == ms::Keyboard::Escape)
            {
                isRunning = false;
            }

            handlePlayerInput(event.key.code, true);
            break;

        case ms::Event::KeyReleased:
            handlePlayerInput(event.key.code, false);
            break;
        }
    }
}

void Game::update(ms::Time elapsedTime)
{
    ms::Vector2f movement(0.f, 0.f);
    if (mIsMovingUp)
    {
        movement.y -= PlayerSpeed;
    }
    if (mIsMovingDown)
    {
        movement.y += PlayerSpeed;
    }
    if (mIsMovingLeft)
    {
        movement.x -= PlayerSpeed;
    }
    if (mIsMovingRight)
    {
        movement.x += PlayerSpeed;
    }

    mPlayer.move(movement * elapsedTime.asSeconds());
}

void Game::render()
{
    mConsole.clear();

    mConsole.draw(mPlayer);

    mConsole.draw(mStatisticsText, 0, 0);
    mConsole.draw(mStatisticsText1, 0, 1);

    mConsole.display();
}

void Game::updateStatistics(ms::Time elapsedTime)
{
    mStatisticsUpdateTime += elapsedTime;
    mStatisticsNumFrames += 1;

    if (mStatisticsUpdateTime >= ms::seconds(1.0f))
    {
        mStatisticsText  =  "Frames / Second = " + std::to_string(mStatisticsNumFrames);
        mStatisticsText1 =  "Time / Update = " + std::to_string(mStatisticsUpdateTime.asMicroseconds() / mStatisticsNumFrames) + "us";

        mStatisticsUpdateTime -= ms::seconds(1.0f);
        mStatisticsNumFrames = 0;
    }
}

void Game::handlePlayerInput(ms::Keyboard::Key key, bool isPressed)
{
    if (key == ms::Keyboard::Up)
    {
        mIsMovingUp = isPressed;
    }
    else if (key == ms::Keyboard::Down)
    {
        mIsMovingDown = isPressed;
    }
    else if (key == ms::Keyboard::Left)
    {
        mIsMovingLeft = isPressed;
    }
    else if (key == ms::Keyboard::Right)
    {
        mIsMovingRight = isPressed;
    }
}

main.cpp

#include "Game.h"

int main()
{
    Game game;
    game.run();
}
\$\endgroup\$
  • 2
    \$\begingroup\$ Regarding your code in the repository, it has little to no comments, you should improve that. Good documentation is a key point for a successful Open Source project. Also, provide a descriptive README for your main GitHub page. \$\endgroup\$ – glampert Feb 5 '15 at 16:22
  • 2
    \$\begingroup\$ The Doxygen tool might be useful to you. \$\endgroup\$ – glampert Feb 5 '15 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.