1 #include "cosmic_protector.hpp"
2
3 #include <ctime>
4
5 static float waveAngle = 0.0f;
6 static ALLEGRO_BITMAP *waveBitmap = 0;
7 static float bgx = 0;
8 static float bgy = 0;
9 static int shakeUpdateCount = 0;
10 static int shakeCount = 0;
11 const int SHAKE_TIME = 100;
12 const int SHAKE_TIMES = 10;
13
renderWave(void)14 static void renderWave(void)
15 {
16 int w = al_get_bitmap_width(waveBitmap);
17 int h = al_get_bitmap_height(waveBitmap);
18
19 float a = waveAngle + ALLEGRO_PI/2;
20
21 int x = (int)(BB_W/2 + 64*cos(a));
22 int y = (int)(BB_H/2 + 64*sin(a));
23
24 al_draw_rotated_bitmap(waveBitmap, w/2, h,
25 x, y, waveAngle, 0);
26 }
27
stopWave(void)28 static void stopWave(void)
29 {
30 waveAngle = 0.0f;
31 al_destroy_bitmap(waveBitmap);
32 waveBitmap = 0;
33 }
34
showWave(int num)35 void showWave(int num)
36 {
37 if (waveBitmap)
38 stopWave();
39
40 ResourceManager& rm = ResourceManager::getInstance();
41
42 ALLEGRO_FONT *myfont = (ALLEGRO_FONT *)rm.getData(RES_LARGEFONT);
43
44 char text[20];
45 sprintf(text, "WAVE %d", num);
46
47 int w = al_get_text_width(myfont, text);
48 int h = al_get_font_line_height(myfont);
49
50 waveBitmap = al_create_bitmap(w, h);
51 ALLEGRO_BITMAP *old_target = al_get_target_bitmap();
52 al_set_target_bitmap(waveBitmap);
53 al_clear_to_color(al_map_rgba(0, 0, 0, 0));
54 al_draw_textf(myfont, al_map_rgb(255, 255, 255), 0, 0, 0, "%s", text);
55 al_set_target_bitmap(old_target);
56
57 waveAngle = (ALLEGRO_PI*2);
58 }
59
shake(void)60 void shake(void)
61 {
62 shakeUpdateCount = SHAKE_TIME;
63 bgx = randf(0.0f, 8.0f);
64 bgy = randf(0.0f, 8.0f);
65 if (rand() % 2) bgx = -bgx;
66 if (rand() % 2) bgy = -bgy;
67 }
68
render(int step)69 void render(int step)
70 {
71 #ifdef ALLEGRO_IPHONE
72 if (switched_out)
73 return;
74 #endif
75
76 ResourceManager& rm = ResourceManager::getInstance();
77
78 ALLEGRO_BITMAP *bg = (ALLEGRO_BITMAP *)rm.getData(RES_BACKGROUND);
79
80 if (shakeUpdateCount > 0) {
81 shakeUpdateCount -= step;
82 if (shakeUpdateCount <= 0) {
83 shakeCount++;
84 if (shakeCount >= SHAKE_TIMES) {
85 shakeCount = 0;
86 shakeUpdateCount = 0;
87 bgx = bgy = 0;
88 }
89 else {
90 bgx = randf(0.0f, 8.0f);
91 bgy = randf(0.0f, 8.0f);
92 if (rand() % 2) bgx = -bgx;
93 if (rand() % 2) bgy = -bgy;
94 shakeUpdateCount = SHAKE_TIME;
95 }
96 }
97 }
98
99 al_clear_to_color(al_map_rgb(0, 0, 0));
100
101 float h = al_get_bitmap_height(bg);
102 float w = al_get_bitmap_width(bg);
103 al_draw_bitmap(bg, (BB_W-w)/2+bgx, (BB_H-h)/2+bgy, 0);
104
105 std::list<Entity *>::iterator it;
106 for (it = entities.begin(); it != entities.end(); it++) {
107 Entity *e = *it;
108 e->render_four(al_map_rgb(255, 255, 255));
109 if (e->isHighlighted()) {
110 al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_ONE);
111 e->render_four(al_map_rgb(150, 150, 150));
112 al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
113 }
114 }
115
116 Player *player = (Player *)rm.getData(RES_PLAYER);
117 player->render_four(al_map_rgb(255, 255, 255));
118 player->render_extra();
119
120 if (waveAngle > 0.0f) {
121 renderWave();
122 waveAngle -= 0.003f * step;
123 if (waveAngle <= 0.0f) {
124 stopWave();
125 }
126 }
127
128 #ifdef ALLEGRO_IPHONE
129 Input *input = (Input *)rm.getData(RES_INPUT);
130 input->draw();
131
132 int xx = BB_W-30;
133 int yy = 30;
134
135 al_draw_line(xx-10, yy-10, xx+10, yy+10, al_map_rgb(255, 255, 255), 4);
136 al_draw_line(xx-10, yy+10, xx+10, yy-10, al_map_rgb(255, 255, 255), 4);
137 al_draw_circle(xx, yy, 20, al_map_rgb(255, 255, 255), 4);
138 #endif
139
140 al_flip_display();
141 }
142
143