summaryrefslogtreecommitdiff
path: root/source/presentation/BitmapStore.cpp
blob: c289ff303791feea9a74512ee42d968dcc586417 (plain)
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
#include "BitmapStore.h"

BitmapStore::BitmapStore(unsigned int blockWidth)
    :_blockWidth(blockWidth)
{
    _bitmapFont = al_load_font("junction 02.ttf", blockWidth/6, 0);
    if (_bitmapFont == NULL)
    {
        al_show_native_message_box(NULL, "Fatal error", "Fatal error", "The file 'junction 02.ttf' was not found. Ensure that it is located in the working directory.", NULL, ALLEGRO_MESSAGEBOX_ERROR);
        throw InstallFailure();
    }
}

BitmapStore::~BitmapStore()
{
    for (map<Image,ALLEGRO_BITMAP*>::iterator iter = _bitmaps.begin();
        iter != _bitmaps.end(); ++iter)
    {
        al_destroy_bitmap(iter->second);
    }
    _bitmaps.clear();
    al_destroy_font(_bitmapFont);
}

ALLEGRO_BITMAP* BitmapStore::getBitmap(Image image)
{
    map<Image,ALLEGRO_BITMAP*>::const_iterator iter = _bitmaps.find(image);
    if (iter != _bitmaps.end())
    {
        return iter->second;
    }
    else
    {
        ALLEGRO_BITMAP* newImage = al_create_bitmap(_blockWidth, _blockWidth);
        switch (image)
        {
            case PLAYER:
                drawPlayerCar(newImage);
                break;
            case ENEMY:
                drawEnemyCar(newImage);
                break;
            case CHECKPOINT:
                drawCheckpoint(newImage);
                break;
            case ROCK:
                drawRock(newImage);
                break;
            case MAZE_WALL:
                drawMazeWall(newImage);
                break;
            case MAZE_FLOOR:
                drawMazeFloor(newImage);
                break;
            case SMOKE:
                drawSmoke(newImage);
                break;
            case CRASHED_CAR:
                drawCrashedCar(newImage);
                break;
            case CLAIMED_CHECKPOINT:
                drawClaimedCheckpoint(newImage);
                break;
        }

        _bitmaps.insert(make_pair(image, newImage));
        return newImage;
    }
}

