File: misc.c

package info (click to toggle)
lbreakout2 2.5.2-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 6,040 kB
  • ctags: 2,374
  • sloc: ansic: 20,911; sh: 2,891; makefile: 374
file content (514 lines) | stat: -rw-r--r-- 16,635 bytes parent folder | download | duplicates (2)
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
/***************************************************************************
                          misc.c  -  description
                             -------------------
    begin                : Thu Sep 6 2001
    copyright            : (C) 2001 by Michael Speck
    email                : kulkanie@gmx.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <time.h>
#include "lbreakout.h"
#include "../game/game.h"
#include "config.h"
#include "event.h"
#include "misc.h"

extern SDL_Surface *stk_display, *nuke_bkgnd, *brick_pic;
extern Game *local_game, *game;
extern Paddle *l_paddle;
extern StkFont *font;
extern SDL_Surface *offscreen;
extern int stk_quit_request;
int shadow_size = 8;
#ifdef AUDIO_ENABLED
extern StkSound *wav_click;
#endif
extern int motion_button;
extern Config config;
extern int bkgnd_count;
extern SDL_Surface **bkgnds;

/*
====================================================================
Load background according to id and draw background to offscreen.
Return Value: loaded background surface
====================================================================
*/
void bkgnd_draw( SDL_Surface *bkgnd, int id )
{
	SDL_Surface *pic = 0;
	int i, j;

	if ( id >= bkgnd_count || id == -1 )
		id = rand() % bkgnd_count;

	/* load background */
	pic = bkgnds[id];
	for ( i = 0; i < bkgnd->w; i += pic->w ) {
		for ( j = 0; j < bkgnd->h; j += pic->h ) {
			stk_surface_blit( pic, 0,0,-1,-1,
					bkgnd, i, j);
		}
	}

	/* draw to offscreen */
	stk_surface_blit( bkgnd, 0,0,-1,-1, offscreen, 0,0 );
}
/*
====================================================================
Confirm request. Darkens screen a bit and display text.
Return Value: True if successful
====================================================================
*/
void draw_confirm_screen( StkFont *font, SDL_Surface *buffer, char *str )
{
    int i, y, x;
    Text *text = create_text( str, 60 );
    stk_surface_fill( stk_display, 0,0,-1,-1, 0x0 );
    stk_surface_alpha_blit( buffer, 0,0,-1,-1, stk_display, 0,0, 128 );
    font->align = STK_FONT_ALIGN_CENTER_X | STK_FONT_ALIGN_TOP;
    y = (stk_display->h - text->count * font->height) / 2;
    x = stk_display->w / 2;
    for ( i = 0; i < text->count; i++ ) {
        stk_font_write(font, stk_display, x, y, STK_OPAQUE, text->lines[i]);
        y += font->height;
    }
    delete_text( text );
}
int confirm( StkFont *font, char *str, int type )
{
    SDL_Event event;
    int go_on = 1;
    int ret = 0;
    SDL_Surface *buffer = 
        stk_surface_create( SDL_SWSURFACE, stk_display->w, stk_display->h );
    SDL_SetColorKey(buffer, 0, 0);

#ifdef AUDIO_ENABLED
    stk_sound_play( wav_click );
#endif

    event_clear_sdl_queue();

    stk_surface_blit( stk_display, 0,0,-1,-1, buffer, 0,0 );
    if ( type == CONFIRM_PAUSE )
        stk_surface_gray( stk_display, 0,0,-1,-1,0 );
    else
        draw_confirm_screen( font, buffer, str );
    stk_display_update( STK_UPDATE_ALL );

    while (go_on && !stk_quit_request) {
        SDL_WaitEvent(&event);
        /* TEST */
        switch ( event.type ) {
            case SDL_QUIT: stk_quit_request = 1; break;
            case SDL_MOUSEBUTTONUP:
                if ( type == CONFIRM_ANY_KEY ) {
                    ret = 1; go_on = 0;
                }
/*                else
                if ( type == CONFIRM_YES_NO ) {
                    if ( event.button.button == LEFT_BUTTON )
                        ret = 1;
                    else
                        ret = 0;
                    go_on = 0;
                }*/
                break;
            case SDL_KEYDOWN:
                if ( type == CONFIRM_ANY_KEY ) {
                    ret = 1; go_on = 0;
                    break;
                }
                else
                if ( type == CONFIRM_PAUSE ) {
                    if ( event.key.keysym.sym == SDLK_p ) {
                        ret = 1; go_on = 0;
                        break;
                    }
					else
					if ( event.key.keysym.sym == SDLK_f ) {
						config.fullscreen = !config.fullscreen;
                        stk_display_apply_fullscreen( config.fullscreen );
						draw_confirm_screen( font, buffer, str );
						stk_display_update( STK_UPDATE_ALL );
					}
                }
                else
                switch (event.key.keysym.sym) {
                    case SDLK_y:
                        go_on = 0;
                        ret = 1;
                        break;
                    case SDLK_ESCAPE:
                    case SDLK_n:
                        go_on = 0;
                        ret = 0;
                    default:
                        break;
                }
                break;
        }
    }
#ifdef AUDIO_ENABLED
    stk_sound_play( wav_click );
#endif
    stk_surface_blit( buffer, 0,0,-1,-1, stk_display, 0,0 );
    stk_display_update( STK_UPDATE_ALL );
    SDL_FreeSurface(buffer);

    /* reset the relative position so paddle wont jump */
    SDL_GetRelativeMouseState(0,0);

    return ret;
}
#ifdef NETWORK_ENABLED
/*
====================================================================
Display a info message (gray screen a bit and display text), 
send a MSG_READY when player has clicked and wait for a remote answer
(timeout 10 secs). Waiting may be cancelled by pressing ESCAPE which
results in sending a MSG_GAME_EXITED.
Return Value: True if both peers clicked to continue, False if the
connection was cancelled for some reason.
====================================================================
*/
int display_info( StkFont *font, char *str, NetSocket *peer )
{
#if 0
    char error[128];
    Net_Msg msg;
    SDL_Event event;
    int ret = 0, leave = 0;
    SDL_Surface *buffer = 
        stk_surface_create( SDL_SWSURFACE, stk_display->w, stk_display->h );
    SDL_SetColorKey(buffer, 0, 0);

#ifdef AUDIO_ENABLED
    stk_sound_play( wav_click );
#endif

    event_clear_sdl_queue();

    stk_surface_blit( stk_display, 0,0,-1,-1, buffer, 0,0 );
	draw_confirm_screen( font, buffer, str );
    stk_display_update( STK_UPDATE_ALL );
    stk_wait_for_input();
    net_write_empty_msg( peer, MSG_READY ); 
	draw_confirm_screen( font, buffer, 
                         "Waiting for remote answer..." );
    stk_display_update( STK_UPDATE_ALL );
    event_clear_sdl_queue();
    while ( !leave ) {
        if ( SDL_PollEvent( &event ) )
            if ( (event.type == SDL_KEYDOWN && 
                 event.key.keysym.sym == SDLK_ESCAPE) ||
                 event.type == SDL_QUIT ) {
                    net_write_empty_msg( peer, MSG_GAME_EXITED );
                    leave = 1;
                    break;
                }
        if ( net_read_msg( peer, &msg, 0 ) )
            switch ( msg.type ) {
                case MSG_READY:
                    ret = 1; leave = 1;
                    break;
                case MSG_GAME_EXITED:
                    ret = 0; leave = 1;
                    sprintf( error, "remote player cancelled the game\n" );
                    confirm( font, error, CONFIRM_ANY_KEY );
                    break;
            }
        SDL_Delay( 10 );
    }
    
#ifdef AUDIO_ENABLED
    stk_sound_play( wav_click );
#endif
    
    stk_surface_blit( buffer, 0,0,-1,-1, stk_display, 0,0 );
    stk_display_update( STK_UPDATE_ALL );
    SDL_FreeSurface(buffer);
    
    /* reset the relative position so paddle wont jump */
    SDL_GetRelativeMouseState(0,0);

    return ret;
#endif
    return 1;
}
#endif
/*
====================================================================
Create shadow surface for specified region in surface.
Return Value: Shadow surface
====================================================================
*/
SDL_Surface* create_shadow( SDL_Surface *surf, int x, int y, int w, int h )
{
    SDL_Surface *shadow = 0;
    int i, j;
    Uint32 white = SDL_MapRGB( stk_display->format, 0xff, 0xff, 0xff );
    Uint32 black = SDL_MapRGB( stk_display->format, 0, 0, 0 );
    shadow = stk_surface_create( SDL_SWSURFACE, w, h );
    SDL_SetColorKey( shadow, SDL_SRCCOLORKEY, white );
    for ( i = 0; i < w; i++ )
        for ( j = 0; j < h; j++ ) {
            if ( surf->flags & SDL_SRCCOLORKEY &&
                 stk_surface_get_pixel( surf, i, j ) == surf->format->colorkey )
                stk_surface_set_pixel( shadow, i, j, white );
            else
                stk_surface_set_pixel( shadow, i, j, black );
        }
    return shadow;
}


