summaryrefslogtreecommitdiff
path: root/src/game.c
blob: 183f716e26c36251400a64bc0297202e95c31d88 (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
874
875
876
877
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#include <stdlib.h>
#include <SDL3_mixer/SDL_mixer.h>
#include "app.h"
#include "font.h"
#include <stdio.h>

#define MIN_TILES_TO_CLEAR 3  //More than this number of tiles per row or column will trigger clear

#define MAX_BOARD_WIDTH 10
#define MAX_BOARD_HEIGHT 10
#define MAX_PIECES 300

#define PIECE_SOURCE_YOFFSET 4
#define PIECE_SOURCE_WIDTH 32
#define PIECE_SOURCE_HEIGHT 32
#define PIECE_DEST_WIDTH 50
#define PIECE_DEST_HEIGHT 50
#define PIECE_LINEAR_SPEED 1.5
#define PIECE_SWAP_SPEED 3
#define PIECE_MAX_ROTATION 40
#define PIECE_ROTATION_SPEED 200
#define GRAVITY 2000.0
#define PIECE_MAX_FALL_XSPEED 200
#define PIECE_FALL_YSPEED_INITIAL -150
#define DELETE_PIECE_OFFSET 50
#define PIECE_FALL_ROTATION_SPEED 200

#define BOARD_WIDTH 8
#define BOARD_HEIGHT 6

#define BOARD_XOFFSET 200
#define BOARD_YOFFSET 150
#define BOARD_PADDING 4

#define BACKGROUND_SCROLL_SPEED 40
#define MAX_CLEARED_TILES_STORED 50
#define LOW_TIME_REMAINING 20  //In seconds

#define CLEAR_EXTRA_TIME_BONUS 0.25

#define TEXT_SPRITE_LENGTH 100
#define TIMED_MODE_NUM_TIME_ADDED_SPRITES 4
#define TIME_ADDED_SPRITE_YVEL -30
#define TIME_ADDED_SPRITE_SHRINK_RATE 0.4
#define TIME_ADDED_SPRITE_START_SCALE 1.2
#define TIME_ADDED_SPRITE_MINIMUM_SCALE 0.9

typedef enum {
  linear_move_state,
  fall_state,
  static_state
} piece_state;

typedef struct {
  int piece_id;
  piece_state state;

  SDL_FPoint position;
  float orientation;

  //Data for linear movement
  SDL_FPoint start_position;
  SDL_FPoint dest_position;
  float travel_speed;
  float fraction_travelled;

  //Data for falling animation
  float xvel, yvel;
  float angular_velocity;

  int piece_color;
  int piece_face;
} piece;

typedef enum {
  tile_filled,
  tile_currently_empty,
  tile_always_empty
} tile_state;

typedef struct {
  tile_state state;
  int piece_id;  //Only if tile_state == tile_filled
} tile;

typedef enum {
  intro_state,
  neutral_state,
  piece_held_state,
  swap_animation_state
} game_scene_state;

typedef enum {
  timed_mode,
  combo_mode
} game_mode;

typedef struct {
  bool active;
  char text[TEXT_SPRITE_LENGTH];
  SDL_FPoint position;

  float scale;
} text_sprite;

typedef struct {
  float time_remaining;
  text_sprite time_added_sprites[TIMED_MODE_NUM_TIME_ADDED_SPRITES];
} timed_mode_data;

typedef struct {
  bool inited;
  SDL_Texture* spritesheet;
  game_scene_state state;
  game_mode mode;

  piece pieces[MAX_PIECES];
  int num_pieces;

  tile board[MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH];
  int board_width;
  int board_height;

  int selected_piece_id;
  int swap_piece_id;

  SDL_Texture* background_texture;
  float background_offset;

  sprite_font text_font;

  float background_scaled_width;
  float background_scaled_height;

  //Sounds
  MIX_Track* effect_track;
  MIX_Audio* pop_audio;

  timed_mode_data timed_mode_data;
} s_game;

extern SDL_Window* window;

s_game game = {0};

extern SDL_Window* window;

static inline float get_window_relative_xpos(float vp) {
  int width;
  SDL_GetWindowSizeInPixels(window, &width, NULL);

  return width * vp;
}

static inline float get_window_relative_ypos(float vp) {
  int height;
  SDL_GetWindowSizeInPixels(window, NULL, &height);

  return height * vp;
}

static inline float random_float_0to1() {
  return (float)rand() / RAND_MAX;
}

static inline bool filled_tile_at(int y, int x) {
  if (x < 0 || x >= game.board_width || y < 0 || y >= game.board_height)
    return false;

  return game.board[y][x].state == tile_filled;
}

SDL_FPoint board_position(int y, int x) {
  return (SDL_FPoint) {
    .x = (PIECE_DEST_WIDTH + BOARD_PADDING) * x + BOARD_XOFFSET,
    .y = (PIECE_DEST_HEIGHT + BOARD_PADDING) * y + BOARD_YOFFSET
  };
}

void piece_start_moving_to(piece* piece, SDL_FPoint dest_position, float speed) {
  piece->start_position = piece->position;
  piece->dest_position = dest_position;
  piece->travel_speed = speed;
  piece->state = linear_move_state;
  piece->fraction_travelled = 0;
}

int add_piece(piece piece) {
  static int last_piece_id = 0;

  if (game.num_pieces + 1 >= MAX_PIECES) {
    return -1;
  }

  piece.piece_id = last_piece_id++;
  game.pieces[game.num_pieces++] = piece;

  return piece.piece_id;
}

piece* retreive_piece(int piece_id) {
  for (int i = 0; i < game.num_pieces; i++) {
    if (game.pieces[i].piece_id == piece_id) {
      return &game.pieces[i];
    }
  }

  return NULL;
}

void delete_piece(int piece_id) {
  piece* to_be_deleted = retreive_piece(piece_id);

  if (to_be_deleted == NULL) {
    return;
  }

  SDL_memcpy(to_be_deleted, &game.pieces[game.num_pieces - 1], sizeof(piece));
  game.num_pieces--;
}

float rand_float() {
  return rand() / (float)RAND_MAX;
}

void add_piece_at(int y, int x) {
  int new_piece_id = add_piece((piece) {
      .position = {
        .x = (800 - PIECE_DEST_WIDTH) * rand_float(),
        .y = (600 - PIECE_DEST_HEIGHT) * rand_float()
      },
      .orientation = 0,
      .piece_color = rand() % 9,
      .piece_face = rand() % 6
    });

  if (new_piece_id == -1) {
    return;
  }

  float speed_diff = PIECE_LINEAR_SPEED * rand_float() - PIECE_LINEAR_SPEED / 2.0;

  piece* new_piece = retreive_piece(new_piece_id);
  piece_start_moving_to(new_piece, board_position(y, x), PIECE_LINEAR_SPEED + speed_diff);

  game.board[y][x] = (tile) {
    .state = tile_filled,
    .piece_id = new_piece_id
  };
}

void fill_board() {
  for (int i = 0; i < game.board_height; i++) {
    for (int j = 0; j < game.board_width; j++) {
      add_piece_at(i, j);
    }
  }
}

void clear_board() {
  game.num_pieces = 0;
}

void init_game(SDL_Renderer* renderer, MIX_Mixer* mixer) {
  clear_board();
  SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);

  if (game.spritesheet == NULL) {
    game.spritesheet = IMG_LoadTexture(renderer, "assets/match3_sheet.png");
    SDL_SetTextureScaleMode(game.spritesheet, SDL_SCALEMODE_NEAREST);
  }

  if (game.background_texture == NULL) {
    game.background_texture = IMG_LoadTexture(renderer, "assets/Full-background.png");

    int window_height;
    float texture_width, texture_height;
    SDL_GetWindowSizeInPixels(window, NULL, &window_height);
    SDL_GetTextureSize(game.background_texture, &texture_width, &texture_height);

    float scale = window_height / texture_height;
    game.background_scaled_width = scale * texture_width;
    game.background_scaled_height = scale * texture_height;
  }

  if (game.effect_track == NULL) {
    game.effect_track = MIX_CreateTrack(mixer);
  }

  if (game.pop_audio == NULL) {
    game.pop_audio = MIX_LoadAudio(mixer, "assets/pop-sound.mp3", false);
  }

  if (game.text_font.spritesheet == NULL) {
    load_sprite_font(renderer,
                     "assets/Fonts/Peaberry-Font-v2.0/Peaberry Bitmap Fonts/1. WhitePeaberry/2. White Peaberry/WhitePeaberry.txt",
                     &game.text_font);
  }

  game.board_width = BOARD_WIDTH;
  game.board_height = BOARD_HEIGHT;

  game.state = intro_state;
  game.selected_piece_id = -1;

  fill_board();
}

