Various improvements

- Better score saving with error handling
- Added OPK support (yay!)
- Improved README
This commit is contained in:
MassiveBox 2022-08-30 12:13:26 +02:00
parent db5163c945
commit a3f0d66c2b
9 changed files with 87 additions and 21 deletions

View file

@ -4,8 +4,11 @@
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_sound.h"
#include "SDL/SDL_sound.h"

View file

@ -1,15 +1,40 @@
#include "include.h"
void get_best_score_path(char* reference) {
sprintf(reference, "%s/.jiljil/best_score", getenv("HOME"));
}
// get_best_score_from_file loads the best saved score from best_score.txt and puts it to best_score
int get_best_score_from_file(void) {
if(access("./best_score.txt", F_OK) != 0) {
char best_score_path[64];
get_best_score_path(best_score_path);
if(access(best_score_path, F_OK) != 0) {
printf("Couldn't read best score file at %s. This is normal if it's your first play.\n", best_score_path);
errno = 0;
char dirtomake[64];
sprintf(dirtomake, "%s/.jiljil/", getenv("HOME"));
if(mkdir(dirtomake, S_IRWXU) == -1) {
switch (errno) {
case EACCES :
printf("The home directory does not to allow write the .jiljil directory\n");
case EEXIST:
printf("Pathname already exists\n");
case ENAMETOOLONG:
printf("Pathname is too long\n");
default:
perror("Error creating directory");
}
}
return 0;
}
FILE *fp;
char buff[10];
fp = fopen("./best_score.txt", "r");
fp = fopen(best_score_path, "r");
fscanf(fp, "%s", buff);
int bs;
sscanf(buff, "%d", &bs);
@ -17,11 +42,21 @@ int get_best_score_from_file(void) {
}
// save_best_score_to_file puts the best score (from global var best_score) to best_score.txt
// save_best_score_to_file puts the best score to best_score.txt
void save_best_score_to_file(int score) {
char best_score_path[64];
get_best_score_path(best_score_path);
FILE *fp;
int best_score = score;
FILE *fp = fopen("./best_score.txt", "w");
fp = fopen(best_score_path, "w");
if(fp == NULL) {
perror("Best score has NOT ben saved! Error opening file to save score");
return;
}
char snum[5];
sprintf(snum, "%d", best_score);
fputs(snum, fp);
@ -83,6 +118,7 @@ void lemon_cap(float *speed_x, float *speed_y) {
entity_cap(speed_x, speed_y);
}
// avoid player flickering because the speed is close to zero, but enough to make it move slightly
void player_approximate(float *player_speed_x, float *player_speed_y) {
if(*player_speed_x > -0.1 && *player_speed_x < 0.1) {
*player_speed_x = 0;
@ -132,4 +168,4 @@ int check_player_inside_prints(int tail_x, int tail_y, int paw1_x, int paw1_y, i
return 0;
}
}

View file

@ -33,7 +33,6 @@ int pause_game(SDL_Surface *screen) {
case SDLK_BACKSPACE: //R -> reset
resume_bgm();
return 2;
break;
case SDLK_LSHIFT: //Y -> clear best score
save_best_score_to_file(0);
resume_bgm();
@ -247,8 +246,13 @@ int match(SDL_Surface *screen) {
Uint32 end = SDL_GetTicks();
float elapsedMS = end - start;
float sleepFor = floor(16.666f - elapsedMS);
SDL_Delay(floor(16.666f - elapsedMS));
if(sleepFor < 0) {
sleepFor = 0; // just to be safe
}
SDL_Delay(sleepFor);
}
@ -274,7 +278,9 @@ int match(SDL_Surface *screen) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE: //select -> pause menu
pause_game(screen);
if(pause_game(screen) == 1){
return 1;
}
case SDLK_RETURN: //start -> new game
return 0;
default:
@ -348,4 +354,4 @@ int boot_animation(SDL_Surface *screen) {
SDL_Delay(100);
}
}
}