summaryrefslogtreecommitdiff
path: root/apps/plugins/test_codec.c
blob: 03c2daa30beebf7f398e75e1fd7689e85829081c (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
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2007 Dave Chapman
 *
 * 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.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"

PLUGIN_HEADER

/* All swcodec targets have BUTTON_SELECT apart from the H10 and M3 */

#if CONFIG_KEYPAD == IRIVER_H10_PAD
#define TESTCODEC_EXITBUTTON BUTTON_RIGHT
#elif CONFIG_KEYPAD == IAUDIO_M3_PAD
#define TESTCODEC_EXITBUTTON BUTTON_RC_PLAY
#elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
#define TESTCODEC_EXITBUTTON BUTTON_PLAY
#elif CONFIG_KEYPAD == COWON_D2_PAD || CONFIG_KEYPAD == ONDAVX747_PAD
#define TESTCODEC_EXITBUTTON BUTTON_POWER
#elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
#define TESTCODEC_EXITBUTTON BUTTON_REC
#elif defined(HAVE_TOUCHSCREEN)
#define TESTCODEC_EXITBUTTON BUTTON_TOPLEFT
#else
#define TESTCODEC_EXITBUTTON BUTTON_SELECT
#endif

/* Log functions copied from test_disk.c */
static int line = 0;
static int max_line = 0;
static int log_fd = -1;
static char logfilename[MAX_PATH];

static bool log_init(bool use_logfile)
{
    int h;

    rb->lcd_getstringsize("A", NULL, &h);
    max_line = LCD_HEIGHT / h;
    line = 0;
    rb->lcd_clear_display();
    rb->lcd_update();

    if (use_logfile) {
        rb->create_numbered_filename(logfilename, "/", "test_codec_log_", ".txt",
                                     2 IF_CNFN_NUM_(, NULL));
        log_fd = rb->open(logfilename, O_RDWR|O_CREAT|O_TRUNC);
        return log_fd >= 0;
    }

    return true;
}

static void log_text(char *text, bool advance)
{
    rb->lcd_puts(0, line, text);
    rb->lcd_update();
    if (advance)
    {
        if (++line >= max_line)
            line = 0;
        if (log_fd >= 0)
            rb->fdprintf(log_fd, "%s\n", text);
    }
}

static void log_close(void)
{
    if (log_fd >= 0)
        rb->close(log_fd);
}

struct wavinfo_t
{
  int fd;
  int samplerate;
  int channels;
  int sampledepth;
  int stereomode;
  int totalsamples;
};

static void* audiobuf;
static void* codec_mallocbuf;
static size_t audiosize;
static char str[MAX_PATH];

/* Our local implementation of the codec API */
static struct codec_api ci;

struct test_track_info {
    struct mp3entry id3;       /* TAG metadata */
    size_t filesize;           /* File total length */
};

static struct test_track_info track;
static bool taginfo_ready = true;

static bool use_dsp;

static bool checksum;
static uint32_t crc32;

static volatile unsigned int elapsed;
static volatile bool codec_playing;
static volatile long endtick;
struct wavinfo_t wavinfo;

static unsigned char wav_header[44] =
{
    'R','I','F','F',     //  0 - ChunkID
     0,0,0,0,            //  4 - ChunkSize (filesize-8)
     'W','A','V','E',    //  8 - Format
     'f','m','t',' ',    // 12 - SubChunkID
     16,0,0,0,           // 16 - SubChunk1ID  // 16 for PCM
     1,0,                // 20 - AudioFormat (1=16-bit)
     0,0,                // 22 - NumChannels
     0,0,0,0,            // 24 - SampleRate in Hz
     0,0,0,0,            // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
     0,0,                // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
     16,0,               // 34 - BitsPerSample
     'd','a','t','a',    // 36 - Subchunk2ID
     0,0,0,0             // 40 - Subchunk2Size
};

static inline void int2le32(unsigned char* buf, int32_t x)
{
  buf[0] = (x & 0xff);
  buf[1] = (x & 0xff00) >> 8;
  buf[2] = (x & 0xff0000) >> 16;
  buf[3] = (x & 0xff000000) >>24;
}

static inline void int2le24(unsigned char* buf, int32_t x)
{
  buf[0] = (x & 0xff);
  buf[1] = (x & 0xff00) >> 8;
  buf[2] = (x & 0xff0000) >> 16;
}

static inline void int2le16(unsigned char* buf, int16_t x)
{
  buf[0] = (x & 0xff);
  buf[1] = (x & 0xff00) >> 8;
}

/* 32KB should be enough */
static unsigned char wavbuffer[32*1024];
static unsigned char dspbuffer[32*1024];

void init_wav(char* filename)
{
    wavinfo.totalsamples = 0;

    wavinfo.fd = rb->creat(filename);

    if (wavinfo.fd >= 0)
    {
        /* Write WAV header - we go back and fill in the details at the end */
        rb->write(wavinfo.fd, wav_header, sizeof(wav_header));
    }
}


void close_wav(void)
{
    int filesize = rb->filesize(wavinfo.fd);
    int channels = (wavinfo.stereomode == STEREO_MONO) ? 1 : 2;
    int bps = 16; /* TODO */

    /* We assume 16-bit, Stereo */

    rb->lseek(wavinfo.fd,0,SEEK_SET);

    int2le32(wav_header+4, filesize-8); /* ChunkSize */

    int2le16(wav_header+22, channels);

    int2le32(wav_header+24, wavinfo.samplerate);

    int2le32(wav_header+28, wavinfo.samplerate * channels * (bps / 8)); /* ByteRate */

    int2le16(wav_header+32, channels * (bps / 8));

    int2le32(wav_header+40, filesize - 44);  /* Subchunk2Size */

    rb->write(wavinfo.fd, wav_header, sizeof(wav_header));

    rb->close(wavinfo.fd);
}

/* Returns buffer to malloc array. Only codeclib should need this. */
static void* codec_get_buffer(size_t *size)
{
   DEBUGF("codec_get_buffer(%d)\n",(int)size);
   *size = CODEC_SIZE;
   return codec_mallocbuf;
}

static int process_dsp(const void *ch1, const void *ch2, int count)
{
    const char *src[2] = { ch1, ch2 };
    int written_count = 0;
    char *dest = dspbuffer;
    
    while (count > 0)
    {
        int out_count = rb->dsp_output_count(ci.dsp, count);
        
        int inp_count = rb->dsp_input_count(ci.dsp, out_count);
        
        if (inp_count <= 0)
            break;
        
        if (inp_count > count)
            inp_count = count;
        
        out_count = rb->dsp_process(ci.dsp, dest, src, inp_count);
        
        if (out_count <= 0)
            break;
        
        written_count += out_count;
        dest += out_count * 4;
        
        count -= inp_count;
    }
    
    return written_count;
}

static inline int32_t clip_sample(int32_t sample)
{
    if ((int16_t)sample != sample)
        sample = 0x7fff ^ (sample >> 31);

    return sample;
}

/* Null output */
static void pcmbuf_insert_null(const void *ch1, const void *ch2, int count)
{
    if (use_dsp)
        process_dsp(ch1, ch2, count);

    /* Prevent idle poweroff */
    rb->reset_poweroff_timer();
}

/* WAV output or calculate crc32 of output*/
static void pcmbuf_insert_wav_checksum(const void *ch1, const void *ch2, int count)
{
    const int16_t* data1_16;
    const int16_t* data2_16;
    const int32_t* data1_32;
    const int32_t* data2_32;
    unsigned char* p = wavbuffer;
    const int scale = wavinfo.sampledepth - 15;
    const int dc_bias = 1 << (scale - 1);
    int channels = (wavinfo.stereomode == STEREO_MONO) ? 1 : 2;

    /* Prevent idle poweroff */
    rb->reset_poweroff_timer();

    if (use_dsp) {
        count = process_dsp(ch1, ch2, count);
        wavinfo.totalsamples += count;
        if (channels == 1)
        {
            unsigned char *s = dspbuffer, *d = dspbuffer;
            int c = count;
            while (c-- > 0)
            {
                *d++ = *s++;
                *d++ = *s++;
                s++;
                s++;
            }
        }
        if (checksum)
            crc32 = rb->crc_32(dspbuffer, count * 2 * channels, crc32);
        else
            rb->write(wavinfo.fd, dspbuffer, count * 2 * channels);
    }
    else
    { 
        if (wavinfo.sampledepth <= 16) {
            data1_16 = ch1;
            data2_16 = ch2;

            switch(wavinfo.stereomode)
            {
                case STEREO_INTERLEAVED:
                    while (count--) {
                        int2le16(p,*data1_16++);
                        p += 2;
                        int2le16(p,*data1_16++);
                        p += 2;
                    }
                    break;
 
                case STEREO_NONINTERLEAVED:
                    while (count--) {
                        int2le16(p,*data1_16++);
                        p += 2;
                        int2le16(p,*data2_16++);
                        p += 2;
                    }

                    break;

                case STEREO_MONO:
                    while (count--) {
                        int2le16(p,*data1_16++);
                        p += 2;
                    }
                    break;
            }
        } else {
            data1_32 = ch1;
            data2_32 = ch2;

            switch(wavinfo.stereomode)
            {
                case STEREO_INTERLEAVED:
                    while (count--) {
                        int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale));
                        p += 2;
                        int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale));
                        p += 2;
                    }
                    break;
 
                case STEREO_NONINTERLEAVED:
                    while (count--) {
                        int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale));
                        p += 2;
                        int2le16(p, clip_sample((*data2_32++ + dc_bias) >> scale));
                        p += 2;
                    }

                    break;

                case STEREO_MONO:
                    while (count--) {
                        int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale));
                        p += 2;
                    }
                    break;
            }
        }

        wavinfo.totalsamples += count;
        if (checksum)
            crc32 = rb->crc_32(wavbuffer, p - wavbuffer, crc32);
        else
            rb->write(wavinfo.fd, wavbuffer, p - wavbuffer);
    } /* else */
}

/* Set song position in WPS (value in ms). */
static void set_elapsed(unsigned long value)
{
    elapsed = value;
}


/* Read next <size> amount bytes from file buffer to <ptr>.
   Will return number of bytes read or 0 if end of file. */
