simple rework for time

This commit is contained in:
Frank Villaro-Dixon 2015-01-04 10:43:15 +01:00
parent f2cef6b0f5
commit 315b36f389
6 changed files with 34 additions and 7 deletions

View file

@ -1,5 +1,5 @@
all:
clang src/*.c -g3 `sdl-config --cflags --libs` -lSDL_mixer -lm -Wall -Wextra -o hyper_rotagon
clang src/*.c -g3 `sdl-config --cflags --libs` -lSDL_mixer -lSDL_ttf -lm -Wall -Wextra -o hyper_rotagon
clean:
rm hyper_rotagon

View file

@ -167,3 +167,15 @@ init_game(struct s_game *g) { //{{{
init_obs(g);
} //}}}
void
update_game_time(struct s_game *g) {
struct timespec actual_time;
clock_gettime(CLOCK_REALTIME, &actual_time);
float secs_passed = actual_time.tv_sec - g->start_time.tv_sec +
(actual_time.tv_nsec - g->start_time.tv_nsec) / 1e9;
g->elapsed_time = secs_passed;
}

View file

@ -79,6 +79,7 @@ struct s_game {
struct timespec start_time;
float elapsed_time;
struct {
int avail_musics;
@ -119,6 +120,9 @@ change_game_color(struct s_game *g);
void
init_random_color(struct s_game *g);
void
update_game_time(struct s_game *g);
#endif /* ndef GAME_H */

View file

@ -37,6 +37,8 @@ start:
//clear the screen
SDL_FillRect(ga.screen, NULL, 0);
update_game_time(&ga);
if(did_have_beat(&ga)) {
ga.sexual_pulsation = 40;

View file

@ -115,11 +115,7 @@ did_have_beat(struct s_game *g) {
if(music == -1)
return 0;
struct timespec actual_time;
clock_gettime(CLOCK_REALTIME, &actual_time);
float secs_passed = actual_time.tv_sec - g->start_time.tv_sec +
(actual_time.tv_nsec - g->start_time.tv_nsec) / 1e9;
float secs_passed = g->elapsed_time;
if(g->audio.num_beats[music] > g->audio.beat_already_done) {
if(g->audio.beats[music][g->audio.beat_already_done] < secs_passed) {

View file

@ -7,17 +7,30 @@
#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL.h"
#include "SDL2/SDL_ttf.h"
#include "game.h"
void
init_SDL(struct s_game *g)
{
/* initialize SDL */
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
perror("Could not init SDL !");
exit(EXIT_FAILURE);
}
/* set the title bar */
SDL_WM_SetCaption("SDL Move", "SDL Move");
/* create window */
g->screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);
if(!g->screen) {
perror("Could not grab screen !");
exit(EXIT_FAILURE);
}
if(TTF_Init() < 0) {
perror("Could not load SDL TTF");
exit(EXIT_FAILURE);
}
}