Project

General

Profile

Revision 348

scoreboard initials

View differences:

proj/include/scoreboards.h
1
#ifndef SCOREBOARD_H_INCLUDED
2
#define SCOREBOARD_H_INCLUDED
3

  
4
/**
5
 * @defgroup proj_structures proj_structures
6
 * @brief Project structures.
7
 *
8
 * @{
9
 */
10

  
11
#include <stdint.h>
12
#include "font.h"
13

  
14
/**
15
 * @brief Score.
16
 */
17
typedef struct  score_info score_info_t;
18

  
19
/**
20
 * @brief Table of highscores.
21
 */
22
typedef struct highscores highscores_t;
23
/**
24
 * @brief Construct score.
25
 * @param   score           Game score.
26
 * @param   time_played     Time elapsed on game.
27
 * @return      Pointer to constructed score, or NULL if failed.
28
 */
29
score_info_t* (score_ctor)(int score, int time_played);
30

  
31
/**
32
 * @brief Destruct score.
33
 * @param   p   Pointer to score to destruct
34
 */
35
void (score_dtor)(score_info_t *p);
36

  
37
/**
38
 * @brief Construct highscores.
39
 * @param   fnt     Font to use when rendering highscores text
40
 * @param   path    Path to file to be read with highscores values.
41
 * @return      Pointer to constructed highscores, or NULL if failed.
42
 */
43
highscores_t* (highscores_ctor)(const font_t *fnt, const char *path);
44
/**
45
 * @brief Save highscore into file.
46
 * @param f     Path to file to be written
47
 */
48
void (highscores_save)(const highscores_t *p, const char *path);
49
/**
50
 * @brief Destruct menu.
51
 * @param   p   Pointer to menu to destruct
52
 */
53
void (highscores_dtor)(highscores_t *p);
54

  
55

  
56

  
57

  
58
#endif /* end of include guard: SCOREBOARD_H_INCLUDED */
0 59

  
proj/src/scoreboards.c
1
#include <lcom/lcf.h>
2

  
3
#include "scoreboards.h"
4

  
5
#include "graph.h"
6
#include "rectangle.h"
7
#include "proj_func.h"
8
#include "proj_macros.h"
9
#include "rtc.h"
10
#include "errors.h"
11

  
12
#include <stdio.h>
13

  
14
#define MAX_HIGHSCORES   3 /** @brief Number of scores saved */
15

  
16
struct  score_info {
17
    int day;
18
    int month;
19
    int year;
20
    int hour;
21
    int min;
22
    int sec;
23
    int score;
24
    int time_played;
25
};
26

  
27
struct highscores {
28
    score_info_t *highscores[MAX_HIGHSCORES];
29

  
30
    const font_t *fnt;
31
    rectangle_t *r[(MAX_HIGHSCORES+1)*3 + 1];
32
    text_t      *t[(MAX_HIGHSCORES+1)*3 + 1];
33
    rectangle_t *frame;
34
};
35

  
36
score_info_t* (score_ctor)(int score, int time_played){
37
    score_info_t *ret = (score_info_t*)malloc(sizeof(score_info_t));
38
    if (ret == NULL) return NULL;
39
    uint8_t score_date[4];
40
    if (rtc_read_date(score_date)) return NULL;
41
    ret->sec = score_date[0];
42
    ret->min = score_date[1];
43
    ret->hour = score_date[2];
44

  
45
    if (rtc_read_time(score_date)) return NULL;
46

  
47
    ret->day = score_date[1];
48
    ret->month = score_date[2];
49
    ret->year = score_date[3];
50

  
51
    ret->score = score;
52
    ret->time_played = time_played;
53

  
54
    return ret;
55
}
56

  
57
void (score_dtor)(score_info_t *p){
58
    if (p != NULL) free(p);
59
}
60

  
61
highscores_t* (highscores_ctor)(const font_t *fnt, const char *path) {
62
    if(fnt == NULL || path == NULL) return NULL;
63

  
64
    FILE *f = fopen(path, "r");
65
    if (f == NULL) return NULL;
66
    highscores_t *ret = (highscores_t*)malloc(sizeof(highscores_t));
67
    if (ret == NULL) return NULL;
68
    char line[30];
69
    for (int i = 0; i < MAX_HIGHSCORES; i++) {
70
        fgets(line, 30, f);
71
        score_info_t *score = (score_info_t*)malloc(sizeof(score_info_t));
72

  
73
        sscanf(line, "%d/%d/%d %d:%d:%d - %d - %d\n",
74
                    &(score->day), &(score->month), &(score->year),
75
                    &(score->hour), &(score->month), &(score->sec),
76
                    &(score->score), &(score->time_played));
77
        ret->highscores[i] = score;
78
    }
79
    fclose(f);
80

  
81
    return ret;
82
}
83

  
84
void (highscores_save)(const highscores_t *p, const char *path) {
85
    if(p == NULL || path == NULL) return;
86

  
87
    FILE *f = fopen(path, "w");
88
    if (f == NULL) return;
89
    for (int i = 0; i < MAX_HIGHSCORES; i++) {
90
        score_info_t *score = p->highscores[i];
91
        fprintf(f, "%d/%d/%d %d:%d:%d - %d - %d\n",
92
                    (score->day), (score->month), (score->year),
93
                    (score->hour), (score->month), (score->sec),
94
                    (score->score), (score->time_played));
95
    }
96
    fclose(f);
97
}
98

  
99
void (highscores_dtor)(highscores_t *p) {
100
    for (int i = 0; i < MAX_HIGHSCORES; i++) {
101
        score_dtor(p->highscores[i]);
102
    }
103
}
0 104

  
proj/Makefile
9 9
.PATH: ${.CURDIR}/libs/classes/src
10 10
.PATH: ${.CURDIR}/libs/utils/src
11 11

  
12
SRCS= list.c queue.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c fast_math.c xpm_utils.c rtc.c uart.c makecode_map.c menu.c proj_func.c rectangle.c font.c ent.c proj.c hltp.c zombies.c campaign.c singleplayer.c chat.c
12
SRCS= list.c queue.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c fast_math.c xpm_utils.c rtc.c uart.c makecode_map.c menu.c proj_func.c rectangle.c font.c ent.c proj.c hltp.c zombies.c campaign.c singleplayer.c chat.c scoreboards.c
13 13
IPATHS=-I./include -I./libs/graph/include -I./libs/kbc/include -I./libs/rtc/include -I./libs/timer/include -I./libs/uart/include -I./libs/classes/include -I./libs/utils/include -I./maps -I./media/xpm
14 14

  
15
CPPFLAGS += -pedantic ${IPATHS} -D LCOM_MACRO #-D __LCOM_OPTIMIZED__ -Wall -Wextra -Wshadow -Wunreachable-code #-Weverything -Wno-padded -Wno-unused-macros
15
CPPFLAGS += -pedantic ${IPATHS} -D LCOM_MACRO -D __LCOM_OPTIMIZED__ -Wall -Wextra -Wshadow -Wunreachable-code #-Weverything -Wno-padded -Wno-unused-macros
16 16

  
17 17
DPADD += ${LIBLCF}
18 18
LDADD += -llcf

Also available in: Unified diff