static size_t read_filebuf(void *ptr, size_t size)
{
   if (ci.curpos > (off_t)track.filesize)
   {
       return 0;
   } else {
       /* TODO: Don't read beyond end of buffer */
       rb->memcpy(ptr, audiobuf + ci.curpos, size);
       ci.curpos += size;
       return size;
   }
}


/* Request pointer to file buffer which can be used to read
   <realsize> amount of data. <reqsize> tells the buffer system
   how much data it should try to allocate. If <realsize> is 0,
   end of file is reached. */
static void* request_buffer(size_t *realsize, size_t reqsize)
{
    *realsize = MIN(track.filesize-ci.curpos,reqsize);

    return (audiobuf + ci.curpos);
}


/* Advance file buffer position by <amount> amount of bytes. */
static void advance_buffer(size_t amount)
{
    ci.curpos += amount;
}


/* Advance file buffer to a pointer location inside file buffer. */
static void advance_buffer_loc(void *ptr)
{
    ci.curpos = ptr - audiobuf;
}


/* Seek file buffer to position <newpos> beginning of file. */
static bool seek_buffer(size_t newpos)
{
    ci.curpos = newpos;
    return true;
}


/* Codec should call this function when it has done the seeking. */
static void seek_complete(void)
{
    /* Do nothing */
}

/* Request file change from file buffer. Returns true is next
   track is available and changed. If return value is false,
   codec should exit immediately with PLUGIN_OK status. */
static bool request_next_track(void)
{
    /* We are only decoding a single track */
    return false;
}


/* Free the buffer area of the current codec after its loaded */
static void discard_codec(void)
{
    /* ??? */
}


static void set_offset(size_t value)
{
    /* ??? */
    (void)value;
}


/* Configure different codec buffer parameters. */
static void configure(int setting, intptr_t value)
{
    if (use_dsp)
        rb->dsp_configure(ci.dsp, setting, value);
    switch(setting)
    {
        case DSP_SWITCH_FREQUENCY:
        case DSP_SET_FREQUENCY:
            DEBUGF("samplerate=%d\n",(int)value);
            wavinfo.samplerate = (int)value;
            break;

        case DSP_SET_SAMPLE_DEPTH:
            DEBUGF("sampledepth = %d\n",(int)value);
            wavinfo.sampledepth=(int)value;
            break;

        case DSP_SET_STEREO_MODE:
            DEBUGF("Stereo mode = %d\n",(int)value);
            wavinfo.stereomode=(int)value;
            break;
    }

}

static void init_ci(void)
{
    /* --- Our "fake" implementations of the codec API functions. --- */

    ci.codec_get_buffer = codec_get_buffer;

    if (wavinfo.fd >= 0 || checksum) {
        ci.pcmbuf_insert = pcmbuf_insert_wav_checksum;
    } else {
        ci.pcmbuf_insert = pcmbuf_insert_null;
    }

    ci.set_elapsed = set_elapsed;
    ci.read_filebuf = read_filebuf;
    ci.request_buffer = request_buffer;
    ci.advance_buffer = advance_buffer;
    ci.advance_buffer_loc = advance_buffer_loc;
    ci.seek_buffer = seek_buffer;
    ci.seek_complete = seek_complete;
    ci.request_next_track = request_next_track;
    ci.discard_codec = discard_codec;
    ci.set_offset = set_offset;
    ci.configure = configure;
    ci.dsp = (struct dsp_config *)rb->dsp_configure(NULL, DSP_MYDSP,
                                                    CODEC_IDX_AUDIO);

    /* --- "Core" functions --- */

    /* kernel/ system */
    ci.sleep = rb->sleep;
    ci.yield = rb->yield;

    /* strings and memory */
    ci.strcpy = rb->strcpy;
    ci.strlen = rb->strlen;
    ci.strcmp = rb->strcmp;
    ci.strcat = rb->strcat;
    ci.memset = rb->memset;
    ci.memcpy = rb->memcpy;
    ci.memmove = rb->memmove;
    ci.memcmp = rb->memcmp;
    ci.memchr = rb->memchr;
    ci.strcasestr = rb->strcasestr;
#if defined(DEBUG) || defined(SIMULATOR)
    ci.debugf = rb->debugf;
#endif
#ifdef ROCKBOX_HAS_LOGF
    ci.logf = rb->logf;
#endif

    ci.qsort = rb->qsort;
    ci.global_settings = rb->global_settings;

#ifdef RB_PROFILE
    ci.profile_thread = rb->profile_thread;
    ci.profstop = rb->profstop;
    ci.profile_func_enter = rb->profile_func_enter;
    ci.profile_func_exit = rb->profile_func_exit;
#endif

#if NUM_CORES > 1
    ci.cpucache_invalidate = rb->cpucache_invalidate;
    ci.cpucache_flush = rb->cpucache_flush;
#endif

#if NUM_CORES > 1
    ci.create_thread = rb->create_thread;
    ci.thread_thaw = rb->thread_thaw;
    ci.thread_wait = rb->thread_wait;
    ci.semaphore_init = rb->semaphore_init;
    ci.semaphore_wait = rb->semaphore_wait;
    ci.semaphore_release = rb->semaphore_release;
#endif

#ifdef CPU_ARM
    ci.__div0 = rb->__div0;
#endif
}

static void codec_thread(void)
{
    const char* codecname;
    int res;

    codecname = rb->get_codec_filename(track.id3.codectype);

    /* Load the codec and start decoding. */
    res = rb->codec_load_file(codecname,&ci);

    /* Signal to the main thread that we are done */
    endtick = *rb->current_tick;
    codec_playing = false;
}

static enum plugin_status test_track(const char* filename)
{
    size_t n;
    int fd;
    enum plugin_status res = PLUGIN_ERROR;
    long starttick;
    long ticks;
    unsigned long speed;
    unsigned long duration;
    const char* ch;

    /* Display filename (excluding any path)*/
    ch = rb->strrchr(filename, '/');
    if (ch==NULL)
       ch = filename; 
    else
       ch++;

    rb->snprintf(str,sizeof(str),"%s",ch);
    log_text(str,true);

    log_text("Loading...",false);

    fd = rb->open(filename,O_RDONLY);
    if (fd < 0)
    {
        log_text("Cannot open file",true);
        goto exit;
    }

    track.filesize = rb->filesize(fd);

    /* Clear the id3 struct */
    rb->memset(&track.id3, 0, sizeof(struct mp3entry));

    if (!rb->get_metadata(&(track.id3), fd, filename))
    {
        log_text("Cannot read metadata",true);
        goto exit;
    }
    
    if (track.filesize > audiosize)
    {
        log_text("File too large",true);
        goto exit;
    }

    n = rb->read(fd, audiobuf, track.filesize);

    if (n != track.filesize)
    {
        log_text("Read failed.",true);
        goto exit;
    }

    /* Initialise the function pointers in the codec API */
    init_ci();

    /* Prepare the codec struct for playing the whole file */
    ci.filesize = track.filesize;
    ci.id3 = &track.id3;
    ci.taginfo_ready = &taginfo_ready;
    ci.curpos = 0;
    ci.stop_codec = false;
    ci.new_track = 0;
    ci.seek_time = 0;

    if (use_dsp)
        rb->dsp_configure(ci.dsp, DSP_RESET, 0);

    if (checksum)
        crc32 = 0xffffffff;

    starttick = *rb->current_tick;

    codec_playing = true;

    rb->codec_thread_do_callback(codec_thread, NULL);

    /* Wait for codec thread to die */
    while (codec_playing)
    {
        rb->sleep(HZ);
        rb->snprintf(str,sizeof(str),"%d of %d",elapsed,(int)track.id3.length);
        log_text(str,false);
    }
    ticks = endtick - starttick;

    /* Be sure it is done */
    rb->codec_thread_do_callback(NULL, NULL);

    log_text(str,true);
    
    if (checksum)
    {
        rb->snprintf(str, sizeof(str), "CRC32 - %x", (unsigned)crc32);
        log_text(str,true);
    }
    else if (wavinfo.fd < 0) 
    {
        /* Display benchmark information */
        rb->snprintf(str,sizeof(str),"Decode time - %d.%02ds",(int)ticks/100,(int)ticks%100);
        log_text(str,true);

        duration = track.id3.length / 10;
        rb->snprintf(str,sizeof(str),"File duration - %d.%02ds",(int)duration/100,(int)duration%100);
        log_text(str,true);

        if (ticks > 0)
            speed = duration * 10000 / ticks;
        else
            speed = 0;

        rb->snprintf(str,sizeof(str),"%d.%02d%% realtime",(int)speed/100,(int)speed%100);
        log_text(str,true);
        
#ifndef SIMULATOR
        /* show effective clockrate in MHz needed for realtime decoding */
        if (speed > 0)
        {
            speed = CPUFREQ_MAX / speed;
            rb->snprintf(str,sizeof(str),"%d.%02dMHz needed for realtime",
            (int)speed/100,(int)speed%100);
            log_text(str,true);
        }   
#endif
    }

    res = PLUGIN_OK;

exit:
    rb->backlight_on();

    if (fd >= 0)
    {
        rb->close(fd);
    }

    return res;
}

/* plugin entry point */
enum plugin_status plugin_start(const void* parameter)
{
    int result, selection = 0;
    enum plugin_status res = PLUGIN_OK;
    int scandir;
    struct dirent *entry;
    DIR* dir;
    char* ch;
    char dirpath[MAX_PATH];
    char filename[MAX_PATH];

    if (parameter == NULL)
    {
        rb->splash(HZ*2, "No File");
        return PLUGIN_ERROR;
    }

    codec_mallocbuf = rb->plugin_get_audio_buffer(&audiosize);
    audiobuf = SKIPBYTES(codec_mallocbuf, CODEC_SIZE);
    audiosize -= CODEC_SIZE;

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(true);
#endif
    rb->lcd_clear_display();
    rb->lcd_update();

    enum
    {
        SPEED_TEST = 0,
        SPEED_TEST_DIR,
        WRITE_WAV,
        SPEED_TEST_WITH_DSP,
        SPEED_TEST_DIR_WITH_DSP,
        WRITE_WAV_WITH_DSP,
        CHECKSUM,
        CHECKSUM_DIR,
        QUIT,
    };

