Add function for rendering map onto main window
This commit is contained in:
parent
a3ab2e32da
commit
cf0fa0f7dd
67
game.cpp
67
game.cpp
|
@ -8,17 +8,23 @@ Game::Game()
|
|||
// Generate level
|
||||
level = std::make_unique<Level>();
|
||||
|
||||
sf::Window window(sf::VideoMode(640, 480), "SFML-Test Application", sf::Style::Default);
|
||||
window.setActive();
|
||||
main_window.create(sf::VideoMode(window_side, window_side), "SFML-Test Application", sf::Style::Default);
|
||||
main_window.setActive();
|
||||
}
|
||||
|
||||
int Game::run()
|
||||
{
|
||||
clock = std::make_unique<sf::Clock>();
|
||||
|
||||
// To where player moved each step of the game loop
|
||||
Direction direction;
|
||||
|
||||
// On the game loop
|
||||
while (main_window.isOpen())
|
||||
{
|
||||
// By default player doesn't move anywhere
|
||||
direction = Direction::None;
|
||||
|
||||
sf::Event event;
|
||||
while (main_window.pollEvent(event))
|
||||
{
|
||||
|
@ -29,14 +35,21 @@ int Game::run()
|
|||
if (event.type == sf::Event::KeyPressed)
|
||||
{
|
||||
// Move
|
||||
onMoving(event.key.code);
|
||||
direction = onMoving(event.key.code);
|
||||
}
|
||||
}
|
||||
|
||||
// Draw level
|
||||
renderMap(direction);
|
||||
|
||||
main_window.display();
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
|
||||
Direction Game::getDirection(sf::Keyboard::Key &key) const
|
||||
{
|
||||
switch (key)
|
||||
|
@ -66,16 +79,13 @@ Direction Game::getDirection(sf::Keyboard::Key &key) const
|
|||
}
|
||||
}
|
||||
|
||||
void Game::onMoving(sf::Keyboard::Key &key)
|
||||
Direction Game::onMoving(sf::Keyboard::Key &key)
|
||||
{
|
||||
// Determine where to move
|
||||
const Direction direction = getDirection(key);
|
||||
|
||||
if (direction == Direction::None)
|
||||
return;
|
||||
|
||||
//////////////////////////
|
||||
|
||||
if (direction != Direction::None)
|
||||
{
|
||||
// Save the initial coordinates
|
||||
coordinate initial_x, initial_y;
|
||||
hero->position(initial_x, initial_y);
|
||||
|
@ -109,4 +119,43 @@ void Game::onMoving(sf::Keyboard::Key &key)
|
|||
hero->refillCharges(1);
|
||||
level->removeCharge(attempt_x, attempt_y);
|
||||
}
|
||||
}
|
||||
|
||||
return direction;
|
||||
}
|
||||
|
||||
void Game::renderMap(const Direction direction)
|
||||
{
|
||||
if (direction == Direction::None)
|
||||
return; // Player didn't move this step of loop, so map couldn't change
|
||||
|
||||
const Map &map = level->mapArray();
|
||||
|
||||
coordinate painter_x = 0, painter_y = 0;
|
||||
|
||||
for (const Row &row : map)
|
||||
{
|
||||
for (const CellType &cell : row)
|
||||
{
|
||||
switch (cell)
|
||||
{ // TO DO!!
|
||||
case CellType::Ground:
|
||||
; break;
|
||||
case CellType::Charge:
|
||||
; break;
|
||||
case CellType::Bridge:
|
||||
; break;
|
||||
case CellType::Water:
|
||||
default:
|
||||
;
|
||||
}
|
||||
|
||||
// Move painter to next cell of row
|
||||
painter_x += cell_length;
|
||||
}
|
||||
|
||||
// Move painter to next row of the map
|
||||
painter_x = 0;
|
||||
painter_y += cell_length;
|
||||
}
|
||||
}
|
||||
|
|
11
game.h
11
game.h
|
@ -3,12 +3,16 @@
|
|||
|
||||
#include <memory>
|
||||
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include <SFML/System/Time.hpp>
|
||||
#include <SFML/Window.hpp>
|
||||
|
||||
#include "hero.h"
|
||||
#include "level.h"
|
||||
|
||||
constexpr int cell_length = 20;
|
||||
constexpr int window_side = cell_length * side;
|
||||
|
||||
/// The main class where all the process happens
|
||||
class Game
|
||||
{
|
||||
|
@ -19,13 +23,16 @@ private:
|
|||
|
||||
// SFML entities
|
||||
std::unique_ptr<sf::Clock> clock;
|
||||
sf::Window main_window;
|
||||
sf::RenderWindow main_window;
|
||||
|
||||
/// Convert pressed key into a game direction
|
||||
Direction getDirection(sf::Keyboard::Key &key) const;
|
||||
|
||||
/// Move player by pressed key
|
||||
void onMoving(sf::Keyboard::Key &key);
|
||||
Direction onMoving(sf::Keyboard::Key &key);
|
||||
|
||||
/// Render game state
|
||||
void renderMap(const Direction direction);
|
||||
|
||||
public:
|
||||
explicit Game();
|
||||
|
|
Loading…
Reference in New Issue