-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessTimer.h
185 lines (156 loc) · 4.39 KB
/
ChessTimer.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef CHESSTIMER_H_INCLUDED
#define CHESSTIMER_H_INCLUDED
#include "ChessExceptions.h"
#include <SFML/Graphics.hpp>
#include <assert.h>
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <mutex>
using namespace std;
using namespace sf;
struct MinuteSecond{
int minute, second;
MinuteSecond(int _minute, int _second){
minute=_minute;
second=_second;
}
};
/*
Esta estructura llevara el manejo del reloj del juego
*/
struct Game_Timer
{
/*Atributos del Timer*/
// Variables con rutas de la imagen del Timer
Texture clock_texture, hand_timer_texture;
Sprite clock_sprite,hand_timer_sprite;
bool eneable;
int seconds, minuts;
int limit_seconds, limit_minuts;
int current_time;
int limit_time;
int running=false;
void (*callback)();
float degrees;
/* Constructores */
Game_Timer()
{
eneable = false;
init_Timer();
}
Game_Timer(int ¤t_time_external)
{
eneable = false;
current_time = current_time_external;
init_Timer();
}
Game_Timer(int ¤t_time_external, MinuteSecond limitTime, void _callback())
{
eneable = false;
clock_texture = loadResource(TIMER_FRAME_FILE);
hand_timer_texture = loadResource(TIMER_HAND_FILE);
clock_sprite.setTexture(clock_texture);
hand_timer_sprite.setTexture(hand_timer_texture);
current_time = current_time_external;
init_Timer();
setMinutsLimit(limitTime.minute);
setSecondsLimit(limitTime.second);
callback=_callback;
}
/* Metodos del Timer*/
//Inicializa los valores por defecto del Timer
void init_Timer()
{
minuts=current_time/60;
seconds=current_time%60;
degrees = 0;
on_timer();
}
//Aqui se maneja la ejecuccion del hilo cunado se invoca el laucher
operator()()
{
mutex mtx;
mtx.lock();
cout<<"Iniciando contador"<<endl;
cout<<"CURRENT TIME: "<<current_time<<"\n";
on_timer();
limit_time = convert_Time_Limit();
degrees = 360/limit_time;
run();
mtx.unlock();
}
//Metodos de control para el Timer, si se ecnuentra ncendido o apagado
void on_timer()
{
running = true;
}
void off_timer()
{
running = false;
}
//En este metodo se ejecuta todas las acciones del timer
void run ()
{
cout<<"Limite de tiempo:"<<limit_time<<endl;
cout<<degrees<<endl;
running=true;
while(limit_time>current_time && running){
updateTimerSprite();
Sleep(1000);
if(seconds == 60){
seconds = 0;
minuts += 1;
}
seconds +=1;
cout<<"Segundos:"<<seconds<<endl;
current_time = (minuts*60) + seconds;
cout<<"Tiempo actual:"<<current_time<<endl;
}
//Termino del turno
if(running){
cout<<"Cambiando turno"<<endl;
callback();
reset_Timer();
}
running=false;
}
void updateTimerSprite(){
hand_timer_sprite.setOrigin(sf::Vector2f(50,50));
hand_timer_sprite.setPosition(sf::Vector2f(50,50));
hand_timer_sprite.setRotation( degrees * current_time );
}
void stop(){
running=false;
}
//Esta funcion convierte los limites de tiempo a una unidad unica para su manejo
int convert_Time_Limit()
{
int sum;
sum = limit_seconds + (limit_minuts*60);
return sum;
}
//Metodo para configurar los calores de segundos y minutos a su valor por defecto
void reset_Timer()
{
cout<<"Se ha reseteado el Timer"<<endl;
seconds = 0;
minuts = 0;
}
//Metodos para configurar el limite de tiempo en minutos y segundos
void setMinutsLimit(int new_minuts_limit){
limit_minuts = new_minuts_limit;
assert(limit_minuts >= 0 && new_minuts_limit <= 60);
}
void setSecondsLimit(int new_seconds_limit){
limit_seconds = new_seconds_limit;
assert(limit_seconds >= 0 && new_seconds_limit <= 60);
}
//Funcion amiga para el operador <<
friend ostream& operator <<(ostream& os, Game_Timer& _game_timer)
{
os << _game_timer.minuts <<":"<<_game_timer.seconds;
return os;
}
};
#endif // CHESSTIMER_H_INCLUDED