void init_timed_game(SDL_Renderer* renderer, MIX_Mixer* mixer, float time_remaining) {
  init_game(renderer, mixer);

  game.mode = timed_mode;
  game.timed_mode_data = (timed_mode_data) {
    .time_remaining = time_remaining
  };
}

void free_game() {
  free_sprite_font(&game.text_font);
  SDL_DestroyTexture(game.spritesheet);
  SDL_DestroyTexture(game.background_texture);
  MIX_DestroyTrack(game.effect_track);
  MIX_DestroyAudio(game.pop_audio);
}

SDL_FPoint lerp_points(SDL_FPoint a, SDL_FPoint b, float fraction) {
  SDL_FPoint target;

  target.x = fraction * b.x + (1 - fraction) * a.x;
  target.y = fraction * b.y + (1 - fraction) * a.y;

  return target;
}

void piece_start_falling(piece* piece) {
  float xvel_multiplier = 2*random_float_0to1() - 1;

  piece->xvel = xvel_multiplier * PIECE_MAX_FALL_XSPEED;
  piece->yvel = PIECE_FALL_YSPEED_INITIAL;

  piece->angular_velocity = (rand() % 2 ? 1 : -1) * PIECE_FALL_ROTATION_SPEED;
  piece->state = fall_state;
}

