Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Commit

Permalink
Eighth tutorial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilyMansfield committed Aug 5, 2014
1 parent 36af56d commit d130078
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 7 deletions.
25 changes: 24 additions & 1 deletion game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ void Game::loadTextures()
texmgr.loadTexture("background", "media/background.png");
}

void Game::loadFonts()
{
sf::Font font;
font.loadFromFile("media/font.ttf");
this->fonts["main_font"] = font;

return;
}

void Game::loadStylesheets()
{
this->stylesheets["button"] = GuiStyle(&this->fonts.at("main_font"), 1,
sf::Color(0xc6,0xc6,0xc6), sf::Color(0x94,0x94,0x94), sf::Color(0x00,0x00,0x00),
sf::Color(0x61,0x61,0x61), sf::Color(0x94,0x94,0x94), sf::Color(0x00,0x00,0x00));
this->stylesheets["text"] = GuiStyle(&this->fonts.at("main_font"), 0,
sf::Color(0x00,0x00,0x00,0x00), sf::Color(0x00,0x00,0x00), sf::Color(0xff,0xff,0xff),
sf::Color(0x00,0x00,0x00,0x00), sf::Color(0x00,0x00,0x00), sf::Color(0xff,0x00,0x00));

return;
}

void Game::pushState(GameState* state)
{
this->states.push(state);
Expand Down Expand Up @@ -116,7 +137,9 @@ Game::Game()
{
this->loadTextures();
this->loadTiles();

this->loadFonts();
this->loadStylesheets();

this->window.create(sf::VideoMode(800, 600), "City Builder");
this->window.setFramerateLimit(60);

Expand Down
5 changes: 5 additions & 0 deletions game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "texture_manager.hpp"
#include "tile.hpp"
#include "gui.hpp"

class GameState;

Expand All @@ -17,6 +18,8 @@ class Game

void loadTextures();
void loadTiles();
void loadStylesheets();
void loadFonts();

public:

Expand All @@ -29,6 +32,8 @@ class Game
sf::Sprite background;

std::map<std::string, Tile> tileAtlas;
std::map<std::string, GuiStyle> stylesheets;
std::map<std::string, sf::Font> fonts;

void pushState(GameState* state);
void popState();
Expand Down
42 changes: 38 additions & 4 deletions game_state_start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ void GameStateStart::draw(const float dt)
this->game->window.setView(this->view);

this->game->window.clear(sf::Color::Black);
this->game->window.draw(this->game->background);
this->game->window.draw(this->game->background);

return;
for(auto gui : this->guiSystem) this->game->window.draw(gui.second);

return;
}

void GameStateStart::update(const float dt)
Expand All @@ -22,6 +24,8 @@ void GameStateStart::handleInput()
{
sf::Event event;

sf::Vector2f mousePos = this->game->window.mapPixelToCoords(sf::Mouse::getPosition(this->game->window), this->view);

while(this->game->window.pollEvent(event))
{
switch(event.type)
Expand All @@ -36,16 +40,39 @@ void GameStateStart::handleInput()
case sf::Event::Resized:
{
this->view.setSize(event.size.width, event.size.height);
this->game->background.setPosition(this->game->window.mapPixelToCoords(sf::Vector2i(0, 0)));
this->game->background.setPosition(this->game->window.mapPixelToCoords(sf::Vector2i(0, 0), this->view));
sf::Vector2f pos = sf::Vector2f(event.size.width, event.size.height);
pos *= 0.5f;
pos = this->game->window.mapPixelToCoords(sf::Vector2i(pos), this->view);
this->guiSystem.at("menu").setPosition(pos);
this->game->background.setScale(
float(event.size.width) / float(this->game->background.getTexture()->getSize().x),
float(event.size.height) / float(this->game->background.getTexture()->getSize().y));
break;
}
/* Highlight menu items */
case sf::Event::MouseMoved:
{
this->guiSystem.at("menu").highlight(this->guiSystem.at("menu").getEntry(mousePos));
break;
}
/* Click on menu items */
case sf::Event::MouseButtonPressed:
{
if(event.mouseButton.button == sf::Mouse::Left)
{
std::string msg = this->guiSystem.at("menu").activate(mousePos);

if(msg == "load_game")
{
this->loadgame();
}
}
break;
}
case sf::Event::KeyPressed:
{
if(event.key.code == sf::Keyboard::Escape) this->game->window.close();
else if(event.key.code == sf::Keyboard::Space) this->loadgame();
break;
}
default: break;
Expand All @@ -69,4 +96,11 @@ GameStateStart::GameStateStart(Game* game)
this->view.setSize(pos);
pos *= 0.5f;
this->view.setCenter(pos);

this->guiSystem.emplace("menu", Gui(sf::Vector2f(192, 32), 4, false, game->stylesheets.at("button"),
{ std::make_pair("Load Game", "load_game") }));

this->guiSystem.at("menu").setPosition(pos);
this->guiSystem.at("menu").setOrigin(96, 32*1/2);
this->guiSystem.at("menu").show();
}
9 changes: 7 additions & 2 deletions game_state_start.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
#define GAME_STATE_START_HPP

#include <SFML/Graphics.hpp>
#include <map>
#include <string>

#include "game_state.hpp"
#include "gui.hpp"

class GameStateStart : public GameState
{
private:

sf::View view;

void loadgame();

std::map<std::string, Gui> guiSystem;

void loadgame();

public:

virtual void draw(const float dt);
Expand Down
135 changes: 135 additions & 0 deletions gui.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include <SFML/Graphics.hpp>
#include <string>

#include "gui.hpp"

sf::Vector2f Gui::getSize()
{
return sf::Vector2f(this->dimensions.x, this->dimensions.y * this->entries.size());
}

int Gui::getEntry(const sf::Vector2f mousePos)
{
/* If there are no entries then outside the menu */
if(entries.size() == 0) return -1;
if(!this->visible) return -1;

for(int i = 0; i < this->entries.size(); ++i)
{
/* Translate point to use the entry's local coordinates*/
sf::Vector2f point = mousePos;
point += this->entries[i].shape.getOrigin();
point -= this->entries[i].shape.getPosition();

if(point.x < 0 || point.x > this->entries[i].shape.getScale().x*this->dimensions.x) continue;
if(point.y < 0 || point.y > this->entries[i].shape.getScale().y*this->dimensions.y) continue;
return i;
}

return -1;
}

void Gui::setEntryText(int entry, std::string text)
{
if(entry >= entries.size() || entry < 0) return;

entries[entry].text.setString(text);

return;
}

void Gui::setDimensions(sf::Vector2f dimensions)
{
this->dimensions = dimensions;

for(auto& entry : entries)
{
entry.shape.setSize(dimensions);
entry.text.setCharacterSize(dimensions.y-style.borderSize-padding);
}

return;
}

void Gui::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
if(!visible) return;

/* Draw each entry of the menu */
for(auto entry : this->entries)
{
/* Draw the entry */
target.draw(entry.shape);
target.draw(entry.text);
}

return;
}

void Gui::show()
{
sf::Vector2f offset(0.0f, 0.0f);

this->visible = true;

/* Draw each entry of the menu */
for(auto& entry : this->entries)
{
/* Set the origin of the entry */
sf::Vector2f origin = this->getOrigin();
origin -= offset;
entry.shape.setOrigin(origin);
entry.text.setOrigin(origin);

/* Compute the position of the entry */
entry.shape.setPosition(this->getPosition());
entry.text.setPosition(this->getPosition());

if(this->horizontal) offset.x += this->dimensions.x;
else offset.y += this->dimensions.y;
}

return;
}

void Gui::hide()
{
this->visible = false;

return;
}

/* Highlights an entry of the menu */
void Gui::highlight(const int entry)
{
for(int i = 0; i < entries.size(); ++i)
{
if(i == entry)
{
entries[i].shape.setFillColor(style.bodyHighlightCol);
entries[i].shape.setOutlineColor(style.borderHighlightCol);
entries[i].text.setColor(style.textHighlightCol);
}
else
{
entries[i].shape.setFillColor(style.bodyCol);
entries[i].shape.setOutlineColor(style.borderCol);
entries[i].text.setColor(style.textCol);
}
}

return;
}

/* Return the message bound to the entry */
std::string Gui::activate(const int entry)
{
if(entry == -1) return "null";
return entries[entry].message;
}

std::string Gui::activate(sf::Vector2f mousePos)
{
int entry = this->getEntry(mousePos);
return this->activate(entry);
}
Loading

0 comments on commit d130078

Please sign in to comment.