menu and scenes

This commit is contained in:
Ralusama19 2017-01-20 23:52:42 +01:00
parent 3ba7354efe
commit cebde72729
14 changed files with 144 additions and 26 deletions

View file

@ -9,7 +9,9 @@ SOURCES += main.cpp \
mygame.cpp \ mygame.cpp \
button.cpp \ button.cpp \
character.cpp \ character.cpp \
compas.cpp compas.cpp \
scene.cpp \
menu.cpp
HEADERS += \ HEADERS += \
game.hpp \ game.hpp \
@ -17,4 +19,6 @@ HEADERS += \
commons.hpp \ commons.hpp \
button.hpp \ button.hpp \
character.hpp \ character.hpp \
compas.hpp compas.hpp \
scene.hpp \
menu.hpp

View file

@ -25,11 +25,11 @@ int Button::getClicks(){
int c = _clicks; int c = _clicks;
//cout << "popping clicks " << c << endl; //cout << "popping clicks " << c << endl;
_clicks = 0; _clicks = 0;
//cout << "popclicks = " << _clicks << endl; if (_clicks > 0) std::cout << "popclicks = " << _clicks << std::endl;
return c; return c;
} }
void Button::update(sf::Vector2i mousePosition){ void Button::update(sf::Vector2f mousePosition){
//cout << "state = " << _state << endl; //cout << "state = " << _state << endl;
if(_state != ButtonState::off){ if(_state != ButtonState::off){
if(!inside(mousePosition)) _state = 0; if(!inside(mousePosition)) _state = 0;
@ -39,7 +39,7 @@ void Button::update(sf::Vector2i mousePosition){
setTextureRect(sf::IntRect(0,_ySize*_state,_xSize,_ySize)); setTextureRect(sf::IntRect(0,_ySize*_state,_xSize,_ySize));
} }
bool Button::inside(sf::Vector2i position){ bool Button::inside(sf::Vector2f position){
sf::FloatRect boundingBox = getGlobalBounds(); sf::FloatRect boundingBox = getGlobalBounds();
if (boundingBox.contains(sf::Vector2f(position.x, position.y))) return true; if (boundingBox.contains(sf::Vector2f(position.x, position.y))) return true;
//cout << "outside!" << endl; //cout << "outside!" << endl;
@ -51,13 +51,14 @@ void Button::handleMouseEvent(sf::Event& event){
switch (event.type){ switch (event.type){
case (sf::Event::MouseButtonPressed): case (sf::Event::MouseButtonPressed):
if(_state == ButtonState::active) _state = ButtonState::pressed; if(_state == ButtonState::active) _state = ButtonState::pressed;
std::cout << "pressed!" << _clicks << std::endl;
break; break;
case (sf::Event::MouseButtonReleased): case (sf::Event::MouseButtonReleased):
if(_state == ButtonState::pressed){ if(_state == ButtonState::pressed){
_state = ButtonState::active; _state = ButtonState::active;
++_clicks; ++_clicks;
//cout << "Click!" << _clicks << endl; std::cout << "released!" << _clicks << std::endl;
} }
break; break;

View file

@ -13,13 +13,13 @@ public:
int getClicks(); int getClicks();
//buttons auto-detect if they are pressed, you just have to passa mouse-event to them //buttons auto-detect if they are pressed, you just have to passa mouse-event to them
void handleMouseEvent(sf::Event& event); void handleMouseEvent(sf::Event& event);
void update(sf::Vector2i mousePosition); void update(sf::Vector2f mousePosition);
protected: protected:
int _xSize; int _xSize;
int _ySize; int _ySize;
sf::Texture _texture; sf::Texture _texture;
bool inside(sf::Vector2i position); bool inside(sf::Vector2f position);
private: private:
int _clicks; int _clicks;

View file

@ -8,6 +8,8 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
const int SCENE_NUM = 4;
#define ASSERT(expression) do \ #define ASSERT(expression) do \
{ \ { \
if(!(expression)) { \ if(!(expression)) { \
@ -26,8 +28,8 @@ namespace ButtonState {
enum basicState {released, active, pressed, off}; enum basicState {released, active, pressed, off};
} }
namespace GameState { namespace GameScene {
enum gameState{menu,inGame,help,credits}; enum gameScene{menu,inGame,help,credits};
} }
namespace PlayerState { namespace PlayerState {

View file

@ -7,8 +7,7 @@
static const float margeErr = 0.80f; static const float margeErr = 0.80f;
class Compas class Compas{
{
private: private:
bool isPress; bool isPress;

View file

@ -13,6 +13,10 @@ Game::~Game() {
Game::instance = nullptr; Game::instance = nullptr;
} }
Game* Game::i() {
return Game::instance;
}
// Main game loop // Main game loop
void Game::run() { void Game::run() {
sf::Clock c; sf::Clock c;

View file

@ -6,7 +6,7 @@ class Game {
public: public:
Game(); Game();
virtual ~Game(); virtual ~Game();
static Game* i() { return Game::instance;} static Game* i();
virtual void run(); virtual void run();

View file

@ -1,7 +1,7 @@
#include "mygame.hpp" #include "mygame.hpp"
#include "compas.hpp" #include "compas.hpp"
int main() { int main() {
MyGame game = MyGame(); MyGame game;
game.run(); game.run();
return 0; return 0;
} }

40
VaporWaveWars/menu.cpp Normal file
View file

@ -0,0 +1,40 @@
#include "menu.hpp"
#include "mygame.hpp"
Menu::Menu() {
_start.setPosition(5,5);
_exit.setPosition(100,100);
_start.turnOn();
_exit.turnOn();
_buttons.push_back(&_start);
_buttons.push_back(&_exit);
}
void Menu::update(float deltaTime, sf::RenderWindow*window){
//float dx = InputManager::action(InputAction::moveX0);
//_buttons[0]->move(dx,0);
//std::cout << dx << std::endl;
for(unsigned int i = 0; i < _buttons.size(); ++i){
_buttons[i]->update(window->mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(*window))));
}
}
void Menu::draw(sf::RenderWindow* window){
for(unsigned int i = 0; i < _buttons.size(); ++i){
window->draw(*_buttons[i]);
}
}
void Menu::updateButtons(sf::Event e){
// std::cout << "update menu buttons" << std::endl;
for(unsigned int i = 0; i < _buttons.size(); ++i){
_buttons[i]->handleMouseEvent(e);
}
MyGame* g = static_cast<MyGame*>(Game::i());
if (_start.getClicks() > 0){
// g->changeScene(GameScene::inGame);
std::cout << "Game not ready to be played. Please Wait." << std::endl;
}
else if(_exit.getClicks() > 0) g->isRunning = false;
}

22
VaporWaveWars/menu.hpp Normal file
View file

@ -0,0 +1,22 @@
#ifndef MENU_HPP
#define MENU_HPP
#include "commons.hpp"
#include "scene.hpp"
#include "button.hpp"
class Menu : public Scene{
public:
Menu();
Menu(const Menu& m) = delete;
Menu(const Menu&& m) = delete;
Menu& operator=(Menu& m) = delete;
Menu& operator=(Menu&& m) = delete;
void update(float deltaTime, sf::RenderWindow *window) final override;
void draw(sf::RenderWindow *window) final override;
void updateButtons(sf::Event e) final override;
private:
Button _start = Button("/home/raluca/Projects/WaveGGJ17/build-VaporWaveWars-Desktop-Debug/Resources/buttontejempl.png");
Button _exit = Button("/home/raluca/Projects/WaveGGJ17/build-VaporWaveWars-Desktop-Debug/Resources/buttontejempl2.png");
std::vector<Button*> _buttons;
};
#endif // MENU_HPP

View file

@ -1,48 +1,64 @@
#include "mygame.hpp" #include "mygame.hpp"
MyGame::MyGame() { MyGame::MyGame() {
_state = GameState::menu; _scene = GameScene::menu;
_scenes = std::vector<Scene*>(SCENE_NUM);
_scenes[GameScene::menu] = &_menu;
std::cout << "in menu" << std::endl; std::cout << "in menu" << std::endl;
} }
MyGame::~MyGame() { MyGame::~MyGame() {
} }
MyGame* i(){
return static_cast<MyGame*>(Game::i());
}
void MyGame::changeScene(GameScene::gameScene n){
_scene = n;
}
// Main game loop // Main game loop
void MyGame::update(float deltaTime, sf::RenderWindow*window) { void MyGame::update(float deltaTime, sf::RenderWindow*window) {
sf::Event event; sf::Event event;
while(window->pollEvent(event)){ while(window->pollEvent(event)){
switch (event.type) { switch (event.type) {
case (sf::Event::Closed): case (sf::Event::Closed):
Game::i()->isRunning = false; Game::i()->isRunning = false;
break; break;
case (sf::Event::KeyPressed): case (sf::Event::KeyPressed):
if(event.key.code == sf::Keyboard::Escape) if(event.key.code == sf::Keyboard::Escape)
// Exit the game like this // Exit the game like this
Game::i()->isRunning = false; Game::i()->isRunning = false;
break; break;
case (sf::Event::MouseMoved): case (sf::Event::MouseMoved):
case (sf::Event::MouseButtonPressed): case (sf::Event::MouseButtonPressed):
switch(_state){ case (sf::Event::MouseButtonReleased):
case(GameState::menu):
std::cout << "still in menu" << std::endl; _scenes[_scene]->updateButtons(event);
switch(_scene){
case(GameScene::menu):
//std::cout << "still in menu" << std::endl;
break; break;
default: default:
break; break;
} }
default: default:
break; break;
} }
} }
// do shit // do shit
_scenes[_scene]->update(deltaTime,window);
} }
void MyGame::draw(sf::RenderWindow*window) { void MyGame::draw(sf::RenderWindow*window) {
//a e s t h e t i c s //a e s t h e t i c s
window->clear(sf::Color::Cyan); window->clear(sf::Color::Cyan);
// draw shit // draw shit
_scenes[_scene]->draw(window);
window->display(); window->display();
} }

View file

@ -1,14 +1,18 @@
#ifndef MYGAME_HPP #ifndef MYGAME_HPP
#define MYGAME_HPP #define MYGAME_HPP
#include "game.hpp" #include "game.hpp"
#include "menu.hpp"
class MyGame : public Game{ class MyGame : public Game{
public: public:
MyGame(); MyGame();
virtual ~MyGame(); virtual ~MyGame();
static MyGame* i();
void changeScene(GameScene::gameScene n);
private: private:
GameState::gameState _state; GameScene::gameScene _scene;
std::vector<Scene*> _scenes;
Menu _menu;
virtual void update(float deltaTime, sf::RenderWindow *window) final override; virtual void update(float deltaTime, sf::RenderWindow *window) final override;
virtual void draw(sf::RenderWindow *window) final override; virtual void draw(sf::RenderWindow *window) final override;
}; };

8
VaporWaveWars/scene.cpp Normal file
View file

@ -0,0 +1,8 @@
#include "scene.hpp"
Scene::Scene(){
}
Scene::~Scene(){
}

18
VaporWaveWars/scene.hpp Normal file
View file

@ -0,0 +1,18 @@
#ifndef SCENE_HPP
#define SCENE_HPP
#include "commons.hpp"
#include "game.hpp"
class Scene
{
public:
Scene();
virtual ~Scene();
virtual void update(float deltaTime, sf::RenderWindow *window) = 0;
virtual void draw(sf::RenderWindow *window) = 0;
virtual void updateButtons(sf::Event) = 0;
protected:
Game* parent;
};
#endif // SCENE_HPP