Source

pebble / mario / src / anim.c

  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
/* ***************************************************************************
// anim.c - the main animation and drawing code for MONOCHRON
// This code is distributed under the GNU Public License
//		which can be found at http://www.gnu.org/licenses/gpl.txt
//
**************************************************************************** */

// MarioChron: Mario Themed Clock Face
// Initial work based on InvaderChron by Dataman
// By: techninja (James T) & Super-Awesome Sylvia
// Ported to Pebble by Trammell Hudson <hudson@osresearch.net>
//
// Originally created for Sylvia's Super-Awesome Mini Maker Show Episode S02E03
// sylviashow.com/monochron

// == EASILY HACKABLE DEFINES ==================================================
// MonoChron as 128x64, Pebble as 144x168.
// Initial version does not change anything

#define GROUND_Y 58    // Ground Position

#define CLOUD_X 65     // Cloud Position
#define CLOUD_Y 11

#define BOX_X 50       // Box Position
#define BOX_Y 26
#define BOX_TYPE 1     // Box Type (0 for standard, 1 for question)

#define TURTLE_MIN 83  // Turtle boundary
#define TURTLE_MAX 110
#define TURTLE_Y 27    // TODO: Derive these from bolted box position
#define TURTLE_SPIN 0  // Shell spin on or off

// ^^ EASILY HACKABLE DEFINES ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#include <pebble_os.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>

#include "font5x7.h"

typedef uint8_t u08;
#define GLCD_XPIXELS 128
#define GLCD_YPIXELS 64

static GContext * ctx;

static inline void
glcdRectangle(
	int x,
	int y,
	int w,
	int h
)
{
	graphics_context_set_stroke_color(ctx, GColorBlack);
	graphics_draw_round_rect(ctx, GRect(x,y,w,h), 0);
}

static inline void
glcdFillRectangle(
	int x,
	int y,
	int w,
	int h,
	int inverted
)
{
	graphics_context_set_fill_color(ctx, inverted ? GColorWhite : GColorBlack);
	graphics_fill_rect(ctx, GRect(x, y, w, h), 0, GCornerNone);
}


static inline void
glcdClearDot(
	int x,
	int y
)
{
	graphics_context_set_stroke_color(ctx, GColorBlack);
	graphics_draw_pixel(ctx, GPoint(x,y));
}

static inline void
glcdSetDot(
	int x,
	int y
)
{
	graphics_context_set_stroke_color(ctx, GColorWhite);
	graphics_draw_pixel(ctx, GPoint(x,y));
}

extern volatile uint8_t time_s, time_m, time_h;
extern volatile uint8_t old_m, old_h;
extern volatile uint8_t date_m, date_d, date_y;
extern volatile uint8_t alarming, alarm_h, alarm_m;
extern volatile uint8_t time_format;
extern volatile uint8_t region;
extern volatile uint8_t score_mode;
extern volatile uint8_t write_font;

uint8_t left_score, right_score, left_score2, right_score2;

extern volatile uint8_t minute_changed, hour_changed;

/**** FUNCTION PROTOTYPES ****/
void encipher(void);
void init_crand(void);
void WriteDigits(uint8_t, uint8_t);
void WriteTime(uint8_t);
void initdisplay(uint8_t inverted); // Draw the clock face initially (and static objects)
void setscore(void);  // TODO: Document these functions!
void initanim(void);
void initdisplay(uint8_t inverted);
void animation_loop(void); // Runs all animation routines with a delay
void animate_mario(void);
void animate_turtle(void);
uint8_t animate_showcoin(u08 x, u08 y);

// All draw_* functions draw sprites to the screen.
// Some remove previously drawn frames, some don't.
void draw_showcoin(u08 x, u08 y, u08 anim_frame); // TODO: Add 'inverted' support to these
void draw_mario(u08 x, u08 y, u08 flipped);
void draw_turtle(u08 x, u08 y, u08 flipped);
void draw_ground(void);
void draw_bolt(u08 x, u08 y);
void draw_coin(u08 x, u08 y);
void draw_box(u08 x, u08 y, u08 type);
void draw_cloud(u08 x, u08 y);
// ^ More TODO: Optimize these functions to use a single sprite drawing func


uint8_t redraw_time = 0;
uint8_t last_score_mode = 0;

uint32_t rval[2]={0,0};
uint32_t key[4];

