-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmgui.c
128 lines (122 loc) · 4.1 KB
/
mgui.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <raylib.h>
#include <stdio.h>
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
#include "board.h"
void print_board(pos** board, size* board_type) {
for (int y = 0; y < (board_type->y); y++) {
for (int x = 0; x < (board_type->x); x++) {
char print = '.';
if (board[y][x].is_bomb) {
print = 'X';
}
printf("%c", print);
}
printf("\n");
}
}
void choose_board(pos*** board, size* size, int* height, int* width) {
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)));
int chosen_size = GuiMessageBox((Rectangle){ (*width-720)/2, (*height-90)/2, 720, 90},
"Choose size",
"How large do you want your board?",
"Beginner\nIntermediate\nExpert\nCustom (not implemented)");
if (chosen_size != -1 && chosen_size != 4) {
*size = sizes[chosen_size - 1];
*board = generate_board(*size);
EndDrawing();
return;
}
EndDrawing();
}
}
/// Get's a 16x16 sprite from the `tiles`.
/// Intended is the 8x2 sprite sheet for tiles,
/// 0 - Undug tile
/// 1 - Empty tile
/// 2 - Flagged tile
/// 3 - ? tile
/// 4 - Dug ? tile
/// 5 - Bomb
/// 6 - Detonated bomb
/// 7 - Fake bomb
/// 8 -> 15 - Numbers 1 to 8
Texture2D splice_tile(Texture2D* tiles, int index) {
Image img = LoadImageFromTexture(*tiles);
ImageCrop(
&img,
(Rectangle){
(float)((index*16)%128),
(float)(((index/8)*16)%32),
16.,
16.
}
);
Texture2D tex = LoadTextureFromImage(img);
UnloadImage(img);
return tex;
}
Texture2D* get_tile_splices(Texture2D* tiles) {
Texture2D* out = malloc(sizeof(Texture2D) * 16);
for (int i = 0; i < 16; i++) {
out[i] = splice_tile(tiles, i);
}
return out;
}
void draw_board(pos** board, size* board_type, float top_left_x, float top_left_y, float size_mul, Texture2D* tiles, int game_over) {
//Texture2D* ret_ptr = malloc(sizeof(Texture2D) * board_type->x * board_type->y);
for (int y = 0; y < (board_type->y); y++) {
for (int x = 0; x < (board_type->x); x++) {
Texture2D texture;// = {0, 0, 0, 0, 0};
//UnloadTexture(texture);
if (board[y][x].is_discovered) {
if (!board[y][x].is_bomb) {
int bomb_count = surround_bombs(board, board_type, x, y);
if (bomb_count != 0) {
texture = tiles[7 + bomb_count];
} else {
texture = tiles[1];
}
} else {
if (game_over) {
if (board[y][x].is_exploded) {
texture = tiles[6];
} else {
texture = tiles[5];
}
} else {
texture = tiles[5];
}
}
} else {
if (!game_over) {
if (!board[y][x].is_flagged) {
texture = tiles[0];
} else {
texture = tiles[2];
}
} else {
if (board[y][x].is_bomb && !board[y][x].is_exploded) {
texture = tiles[5];
} else if (board[y][x].is_bomb && board[y][x].is_exploded) {
texture = tiles[6];
} else {
texture = tiles[0];
}
}
}
DrawTextureEx(
texture,
(Vector2){top_left_x + x*16*size_mul, top_left_y + y*16*size_mul},
0.,
size_mul,
WHITE
);
//ret_ptr[x + board_type->x * y] = texture;
}
}
//return ret_ptr;
return;
}