1
1
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

217 lines
5.0 KiB

#include <SDL2/SDL_mutex.h>
#include <SDL2/SDL_scancode.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL2/SDL.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
#include "logger.h"
#include "game.h"
#include "draw.h"
#include "garbo.h"
const int screen_width = 800;
const int screen_height = 600;
SDL_sem *resume;
long int *times;
struct sg_times_list {
long *times;
int size;
int capacity;
void (*sort)(void);
};
struct sg_times_list save_times_lst;
struct SDL_Window* make_window(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
}
return SDL_CreateWindow("sdl_tester",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
0, 0,
SDL_WINDOW_FULLSCREEN_DESKTOP);
}
void redraw(struct SDL_Renderer * ren) {
SDL_RenderClear(ren);
redraw_buffer(ren);
SDL_RenderPresent(ren);
}
int physics_loop(void *ptr) {
game_paused = 1;
SDL_Delay(1000);
game_paused = 0;
while (1) {
if (step()) {
// display end level screen
game_paused = true;
SDL_Delay(1000);
in_game = false;
SDL_SemWait(resume);
SDL_LockMutex(player.physics->lock);
game_paused = true;
in_game = true;
next_level();
SDL_UnlockMutex(player.physics->lock);
SDL_Delay(1000);
game_paused = 0;
}
}
}
int game(void) {
LOGLEVEL = DEBUG;
STDOUTLEVEL = DEBUG;
//SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "2" );
logwrite(INFO, "Starting\n");
SDL_Window * win = make_window();
SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
// | SDL_RENDERER_PRESENTVSYNC);
queue_for_cleanup(win, WINDOW);
queue_for_cleanup(ren, RENDERER);
in_game = true;
resume = SDL_CreateSemaphore(0);
// IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG);
if (ren == NULL) {
SDL_DestroyWindow(win);
SDL_Quit();
}
TTF_Init();
int close = 0;
//draw_pictures(ren);
SDL_Thread *physics_thread;
int ignore;
startgame(ren);
physics_thread = SDL_CreateThread(physics_loop, "Physics", (void *)ren);
while (!close) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
close = 1;
return 0;
case SDL_KEYDOWN:
if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
return 0;
}
if (!SDL_SemValue(resume)) {
SDL_SemPost(resume);
}
case SDL_KEYUP:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
handle_input_event (event);
}
}
/* Redraw Screen */
// step(10);
if (!in_game) {
draw_end_screen(ren);
} else {
redraw(ren);
}
}
}
int long_comparator(const void *a, const void *b, void *non) {
long A = *(long *)a;
long B = *(long *)b;
return A - B;
}
void sort_times(void) {
qsort_r(save_times_lst.times, save_times_lst.size, sizeof(long), long_comparator, NULL);
}
int load_score_times(char *filename) {
FILE *f = fopen(filename, "r");
if (f) {
// get file size
fseek(f, 0L, SEEK_END);
long sz = ftell(f);
fseek(f, 0L, SEEK_SET);
char *text = malloc((sz + 1) * (sizeof(char)));
// read file
fread(text,sizeof(char),sz, f);
fclose(f);
int count = 1;
for (int i = 0; i < strlen(text); i++) {
if (text[i] == '\n') {
count++;
}
}
long *times = malloc(count * sizeof(long));
char *saveptr;
// extract times
char * token = strtok_r(text, "\n", &saveptr);
int i = 0;
while (token != NULL) {
if (strlen(token) > 2) {
times[i] = atol(token);
i++;
}
token = strtok_r(NULL, "\n", &saveptr);
}
save_times_lst = (struct sg_times_list){.size=i, .capacity = count, .times=times, .sort=sort_times};
save_times_lst.sort();
} else {
perror("loading times");
save_times_lst = (struct sg_times_list){.size=0, .capacity = 5, .times=calloc(5, sizeof(long)), .sort=sort_times};
}
for (int i = 0; i < save_times_lst.size; i++) {
printf("%ld\n", save_times_lst.times[i]);
}
return 0;
}
long get_best_time(void) {
save_times_lst.sort();
return save_times_lst.times[0];
}
int main (int argc, char** argv) {
load_score_times("savegame");
game();
SDL_Quit();
//empty_cleanup_queue();
return 0;
}