void BitmapStore::drawPlayerCar(ALLEGRO_BITMAP* canvas)
{
    ALLEGRO_BITMAP* prev_draw = al_get_target_bitmap();
    al_set_target_bitmap(canvas);

    //car body
    al_draw_filled_rounded_rectangle(_blockWidth*0.2, 0, _blockWidth*0.8, _blockWidth*0.96, _blockWidth*0.1, _blockWidth*0.1, al_map_rgb(0,0,255));

    //racing stripes
    al_draw_filled_rectangle(_blockWidth*0.35, 0, _blockWidth*0.4, _blockWidth*0.3, al_map_rgb(255,255,255));
    al_draw_filled_rectangle(_blockWidth*0.6, 0, _blockWidth*0.65, _blockWidth*0.3, al_map_rgb(255,255,255));

    //windscreen
    al_draw_filled_rectangle(_blockWidth*0.3, _blockWidth*0.3, _blockWidth*0.7, _blockWidth*0.5, al_map_rgb (0,0,0));

    //roof
    al_draw_rounded_rectangle(_blockWidth*0.3, _blockWidth*0.5, _blockWidth*0.7, _blockWidth*0.9, _blockWidth*0.04, _blockWidth*0.04, al_map_rgb (25,25, 112), _blockWidth*0.04);

    //spoiler
    al_draw_filled_rectangle(_blockWidth*0.2, _blockWidth*0.96, _blockWidth*0.8, _blockWidth, al_map_rgb (0,0, 225));
    al_draw_rectangle(_blockWidth*0.2, _blockWidth*0.96, _blockWidth*0.8, _blockWidth, al_map_rgb(25,25, 112),_blockWidth*0.04);

    //headlights
    al_draw_filled_rectangle (_blockWidth*0.3,0,_blockWidth*0.35,_blockWidth*0.06, al_map_rgb(255,225,0));
    al_draw_filled_rectangle (_blockWidth*0.65,0,_blockWidth*0.7,_blockWidth*0.06, al_map_rgb(255,225,0));

    //tyres
    al_draw_filled_rounded_rectangle (_blockWidth*0.1,_blockWidth*0.13,_blockWidth*0.2,_blockWidth*0.37,_blockWidth*0.03,_blockWidth*0.03, al_map_rgb(131,139,131));
    al_draw_filled_rounded_rectangle (_blockWidth*0.8,_blockWidth*0.13,_blockWidth*0.9,_blockWidth*0.37,_blockWidth*0.03,_blockWidth*0.03, al_map_rgb(131,139,131));
    al_draw_filled_rounded_rectangle (_blockWidth*0.1,_blockWidth*0.63,_blockWidth*0.2,_blockWidth*0.87,_blockWidth*0.03,_blockWidth*0.03, al_map_rgb(131,139,131));
    al_draw_filled_rounded_rectangle (_blockWidth*0.8,_blockWidth*0.63,_blockWidth*0.9,_blockWidth*0.87,_blockWidth*0.03,_blockWidth*0.03, al_map_rgb(131,139,131));

    al_set_target_bitmap(prev_draw);
}
void BitmapStore::drawEnemyCar(ALLEGRO_BITMAP* canvas)
{
    ALLEGRO_BITMAP* prev_draw = al_get_target_bitmap();
    al_set_target_bitmap(canvas);

    //car body
    al_draw_filled_rounded_rectangle(_blockWidth*0.2, 0, _blockWidth*0.8, _blockWidth*0.96, _blockWidth*0.1, _blockWidth*0.1, al_map_rgb(255,0,0));

    //racing stripes
    al_draw_filled_rectangle(_blockWidth*0.35, 0, _blockWidth*0.4, _blockWidth*0.3, al_map_rgb(255,255,255));
    al_draw_filled_rectangle(_blockWidth*0.6, 0, _blockWidth*0.65, _blockWidth*0.3, al_map_rgb(255,255,255));

    //windscreen
    al_draw_filled_rectangle(_blockWidth*0.3, _blockWidth*0.3, _blockWidth*0.7, _blockWidth*0.5, al_map_rgb (0,0,0));

    //roof
    al_draw_rounded_rectangle(_blockWidth*0.3, _blockWidth*0.5, _blockWidth*0.7, _blockWidth*0.9, _blockWidth*0.04, _blockWidth*0.04, al_map_rgb (25,25, 112), _blockWidth*0.04);

    //spoiler
    al_draw_filled_rectangle(_blockWidth*0.2, _blockWidth*0.96, _blockWidth*0.8, _blockWidth, al_map_rgb (0,0, 225));
    al_draw_rectangle(_blockWidth*0.2, _blockWidth*0.96, _blockWidth*0.8, _blockWidth, al_map_rgb(25,25, 112),_blockWidth*0.04);

    //headlights
    al_draw_filled_rectangle (_blockWidth*0.3,0,_blockWidth*0.35,_blockWidth*0.06, al_map_rgb(255,225,0));
    al_draw_filled_rectangle (_blockWidth*0.65,0,_blockWidth*0.7,_blockWidth*0.06, al_map_rgb(255,225,0));

    //tyres
    al_draw_filled_rounded_rectangle (_blockWidth*0.1,_blockWidth*0.13,_blockWidth*0.2,_blockWidth*0.37,_blockWidth*0.03,_blockWidth*0.03, al_map_rgb(131,139,131));
    al_draw_filled_rounded_rectangle (_blockWidth*0.8,_blockWidth*0.13,_blockWidth*0.9,_blockWidth*0.37,_blockWidth*0.03,_blockWidth*0.03, al_map_rgb(131,139,131));
    al_draw_filled_rounded_rectangle (_blockWidth*0.1,_blockWidth*0.63,_blockWidth*0.2,_blockWidth*0.87,_blockWidth*0.03,_blockWidth*0.03, al_map_rgb(131,139,131));
    al_draw_filled_rounded_rectangle (_blockWidth*0.8,_blockWidth*0.63,_blockWidth*0.9,_blockWidth*0.87,_blockWidth*0.03,_blockWidth*0.03, al_map_rgb(131,139,131));

    al_set_target_bitmap(prev_draw);
}
void BitmapStore::drawRock(ALLEGRO_BITMAP* canvas)
{
    ALLEGRO_BITMAP* prev_draw = al_get_target_bitmap();
    al_set_target_bitmap(canvas);

    ALLEGRO_COLOR colour = al_map_rgb(131,139,131);
    al_draw_filled_circle(_blockWidth/2, _blockWidth/2, _blockWidth/2-1, colour);

    al_draw_filled_circle(_blockWidth/2, _blockWidth/2, _blockWidth/2-6, colour);
    al_draw_filled_circle(_blockWidth/4, _blockWidth/4, _blockWidth/4-1, al_map_rgb(205,197,191));
    al_draw_filled_circle(_blockWidth/3.2, _blockWidth/4.2, _blockWidth/4-2, al_map_rgb(205,197,191));
    al_draw_filled_circle(_blockWidth/1.2, _blockWidth/2, _blockWidth/2-15, al_map_rgb(205,197,191));
    al_draw_filled_circle(_blockWidth/2, _blockWidth/2, _blockWidth/2-8, al_map_rgb(205,205,193));

    al_set_target_bitmap(prev_draw);
}
void BitmapStore::drawCheckpoint(ALLEGRO_BITMAP* canvas)
{
    ALLEGRO_BITMAP* prev_draw = al_get_target_bitmap();
    al_set_target_bitmap(canvas);

    ALLEGRO_COLOR colour = al_map_rgb(255,255,0);

    al_draw_filled_rectangle (_blockWidth*0.44, _blockWidth*0.1, _blockWidth*0.5, _blockWidth*0.9, colour);
    al_draw_filled_rounded_rectangle (_blockWidth*0.34, _blockWidth*0.9, _blockWidth*0.6, _blockWidth*0.98, _blockWidth*0.01, _blockWidth*0.01, colour);
    al_draw_filled_circle (_blockWidth*0.47, _blockWidth*0.14, _blockWidth*0.1, colour);
    al_draw_filled_triangle (_blockWidth*0.44, _blockWidth*0.26, _blockWidth*0.44, _blockWidth*0.58, _blockWidth*0.8, _blockWidth*0.42, colour);

    al_set_target_bitmap(prev_draw);
}
void BitmapStore::drawMazeWall(ALLEGRO_BITMAP* canvas)
{
    ALLEGRO_BITMAP* prev_draw = al_get_target_bitmap();
    al_set_target_bitmap(canvas);

    ALLEGRO_COLOR colour = al_map_rgb(203,255,151);
    al_clear_to_color(colour);

    al_set_target_bitmap(prev_draw);
}
void BitmapStore::drawMazeFloor(ALLEGRO_BITMAP* canvas)
{
    ALLEGRO_BITMAP* prev_draw = al_get_target_bitmap();
    al_set_target_bitmap(canvas);

    ALLEGRO_COLOR colour = al_map_rgb(0,0,0);
    al_clear_to_color(colour);

    al_set_target_bitmap(prev_draw);
}
void BitmapStore::drawSmoke(ALLEGRO_BITMAP* canvas)
{
    ALLEGRO_BITMAP* prev_draw = al_get_target_bitmap();
    al_set_target_bitmap(canvas);

    ALLEGRO_COLOR colour = al_map_rgb(255,255,255);
    al_draw_circle (_blockWidth/2.3, _blockWidth/2.1, _blockWidth/2-1, colour,1);
    al_draw_circle (_blockWidth/4, _blockWidth/4, _blockWidth/4, colour,2);
    al_draw_circle (_blockWidth/5, _blockWidth/1.5, _blockWidth/4, colour,4);
    al_draw_circle (_blockWidth/2.5, _blockWidth/2.7, _blockWidth/3, colour,3);
    al_draw_circle (_blockWidth/1.2, _blockWidth/1.8, _blockWidth/3.7, colour,2);
    al_draw_circle (_blockWidth/2.8, _blockWidth/2.2, _blockWidth/6, colour,3);
    al_draw_circle (_blockWidth/1.1, _blockWidth/1.2, _blockWidth/3, colour,2);
    al_draw_circle (_blockWidth/1.2, _blockWidth/1.7, _blockWidth/2, colour,3);
    al_draw_circle (_blockWidth/1.3, _blockWidth/1.3, _blockWidth/5, colour,2);

    al_set_target_bitmap(prev_draw);
}

