add game times

This commit is contained in:
2025-02-11 11:45:19 +01:00
parent 5db3519a87
commit 3b5b7bf15b
5 changed files with 39 additions and 15 deletions

View File

@@ -12,6 +12,7 @@
#include "../window/colour8.h" #include "../window/colour8.h"
#include "./tetromino/shapes.h" #include "./tetromino/shapes.h"
#include "SDL_audio.h" #include "SDL_audio.h"
#include "SDL_timer.h"
#include "tetromino/placing.h" #include "tetromino/placing.h"
// shuffle the array using a FisherYates shuffle // shuffle the array using a FisherYates shuffle
@@ -72,19 +73,38 @@ void game_update(game_data* const dat, uint8_t const* const keys) {
if (keys[SDL_SCANCODE_ESCAPE]) if (keys[SDL_SCANCODE_ESCAPE])
stop(); stop();
time_t ctime = time(NULL); InputData move = MOVE_NONE; // contains the move data
if (ctime > dat->music_timer) { uint32_t ctime = SDL_GetTicks();
dat->music_timer = ctime + dat->music.sec;
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); audio_play(dat->audio_device, &dat->music);
} }
InputData move = MOVE_NONE; if (ctime > dat->timer_input) {
if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A]) move |= MOVE_LEFT; InputData umove = MOVE_NONE;
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; // get the input data and apply it to move
if (keys[SDL_SCANCODE_Q]) move |= MOVE_ROTLEFT; if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A]) umove |= MOVE_LEFT;
if (keys[SDL_SCANCODE_E]) move |= MOVE_ROTRIGHT; if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D]) umove |= MOVE_RIGHT;
place_update(dat, move); 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) { void game_free(game_data* const dat) {

View File

@@ -1,7 +1,6 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include <time.h>
#include "../window/audio.h" #include "../window/audio.h"
#include "../window/colour8.h" #include "../window/colour8.h"
@@ -18,7 +17,9 @@ typedef struct {
row rows[ROWS]; row rows[ROWS];
audio_device* audio_device; audio_device* audio_device;
audio_data music; audio_data music;
time_t music_timer; uint32_t timer_music;
uint32_t timer_update;
uint32_t timer_input;
uint16_t score; uint16_t score;
shape_id nxt[7]; // the order of the shape ids that they should appear in shape_id nxt[7]; // the order of the shape ids that they should appear in
uint8_t curr_idx; // current shape index uint8_t curr_idx; // current shape index

View File

@@ -9,7 +9,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <wchar.h>
#include "SDL_render.h"
#include "SDL_timer.h" #include "SDL_timer.h"
#include "SDL_ttf.h" #include "SDL_ttf.h"
#include "errors.h" #include "errors.h"
@@ -71,9 +73,10 @@ int main(int const argc, char const* const* const argv) {
init(); init();
printf("target framerate: %u\n", 0);
while (playing) { while (playing) {
update(); update();
SDL_Delay(50);
} }
// cleanup of resources // cleanup of resources

View File

@@ -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); convert_audio(dev, wav_spec, &audio.buf, &audio.len);
// calculate the amount of seconds that the audio fragment has // 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; return audio;
} }

View File

@@ -6,7 +6,7 @@
typedef struct { typedef struct {
uint8_t* buf; // the audio buffer uint8_t* buf; // the audio buffer
uint32_t len; // length in bytes of 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; } audio_data;
typedef struct audio_player { typedef struct audio_player {