-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
127 lines (109 loc) · 2.04 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#pragma once
#ifndef UTILS_H_INCLUDED
#define UTILS_H_INCLUDED
#include <chrono>
#include <SFML/System/Vector2.hpp>
#include "block_properties.h"
#include "log.h"
#include "program_const.h"
class Utils
{
public:
static BlockOrientation swapside(int side)
{
return (BlockOrientation)(side < 4 ? side + 4 : side - 4);
}
static BlockOrientation rotate(bool clockwise, int rot)
{
if (clockwise)
{
rot++;
if (rot == 4)
rot = 0;
}
else
{
if (rot == 0)
rot = 3;
else
rot--;
}
return (BlockOrientation)rot;
}
static sf::Vector2f origin(BlockOrientation rot)
{
switch (rot)
{
case RIGHT:
return sf::Vector2f(0, SPRITE_SIZE);
case BOT:
return sf::Vector2f(SPRITE_SIZE, SPRITE_SIZE);
case LEFT:
return sf::Vector2f(SPRITE_SIZE, 0);
default:
return sf::Vector2f(0, 0);
}
}
// CLASSES
class Vec2i_predicate
{
public:
bool operator()(sf::Vector2i const& v1, sf::Vector2i const& v2) const
{
if (v1.y < v2.y)
return true;
if (v1.y == v2.y && v1.x < v2.x)
return true;
return false;
}
};
class TimeCounter
{
std::chrono::steady_clock::time_point m_start;
unsigned long m_duration;
bool m_started;
public:
TimeCounter(unsigned int duration_ms)
{
m_duration = duration_ms * 1000000;
m_started = false;
}
TimeCounter()
{
m_duration = 1000000000l;
m_started = false;
}
void start()
{
m_start = std::chrono::steady_clock::now();
m_started = true;
}
double progress()
{
if (m_started)
{
std::chrono::steady_clock::duration time = std::chrono::steady_clock::now() - m_start;
double result = (long double)time.count() / (long double)m_duration;
return result;
}
return 0;
}
void reset()
{
m_started = false;
}
bool finished() const
{
if (m_started) {
std::chrono::steady_clock::duration duration = std::chrono::steady_clock::now() - m_start;
return duration.count() >= m_duration;
}
return false;
}
bool started() const
{
return m_started;
}
};
};
#endif // !UTILS_H_INCLUDED