~ubuntu-branches/ubuntu/trusty/tomatoes/trusty

« back to all changes in this revision

Viewing changes to .pc/06_SDLK_KP_ENTER.diff/src/game.cpp

  • Committer: Package Import Robot
  • Author(s): Bart Martens
  • Date: 2012-05-23 05:13:05 UTC
  • Revision ID: package-import@ubuntu.com-20120523051305-21nvzupxhtuovr5y
Tags: 1.55-5
* Switch to dpkg-source 3.0 (quilt) format, debhelper 9, no cdbs.
* debian/README.Debian-source: Moved into debian/copyright.
* debian/tomatoes.6, debian/manpages: Added.  Closes: #626934.
* debian/tomatoes.desktop: Updated.  Closes: #626933.
* debian/patches/flags.diff: Added.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************
 
2
 
 
3
                         "I Have No Tomatoes"
 
4
                  Copyright (c) 2004, Mika Halttunen
 
5
 
 
6
 This software is provided 'as-is', without any express or implied
 
7
 warranty. In no event will the authors be held liable for any damages
 
8
 arising from the use of this software.
 
9
 
 
10
 Permission is granted to anyone to use this software for any purpose,
 
11
 including commercial applications, and to alter it and redistribute
 
12
 it freely, subject to the following restrictions:
 
13
 
 
14
    1. The origin of this software must not be misrepresented; you must
 
15
    not claim that you wrote the original software. If you use this
 
16
    software in a product, an acknowledgment in the product documentation
 
17
    would be appreciated but is not required.
 
18
 
 
19
    2. Altered source versions must be plainly marked as such, and must
 
20
    not be misrepresented as being the original software.
 
21
 
 
22
    3. This notice may not be removed or altered from any source
 
23
    distribution.
 
24
 
 
25
 
 
26
 Mika Halttunen <lsoft@mbnet.fi>
 
27
 
 
28
*************************************************************************/
 
29
 
 
30
#include <stdio.h>
 
31
#include "SDL.h"
 
32
#include "SDL_opengl.h"
 
33
#include "SDL_image.h"
 
34
#include "game.h"
 
35
#include "texture.h"
 
36
#include "init.h"
 
37
#include "timer.h"
 
38
#include "player.h"
 
39
#include "mymath.h"
 
40
#include "tilemap.h"
 
41
#include "enemy.h"
 
42
#include "font.h"
 
43
#include "bomb.h"
 
44
#include "particle.h"
 
45
#include "bonus.h"
 
46
#include "screenshot.h"
 
47
#include "levels.h"
 
48
#include "teleport.h"
 
49
#include "effects.h"
 
50
#include "special_power.h"
 
51
#include "soundmusic.h"
 
52
#include "trap.h"
 
53
#include "bgrounds.h"
 
54
#include "mpak.h"
 
55
#include "helpers.h"
 
56
#include "comments.h"
 
57
#include "select_special.h"
 
58
#include "hiscore.h"
 
59
 
 
60
 
 
61
// Background texture list
 
62
int bgrounds[NUM_BGROUNDS];
 
63
GLuint cur_bground;
 
64
 
 
65
// Fading stuff
 
66
// Are we fading? (0 == no, 1 == fade in, 2 == fade out)
 
67
int fading;
 
68
float fade_amount;
 
69
 
 
70
 
 
71
// Lighting
 
72
static float light_ambient[] = { 0.75f, 0.75f, 0.75f, 1.0f };
 
73
static float light_diffuse1[] = { 1.0f, 1.0f, 1.0f, 1.0f };
 
74
static float light_diffuse2[] = { 0.5f, 0.5f, 0.5f, 1.0f };
 
75
//static float light_diffuse2[] = { 0.3f, 0.8f, 1.0f, 1.0f };
 
76
static float light_position1[] = { MAP_W*0.5f, 6.0f, MAP_H*0.75f, 1.0f };
 
77
static float light_position2[] = { -MAP_W*0.5f, 2.0f, MAP_H*0.25f, 1.0f };
 
78
 
 
79
 
 
80
// Array containing currently pressed keys
 
81
Uint8 key[SDLK_LAST];
 
82
 
 
83
// Kill count
 
84
int kill_count;
 
85
 
 
86
// Are we playing a two player game?
 
