diff --git a/src/game/game.c b/src/game/game.c index c329d0f..124697d 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -12,6 +12,7 @@ #include "../window/colour8.h" #include "./tetromino/shapes.h" #include "SDL_audio.h" +#include "SDL_timer.h" #include "tetromino/placing.h" // shuffle the array using a Fisher–Yates shuffle @@ -72,19 +73,38 @@ void game_update(game_data* const dat, uint8_t const* const keys) { if (keys[SDL_SCANCODE_ESCAPE]) stop(); - time_t ctime = time(NULL); - if (ctime > dat->music_timer) { - dat->music_timer = ctime + dat->music.sec; + InputData move = MOVE_NONE; // contains the move data + uint32_t ctime = SDL_GetTicks(); + + if (ctime > dat->timer_update) { + dat->timer_update = ctime + 500; + move |= MOVE_DOWN; + } + + if (ctime > dat->timer_music) { + dat->timer_music = ctime + (dat->music.ms); audio_play(dat->audio_device, &dat->music); } - InputData move = MOVE_NONE; - if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A]) move |= MOVE_LEFT; - if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D]) move |= MOVE_RIGHT; - if (keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S] || keys[SDL_SCANCODE_SPACE]) move |= MOVE_DOWN; - if (keys[SDL_SCANCODE_Q]) move |= MOVE_ROTLEFT; - if (keys[SDL_SCANCODE_E]) move |= MOVE_ROTRIGHT; - place_update(dat, move); + if (ctime > dat->timer_input) { + InputData umove = MOVE_NONE; + + // get the input data and apply it to move + if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A]) umove |= MOVE_LEFT; + if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D]) umove |= MOVE_RIGHT; + if (keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S] || keys[SDL_SCANCODE_SPACE]) umove |= MOVE_DOWN; + if (keys[SDL_SCANCODE_Q]) umove |= MOVE_ROTLEFT; + if (keys[SDL_SCANCODE_E]) umove |= MOVE_ROTRIGHT; + + if (umove != MOVE_NONE) { + dat->timer_input = ctime + 60; + move |= umove; + } + } + + // update the block position + if (move != MOVE_NONE) + place_update(dat, move); } void game_free(game_data* const dat) { diff --git a/src/game/game.h b/src/game/game.h index 3ec4285..71dbf7e 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include "../window/audio.h" #include "../window/colour8.h" @@ -18,7 +17,9 @@ typedef struct { row rows[ROWS]; audio_device* audio_device; audio_data music; - time_t music_timer; + uint32_t timer_music; + uint32_t timer_update; + uint32_t timer_input; uint16_t score; shape_id nxt[7]; // the order of the shape ids that they should appear in uint8_t curr_idx; // current shape index diff --git a/src/main.c b/src/main.c index c7ac655..a6503ac 100644 --- a/src/main.c +++ b/src/main.c @@ -9,7 +9,9 @@ #include #include #include +#include +#include "SDL_render.h" #include "SDL_timer.h" #include "SDL_ttf.h" #include "errors.h" @@ -71,9 +73,10 @@ int main(int const argc, char const* const* const argv) { init(); + printf("target framerate: %u\n", 0); + while (playing) { update(); - SDL_Delay(50); } // cleanup of resources diff --git a/src/window/audio.c b/src/window/audio.c index 5fa0f3b..a014257 100644 --- a/src/window/audio.c +++ b/src/window/audio.c @@ -86,7 +86,7 @@ audio_data audio_wav_load(audio_device const* const dev, char const* const fpath convert_audio(dev, wav_spec, &audio.buf, &audio.len); // calculate the amount of seconds that the audio fragment has - audio.sec = (audio.len / (SDL_AUDIO_BITSIZE(dev->fmt) / 8)) / wav_spec.channels / dev->freq; + audio.ms = 1000 * (((audio.len) / (SDL_AUDIO_BITSIZE(dev->fmt) / 8)) / wav_spec.channels / dev->freq); return audio; } diff --git a/src/window/audio.h b/src/window/audio.h index 2b7e455..99f223f 100644 --- a/src/window/audio.h +++ b/src/window/audio.h @@ -6,7 +6,7 @@ typedef struct { uint8_t* buf; // the audio buffer uint32_t len; // length in bytes of the audio buffer - uint32_t sec; // length in seconds of the audio buffer + uint32_t ms; // length in seconds of the audio buffer } audio_data; typedef struct audio_player {