Skip to content

Commit

Permalink
Added StandardInput class inheriting abstract class Input.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Oct 2, 2024
1 parent ac86eaa commit 275f6af
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 73 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ set(${PROJECT_NAME}_SOURCES
src/Resources.cpp
src/ResourceType.h
src/Input.h
src/Input.cpp
src/InputAction.h
src/TankType.h
src/Bullet.cpp
Expand All @@ -65,6 +64,8 @@ set(${PROJECT_NAME}_SOURCES
src/Display.cpp
src/Utils.h
src/Utils.cpp
src/StandardInput.h
src/StandardInput.cpp
src/map/Water.cpp
src/map/Water.h
src/map/Steel.cpp
Expand Down
7 changes: 4 additions & 3 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#include "Bullet.h"
#include "Config.h"
#include "Display.h"
#include "Input.h"
#include "InputAction.h"
#include "Level.h"
#include "Map.h"
#include "MapUtils.h"
#include "PointUtils.h"
#include "Resources.h"
#include "StandardInput.h"
#include "Tank.h"
#include "Utils.h"

Expand Down Expand Up @@ -87,7 +87,7 @@ bool Game::play(Level level)
std::list<Tank> tanks{map.loadMap(levelContent)};
levelContent.close();

Input input;
StandardInput input;
display_.clearScreenWithBlack();
std::list<Bullet> bullets;

Expand Down Expand Up @@ -115,13 +115,14 @@ void Game::control(Map& map, std::list<Tank>& tanks, std::list<Bullet>& bullets)
{
moveBullets(bullets, tanks, map);

StandardInput input;
for (auto& tank : tanks)
{
Direction direction{Direction::UP};
if (tank.isPlayerControlled())
{
setPower(tank, map);
const auto actions{Input::getGameActions()};
const auto actions{input.getGameActions()};
const auto now{std::chrono::system_clock::now()};
if ((actions.find(InputAction::FIRE) != actions.end()) &&
tank.canFire(now))
Expand Down
1 change: 0 additions & 1 deletion src/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ struct Point;
class Tank;
class Map;
class Display;
class Input;
enum class InputAction : char;
class Bullet;
enum class Level : char;
Expand Down
50 changes: 5 additions & 45 deletions src/Input.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,16 @@
#include <set>
#include <utility>

struct ALLEGRO_EVENT_QUEUE;
union ALLEGRO_EVENT;
struct ALLEGRO_KEYBOARD_STATE;
enum class InputAction : char;

class Input final
class Input
{
public:
Input();
virtual ~Input() = default;

static void init();
virtual InputAction getMenuAction() = 0;

InputAction getMenuAction();
virtual std::set<InputAction> getGameActions() = 0;

static std::set<InputAction> getGameActions();

std::pair<int, int> getMousePosition() const;

private:
static InputAction getCommonAction(const ALLEGRO_EVENT& event);

static bool itemPicked(const ALLEGRO_EVENT& event);

static bool fired(const ALLEGRO_KEYBOARD_STATE& keyState);

static bool userWantToExit(const ALLEGRO_EVENT& event);

static bool keyEscapeUsed(const ALLEGRO_EVENT& event);

static bool keyUpUsed(const ALLEGRO_EVENT& event);

static bool keyDownUsed(const ALLEGRO_EVENT& event);

static bool keyUpPressed(const ALLEGRO_KEYBOARD_STATE& keyState);

static bool keyDownPressed(const ALLEGRO_KEYBOARD_STATE& keyState);

static bool keyLeftPressed(const ALLEGRO_KEYBOARD_STATE& keyState);

static bool keyRightPressed(const ALLEGRO_KEYBOARD_STATE& keyState);

static bool keyEnterUsed(const ALLEGRO_EVENT& event);

static bool keySpaceUsed(const ALLEGRO_EVENT& event);

static bool mouseClickUsed(const ALLEGRO_EVENT& event);

ALLEGRO_EVENT_QUEUE* events_;

int mouseX_{0};

int mouseY_{0};
virtual std::pair<int, int> getMousePosition() const = 0;
};
40 changes: 20 additions & 20 deletions src/Input.cpp → src/StandardInput.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "Input.h"
#include "StandardInput.h"

#include <allegro5/allegro.h>

#include "Config.h"
#include "InputAction.h"

Input::Input() : events_(::al_create_event_queue())
StandardInput::StandardInput() : events_(::al_create_event_queue())
{
ALLEGRO_TIMER* timer{
::al_create_timer(1.0 / Config::getInstance().getFps())};
Expand All @@ -17,13 +17,13 @@ Input::Input() : events_(::al_create_event_queue())
::al_start_timer(timer);
}

void Input::init()
void StandardInput::init()
{
::al_install_keyboard();
::al_install_mouse();
}

InputAction Input::getMenuAction()
InputAction StandardInput::getMenuAction()
{
ALLEGRO_EVENT event{};
::al_wait_for_event(events_, &event);
Expand Down Expand Up @@ -59,7 +59,7 @@ InputAction Input::getMenuAction()
return InputAction::MOUSE_MOVE;
}

std::set<InputAction> Input::getGameActions()
std::set<InputAction> StandardInput::getGameActions()
{
ALLEGRO_KEYBOARD_STATE keyState;
::al_get_keyboard_state(&keyState);
Expand All @@ -83,12 +83,12 @@ std::set<InputAction> Input::getGameActions()
return ongoingActions;
}

std::pair<int, int> Input::getMousePosition() const
std::pair<int, int> StandardInput::getMousePosition() const
{
return {mouseX_, mouseY_};
}

InputAction Input::getCommonAction(const ALLEGRO_EVENT& event)
InputAction StandardInput::getCommonAction(const ALLEGRO_EVENT& event)
{
if (userWantToExit(event))
return InputAction::QUIT;
Expand All @@ -102,73 +102,73 @@ InputAction Input::getCommonAction(const ALLEGRO_EVENT& event)
return InputAction::EMPTY;
}

bool Input::itemPicked(const ALLEGRO_EVENT& event)
bool StandardInput::itemPicked(const ALLEGRO_EVENT& event)
{
return keyEnterUsed(event) || keySpaceUsed(event) || mouseClickUsed(event);
}

bool Input::fired(const ALLEGRO_KEYBOARD_STATE& keyState)
bool StandardInput::fired(const ALLEGRO_KEYBOARD_STATE& keyState)
{
return ::al_key_down(&keyState, ALLEGRO_KEY_SPACE) ||
::al_key_down(&keyState, ALLEGRO_KEY_ENTER);
}

bool Input::userWantToExit(const ALLEGRO_EVENT& event)
bool StandardInput::userWantToExit(const ALLEGRO_EVENT& event)
{
return event.type == ALLEGRO_EVENT_DISPLAY_CLOSE;
}

bool Input::keyEscapeUsed(const ALLEGRO_EVENT& event)
bool StandardInput::keyEscapeUsed(const ALLEGRO_EVENT& event)
{
return (event.type == ALLEGRO_EVENT_KEY_CHAR) &&
(event.keyboard.keycode == ALLEGRO_KEY_ESCAPE);
}

bool Input::keyUpUsed(const ALLEGRO_EVENT& event)
bool StandardInput::keyUpUsed(const ALLEGRO_EVENT& event)
{
return (event.type == ALLEGRO_EVENT_KEY_CHAR) &&
(event.keyboard.keycode == ALLEGRO_KEY_UP);
}

bool Input::keyDownUsed(const ALLEGRO_EVENT& event)
bool StandardInput::keyDownUsed(const ALLEGRO_EVENT& event)
{
return (event.type == ALLEGRO_EVENT_KEY_CHAR) &&
(event.keyboard.keycode == ALLEGRO_KEY_DOWN);
}

bool Input::keyUpPressed(const ALLEGRO_KEYBOARD_STATE& keyState)
bool StandardInput::keyUpPressed(const ALLEGRO_KEYBOARD_STATE& keyState)
{
return ::al_key_down(&keyState, ALLEGRO_KEY_UP);
}

bool Input::keyDownPressed(const ALLEGRO_KEYBOARD_STATE& keyState)
bool StandardInput::keyDownPressed(const ALLEGRO_KEYBOARD_STATE& keyState)
{
return ::al_key_down(&keyState, ALLEGRO_KEY_DOWN);
}

bool Input::keyLeftPressed(const ALLEGRO_KEYBOARD_STATE& keyState)
bool StandardInput::keyLeftPressed(const ALLEGRO_KEYBOARD_STATE& keyState)
{
return ::al_key_down(&keyState, ALLEGRO_KEY_LEFT);
}

bool Input::keyRightPressed(const ALLEGRO_KEYBOARD_STATE& keyState)
bool StandardInput::keyRightPressed(const ALLEGRO_KEYBOARD_STATE& keyState)
{
return ::al_key_down(&keyState, ALLEGRO_KEY_RIGHT);
}

bool Input::keyEnterUsed(const ALLEGRO_EVENT& event)
bool StandardInput::keyEnterUsed(const ALLEGRO_EVENT& event)
{
return (event.type == ALLEGRO_EVENT_KEY_UP) &&
(event.keyboard.keycode == ALLEGRO_KEY_ENTER);
}

bool Input::keySpaceUsed(const ALLEGRO_EVENT& event)
bool StandardInput::keySpaceUsed(const ALLEGRO_EVENT& event)
{
return (event.type == ALLEGRO_EVENT_KEY_UP) &&
(event.keyboard.keycode == ALLEGRO_KEY_SPACE);
}

bool Input::mouseClickUsed(const ALLEGRO_EVENT& event)
bool StandardInput::mouseClickUsed(const ALLEGRO_EVENT& event)
{
return (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) &&
(event.mouse.button == 1);
Expand Down
57 changes: 57 additions & 0 deletions src/StandardInput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

#include "Input.h"

struct ALLEGRO_EVENT_QUEUE;
union ALLEGRO_EVENT;
struct ALLEGRO_KEYBOARD_STATE;
enum class InputAction : char;

class StandardInput : public Input
{
public:
StandardInput();

static void init();

InputAction getMenuAction() override;

std::set<InputAction> getGameActions() override;

std::pair<int, int> getMousePosition() const override;

private:
static InputAction getCommonAction(const ALLEGRO_EVENT& event);

static bool itemPicked(const ALLEGRO_EVENT& event);

static bool fired(const ALLEGRO_KEYBOARD_STATE& keyState);

static bool userWantToExit(const ALLEGRO_EVENT& event);

static bool keyEscapeUsed(const ALLEGRO_EVENT& event);

static bool keyUpUsed(const ALLEGRO_EVENT& event);

static bool keyDownUsed(const ALLEGRO_EVENT& event);

static bool keyUpPressed(const ALLEGRO_KEYBOARD_STATE& keyState);

static bool keyDownPressed(const ALLEGRO_KEYBOARD_STATE& keyState);

static bool keyLeftPressed(const ALLEGRO_KEYBOARD_STATE& keyState);

static bool keyRightPressed(const ALLEGRO_KEYBOARD_STATE& keyState);

static bool keyEnterUsed(const ALLEGRO_EVENT& event);

static bool keySpaceUsed(const ALLEGRO_EVENT& event);

static bool mouseClickUsed(const ALLEGRO_EVENT& event);

ALLEGRO_EVENT_QUEUE* events_;

int mouseX_{0};

int mouseY_{0};
};
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include <cstdlib>

#include "Game.h"
#include "Input.h"
#include "Menu.h"
#include "Screen.h"
#include "StandardInput.h"
#include "UserChoice.h"

namespace
{
UserChoice getUserChoice(Menu& menu)
{
UserChoice choice{UserChoice::MAIN_MENU};
Input input;
StandardInput input;
while ((choice != UserChoice::EXIT) && (!menu.isLevelPicked(choice)))
{
menu.refresh(choice);
Expand All @@ -27,7 +27,7 @@ int main()
if (!screen.init())
return EXIT_FAILURE;

Input::init();
StandardInput::init();

Menu menu(screen);

Expand Down

0 comments on commit 275f6af

Please sign in to comment.