void encipher(void) {  // Using 32 rounds of XTea encryption as a PRNG.
  unsigned int i;
  uint32_t v0=rval[0], v1=rval[1], sum=0, delta=0x9E3779B9;
  for (i=0; i < 32; i++) {
    v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
    sum += delta;
    v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
  }
  rval[0]=v0; rval[1]=v1;
}

void init_crand(void) {
  uint32_t temp;
  key[0]=0x2DE9716E;  //Initial XTEA key. Grabbed from the first 16 bytes
  key[1]=0x993FDDD1;  //of grc.com/password.  1 in 2^128 chance of seeing
  key[2]=0x2A77FB57;  //that key again there.
  key[3]=0xB172E6B0;
  rval[0]=0;
  rval[1]=0;
  encipher();
  temp = alarm_h;
  temp<<=8;
  temp|=time_h;
  temp<<=8;
  temp|=time_m;
  temp<<=8;
  temp|=time_s;
  key[0]^=rval[1]<<1;
  encipher();
  key[1]^=temp<<1;
  encipher();
  key[2]^=temp>>1;
  encipher();
  key[3]^=rval[1]>>1;
  encipher();
  temp = alarm_m;
  temp<<=8;
  temp|=date_m;
  temp<<=8;
  temp|=date_d;
  temp<<=8;
  temp|=date_y;
  key[0]^=temp<<1;
  encipher();
  key[1]^=rval[0]<<1;
  encipher();
  key[2]^=rval[0]>>1;
  encipher();
  key[3]^=temp>>1;
  rval[0]=0;
  rval[1]=0;
  encipher();	//And at this point, the PRNG is now seeded, based on power on/date/time reset.
}

uint16_t crand(uint8_t type) {
  if((type==0)||(type>2))
  {
    //wdt_reset();
    encipher();
    return (rval[0]^rval[1])&RAND_MAX;
  } else if (type==1) {
  	return ((rval[0]^rval[1])>>15)&3;
  } else if (type==2) {
  	return ((rval[0]^rval[1])>>17)&1;
  }

  return 0;
}

void setscore(void){
  if(score_mode != last_score_mode) {
    redraw_time = 1;
    last_score_mode = score_mode;
    // Default left and right displays
    left_score = time_h;
    right_score = time_m;
    //if((region == REGION_US)||(region == DOW_REGION_US))
    if (1) {
      left_score2 = date_m;
      right_score2 = date_d;
    } else {
      left_score2 = date_d;
      right_score2 = date_m;
    }
  }

#if 0
  switch(score_mode) {
  	case SCORE_MODE_DOW:
  	  break;
  	case SCORE_MODE_DATELONG:
      right_score2 = date_d;
  	  break;
    case SCORE_MODE_TIME:
      if(alarming && (minute_changed || hour_changed)) {
      	if(hour_changed) {
	      left_score = old_h;
	      right_score = old_m;
	    } else if (minute_changed) {
	      right_score = old_m;
	    }
      } else {
#endif
        left_score = time_h;
        right_score = time_m;
#if 0
      }
      break;
    case SCORE_MODE_DATE:
      if((region == REGION_US)||(region == DOW_REGION_US)) {
        left_score = date_m;
        right_score = date_d;
      } else {
        left_score = date_d;
        right_score = date_m;
      }
      break;
    case SCORE_MODE_YEAR:
      left_score2 = 20;
      right_score2 = date_y;
      break;
    case SCORE_MODE_ALARM:
      left_score2 = alarm_h;
      right_score2 = alarm_m;
      break;
  }

  if (time_format == TIME_12H && left_score>12) {left_score = left_score % 12;}
#endif
}

void initanim(void) {
#if 0
  DEBUG(putstring("screen width: "));
  DEBUG(uart_putw_dec(GLCD_XPIXELS));
  DEBUG(putstring("\n\rscreen height: "));
  DEBUG(uart_putw_dec(GLCD_YPIXELS));
  DEBUG(putstring_nl(""));
#endif
}

void initdisplay(uint8_t inverted) {
  // clear screen
  glcdFillRectangle(0, 0, GLCD_XPIXELS, GLCD_YPIXELS, inverted);
  // get time & display
  last_score_mode = 99; // ???
  setscore();

#if 0
  // Write "WORLD" in the top left corner
  glcdSetAddress(0,0);
  write_font = 77;
    // TODO: This is, just wrong. Seriously. Right?
    glcdWriteChar('W', inverted);glcdWriteChar('O', inverted);
    glcdWriteChar('R', inverted);glcdWriteChar('L', inverted);
    glcdWriteChar('D', inverted);
  write_font = 57;
#endif

  // Add the coin and draw the time
  draw_coin(80, 0);
  WriteTime(inverted);

  // Draw the bolted box (height doesn't matter as long as its below the ground)
  // TODO: Make a wrapper function for this (should be easy!)
  glcdRectangle(80, 43, 44, 20);
  glcdClearDot(80, 43); glcdClearDot(123, 43); // Round top corners
  draw_bolt(82, 45); draw_bolt(118, 45); // Add top corner bolts

  // Untouchable static elements
  draw_ground();
  draw_cloud(CLOUD_X, CLOUD_Y);

  // Touchable mostly static elements
  draw_box(BOX_X, BOX_Y, BOX_TYPE);
}

