/* ** init.c for in /home/gayot_o/prog/minesweeper ** ** Made by olivier gayot ** Login ** ** Started on Sun Apr 22 16:09:49 2012 olivier gayot ** Last update Sun Apr 22 16:09:49 2012 olivier gayot */ #include #include #include #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); }