menu.c 2.75 KB
Newer Older
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
1
#include "menu.h"
Ivo Júnior's avatar
Ivo Júnior committed
2

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
3
int menuColors[3][3];
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
4
5
6
char input[WIDTH];
int offmenuy, offmenux;
int row, col;
Ivo Júnior's avatar
Ivo Júnior committed
7
char mps[7] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
8
int currmap = 0;
9
int buffer;
Ivo Júnior's avatar
Ivo Júnior committed
10
11
12
13

void readFileMenu(menu *menu) {
    int i, j;
    menu->sprite = fopen("./sprites/menu.txt", "r");
14
    buffer = -1;
Ivo Júnior's avatar
Ivo Júnior committed
15

Ivo Júnior's avatar
Ivo Júnior committed
16
	for (i = 0; i < 3; i++)
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
17
18
        for (j = 0; j < 3; j++)
          	fscanf(menu->sprite, "%d", &menuColors[i][j]);
Ivo Júnior's avatar
Ivo Júnior committed
19

Ivo Júnior's avatar
Ivo Júnior committed
20
21
    for (i = 0; i < HEIGHT; i++) {
        fscanf(menu->sprite, "%s", input);
22
        for (j = 0; j < WIDTH; j++)
Ivo Júnior's avatar
Ivo Júnior committed
23
24
            menu->grid[j][i] = input[j];
    }
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
25
    fclose(menu->sprite);
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
26
    setMenuColors();
Ivo Júnior's avatar
Ivo Júnior committed
27
28
}

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
29
30
31
32
void setMenuColors() {
    init_color(COLOR_WHITE,   menuColors[0][0], menuColors[0][1], menuColors[0][2]);
    init_color(COLOR_BLACK,   menuColors[1][0], menuColors[1][1], menuColors[1][2]);
    init_color(COLOR_BLUE,    menuColors[2][0], menuColors[2][1], menuColors[2][2]);
Ivo Júnior's avatar
Ivo Júnior committed
33
34
35
36
37
38
39
40
41

    // background
    init_pair(1, COLOR_BLACK, COLOR_BLACK);

    // objects
    init_pair(2, COLOR_WHITE, COLOR_BLACK);

    // background_smallbox
    init_pair(3, COLOR_BLUE, COLOR_BLUE);
42
43
44

    // all white
    init_pair(4, COLOR_WHITE, COLOR_WHITE);
Ivo Júnior's avatar
Ivo Júnior committed
45
46
}

47
int updateMenu(menu *menu, int action, int *loadMenu) {
48
	buffer = -1;
49
    if (*loadMenu) {
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
50
51
52
53
54
55
        menu->highscore = fopen("./data/highscore.txt", "r");
        fscanf(menu->highscore, "%d", &menu->high);
        menu->lastscore = fopen("./data/lastscore.txt", "r");
        fscanf(menu->lastscore, "%d", &menu->last);

       setMenuColors();
56
57
        *loadMenu = false;
    }
58
59
    switch(action){
		case 6:
60
			currmap = (currmap + 1) % 7;
61
62
			break;
		case 4:
Ivo Júnior's avatar
Ivo Júnior committed
63
			currmap = currmap == 0 ? 6 : currmap - 1;
64
			break;
65
66
67
68
69
        case 1:
            buffer = currmap;
            break;
        default:
            break;
70
	}
71
    return buffer;
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
72
73
74
}

void renderMenu(menu *menu){
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
75
    int i, j;
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
76
77
78
    getmaxyx(stdscr, row, col);
    offmenuy = (row / 2) - (12 * 3);
    offmenux = (col / 2) - (20 * 6);
79
	menu->grid[21][22] = mps[currmap];
80

81
82
83
84
85
86
	menu->grid[21][20] = ((menu->high / 100000) % 10) + '0';
    menu->grid[22][20] = ((menu->high / 10000) % 10) + '0';
    menu->grid[23][20] = ((menu->high / 1000) % 10) + '0';
    menu->grid[24][20] = ((menu->high / 100) % 10) + '0';
    menu->grid[25][20] = ((menu->high / 10) % 10) + '0';
    menu->grid[26][20] = (menu->high % 10) + '0';
87

88
89
90
91
92
93
	menu->grid[21][21] = ((menu->last / 100000) % 10) + '0';
    menu->grid[22][21] = ((menu->last / 10000) % 10) + '0';
    menu->grid[23][21] = ((menu->last / 1000) % 10) + '0';
    menu->grid[24][21] = ((menu->last / 100) % 10) + '0';
    menu->grid[25][21] = ((menu->last / 10) % 10) + '0';
    menu->grid[26][21] = (menu->last % 10) + '0';
94

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
95
96
    for (i = 0; i < WIDTH; i++)
        for (j = 0; j < HEIGHT; j++)
97
            renderLetter(menu->grid[i][j], i, j, offmenux, offmenuy);
Ivo Júnior's avatar
Ivo Júnior committed
98
}