void update_piece(piece* piece, float dt) {
  switch (piece->state) {

  case linear_move_state:
    piece->fraction_travelled += dt * piece->travel_speed;

    if (piece->fraction_travelled >= 1) {
      piece->fraction_travelled = 1;
      piece->position = piece->dest_position;
      piece->state = static_state;
    } else {
      piece->position =
        lerp_points(piece->start_position, piece->dest_position, piece->fraction_travelled);
    }
    break;

  case fall_state:
    piece->position.x += piece->xvel * dt;
    piece->yvel += GRAVITY * dt / 2;
    piece->position.y += piece->yvel * dt;
    piece->yvel += GRAVITY * dt / 2;

    piece->orientation += piece->angular_velocity * dt;
    piece->orientation = SDL_max(-PIECE_MAX_ROTATION, SDL_min(PIECE_MAX_ROTATION, piece->orientation));
    break;

  default:
    break;
  }
}

int mouse_on_piece() {
  SDL_FPoint mouse_position;
  SDL_GetMouseState(&mouse_position.x, &mouse_position.y);

  for (int i = 0; i < game.num_pieces; i++) {
    const piece* piece = &game.pieces[i];

    SDL_FRect piece_box = (SDL_FRect){
      .x = piece->position.x,
      .y = piece->position.y,
      .w = PIECE_DEST_WIDTH,
      .h = PIECE_DEST_HEIGHT
    };

    if (SDL_PointInRectFloat(&mouse_position, &piece_box)) {
      for (int i = 0; i < game.board_height; i++) {
        for (int j = 0; j < game.board_width; j++) {
          if (game.board[i][j].state == tile_filled && game.board[i][j].piece_id == piece->piece_id)
            return piece->piece_id;
        }
      }
    }
  }

  return -1;
}

void update_intro_state() {
  if (game.state != intro_state)
    return;

  for (int i = 0; i < game.num_pieces; i++) {
    if (game.pieces[i].state != static_state && game.pieces[i].state != fall_state) {
      return;
    }
  }

  game.state = neutral_state;
}

static inline void find_piece_on_board
(
 int piece_id,
 int* board_y_pos,
 int* board_x_pos
 ) {
  *board_y_pos = -1;
  *board_x_pos = -1;

  for (int i = 0; i < game.board_height; i++) {
    for (int j = 0; j < game.board_width; j++) {
      if (filled_tile_at(i, j) && game.board[i][j].piece_id == piece_id) {
        *board_y_pos = i;
        *board_x_pos = j;

        return;
      }
    }
  }
}

void find_last_color_matching_tile_in_direction(int* sy, int* sx, int dy, int dx) {
  if (!filled_tile_at(*sy, *sx))
    return;

  piece* start_piece = retreive_piece(game.board[*sy][*sx].piece_id);

  for(;;) {
    int ny = *sy + dy, nx = *sx + dx;

    if (!filled_tile_at(ny, nx))
      return;

    piece* next_piece = retreive_piece(game.board[ny][nx].piece_id);

    if (!next_piece || next_piece->piece_color != start_piece->piece_color)
      return;

    *sy = ny; *sx = nx;
  }
}