void BitmapStore::drawCrashedCar(ALLEGRO_BITMAP* canvas)
{
    ALLEGRO_BITMAP* prev_draw = al_get_target_bitmap();
    al_set_target_bitmap(canvas);

    al_draw_filled_rounded_rectangle(_blockWidth/3.33, _blockWidth/5, _blockWidth/1.25, _blockWidth/1.04, 5, 5, al_map_rgb (200, 200, 200));
    al_draw_circle (_blockWidth/2.3, _blockWidth/2.1, _blockWidth/2-1, al_map_rgb (255, 0, 0),1);
    al_draw_circle (_blockWidth/4, _blockWidth/4, _blockWidth/4, al_map_rgb (100, 100, 100),2);
    al_draw_circle (_blockWidth/5, _blockWidth/1.5, _blockWidth/4, al_map_rgb (255, 0, 0),4);
    al_draw_filled_rectangle(_blockWidth/2.5, _blockWidth/2, _blockWidth/1.43, _blockWidth/1.7, al_map_rgb (0,0, 0));
    al_draw_circle (_blockWidth/2.5, _blockWidth/2.7, _blockWidth/3, al_map_rgb (100, 100, 100),3);
    al_draw_circle (_blockWidth/1.2, _blockWidth/1.8, _blockWidth/3.7, al_map_rgb (255, 0, 0),2);
    al_draw_rectangle(_blockWidth/3.13, _blockWidth/1.04, _blockWidth/1.25, _blockWidth, al_map_rgb (25,25, 112),1);
    al_draw_circle (_blockWidth/2.8, _blockWidth/2.2, _blockWidth/6, al_map_rgb (255, 0, 0),3);
    al_draw_circle (_blockWidth/1.1, _blockWidth/1.2, _blockWidth/3, al_map_rgb (100, 100, 100),2);
    al_draw_circle (_blockWidth/1.2, _blockWidth/1.7, _blockWidth/2, al_map_rgb (100, 100, 100),3);
    al_draw_circle (_blockWidth/1.3, _blockWidth/1.3, _blockWidth/5, al_map_rgb (255, 0, 0),2);

    al_set_target_bitmap(prev_draw);
}

void BitmapStore::drawClaimedCheckpoint(ALLEGRO_BITMAP* canvas)
{
    ALLEGRO_BITMAP* prev_draw = al_get_target_bitmap();
    al_set_target_bitmap(canvas);

    ALLEGRO_COLOR colour = al_map_rgb(255,255,255);
    al_draw_text(_bitmapFont, colour, _blockWidth/2, _blockWidth/2, ALLEGRO_ALIGN_CENTRE , "GOTCHA");

    al_set_target_bitmap(prev_draw);
}