/*
====================================================================
Enter a string and return True if ENTER received and False
if ESCAPE received.
====================================================================
*/
int enter_string( StkFont *font, char *caption, char *edit, int limit )
{
    SDL_Event event;
    int go_on = 1;
    int ret = 0;
    SDL_Surface *buffer = 
        stk_surface_create( SDL_SWSURFACE, stk_display->w, stk_display->h );
    int length = strlen( edit );

    SDL_SetColorKey(buffer, 0, 0);

    stk_surface_blit( stk_display, 0,0,-1,-1, buffer, 0,0 );
    font->align = STK_FONT_ALIGN_CENTER_X | STK_FONT_ALIGN_CENTER_Y;

    while ( go_on && !stk_quit_request ) {

        stk_surface_fill( stk_display, 0,0,-1,-1, 0x0 );
        stk_surface_alpha_blit( buffer, 0,0,-1,-1, stk_display, 0,0, 128 );
        stk_font_write(font, stk_display, stk_display->w / 2, stk_display->h / 2, STK_OPAQUE, caption);
        write_text_with_cursor(font, stk_display, stk_display->w / 2, stk_display->h / 2 + font->height, edit, STK_OPAQUE);
        stk_display_update( STK_UPDATE_ALL );
        event.type = SDL_NOEVENT;
        SDL_PollEvent(&event);
        /* TEST */
        switch ( event.type ) {
            case SDL_QUIT: stk_quit_request = 1; break;
            case SDL_KEYDOWN:
                switch ( event.key.keysym.sym ) {
                    case SDLK_ESCAPE:
                        ret = 0;
                        go_on = 0;
                        break;
                    case SDLK_RETURN:
                        ret = 1;
                        go_on = 0;
                        break;
                    case SDLK_BACKSPACE:
                        if ( length > 0 ) edit[--length] = 0;
                        break;
                    default:
                        if ( event.key.keysym.sym >= 32 && event.key.keysym.sym < 128 && length < limit ) {
                            edit[length++] = event.key.keysym.unicode;
                            edit[length] = 0;
                        }
                        break;
                }
                break;
        }
    }
    stk_surface_blit( buffer, 0,0,-1,-1, stk_display, 0,0 );
    stk_display_update( STK_UPDATE_ALL );
    SDL_FreeSurface(buffer);

    /* reset the relative position so paddle wont jump */
    SDL_GetRelativeMouseState(0,0);

    return ret;
}

