This commit is contained in:
serk 2017-01-20 23:13:36 +01:00
parent f1fbf74bc4
commit 06878adc7e
5 changed files with 37 additions and 16 deletions

View file

@ -7,6 +7,9 @@
#include <assert.h> #include <assert.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
#define MARGEERR 0.30f
#define BLACKVALUE 0.00025f
#define ASSERT(expression) do \ #define ASSERT(expression) do \
{ \ { \
if(!(expression)) { \ if(!(expression)) { \

View file

@ -1,7 +1,7 @@
#include "compas.hpp" #include "compas.hpp"
Compas::Compas() { Compas::Compas() {
spaceTime = 0; spaceTime = 1;
isPress = false; isPress = false;
} }
@ -9,7 +9,7 @@ void Compas::start() {
if (not isPress) { if (not isPress) {
std::cout << "start" << std::endl; std::cout << "start" << std::endl;
isPress = true; isPress = true;
spaceTime = 0; spaceTime = 1;
notes = std::vector<int>(); notes = std::vector<int>();
} }
} }
@ -17,7 +17,7 @@ void Compas::start() {
void Compas::add() { void Compas::add() {
if (isPress) { if (isPress) {
notes.push_back(spaceTime); notes.push_back(spaceTime);
spaceTime = 0; spaceTime = 1;
std::cout << "add" << std::endl; std::cout << "add" << std::endl;
} }
} }
@ -32,7 +32,7 @@ void Compas::end() {
} }
} }
void Compas::incriseTime() { void Compas::incraeseTime() {
++spaceTime; ++spaceTime;
} }
@ -40,11 +40,16 @@ int Compas::get (int i) const {
return notes[i]; return notes[i];
} }
int Compas::size() const {
return notes.size();
}
bool Compas::operator ==(const Compas& d) const{ bool Compas::operator ==(const Compas& d) const{
int n = notes.size(); int n = notes.size();
if (n != d.size()) return false;
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
float diff = ((float) (std::abs(notes[i] - d.get(i))))/((float) (notes[i])); float diff = ((float) (std::abs(notes[i] - d.get(i))))/((float) (notes[i]));
if (diff > margeErr) return false; if (diff > MARGEERR) return false;
} }
return true; return true;
} }

View file

@ -4,24 +4,22 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include <commons.hpp>
static const float margeErr = 0.80f; class Compas {
class Compas
{
private: private:
bool isPress; bool isPress;
int spaceTime; int spaceTime;
std::vector<int> notes; std::vector<int> notes;
int get(int i) const;
int size() const;
public: public:
Compas(); Compas();
void start(); void start();
void add(); void add();
void end(); void end();
void incriseTime(); void incraeseTime();
int get(int i) const;
bool operator ==(const Compas& d) const; bool operator ==(const Compas& d) const;
}; };

View file

@ -3,6 +3,7 @@
MyGame::MyGame() { MyGame::MyGame() {
_state = GameState::menu; _state = GameState::menu;
std::cout << "in menu" << std::endl; std::cout << "in menu" << std::endl;
compas2 = compas = Compas();
} }
MyGame::~MyGame() { MyGame::~MyGame() {
@ -10,6 +11,8 @@ MyGame::~MyGame() {
// Main game loop // Main game loop
void MyGame::update(float deltaTime, sf::RenderWindow*window) { void MyGame::update(float deltaTime, sf::RenderWindow*window) {
// std::cout << deltaTime << std::endl;
if (deltaTime > BLACKVALUE) compas.incraeseTime();
sf::Event event; sf::Event event;
while(window->pollEvent(event)){ while(window->pollEvent(event)){
switch (event.type) { switch (event.type) {
@ -17,9 +20,19 @@ void MyGame::update(float deltaTime, sf::RenderWindow*window) {
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 Game::i()->isRunning = false; // Exit the game like this
Game::i()->isRunning = false; if(event.key.code == sf::Keyboard::C)
compas.start();
if(event.key.code == sf::Keyboard::Space)
compas.add();
break;
case (sf::Event::KeyReleased):
if (event.key.code == sf::Keyboard::C) {
compas.end();
std::cout << (compas == compas2) << std::endl;
compas2 = compas;
}
break; break;
case (sf::Event::MouseMoved): case (sf::Event::MouseMoved):
case (sf::Event::MouseButtonPressed): case (sf::Event::MouseButtonPressed):

View file

@ -1,13 +1,15 @@
#ifndef MYGAME_HPP #ifndef MYGAME_HPP
#define MYGAME_HPP #define MYGAME_HPP
#include "game.hpp" #include "game.hpp"
#include "compas.hpp"
class MyGame : public Game{ class MyGame : public Game{
public: public:
MyGame(); MyGame();
virtual ~MyGame(); virtual ~MyGame();
private: private:
Compas compas, compas2;
GameState::gameState _state; GameState::gameState _state;
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;