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
|
// Generate level
|
||||||
level = std::make_unique<Level>();
|
level = std::make_unique<Level>();
|
||||||
|
|
||||||
sf::Window window(sf::VideoMode(640, 480), "SFML-Test Application", sf::Style::Default);
|
main_window.create(sf::VideoMode(window_side, window_side), "SFML-Test Application", sf::Style::Default);
|
||||||
window.setActive();
|
main_window.setActive();
|
||||||
}
|
}
|
||||||
|
|
||||||
int Game::run()
|
int Game::run()
|
||||||
{
|
{
|
||||||
clock = std::make_unique<sf::Clock>();
|
clock = std::make_unique<sf::Clock>();
|
||||||
|
|
||||||
|
// To where player moved each step of the game loop
|
||||||
|
Direction direction;
|
||||||
|
|
||||||
// On the game loop
|
// On the game loop
|
||||||
while (main_window.isOpen())
|
while (main_window.isOpen())
|
||||||
{
|
{
|
||||||
|
// By default player doesn't move anywhere
|
||||||
|
direction = Direction::None;
|
||||||
|
|
||||||
sf::Event event;
|
sf::Event event;
|
||||||
while (main_window.pollEvent(event))
|
while (main_window.pollEvent(event))
|
||||||
{
|
{
|
||||||
|
@ -29,14 +35,21 @@ int Game::run()
|
||||||
if (event.type == sf::Event::KeyPressed)
|
if (event.type == sf::Event::KeyPressed)
|
||||||
{
|
{
|
||||||
// Move
|
// Move
|
||||||
onMoving(event.key.code);
|
direction = onMoving(event.key.code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Draw level
|
||||||
|
renderMap(direction);
|
||||||
|
|
||||||
|
main_window.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////
|
||||||
|
|
||||||
Direction Game::getDirection(sf::Keyboard::Key &key) const
|
Direction Game::getDirection(sf::Keyboard::Key &key) const
|
||||||
{
|
{
|
||||||
switch (key)
|
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
|
// Determine where to move
|
||||||
const Direction direction = getDirection(key);
|
const Direction direction = getDirection(key);
|
||||||
|
|
||||||
if (direction == Direction::None)
|
if (direction != Direction::None)
|
||||||
return;
|
{
|
||||||
|
|
||||||
//////////////////////////
|
|
||||||
|
|
||||||
// Save the initial coordinates
|
// Save the initial coordinates
|
||||||
coordinate initial_x, initial_y;
|
coordinate initial_x, initial_y;
|
||||||
hero->position(initial_x, initial_y);
|
hero->position(initial_x, initial_y);
|
||||||
|
@ -109,4 +119,43 @@ void Game::onMoving(sf::Keyboard::Key &key)
|
||||||
hero->refillCharges(1);
|
hero->refillCharges(1);
|
||||||
level->removeCharge(attempt_x, attempt_y);
|
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 <memory>
|
||||||
|
|
||||||
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
#include <SFML/System/Time.hpp>
|
#include <SFML/System/Time.hpp>
|
||||||
#include <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
|
|
||||||
#include "hero.h"
|
#include "hero.h"
|
||||||
#include "level.h"
|
#include "level.h"
|
||||||
|
|
||||||
|
constexpr int cell_length = 20;
|
||||||
|
constexpr int window_side = cell_length * side;
|
||||||
|
|
||||||
/// The main class where all the process happens
|
/// The main class where all the process happens
|
||||||
class Game
|
class Game
|
||||||
{
|
{
|
||||||
|
@ -19,13 +23,16 @@ private:
|
||||||
|
|
||||||
// SFML entities
|
// SFML entities
|
||||||
std::unique_ptr<sf::Clock> clock;
|
std::unique_ptr<sf::Clock> clock;
|
||||||
sf::Window main_window;
|
sf::RenderWindow main_window;
|
||||||
|
|
||||||
/// Convert pressed key into a game direction
|
/// Convert pressed key into a game direction
|
||||||
Direction getDirection(sf::Keyboard::Key &key) const;
|
Direction getDirection(sf::Keyboard::Key &key) const;
|
||||||
|
|
||||||
/// Move player by pressed key
|
/// 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:
|
public:
|
||||||
explicit Game();
|
explicit Game();
|
||||||
|
|
Loading…
Reference in New Issue