    MENUITEM_STRINGLIST(
        menu, "test_codec", NULL,
        "Speed test",
        "Speed test folder",
        "Write WAV",
        "Speed test with DSP",
        "Speed test folder with DSP",
        "Write WAV with DSP",
        "Checksum",
        "Checksum folder",
        "Quit",
    );

show_menu:
    rb->lcd_clear_display();

    result = rb->do_menu(&menu, &selection, NULL, false);

    if (result == QUIT)
    {
        res = PLUGIN_OK;
        goto exit;
    }

    scandir = 0;

    if ((checksum = (result == CHECKSUM || result == CHECKSUM_DIR)))
        result -= 6;

    if ((use_dsp = ((result >= SPEED_TEST_WITH_DSP)
                   && (result <= WRITE_WAV_WITH_DSP)))) {
        result -= 3;
    }
    if (result == SPEED_TEST) {
        wavinfo.fd = -1;
        log_init(false);
    } else if (result == SPEED_TEST_DIR) {
        wavinfo.fd = -1;
        scandir = 1;

        /* Only create a log file when we are testing a folder */
        if (!log_init(true)) {
            rb->splash(HZ*2, "Cannot create logfile");
            res = PLUGIN_ERROR;
            goto exit;
        }
    } else if (result == WRITE_WAV) {
        log_init(false);
        init_wav("/test.wav");
        if (wavinfo.fd < 0) {
            rb->splash(HZ*2, "Cannot create /test.wav");
            res = PLUGIN_ERROR;
            goto exit;
        }
    } else if (result == MENU_ATTACHED_USB) {
        res = PLUGIN_USB_CONNECTED;
        goto exit;
    } else if (result < 0) {
        res = PLUGIN_OK;
        goto exit;
    }

    if (scandir) {
        /* Test all files in the same directory as the file selected by the
           user */

        rb->strlcpy(dirpath,parameter,sizeof(dirpath));
        ch = rb->strrchr(dirpath,'/');
        ch[1]=0;

        DEBUGF("Scanning directory \"%s\"\n",dirpath);
        dir = rb->opendir(dirpath);
        if (dir) {
            entry = rb->readdir(dir);
            while (entry) {
                if (!(entry->attribute & ATTR_DIRECTORY)) {
                    rb->snprintf(filename,sizeof(filename),"%s%s",dirpath,entry->d_name);
                    test_track(filename);
                    log_text("", true);
                }

                /* Read next entry */
                entry = rb->readdir(dir);
            }
            
            rb->closedir(dir);
        }
    } else {
        /* Just test the file */
        res = test_track(parameter);

        /* Close WAV file (if there was one) */
        if (wavinfo.fd >= 0) {
            close_wav();
            log_text("Wrote /test.wav",true);
        }
        while (rb->button_get(true) != TESTCODEC_EXITBUTTON);
    }
    goto show_menu;

exit:
    log_close();

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(false);
#endif

