Skip to content

Commit

Permalink
add basic view support (WIP)
Browse files Browse the repository at this point in the history
refactor code
  • Loading branch information
David Arutiunian committed Dec 20, 2017
1 parent 0642515 commit 264c0e1
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 41 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ if (NOT CMAKE_BUILD_TYPE)
endif ()

if (WIN32 AND CMAKE_BUILD_TYPE MATCHES Release)
add_executable(doodle_jump WIN32 main.cpp resources consts.h EventLoop.cpp EventLoop.h Doodler.cpp Doodler.h IEntity.h Platform.cpp Platform.h IPhysicsObject.h Engine.cpp Engine.h KeyboardState.cpp KeyboardState.h)
add_executable(doodle_jump WIN32 main.cpp resources consts.h EventLoop.cpp EventLoop.h Doodler.cpp Doodler.h IEntity.h Platform.cpp Platform.h IPhysicsObject.h Engine.cpp Engine.h KeyboardState.cpp KeyboardState.h View.cpp View.h)
else ()
add_executable(doodle_jump main.cpp resources consts.h EventLoop.cpp EventLoop.h Doodler.cpp Doodler.h IEntity.h Platform.cpp Platform.h IPhysicsObject.h Engine.cpp Engine.h KeyboardState.cpp KeyboardState.h)
add_executable(doodle_jump main.cpp resources consts.h EventLoop.cpp EventLoop.h Doodler.cpp Doodler.h IEntity.h Platform.cpp Platform.h IPhysicsObject.h Engine.cpp Engine.h KeyboardState.cpp KeyboardState.h View.cpp View.h)
endif ()

target_compile_features(doodle_jump PUBLIC cxx_std_17)
Expand Down
9 changes: 7 additions & 2 deletions Doodler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ void Doodler::draw(sf::RenderTarget &target, sf::RenderStates states) const
target.draw(m_shape, states);
}

Doodler::Doodler(KeyboardState &m_keyboardState) : m_keyboardState(m_keyboardState)
Doodler::Doodler()
{
m_shape.setSize(m_size);
m_shape.setOrigin(m_size.x / 2, m_size.x / 2);
Expand Down Expand Up @@ -44,7 +44,7 @@ void Doodler::checkCollision()
// TODO: fix side collision check
void Doodler::setVerticalPosition(const float nextX, const float deltaTime)
{
const KeysMap &keysMap = m_keyboardState.getKeysMap();
const KeysMap &keysMap = p_m_keyboardState->getKeysMap();
const bool isMaxRightPosition = m_position.x + m_size.x / 2 + m_outlineThickness < WINDOW_WIDTH;
const bool isMaxLeftPosition = m_position.x - m_size.x / 2 - m_outlineThickness > 0;
if (keysMap.at(sf::Keyboard::Right) && isMaxRightPosition)
Expand Down Expand Up @@ -80,3 +80,8 @@ void Doodler::setFalling(const float nextY)
{
m_isFalling = getPosition().y - nextY <= 0;
}

void Doodler::addKeyboardState(const std::shared_ptr<KeyboardState> p_keyboardState)
{
p_m_keyboardState = p_keyboardState;
}
6 changes: 4 additions & 2 deletions Doodler.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class Doodler : public IEntity
{
public:
explicit Doodler(KeyboardState &m_keyboardState);
Doodler();

~Doodler() override = default;

Expand All @@ -21,8 +21,10 @@ class Doodler : public IEntity

const sf::Vector2f &getBounds() const override;

void addKeyboardState(std::shared_ptr<KeyboardState> p_keyboardState);

private:
KeyboardState &m_keyboardState;
std::shared_ptr<KeyboardState> p_m_keyboardState;

sf::Vector2f m_size = sf::Vector2f(35.f, 50.f);
const float m_outlineThickness = 2;
Expand Down
39 changes: 25 additions & 14 deletions EventLoop.cpp
Original file line number Diff line number Diff line change
@@ -1,56 +1,51 @@
#include "Doodler.h"
#include "EventLoop.h"

EventLoop::EventLoop(sf::RenderWindow &window, sf::Clock &clock, KeyboardState &keyboardState)
: m_window(window), m_clock(clock), m_keyboardState(keyboardState) {}

void EventLoop::createWindow() const
void EventLoop::createWindow()
{
sf::ContextSettings settings;
settings.antialiasingLevel = ANTIALIASING_LEVEL;
m_window.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), WINDOW_TITLE, sf::Style::Close, settings);
m_window.setFramerateLimit(MAX_FPS);

sf::Image icon;
if (icon.loadFromFile(ICON_PATH)) {
if (icon.loadFromFile(ICON_PATH))
{
m_window.setIcon(ICON_SIZE.x, ICON_SIZE.y, icon.getPixelsPtr());
}
}

EventLoop &EventLoop::pollEvents()
void EventLoop::pollEvents()
{
m_deltaTime = m_clock.restart().asSeconds();
sf::Event event{};
while (m_window.pollEvent(event))
{
m_keyboardState.onKeyEventHandler(event);
p_m_keyboardState->onKeyEventHandler(event);
onWindowEventHandler(event);
}
return *this;
}

EventLoop &EventLoop::redrawFrame(const Entities &entities)
void EventLoop::redrawFrame(const Entities &entities)
{
m_window.clear(sf::Color::White);
m_window.setView(p_m_view->getView());
std::for_each(entities.begin(), entities.end(), [&](const std::shared_ptr<IEntity> &p_item) -> void {
m_window.draw(*p_item);
});
m_window.display();
return *this;
}

EventLoop &EventLoop::init()
void EventLoop::init()
{
createWindow();
return *this;
}

EventLoop &EventLoop::update(const Entities &entities)
void EventLoop::update(const Entities &entities)
{
std::for_each(entities.begin(), entities.end(), [&](const std::shared_ptr<IEntity> &p_item) -> void {
p_item->updatePosition(m_deltaTime);
});
return *this;
}

// TODO: create GameState class and dispatch this stuff there
Expand All @@ -62,3 +57,19 @@ void EventLoop::onWindowEventHandler(const sf::Event &event)
}
m_window.close();
}

