This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtestscene.cpp
623 lines (568 loc) · 14.3 KB
/
testscene.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
#include "testscene.h"
#include <cstdio>
#include <cstring>
#define CN 0
#define EN 1
const char *str[] = {"图片读取渲染",
"3D模型读取渲染",
"物理引擎调用",
"字体渲染",
"音频播放",
"动画演示",
"着色器渲染",
"输入模块示例",
"时间模块示例",
"返回",
"测试",
"En",
"调试绘图: 关",
"调试绘图: 开",
"播放",
"停止",
"循环: 关",
"循环: 开",
"按一下空格试试"
};
const char *strEn[] = {"Texture",
"3DModel",
"Physics",
"Font",
"Audio",
"Anime",
"Shader",
"Input",
"Time",
"Back",
"Demo",
"中文",
"DebugDraw: Off",
"DebugDraw: On",
"Play",
"Stop",
"Repeat: Off",
"Repeat: On",
"Press Space"
};
const char **useStr[] = {str, strEn};
int language = CN;
//class SButton
SButton::SButton()
{
ftUI::Button();
setForeColor(FT_White);
setBackColor(FT_Black);
}
void SButton::update()
{
ftUI::Button::update();
int st = getState();
if (st == FT_isOn) {
setForeColor(FT_White);
setBackColor(ftColor("#0099CC"));
} else if (st == FT_isDown || st == FT_ButtonDown || st == FT_ButtonUp) {
setForeColor(FT_Yellow);
setBackColor(ftColor("#0099CC"));
} else {
setForeColor(FT_White);
setBackColor(FT_Black);
}
}
void SButton::setId(int idd)
{
id = idd;
setCaption(useStr[language][id]);
}
//class HWButton
HWButton::HWButton()
{
SButton();
}
void updateCaption(HWButton & but)
{
but.setId(but.id);
}
void HWButton::click()
{
FT_OUT("Button %d click!\n", id);
switch (id) {
case 0:
fountain::sceneSelector.gotoScene(new TextureScene());
break;
case 1:
fountain::sceneSelector.gotoScene(new ModelScene());
break;
case 2:
fountain::sceneSelector.gotoScene(new PhysicsScene());
break;
case 3:
fountain::sceneSelector.gotoScene(new TypeScene());
break;
case 4:
fountain::sceneSelector.gotoScene(new AudioScene());
break;
case 5:
fountain::sceneSelector.gotoScene(new AnimeScene());
break;
case 6:
fountain::sceneSelector.gotoScene(new ShaderScene());
break;
case 7:
fountain::sceneSelector.gotoScene(new InputScene());
break;
case 8:
fountain::sceneSelector.gotoScene(new TimeScene());
break;
case 9:
fountain::sceneSelector.gotoScene(new HelloWorld());
break;
/*
case 10:
fountain::sceneSelector.gotoScene(new FragmentScene());
break;
*/
case 11:
if (language == CN) {
language = EN;
}
else {
language = CN;
}
break;
};
}
//class MyShaderProgram
void MyShaderProgram::update()
{
ftVec2 mp = fountain::sysMouse.getPos();
this->setUniform("time", fountain::mainClock.getTotalT());
this->setUniform("resolution", fountain::getWinSize());
this->setUniform("mouse", mp);
}
//class HelloWorld
void HelloWorld::init()
{
HWButton t;
for (int i = 0; i < 9; i++) {
t.setPosition(ftVec2(0, 240 - i * 60));
t.setRectSize(ftVec2(300, 50));
t.setId(i);
butCon.add(t);
}
/*
t.setPosition(ftVec2(280, 180));
t.setRectSize(ftVec2(200, 200));
t.setId(10);
butCon.add(t);
*/
t.setPosition(ftVec2(-320, -220));
t.setRectSize(ftVec2(100, 100));
t.setId(11);
butCon.add(t);
mainCamera.setViewport(fountain::getWinRect());
}
void HelloWorld::update()
{
butCon.update();
butCon.doWith(updateCaption);
}
void HelloWorld::draw()
{
mainCamera.update();
butCon.draw();
}
//class TestScene
void TestScene::init()
{
button.setPosition(ftVec2(320, 260));
button.setRectSize(ftVec2(130, 50));
button.setId(9);
mainCamera.setViewport(fountain::getWinRect());
customInit();
}
void TestScene::update()
{
button.update();
customUpdate();
}
void TestScene::draw()
{
mainCamera.update();
button.draw();
customDraw();
}
void TestScene::customInit() {}
void TestScene::customUpdate() {}
void TestScene::customDraw() {}
//class TextureScene
void TextureScene::customInit()
{
picID = ftRender::getPicture("resources/image/logo.png");
}
void TextureScene::customUpdate()
{
}
void TextureScene::customDraw()
{
ftRender::useColor(FT_White);
ftRender::ftScale(0.5f);
ftRender::drawAlphaPic(picID);
}
//class ModelScene
void ModelScene::customInit()
{
modelCamera = ftRender::Camera(0, 0, 1000);
modelCamera.setProjectionType(FT_PERSPECTIVE);
x.loadObj("resources/model/teapot.obj", true);
lightSP.load("resources/shader/vs.vert", "resources/shader/model.frag");
lightSP.init();
rx = ry = 0;
}
void ModelScene::customUpdate()
{
if (fountain::sysMouse.getState(FT_LButton) == FT_isDown) {
ftVec2 dv = fountain::sysMouse.getDeltaV();
ry += dv.x;
rx -= dv.y;
}
}
void ModelScene::customDraw()
{
modelCamera.update();
lightSP.use();
ftRender::useColor(FT_White);
ftRender::transformBegin();
ftRender::ftScale(220.0f);
ftRender::ftRotate(rx, ry, 0);
x.render();
ftRender::transformEnd();
ftRender::useBasicShader();
}
//class PhysicsScene
void PhysicsScene::customInit()
{
ftPhysics::setRatio(100.0f);
ground = ftPhysics::Body(0, -100, FT_Static);
ground.setShape(ftShape(ftRect(0, 0, 500, 20)));
world.addBody(&ground);
ball = ftPhysics::Body(-5, 200, FT_Dynamic);
ball.setShape(ftShape(15));
world.addBody(&ball);
for (int i = 0; i < 25; i++) {
card[i] = ftPhysics::Body(-240 + i * 20, -75, FT_Dynamic);
card[i].setShape(ftShape(ftRect(0, 0, 3, 30)));
world.addBody(&card[i]);
}
debugDraw.setRectSize(ftVec2(300, 80));
debugDraw.setPosition(0, -180);
debugDraw.setCaption(useStr[language][12]);
ddFlag = false;
world.setDebugDraw(ddFlag);
tmpn = 0;
}
void PhysicsScene::customUpdate()
{
if (fountain::sysMouse.getState(FT_LButton) == FT_ButtonDown) {
ftVec2 pos = mainCamera.mouseToWorld(fountain::sysMouse.getPos());
tmp[tmpn] = ftPhysics::Body(pos.x, pos.y, FT_Dynamic);
tmp[tmpn].setShape(ftShape(15));
world.addBody(&tmp[tmpn]);
if (tmpn < 100) tmpn++;
}
debugDraw.update();
if (debugDraw.getState() == FT_ButtonDown) {
ddFlag = !ddFlag;
if (ddFlag) {
world.setDebugDraw(true);
debugDraw.setCaption(useStr[language][13]);
} else {
world.setDebugDraw(false);
debugDraw.setCaption(useStr[language][12]);
}
}
world.update(mainClock.getDeltaT());
}
void PhysicsScene::customDraw()
{
world.draw();
debugDraw.draw();
}
//class TypeScene
//TODO: simplize drawString with different font size
void TypeScene::customInit()
{
fontMan2.loadFont("resources/font/test.ttc");
fontMan2.genStringTable("HWerlod! ", 32);
fontMan.loadFont("resources/font/test.ttc");
fontMan.genStringTable("HWerlod! ", 16);
fontMan1.loadFont("resources/font/test.ttc");
fontMan1.genStringTable("HWerlod! ", 8);
}
void TypeScene::customUpdate()
{
}
void TypeScene::customDraw()
{
ftRender::useColor(FT_White);
ftRender::transformBegin();
ftRender::ftTranslate(-100, 0);
fontMan.drawString("Hello World!");
ftRender::transformEnd();
ftRender::transformBegin();
ftRender::ftTranslate(-100, -100);
fontMan1.drawString("Hello World!");
ftRender::transformEnd();
ftRender::transformBegin();
ftRender::ftTranslate(-100, 100);
fontMan2.drawString("Hello World!");
ftRender::transformEnd();
}
//class AudioScene
void AudioScene::customInit()
{
play.setRectSize(ftVec2(100, 100));
play.setPosition(-70, 70);
play.setCaption(useStr[language][14]);
buttonCon.add(&play);
stop.setRectSize(ftVec2(100, 100));
stop.setPosition(70, 70);
stop.setCaption(useStr[language][15]);
buttonCon.add(&stop);
loop.setRectSize(ftVec2(240, 100));
loop.setPosition(0, -60);
loop.setCaption(useStr[language][17]);
buttonCon.add(&loop);
loopFlag = true;
ch.init();
ch.load("resources/sound/test.wav");
ch.setLoop(loopFlag);
}
void AudioScene::customUpdate()
{
buttonCon.update();
if (play.getState() == FT_ButtonDown) {
ch.play();
}
if (stop.getState() == FT_ButtonDown) {
ch.stop();
}
if (loop.getState() == FT_ButtonDown) {
loopFlag = !loopFlag;
ch.setLoop(loopFlag);
if (loopFlag) {
loop.setCaption(useStr[language][17]);
}
else {
loop.setCaption(useStr[language][16]);
}
}
}
void AudioScene::customDraw()
{
buttonCon.draw();
}
//class AnimeScene
void AnimeScene::customInit()
{
animeTest = ftRender::SubImagePool("resources/image/PlaceWeapon.png", "resources/image/PlaceWeapon.sip");
anime.init(animeTest, 15.0f);
anime.setLoop(true);
anime.setMasterClock(&mainClock);
anime.play();
}
void AnimeScene::customUpdate()
{
anime.update();
}
void AnimeScene::customDraw()
{
ftRender::useColor(FT_White);
ftRender::transformBegin();
ftRender::ftScale(2.0f);
anime.draw();
ftRender::transformEnd();
}
//class ShaderScene
void ShaderScene::customInit()
{
const char *titleStr[][7] = {
{"效果1", "效果2", "效果3", "高斯模糊", "法线贴图", "曼德博集", "立方贴图"},
{"star", "wave", "smoke", "blur", "normal", "mandelbrot", "cubemap"}
};
teaPot.loadObj("resources/model/teapot.obj", true);
rx = ry = 0;
scale = 150.0f;
observePos = ftVec2(0.0, 0.0);
shaderNumber = 7;
sp[0].load("resources/shader/vs.vert", "resources/shader/magicStar.frag");
sp[1].load("resources/shader/vs.vert", "resources/shader/wave.frag");
sp[2].load("resources/shader/vs.vert", "resources/shader/purple.frag");
sp[3].load("resources/shader/vs.vert", "resources/shader/blur.frag");
sp[4].load("resources/shader/vs.vert", "resources/shader/normalmap.frag");
sp[5].load("resources/shader/vs.vert", "resources/shader/mandelbrot.frag");
sp[6].load("resources/shader/vs.vert", "resources/shader/cubemap.frag");
for (int i = 0; i < shaderNumber; i++) sp[i].init();
use = 0;
b[0].setPosition(-300, -210);
b[1].setPosition(-100, -210);
b[2].setPosition(100, -210);
b[3].setPosition(300, -210);
b[4].setPosition(-300, -270);
b[5].setPosition(-100, -270);
b[6].setPosition(100, -270);
for (int i = 0; i < shaderNumber; i++) {
b[i].setRectSize(ftVec2(190, 50));
b[i].setCaption(titleStr[language][i]);
}
}
void ShaderScene::customUpdate()
{
if (use == 5) {
if (fountain::sysMouse.getState(FT_ScrollUp)) scale *= 0.8;
if (fountain::sysMouse.getState(FT_ScrollDown)) scale *= 1.2;
if (fountain::sysMouse.getState(FT_RButton) == FT_isDown) {
ftVec2 v = fountain::sysMouse.getDeltaV();
v = v / scale;
observePos += v;
}
}
if (use == 6) {
if (fountain::sysMouse.getState(FT_LButton) == FT_isDown) {
ftVec2 dv = fountain::sysMouse.getDeltaV();
ry += dv.x;
rx -= dv.y;
}
}
for (int i = 0; i < shaderNumber; i++) {
b[i].update();
if (b[i].getState() == FT_ButtonUp) {
use = i;
}
}
}
void ShaderScene::customDraw()
{
if (use == 3) {
ftRender::useColor(FT_White);
ftRender::transformBegin();
ftRender::ftTranslate(-154, 0);
ftRender::ftScale(0.6f);
ftRender::drawAlphaPic(ftRender::getPicture("resources/image/logo.png"));
ftRender::transformEnd();
}
sp[use].use();
ftRender::useColor(FT_White);
if (use == 3) {
ftRender::useColor(FT_White);
ftRender::transformBegin();
ftRender::ftTranslate(154, 0);
ftRender::ftScale(0.6f);
ftRender::drawAlphaPic(ftRender::getPicture("resources/image/logo.png"));
ftRender::transformEnd();
} else if (use == 4) {
sp[4].setTexture("nmTex", ftRender::getPicture("resources/image/normalmap.png"), 1);
ftRender::useColor(FT_White);
ftRender::transformBegin();
ftRender::drawAlphaPic(ftRender::getPicture("resources/image/normalmap.png"));
ftRender::transformEnd();
} else if (use == 5) {
sp[5].setUniform("scale", scale);
sp[5].setUniform("observePos", observePos);
ftRender::drawQuad(800, 400);
} else if (use == 6) {
ftRender::Camera *camera = ftRender::getCurrentCamera();
camera->setProjectionType(FT_PERSPECTIVE);
camera->update();
sp[6].setTexture("cubeTex", ftRender::getPicture("resources/image/cube.png", true));
ftRender::transformBegin();
ftRender::ftScale(220);
ftRender::ftRotate(rx, ry, 0);
teaPot.render();
ftRender::transformEnd();
camera->setProjectionType(FT_PLANE);
camera->update();
} else {
ftRender::drawQuad(800, 400);
}
ftRender::useBasicShader();
for (int i = 0; i < shaderNumber; i++) b[i].draw();
}
void ShaderScene::destroy()
{
ftRender::useBasicShader();
}
//class InputScene
void InputScene::customInit()
{
info.setCaption(useStr[language][18]);
info.setForeColor(FT_White);
}
void InputScene::customUpdate()
{
if (fountain::sysKeyboard.getState(FT_Space)) {
ftRender::setClearColor(ftColor("#333"));
} else
ftRender::setClearColor(ftColor("#131313"));
}
void InputScene::customDraw()
{
info.draw();
}
void InputScene::destroy()
{
ftRender::setClearColor(ftColor("#131313"));
}
//class TimeScene
void TimeScene::customInit()
{
float t = ftTime::getFps();
float ms = ftTime::getMsPerFrame();
std::sprintf(s, "fps: %.2f", t);
std::sprintf(mss, "mspf: %.2f", ms);
fps.setCaption(s);
mspf.setCaption(mss);
fps.setForeColor(FT_White);
mspf.setForeColor(FT_White);
mspf.move(0, -50);
}
void TimeScene::customUpdate()
{
float t = ftTime::getFps();
float ms = ftTime::getMsPerFrame();
std::sprintf(s, "fps: %.3f", t);
std::sprintf(mss, "mspf: %.3f", ms);
fps.setCaption(s);
mspf.setCaption(mss);
}
void TimeScene::customDraw()
{
fps.draw();
mspf.draw();
}
/*
//class FragmentScene
void FragmentScene::customInit()
{
nt.init(&mainClock);
}
void FragmentScene::customUpdate()
{
if (fountain::sysKeyboard.getState(FT_Z)) nt.attack();
ftVec2 v(0.0, 0.0);
if (fountain::sysKeyboard.getState(FT_Up)) v += ftVec2(0.0, 200.0);
if (fountain::sysKeyboard.getState(FT_Down)) v += ftVec2(0.0, -200.0);
if (fountain::sysKeyboard.getState(FT_Left)) v += ftVec2(-200.0, 0.0);
if (fountain::sysKeyboard.getState(FT_Right)) v += ftVec2(200.0, 0.0);
nt.setSpeed(v);
nt.update();
STG::Bullet tmpb;
tmpb.setColor(ftColor(std::cos(mainClock.getTotalT()), std::sin(mainClock.getTotalT()), 0.3, 1.0));
tmpb.setSpeed(ftVec2(std::cos(mainClock.getTotalT() * 10.0), std::sin(mainClock.getTotalT() * 10.0)) * 200.0);
enemyBulletCon.add(tmpb);
enemyBulletCon.update();
}
void FragmentScene::customDraw()
{
nt.draw();
enemyBulletCon.draw();
}
*/