-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCCGameSWF.cpp
249 lines (225 loc) · 6.74 KB
/
CCGameSWF.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//
// CCGameSWF.m
// CCGameSWF
//
// Created by dario on 13-02-22.
// Modify by chenee (chenee543216@gmail.com) on 13-05-20
// @chenee: 1\change Objective-C to C++ ;2\change opengl es 1.1 to opengl es 2.0
//
//
#include "CCGameSWF.h"
#include "CCGameSWF.h"
#include "gameswf.h"
#include "gameswf_character.h"
#include "tu_file.h"
using namespace cocos2d;
#pragma mark - Callback declarations
// handler functions //
static void CCGameSWF_fscommand_handler (gameswf::character* movie, const char* command, const char* arg); // fscommand handler //
static tu_file* CCGameSWF_file_opener (const char* url_or_path); // file opener //
static void CCGameSWF_log_handler (bool error, const char* message); // log handler //
#pragma mark - CCGameSWF implementation
//@implementation CCGameSWF
CCGameSWF* CCGameSWF::sharedInstance()
{
static CCGameSWF *s_ccGameSWF_sharedInstance = NULL;
if (!s_ccGameSWF_sharedInstance)
{
s_ccGameSWF_sharedInstance = new CCGameSWF();
s_ccGameSWF_sharedInstance->init();
}
return s_ccGameSWF_sharedInstance;
}
bool CCGameSWF::init()
{
// self = [super init];
// if (self)
// {
// m_fscommandListeners = [[NSMutableDictionary alloc] init];
m_fscommandListeners = new cocos2d::CCArray();
gameswf::register_file_opener_callback(&CCGameSWF_file_opener);
gameswf::register_fscommand_callback(&CCGameSWF_fscommand_handler);
#ifdef DEBUG
gameswf::register_log_callback(&CCGameSWF_log_handler);
#endif
// TODO: Enable audio //
sound = gameswf::create_sound_handler_openal();
gameswf::set_sound_handler(sound);
gameswf::render_handler* render = gameswf::create_render_handler_ogles();
gameswf::set_render_handler(render);
gameswf::set_glyph_provider(gameswf::create_glyph_provider_tu());
// }
// return self;
return true;
}
void CCGameSWF::dealloc()
{
// [m_fscommandListeners release];
m_fscommandListeners->release();
// [super dealloc];
}
#pragma mark - fscommand handlers
//void addFscommandResponder:(id<CCSWFFscommandResponder>)responder forMovieNamed:(NSString*)movieName
//{
// NSMutableArray *movieResponders = [m_fscommandListeners objectForKey:movieName];
// if (!movieResponders)
// {
// movieResponders = [NSMutableArray array];
// [m_fscommandListeners setObject:movieResponders forKey:movieName];
// }
//
// [movieResponders addObject:responder];
//}
//
//-(void) removeFscommandResponder:(id<CCSWFFscommandResponder>)responder forMovieNamed:(NSString*)movieName
//{
// NSMutableArray *movieResponders = [m_fscommandListeners objectForKey:movieName];
// if (!movieResponders)
// {
// NSLog(@"ERROR: trying to remove fscommand responder from empty responder list, did you pass the correct movie name?");
// return;
// }
//
// [movieResponders removeObject:responder];
//
// if ([movieResponders count] == 0)
// {
// [m_fscommandListeners removeObjectForKey:movieName];
// }
//}
//void movieNamed:(NSString*)movieName sentCommand:(NSString*)command withArguments:(NSString*)args
////void movieNamedSentCommandWithArguments(CCString* movieName,CCString*command,CCString*args)
//{
// NSMutableArray *movieResponders = [m_fscommandListeners objectForKey:movieName];
// if (movieResponders)
// {
// for (int i = 0; i < [movieResponders count]; ++i)
// {
// id<CCSWFFscommandResponder> responder = [movieResponders objectAtIndex:i];
// if ([responder respondsToSelector:@selector(movieNamed:sentCommand:withArguments:)])
// {
// [responder movieNamed:movieName sentCommand:command withArguments:args];
// }
// }
// }
//}
//
//@end
#pragma mark - Callback implementations
void CCGameSWF_fscommand_handler (gameswf::character* movie, const char* command, const char* arg)
{
// [[CCGameSWF sharedInstance] movieNamed:[NSString stringWithUTF8String:movie->m_name.c_str()] sentCommand:[NSString stringWithUTF8String:command] withArguments:[NSString stringWithUTF8String:arg]];
}
tu_file* CCGameSWF_file_opener (const char* url_or_path)
{
assert(url_or_path);
if (strncasecmp(url_or_path, "http://", 7) == 0)
{
// const char* slash = strchr(url_or_path + 7, '/');
// if (slash == NULL)
// {
// return NULL;
// }
//
// char host[256];
// memset(host, 0, 256);
// strncpy(host, url_or_path + 7, slash - url_or_path - 7);
//
// int len = 0;
// const void* data = NULL;
// tu_string swfile;
// tu_string hisfile;
//
// NSString* swfFile = [NSString stringWithFormat: @"%@/Documents/my.swf", NSHomeDirectory()];
// swfile = [swfFile UTF8String];
// NSString* hisFile = [NSString stringWithFormat: @"%@/Documents/his.txt", NSHomeDirectory()];
// hisfile = [hisFile UTF8String];
// NSString* hostName = [NSString stringWithUTF8String: host];
// NSString* pathToFile= [NSString stringWithUTF8String: slash];
// NSURL* fileURL = [[NSURL alloc] initWithScheme:@"http" host:hostName path:pathToFile];
// if (fileURL)
// {
// NSData* fileContents = [NSData dataWithContentsOfURL:fileURL];
// if (fileContents)
// {
// len = [fileContents length];
// data = [fileContents bytes];
// }
// else
// {
// return NULL;
// }
// }
// else
// {
// return NULL;
// }
//
// // write swf
// {
// tu_file fi(swfile.c_str(), "wb");
// fi.write_bytes(data, len);
// }
//
// // update history
//
// // read history
// array<tu_string> his;
// {
// tu_file fi(hisfile.c_str(), "r");
// if (fi.get_error() == TU_FILE_NO_ERROR)
// {
// char buf[1024];
// while (fi.read_string(buf, 1024) > 1) // !!!!
// {
// his.push_back(buf);
// }
// }
// }
//
// // test for same
// bool is_new = true;
// for (int i = 0; i < his.size(); i++)
// {
// if (his[i] == url_or_path + 7)
// {
// is_new = false;
// break;
// }
// }
//
// // append new item
// if (is_new)
// {
// his.push_back(url_or_path + 7);
// }
//
// // truncate to 20
// while (his.size() > 20)
// {
// his.remove(0);
// }
//
// // save history
// {
// tu_file fi(hisfile.c_str(), "w");
// for (int i = 0; i < his.size(); i++)
// {
// fi.write_string(his[i].c_str());
// fi.write_string("\n");
// }
// }
// // printf("%s as %s\n", url, swfile.c_str());
// return new tu_file(swfile.c_str(), "rb");
}
return new tu_file(url_or_path, "rb");
}
static void CCGameSWF_log_handler (bool error, const char* message)
{
// NSLog(@"%@: %s", (error ? @"::ERROR: " : @":::: "), message);
if (error) {
CCLOG("::ERROR:: %s",message);
}else{
CCLOG(":::: %s",message);
}
}