    return res;
}
/span>,0x1}, {0x1,0x1,0x1,0x1,0x0,0x0,0x1,0x1,0x1,0x1}, {0x1,0x1,0x2,0x1,0x0,0x0,0x1,0x2,0x1,0x1}, {0x1,0x2,0x0,0x2,0x2,0x2,0x2,0x0,0x2,0x1}, {0x0,0x1,0x2,0x0,0x0,0x0,0x0,0x2,0x1,0x0}, {0x0,0x0,0x1,0x2,0x2,0x2,0x2,0x1,0x0,0x0} }, { /* level 3 */ {0x3,0x3,0x3,0x3,0x0,0x0,0x2,0x2,0x2,0x2}, {0x3,0x23,0x23,0x3,0x0,0x0,0x2,0x22,0x22,0x2}, {0x3,0x3,0x3,0x3,0x0,0x0,0x2,0x2,0x2,0x2}, {0x0,0x0,0x0,0x0,0x37,0x37,0x0,0x0,0x0,0x0}, {0x0,0x0,0x0,0x0,0x37,0x37,0x0,0x0,0x0,0x0}, {0x5,0x5,0x5,0x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} }, { /* level 27 (Frog) by ts-x */ {0x0,0x5,0x25,0x0,0x0,0x0,0x0,0x25,0x5,0x0}, {0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5}, {0x25,0x0,0x0,0x5,0x6,0x6,0x5,0x0,0x0,0x25}, {0x5,0x0,0x3,0x0,0x6,0x6,0x0,0x3,0x0,0x5}, {0x5,0x0,0x31,0x0,0x6,0x6,0x0,0x31,0x0,0x5}, {0x5,0x0,0x0,0x5,0x6,0x6,0x5,0x0,0x0,0x5}, {0x5,0x5,0x5,0x35,0x0,0x0,0x35,0x5,0x5,0x5}, {0x0,0x25,0x5,0x0,0x4,0x4,0x0,0x5,0x25,0x0} }, { /* level 28 (DigDug) by ts-x */ {0x35,0x5,0x5,0x25,0x0,0x25,0x25,0x5,0x5,0x35}, {0x6,0x0,0x0,0x6,0x0,0x6,0x6,0x0,0x0,0x6}, {0x7,0x0,0x37,0x37,0x0,0x37,0x37,0x7,0x0,0x7}, {0x7,0x0,0x7,0x0,0x0,0x0,0x7,0x7,0x7,0x7}, {0x4,0x4,0x4,0x24,0x0,0x24,0x4,0x0,0x0,0x4}, {0x4,0x4,0x0,0x0,0x0,0x4,0x4,0x0,0x4,0x4}, {0x24,0x24,0x4,0x4,0x4,0x4,0x0,0x0,0x24,0x4}, {0x1,0x1,0x1,0x1,0x1,0x1,0x21,0x21,0x1,0x1} }, { /* level 29 UK Flag by Seth Opgenorth */ {0x32,0x0,0x3,0x3,0x2,0x2,0x3,0x3,0x0,0x32}, {0x0,0x2,0x0,0x3,0x32,0x22,0x33,0x0,0x32,0x0}, {0x33,0x0,0x22,0x0,0x2,0x2,0x0,0x2,0x0,0x33}, {0x22,0x32,0x2,0x2,0x2,0x2,0x2,0x2,0x22,0x2}, {0x3,0x0,0x0,0x32,0x22,0x2,0x2,0x0,0x0,0x3}, {0x23,0x0,0x32,0x0,0x32,0x2,0x0,0x2,0x0,0x3}, {0x0,0x2,0x0,0x3,0x2,0x2,0x3,0x0,0x22,0x0}, {0x32,0x0,0x3,0x23,0x2,0x2,0x23,0x33,0x0,0x32} }, { /* level 30 Win-Logo by Seth Opgenorth */ {0x0,0x0,0x0,0x22,0x0,0x0,0x0,0x0,0x0,0x0}, {0x0,0x0,0x32,0x2,0x2,0x25,0x0,0x5,0x0,0x0}, {0x0,0x0,0x2,0x22,0x2,0x5,0x5,0x35,0x0,0x0}, {0x0,0x0,0x2,0x1,0x2,0x5,0x5,0x25,0x0,0x0}, {0x0,0x0,0x21,0x1,0x1,0x36,0x7,0x26,0x0,0x0}, {0x0,0x0,0x1,0x1,0x1,0x6,0x6,0x6,0x0,0x0}, {0x0,0x0,0x21,0x0,0x21,0x6,0x6,0x26,0x0,0x0}, {0x0,0x0,0x0,0x0,0x0,0x0,0x6,0x0,0x0,0x0} }, { /* level 31 Color wave/V by Seth Opgenorth */ {0x25,0x34,0x2,0x31,0x33,0x23,0x1,0x2,0x34,0x5}, {0x3,0x5,0x24,0x2,0x1,0x1,0x2,0x4,0x5,0x3}, {0x1,0x3,0x5,0x4,0x2,0x2,0x4,0x5,0x3,0x1}, {0x2,0x1,0x33,0x35,0x4,0x4,0x5,0x33,0x1,0x22}, {0x31,0x22,0x1,0x3,0x5,0x25,0x3,0x1,0x2,0x31}, {0x3,0x1,0x2,0x1,0x3,0x3,0x1,0x2,0x21,0x3}, {0x5,0x23,0x1,0x32,0x1,0x1,0x2,0x1,0x3,0x5}, {0x4,0x35,0x3,0x1,0x2,0x22,0x31,0x3,0x5,0x4} }, { /* level 32 Sweedish Flag by Seth Opgenorth */ {0x3,0x3,0x3,0x36,0x3,0x3,0x3,0x3,0x3,0x3}, {0x3,0x3,0x3,0x36,0x3,0x3,0x3,0x3,0x3,0x3}, {0x3,0x3,0x3,0x36,0x3,0x3,0x3,0x3,0x3,0x3}, {0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36}, {0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36,0x36}, {0x3,0x3,0x3,0x36,0x3,0x3,0x3,0x3,0x3,0x3}, {0x3,0x3,0x3,0x36,0x3,0x3,0x3,0x3,0x3,0x3}, {0x3,0x3,0x3,0x36,0x3,0x3,0x3,0x3,0x3,0x3} }, { /* level 33 Color Pyramid by Seth Opgenorth */ {0x0,0x0,0x0,0x0,0x2,0x2,0x0,0x0,0x0,0x0}, {0x0,0x0,0x0,0x4,0x24,0x4,0x24,0x0,0x0,0x0}, {0x0,0x0,0x23,0x3,0x3,0x3,0x23,0x3,0x0,0x0}, {0x0,0x0,0x25,0x5,0x25,0x35,0x5,0x35,0x0,0x0}, {0x0,0x36,0x6,0x6,0x6,0x6,0x26,0x6,0x6,0x0}, {0x0,0x7,0x7,0x7,0x7,0x25,0x27,0x7,0x27,0x0}, {0x2,0x2,0x22,0x2,0x2,0x2,0x22,0x2,0x2,0x2}, {0x21,0x1,0x1,0x31,0x1,0x21,0x1,0x1,0x31,0x1} }, { /* level 34 Rhombus by Seth Opgenorth */ {0x0,0x0,0x0,0x0,0x3,0x33,0x0,0x0,0x0,0x0}, {0x0,0x0,0x0,0x3,0x32,0x22,0x23,0x0,0x0,0x0}, {0x0,0x0,0x3,0x2,0x24,0x4,0x2,0x23,0x0,0x0}, {0x26,0x3,0x2,0x4,0x5,0x5,0x4,0x22,0x3,0x6}, {0x36,0x3,0x2,0x34,0x5,0x5,0x4,0x2,0x3,0x36}, {0x0,0x0,0x3,0x2,0x4,0x34,0x2,0x23,0x0,0x0}, {0x0,0x0,0x0,0x23,0x2,0x2,0x3,0x0,0x0,0x0}, {0x0,0x0,0x0,0x0,0x3,0x33,0x0,0x0,0x0,0x0} }, { /* level 35 PacMan Ghost by Seth Opgenorth */ {0x0,0x0,0x0,0x0,0x2,0x32,0x2,0x0,0x0,0x0}, {0x0,0x0,0x0,0x2,0x2,0x2,0x2,0x2,0x0,0x0}, {0x0,0x0,0x2,0x24,0x4,0x2,0x4,0x4,0x32,0x0}, {0x0,0x0,0x2,0x24,0x0,0x22,0x24,0x0,0x22,0x0}, {0x0,0x0,0x2,0x2,0x2,0x2,0x2,0x2,0x2,0x0}, {0x0,0x0,0x2,0x2,0x2,0x2,0x2,0x2,0x2,0x0}, {0x0,0x0,0x2,0x32,0x2,0x2,0x22,0x2,0x32,0x0}, {0x0,0x0,0x0,0x22,0x0,0x32,0x0,0x22,0x0,0x0} }, { /* level 36 Star by Seth Opgenorth */ {0x3,0x4,0x3,0x4,0x6,0x24,0x3,0x24,0x3,0x0}, {0x24,0x0,0x0,0x6,0x6,0x6,0x0,0x0,0x4,0x0}, {0x3,0x26,0x6,0x2,0x6,0x2,0x6,0x26,0x23,0x0}, {0x4,0x0,0x6,0x6,0x36,0x6,0x6,0x0,0x4,0x0}, {0x3,0x0,0x0,0x26,0x6,0x26,0x0,0x0,0x33,0x0}, {0x34,0x0,0x6,0x6,0x0,0x6,0x6,0x0,0x4,0x0}, {0x3,0x26,0x6,0x0,0x0,0x0,0x6,0x6,0x3,0x0}, {0x4,0x3,0x4,0x23,0x24,0x3,0x4,0x33,0x4,0x0} }, { /* level 37 It's 8-Bit by Seth Opgenorth */ {0x26,0x26,0x6,0x6,0x5,0x6,0x26,0x6,0x26,0x6}, {0x2,0x2,0x22,0x3,0x3,0x0,0x0,0x0,0x4,0x0}, {0x2,0x0,0x2,0x33,0x3,0x3,0x5,0x0,0x24,0x0}, {0x32,0x2,0x2,0x33,0x0,0x23,0x0,0x4,0x4,0x4}, {0x2,0x22,0x2,0x3,0x3,0x0,0x5,0x4,0x4,0x24}, {0x2,0x0,0x2,0x23,0x0,0x3,0x25,0x0,0x4,0x0}, {0x22,0x2,0x2,0x3,0x23,0x0,0x5,0x0,0x4,0x0}, {0x6,0x26,0x6,0x36,0x6,0x36,0x6,0x6,0x6,0x6} }, { /* level 38 Linux by Seth Opgenorth */ {0x27,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}, {0x7,0x0,0x0,0x0,0x33,0x0,0x23,0x0,0x0,0x0}, {0x7,0x32,0x0,0x0,0x3,0x0,0x23,0x6,0x0,0x6}, {0x37,0x0,0x0,0x0,0x23,0x0,0x3,0x6,0x0,0x26}, {0x7,0x22,0x24,0x0,0x3,0x33,0x3,0x0,0x26,0x0}, {0x37,0x22,0x24,0x24,0x4,0x0,0x0,0x0,0x26,0x0}, {0x7,0x2,0x4,0x0,0x4,0x0,0x0,0x6,0x0,0x26}, {0x7,0x27,0x4,0x0,0x34,0x0,0x0,0x6,0x0,0x26} }, { /* level 39 Colorful Squares by Seth Opgenorth*/ {0x0,0x4,0x4,0x4,0x4,0x4,0x4,0x4,0x4,0x0}, {0x0,0x4,0x5,0x5,0x5,0x5,0x5,0x5,0x4,0x0}, {0x0,0x4,0x5,0x3,0x3,0x3,0x3,0x5,0x4,0x0}, {0x0,0x4,0x5,0x3,0x4,0x4,0x3,0x5,0x4,0x0}, {0x0,0x4,0x5,0x3,0x4,0x4,0x3,0x5,0x4,0x0}, {0x0,0x4,0x5,0x3,0x3,0x3,0x3,0x5,0x4,0x0}, {0x0,0x4,0x5,0x5,0x5,0x5,0x5,0x5,0x4,0x0}, {0x0,0x4,0x4,0x4,0x4,0x4,0x4,0x4,0x4,0x0} }, { /* TheEnd */ {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}, {0x32,0x32,0x36,0x0,0x0,0x36,0x34,0x34,0x0,0x0}, {0x32,0x0,0x36,0x36,0x0,0x36,0x34,0x0,0x34,0x0}, {0x32,0x32,0x36,0x36,0x0,0x36,0x34,0x0,0x34,0x0}, {0x32,0x32,0x36,0x0,0x36,0x36,0x34,0x0,0x34,0x0}, {0x32,0x0,0x36,0x0,0x36,0x36,0x34,0x0,0x34,0x0}, {0x32,0x32,0x36,0x0,0x0,0x36,0x34,0x34,0x0,0x0}, {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0} } }; /* * * Enums and structs * */ enum power_types { POWER_TYPE_LIFE_GAIN = 0, POWER_TYPE_LIFE_LOSS, POWER_TYPE_PADDLE_STICKY, POWER_TYPE_PADDLE_SHOOTER, POWER_TYPE_PADDLE_NORMAL, POWER_TYPE_PADDLE_FLIP, POWER_TYPE_EXTRA_BALL, POWER_TYPE_PADDLE_LONG, POWER_TYPE_PADDLE_SHORT, NUMBER_OF_POWERUPS, }; #define POWERUP_HEIGHT FIXED3(BMPHEIGHT_brickmania_powerups/NUMBER_OF_POWERUPS) /* Increasing this value makes the game with less powerups */ #define POWER_RAND (NUMBER_OF_POWERUPS + 15) enum difficulty_options { EASY, NORMAL }; enum game_state { ST_READY, ST_START, ST_PAUSE }; enum paddle_type { PADDLE_TYPE_NORMAL = 0, PADDLE_TYPE_STICKY, PADDLE_TYPE_SHOOTER, }; enum intersection { INTERSECTION_TOP, INTERSECTION_BOTTOM, INTERSECTION_LEFT, INTERSECTION_RIGHT, INTERSECTION_ALL, }; struct brick { bool used; /* Is the brick still in play? */ int color; int hits; /* How many hits can this brick take? */ int hiteffect; }; struct ball { /* pos_x and y store the current center position of the ball */ int pos_x; int pos_y; /* tempx and tempy store an absolute position the ball should be in. If * they are equal to 0, they are not used when positioning the ball. */ int tempx; int tempy; /* speedx and speedy store the current speed of the ball */ int speedx; int speedy; bool glue; /* Is the ball stuck to the paddle? */ }; struct fire { int top; /* This stores the fire y position, it is a fixed point num */ int x_pos; /* This stores the fire x position, it is a whole number */ }; struct power { int top; /* This stores the powerup y position, it is a fixed point num */ int x_pos; /* This stores the (middle of) powerup x position, it is a whole number */ enum power_types type; /* This stores the powerup type */ }; struct point { int x; int y; }; struct line { struct point p1; struct point p2; }; struct rect { struct point top_left; struct point bottom_right; }; /* * * Globals * */ static enum game_state game_state; static int pad_pos_x; static int life; static int score,vscore; static bool flip_sides; static int level; static int brick_on_board; static int used_balls; static int used_fires; static int used_powers; static int difficulty = NORMAL; static int pad_width; static int flip_sides_delay; static bool resume = false; static bool resume_file = false; static struct brick brick[NUM_BRICKS_ROWS][NUM_BRICKS_COLS]; static struct ball ball[MAX_BALLS]; static struct fire fire[MAX_FIRES]; static struct power power[MAX_POWERS]; static enum paddle_type paddle_type; static struct configdata config[] = { {TYPE_INT, 0, 1, { .int_p = &difficulty }, "difficulty", NULL}, }; static struct highscore highscores[NUM_SCORES]; /* * * Functions * */ /* * check_lines: * This is based off an explanation and expanded math presented by Paul Bourke: * http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/ * * It takes two lines as inputs and returns 1 if they intersect, 0 if they do * not. hitp returns the point where the two lines intersected. * * This function expects fixed point inputs with a precision of 3. When a * collision occurs hitp is updated with a fixed point location (precision 3) * where the collision happened. The internal calculations are fixed * point with a 7 bit fractional precision. * * If you choose 10 bits of precision a screen size of about 640x480 is the * largest this can go. 7 bits allows for an accurate intersection calculation * with a line length of about 64 and a rougher line lenght of 128 which is * larger than any target currently needs (the pad is the longest line and it * only needs an accuracy of 2^4 at most to figure out which section of the pad * the ball hit). A precision of 7 gives breathing room for larger screens. * Longer line sizes that need accurate intersection points will need more * precision, but will decrease the maximum screen resolution. */ #define LINE_PREC 7 static int check_lines(struct line *line1, struct line *line2, struct point *hitp) { /* Introduction: * This code is based on the solution of these two input equations: * Pa = P1 + ua (P2-P1) * Pb = P3 + ub (P4-P3) * * Where line one is composed of points P1 and P2 and line two is composed * of points P3 and P4. * * ua/b is the fractional value you can multiply the x and y legs of the * triangle formed by each line to find a point on the line. * * The two equations can be expanded to their x/y components: * Pa.x = p1.x + ua(p2.x - p1.x) * Pa.y = p1.y + ua(p2.y - p1.y) * * Pb.x = p3.x + ub(p4.x - p3.x) * Pb.y = p3.y + ub(p4.y - p3.y) * * When Pa.x == Pb.x and Pa.y == Pb.y the lines intersect so you can come * up with two equations (one for x and one for y): * * p1.x + ua(p2.x - p1.x) = p3.x + ub(p4.x - p3.x) * p1.y + ua(p2.y - p1.y) = p3.y + ub(p4.y - p3.y) * * ua and ub can then be individually solved for. This results in the * equations used in the following code. */ /* Denominator for ua and ub are the same so store this calculation */ int d = FIXED3_MUL((line2->p2.y - line2->p1.y),(line1->p2.x-line1->p1.x)) -FIXED3_MUL((line2->p2.x - line2->p1.x),(line1->p2.y-line1->p1.y)); /* n_a and n_b are calculated as seperate values for readability */ int n_a = FIXED3_MUL((line2->p2.x - line2->p1.x),(line1->p1.y-line2->p1.y)) -FIXED3_MUL((line2->p2.y - line2->p1.y),(line1->p1.x-line2->p1.x)); int n_b = FIXED3_MUL((line1->p2.x - line1->p1.x),(line1->p1.y-line2->p1.y)) -FIXED3_MUL((line1->p2.y - line1->p1.y),(line1->p1.x-line2->p1.x)); /* Make sure there is not a division by zero - this also indicates that * the lines are parallel. * * If n_a and n_b were both equal to zero the lines would be on top of each * other (coincidental). This check is not done because it is not * necessary for this implementation (the parallel check accounts for this). */ if(d == 0) return 0; /* Calculate the intermediate fractional point that the lines potentially * intersect. */ int ua = (n_a << LINE_PREC)/d; int ub = (n_b << LINE_PREC)/d; /* The fractional point will be between 0 and 1 inclusive if the lines * intersect. If the fractional calculation is larger than 1 or smaller * than 0 the lines would need to be longer to intersect. */ if(ua >=0 && ua <= (1<<LINE_PREC) && ub >= 0 && ub <= (1<<LINE_PREC)) { hitp->x = line1->p1.x + ((ua * (line1->p2.x - line1->p1.x))>>LINE_PREC); hitp->y = line1->p1.y + ((ua * (line1->p2.y - line1->p1.y))>>LINE_PREC); return 1; } return 0; } static int check_rect(struct line *line, struct rect *rect, enum intersection intersection, struct point *hitp) { struct line edge; switch (intersection) { case INTERSECTION_TOP: { edge.p1.x = rect->top_left.x; edge.p1.y = rect->top_left.y; edge.p2.x = rect->bottom_right.x; edge.p2.y = rect->top_left.y; break; } case INTERSECTION_BOTTOM: { edge.p1.x = rect->top_left.x; edge.p1.y = rect->bottom_right.y; edge.p2.x = rect->bottom_right.x; edge.p2.y = rect->bottom_right.y; break; } case INTERSECTION_LEFT: { edge.p1.x = rect->top_left.x; edge.p1.y = rect->top_left.y; edge.p2.x = rect->top_left.x; edge.p2.y = rect->bottom_right.y; break; } case INTERSECTION_RIGHT: { edge.p1.x = rect->bottom_right.x; edge.p1.y = rect->top_left.y; edge.p2.x = rect->bottom_right.x; edge.p2.y = rect->bottom_right.y; break; } case INTERSECTION_ALL: /* Test hit on all edges */ { return (check_rect(line, rect, INTERSECTION_TOP, hitp) || check_rect(line, rect, INTERSECTION_BOTTOM, hitp) || check_rect(line, rect, INTERSECTION_LEFT, hitp) || check_rect(line, rect, INTERSECTION_RIGHT, hitp)); } } return check_lines(line, &edge, hitp); } static void brickmania_init_game(bool new_game) { int i,j; pad_pos_x = GAMESCREEN_WIDTH/2 - PAD_WIDTH/2; for(i=0;i<MAX_BALLS;i++) { ball[i].speedx = 0; ball[i].speedy = 0; ball[i].tempy = 0; ball[i].tempx = 0; ball[i].pos_y = ON_PAD_POS_Y; ball[i].pos_x = GAMESCREEN_WIDTH/2; ball[i].glue = false; } used_balls = 1; used_fires = 0; used_powers = 0; game_state = ST_READY; paddle_type = PADDLE_TYPE_NORMAL; pad_width = PAD_WIDTH; flip_sides = false; flip_sides_delay = FLIP_SIDES_DELAY; if (new_game) { brick_on_board=0; /* add one life per achieved level */ if (difficulty==EASY && life<2) { score+=SCORE_LEVEL_COMPLETED; life++; } } for(i=0;i<NUM_BRICKS_ROWS;i++) { for(j=0;j<NUM_BRICKS_COLS;j++) { if (new_game) { brick[i][j].hits=levels[level][i][j]>=10? levels[level][i][j]/16-1:0; brick[i][j].hiteffect=0; brick[i][j].used=!(levels[level][i][j]==0); brick[i][j].color=(levels[level][i][j]>=10? levels[level][i][j]%16: levels[level][i][j])-1; if (levels[level][i][j]!=0) brick_on_board++; } } } } static void brickmania_loadgame(void) { int fd; resume = false; /* open game file */ fd = rb->open(SAVE_FILE, O_RDONLY); if(fd < 0) return; /* read in saved game */ if((rb->read(fd, &pad_pos_x, sizeof(pad_pos_x)) <= 0) || (rb->read(fd, &life, sizeof(life)) <= 0) || (rb->read(fd, &game_state, sizeof(game_state)) <= 0) || (rb->read(fd, &paddle_type, sizeof(paddle_type)) <= 0) || (rb->read(fd, &score, sizeof(score)) <= 0) || (rb->read(fd, &flip_sides, sizeof(flip_sides)) <= 0) || (rb->read(fd, &level, sizeof(level)) <= 0) || (rb->read(fd, &brick_on_board, sizeof(brick_on_board)) <= 0) || (rb->read(fd, &used_balls, sizeof(used_balls)) <= 0) || (rb->read(fd, &used_fires, sizeof(used_fires)) <= 0) || (rb->read(fd, &used_powers, sizeof(used_powers)) <= 0) || (rb->read(fd, &pad_width, sizeof(pad_width)) <= 0) || (rb->read(fd, &flip_sides_delay, sizeof(flip_sides_delay)) <= 0) || (rb->read(fd, &brick, sizeof(brick)) <= 0) || (rb->read(fd, &ball, sizeof(ball)) <= 0) || (rb->read(fd, &fire, sizeof(fire)) <= 0) || (rb->read(fd, &power, sizeof(power)) <= 0)) { rb->splash(HZ/2, "Failed to load game"); } else { vscore = score; resume = true; } rb->close(fd); return; } static void brickmania_savegame(void) { int fd; /* write out the game state to the save file */ fd = rb->open(SAVE_FILE, O_WRONLY|O_CREAT, 0666); if(fd < 0) return; if ((rb->write(fd, &pad_pos_x, sizeof(pad_pos_x)) <= 0) || (rb->write(fd, &life, sizeof(life)) <= 0) || (rb->write(fd, &game_state, sizeof(game_state)) <= 0) || (rb->write(fd, &paddle_type, sizeof(paddle_type)) <= 0) || (rb->write(fd, &score, sizeof(score)) <= 0) || (rb->write(fd, &flip_sides, sizeof(flip_sides)) <= 0) || (rb->write(fd, &level, sizeof(level)) <= 0) || (rb->write(fd, &brick_on_board, sizeof(brick_on_board)) <= 0) || (rb->write(fd, &used_balls, sizeof(used_balls)) <= 0) || (rb->write(fd, &used_fires, sizeof(used_fires)) <= 0) || (rb->write(fd, &used_powers, sizeof(used_powers)) <= 0) || (rb->write(fd, &pad_width, sizeof(pad_width)) <= 0) || (rb->write(fd, &flip_sides_delay, sizeof(flip_sides_delay)) <= 0) || (rb->write(fd, &brick, sizeof(brick)) <= 0) || (rb->write(fd, &ball, sizeof(ball)) <= 0) || (rb->write(fd, &fire, sizeof(fire)) <= 0) || (rb->write(fd, &power, sizeof(power)) <= 0)) { rb->close(fd); rb->remove(SAVE_FILE); rb->splash(HZ/2, "Failed to save game"); return; } rb->close(fd); } /* brickmania_sleep timer counting the score */ static void brickmania_sleep(int secs) { bool done=false; char s[20]; int count=0; int sw, w; while (!done) { if (count == 0) count = *rb->current_tick + HZ*secs; if ( (TIME_AFTER(*rb->current_tick, count)) && (vscore == score) ) done = true; if(vscore != score) { if (vscore<score) vscore++; if (vscore>score) vscore--; rb->snprintf(s, sizeof(s), "%d", vscore); rb->lcd_getstringsize(s, &sw, &w); rb->lcd_putsxy(LCD_WIDTH/2-sw/2, 0, s); rb->lcd_update_rect(0,0,LCD_WIDTH,w+2); } rb->yield(); } } static int brickmania_help(void) { static char *help_text[] = { "Brickmania", "", "Aim", "", "Destroy", "all", "the", "bricks", "by", "bouncing", "the", "ball", "of", "them", "using", "the", "paddle.", "", "", "Controls", "", #if CONFIG_KEYPAD == COWON_D2_PAD "- & +:", #else "< & >:", #endif "Moves", "the", "paddle", "", #if CONFIG_KEYPAD == ONDIO_PAD "MENU:", #elif (CONFIG_KEYPAD == RECORDER_PAD) || (CONFIG_KEYPAD == IAUDIO_M3_PAD) "PLAY:", #elif CONFIG_KEYPAD == IRIVER_H300_PAD "NAVI:", #elif CONFIG_KEYPAD == COWON_D2_PAD "MENU:", #else "SELECT:", #endif "Releases", "the", "ball/Fire!", "", #if CONFIG_KEYPAD == IAUDIO_M3_PAD "REC:", #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD) || \ (CONFIG_KEYPAD == CREATIVEZVM_PAD) "BACK:", #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \ (CONFIG_KEYPAD == IPOD_3G_PAD) || \ (CONFIG_KEYPAD == IPOD_1G2G_PAD) || \ (CONFIG_KEYPAD == SANSA_FUZE_PAD) "MENU:", #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \ (CONFIG_KEYPAD == IRIVER_H300_PAD) || \ (CONFIG_KEYPAD == ONDIO_PAD) || \ (CONFIG_KEYPAD == RECORDER_PAD) || \ (CONFIG_KEYPAD == ARCHOS_AV300_PAD) "STOP:", #else "POWER:", #endif "Returns", "to", "menu", "", "", "Specials", "", "N", "Normal:", "returns", "paddle", "to", "normal", "", "D", "DIE!:", "loses", "a", "life", "", "L", "Life:", "gains", "a", "life/power", "up", "", "F", "Fire:", "allows", "you", "to", "shoot", "bricks", "", "G", "Glue:", "ball", "sticks", "to", "paddle", "", "B", "Ball:", "generates", "another", "ball", "", "FL", "Flip:", "flips", "left / right", "movement", "", "<->", "or", "<E>:", "enlarges", "the", "paddle", "", ">-<", "or", ">S<:", "shrinks", "the", "paddle", "", }; static struct style_text formation[]={ { 0, TEXT_CENTER|TEXT_UNDERLINE }, { 2, C_RED }, { 19, C_RED }, { 37, C_RED }, { 39, C_BLUE }, { 46, C_RED }, { 52, C_GREEN }, { 59, C_ORANGE }, { 67, C_GREEN }, { 74, C_YELLOW }, { 80, C_RED }, LAST_STYLE_ITEM }; rb->lcd_setfont(FONT_UI); #ifdef HAVE_LCD_COLOR rb->lcd_set_background(LCD_BLACK); rb->lcd_set_foreground(LCD_WHITE); #endif if (display_text(ARRAYLEN(help_text), help_text, formation, NULL, true)) return 1; rb->lcd_setfont(FONT_SYSFIXED); return 0; } static int brickmania_menu_cb(int action, const struct menu_item_ex *this_item) { int i = ((intptr_t)this_item); if(action == ACTION_REQUEST_MENUITEM && !resume && (i==0 || i==6)) return ACTION_EXIT_MENUITEM; return action; } static int brickmania_menu(void) { int selected = 0; static struct opt_items options[] = { { "Easy", -1 }, { "Normal", -1 }, }; #ifdef HAVE_TOUCHSCREEN /* Entering Menu, set the touchscreen to the global setting */ rb->touchscreen_set_mode(rb->global_settings->touch_mode); #endif MENUITEM_STRINGLIST(main_menu, "Brickmania Menu", brickmania_menu_cb, "Resume Game", "Start New Game", "Difficulty", "Help", "High Scores", "Playback Control", "Quit without Saving", "Quit"); rb->button_clear_queue(); while (true) { switch (rb->do_menu(&main_menu, &selected, NULL, false)) { case 0: if(game_state!=ST_READY) game_state = ST_PAUSE; if(resume_file) rb->remove(SAVE_FILE); return 0; case 1: score=0; vscore=0; life=2; level=0; brickmania_init_game(true); return 0; case 2: rb->set_option("Difficulty", &difficulty, INT, options, 2, NULL); break; case 3: if (brickmania_help()) return 1; break; case 4: highscore_show(-1, highscores, NUM_SCORES, true); break; case 5: if (playback_control(NULL)) return 1; break; case 6: return 1; case 7: if (resume) { rb->splash(HZ*1, "Saving game ..."); brickmania_savegame(); } return 1; case MENU_ATTACHED_USB: return 1; default: break; } } #ifdef HAVE_TOUCHSCREEN rb->touchscreen_set_mode(TOUCHSCREEN_POINT); #endif } static void brick_hit(int i, int j) { if(!brick[i][j].used) return; /* if this is a crackable brick hits starts as * greater than 0. */ if (brick[i][j].hits > 0) { brick[i][j].hits--; brick[i][j].hiteffect++; score+=SCORE_BALL_HIT_BRICK; } else { brick[i][j].used=false; if (used_powers<MAX_POWERS) { int ran = rb->rand()%POWER_RAND; if (ran<NUMBER_OF_POWERUPS) { power[used_powers].top = TOPMARGIN + i*BRICK_HEIGHT; power[used_powers].x_pos = LEFTMARGIN + j*BRICK_WIDTH + (BRICK_WIDTH >> 1); power[used_powers].type = ran; used_powers++; } } brick_on_board--; score+=SCORE_BALL_DEMOLISHED_BRICK; } } static int brickmania_game_loop(void) { int j,i,k; int sw, sh; char s[30]; int sec_count=0; int end; /* pad_rect used for powerup/ball checks */ struct rect pad_rect; /* This is used for various lines that are checked (ball and powerup) */ struct line misc_line; /* This stores the point that the two lines intersected in a test */ struct point pt_hit; if (brickmania_menu()) { return 1; } resume = false; resume_file = false; #ifdef HAVE_LCD_COLOR rb->lcd_set_background(LCD_BLACK); rb->lcd_set_foreground(LCD_WHITE); rb->lcd_set_drawmode(DRMODE_SOLID); rb->lcd_clear_display(); #endif while(true) { /* Convert CYCLETIME (in ms) to HZ */ end = *rb->current_tick + (CYCLETIME * HZ) / 1000; if (life >= 0) { rb->lcd_clear_display(); if (flip_sides) { if (TIME_AFTER(*rb->current_tick, sec_count)) { sec_count=*rb->current_tick+HZ; if (flip_sides_delay > 1) flip_sides_delay--; else flip_sides=false; } rb->snprintf(s, sizeof(s), "%d", flip_sides_delay); rb->lcd_getstringsize(s, &sw, NULL); rb->lcd_putsxy(LCD_WIDTH/2-2, INT3(STRINGPOS_FLIP), s); } /* write life num */ #if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64) rb->snprintf(s, sizeof(s), "L:%d", life); #else rb->snprintf(s, sizeof(s), "Life: %d", life); #endif rb->lcd_putsxy(0, 0, s); #if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64) rb->snprintf(s, sizeof(s), "L%d", level+1); #else rb->snprintf(s, sizeof(s), "Level %d", level+1); #endif rb->lcd_getstringsize(s, &sw, NULL); rb->lcd_putsxy(LCD_WIDTH-sw, 0, s); if (vscore<score) vscore++; rb->snprintf(s, sizeof(s), "%d", vscore); rb->lcd_getstringsize(s, &sw, NULL); rb->lcd_putsxy(LCD_WIDTH/2-sw/2, 0, s); /* continue game */ if (game_state == ST_PAUSE) { rb->snprintf(s, sizeof(s), CONTINUE_TEXT); rb->lcd_getstringsize(s, &sw, NULL); rb->lcd_putsxy(LCD_WIDTH/2-sw/2, INT3(STRINGPOS_NAVI), s); sec_count=*rb->current_tick+HZ; } /* draw the ball */ for(i=0;i<used_balls;i++) rb->lcd_bitmap(brickmania_ball, INT3(ball[i].pos_x - HALFBALL), INT3(ball[i].pos_y - HALFBALL), INT3(BALL), INT3(BALL)); if (brick_on_board==0) brick_on_board--; /* Setup the pad line-later used in intersection test */ pad_rect.top_left.x = pad_pos_x; pad_rect.top_left.y = PAD_POS_Y; pad_rect.bottom_right.x = pad_pos_x + pad_width; pad_rect.bottom_right.y = PAD_POS_Y + PAD_HEIGHT; if (game_state!=ST_PAUSE) { /* move the fires */ for(k=0;k<used_fires;k++) { fire[k].top -= SPEED_FIRE; if (fire[k].top < 0) { used_fires--; fire[k].top = fire[used_fires].top; fire[k].x_pos = fire[used_fires].x_pos; k--; } else if (fire[k].x_pos >= LEFTMARGIN && fire[k].x_pos < LEFTMARGIN + NUM_BRICKS_COLS * BRICK_WIDTH) { j = (fire[k].x_pos - LEFTMARGIN) / BRICK_WIDTH; for (i=NUM_BRICKS_ROWS-1;i>=0;i--) { if (TOPMARGIN + i*BRICK_HEIGHT<=fire[k].top) break; if (brick[i][j].used) { score += SCORE_FIRE_HIT_BRICK; brick_hit(i, j); used_fires--; fire[k].top = fire[used_fires].top; fire[k].x_pos = fire[used_fires].x_pos; k--; break; } } } } /* move and handle the powerups */ for (k=0;k<used_powers;k++) { int remove_power = 0; power[k].top += SPEED_POWER; if (power[k].top > PAD_POS_Y) { /* power hit bottom */ remove_power = 1; } else { /* Use misc_line to check if the center of the powerup * hit the paddle. */ misc_line.p1.x = power[k].x_pos; misc_line.p1.y = power[k].top; misc_line.p2 = misc_line.p1; misc_line.p2.y += SPEED_POWER; /* Check if the powerup will hit the paddle */ if (check_rect(&misc_line, &pad_rect, INTERSECTION_ALL, &pt_hit)) { /* power hit paddle */ remove_power = 1; switch(power[k].type) { case POWER_TYPE_LIFE_GAIN: life++; score += SCORE_POWER_LIFE_GAIN; break; case POWER_TYPE_LIFE_LOSS: life--; if (life>=0) { brickmania_init_game(false); brickmania_sleep(2); } break; case POWER_TYPE_PADDLE_STICKY: score += SCORE_POWER_PADDLE_STICKY; paddle_type = PADDLE_TYPE_STICKY; break; case POWER_TYPE_PADDLE_SHOOTER: score += SCORE_POWER_PADDLE_SHOOTER; paddle_type = PADDLE_TYPE_SHOOTER; for(i=0;i<used_balls;i++) ball[i].glue=false; break; case POWER_TYPE_PADDLE_NORMAL: score += SCORE_POWER_PADDLE_NORMAL; paddle_type = PADDLE_TYPE_NORMAL; for(i=0;i<used_balls;i++) ball[i].glue=false; flip_sides=false; pad_pos_x += (pad_width-PAD_WIDTH)/2; pad_width = PAD_WIDTH; break; case POWER_TYPE_PADDLE_FLIP: score += SCORE_POWER_FLIP; sec_count = *rb->current_tick+HZ; flip_sides_delay = FLIP_SIDES_DELAY; flip_sides = true; break; case POWER_TYPE_EXTRA_BALL: score += SCORE_POWER_EXTRA_BALL; if(used_balls<MAX_BALLS) { /* Set the speed */ if(rb->rand()%2 == 0) ball[used_balls].speedx=-SPEED_4Q_X; else ball[used_balls].speedx= SPEED_4Q_X; ball[used_balls].speedy= SPEED_4Q_Y; /* Ball is not glued */ ball[used_balls].glue= false; used_balls++; } break; case POWER_TYPE_PADDLE_LONG: score+=SCORE_POWER_LONG_PADDLE; if (pad_width==PAD_WIDTH) { pad_width = LONG_PAD_WIDTH; pad_pos_x -= (LONG_PAD_WIDTH - PAD_WIDTH)/2; } else if (pad_width==SHORT_PAD_WIDTH) { pad_width = PAD_WIDTH; pad_pos_x-=(PAD_WIDTH- SHORT_PAD_WIDTH)/2; } if (pad_pos_x < 0) pad_pos_x = 0; else if(pad_pos_x + pad_width > GAMESCREEN_WIDTH) pad_pos_x = GAMESCREEN_WIDTH-pad_width; break; case POWER_TYPE_PADDLE_SHORT: if (pad_width==PAD_WIDTH) { pad_width=SHORT_PAD_WIDTH; pad_pos_x+=(PAD_WIDTH- SHORT_PAD_WIDTH)/2; } else if (pad_width==LONG_PAD_WIDTH) { pad_width=PAD_WIDTH; pad_pos_x+=(LONG_PAD_WIDTH-PAD_WIDTH)/2; } break; default: break; } } if (remove_power) { used_powers--; if (k != used_powers) { power[k].top = power[used_powers].top; power[k].x_pos = power[used_powers].x_pos; power[k].type = power[used_powers].type; } k--; } } } } /* draw the fires */ for(k=0;k<used_fires;k++) { rb->lcd_vline(INT3(fire[k].x_pos), INT3(fire[k].top), INT3(fire[k].top + FIRE_LENGTH)); } /* draw the powerups */ for(k=0;k<used_powers;k++) { rb->lcd_bitmap_part(brickmania_powerups,0, INT3(POWERUP_HEIGHT)*power[k].type, STRIDE(SCREEN_MAIN, BMPWIDTH_brickmania_powerups, BMPHEIGHT_brickmania_powerups), INT3(power[k].x_pos - (POWERUP_WIDTH >> 1)), INT3(power[k].top), INT3(POWERUP_WIDTH), INT3(POWERUP_HEIGHT)); } /* handle all of the bricks */ for (i=0; i<NUM_BRICKS_ROWS; i++) { for (j=0; j<NUM_BRICKS_COLS ;j++) { int brickx,bricky; /* The brick is a brick, but it may or may not be in use */ if(brick[i][j].used) { struct rect brick_rect; brickx = LEFTMARGIN + j*BRICK_WIDTH; bricky = TOPMARGIN + i*BRICK_HEIGHT; brick_rect.top_left.x = brickx; brick_rect.top_left.y = bricky; brick_rect.bottom_right.x = brickx + BRICK_WIDTH; brick_rect.bottom_right.y = bricky + BRICK_HEIGHT; /* Draw the brick */ rb->lcd_bitmap_part(brickmania_bricks,0, INT3(BRICK_HEIGHT)*brick[i][j].color, STRIDE( SCREEN_MAIN, BMPWIDTH_brickmania_bricks, BMPHEIGHT_brickmania_bricks), INT3(brickx), INT3(bricky), INT3(BRICK_WIDTH), INT3(BRICK_HEIGHT) ); #ifdef HAVE_LCD_COLOR /* No transparent effect for greyscale lcds for now */ if (brick[i][j].hiteffect > 0) rb->lcd_bitmap_transparent_part(brickmania_break,0, INT3(BRICK_HEIGHT)*brick[i][j].hiteffect, STRIDE( SCREEN_MAIN, BMPWIDTH_brickmania_break, BMPHEIGHT_brickmania_break), INT3(brickx), INT3(bricky), INT3(BRICK_WIDTH), INT3(BRICK_HEIGHT) ); #endif /* Check if any balls collided with the brick */ for(k=0; k<used_balls; k++) { int hit = 0; /* Setup the ball path to describe the current ball * position and the line it makes to its next * position. */ misc_line.p1.x = ball[k].pos_x; misc_line.p1.y = ball[k].pos_y; misc_line.p2.x = ball[k].pos_x + ball[k].speedx; misc_line.p2.y = ball[k].pos_y + ball[k].speedy; /* Check to see if the ball and the bottom hit. If * the ball is moving down we don't want to * include the bottom line intersection. * * The order that the sides are checked matters. * * Note that tempx/tempy store the next position * that the ball should be drawn. */ if (ball[k].speedy <= 0 && check_rect(&misc_line, &brick_rect, INTERSECTION_BOTTOM, &pt_hit)) { ball[k].speedy = -ball[k].speedy; hit = 1; } /* Check the top, if the ball is moving up dont * count it as a hit. */ else if (ball[k].speedy > 0 && check_rect(&misc_line, &brick_rect, INTERSECTION_TOP, &pt_hit)) { ball[k].speedy = -ball[k].speedy; hit = 1; } /* Check the left side of the brick */ else if (check_rect(&misc_line, &brick_rect, INTERSECTION_LEFT, &pt_hit)) { ball[k].speedx = -ball[k].speedx; hit = 1; } /* Check the right side of the brick */ else if (check_rect(&misc_line, &brick_rect, INTERSECTION_RIGHT, &pt_hit)) { ball[k].speedx = -ball[k].speedx; hit = 1; } if (hit) { ball[k].tempy = pt_hit.y; ball[k].tempx = pt_hit.x; brick_hit(i, j); break; } } /* for k */ } /* if(used) */ } /* for j */ } /* for i */ /* draw the paddle according to the PAD_WIDTH */ if( pad_width == PAD_WIDTH ) /* Normal width */ { rb->lcd_bitmap_part( brickmania_pads, 0, paddle_type*INT3(PAD_HEIGHT), STRIDE( SCREEN_MAIN, BMPWIDTH_brickmania_pads, BMPHEIGHT_brickmania_pads), INT3(pad_pos_x), INT3(PAD_POS_Y), INT3(pad_width), INT3(PAD_HEIGHT) ); } else if( pad_width == LONG_PAD_WIDTH ) /* Long Pad */ { rb->lcd_bitmap_part( brickmania_long_pads, 0,paddle_type*INT3(PAD_HEIGHT), STRIDE( SCREEN_MAIN, BMPWIDTH_brickmania_long_pads, BMPHEIGHT_brickmania_long_pads), INT3(pad_pos_x), INT3(PAD_POS_Y), INT3(pad_width), INT3(PAD_HEIGHT) ); } else /* Short pad */ { rb->lcd_bitmap_part( brickmania_short_pads, 0,paddle_type*INT3(PAD_HEIGHT), STRIDE( SCREEN_MAIN, BMPWIDTH_brickmania_short_pads, BMPHEIGHT_brickmania_short_pads), INT3(pad_pos_x), INT3(PAD_POS_Y), INT3(pad_width), INT3(PAD_HEIGHT) ); } /* If the game is not paused continue */ if (game_state!=ST_PAUSE) { /* Loop through all of the balls in play */ for(k=0;k<used_balls;k++) { struct line screen_edge; /* Describe the ball movement for the edge collision detection */ misc_line.p1.x = ball[k].pos_x; misc_line.p1.y = ball[k].pos_y; misc_line.p2.x = ball[k].pos_x + ball[k].speedx; misc_line.p2.y = ball[k].pos_y + ball[k].speedy; /* Did the Ball hit the top of the screen? */ screen_edge.p1.x = 0; screen_edge.p1.y = 0; screen_edge.p2.x = GAMESCREEN_WIDTH; screen_edge.p2.y = 0; /* the test for pos_y prevents the ball from bouncing back * from _over_ the top to infinity on some rare cases */ if (ball[k].pos_y > 0 && check_lines(&misc_line, &screen_edge, &pt_hit)) { ball[k].tempy = pt_hit.y + 1; ball[k].tempx = pt_hit.x; /* Reverse the direction */ ball[k].speedy = -ball[k].speedy; } /* Player missed the ball and hit bottom of screen */ if (ball[k].pos_y >= GAMESCREEN_HEIGHT) { /* Player had balls to spare, so handle the removal */ if (used_balls>1) { /* decrease number of balls in play */ used_balls--; /* Replace removed ball with the last ball */ ball[k].pos_x = ball[used_balls].pos_x; ball[k].pos_y = ball[used_balls].pos_y; ball[k].speedy = ball[used_balls].speedy; ball[k].tempy = ball[used_balls].tempy; ball[k].speedx = ball[used_balls].speedx; ball[k].tempx = ball[used_balls].tempx; ball[k].glue = ball[used_balls].glue; /* Reset the last ball that was removed */ ball[used_balls].speedx=0; ball[used_balls].speedy=0; ball[used_balls].tempy=0; ball[used_balls].tempx=0; ball[used_balls].pos_y=ON_PAD_POS_Y; ball[used_balls].pos_x=pad_pos_x+(pad_width/2)-HALFBALL; k--; continue; } else { /* Player lost a life */ life--; if (life>=0) { /* No lives left reset game */ brickmania_init_game(false); brickmania_sleep(2); rb->button_clear_queue(); } } } if (game_state != ST_READY && !ball[k].glue) { /* Check if the ball hit the left side */ screen_edge.p1.x = 0; screen_edge.p1.y = 0; screen_edge.p2.x = 0; screen_edge.p2.y = GAMESCREEN_HEIGHT; if (check_lines(&misc_line, &screen_edge, &pt_hit)) { /* Reverse direction */ ball[k].speedx = abs(ball[k].speedx); /* Re-position ball in gameboard */ ball[k].tempy = pt_hit.y; ball[k].tempx = 0; } /* Check if the ball hit the right side */ screen_edge.p1.x = GAMESCREEN_WIDTH; screen_edge.p1.y = 0; screen_edge.p2.x = GAMESCREEN_WIDTH; screen_edge.p2.y = GAMESCREEN_HEIGHT; if (check_lines(&misc_line, &screen_edge, &pt_hit)) { /* Reverse direction */ ball[k].speedx = -abs(ball[k].speedx); /* Re-position ball in gameboard */ ball[k].tempy = pt_hit.y; ball[k].tempx = GAMESCREEN_WIDTH - FIXED3(1); } /* Did the ball hit the paddle? Depending on where the ball * Hit set the x/y speed appropriately. */ if(check_rect(&misc_line, &pad_rect, INTERSECTION_TOP, &pt_hit) ) { /* Re-position ball based on collision */ ball[k].tempy = ON_PAD_POS_Y; ball[k].tempx = pt_hit.x; /* Calculate the ball position relative to the paddle width */ int ball_repos = pt_hit.x - pad_pos_x; /* If the ball hits the right half of paddle, x speed * should be positive, if it hits the left half it * should be negative. */ int x_direction = -1; /* Comparisons are done with respect to 1/2 pad_width */ if(ball_repos > pad_width/2) { /* flip the relative position */ ball_repos -= ((ball_repos - pad_width/2) << 1); /* Ball hit the right half so X speed calculations * should be positive. */ x_direction = 1; } /* Figure out where the ball hit relative to 1/2 pad * and in divisions of 4. */ ball_repos = ball_repos / (pad_width/2/4); switch(ball_repos) { /* Ball hit the outer edge of the paddle */ case 0: ball[k].speedy = SPEED_1Q_Y; ball[k].speedx = SPEED_1Q_X * x_direction; break; /* Ball hit the next fourth of the paddle */ case 1: ball[k].speedy = SPEED_2Q_Y; ball[k].speedx = SPEED_2Q_X * x_direction; break; /* Ball hit the third fourth of the paddle */ case 2: ball[k].speedy = SPEED_3Q_Y; ball[k].speedx = SPEED_3Q_X * x_direction; break; /* Ball hit the fourth fourth of the paddle or dead * center. */ case 3: case 4: ball[k].speedy = SPEED_4Q_Y; /* Since this is the middle we don't want to * force the ball in a different direction. * Just keep it going in the same direction * with a specific speed. */ if(ball[k].speedx > 0) { ball[k].speedx = SPEED_4Q_X; } else { ball[k].speedx = -SPEED_4Q_X; } break; default: ball[k].speedy = SPEED_4Q_Y; break; } if(paddle_type == PADDLE_TYPE_STICKY) { ball[k].speedy = -ball[k].speedy; ball[k].glue=true; /* X location should not be forced since that is moved with the paddle. The Y * position should be forced to keep the ball at the paddle. */ ball[k].tempx = 0; ball[k].tempy = ON_PAD_POS_Y; } } } /* Update the ball position */ if (!ball[k].glue) { if(ball[k].tempx) ball[k].pos_x = ball[k].tempx; else ball[k].pos_x += ball[k].speedx; if(ball[k].tempy) ball[k].pos_y = ball[k].tempy; else ball[k].pos_y += ball[k].speedy; ball[k].tempy=0; ball[k].tempx=0; } } /* for k */ } rb->lcd_update(); if (brick_on_board < 0) { if (level+1<NUM_LEVELS) { level++; if (difficulty==NORMAL) score+=SCORE_LEVEL_COMPLETED; brickmania_init_game(true); brickmania_sleep(2); rb->button_clear_queue(); } else { rb->lcd_getstringsize("Congratulations!", &sw, &sh); rb->lcd_putsxy(LCD_WIDTH/2-sw/2, INT3(STRINGPOS_FINISH) - 2 * sh, "Congratulations!"); #if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64) rb->lcd_getstringsize("No more levels", &sw, NULL); rb->lcd_putsxy(LCD_WIDTH/2-sw/2, INT3(STRINGPOS_FINISH), "No more levels"); #else rb->lcd_getstringsize("You have finished the game!", &sw, NULL); rb->lcd_putsxy(LCD_WIDTH/2-sw/2, INT3(STRINGPOS_FINISH), "You have finished the game!"); #endif vscore=score; rb->lcd_update(); brickmania_sleep(2); return 0; } } int button=rb->button_get(false); int move_button = rb->button_status(); #if defined(HAS_BUTTON_HOLD) && !defined(HAVE_REMOTE_LCD_AS_MAIN) /* FIXME: Should probably check remote hold here */ if (rb->button_hold()) button = QUIT; #endif #ifdef HAVE_TOUCHSCREEN if( move_button & BUTTON_TOUCHSCREEN) { int data; short touch_x, touch_y; rb->button_status_wdata(&data); touch_x = FIXED3(data >> 16); touch_y = FIXED3(data & 0xffff); if(flip_sides) { pad_pos_x = GAMESCREEN_WIDTH - (touch_x + pad_width/2); } else { pad_pos_x = (touch_x - pad_width/2); } if(pad_pos_x < 0) pad_pos_x = 0; else if(pad_pos_x + pad_width > GAMESCREEN_WIDTH) pad_pos_x = GAMESCREEN_WIDTH-pad_width; for(k=0; k<used_balls; k++) if (game_state==ST_READY || ball[k].glue) ball[k].pos_x = pad_pos_x + pad_width/2; } else #endif { int button_right, button_left; #ifdef ALTRIGHT button_right = move_button & (RIGHT | ALTRIGHT); button_left = move_button & (LEFT | ALTLEFT); #else button_right =((move_button & RIGHT)|| SCROLL_FWD(button)); button_left =((move_button & LEFT) ||SCROLL_BACK(button)); #endif if ((game_state==ST_PAUSE) && (button_right || button_left)) continue; if (button_left || button_right) { int dx = 0; if ((button_right && !flip_sides) || (button_left && flip_sides)) { if (pad_pos_x+SPEED_PAD+pad_width > GAMESCREEN_WIDTH) dx = GAMESCREEN_WIDTH - pad_pos_x - pad_width; else dx = SPEED_PAD; } else if ((button_left && !flip_sides) || (button_right && flip_sides)) { if (pad_pos_x-SPEED_PAD < 0) dx = -pad_pos_x; else dx = -SPEED_PAD; } pad_pos_x+=dx; for(k=0;k<used_balls;k++) { if (game_state==ST_READY || ball[k].glue) { ball[k].pos_x+=dx; if (ball[k].pos_x < HALFBALL) ball[k].pos_x = HALFBALL; else if (ball[k].pos_x > GAMESCREEN_WIDTH - HALFBALL) ball[k].pos_x = GAMESCREEN_WIDTH - HALFBALL; } } } } switch(button) { #if defined(HAVE_TOUCHSCREEN) case (BUTTON_REL | BUTTON_TOUCHSCREEN): #endif case UP: case SELECT: #ifdef ALTSELECT case ALTSELECT: #endif if (game_state==ST_READY) { /* Initialize used balls starting speed */ for(k=0 ; k < used_balls ; k++) { ball[k].speedy = SPEED_4Q_Y; if(pad_pos_x + (pad_width/2) >= GAMESCREEN_WIDTH/2) { ball[k].speedx = SPEED_4Q_X; } else { ball[k].speedx = -SPEED_4Q_X; } } game_state=ST_START; } else if (game_state==ST_PAUSE) { game_state=ST_START; } else if (paddle_type == PADDLE_TYPE_STICKY) { for(k=0;k<used_balls;k++) { if (ball[k].glue) { ball[k].glue=false; ball[k].speedy = -ball[k].speedy; } } } else if (paddle_type == PADDLE_TYPE_SHOOTER) { if (used_fires < MAX_FIRES) { fire[used_fires].top = PAD_POS_Y - FIRE_LENGTH; fire[used_fires].x_pos = pad_pos_x + 1; /* Add 1 for edge */ used_fires++; } if (used_fires < MAX_FIRES) { fire[used_fires].top = PAD_POS_Y - FIRE_LENGTH; fire[used_fires].x_pos = pad_pos_x + pad_width - 1; /* Sub1 edge*/ used_fires++; } } break; #ifdef RC_QUIT case RC_QUIT: #endif case QUIT: resume = true; return 0; break; default: if(rb->default_event_handler(button) == SYS_USB_CONNECTED) return 1; break; } } else { #ifdef HAVE_LCD_COLOR rb->lcd_bitmap_transparent(brickmania_gameover, (LCD_WIDTH - INT3(GAMEOVER_WIDTH))/2, INT3(GAMESCREEN_HEIGHT - GAMEOVER_HEIGHT)/2, INT3(GAMEOVER_WIDTH),INT3(GAMEOVER_HEIGHT)); #else /* greyscale and mono */ rb->lcd_bitmap(brickmania_gameover,(LCD_WIDTH - INT3(GAMEOVER_WIDTH))/2, INT3(GAMESCREEN_HEIGHT - GAMEOVER_HEIGHT)/2, INT3(GAMEOVER_WIDTH),INT3(GAMEOVER_HEIGHT) ); #endif rb->lcd_update(); brickmania_sleep(2); return 0; } /* Game always needs to yield for other threads */ rb->yield(); /* Sleep for a bit if there is time to spare */ if (TIME_BEFORE(*rb->current_tick, end)) rb->sleep(end-*rb->current_tick); } return 0; } /* this is the plugin entry point */ enum plugin_status plugin_start(const void* parameter) { (void)parameter; int last_difficulty; highscore_load(SCORE_FILE, highscores, NUM_SCORES); configfile_load(CONFIG_FILE_NAME,config,1,0); last_difficulty = difficulty; #ifdef HAVE_TOUCHSCREEN rb->touchscreen_set_mode(TOUCHSCREEN_POINT); #endif rb->lcd_setfont(FONT_SYSFIXED); #if LCD_DEPTH > 1 rb->lcd_set_backdrop(NULL); #endif /* Turn off backlight timeout */ backlight_force_on(); /* backlight control in lib/helper.c */ /* now go ahead and have fun! */ rb->srand( *rb->current_tick ); brickmania_loadgame(); resume_file = resume; while(!brickmania_game_loop()) { if(!resume) { int position = highscore_update(score, level+1, "", highscores, NUM_SCORES); if (position != -1) { if (position == 0) rb->splash(HZ*2, "New High Score"); highscore_show(position, highscores, NUM_SCORES, true); } else { brickmania_sleep(3); } } } highscore_save(SCORE_FILE, highscores, NUM_SCORES); if(last_difficulty != difficulty) configfile_save(CONFIG_FILE_NAME,config,1,0); /* Restore user's original backlight setting */ rb->lcd_setfont(FONT_UI); /* Turn on backlight timeout (revert to settings) */ backlight_use_settings(); /* backlight control in lib/helper.c */ return PLUGIN_OK; }