Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/cocos2d_hello.cpp
4128 views
1
// Copyright 2017 The Emscripten Authors. All rights reserved.
2
// Emscripten is available under two separate licenses, the MIT license and the
3
// University of Illinois/NCSA Open Source License. Both these licenses can be
4
// found in the LICENSE file.
5
6
#include <stdlib.h>
7
#include <stdio.h>
8
#include <unistd.h>
9
#include <string>
10
11
#include "cocos2d.h"
12
13
#include <emscripten.h>
14
15
// =============
16
// AppMacros.h
17
// =============
18
19
/* For demonstrating using one design resolution to match different resources,
20
or one resource to match different design resolutions.
21
22
[Situation 1] Using one design resolution to match different resources.
23
Please look into Appdelegate::applicationDidFinishLaunching.
24
We check current device frame size to decide which resource need to be selected.
25
So if you want to test this situation which said in title '[Situation 1]',
26
you should change ios simulator to different device(e.g. iphone, iphone-retina3.5, iphone-retina4.0, ipad, ipad-retina),
27
or change the window size in "proj.XXX/main.cpp" by "CCEGLView::setFrameSize" if you are using win32 or linux plaform
28
and modify "proj.mac/AppController.mm" by changing the window rectangle.
29
30
[Situation 2] Using one resource to match different design resolutions.
31
The coordinates in your codes is based on your current design resolution rather than resource size.
32
Therefore, your design resolution could be very large and your resource size could be small.
33
To test this, just define the marco 'TARGET_DESIGN_RESOLUTION_SIZE' to 'DESIGN_RESOLUTION_2048X1536'
34
and open iphone simulator or create a window of 480x320 size.
35
36
[Note] Normally, developer just need to define one design resolution(e.g. 960x640) with one or more resources.
37
*/
38
39
#define DESIGN_RESOLUTION_480X320 0
40
#define DESIGN_RESOLUTION_1024X768 1
41
#define DESIGN_RESOLUTION_2048X1536 2
42
43
/* If you want to switch design resolution, change next line */
44
#define TARGET_DESIGN_RESOLUTION_SIZE DESIGN_RESOLUTION_480X320
45
46
typedef struct tagResource
47
{
48
cocos2d::Size size;
49
char directory[100];
50
}Resource;
51
52
static Resource smallResource = { cocos2d::Size(480, 320), "iphone" };
53
static Resource mediumResource = { cocos2d::Size(1024, 768), "ipad" };
54
static Resource largeResource = { cocos2d::Size(2048, 1536), "ipadhd" };
55
56
#if (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_480X320)
57
static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320);
58
#elif (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_1024X768)
59
static cocos2d::Size designResolutionSize = cocos2d::Size(1024, 768);
60
#elif (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_2048X1536)
61
static cocos2d::Size designResolutionSize = cocos2d::Size(2048, 1536);
62
#else
63
#error unknown target design resolution!
64
#endif
65
66
// The font size 24 is designed for small resolution, so we should change it to fit for current design resolution
67
#define TITLE_FONT_SIZE (cocos2d::EGLView::getInstance()->getDesignResolutionSize().width / smallResource.size.width * 24)
68
69
// ===============
70
// AppDelegate.h
71
// ===============
72
73
/**
74
@brief The cocos2d Application.
75
76
The reason for implement as private inheritance is to hide some interface call by Director.
77
*/
78
class AppDelegate : private cocos2d::Application
79
{
80
public:
81
AppDelegate();
82
virtual ~AppDelegate();
83
84
/**
85
@brief Implement Director and Scene init code here.
86
@return true Initialize success, app continue.
87
@return false Initialize failed, app terminate.
88
*/
89
virtual bool applicationDidFinishLaunching();
90
91
/**
92
@brief The function be called when the application enter background
93
@param the pointer of the application
94
*/
95
virtual void applicationDidEnterBackground();
96
97
/**
98
@brief The function be called when the application enter foreground
99
@param the pointer of the application
100
*/
101
virtual void applicationWillEnterForeground();
102
};
103
104
// ===================
105
// HelloWorldScene.h
106
// ===================
107
108
class HelloWorld : public cocos2d::Layer
109
{
110
public:
111
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
112
virtual bool init();
113
114
// there's no 'id' in cpp, so we recommend returning the class instance pointer
115
static cocos2d::Scene* scene();
116
117
// a selector callback
118
void menuCloseCallback(Object* sender);
119
120
// implement the "static node()" method manually
121
CREATE_FUNC(HelloWorld);
122
};
123
124
USING_NS_CC;
125
126
// =======================
127
// HelloWorldScene.cpp
128
// =======================
129
130
Scene* HelloWorld::scene()
131
{
132
// 'scene' is an autorelease object
133
Scene *scene = Scene::create();
134
135
// 'layer' is an autorelease object
136
HelloWorld *layer = HelloWorld::create();
137
138
// add layer as a child to scene
139
scene->addChild(layer);
140
141
// return the scene
142
return scene;
143
}
144
145
// on "init" you need to initialize your instance
146
bool HelloWorld::init()
147
{
148
//////////////////////////////
149
// 1. super init first
150
if ( !Layer::init() )
151
{
152
return false;
153
}
154
155
Size visibleSize = Director::getInstance()->getVisibleSize();
156
Point origin = Director::getInstance()->getVisibleOrigin();
157
158
/////////////////////////////
159
// 2. add a menu item with "X" image, which is clicked to quit the program
160
// you may modify it.
161
162
// add a "close" icon to exit the progress. it's an autorelease object
163
MenuItemImage *closeItem = MenuItemImage::create(
164
"CloseNormal.png",
165
"CloseSelected.png",
166
CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
167
168
closeItem->setPosition(origin + Point(visibleSize) - Point(closeItem->getContentSize() / 2));
169
170
// create menu, it's an autorelease object
171
Menu* menu = Menu::create(closeItem, nullptr);
172
menu->setPosition(Point::ZERO);
173
this->addChild(menu, 1);
174
175
/////////////////////////////
176
// 3. add your codes below...
177
178
// add a label shows "Hello World"
179
// create and initialize a label
180
181
LabelTTF* label = LabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE);
182
183
// position the label on the center of the screen
184
label->setPosition(Point(origin.x + visibleSize.width/2,
185
origin.y + visibleSize.height - label->getContentSize().height));
186
187
// add the label as a child to this layer
188
this->addChild(label, 1);
189
190
// add "HelloWorld" splash screen"
191
Sprite* sprite = Sprite::create("HelloWorld.png");
192
193
// position the sprite on the center of the screen
194
sprite->setPosition(Point(visibleSize / 2) + origin);
195
196
// add the sprite as a child to this layer
197
this->addChild(sprite, 0);
198
199
return true;
200
}
201
202
203
void HelloWorld::menuCloseCallback(Object* sender)
204
{
205
Director::getInstance()->end();
206
207
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
208
exit(0);
209
#endif
210
}
211
212
// =================
213
// AppDelegate.cpp
214
// =================
215
216
using namespace std;
217
218
AppDelegate::AppDelegate() {
219
220
}
221
222
AppDelegate::~AppDelegate()
223
{
224
}
225
226
bool AppDelegate::applicationDidFinishLaunching() {
227
// initialize director
228
Director* director = Director::getInstance();
229
EGLView* glView = EGLView::getInstance();
230
231
director->setOpenGLView(glView);
232
233
Size size = director->getWinSize();
234
235
// Set the design resolution
236
glView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
237
238
Size frameSize = glView->getFrameSize();
239
240
vector<string> searchPath;
241
242
// In this demo, we select resource according to the frame's height.
243
// If the resource size is different from design resolution size, you need to set contentScaleFactor.
244
// We use the ratio of resource's height to the height of design resolution,
245
// this can make sure that the resource's height could fit for the height of design resolution.
246
247
// if the frame's height is larger than the height of medium resource size, select large resource.
248
if (frameSize.height > mediumResource.size.height)
249
{
250
searchPath.push_back(largeResource.directory);
251
252
director->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height, largeResource.size.width/designResolutionSize.width));
253
}
254
// if the frame's height is larger than the height of small resource size, select medium resource.
255
else if (frameSize.height > smallResource.size.height)
256
{
257
searchPath.push_back(mediumResource.directory);
258
259
director->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height, mediumResource.size.width/designResolutionSize.width));
260
}
261
// if the frame's height is smaller than the height of medium resource size, select small resource.
262
else
263
{
264
searchPath.push_back(smallResource.directory);
265
266
director->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height, smallResource.size.width/designResolutionSize.width));
267
}
268
269
// set searching path
270
FileUtils::getInstance()->setSearchPaths(searchPath);
271
272
// turn on display FPS
273
director->setDisplayStats(false);
274
275
// set FPS. the default value is 1.0/60 if you don't call this
276
director->setAnimationInterval(1.0 / 60);
277
278
// create a scene. it's an autorelease object
279
Scene *scene = HelloWorld::scene();
280
281
// run
282
director->runWithScene(scene);
283
284
return true;
285
}
286
287
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
288
void AppDelegate::applicationDidEnterBackground() {
289
Director::getInstance()->stopAnimation();
290
291
// if you use SimpleAudioEngine, it must be pause
292
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
293
}
294
295
// this function will be called when the app is active again
296
void AppDelegate::applicationWillEnterForeground() {
297
Director::getInstance()->startAnimation();
298
299
// if you use SimpleAudioEngine, it must resume here
300
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
301
}
302
303
304
// ==========
305
// main.cpp
306
// ==========
307
308
int main(int argc, char **argv)
309
{
310
// create the application instance
311
AppDelegate app;
312
EM_ASM({
313
Browser.setCanvasSize(640, 480);
314
});
315
return Application::getInstance()->run();
316
}
317
318