Skip to content

Commit

Permalink
Add -Wall -pedantic -Wextra compiler flags and remove all warnings po…
Browse files Browse the repository at this point in the history
…ssible
  • Loading branch information
hckr committed Aug 29, 2017
1 parent e6bb8bc commit 5d991be
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 2.8)

project(space-logic-adventure)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra")

FILE(GLOB SOURCES "*.cpp")
add_executable(${PROJECT_NAME} ${SOURCES})

Expand Down
4 changes: 4 additions & 0 deletions endscreen.cpp
Expand Up @@ -14,8 +14,12 @@ void EndScreen::processEvent(const sf::Event &event) {
case sf::Keyboard::Space:
eventReceiver({Event::SHOW_CLEAN_MENU});
break;
default:
break;
}
break;
default:
break;
}
}

Expand Down
78 changes: 74 additions & 4 deletions level.cpp
Expand Up @@ -43,6 +43,8 @@ void Level::processEvent(const sf::Event &event) {
case sf::Keyboard::Space:
changeGameState(COUNTING);
break;
default:
break;
}
break;
case PLAYING:
Expand All @@ -63,10 +65,16 @@ void Level::processEvent(const sf::Event &event) {
case sf::Keyboard::S:
movePlayer(BACK);
break;
default:
break;
}
break;
default:
break;
}
break;
default:
break;
}
}