87
bool two_players;
 
88
 
 
89
 
 
90
// From bonus.cpp
 
91
extern int killed_5_diamonds;
 
92
 
 
93
 
 
94
// Confirm the ESC press
 
95
static bool confirm_esc;
 
96
 
 
97
 
 
98
// Current fps
 
99
static float fps;
 
100
 
 
101
 
 
102
// Load background textures
 
103
void load_bgrounds() {
 
104
        // Prepare the background list
 
105
        for(int f=0; f<NUM_BGROUNDS; f++)
 
106
                bgrounds[f] = f;
 
107
 
 
108
        // Set the lighting
 
109
        glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse1);
 
110
        glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
 
111
        glEnable(GL_LIGHT1);
 
112
        glLightfv(GL_LIGHT2, GL_DIFFUSE, light_diffuse2);
 
113
        glLightfv(GL_LIGHT2, GL_POSITION, light_position2);
 
114
        glEnable(GL_LIGHT2);
 
115
        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient);
 
116
}
 
117
 
 
118
 
 
119
// Draw the scene
 
120
void draw() {
 
121
        //glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 
122
        glClear(GL_DEPTH_BUFFER_BIT);
 
123
 
 
124
        // Set up the viewport (either perspective or ortho)
 
125
        glMatrixMode(GL_PROJECTION);
 
126
        glLoadIdentity();
 
127
        if(!config.perspective_mode)
 
128
                glOrtho(-11.0f, 11.0f, -8.0f, 8.0f, -50.0f, 50.0f);
 
129
        else {
 
130
                gluPerspective(30.0f, 1.333333f, 1, 100);
 
131
                glTranslatef(0,0,-32);
 
132
        }
 
133
 
 
134
        // Draw the background
 
135
        glMatrixMode(GL_MODELVIEW);
 
136
        glLoadIdentity();
 
137
        BIND_TEXTURE(cur_bground);
 
138
        glDepthMask(GL_FALSE);
 
139
        glColor3f(1,1,1);
 
140
        glBegin(GL_TRIANGLE_STRIP);
 
141
                if(!config.perspective_mode) {
 
142
                        glTexCoord2f(1,1); glVertex3f(11.0f, 8.0f, -2);
 
143
                        glTexCoord2f(0,1); glVertex3f(-11.0f, 8.0f, -2);
 
144
                        glTexCoord2f(1,0); glVertex3f(11.0f, -8.0f, -2);
 
145
                        glTexCoord2f(0,0); glVertex3f(-11.0f, -8.0f, -2);
 
146
                }
 
147
                else {
 
148
                        glTexCoord2f(1,1); glVertex3f(12.2f, 9.1f, -2);
 
149
                        glTexCoord2f(0,1); glVertex3f(-12.2f, 9.1f, -2);
 
150
                        glTexCoord2f(1,0); glVertex3f(12.2f, -9.1f, -2);
 
151
                        glTexCoord2f(0,0); glVertex3f(-12.2f, -9.1f, -2);
 
152
                }
 
153
        glEnd();
 
154
        glDepthMask(GL_TRUE);
 
155
 
 
156
        // Rotate to the isometric view
 
157
        glRotatef(30.0f, 1.0f, 0,0);
 
158
        glRotatef(-45.0f, 0, 1.0f, 0.0f);
 
159
 
 
160
        // Center the map around the origin
 
161
        glTranslatef(-MAP_W * 0.5f, 0, -MAP_H * 0.5f);
 
162
 
 
163
 
 
164
        // Draw the map
 
165
        draw_map();
 
166
 
 
167
        // Enable alpha blending for sprites
 
168
        glEnable(GL_BLEND);
 
169
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
170
 
 
171
        // Draw the traps
 
172
        draw_traps();
 
173
 
 
174
 
 
175
        // Enable alpha testing
 
176
        glEnable(GL_ALPHA_TEST);
 
177
 
 
178
        // Draw the enemies
 
179
        draw_enemies();
 
180
 
 
181
        // Draw the players
 
182
        p1.draw();
 
183
        if(two_players)
 
184
                p2.draw();
 
185
 
 
186
        // Draw the potato men
 
187
        draw_potatomen();
 
188
 
 
189
        // Draw the bombs
 
190
        draw_bombs();
 
191
 
 
192
        // Disable alpha blending
 
193
        glDisable(GL_ALPHA_TEST);
 
194
 
 
195
 
 
196
 
 
197
        // Draw the bonuses
 
198
        draw_bonus();
 
199
 
 
200
        // Draw the teleports
 
201
        draw_teleports();
 
202
 
 
203
 
 
204
        // Draw the particles
 
205
        draw_particles();
 
206
 
 
207
        // Draw the wisps
 
208
        draw_wisps();
 
209
 
 
210
        // Draw the special powers
 
211
        draw_special_power();
 
212
 
 
213
        // Draw the player icons when in two player mode
 
214
        draw_player_icons();
 
215
 
 
216
 
 
217
        // Begin font drawing
 
218
        begin_fonts();
 
219
 
 
220
        // Draw the FPS amount
 
221
        if(config.show_fps && !icon_menu.visible) {
 
222
                glColor3f(1,1,1);
 
223
                glprintf_center(font1, 0, 0, 9, -25, "FPS: %02.1f", fps);
 
224
        }
 
225
 
 
226
        // Draw the kill count
 
227
        glColor3f(1,1,0.4f);
 
228
        char str[10] = "";
 
229
        sprintf(str, "%4d", kill_count);
 
230
        glprint_num(2.0f, 3.0f - 1.2f*icon_menu.pos, -10, str);
 
231
 
 
232
        // Draw the remaining seconds
 
233
        sprintf(str, "%2d", num_seconds_left);
 
234
        glColor3f(1, 0.8f, 0.4f);
 
235
        set_font_scale(0.25f, 0.3f);
 
236
        glprint_num(4.7f, 2.6f - 1.2f*icon_menu.pos, -10, str);
 
237
        set_font_scale(1,1);
 
238
 
 
239
 
 
240
        // Draw the comments
 
241
        draw_comments();
 
242
 
 
243
        // Draw the special power selection menu
 
244
        draw_special_selection();
 
245
 
 
246
        // Draw the ESC confirmation
 
247
        if(confirm_esc) {
 
248
                glLoadIdentity();
 
249
                BIND_TEXTURE(0);
 
250
                glColor4f(0,0,0, 0.45f);
 
251
                glBegin(GL_TRIANGLE_STRIP);
 
252
                        glVertex3f(4, 3, -2);
 
253
                        glVertex3f(-4, 3, -2);
 
254
                        glVertex3f(4,-3,-2);
 
255
                        glVertex3f(-4,-3,-2);
 
256
                glEnd();
 
257
 
 
258
                glColor3f(1,1,1);
 
259
                set_font_scale(0.4f, 0.6f);
 
260
                glprintf_center(font1, 0, 0, 2.0f, -13, "ARE YOU SURE YOU WANT TO EXIT?");
 
261
                glprintf_center(font1, 0, 0, 1.1f, -13, "Press ESC to exit, or ENTER to resume.");
 
262
                set_font_scale(1,1);
 
263
        }
 
264
 
 
265
        // Draw the fade
 
266
        if(fade_amount != 0.0f) {
 
267
                glLoadIdentity();
 
268
                BIND_TEXTURE(0);
 
269
                glColor4f(0,0,0,fade_amount);
 
270
                glBegin(GL_TRIANGLE_STRIP);
 
271
                        glVertex3f(4, 3, -2);
 
272
                        glVertex3f(-4, 3, -2);
 
273
                        glVertex3f(4,-3,-2);
 
274
                        glVertex3f(-4,-3,-2);
 
275
                glEnd();
 
276
        }
 
277
 
 
278
 
 
279
        end_fonts();
 
280
 
 
281
        // Flush and swap the buffers
 
282
        glFlush();
 
283
        SDL_GL_SwapBuffers();
 
284
}
 
