mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
add game times
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
|
||||
#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
|
||||
|
||||
@@ -9,7 +9,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user