void animation_loop(void) {
  // TODO: Measure time taken by animation functions and remove that time from the delay
  animate_mario();
  animate_turtle();

  // TODO: Allow for object overlap without unsetting background pixels

#if 0
  _delay_ms(20); // ~16 FPS (not guaranteeable unless animation payload is measured)
#endif
}

void animate_mario(void){
  static uint8_t x = 0;
  static uint8_t y = 0;
  static uint8_t direction = 0;
  static uint8_t yarc_count = 0;
  static uint8_t run_coin_anim = 0;
  static uint8_t jump_next_chance = 0;
  static uint8_t should_jump = 0;

  // A faked "arc" of Y offsets for the jump. TODO: Calculate this
  uint8_t yarc[] = {3,9,10,12,9,6,2};

  // Maximum X for mario to walk (hits the big bolted box)
  uint8_t max = 72; // TODO: Derive this from actual collision

  // If the minute, or hour has changed, jump the next chance we get
  if(minute_changed || hour_changed){
    minute_changed = 0;
    hour_changed = 0;
    jump_next_chance = 1;
  }

  // Two specific points we should start jumping from (depending on direction)
  // Only flip this on when we're ready to jump next
  if (((direction == 0 && x == BOX_X - 5) || (direction == 1 && x == BOX_X + 5)) && jump_next_chance){
    should_jump = 1;
  }

  y = GROUND_Y - 12; // (ground_pos - sprite height)

  // Jump!
  if (should_jump && jump_next_chance) {
    y = y - yarc[yarc_count];
    yarc_count++;

    // Box hit! (totally not fake and hardcoded, really...)
    if (yarc[yarc_count] == 12){
      run_coin_anim = 1;
    }

    // Stop/reset the jump when we've reached the last value of Y Arc (Hack!)
    if (yarc_count == sizeof(yarc)){
      jump_next_chance = 0;
      should_jump = 0;
      yarc_count = 0;
    }
  }

  if (direction == 0){
    draw_mario(x, y, 0);
    x++;
  }else if (direction == 1){
    draw_mario(x, y, 1);
    x--;
  }

  if (x >= max){ // Switch to going left
    direction = 1;
  }

  if (x <= 0){ // Switch to going right
    direction = 0;
  }

  // Turns itself off when done, Async style!
  if (run_coin_anim){
    run_coin_anim = animate_showcoin(BOX_X, BOX_Y - 10);
  }
}

void animate_turtle(void){
  static uint8_t x = TURTLE_MIN;
  static uint8_t y = TURTLE_Y;
  static uint8_t direction = 0;
  static uint8_t wait_frame = 0;

  if (!wait_frame){

    if (direction == 0){
      draw_turtle(x, y, 0);
      x++;
    }

    if (direction == 1){
      draw_turtle(x, y, 1);
      x--;
    }

    wait_frame = 1;
  }else{
    wait_frame = 0;
  }

  if (x >= TURTLE_MAX){ // Switch to going left
    direction = 1;
  }

  if (x <= TURTLE_MIN){ // Switch to going right
    direction = 0;
  }

}

uint8_t animate_showcoin(u08 x, u08 y){
  static u08 frame = 0;
  static u08 frame_max = 19;

  draw_showcoin(x, y, frame);

  if (frame == 4){
    redraw_time = 1; // New coin, new time!
  }

  if (frame == frame_max){
    frame = 0;
    return 0;
  }

  frame++;
  return 1;
}

