summaryrefslogtreecommitdiff
path: root/src/game.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.c')
-rw-r--r--src/game.c54
1 files changed, 31 insertions, 23 deletions
diff --git a/src/game.c b/src/game.c
index c4dd0e0..bac0e6c 100644
--- a/src/game.c
+++ b/src/game.c
@@ -36,6 +36,7 @@
#define BACKGROUND_SCROLL_SPEED 40
#define MAX_CLEARED_TILES_STORED 50
+#define LOW_TIME_REMAINING 20 //In seconds
typedef enum {
linear_move_state,
@@ -110,13 +111,12 @@ typedef struct {
SDL_Texture* background_texture;
float background_offset;
- sprite_font f;
+ sprite_font text_font;
float background_scaled_width;
float background_scaled_height;
//Sounds
- MIX_Track* background_music_track;
MIX_Track* effect_track;
MIX_Audio* pop_audio;
@@ -129,6 +129,20 @@ 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;
}
@@ -232,6 +246,7 @@ void clear_board() {
}
void init_game(SDL_Renderer* renderer, MIX_Mixer* mixer) {
+ clear_board();
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
if (game.spritesheet == NULL) {
@@ -252,14 +267,6 @@ void init_game(SDL_Renderer* renderer, MIX_Mixer* mixer) {
game.background_scaled_height = scale * texture_height;
}
- if (game.background_music_track == NULL) {
- MIX_Audio* music_audio = MIX_LoadAudio(mixer, "assets/Music/background.mp3", false);
- game.background_music_track = MIX_CreateTrack(mixer);
-
- MIX_SetTrackAudio(game.background_music_track, music_audio);
- MIX_DestroyAudio(music_audio);
- }
-
if (game.effect_track == NULL) {
game.effect_track = MIX_CreateTrack(mixer);
}
@@ -268,17 +275,12 @@ void init_game(SDL_Renderer* renderer, MIX_Mixer* mixer) {
game.pop_audio = MIX_LoadAudio(mixer, "assets/pop-sound.mp3", false);
}
- if (game.f.spritesheet == NULL) {
+ 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.f);
+ &game.text_font);
}
- SDL_PropertiesID props = SDL_CreateProperties();
- SDL_SetNumberProperty(props, MIX_PROP_PLAY_LOOPS_NUMBER, -1);
- MIX_PlayTrack(game.background_music_track, props);
- SDL_DestroyProperties(props);
-
game.board_width = BOARD_WIDTH;
game.board_height = BOARD_HEIGHT;
@@ -298,10 +300,9 @@ void init_timed_game(SDL_Renderer* renderer, MIX_Mixer* mixer, float time_remain
}
void free_game() {
- free_sprite_font(&game.f);
+ free_sprite_font(&game.text_font);
SDL_DestroyTexture(game.spritesheet);
SDL_DestroyTexture(game.background_texture);
- MIX_DestroyTrack(game.background_music_track);
MIX_DestroyTrack(game.effect_track);
MIX_DestroyAudio(game.pop_audio);
}
@@ -751,12 +752,19 @@ 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", timed_mode_data->time_remaining);
+ 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);
+ }
draw_text_with_spritefont_extended(renderer,
- &game.f,
+ &game.text_font,
(unsigned char*)timed_mode_display,
- 0, 0,
- 2.0
+ get_window_relative_xpos(0.5),
+ get_window_relative_ypos(0.1),
+ scale
);
}