mirror of
https://github.com/thepigeongenerator/tetris_clone
synced 2026-02-09 11:52:55 +01:00
move timer data to static variables in the update function, rather than storing it in the gamedata.
This commit is contained in:
@@ -58,9 +58,6 @@ void game_init(gamedata* const dat) {
|
|||||||
ad, // audio_device
|
ad, // audio_device
|
||||||
audio_wav_load(ad, "korobeiniki.wav"), // music
|
audio_wav_load(ad, "korobeiniki.wav"), // music
|
||||||
audio_wav_load(ad, "place.wav"), // place_sfx
|
audio_wav_load(ad, "place.wav"), // place_sfx
|
||||||
0, // timer_music
|
|
||||||
0, // timer_update
|
|
||||||
0, // timer_input
|
|
||||||
0, // score
|
0, // score
|
||||||
{0}, // nxt
|
{0}, // nxt
|
||||||
0, // curr_idx
|
0, // curr_idx
|
||||||
@@ -93,6 +90,9 @@ static inline void update_gametime(gamedata* dat) {
|
|||||||
|
|
||||||
// called every time the game's state is updated
|
// called every time the game's state is updated
|
||||||
void game_update(gamedata* const dat) {
|
void game_update(gamedata* const dat) {
|
||||||
|
static time_t timer_update = 0;
|
||||||
|
static time_t timer_music = 0;
|
||||||
|
static time_t timer_input = 0;
|
||||||
update_gametime(dat);
|
update_gametime(dat);
|
||||||
uint8_t const* keys = SDL_GetKeyboardState(NULL);
|
uint8_t const* keys = SDL_GetKeyboardState(NULL);
|
||||||
|
|
||||||
@@ -102,17 +102,17 @@ void game_update(gamedata* const dat) {
|
|||||||
input_data move = MOVE_NONE; // contains the move data
|
input_data move = MOVE_NONE; // contains the move data
|
||||||
time_t ctime = dat->time.ms;
|
time_t ctime = dat->time.ms;
|
||||||
|
|
||||||
if (ctime > dat->timer_update) {
|
if (ctime > timer_update) {
|
||||||
dat->timer_update = ctime + 500;
|
timer_update = ctime + 500;
|
||||||
move |= MOVE_DOWN;
|
move |= MOVE_DOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctime > dat->timer_music) {
|
if (ctime > timer_music) {
|
||||||
dat->timer_music = ctime + (dat->music.ms);
|
timer_music = ctime + (dat->music.ms);
|
||||||
audio_play(dat->audio_device, &dat->music);
|
audio_play(dat->audio_device, &dat->music);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctime > dat->timer_input) {
|
if (ctime > timer_input) {
|
||||||
input_data umove = MOVE_NONE;
|
input_data umove = MOVE_NONE;
|
||||||
|
|
||||||
// get the input data and apply it to move
|
// get the input data and apply it to move
|
||||||
@@ -123,7 +123,7 @@ void game_update(gamedata* const dat) {
|
|||||||
if (keys[SDL_SCANCODE_E]) umove |= MOVE_ROTRIGHT;
|
if (keys[SDL_SCANCODE_E]) umove |= MOVE_ROTRIGHT;
|
||||||
|
|
||||||
if (umove != MOVE_NONE) {
|
if (umove != MOVE_NONE) {
|
||||||
dat->timer_input = ctime + 80;
|
timer_input = ctime + 20;
|
||||||
move |= umove;
|
move |= umove;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#include "../window/audio.h"
|
#include "../window/audio.h"
|
||||||
#include "../window/colour/colour8.h"
|
#include "../window/colour/colour8.h"
|
||||||
@@ -29,9 +28,6 @@ typedef struct {
|
|||||||
audiodevice* audio_device;
|
audiodevice* audio_device;
|
||||||
audiodata music;
|
audiodata music;
|
||||||
audiodata place_sfx;
|
audiodata place_sfx;
|
||||||
time_t timer_music;
|
|
||||||
time_t timer_update;
|
|
||||||
time_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
|
||||||
|
|||||||
Reference in New Issue
Block a user