-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·745 lines (625 loc) · 14.7 KB
/
main.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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
// UTF-8
#include "include/dxlibp.h"
#include "include/exception.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdint.h>
#include <fastmath.h>
#include <time.h>
#include <pspmath.h>
PSP_MODULE_INFO("Particle", 0, 1, 1)
;
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
/*
HSV to RGB
http://laputa.cs.shinshu-u.ac.jp/~gtakano/prog3.html
こちらのページより拝借させて頂きました。
ライセンスに当たるものが見つからなかったので無断で使用及び改変をさせて頂きました。
もしライセンスに当たる物を見つけたなど、問題があるようならご一報下さい。
*/
unsigned int HSV2RGB(float H, float S, float V)
{
int in = (int) floor(H / 60);
float fl = (H / 60) - in;
if (!(in & 1))
fl = 1 - fl; // if i is even
float m = V * (1 - S);
float n = V * (1 - S * fl);
float r, g, b;
switch (in)
{
case 0:
r = V, g = n, b = m;
break;
case 1:
r = n, g = V, b = m;
break;
case 2:
r = m, g = V, b = n;
break;
case 3:
r = m, g = n, b = V;
break;
case 4:
r = n, g = m, b = V;
break;
case 5:
r = V, g = m, b = n;
break;
default:
r = 0, g = 0, b = 0;
break;
}
return (unsigned int) (0xFF << 24 | // alfa
(unsigned char) (b * 0xFF) << 16 | // 16-23
(unsigned char) (g * 0xFF) << 8 | // 8-15
(unsigned char) (r * 0xFF)); // 0-7
}
float GetRandSafe()
{
return (GetRand(98) + 1) / 10.0f;
}
////////////////////////////////////////////
#include <vector>
#include <map>
#include <memory>
#include <algorithm>
#include <functional>
namespace Psp
{
// 突貫のアロケータ(gdgd)
template<typename _Tp>
class allocator : public std::allocator<_Tp>
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _Tp* pointer;
typedef const _Tp* const_pointer;
typedef _Tp& reference;
typedef const _Tp& const_reference;
typedef _Tp value_type;
private:
static std::map<_Tp*, SceUID> indexs;
public:
template<typename _Tp1>
struct rebind
{ typedef allocator<_Tp1> other; };
allocator(){}
allocator(const allocator& __a)
: std::allocator<_Tp>(__a){}
template<typename _Tp1>
allocator(const allocator<_Tp1>&){}
~allocator() { }
pointer
allocate(size_type __n, const void* = 0)
{
if (__builtin_expect(__n > this->max_size(), false))
std::__throw_bad_alloc();
SceUID id = sceKernelAllocPartitionMemory(2, "StlMem", 0, __n * sizeof(_Tp), NULL);
_Tp* ptr = static_cast<_Tp*>(sceKernelGetBlockHeadAddr(id));
if(!ptr || !id)
std::__throw_bad_alloc();
indexs.insert(std::pair<_Tp*, SceUID>(ptr, id));
return ptr;
}
// __p is not permitted to be a null pointer.
void
deallocate(pointer __p, size_type)
{
SceUID id = indexs[__p];
sceKernelFreePartitionMemory(id);
indexs.erase(__p);
}
size_type
max_size()
{
return sceKernelMaxFreeMemSize() / sizeof(_Tp);
}
};
template<typename _Tp>
std::map<_Tp*, SceUID> allocator<_Tp>::indexs;
}
#ifdef _PSP
using Psp::allocator;
#else
using std::allocator;
#endif
class BlockBreaker;
class Screen
{
static uint32_t m_width, m_height;
public:
Screen(void)
{
}
Screen(uint32_t width, uint32_t height)
{
m_width = width;
m_height = height;
}
public:
uint32_t GetWidth(void) const
{
return m_width;
}
uint32_t GetHeight(void) const
{
return m_height;
}
};
uint32_t Screen::m_width, Screen::m_height;
class Particle
{
friend class BlockBreaker;
friend class Blocks;
float m_x, m_y;
float m_vx, m_vy;
uint32_t m_color;
enum State
{
BLOCK, FALL, BALL, DELETED
} m_state;
public:
Particle(uint16_t x = 200, uint16_t y = 200) :
m_x(x), m_y(y), m_state(BLOCK)
{
}
};
class Bar
{
const Screen m_screen;
uint16_t m_x, m_y;
const uint16_t m_width, m_height;
const uint32_t m_color;
const uint32_t m_speed;
const uint8_t m_rate;
bool m_speedup;
public:
// メンバイニシャライザ
Bar(uint16_t width, uint16_t height, uint32_t color, uint32_t speed,
uint8_t rate = 3) :
m_x(m_screen.GetWidth() / 2), // 中央
m_y(m_screen.GetHeight() - height), // 下から少し
m_width(width), m_height(height), m_color(color), m_speed(speed),
m_rate(rate)
{
}
// もどき
bool IsHited(uint32_t x, uint32_t y)
{
// 条件式の簡略化
// 0-1=0xFF..Fで > m_widthとなる
return x - m_x < m_width && y - m_y < m_height;
}
bool SetSpeedUpFlag(bool arg)
{
return m_speedup = arg;
}
void MoveRight(void)
{
uint8_t rate = m_speedup ? m_rate : 1;
if (m_x < m_screen.GetWidth() - m_width - m_speed * rate)
m_x += m_speed * rate;
else
m_x = m_screen.GetWidth() - m_width;
}
void MoveLeft(void)
{
uint8_t rate = m_speedup ? m_rate : 1;
if (m_x >= m_speed * rate)
m_x -= m_speed * rate;
else
m_x = 0;
}
void Update(void)
{
// バーの移動
if (GetInputState() & (DXP_INPUT_LTRIGGER | DXP_INPUT_RTRIGGER))
SetSpeedUpFlag(1);
else
SetSpeedUpFlag(0);
if (GetInputState() & DXP_INPUT_RIGHT)
MoveRight();
else if (GetInputState() & DXP_INPUT_LEFT)
MoveLeft();
DrawBox(m_x, m_y, m_x + m_width, m_y + m_height, 0xFF00FF00, 1);
}
};
class Blocks
{
uint16_t m_width;
uint16_t m_height;
typedef std::vector<Particle, allocator<Particle> > vector;
vector m_blocks;
public:
Blocks(uint16_t height) :
m_width(Screen().GetWidth()), m_height(height), //
m_blocks(m_width * m_height + 1)
{
for (int i = 0; i < m_width; i++)
{
// make a color
uint32_t c = HSV2RGB((float) i * (360.0f / m_width), 1, 1);
for (int j = 0; j < m_height; j++)
{
Particle& p = m_blocks[i + j * m_width];
p.m_x = i;
p.m_y = j;
p.m_color = c;
}
}
}
public:
vector& GetBlocks()
{
return m_blocks;
}
Particle* GetParticle(uint32_t x, uint32_t y)
{
uint32_t index = x + y * m_width;
if (index >= m_blocks.size())
{
return NULL;
}
return &m_blocks[index];
}
};
class BlockBreaker
{
class Worker;
class Counter
{
friend class Worker;
BlockBreaker& m_breaker;
uint32_t m_block, m_fall, m_ball;
public:
Counter(BlockBreaker& breaker) :
m_breaker(breaker), //
m_block(m_breaker.m_blocks.GetBlocks().size()), //
m_fall(0), //
m_ball(0) //
{
}
public:
uint32_t GetBlockCount(void)
{
return this->m_block;
}
uint32_t GetFallCount(void)
{
return this->m_fall;
}
uint32_t GetBallCount(void)
{
return this->m_ball;
}
uint32_t GetRemovedBallCount(void)
{
return m_breaker.m_blocks.GetBlocks().size() - //
GetBlockCount() - //
GetFallCount() - //
GetBallCount();
}
};
const Screen m_screen;
Blocks& m_blocks;
Bar& m_bar;
Counter m_counter;
bool m_cleared;
//int m_graph_handler;
public:
BlockBreaker(Bar& bar, Blocks& blocks) :
m_blocks(blocks), m_bar(bar), m_counter(*this), m_cleared(false)
{
Worker worker(*this);
// trigger to start
Particle& particle = m_blocks.GetBlocks().back();
worker.BreakParticle(particle);
worker.MakeBall(particle);
particle.m_x = m_screen.GetWidth() / 2;
particle.m_y = m_screen.GetHeight() / 2;
//particle.m_x = 1; // さっさと「ドバーッ」モードにしたい人向け
//particle.m_y = 1;
particle.m_vx = GetRandSafe();
particle.m_vy = -GetRandSafe();
particle.m_color = 0xFFFFFFFF;
}
private:
class Worker: std::unary_function<Particle&, void>
{
BlockBreaker& m_breaker;
public:
Worker(BlockBreaker& breaker) :
m_breaker(breaker)
{
}
public:
bool RemoveParticle(Particle& particle)
{
bool i = true;
if (particle.m_state == particle.FALL)
m_breaker.m_counter.m_fall -= 1;
else if (particle.m_state == particle.BALL)
m_breaker.m_counter.m_ball -= 1;
else
i = false;
particle.m_state = particle.DELETED;
return i;
}
void BreakParticle(Particle& particle)
{
if (particle.m_state == particle.BLOCK)
m_breaker.m_counter.m_block -= 1;
particle.m_state = particle.FALL;
m_breaker.m_counter.m_fall += 1;
}
void MakeBall(Particle& particle)
{
if (particle.m_state == particle.FALL)
m_breaker.m_counter.m_fall -= 1;
particle.m_state = particle.BALL;
m_breaker.m_counter.m_ball += 1;
}
private:
void UpdateBlock(Particle& particle)
{
DrawPixel(particle.m_x, particle.m_y, particle.m_color);
}
void UpdateFallBlock(Particle& particle)
{
particle.m_vy += 0.1;
particle.m_x += particle.m_vx;
particle.m_y += particle.m_vy;
DrawPixel(particle.m_x, particle.m_y, particle.m_color);
if (m_breaker.m_bar.IsHited(particle.m_x, particle.m_y))
{
MakeBall(particle);
// newball.vx = GetRandSafe() * (particle.m_vx < 0?-1:1); //prototype
particle.m_vx = GetRandSafe(); //simple
particle.m_vy = GetRandSafe() + 1;
}
else if (particle.m_y > m_breaker.m_screen.GetHeight())
{
RemoveParticle(particle);
}
}
void UpdateBall(Particle& particle)
{
float bvx = particle.m_vx;
float bvy = particle.m_vy;
float bspeed = vfpu_powf(0.5f, bvx * bvx + bvy * bvy);
DrawPixel(particle.m_x, particle.m_y, particle.m_color);
for (int i = 0; i < bspeed; i++)
{
float r_bspeed = 1 / bspeed;
particle.m_x += particle.m_vx * r_bspeed;
particle.m_y += particle.m_vy * r_bspeed;
Particle* hitParticle = //
m_breaker.m_blocks.GetParticle( //
particle.m_x, particle.m_y);
// printfDx("ball:%p (%f,%f)\n",hitParticle,particle.m_x, particle.m_y);
if (hitParticle)
if (hitParticle->m_state == hitParticle->BLOCK)
{
float bradius = vfpu_atan2f(bvy, bvx);
// printfDx("hit:%p (%f,%f)\n", hitParticle, particle.m_x, particle.m_y);
BreakParticle(*hitParticle);
hitParticle->m_vx = //
vfpu_cosf(bradius + M_PI * 2 / //
(3 * GetRandSafe()) - 15) * //
((hitParticle->m_vx < 0) ? -3 : 3);
hitParticle->m_vy = 1;
// reflection
particle.m_vy = -particle.m_vy;
}
//この辺は純粋にノータッチ
if ((particle.m_x < 0 && particle.m_vx < 0) //
|| (particle.m_x > m_breaker.m_screen.GetWidth() //
&& particle.m_vx > 0))
{
particle.m_vx = -particle.m_vx;
}
if (particle.m_y < 0 && particle.m_vy < 0)
{
particle.m_vy = -particle.m_vy;
}
if (particle.m_y > m_breaker.m_screen.GetHeight())
{
RemoveParticle(particle);
}
if (m_breaker.m_bar.IsHited(particle.m_x, particle.m_y))
{
particle.m_vy = -fabsf(particle.m_vy);
}
// DrawPixel(particle.m_x, particle.m_y, particle.m_color);
}
}
public:
void operator()(Particle& particle)
{
switch (particle.m_state)
{
case (Particle::DELETED):
return;
case (Particle::BLOCK):
UpdateBlock(particle);
break;
case (Particle::FALL):
UpdateFallBlock(particle);
break;
case (Particle::BALL):
UpdateBall(particle);
break;
default:
break;
}
}
};
public:
void Update(void)
{
// 走査。<algorithm>愛してる。
std::for_each( //
m_blocks.GetBlocks().begin(), // 先頭
m_blocks.GetBlocks().end(), // 後尾
Worker(*this)); // ファンクタ
m_bar.Update();
if (m_counter.GetBlockCount() == 0)
{
/*
// クリアー!
DrawString(
m_screen.GetWidth() / 2 - 25, // 25は適当
m_screen.GetHeight() / 2 - 16, // FONTHEIGHT
"CLEAR!",//
DXP_COLOR_WHITE);
DrawString(
m_screen.GetWidth() / 2 - 25, // 適当
m_screen.GetHeight() / 2, // 中央
"\x82\xA8\x82\xDF\x82\xC5\x82\xC6", // おめでと
DXP_COLOR_WHITE);
*/
clsDx();
// クリアー!
for(int i = 0; i < 9; i++)
printfDx("\n");
for(int i = 0; i < 37; i++)
printfDx(" ");
printfDx("CLEAR!\n");
for(int i = 0; i < 36; i++)
printfDx(" ");
printfDx("\x82\xA8\x82\xDF\x82\xC5\x82\xC6\n");
//m_graph_handler = MakeGraph(480,272);
m_cleared = 1;
}
}
public:
// 使わなくなtt\(^o^)/
/*
// クリア前の動作は未定義
void Redraw()
{
DrawGraph(0, 0, m_graph_handler, 0);
}
*/
public:
bool IsCleared(void) const
{
return m_cleared;
}
Counter& GetCounter(void)
{
return m_counter;
}
};
class Fps
{
int m_count;
int m_time;
int m_value;
public:
Fps() :
m_count(0), m_time(GetNowCount()), m_value(0)
{
}
int Update()
{
m_count++;
// 一秒たったか
if (m_time + 1000 <= GetNowCount())
{
m_value = m_count;
m_count = 0;
m_time = GetNowCount();
}
return m_count;
}
int GetFps()
{
return m_value;
}
};
// ロック付きスイッチ
bool CheckKey(bool& flag, bool& locked_flag, int Key)
{
//DXP_INPUT_START
if (GetInputState() & Key)
{
if (!locked_flag)
{
flag = !flag;
locked_flag = true;
}
} else
locked_flag = false;
return flag;
}
int main(void)
{
if (DxLib_Init() != 0)// 初期化
exit(0);
// エラーハンドラの登録
initExceptionHandler();
SetDisplayFormat(DXP_FMT_8888); // 色数を16bitに
ChangeRandMode(DXP_RANDMODE_HW); // 高速化
ClearDrawScreen(); // 画面消し
Screen screen(480, 272);
bool replay;
do
{
replay = false;
{
Bar bar(screen.GetWidth() / 4, 10, DXP_COLOR_GREEN, screen.GetWidth() / 80);
Blocks block(20);
BlockBreaker breaker(bar, block);
Fps fps;
bool paused = false,
locked_start = false,
showed_debug = false,
locked_select = false;
while(ProcessMessage() == 0)
{
if (!breaker.IsCleared())
{
CheckKey(paused, locked_start, DXP_INPUT_START);
if (paused)
continue;
// 画面の初期化
clsDx();
ClearDrawScreen();
// デバッグの出力
if (CheckKey(showed_debug, locked_select, DXP_INPUT_SELECT))
{
printfDx("\nBLOCK=%d\nFALL=%d\nBALL=%d\nFPS=%d\n", //
breaker.GetCounter().GetBlockCount(), //
breaker.GetCounter().GetFallCount(), //
breaker.GetCounter().GetBallCount(), //
fps.GetFps());
}
// 更新
breaker.Update();
// 更新
ScreenFlip();
fps.Update();
}else
{
bool start = false;
if(CheckKey(start, locked_start, DXP_INPUT_START))
{
replay = true;
break;
}
//breaker.Redraw();
//ScreenCopy();
// 0.1秒
Sleep(100);
}
}
}
} while(replay == true);
DxLib_End();
exit(0);
return 0;
}