Expand Down Expand Up @@ -110,6 +118,8 @@ bool Level::movePlayer(PlayerMove move) {
}
newPos.x = hero.getPos().x - 1;
break;
default:
break;
}
break;
case BACK:
Expand Down Expand Up @@ -138,6 +148,8 @@ bool Level::movePlayer(PlayerMove move) {
}
newPos.x = hero.getPos().x + 1;
break;
default:
break;
}
break;
case ROTATE_CLOCKWISE:
Expand Down Expand Up @@ -171,6 +183,9 @@ void Level::update() {

switch (gameState) {

case SHOWING_INFO:
break;

case COUNTING:
if (countingClock.getElapsedTime().asSeconds() >= countingLength) {
changeGameState(PLAYING);
Expand Down Expand Up @@ -208,6 +223,7 @@ void Level::update() {
case AFTER_LOST:
eventReceiver({ Event::GAME_OVER });
break;

}


Expand All @@ -229,21 +245,28 @@ void Level::loadMapFromFile(std::string fileName) {
void Level::closeMapBorders() {
for (auto& [coords, field] : map) {
auto& [row, column] = coords;

switch (field.tileAppearance) {
case FIELD_VERTICAL:

case FIELD_VERTICAL:
if (map.count(std::make_pair(row - 1, column)) == 0) {
field.tileAppearance = FIELD_VERTICAL_OPENED_BOTTOM;
} else if (map.count(std::make_pair(row + 1, column)) == 0) {
field.tileAppearance = FIELD_VERTICAL_OPENED_TOP;
}
break;

case FIELD_HORIZONTAL:
if (map.count(std::make_pair(row, column - 1)) == 0) {
field.tileAppearance = FIELD_HORIZONTAL_OPENED_RIGHT;
} else if (map.count(std::make_pair(row, column + 1)) == 0) {
field.tileAppearance = FIELD_HORIZONTAL_OPENED_LEFT;
}
break;

default:
break;

}
}
}
Expand Down Expand Up @@ -348,38 +371,49 @@ void Level::setFieldFunction(int row, int column, FieldFunction function) {
}
}

sf::Vector2f cartesianToIsometric(sf::Vector2f cartesian, Level::TileAppearance tileAppearance) {
sf::Vector2f cartesianToIsometric(sf::Vector2i cartesian, Level::TileAppearance tileAppearance) {
sf::Vector2f mod(0, 0);

switch(tileAppearance) { // TODO this should probably be outside of the class

case Level::FIELD_DOWN_RIGHT_TURN:
mod = {0, 2};
break;

case Level::FIELD_UP_RIGHT_TURN:
mod = {4, 0};
break;

case Level::FIELD_HORIZONTAL_OPENED_RIGHT:
mod = {4, 2};
break;

case Level::FIELD_VERTICAL_OPENED_TOP:
mod = {4, 0};
break;

case Level::FIELD_VERTICAL_OPENED_BOTTOM:
mod = {0, 2};
break;

case Level::PLAYER_FACED_TOP:
case Level::PLAYER_FACED_BOTTOM:
case Level::PLAYER_FACED_LEFT:
case Level::PLAYER_FACED_RIGHT:
mod = {20, -8};
break;

default:
break;

}

sf::Vector2f iso((cartesian.x - cartesian.y) * 38, (cartesian.x + cartesian.y) / 1.43f * 38);

return iso + mod;
}

void Level::addFieldToVertexArray(Field &field, sf::Vector2f pos) {
void Level::addFieldToVertexArray(Field &field, sf::Vector2i pos) {
pos.x += 8; // TODO normalize coordinate system
pos.y -= 2;
auto leftTopPos = cartesianToIsometric(pos, field.tileAppearance);
Expand Down Expand Up @@ -429,20 +463,29 @@ void Level::draw(sf::RenderTarget &target, sf::RenderStates states) const
sf::VertexArray playerVertices;
playerVertices.setPrimitiveType(sf::Quads);

TileAppearance playerTileAppearance;
TileAppearance playerTileAppearance = PLAYER_FACED_TOP;

switch(hero.face) {

case Hero::TOP:
playerTileAppearance = PLAYER_FACED_TOP;
break;

case Hero::BOTTOM:
playerTileAppearance = PLAYER_FACED_BOTTOM;
break;

case Hero::LEFT:
playerTileAppearance = PLAYER_FACED_LEFT;
break;

case Hero::RIGHT:
playerTileAppearance = PLAYER_FACED_RIGHT;
break;

default:
break;

}

const SpriteInfo &playerSpriteInfo = tilesSpriteInfo.at(playerTileAppearance);
Expand All @@ -459,41 +502,68 @@ void Level::draw(sf::RenderTarget &target, sf::RenderStates states) const
target.draw(playerVertices, states);

switch (gameState) {

case SHOWING_INFO:
drawCenteredText(target, states, "LEVEL CODE: " + code, 40, 1, 10);
drawCenteredText(target, states, "PRESS ENTER/SPACEBAR TO START", 50, 1, target.getSize().y - 150);
drawCenteredText(target, states, message, 40, 1, target.getSize().y - 100);
break;

case COUNTING:
{
int secondsLeft = std::ceil(countingLength - countingClock.getElapsedTime().asSeconds());
drawCenteredText(target, states, std::to_string(secondsLeft), 80, 3, target.getSize().y / 2 - 80);
break;
}

case PLAYING:
break;

case WON:
drawCenteredText(target, states, "LEVEL FINISHED", 80, 3, target.getSize().y / 2 - 80);
break;

case LOST:
drawCenteredText(target, states, "YOU LOST", 80, 3, target.getSize().y / 2 - 80);
break;

case AFTER_WON:
break;

case AFTER_LOST:
break;
}
}

void Level::changeGameState(GameState newState) {
gameState = newState;

switch (newState) {

case SHOWING_INFO:
break;

case COUNTING:
countingClock.restart();
break;

case PLAYING:
stepOnField(hero.getPos().y, hero.getPos().x);
break;

case WON:
wonClock.restart();
break;

case LOST:
lostClock.restart();
break;

case AFTER_WON:
break;

case AFTER_LOST:
break;

}
}
11 changes: 5 additions & 6 deletions level.hpp
Expand Up @@ -57,11 +57,11 @@ class Level : public Screen {
struct Field {
TileAppearance tileAppearance;
FieldFunction fieldFunction = NORMAL;
size_t firstVertexIndex; // should be 4 of them
size_t firstVertexIndex = 0; // should be 4 of them

bool stepped = false;
bool active = true;
sf::Clock sinceStepped;
sf::Clock sinceStepped = {};
};

enum PlayerMove {
Expand Down Expand Up @@ -90,7 +90,7 @@ class Level : public Screen {
void addNewField(int row, int column, Field field);
Field& getField(int row, int column);
void setFieldFunction(int row, int column, FieldFunction function);
void addFieldToVertexArray(Field &field, sf::Vector2f pos);
void addFieldToVertexArray(Field &field, sf::Vector2i pos);
void modifyFieldVertices(Field &field, std::function<void (sf::Vertex &)> modifyVertex);
void stepOnField(int row, int column);
void changeGameState(GameState newState);
Expand All @@ -99,8 +99,6 @@ class Level : public Screen {

private:

Hero hero;

enum Direction { // TODO duplicate with Player::Face?
TOP = 1,
RIGHT = 2,
Expand Down Expand Up @@ -133,9 +131,10 @@ class Level : public Screen {
float fieldLifetimeSeconds;
std::string code;
std::string message;
LevelMap_t map;
sf::Texture &tileset;
TileAppearanceToSpriteInfoMap_t tilesSpriteInfo;
Hero hero;
sf::VertexArray vertices;
sf::Sprite &background_sp;
LevelMap_t map;
};
4 changes: 2 additions & 2 deletions main.cpp
Expand Up @@ -100,10 +100,10 @@ int main() {
switch (event.type) {
case Event::MENU_START_NEW_GAME:
currentLevelIndex = -1;
// no break here
// fallthrough
case Event::LEVEL_FINISHED:
currentLevelIndex += 1;
// no break here
// fallthrough
case Event::MENU_TRY_AGAIN:
if (currentLevelIndex < levelsData.size()) {
const LevelData &levelData = levelsData[currentLevelIndex];
Expand Down
4 changes: 4 additions & 0 deletions menuscreen.cpp
Expand Up @@ -71,6 +71,8 @@ void MenuScreen::processEvent(const sf::Event &event) {
eventReceiver({Event::MENU_QUIT});
break;
}
default:
break;
}
break;
case sf::Event::TextEntered:
Expand All @@ -81,6 +83,8 @@ void MenuScreen::processEvent(const sf::Event &event) {
levelCodeInput += static_cast<char>(event.text.unicode - ('a' - 'A'));
}
break;
default:
break;
}
}

Expand Down
6 changes: 3 additions & 3 deletions screen.hpp
Expand Up @@ -24,10 +24,10 @@ class Screen : public sf::Drawable {

public:
Screen(const sf::Color &fillColor, const sf::Color &outlineColor, const sf::Font &font)
: fillColor(fillColor),
: eventReceiver([](Event){}),
fillColor(fillColor),
outlineColor(outlineColor),
font(font),
eventReceiver([](Event){})
font(font)
{ }

void setEventReceiver(std::function<void (Event)> receiver) {
Expand Down

0 comments on commit 5d991be

Please sign in to comment.