void draw_mario(u08 x, u08 y, u08 flipped){
  uint8_t xp;
  uint8_t yp;
  uint8_t anim_frame;
  static int mario[5][9] = {
    {0x918,0xE9C,0xEC6,0x7CB,0x6C3,0x7D6,0x492,0x500,0x0}, // Moving Right, frame 1
    {0x518,0x49C,0x6C6,0x7CB,0x6C3,0xFD6,0xE92,0x900,0x0}, // Moving Right, frame 2
    {0x500,0x492,0x7D6,0x6C3,0x7CB,0xEC6,0xE9C,0x918,0x0}, // Moving Left, frame 1
    {0x900,0xE92,0xFD6,0x6C3,0x7CB,0x6C6,0x49C,0x518,0x0}, // Moving Left, frame 2
    {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}, // Dummy (?)
  };
  static u08 frame_offset = 0;
  static u08 lastx; static u08 lasty; static u08 lastanim_frame;

  uint8_t sprite_width = 8;
  uint8_t sprite_height = 12;

  if (frame_offset == 0){
    frame_offset = 1;
  }else{
    frame_offset = 0;
  }

  if (flipped){
    anim_frame = 2 + frame_offset;
  }else{
    anim_frame = 0 + frame_offset;
  }

  // Clear the last sprite
  if (lastx || lasty) {
    for(yp = 0; yp < sprite_height; yp++){
      for(xp = 0; xp < sprite_width; xp++){
        if (mario[lastanim_frame][xp] & (1 << yp)){
          glcdClearDot(lastx + xp, lasty + yp);
        }
      }
    }
  }

  lastx = x; lasty = y; lastanim_frame = anim_frame;

  // Draw the sprite
  for(yp = 0; yp < sprite_height; yp++){
    for(xp = 0; xp < sprite_width; xp++){
      if (mario[anim_frame][xp] & (1 << yp)){
        glcdSetDot(x + xp, y + yp);
      }
    }
  }
}

void draw_showcoin(u08 x, u08 y, u08 anim_frame){
  uint8_t xp;
  uint8_t yp;
  static char coin[16][9] = {
    {0x0,0x0,0x0,0xE0,0xE0,0x0,0x0,0x0,0x0}, // Coin show frame 1
    {0x0,0x0,0x0,0x78,0x78,0x0,0x0,0x0,0x0}, // Coin show frame 2
    {0x0,0x0,0x38,0xC6,0xD6,0x38,0x0,0x0,0x0}, // Coin show frame 3
    {0x0,0x38,0x44,0x82,0x92,0x44,0x38,0x0,0x0}, // Coin show frame 4
    {0x3C,0x42,0x81,0x81,0xA1,0x9D,0x42,0x3C,0x0}, // Coin show frame 5
    {0x0,0x3C,0x42,0x81,0xBD,0x42,0x3C,0x0,0x0}, // Coin show frame 6
    {0x0,0x0,0x7E,0x81,0xBD,0x7E,0x0,0x0,0x0}, // Coin show frame 7
    {0x0,0x0,0x0,0xFF,0xFF,0x0,0x0,0x0,0x0}, // Coin show frame 8
    {0x0,0x0,0x0,0x55,0xAA,0x0,0x0,0x0,0x0}, // Coin show frame 9
    {0x0,0x0,0x0,0x3C,0x3C,0x0,0x0,0x0,0x0}, // Coin show frame 10
    {0x0,0x0,0x24,0x18,0x18,0x24,0x0,0x0,0x0}, // Coin show frame 11
    {0x0,0x42,0x24,0x0,0x0,0x24,0x42,0x0,0x0}, // Coin show frame 12
    {0x81,0x42,0x0,0x0,0x0,0x0,0x42,0x81,0x0}, // Coin show frame 13
    {0x81,0x0,0x0,0x0,0x0,0x0,0x0,0x81,0x0}, // Coin show frame 14
    {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0},   // Blank for clearing
    {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}, // Dummy (?)
  };
  static u08 lastx; static u08 lasty; static u08 lastanim_frame;
  /* The frames coming in don't match 1:1 with the actual sprite frames (to save repetition)
  0:0
  1:1
  2:2
  3:3
  4:4 - Coin Is fully visible
  5:5 - Starts to rotate
  6:6
  7:7
  8:6 - The deviation starts here, Where the coin rotates again
  9:5
  10:6
  11:7 - Rotation done, start to dissapear
  12:8
  13:9
  14:10
  15:11
  16:12
  17:13
  18:14
  19:15 - Blank frame (a cheap hack to erase the previous frame and draw nothing)
  */

  if (anim_frame > 7){
    switch (anim_frame){
      case 8:
        anim_frame = 6;
        break;
      case 9:
        anim_frame = 5;
        break;
    }

    if (anim_frame > 9){
      anim_frame = anim_frame - 4;
    }
  }


  uint8_t sprite_width = 8;
  uint8_t sprite_height = 16;

  // Clear the last sprite
  if (lastx || lasty) {
    for(yp = 0; yp < sprite_height; yp++){
      for(xp = 0; xp < sprite_width; xp++){
        if (coin[lastanim_frame][xp] & (1 << yp)){
          glcdClearDot(lastx + xp, lasty + yp);
        }
      }
    }
  }

  lastx = x; lasty = y; lastanim_frame = anim_frame;

  // Draw the sprite
  for(yp = 0; yp < sprite_height; yp++){
    for(xp = 0; xp < sprite_width; xp++){
      if (coin[anim_frame][xp] & (1 << yp)){
        glcdSetDot(x + xp, y + yp);
      }
    }
  }
}