void add_time_added_sprite(float time_added, float xpos, float ypos) {
  timed_mode_data* timed_mode_data = &game.timed_mode_data;

  //If there are any inactive sprites add there
  //if not use the oldest/most shrunk
  int add_sprite_at = 0;
  for (int i = 0; i < TIMED_MODE_NUM_TIME_ADDED_SPRITES; i++) {
    if (!timed_mode_data->time_added_sprites[i].active) {
      add_sprite_at = i;
      break;
    }

    text_sprite* s1 = &timed_mode_data->time_added_sprites[i];
    text_sprite* s2 = &timed_mode_data->time_added_sprites[add_sprite_at];

    if (s1->scale < s2->scale)
      add_sprite_at = i;
  }

  text_sprite* sprite = &timed_mode_data->time_added_sprites[add_sprite_at];
  sprite->active = true;
  snprintf(sprite->text, TEXT_SPRITE_LENGTH, "+%.2f", time_added);
  sprite->position = (SDL_FPoint){xpos, ypos};
  sprite->scale = TIME_ADDED_SPRITE_START_SCALE;
}

void timed_mode_process_clear(int sy, int sx, int ey, int ex) {
  timed_mode_data* timed_mode_data = &game.timed_mode_data;
    int tiles_cleared = SDL_abs(ey-sy) + SDL_abs(ex-sx) + 1;

  float time_added = 1;
  if (tiles_cleared > MIN_TILES_TO_CLEAR) {
    time_added +=
      (tiles_cleared - MIN_TILES_TO_CLEAR) * CLEAR_EXTRA_TIME_BONUS;
  }

  SDL_FPoint clear_position = board_position(sy, sx);
  add_time_added_sprite(time_added, clear_position.x, clear_position.y);
  timed_mode_data->time_remaining += time_added;
}

void process_clear(int sy, int sx, int ey, int ex) {
  if (game.mode == timed_mode) {
    timed_mode_process_clear(sy, sx, ey, ex);
  }
}

bool try_clear_at(int y, int x) {
  const int axes[2][2] = {
    {1, 0},    //Horizontal
    {0, 1}     //Vertical
  };

  for (int a = 0; a < 2; a++) {
    int sy = y, sx = x, dy = axes[a][1], dx = axes[a][0];
    find_last_color_matching_tile_in_direction(&sy, &sx, dy, dx);

    int ey = sy, ex = sx;
    find_last_color_matching_tile_in_direction(&ey, &ex, -dy, -dx);

    if (SDL_abs(ey-sy) + SDL_abs(ex-sx) + 1 < MIN_TILES_TO_CLEAR)
      continue;

    process_clear(sy, sx, ey, ex);

    for (;;) {
      piece_start_falling(retreive_piece(game.board[ey][ex].piece_id));
      game.board[ey][ex].state = tile_currently_empty;

      if (ey == sy && ex == sx)
        break;

      ey += dy; ex += dx;
    }

    return true;
  }

  return false;
}

void cascade_tiles() {
  for (int j = 0; j < game.board_width; j++) {
    for (int i = 0; i < game.board_height; i++) {
      if (game.board[i][j].state != tile_currently_empty)
        continue;

      //Look for the first filled tile above and start moving it down
      for (int c = i-1; c >= 0; c--) {
        if (game.board[c][j].state == tile_filled) {
          piece* piece = retreive_piece(game.board[c][j].piece_id);
          piece_start_moving_to(piece,
                                board_position(c+1, j),
                                PIECE_LINEAR_SPEED
                                );
          game.board[c+1][j] = game.board[c][j];
        }
      }

      add_piece_at(0, j);
    }
  }
}

static inline void play_sound_effect(MIX_Audio* audio) {
  MIX_SetTrackAudio(game.effect_track, audio);
  MIX_PlayTrack(game.effect_track, 0);
}

void update_piece_swap_animation_state() {
  if (game.state != swap_animation_state)
    return;

  for (int i = 0; i < game.num_pieces; i++) {
    if (game.pieces[i].state != static_state && game.pieces[i].state != fall_state)
      return;
  }

  int selected_piece_id = game.selected_piece_id;
  int swap_piece_id = game.swap_piece_id;

  int selected_y, selected_x;
  int swap_y, swap_x;

  find_piece_on_board(selected_piece_id, &selected_y, &selected_x);
  find_piece_on_board(swap_piece_id, &swap_y, &swap_x);

  if (filled_tile_at(selected_y, selected_x) && filled_tile_at(swap_y, swap_x)) {
    bool tiles_cleared = try_clear_at(selected_y, selected_x);
    tiles_cleared = try_clear_at(swap_y, swap_x) || tiles_cleared;

    if (tiles_cleared) {
      play_sound_effect(game.pop_audio);
      cascade_tiles();
      game.state = intro_state;
      return;
    }
  }

  game.state = neutral_state;
}

