-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
178 lines (148 loc) · 4.13 KB
/
Copy pathtest.cpp
File metadata and controls
178 lines (148 loc) · 4.13 KB
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
#include <iostream>
#include "Event.hpp"
#include <thread>
#include <chrono>
#include <list>
using namespace std;
Event::Event es;
// void my_event_checker( Event::id_type checker_id, const void* data ) {
// cout << "my event checker" << endl;
// es.EmitEvent( checker_id, 2, 67 );
// }
void my_event_click_handler( int x, int y) {
static uint32_t counter = 0;
if( ++counter == 100000 ) {
cout << "clicked at " << x << ", " << y << " : " << counter << endl;
counter = 0;
}
}
class EventWithClass {
private:
Event::Event* es;
Event::id_type id;
struct container {
Event::id_type listener_id;
int fixed_time;
int time;
};
std::list<container> time_list;
unsigned int time_passed;
void addToList( container &c ) {
c.time = c.fixed_time;
c.time += time_passed;
if( time_passed > c.time ) {
// reset time_passed
for( auto &e : time_list ) {
e.time -= time_passed;
}
time_passed = 0;
}
if( time_list.size() > 0 ) {
// find place to insert
bool found = false;
for( auto it = time_list.begin(); it != time_list.end(); it++ ) {
if( it->time > c.time ) {
time_list.insert( it, c );
found = true;
break;
}
}
if( !found )
time_list.push_back( c );
} else {
time_list.push_front( c );
}
}
public:
EventWithClass(std::string event_name, Event::Event* evt_system) : time_passed(0) {
es = evt_system;
es->Register(event_name,
[&] (bool add, Event::id_type listener_id, int time)
{ this->OnAddOrRemoveListener(add,listener_id,time); });
es->Listen("tick", [&](unsigned int time_step) {
this->OnCheckEvent(time_step);
});
}
void OnAddOrRemoveListener( bool add, Event::id_type listener_id, int time ) {
if(add) {
cout << "registered new listener: " << listener_id << " , with param " << time << endl;
container c;
c.listener_id = listener_id;
c.time = time;
c.fixed_time = time;
addToList( c );
} else {
for( auto it = time_list.begin(); it != time_list.end(); it++ ) {
if( it->listener_id == listener_id ) {
time_list.erase( it );
break;
}
}
cout << "on remove listener ..." << endl;
}
}
// void OnCheckEvent( const void* data ) {
// unsigned int time_step;
// Event::GetData( data, time_step );
void OnCheckEvent(unsigned int time_step) {
Event::id_type send_to_listener_id = 0;
unsigned int time_p;
time_passed += time_step;
if( time_passed >= time_list.front().time ) {
container c = time_list.front();
time_list.pop_front();
send_to_listener_id = c.listener_id;
time_p = time_passed - c.time;
addToList( c );
}
//cout << "on check event ... sending event to: " << send_to_listener_id << endl;
if( send_to_listener_id != 0 )
es->Emit( send_to_listener_id, time_p );
}
};
// events
void my_event( int a, char* b ) {
cout << "my event called " << a << " , " << b << endl;
}
void my_event2( const void* data ) {
cout << "my event2 called" << endl;
}
void my_event3( const void* data ) {
cout << "my event3 called" << endl;
}
void func_class_event_test( int time_diff ) {
cout << "1s passed with error " << time_diff << endl;
}
// void my_chain_event_checker( unsigned int checker_id, const void* data ) {
// cout << "chain event checker called" << endl;
// }
int main() {
Event::id_type tick_event = es.Register("tick");
cout << "tick_event: " << tick_event << endl;
EventWithClass ewc("timer", &es);
// es.RegisterEvent("timer", "tick", &ewc);
/*
es.Listen("timer", func_class_event_test, 1000);
es.Listen("timer", []( int error ) {
cout << "500ms passed with error " << error << endl;
}, 500);
// es.Listen("tick", []( int ts ) {
// cout << "TICK: " << ts << endl;
// });
es.Listen("timer", []() {
// cout << "\x1b[33munregistered some_event\x1b[0m" << endl;
// es.RemoveEventListener(evt);
// cout << "trying to unregister: " << evt2 << endl;
// es.RemoveEventListener(evt2);
//es.UnregisterEvent( "some_event" );
cout << "LOL\n";
}, 10000);
*/
// emit base event 1 time
unsigned int time_step = 1;
while(true) {
es.Emit( tick_event, time_step );
this_thread::sleep_for(chrono::milliseconds(time_step));
}
return 0;
}