-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCCSWFNode.cpp
697 lines (595 loc) · 17.8 KB
/
CCSWFNode.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//
// CCSWFNode.m
// GSGGameSWF
//
// Created by dario on 13-02-25.
// 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 "CCSWFNode.h"
#include "CCGameSWF.h"
#include "cocos2d.h"
#include "gameswf.h"
#include "gameswf_player.h"
#include "gameswf_root.h"
#include "ccMacros.h"
#include "gameswf_types.h"
#include "gameswf_impl.h"
using namespace cocos2d;
//@interface CCSWFNode_touchContainer : NSObject
//{
// CGPoint m_position;
// int m_state;
//}
//
//@property (readonly) CGPoint position;
//@property (readonly) int state;
//
//+(id) touchContainerWithPosition:(CGPoint)position andState:(int)state;
//-(id) initWithPosition:(CGPoint)position andState:(int)state;
//
//@end
class CCSWFNode_touchContainer : public cocos2d::CCNode
{
CCPoint m_position;
int m_state;
CCSWFNode_touchContainer(CCPoint position,int state);
// bool touchContainerWithPosition(CCPoint position, int state);
};
//@implementation CCSWFNode_touchContainer
//@synthesize position = m_position;
//@synthesize state = m_state;
CCSWFNode_touchContainer::CCSWFNode_touchContainer(CCPoint position, int state)
{
m_position = position;
m_state = state;
}
//bool CCSWFNode_touchContainer::touchContainerWithPosition(CCPoint position, int state)
//{
//// return [[CCSWFNode_touchContainer alloc] initWithPosition:position andState:state];
// return true;
//}
//
//void CCSWFNode_touchContainer::initWithPositionAndState(CCPoint position, int state)
//{
//// self = [super init];
//// if (self)
//// {
// m_position = position;
// m_state = state;
//// }
//// return self;
//}
//
//@end
//@interface CCSWFNode_imp : NSObject
//{
//@public
// gameswf::gc_ptr<gameswf::player> m_player;
// gameswf::gc_ptr<gameswf::root> m_movie;
//}
//
//-(id) initWithSWFFile:(NSString*)file;
//
//@end
class CCSWFNode_imp : public cocos2d::CCNode
{
public:
gameswf::gc_ptr<gameswf::player> m_player;
gameswf::gc_ptr<gameswf::root> m_movie;
bool initWithSWFFile(const char *file);
// void dealloc();
};
//@implementation CCSWFNode_imp
bool CCSWFNode_imp::initWithSWFFile(const char *file)
{
std::string pathKey = file;
pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(pathKey.c_str());
if (pathKey.size() == 0)
{
return false;
}
// self = [super init];
// if (self)
// {
// make sure CCGameSWF is initialized //
// [CCGameSWF sharedInstance];
CCGameSWF::sharedInstance();
m_player = new gameswf::player();
m_movie = m_player->load_file(pathKey.c_str());
if (m_movie == NULL)
{
printf("ERROR: Cannot open input file %s", file);
// [self release];
return false;
}
// }
//
// return self;
return true;
}
//void CCSWFNode_imp::dealloc()
//{
// delete m_movie;
// delete m_player;
//// [super dealloc];
//}
//@end
//@implementation CCSWFNode
CCSWFNode::CCSWFNode()
{
// CCLOG("CCSWFNode init ....");
}
CCSWFNode::~CCSWFNode()
{
this->stopFlash();
this->removeFromParentAndCleanup(true);
this->imp->m_movie = NULL;
this->imp->m_player = NULL;
delete imp;
// CCLOG("CCSWFNode delete ....");
}
CCString* CCSWFNode::movieName()
{
// return [NSString stringWithUTF8String:imp->m_movie->m_movie->m_name.c_str()];
return CCString::createWithFormat(imp->m_movie->m_movie->m_name.c_str());
}
void CCSWFNode::setMovieName(char *movieName)
{
imp->m_movie->m_movie->m_name = movieName;// UTF8String];
}
//+(id) nodeWithSWFFile:(NSString*)file
//{
// return [[[CCSWFNode alloc] initWithSWFFile:file] autorelease];
//}
CCSWFNode* CCSWFNode::create(const char* file)
{
CCSWFNode* swf = new CCSWFNode();
if (! swf->initWithSWFFile(file)) {
swf->release();
return NULL;
}
return swf;
}
bool CCSWFNode::initWithSWFFile(const char* file)
{
// self = [super init];
// if (self)
// {
// imp = [[CCSWFNode_imp alloc] initWithSWFFile:file];
imp = new CCSWFNode_imp();
if (!imp->initWithSWFFile(file))
{
// [self release];
imp->release();
return false;
}
m_movieWidth = imp->m_movie->m_def->m_frame_size.m_x_max - imp->m_movie->m_def->m_frame_size.m_x_min;
m_movieHeight = imp->m_movie->m_def->m_frame_size.m_y_max - imp->m_movie->m_def->m_frame_size.m_y_min;
m_localScaleX = (imp->m_movie->get_movie_width() / m_movieWidth);
m_localScaleY = -(imp->m_movie->get_movie_height() / m_movieHeight);
m_scaleX = 1.0;
m_scaleY = 1.0;
m_touchEvents = new CCArray();//[[NSMutableArray alloc] init];
this->setContentSize(CCSize(m_movieWidth, m_movieHeight));
this->setScale(1.0f);
this->setAnchorPoint(ccp(0.5,0.5));
this->isGrouped = false;
this->isRuning = false;
this->m_listener = NULL;
this->m_endListener = NULL;
this->repeat = false;
this->iFrameCount = imp->m_movie->get_frame_count();
// [self setContentSize:CGSizeMake(m_movieWidth, m_movieHeight)];
// [self setScale:1.0];
// [self setAnchorPoint:ccp(0.5f, 0.5f)];
// }
return true;
}
void CCSWFNode::setFlipX(bool flipX)
{
// [super setFlipX:flipX];
if (flipX) {
// m_localScaleX *= -1;
// m_scaleX = m_scaleX*-1;
// [super setScaleX:m_localScaleX*-1];
setScaleX(m_localScaleX * -1);
}
}
void CCSWFNode::setFlipY(bool flipY)
{
if (flipY) {
setScaleY(m_localScaleY * -1);
}
}
float CCSWFNode::scale()
{
CCAssert(m_scaleX == m_scaleY, "CCNode#scale. ScaleX != ScaleY. Don't know which one to return");
return m_scaleX;
}
void CCSWFNode::setScale(float scale)
{
m_scaleX = m_scaleY = scale;
// [super setScaleX:m_localScaleX * m_scaleX];
// [super setScaleY:m_localScaleY * m_scaleY];
setScaleX(m_localScaleX*m_scaleX);
setScaleY(m_localScaleY*m_scaleY);
}
float CCSWFNode::scaleX()
{
return m_scaleX;
}
void CCSWFNode::setScaleX(float scaleX)
{
m_scaleX = scaleX;
// [super setScaleX:m_localScaleX * m_scaleX];
// setScaleX(m_localScaleX*m_scaleX);
cocos2d::CCSprite::setScaleX(m_scaleX);
}
float CCSWFNode::scaleY()
{
return m_scaleY;
}
void CCSWFNode::setScaleY(float scaleY)
{
m_scaleY = scaleY;
// [super setScaleY:m_localScaleY];
// setScaleY(m_localScaleY);
CCSprite::setScaleY(m_scaleY);
}
//void CCSWFNode::dealloc()
//{
//// m_touchEvents->release();
//// [m_touchEvents release];
//// [super dealloc];
// this->stopAction();
//// this->imp->dealloc();
// delete imp;
//}
void CCSWFNode::onEnterTransitionDidFinish()
{
// [self scheduleUpdate];
// if(isRuning)
// return;
//
// this->isRuning = true;
//// this->imp->m_movie->goto_frame(48);
//// this->imp->m_movie->set_play_state((gameswf::character::play_state)1);
//// this->imp->m_movie->set_play_run();
// schedule(schedule_selector(CCSWFNode::update),1/2);
// schedule(schedule_selector(CCSWFNode::update));
// [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:YES];
}
void CCSWFNode::onExit()
{
// [self unscheduleAllSelectors];
if(! isRuning)
return;
unschedule(schedule_selector(CCSWFNode::update));
this->isRuning = false;
// [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
}
void CCSWFNode::runFlash()
{
if(isRuning)
return;
//if in singleton group we stop other sibling.
if (isGrouped) {
CCSWFNodeGroup* g = (CCSWFNodeGroup*)this->getParent();
if (g->getSingleton()) {
g->stopAction();
}
}
this->isRuning = true;
//reset
this->imp->m_movie->goto_frame(0);
this->imp->m_movie->set_play_run();
schedule(schedule_selector(CCSWFNode::update),1/2);
}
bool CCSWFNode::gotoLabel(char* label)
{
bool check = this->imp->m_movie->goto_labeled_frame(label);
if(check == true)
{
this->imp->m_movie->set_play_run();
}
else
{
CCLOG("没有那一帧");
}
return check;
}
bool CCSWFNode::gotoFrame(int frame)
{
this->imp->m_movie->goto_frame(frame);
this->imp->m_movie->set_play_run();
return true;
}
void CCSWFNode::stopFlash()
{
if(! isRuning)
return;
unschedule(schedule_selector(CCSWFNode::update));
this->isRuning = false;
}
void CCSWFNode::addFrameListener(int iFrame, CCObject* target,SEL_MenuHandler selector)
{
CCAssert(target != NULL, "swf action should not be NULL");
if(iFrame == -1){
m_endListener = target;
m_pfnEndSelector = selector;
}else if(iFrame > 0){
m_listener = target;
m_pfnSelector = selector;
iListenFrame = iFrame;
}
}
void CCSWFNode::setNextAction(CCSWFNode* target)
{
CCAssert(target != NULL, "next swf action should not be NULL");
m_endListener = target;
//m_pfnEndSelector = cocos2d::SEL_MenuHandler(CCSWFNode::runAction);
}
void CCSWFNode::setRepeat(bool flag)
{
this->repeat = flag;
}
bool CCSWFNode::getRepeat()
{
return repeat;
}
void CCSWFNode::registerEndHandler(int nHandler)
{
unregisterEndHandler();
m_nScriptHandler = nHandler;
LUALOG("[LUA] Add CCNode event handler: %d", m_nScriptHandler);
}
void CCSWFNode::unregisterEndHandler(void)
{
if (m_nScriptHandler)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(m_nScriptHandler);
LUALOG("[LUA] Remove CCNode event handler: %d", m_nScriptHandler);
m_nScriptHandler = 0;
}
}
//-(CGPoint) getTouchInMovieCoordinates:(UITouch*)touch
//{
// // find the movie rect in pixels //
// CGRect movieRect = CGRectMake(
// self.position.x - (self.contentSize.width * super.scaleX * self.anchorPoint.x),
// self.position.y - (self.contentSize.height * -super.scaleY * self.anchorPoint.y),
// self.contentSize.width * super.scaleX,
// self.contentSize.height * -super.scaleY
// );
//
// // find the touch position in pixels //
// CGPoint touchPoint = ccpMult([[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]], CC_CONTENT_SCALE_FACTOR());
//
// // find the touch position in respect to the movie //
// CGPoint touchInMovie = ccp(
// (touchPoint.x - movieRect.origin.x) / m_scaleX,
// ((self.contentSize.height * -super.scaleY) - (touchPoint.y - movieRect.origin.y)) / m_scaleY
// );
//
// return touchInMovie;
//}
//- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
//{
// // make rect for movie //
// CGRect movieRect = CGRectMake(0, 0, (self.contentSize.width * super.scaleX), (self.contentSize.height * -super.scaleY));
// // find the touch position in pixels //
// CGPoint touchPoint = [self getTouchInMovieCoordinates:touch];
// BOOL isInMovie = CGRectContainsPoint(movieRect, touchPoint);
// [m_touchEvents addObject:[CCSWFNode_touchContainer touchContainerWithPosition:touchPoint andState:0]];
// [m_touchEvents addObject:[CCSWFNode_touchContainer touchContainerWithPosition:touchPoint andState:1]];
// return isInMovie;
//}
//
//- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
//{
// [m_touchEvents addObject:[CCSWFNode_touchContainer touchContainerWithPosition:[self getTouchInMovieCoordinates:touch] andState:1]];
//}
//
//- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
//{
// [m_touchEvents addObject:[CCSWFNode_touchContainer touchContainerWithPosition:[self getTouchInMovieCoordinates:touch] andState:0]];
//}
//
//- (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
//{
// [m_touchEvents addObject:[CCSWFNode_touchContainer touchContainerWithPosition:[self getTouchInMovieCoordinates:touch] andState:0]];
//}
void CCSWFNode::update(float dt)
{
// if (m_touchEvents.count)
// {
// CCSWFNode_touchContainer *touch = [m_touchEvents objectAtIndex:0];
// imp->m_movie->notify_mouse_state(touch.position.x, touch.position.y, touch.state);
// [m_touchEvents removeObjectAtIndex:0];
// }
bool tobestop = false;
int iFrame = imp->m_movie->get_current_frame();
if (m_listener && iFrame == iListenFrame) {
(m_listener->*m_pfnSelector)(this);
}
if(! repeat){
if (iFrame == iFrameCount - 1) {
tobestop = true;
}
}
imp->m_movie->advance(dt);
if(tobestop){
this->stopFlash();
if(m_endListener){
(this->m_endListener->*m_pfnEndSelector)(this);
}
if (m_nScriptHandler != 0) {
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(m_nScriptHandler, "battle");
}
}
// CCLOG(".....");
// TODO: Enable sound //
// sound->advance(dt);
}
#define STRINGIFY(A) #A
const char* ColorVertexShader1 = STRINGIFY(
attribute vec4 a_position;
uniform mat4 Projection;
uniform mat4 u_MVPMatrix;
void main(void)
{
vec4 tmp;
tmp = Projection * u_MVPMatrix * a_position;
// tmp = u_MVPMatrix * a_position;
// tmp = a_position;
// tmp.x *= -1.0;
// tmp.y *= -1.0;
gl_Position = tmp;
}
);
const char* ColorFragmentShader1 = STRINGIFY(
uniform lowp vec4 u_color;
void main(void)
{
gl_FragColor = u_color;
}
);
unsigned int m_currentProgram = 0;
void ApplyOrtho(float maxX, float maxY)
{
// maxX = 1024*20/2;
// maxY = 768*20/2;
float a = 1.0f / maxX;
float b = 1.0f / maxY;
float ortho[16] = {
a, 0, 0, 0,
0, b, 0, 0,
0, 0, -1, 0,
0, 0, 0, 1
};
GLint projectionUniform = glGetUniformLocation(m_currentProgram, "Projection");
glUniformMatrix4fv(projectionUniform, 1, 0, &ortho[0]);
CHECK_GL_ERROR_DEBUG();
}
void ApplyMatrix()
{
float mat[16]={
// 1.0f/512, 0, 0, 0,
// 0, 1.0f/384, 0, 0,
// 0,0,-0.000976562, 0,
// -0.273438,-0.234375,0,1,
1/512,0,0,0,
0,-1/384,0,0,
0,0,-1/1024,0,
// 0,0,0,1,
-372,-474,0,1,
};
GLint modelviewUniform = glGetUniformLocation(m_currentProgram, "u_MVPMatrix");
glUniformMatrix4fv(modelviewUniform, 1, 0, mat);
CHECK_GL_ERROR_DEBUG();
}
static void ApplyColor()
{
GLfloat color[4];
{
color[0] = 0.5;
color[1] = 0.5;
color[2] = 1.0;
color[3] = 1.0;
}
GLint lineColorSlot = glGetUniformLocation(m_currentProgram, "u_color");
glUniform4fv(lineColorSlot, 1, color);
CHECK_GL_ERROR_DEBUG();
}
GLuint BuildShader(const char* source, GLenum shaderType)
{
GLuint shaderHandle = glCreateShader(shaderType);
glShaderSource(shaderHandle, 1, &source, 0);
glCompileShader(shaderHandle);
GLint compileSuccess;
glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &compileSuccess);
if (compileSuccess == GL_FALSE) {
GLchar messages[256];
glGetShaderInfoLog(shaderHandle, sizeof(messages), 0, &messages[0]);
printf("%s", messages);
exit(1);
}
return shaderHandle;
}
GLuint BuildProgram(const char* vertexShaderSource,
const char* fragmentShaderSource)
{
GLuint vertexShader = BuildShader(vertexShaderSource, GL_VERTEX_SHADER);
GLuint fragmentShader = BuildShader(fragmentShaderSource, GL_FRAGMENT_SHADER);
GLuint programHandle = glCreateProgram();
glAttachShader(programHandle, vertexShader);
glAttachShader(programHandle, fragmentShader);
glLinkProgram(programHandle);
GLint linkSuccess;
glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess);
if (linkSuccess == GL_FALSE) {
GLchar messages[256];
glGetProgramInfoLog(programHandle, sizeof(messages), 0, &messages[0]);
printf("%s", messages);
exit(1);
}
return programHandle;
}
void render()
{
m_currentProgram = BuildProgram(ColorVertexShader1, ColorFragmentShader1);
CHECK_GL_ERROR_DEBUG();
kmMat4 matrixP;
kmMat4 matrixMV;
kmMat4 matrixMVP;
kmGLGetMatrix(KM_GL_PROJECTION, &matrixP );
kmGLGetMatrix(KM_GL_MODELVIEW, &matrixMV );
kmMat4Multiply(&matrixMVP, &matrixP, &matrixMV);
#if CC_ENABLE_GL_STATE_CACHE
ccGLUseProgram(0);
#endif
ApplyColor();
// ApplyOrtho(512, 384);
// ApplyMatrix();
GLint projectionUniform = glGetUniformLocation(m_currentProgram, "Projection");
glUniformMatrix4fv(projectionUniform, 1, 0, &matrixP.mat[0]);
CHECK_GL_ERROR_DEBUG();
GLint modelviewUniform = glGetUniformLocation(m_currentProgram, "u_MVPMatrix");
glUniformMatrix4fv(modelviewUniform, 1, 0, &matrixMV.mat[0]);
CHECK_GL_ERROR_DEBUG();
GLuint positionSlot = glGetAttribLocation(m_currentProgram, "a_position");
glEnableVertexAttribArray(positionSlot);
float fcoords[8] ={
80,-60,
5680,-60,
80,3540,
5680,3540,
};
glVertexAttribPointer(positionSlot,2, GL_FLOAT,GL_FALSE, 0, fcoords);
CHECK_GL_ERROR_DEBUG();
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
CHECK_GL_ERROR_DEBUG();
}
void CCSWFNode::draw()
{
if (! isRuning) {
return;
}
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
kmMat4 matrixP;
kmMat4 matrixMV;
kmMat4 matrixMVP;
kmGLGetMatrix(KM_GL_PROJECTION, &matrixP );
kmGLGetMatrix(KM_GL_MODELVIEW, &matrixMV );
kmMat4Multiply(&matrixMVP, &matrixP, &matrixMV);
#if CC_ENABLE_GL_STATE_CACHE
ccGLUseProgram(0);//valid program is NON_ZERO unsigned int
#endif
imp->m_movie->display(&matrixMVP.mat[0]);
CHECK_GL_ERROR_DEBUG();
// render();
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
}
//@end