void handle_piece_rotation(float dt) {
  int selected_piece_id = mouse_on_piece();

  for (int i = 0; i < game.num_pieces; i++) {
    piece* piece = &game.pieces[i];

    if (piece->state == fall_state)
      continue;

    if (piece->piece_id == selected_piece_id) {
      piece->orientation =
        SDL_min(piece->orientation + PIECE_ROTATION_SPEED * dt,
                PIECE_MAX_ROTATION);
    } else {
      piece->orientation =
        SDL_max(piece->orientation - PIECE_ROTATION_SPEED * dt,
                0);
    }
  }
}

void scroll_background(float dt) {
  game.background_offset -= BACKGROUND_SCROLL_SPEED * dt;

  while (game.background_offset < -game.background_scaled_width) {
    game.background_offset += game.background_scaled_width;
  }
}


void delete_out_of_screen_pieces() {
  int window_width, window_height;
  SDL_GetWindowSizeInPixels(window, &window_width, &window_height);

  for (int i = 0; i < game.num_pieces; i++) {
    if (game.pieces[i].position.y > window_height + DELETE_PIECE_OFFSET) {
      delete_piece(game.pieces[i].piece_id);
    }
  }
}

void update_timed_mode(float dt);

void update_game(float dt) {
  for (int i = 0; i < game.num_pieces; i++) {
    update_piece(&game.pieces[i], dt);
  }
  delete_out_of_screen_pieces();
  handle_piece_rotation(dt);
  scroll_background(dt);

  update_intro_state();
  update_piece_swap_animation_state();

  update_timed_mode(dt);
}

void draw_piece(SDL_Renderer* renderer, const piece* piece) {
  const int piece_source_xoffs[] = {4, 209};

  SDL_RenderTextureRotated(renderer,
                           game.spritesheet,
                           &(SDL_FRect){
                             .x = piece_source_xoffs[piece->piece_color / 9] + piece->piece_face * (PIECE_SOURCE_WIDTH + 1),
                             .y = PIECE_SOURCE_YOFFSET + (piece->piece_color % 9) * (PIECE_SOURCE_HEIGHT + 1),
                             .w = PIECE_SOURCE_WIDTH,
                             .h = PIECE_SOURCE_HEIGHT
                           },
                           &(SDL_FRect){
                             .x = piece->position.x,
                             .y = piece->position.y,
                             .w = PIECE_DEST_WIDTH,
                             .h = PIECE_DEST_HEIGHT
                           },
                           piece->orientation,
                           NULL,
                           SDL_FLIP_NONE
                           );
}

void draw_background(SDL_Renderer* renderer) {
  int x_offset = game.background_offset;

  while (x_offset <= 800) {
    SDL_RenderTexture(renderer,
                      game.background_texture,
                      NULL,
                      &(SDL_FRect){
                        .x = x_offset,
                        .y = 0,
                        .w = game.background_scaled_width,
                        .h = game.background_scaled_height
                      });

    x_offset += game.background_scaled_width;
  }
}

void draw_text_sprite
(
 SDL_Renderer* renderer,
 sprite_font* font,
 const text_sprite* text_sprite
) {
  if (!text_sprite->active)
    return;

  draw_text_with_spritefont_extended(renderer,
                                     font,
                                     (unsigned char*)text_sprite->text,
                                     text_sprite->position.x,
                                     text_sprite->position.y,
                                     text_sprite->scale
                                     );
}

void draw_timed_mode(SDL_Renderer* renderer);

void draw_game(SDL_Renderer* renderer) {
  SDL_RenderClear(renderer);

  draw_background(renderer);

  piece* selected_piece = NULL;
  for (int i = 0; i < game.num_pieces; i++) {
    if (game.pieces[i].piece_id == game.selected_piece_id) {
      selected_piece = &game.pieces[i];
      continue;
    }
    draw_piece(renderer, &game.pieces[i]);
  }

  //Gives it the apearance that you are sliding your tiles over
  if (selected_piece) {
    draw_piece(renderer, selected_piece);
  }

  if (game.mode == timed_mode) {
    draw_timed_mode(renderer);
  }

  SDL_RenderPresent(renderer);
}