/*
====================================================================
Display text blinking.
====================================================================
*/
void  write_text_with_cursor( StkFont *fnt, SDL_Surface *dest,
    int x, int y, char *str, int alpha)
{
    static int cursor_on = 0;
    static Uint32 last_tick = 0;
    // create temporary space for cursor and text
    char *text_with_cursor = calloc(strlen(str) + 2, sizeof(char));
    if (text_with_cursor) {
       strcpy(text_with_cursor, str);

       // Time to blink cursor on/off?
       if (SDL_GetTicks() - last_tick > 500) {
            last_tick = SDL_GetTicks();
	    cursor_on = ! cursor_on;
       }
       // Tack on cursor to end of text
       if (cursor_on) {
	    strcat(text_with_cursor, "_");
       }
       else {
	    strcat(text_with_cursor, " ");
       }
    	    
       stk_font_write(fnt,dest,x,y,alpha,text_with_cursor);
    
       free(text_with_cursor);
    }
}

/*
====================================================================
Enter nuke mode and allow player to disintegrate single bricks
by spending 5% of his/her score.
====================================================================
*/
void game_nuke( void )
{
	char buf[128];
	SDL_Event event;
	int x,y,i,j,leave = 0;
	SDL_Surface *buffer = 
		stk_surface_create( SDL_SWSURFACE, stk_display->w, stk_display->h );
	SDL_Surface *red_mask = 
		stk_surface_create( SDL_SWSURFACE, BRICK_WIDTH, BRICK_HEIGHT );
	stk_surface_fill( red_mask, 0,0,-1,-1, 0xFF0000 );
	SDL_SetAlpha( red_mask, SDL_SRCALPHA, 128 );
	SDL_SetColorKey(buffer, 0, 0);

#ifdef AUDIO_ENABLED
	stk_sound_play( wav_click );
#endif
	SDL_SetEventFilter(0);
	event_clear_sdl_queue();
	/* backup screen contents */
	stk_surface_blit( stk_display, 0,0,-1,-1, buffer, 0,0 );
	/* display bricks darkened */
	stk_surface_blit( nuke_bkgnd, 0,0,-1,-1, 
			stk_display, 0,0 );
	for ( i = 1; i < MAP_WIDTH - 1; i++ )
		for ( j = 1; j < MAP_HEIGHT - 1; j++ )
			if ( game->bricks[i][j].id >= 0 )
				stk_surface_alpha_blit( brick_pic,
					game->bricks[i][j].id * BRICK_WIDTH, 0,
					BRICK_WIDTH, BRICK_HEIGHT,
					stk_display,
					i*BRICK_WIDTH, j*BRICK_HEIGHT, 128 );
	/* info */
	font->align = STK_FONT_ALIGN_LEFT;
	sprintf( buf, "Plane Of Inner Stability entered (Score: %i)",
			l_paddle->player->stats.total_score + l_paddle->score );
	stk_font_write( font, stk_display,
			BRICK_WIDTH, (MAP_HEIGHT-1)*BRICK_HEIGHT, 
			128, buf );
	/* show score of player */
	stk_display_update( STK_UPDATE_ALL );

	x = y = -1;
	while (!leave && !stk_quit_request) {
		SDL_WaitEvent(&event);
		switch ( event.type ) {
			case SDL_QUIT: 
				stk_quit_request = 1; 
				break;
			case SDL_MOUSEBUTTONDOWN:
				if ( x != -1 )
				if ( confirm( font, 
					"Disintegrate Brick? (Costs 5% of your score.) y/n", 
					CONFIRM_YES_NO ) ) {
					/* implant a bomb to this brick and return */
					game_set_current( local_game );
					brick_start_expl( x,y, local_game->paddles[0] );
					local_game->bricks[x][y].score = 0;
					game_set_current( game );
					l_paddle->player->stats.total_score -= (int)(0.05 * 
					       (l_paddle->score + 
					        l_paddle->player->stats.total_score));
					leave = 1;
				}
				break;
			case SDL_MOUSEMOTION:
				if ( x != -1 ) {
					/* clear old selection */
					stk_surface_blit( nuke_bkgnd,
							x*BRICK_WIDTH, y*BRICK_HEIGHT,
							BRICK_WIDTH, BRICK_HEIGHT,
							stk_display, 
							x*BRICK_WIDTH, y*BRICK_HEIGHT );
					stk_surface_alpha_blit( brick_pic,
							game->bricks[x][y].id * BRICK_WIDTH, 0,
							BRICK_WIDTH, BRICK_HEIGHT,
							stk_display,
							x*BRICK_WIDTH, y*BRICK_HEIGHT, 128 );
					stk_display_store_drect();
					x = y = -1;
				}
				/* make new selection if brick */
				i = event.motion.x / BRICK_WIDTH;
				j = event.motion.y / BRICK_HEIGHT;
				if ( i >= 1 && i <= MAP_WIDTH -2 )
				if ( j >= 1 && j <= MAP_HEIGHT - 2 )
				if ( game->bricks[i][j].id >= 0 ) {
					x = i; y = j;
					stk_surface_blit( red_mask, 0,0,-1,-1,
					stk_display,x*BRICK_WIDTH, y*BRICK_HEIGHT );
					stk_display_store_drect();
				}
				break;
			case SDL_KEYDOWN:
				if ( event.key.keysym.sym == SDLK_ESCAPE )
					leave = 1;
				break;
		}
		stk_display_update( STK_UPDATE_RECTS );
	}

	stk_surface_blit( buffer, 0,0,-1,-1, stk_display, 0,0 );
	stk_display_update( STK_UPDATE_ALL );
	SDL_FreeSurface(red_mask);
	SDL_FreeSurface(buffer);
	SDL_SetEventFilter(event_filter);
}

/* gray screen and display a formatted text, directly update the
 * screen */
void display_text( StkFont *font, char *format, ... )
{
	int i, y, x;
	Text *text;
	char buf[512];
	va_list args;

	va_start( args, format );
	vsnprintf( buf, 512, format, args );
	va_end( args );

        stk_surface_gray( stk_display, 0,0,-1,-1, 2 );
	text = create_text( buf, 60 );
	font->align = STK_FONT_ALIGN_CENTER_X | STK_FONT_ALIGN_TOP;
	y = (stk_display->h - text->count * font->height) / 2;
	x = stk_display->w / 2;
	for ( i = 0; i < text->count; i++ ) {
		stk_font_write(font, stk_display, x, y, STK_OPAQUE, text->lines[i]);
		y += font->height;
	}
	delete_text( text );

	stk_display_update( STK_UPDATE_ALL );
}