void draw_turtle(u08 x, u08 y, u08 flipped){
  uint8_t xp;
  uint8_t yp;
  uint8_t anim_frame;
  static int turtle[9][9] = {
    {0x1C00,0xF780,0xFE80,0x9FC0,0x3BFE,0x3FFF,0x2C33,0x78,0x0}, // Moving Right, frame 1
    {0x1C00,0x3780,0x3E80,0x3FC0,0xFBFE,0xFFFF,0x8C33,0x78,0x0}, // Moving Right, frame 2
    {0x78,0x2C33,0x3FFF,0x3BFE,0x9FC0,0xFE80,0xF780,0x1C00,0x0}, // Moving Left, frame 1
    {0x78,0x8C33,0xFFFF,0xFBFE,0x3FC0,0x3E80,0x3780,0x1C00,0x0}, // Moving Left, frame 2
    {0x30,0x6E,0xEB,0xD5,0xD5,0xEB,0x6E,0x30,0x0}, // Shell Spin, frame 1
    {0xF0,0x6E,0x6B,0xD5,0xD5,0xEB,0xEE,0xF0,0x0}, // Shell Spin, frame 2
    {0xF0,0x6E,0x6B,0x75,0x75,0x6B,0x6E,0xF0,0x0}, // Shell Spin, frame 3
    {0xF0,0xEE,0xEB,0xD5,0xD5,0x6B,0x6E,0xF0,0x0}, // Shell Spin, frame 4
    {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}, // Dummy (?)
  };
  static u08 frame_offset = 0;
  static u08 lastx; static u08 lasty; static u08 lastanim_frame;

  uint8_t sprite_width = 8;
  uint8_t sprite_height = 16;

  if (!TURTLE_SPIN){
    if (frame_offset == 0){
      frame_offset = 1;
    }else{
      frame_offset = 0;
    }

    if (flipped){
      anim_frame = 2 + frame_offset;
    }else{
      anim_frame = 0 + frame_offset;
    }
  }else{
    if (frame_offset >= 3){
      frame_offset = 0;
    }else{
      frame_offset++;
    }
    y = y + 8; // Offset for the shorter sprite
    anim_frame = 4 + frame_offset;
  }

  // Clear the last sprite
  if (lastx || lasty) {
    for(yp = 0; yp < sprite_height; yp++){
      for(xp = 0; xp < sprite_width; xp++){
        if (turtle[lastanim_frame][xp] & (1 << yp)){
          glcdClearDot(lastx + xp, lasty + yp);
        }
      }
    }
  }

  lastx = x; lasty = y; lastanim_frame = anim_frame;

  // Draw the sprite
  for(yp = 0; yp < sprite_height; yp++){
    for(xp = 0; xp < sprite_width; xp++){
      if (turtle[anim_frame][xp] & (1 << yp)){
        glcdSetDot(x + xp, y + yp);
      }
    }
  }
}

void draw_ground(void){
  static char ground[] = {0xD,0x10,0x9,0x4,0x19,0x20,0x11,0x8,0x0};
  uint8_t y = GROUND_Y;
  uint8_t xp;
  uint8_t yp;
  uint8_t i;

  uint8_t sprite_width = 8;
  uint8_t sprite_height = 6;

  // Draw the sprite
  for(i = 0; i < 16; i++){
    for(yp = 0; yp < sprite_height; yp++){
      for(xp = 0; xp < sprite_width; xp++){
        if (ground[xp] & (1 << yp)){
          glcdSetDot((i * 8) + xp, y + yp);
        }else{
         glcdClearDot((i * 8) + xp, y + yp);
        }
      }
    }
  }
}