285
 
 
286
 
 
287
// Handle SDL events
 
288
// Return true if we're going to quit
 
289
bool handle_event(SDL_Event &event) {
 
290
 
 
291
        switch(event.type) {
 
292
                case SDL_QUIT:          // Quit
 
293
                        return true;
 
294
 
 
295
                case SDL_KEYDOWN:
 
296
                        // Update the 'key' array
 
297
                        key[event.key.keysym.sym] = 1;
 
298
                        return false;
 
299
 
 
300
                case SDL_KEYUP:
 
301
                        // Update the 'key' array
 
302
                        key[event.key.keysym.sym] = 0;
 
303
                        return false;
 
304
        }
 
305
 
 
306
        return false;
 
307
}
 
308
 
 
309
 
 
310
// Start the game
 
311
void start_game(bool two_pls) {
 
312
        // Initialize
 
313
        memset(key, 0, sizeof(key));
 
314
        clear_comments();
 
315
        clear_map();
 
316
        clear_enemies();
 
317
        clear_bombs();
 
318
        clear_particles();
 
319
        clear_bonus();
 
320
        clear_traps();
 
321
        kill_count = 0;
 
322
        two_players = two_pls;
 
323
 
 
324
        // Set the players
 
325
        p1.clear();
 
326
        p2.clear();
 
327
 
 
328
        // Init the levels
 
329
        init_levels();
 
330
 
 
331
 
 
332
        // Fade in
 
333
        fading = 1;
 
334
        fade_amount = 1.0f;
 
335
 
 
336
 
 
337
        // Set up the main loop
 
338
        game_paused = false;
 
339
        confirm_esc = false;
 
340
        timer_count = 0;
 
341
        Uint32 fps_counter = 0;
 
342
        int frames_drawn = 0;
 
343
        Uint32 prev_ticks;
 
344
        prev_ticks = SDL_GetTicks();
 
345
        Uint32 confirm_time = 0;
 
346
        bool main_loop_done = false;
 
347
        bool f12_down = false;
 
348
        bool key_return_down = false;
 
349
        bool key_esc_down = false;
 
350
        bool finished = false;
 
351
 
 
352
 
 
353
        // Main loop
 
354
        while(!main_loop_done) {
 
355
                // Handle events
 
356
                SDL_Event event;
 
357
                while(SDL_PollEvent(&event)) {
 
358
                        main_loop_done = handle_event(event);
 
359
                }
 
360
 
 
361
                // Game loop
 
362
                while(timer_count > 0 && !game_paused) {
 
363
                        // Fade
 
364
                        if(fading) {
 
365
                                if(fading == 1) {
 
366
                                        // Fade in
 
367
                                        fade_amount -= 0.01f;
 
368
                                        if(fade_amount <= 0.0f) {
 
369
                                                fading = 0;
 
370
                                                fade_amount = 0.0f;
 
371
                                        }
 
372
                                }
 
373
                                else if(fading == 2) {
 
374
                                        // Fade out
 
375
                                        fade_amount += 0.01f;
 
376
                                        if(fade_amount >= 1.0f) {
 
377
                                                fading = 0;
 
378
                                                fade_amount = 1.0f;
 
379
                                                main_loop_done = true;
 
380
                                        }
 
381
                                }
 
382
                        }
 
383
 
 
384
                        // Check keypresses
 
385
                        // ESC press
 
386
                        if(key[SDLK_ESCAPE] && !fading) {
 
387
                                if(key_esc_down == false) {
 
388
                                        key_esc_down = true;
 
389
                                        if(!confirm_esc) {
 
390
                                                confirm_esc = true;
 
391
                                                // Keep track of the time, so we can restore it
 
392
                                                confirm_time = SDL_GetTicks();
 
393
                                        }
 
394
                                        else {
 
395
                                                // Fade out
 
396
                                                fading = 2;
 
397
                                                fade_amount = 0.0f;
 
398
                                        }
 
399
                                }
 
400
                        }
 
401
                        else
 
402
                                key_esc_down = false;
 
403
 
 
404
                        // ENTER press
 
405
                        if(confirm_esc) {
 
406
                                if(key[SDLK_RETURN]) {
 
407
                                        if(key_return_down == false) {
 
408
                                                key_return_down = true;
 
409
                                                confirm_esc = false;
 
410
                                                if(!killed_5_diamonds)
 
411
                                                        level_time += (SDL_GetTicks() - confirm_time);
 
412
                                        }
 
413
                                }
 
414
                                else
 
415
                                        key_return_down = false;
 
416
                        }
 
417
 
 
418
 
 
419
                        // Screenshot?
 
420
                        if(key[SDLK_F12]) {
 
421
                                if(f12_down == false) {
 
422
                                        f12_down = true;
 
423
                                        save_screenshot();
 
424
                                }
 
425
                        }
 
426
                        else
 
427
                                f12_down = false;
 
428
 
 
429
                        if(!confirm_esc) {
 
430
 
 
431
                                // Handle the level pause
 
432
                                if(level_pause == LEVEL_PAUSE_BEGIN) {
 
433
                                        if(key[SDLK_RETURN] || key[config.key_shoot[0]] || key[config.key_shoot[1]]) {
 
434
                                                if(!key_return_down) {
 
435
                                                        // Start timing the level change
 
436
                                                        start_level_timing();
 
437
                                                        level_pause = 0;
 
438
                                                        key_return_down = true;
 
439
 
 
440
                                                        // A little hack to prevent players firing their bombs accidentally,
 
441
                                                        // if they press their Shoot button to start the level.
 
442
                                                        p1.reload = 10;
 
443
                                                        p2.reload = 10;
 
444
                                                }
 
445
                                        }
 
446
                                        else
 
447
                                                key_return_down = false;
 
448
                                }
 
449
 
 
450
                                // Do the game stuff
 
451
                                if(!icon_menu.visible && level_pause != LEVEL_PAUSE_BEGIN && !finished) {
 
452
                                        // Change to the next level if enough time has passed
 
453
                                        // (we don't call this when the Killed-5 diamonds are visible, because
 
454
                                        //  it advances the time, and we don't want that to happen.)
 
455
                                        if(!killed_5_diamonds || level_pause == LEVEL_PAUSE_END) {
 
456
                                                if(!change_level()) {
 
457
                                                        // We've finished all the levels.
 
458
                                                        // Fade out
 
459
                                                        finished = true;
 
460
                                                        fading = 2;
 
461
                                                        fade_amount = 0.0f;
 
462
                                                }
 
463
                                        }
 
464
 
 
465
                                        // Animate stuff
 
466
                                        move_particles();
 
467
                                        animate_teleports();
 
468
 
 
469
                                        // Update the special powers
 
470
                                        update_special_power();
 
471
 
 
472
                                        // Move the stuff
 
473
                                        p1.move();
 
474
                                        if(two_players)
 
475
                                                p2.move();
 
476
                                        move_enemies();
 
477
                                        move_bombs();
 
478
                                        move_bonus();
 
479
                                        move_traps();
 
480
                                        move_wisps();
 
481
                                        move_potatomen();
 
482
                                }
 
483
 
 
484
                                // Do the comments
 
485
                                move_comments();
 
486
 
 
487
                                // Handle the special power selection menu
 
488
                                if(!finished)
 
489
                                        do_special_selection();
 
490
                        }
 
491
 
 
492
 
 
493
                        // Advance the fps_counter
 
494
                        if(config.show_fps) {
 
495
                                Uint32 ticks_now = SDL_GetTicks();
 
496
                                Uint32 diff = ticks_now - prev_ticks;
 
497
                                fps_counter += diff;
 
498
                                prev_ticks = ticks_now;
 
499
 
 
500
                                if(fps_counter >= 1000) {
 
501
                                        fps = (float)frames_drawn / (float)(fps_counter/1000.0f);
 
502
                                        frames_drawn = 0;
 
503
                                        fps_counter = 0;
 
504
                                }
 
505
                        }
 
506
 
 
507
                        timer_count--;
 
508
                }
 
509
 
 
510
                // Draw the scene
 
511
                draw();
 
512
                frames_drawn++;
 
513
        }
 
514
 
 
515
        // Free the stuff
 
516
        clear_enemies();
 
517
        clear_bombs();
 
518
        clear_particles();
 
519
        clear_bonus();
 
520
        clear_traps();
 
521
        kill_map();
 
522
 
 
523
 
 
524
        // Check if we get to the hiscore list
 
525
        HISCORE_LIST &list = (two_players) ? hiscore_2 : hiscore_1;
 
526
        int place = list.add_name("null", kill_count);
 
527
        if(place != -1) {
 
528
                // Get the name and save the list
 
529
                list.input_name(place);
 
530
                if(two_players)
 
531
                        list.save(get_hiscore_location(2,true));
 
532
                else
 
533
                        list.save(get_hiscore_location(1,true));
 
534
        }
 
535
}
 
536
 
 
537