-
Notifications
You must be signed in to change notification settings - Fork 217
/
swf.cpp
179 lines (156 loc) · 3.52 KB
/
swf.cpp
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
#include <iostream>
#include <string.h>
#include <pthread.h>
#include <SDL/SDL.h>
#include "swf.h"
#include "actions.h"
using namespace std;
pthread_t ParseThread::t;
pthread_t InputThread::t;
std::list < IActiveObject* > InputThread::listeners;
sem_t InputThread::sem_listeners;
extern SystemState sys;
int thread_debug(char* msg);
SWF_HEADER::SWF_HEADER(ifstream& in)
{
//Valid only on little endian platforms
in >> Signature[0] >> Signature[1] >> Signature[2];
if(Signature[0]!='F' || Signature[1]!='W' || Signature[2]!='S')
throw "bad file\n";
in >> Version >> FileLenght >> FrameSize >> FrameRate >> FrameCount;
}
MovieClip::MovieClip()
{
sem_init(&sem_frames,0,1);
}
SystemState::SystemState():currentDisplayList(&clip.displayList),currentState(&clip.state)
{
sem_init(&sem_dict,0,1);
sem_init(&new_frame,0,0);
sem_init(&sem_run,0,0);
}
bool list_orderer(const DisplayListTag* a, int d)
{
return a->getDepth()<d;
}
void* ParseThread::worker(void* in_ptr)
{
ifstream& f=*(ifstream*)in_ptr;
int done=0;
try
{
TagFactory factory(f);
while(1)
{
Tag* tag=factory.readTag();
switch(tag->getType())
{
// case TAG:
case END_TAG:
//sleep(5);
return 0;
case RENDER_TAG:
// std::cout << "add to dict" << std::endl;
// thread_debug( "PARSER: dict mutex lock");
sem_wait(&sys.sem_dict);
sys.dictionary.push_back(dynamic_cast<RenderTag*>(tag));
// thread_debug( "PARSER: dict mutex unlock");
sem_post(&sys.sem_dict);
break;
case DISPLAY_LIST_TAG:
if(dynamic_cast<DisplayListTag*>(tag)->add_to_list)
{
list<DisplayListTag*>::iterator it=lower_bound(sys.clip.displayList.begin(),
sys.clip.displayList.end(),
dynamic_cast<DisplayListTag*>(tag)->getDepth(),
list_orderer);
sys.clip.displayList.insert(it,dynamic_cast<DisplayListTag*>(tag));
}
break;
case SHOW_TAG:
{
// thread_debug( "PARSER: frames mutex lock");
sem_wait(&sys.clip.sem_frames);
sys.clip.frames.push_back(Frame(sys.clip.displayList));
// thread_debug( "PARSER: frames mutex unlock" );
sem_post(&sys.new_frame);
//\ thread_debug("PARSER: new frame signal");
sem_post(&sys.clip.sem_frames);
break;
}
case CONTROL_TAG:
dynamic_cast<ControlTag*>(tag)->execute();
break;
}
}
}
catch(const char* s)
{
cout << "CATCH!!!!" << endl;
cout << s << endl;
cout << "ERRORE!!!!" << endl;
exit(-1);
}
}
ParseThread::ParseThread(ifstream& in)
{
pthread_create(&t,NULL,worker,&in);
}
void ParseThread::wait()
{
pthread_join(t,NULL);
}
InputThread::InputThread()
{
cout << "creating input" << endl;
sem_init(&sem_listeners,0,1);
pthread_create(&t,NULL,worker,NULL);
}
void InputThread::wait()
{
pthread_join(t,NULL);
}
void* InputThread::worker(void* in_ptr)
{
SDL_Event event;
cout << "waiting for input" << endl;
while(SDL_WaitEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
{
switch(event.key.keysym.sym)
{
case SDLK_q:
exit(0);
break;
case SDLK_n:
list<IActiveObject*>::const_iterator it=listeners.begin();
cout << "Fake mouse event" << endl;
int c=0;
for(it;it!=listeners.end();it++)
{
if(c==2)
(*it)->MouseEvent(0,0);
c++;
}
sem_post(&sys.sem_run);
break;
}
break;
}
case SDL_MOUSEMOTION:
{
printf("Oh! mouse\n");
break;
}
}
}
}
void InputThread::addListener(IActiveObject* ob)
{
sem_wait(&sem_listeners);
listeners.push_back(ob);
sem_post(&sem_listeners);
}