void draw_cloud(u08 x, u08 y){
  static int cloud[] = {0x3776,0x6889,0x8441,0x8002,0x6005,0x8001,0x8892,0x776C,0x0};
  uint8_t xp;
  uint8_t yp;

  uint8_t sprite_width = 16;
  uint8_t sprite_height = 8;

  // Draw the sprite
  for(yp = 0; yp < sprite_height; yp++){
    for(xp = 0; xp < sprite_width; xp++){
    // Sprite is stored on it's side (8x16), so draw it flipped
      if (cloud[yp] & (1 << xp)){
        glcdSetDot(x + xp, y + yp);
      }
    }
  }
}

void draw_bolt(u08 x, u08 y){
  static char bolt[] = {0x6,0xD,0xB,0x6,0x0,0x0,0x0,0x0,0x0};
  uint8_t xp;
  uint8_t yp;

  uint8_t sprite_width = 4;
  uint8_t sprite_height = 4;

  // Draw the sprite
  for(yp = 0; yp < sprite_height; yp++){
    for(xp = 0; xp < sprite_width; xp++){
      if (bolt[xp] & (1 << yp)){
        glcdSetDot(x + xp, y + yp);
      }
    }
  }
}

void draw_coin(u08 x, u08 y){
  static char coin[] = {0x3C,0x42,0x81,0x81,0xA1,0x9D,0x42,0x3C,0x0};
  uint8_t xp;
  uint8_t yp;

  uint8_t sprite_width = 8;
  uint8_t sprite_height = 8;

  // Draw the sprite
  for(yp = 0; yp < sprite_height; yp++){
    for(xp = 0; xp < sprite_width; xp++){
      if (coin[xp] & (1 << yp)){
        glcdSetDot(x + xp, y + yp);
      }
    }
  }
}

void draw_box(u08 x, u08 y, u08 type){
  static char boxes[3][9] = {
    {0x7E,0xD5,0xAB,0xD5,0xAB,0xD5,0xAB,0x7E,0x0}, // Standard
    {0x7E,0x81,0x85,0xD3,0xDB,0x8D,0x81,0x7E,0x0}, // Question
    {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}, // Dummy (?)
  };

  uint8_t xp;
  uint8_t yp;

  uint8_t sprite_width = 8;
  uint8_t sprite_height = 8;

  // Draw the sprite
  for(yp = 0; yp < sprite_height; yp++){
    for(xp = 0; xp < sprite_width; xp++){
      if (boxes[type][xp] & (1 << yp)){
        glcdSetDot(x + xp, y + yp);
      }
    }
  }
}

void WriteTime(uint8_t inverted) {
  write_font = 77;

#if 0
  // Draw Date
  glcdSetAddress(1,1);
  WriteDigits(left_score2,inverted);
  glcdWriteChar('-',inverted);
  WriteDigits(right_score2,inverted);

  // Draw Time
  glcdSetAddress(89,0);
  glcdWriteChar('x', inverted);
  WriteDigits(left_score,inverted);
  WriteDigits(right_score,inverted);
#endif

  write_font = 57;
}

void WriteDigits(uint8_t t, uint8_t inverted)
{
#if 0
	glcdWriteChar(48 + (t/10),inverted);
	glcdWriteChar(48 + (t%10),inverted);
#endif
}


void draw(GContext * gctx)
{
   ctx = gctx;
	//glcdFillRectangle(0, 0, 32, 32, 0);
	//glcdClearDot(16, 16);
	//glcdSetDot(64, 16);

	initdisplay(0);

  // Animation Loop + delay
  animation_loop();
  setscore();

#if 0
  if (redraw_time){
    redraw_time = 0;
    WriteTime(inverted);
  }
#endif
  return;
}


static unsigned char MonthText[] = {
	0,0,0,
	'J','A','N',
	'F','E','B',
	'M','A','R',
	'A','P','R',
	'M','A','Y',
	'J','U','N',
	'J','U','L',
	'A','U','G',
	'S','E','P',
	'O','C','T',
	'N','O','V',
	'D','E','C',
};

static unsigned char DOWText[] = {
	'S','U','N',
	'M','O','N',
	'T','U','E',
	'W','E','D',
	'T','H','U',
	'F','R','I',
	'S','A','T',
};

uint8_t dotw(uint8_t mon, uint8_t day, uint8_t yr)
{
  uint16_t month, year;

    // Calculate day of the week
    month = mon;
    year = 2000 + yr;
    if (mon < 3)  {
      month += 12;
      year -= 1;
    }
    return (day + (2 * month) + (6 * (month+1)/10) + year + (year/4) - (year/100) + (year/400) + 1) % 7;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.