const sf::RenderWindow &EventLoop::getWindow() const
{
return m_window;
}

void EventLoop::addKeyboardState(const std::shared_ptr<KeyboardState> p_keyboardState)
{
p_m_keyboardState = p_keyboardState;
}

void EventLoop::addView(const std::shared_ptr<View> p_view)
{
p_m_view = p_view;
}

27 changes: 17 additions & 10 deletions EventLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,37 @@
#include "consts.h"
#include "IEntity.h"
#include "KeyboardState.h"
#include "View.h"

class EventLoop
{
public:
EventLoop(sf::RenderWindow &window, sf::Clock &clock, KeyboardState &keyboardState);

~EventLoop() = default;

EventLoop &init();
void init();

void pollEvents();

void update(const Entities &entities);

EventLoop &pollEvents();
void redrawFrame(const Entities &entities);

EventLoop &update(const Entities &entities);
void addKeyboardState(std::shared_ptr<KeyboardState> p_keyboardState);

EventLoop &redrawFrame(const Entities &entities);
void addView(std::shared_ptr<View> p_view);

const sf::RenderWindow &getWindow() const;

private:
sf::RenderWindow &m_window;
sf::Clock &m_clock;
KeyboardState &m_keyboardState;
sf::RenderWindow m_window;
sf::Clock m_clock;

std::shared_ptr<KeyboardState> p_m_keyboardState;
std::shared_ptr<View> p_m_view;

float m_deltaTime = 0.f;

void createWindow() const;
void createWindow();

void onWindowEventHandler(const sf::Event &event);

Expand Down
17 changes: 17 additions & 0 deletions View.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "SFML/Graphics.hpp"
#include "View.h"

View::View()
{
m_view.setViewport(sf::FloatRect(0, 0, 1, 1));
}

void View::followTo(const std::shared_ptr<Doodler> p_doodler)
{
m_view.setCenter(p_doodler->getPosition());
}

const sf::View &View::getView() const
{
return m_view;
}
20 changes: 20 additions & 0 deletions View.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef DOODLE_JUMP_VIEW_H
#define DOODLE_JUMP_VIEW_H

#include <SFML/Graphics.hpp>
#include "Doodler.h"

class View
{
public:
View();

const sf::View &getView() const;

void followTo(std::shared_ptr<Doodler> p_doodler);

private:
sf::View m_view;
};

#endif //DOODLE_JUMP_VIEW_H
2 changes: 1 addition & 1 deletion consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static const sf::Vector2u ICON_SIZE = {32, 32};
static const float G = 9.8f;
static const float TIME_ACCELERATOR = 15.f;
static const float MOVE_SPEED = 500.f;
static const size_t PLATFORM_COUNT = 10;
static const size_t PLATFORM_COUNT = 30;

enum class Types : size_t
{
Expand Down
22 changes: 12 additions & 10 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <SFML/Graphics.hpp>
#include <memory>
#include "View.h"
#include "Engine.h"
#include "EventLoop.h"
#include "Platform.h"
Expand All @@ -8,29 +9,30 @@ int main()
{
srand(static_cast<unsigned>(time(nullptr)));

sf::RenderWindow window;
sf::Clock clock;

Entities entities;
Engine engine;
EventLoop eventLoop;

KeyboardState keyboardState;
std::shared_ptr<KeyboardState> p_keyboardState = std::make_shared<KeyboardState>(KeyboardState());
std::shared_ptr<View> p_view = std::make_shared<View>(View());

EventLoop eventLoop(window, clock, keyboardState);
eventLoop.init();

Engine engine;
eventLoop.addView(p_view);
eventLoop.addKeyboardState(p_keyboardState);

for (size_t i = 0; i < PLATFORM_COUNT; ++i)
{
std::shared_ptr<Platform> p_platform = std::make_shared<Platform>(Platform());
entities.push_back(p_platform);
}

std::shared_ptr<Doodler> p_doodler = std::make_shared<Doodler>(Doodler(keyboardState));

std::shared_ptr<Doodler> p_doodler = std::make_shared<Doodler>(Doodler());
p_doodler->addKeyboardState(p_keyboardState);
entities.push_back(p_doodler);

while (window.isOpen())
p_view->followTo(p_doodler);

while (eventLoop.getWindow().isOpen())
{
eventLoop.pollEvents();
eventLoop.update(entities);
Expand Down

0 comments on commit 264c0e1

Please sign in to comment.