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
|
/*
** init.c for in /home/gayot_o/prog/minesweeper
**
** Made by olivier gayot
** Login <gayot_o@epitech.net>
**
** Started on Sun Apr 22 16:09:49 2012 olivier gayot
** Last update Sun Apr 22 16:09:49 2012 olivier gayot
*/
#include <stdio.h>
#include <sdl_digit.h>
#include <strings.h>
#include "minesweeper.h"
static void rand_bombs(bool bombs[BLOCK_H][BLOCK_W]);
static SDL_Surface *xSDL_LoadBMP(const char *path);
void init_map(map_t *map) {
bool bombs[BLOCK_H][BLOCK_W];
rand_bombs(bombs); map->bombs = BOMBS;
for (int j = 0; j < BLOCK_H; ++j) {
for (int i = 0; i < BLOCK_W; ++i) {
if (!(map->tab[j][i].type = bombs[j][i]))
map->tab[j][i].touch = get_nbr_touch(bombs, j, i);
map->tab[j][i].x = i * BLOCK_SIZE;
map->tab[j][i].y = j * BLOCK_SIZE;
map->tab[j][i].hidden = true;
map->tab[j][i].selected = false;
map->tab[j][i].flagged = false;
}
}
map->tab[0][0].selected = true;
}
static void rand_bombs(bool bombs[BLOCK_H][BLOCK_W]) {
int i = 0;
memset((void *)bombs, 0, sizeof(bool) * BLOCK_H * BLOCK_W);
while (i < BOMBS) {
int id = rand() % (BLOCK_H * BLOCK_W);
if (bombs[id / BLOCK_W][id % BLOCK_W] == 0) {
bombs[id / BLOCK_W][id % BLOCK_W] = 1;
++i;
}
}
}
void load_surfaces(void) {
char str[256];
for (int i = 0; i < 9; ++i) {
snprintf(str, 256, "img/%d.bmp", i);
surf_number[i] = xSDL_LoadBMP(str);
}
bmb_surf = xSDL_LoadBMP("img/b.bmp");
hidden_surf = xSDL_LoadBMP("img/hidden.bmp");
flag_surf = xSDL_LoadBMP("img/flag.bmp");
wrong_flag = xSDL_LoadBMP("img/wrong_flag.bmp");
selection_surf = xSDL_LoadBMP("img/selection.bmp");
SDL_SetColorKey(selection_surf, SDL_SRCCOLORKEY, 0);
}
void dsp_ribbon_flags(void) {
SDL_Surface *surf;
char str[256];
SDL_Rect rect;
surf = SDL_CreateRGBSurface(SDL_HWSURFACE, 90, 40, 32, 0, 0, 0, 0);
rect.x = BLOCK_SIZE * BLOCK_W - 90;
rect.y = BLOCK_SIZE * BLOCK_H + 5;
SDL_FillRect(surf, NULL, 0);
xSDL_BlitSurface(surf, NULL, scr, &rect);
if ((int)(BOMBS - g_flags) <= -100)
sprintf(str, "-%u", ABS((int)(BOMBS - g_flags)));
else if ((int)(BOMBS - g_flags) <= -10)
sprintf(str, "-0%u", ABS((int)(BOMBS - g_flags)));
else if ((int)(BOMBS - g_flags) < 0)
sprintf(str, "-00%u", ABS((int)(BOMBS - g_flags)));
else if ((int)(BOMBS - g_flags) < 10)
sprintf(str, " 00%u", ABS((int)(BOMBS - g_flags)));
else if ((int)(BOMBS - g_flags) < 100)
sprintf(str, " 0%u", ABS((int)(BOMBS - g_flags)));
else
sprintf(str, " %d", BOMBS - g_flags);
draw_string(surf, NULL, str, 0xff, 15);
xSDL_BlitSurface(surf, NULL, scr, &rect);
SDL_FreeSurface(surf);
}
int callback(void *param __attribute__((unused))) {
Uint32 base_ticks = SDL_GetTicks();
for (;;) {
dsp_clock(base_ticks);
SDL_Delay(200);
}
return 0;
}
SDL_Thread *start_clock(void) {
return (SDL_CreateThread(&callback, NULL));
}
static SDL_Surface *xSDL_LoadBMP(const char *path) {
SDL_Surface *surf;
char buffer[256];
if (!(surf = SDL_LoadBMP(path))) {
snprintf(buffer, 256, "Unable to load %s: %s\n", path, SDL_GetError());
exit(-1);
}
return (surf);
}
|