void neutral_state_event(SDL_Event* e) {
  if (e->type == SDL_EVENT_MOUSE_BUTTON_DOWN && e->button.button == SDL_BUTTON_LEFT) {
    int selected_piece_id = mouse_on_piece();

    if (selected_piece_id >= 0) {
      game.selected_piece_id = selected_piece_id;
      game.state = piece_held_state;
    }
  }
}

void swap_pieces_at(int x1, int y1, int x2 , int y2) {
  int id1 = game.board[y1][x1].piece_id;
  int id2 = game.board[y2][x2].piece_id;

  piece* selected = retreive_piece(id1);
  piece* swap = retreive_piece(id2);

  if (selected && swap) {
    piece_start_moving_to(selected, board_position(y2, x2), PIECE_SWAP_SPEED);
    piece_start_moving_to(swap, board_position(y1, x1), PIECE_SWAP_SPEED);

    game.board[y2][x2].piece_id = id1;
    game.board[y1][x1].piece_id = id2;
  }
}

void piece_held_event(SDL_Event* e) {
  if (e->type == SDL_EVENT_MOUSE_BUTTON_UP && e->button.button == SDL_BUTTON_LEFT) {
    int swap_piece_id = mouse_on_piece();
    int selected_piece_id = game.selected_piece_id;

    if (swap_piece_id >= 0) {
      int selected_x, selected_y;
      int swap_x, swap_y;

      for (int i = 0; i < game.board_height; i++) {
        for (int j = 0; j < game.board_width; j++) {
          if (game.board[i][j].state != tile_filled)
            continue;

          if (game.board[i][j].piece_id == selected_piece_id) {
            selected_x = j;
            selected_y = i;
          } if (game.board[i][j].piece_id == swap_piece_id) {
            swap_x = j;
            swap_y = i;
          }

        }
      }

      //If pieces are more that one tile apart or
      //they are of the same color do nothing
      if (SDL_abs(selected_x-swap_x) + SDL_abs(selected_y-swap_y) != 1 ||
          retreive_piece(selected_piece_id)->piece_color == retreive_piece(swap_piece_id)->piece_color) {
        game.state = neutral_state;
        return;
      }

      swap_pieces_at(selected_x, selected_y, swap_x, swap_y);
      game.swap_piece_id = swap_piece_id;

      game.state = swap_animation_state;
      return;
    }

    game.state = neutral_state;
  }
}

void game_event(SDL_Event* e) {
  if (game.state == neutral_state) {
    neutral_state_event(e);
  } else if (game.state == piece_held_state) {
    piece_held_event(e);
  }
}


//**********************************************
//*****************TIMED MODE*******************
//*********************************************/
void update_timed_mode(float dt) {
  timed_mode_data* timed_mode_data = &game.timed_mode_data;

  timed_mode_data->time_remaining =
    SDL_max(timed_mode_data->time_remaining - dt, 0);

  for (int i = 0; i < TIMED_MODE_NUM_TIME_ADDED_SPRITES; i++) {
    text_sprite* text_sprite = &timed_mode_data->time_added_sprites[i];

    if (!text_sprite->active)
      continue;

    text_sprite->position.y += TIME_ADDED_SPRITE_YVEL * dt;
    text_sprite->scale -= TIME_ADDED_SPRITE_SHRINK_RATE * dt;

    if (text_sprite->scale <= TIME_ADDED_SPRITE_MINIMUM_SCALE) {
      text_sprite->active = false;
    }
  }
}

void draw_timed_mode(SDL_Renderer* renderer) {
  timed_mode_data* timed_mode_data = &game.timed_mode_data;

  char timed_mode_display[50];
  snprintf(timed_mode_display, 50, "%.1fs", timed_mode_data->time_remaining);

  //Slight wobble effect when running low on time
  float scale = 2;
  if (timed_mode_data->time_remaining <= LOW_TIME_REMAINING) {
    scale += 0.25 * SDL_sin(-timed_mode_data->time_remaining * 4);
  }

  for (int i = 0; i < TIMED_MODE_NUM_TIME_ADDED_SPRITES; i++) {
    text_sprite* text_sprite = &timed_mode_data->time_added_sprites[i];

    if (!text_sprite->active)
      continue;
    draw_text_sprite(renderer, &game.text_font, text_sprite);
  }

  draw_text_with_spritefont_extended(renderer,
                                     &game.text_font,
                                     (unsigned char*)timed_mode_display,
                                     get_window_relative_xpos(0.5),
                                     get_window_relative_ypos(0.1),
                                     scale
                                     );
}