diff --git a/CMakeLists.txt b/CMakeLists.txt index c16cb66..d367d85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,36 +1,37 @@ -cmake_minimum_required(VERSION 3.31) +cmake_minimum_required(VERSION 3.30.1) project( - saolei + MineSweeper VERSION 0.1.0 LANGUAGES C ) set(CMAKE_C_STANDARD 11) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static") + set(CMAKE_PREFIX_PATH ./lib/SDL/cmake) find_package(SDL2 REQUIRED) find_package(SDL2_image REQUIRED) -find_package(SDL2_mixer REQUIRED) find_package(SDL2_ttf REQUIRED) file(GLOB res res/**) -file(COPY ${res} DESTINATION ${CMAKE_BINARY_DIR}/res) +file(COPY ${res} DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/res) -add_executable(saolei +file(GLOB lib lib/SDL/x86_64-w64-mingw32/bin/**) +file(COPY ${lib} DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) + +add_executable(MineSweeper src/main.c src/game.c - include/game.h src/data.c - include/data.h src/img.c - include/img.h + src/over.c + src/resource/logo.rc ) -target_link_libraries(saolei +target_link_libraries(MineSweeper ${SDL2_LIBRARIES} - SDL2_mixer::SDL2_mixer SDL2_ttf::SDL2_ttf SDL2_image::SDL2_image ) - -target_link_options(saolei PRIVATE -Wl,-subsystem,console) \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..2afd23b --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# MineSweeper + +这一个初学者对c语言的初次探索,在mingw的环境下基于SDL2实现的一个有着可视化窗口的扫雷小游戏。 + +## 使用Cmkae构建 + +```shell +cmake -B build -G "MingGW Makefiles" +``` + +```shell +cmake --build build --config Release -j18 +``` \ No newline at end of file diff --git a/include/Global.h b/include/Global.h deleted file mode 100644 index fd81cbe..0000000 --- a/include/Global.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef GLOBAL_H -#define GLOBAL_H - -#include "def.h" -#include "context.h" -#include "window.h" - -typedef struct Global { - Context context; - Window window; -} Global; - -extern Global global; -#endif //GLOBAL_H diff --git a/include/context.h b/include/context.h deleted file mode 100644 index b9691d3..0000000 --- a/include/context.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef INIT_H -#define INIT_H -#include "def.h" -#include - -typedef struct Context { - int map[ROW][COL]; - int mapcopy[ROW][COL]; - SDL_Texture * imgs[10]; -} Context; - -void contextInit(); -void imgInit(); - -#endif //INIT_H - - diff --git a/include/data.h b/include/data.h index 3126971..c348eac 100644 --- a/include/data.h +++ b/include/data.h @@ -9,7 +9,5 @@ typedef struct Data { void initData(Data *data, int x, int y ,int boom); void getData(Data *data, int x, int y); - -void showData(Data *data); - +int checkData(Data *data); #endif //DATA_H diff --git a/include/def.h b/include/def.h deleted file mode 100644 index 3511e4e..0000000 --- a/include/def.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef DEF_H -#define DEF_Hs - -// 行列数 -#define ROW 10 -#define COL 10 - -// 地雷个数 -#define BOOM 15 - -// 窗口大小 -#define WinWidth 400 -#define WinHeight 400 - -// 矩形大小 -#define rectWidth 40 -#define rectHeight 40 - -// 颜色 -#define OVER (Color){0X4E,0X4E,0X4E,255} -#define BACKGROUND (Color){0XE1,0XE2,0XE3,0XFF} -#define FLOW (Color){175,175,175,0} - -#endif //DEF_H diff --git a/include/game.h b/include/game.h index 3139166..aec065e 100644 --- a/include/game.h +++ b/include/game.h @@ -18,12 +18,14 @@ typedef struct Color { int r,g,b,a; } Color; -#define RED (Color){150,40,40,255} -#define GREEN (Color){40,150,40,255} -#define BLUE (Color){40,40,150,255} +#define SQUARE (Color){0xC0,0XC0,0XC0,255} +#define BACKGROUND (Color){0x24,0x49,0X5E,255} +#define BUTTON (Color){0X2c,0X3E,0X50,255} +#define FLOW (Color){0xD9,0xD9,0xD9} void initGame(const char* name, int width, int height); void gameLoop(); +void setColor(Color color); void windowClose(); #endif //RUNNING_H diff --git a/include/over.h b/include/over.h new file mode 100644 index 0000000..ae2f6b6 --- /dev/null +++ b/include/over.h @@ -0,0 +1,22 @@ +#ifndef WIN_H +#define WIN_H +#include "game.h" +#include + +typedef struct Font { + TTF_Font * fontKind; + SDL_Texture* texture; + int w, h; +} Font; + +typedef struct Button { + int x,y,w,h; +} Button; +typedef struct Over { + int nameH,buttonH,w; + Button botton; +} Over; + +void initOver(Game * game, Over * over); +void drawOver(int fontSizeName, int fontSizeButton,Font * font, const char * nameText, Color buttonColor, SDL_Renderer * renderer , Over* over); +#endif //WIN_H diff --git a/include/window.h b/include/window.h deleted file mode 100644 index c907ea3..0000000 --- a/include/window.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef WINDOW_H -#define WINDOW_H -#include -#include - -// 颜色 -typedef struct Color { - int r; - int g; - int b; - int a; -} Color; - -// 窗口 -typedef struct Window{ - SDL_Window *Window; - SDL_Renderer *Renderer; - SDL_Event Event; -} Window; - -void windowInit(); -void windowLoop(); -void windowShutdown(); - -#endif //WINDOW_H diff --git a/lib/SDL/x86_64-w64-mingw32/bin/SDL2_mixer.dll b/lib/SDL/x86_64-w64-mingw32/bin/SDL2_mixer.dll deleted file mode 100644 index 73376f0..0000000 Binary files a/lib/SDL/x86_64-w64-mingw32/bin/SDL2_mixer.dll and /dev/null differ diff --git a/lib/SDL/x86_64-w64-mingw32/bin/sdl2-config b/lib/SDL/x86_64-w64-mingw32/bin/sdl2-config deleted file mode 100644 index ad54d47..0000000 --- a/lib/SDL/x86_64-w64-mingw32/bin/sdl2-config +++ /dev/null @@ -1,65 +0,0 @@ -#!/bin/sh - -# Get the canonical path of the folder containing this script -bindir=`cd -P -- "\`dirname -- "$0"\`" && printf '%s\n' "\`pwd -P\`"` - -# Calculate the canonical path of the prefix, relative to the folder of this script -prefix=`cd -P -- "$bindir/.." && printf '%s\n' "\`pwd -P\`"` -exec_prefix=${prefix}/bin -exec_prefix_set=no -libdir=${prefix}/lib - -#usage="\ -#Usage: $0 [--prefix[=DIR]] [--exec-prefix[=DIR]] [--version] [--cflags] [--libs]" -usage="\ -Usage: $0 [--prefix[=DIR]] [--exec-prefix[=DIR]] [--version] [--cflags] [--libs] [--static-libs]" - -if test $# -eq 0; then - echo "${usage}" 1>&2 - exit 1 -fi - -while test $# -gt 0; do - case "$1" in - -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;; - *) optarg= ;; - esac - - case $1 in - --prefix=*) - prefix=$optarg - if test $exec_prefix_set = no ; then - exec_prefix=$optarg - fi - ;; - --prefix) - echo $prefix - ;; - --exec-prefix=*) - exec_prefix=$optarg - exec_prefix_set=yes - ;; - --exec-prefix) - echo $exec_prefix - ;; - --version) - echo 2.32.8 - ;; - --cflags) - echo -I${prefix}/include/SDL2 -Dmain=SDL_main - ;; - --libs) - echo -L${prefix}/lib -lmingw32 -lSDL2main -lSDL2 -mwindows - ;; - --static-libs) -# --libs|--static-libs) - sdl_static_libs=$(echo "-lmingw32 -lSDL2main -lSDL2 -mwindows -Wl,--dynamicbase -Wl,--nxcompat -Wl,--high-entropy-va -lm -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lsetupapi -lversion -luuid" | sed -E "s#-lSDL2[ $]#$libdir/libSDL2.a #g") - echo -L${prefix}/lib $sdl_static_libs - ;; - *) - echo "${usage}" 1>&2 - exit 1 - ;; - esac - shift -done diff --git a/res/1.png b/res/1.png deleted file mode 100644 index 212c76a..0000000 Binary files a/res/1.png and /dev/null differ diff --git a/res/2.png b/res/2.png deleted file mode 100644 index 4e80caa..0000000 Binary files a/res/2.png and /dev/null differ diff --git a/res/3.png b/res/3.png deleted file mode 100644 index 5165367..0000000 Binary files a/res/3.png and /dev/null differ diff --git a/res/4.png b/res/4.png index 6f5f56f..44604ee 100644 Binary files a/res/4.png and b/res/4.png differ diff --git a/res/5.png b/res/5.png index 81d23c0..173a99c 100644 Binary files a/res/5.png and b/res/5.png differ diff --git a/res/6.png b/res/6.png index 3210141..080810d 100644 Binary files a/res/6.png and b/res/6.png differ diff --git a/res/7.png b/res/7.png index 3868222..ab8c368 100644 Binary files a/res/7.png and b/res/7.png differ diff --git a/res/8.png b/res/8.png index 80ebc3e..6b8abcd 100644 Binary files a/res/8.png and b/res/8.png differ diff --git a/res/Minecraft.ttf b/res/Minecraft.ttf new file mode 100644 index 0000000..330c074 Binary files /dev/null and b/res/Minecraft.ttf differ diff --git a/res/boom.png b/res/boom.png index 2286e60..c46aa79 100644 Binary files a/res/boom.png and b/res/boom.png differ diff --git a/res/flag.png b/res/flag.png index efae38e..e3fa2c4 100644 Binary files a/res/flag.png and b/res/flag.png differ diff --git a/src/Global.c b/src/Global.c deleted file mode 100644 index d8da325..0000000 --- a/src/Global.c +++ /dev/null @@ -1,3 +0,0 @@ -#include "../include/Global.h" - -Global global = { 0 } ; \ No newline at end of file diff --git a/src/context.c b/src/context.c deleted file mode 100644 index 34cdb56..0000000 --- a/src/context.c +++ /dev/null @@ -1,57 +0,0 @@ -#include -#include -#include -#include - -#include "../include/Global.h" - -char * img_paths[10] = { - "./res/1.png", - "./res/2.png", - "./res/3.png", - "./res/4.png", - "./res/5.png", - "./res/6.png", - "./res/7.png", - "./res/8.png", - "./res/boom.png", - "./res/flag.png" -}; - -// 初始化数据 -void contextInit() { - // 设置随机位置为-1 - srand((unsigned)time(NULL)); - for (int i = 0; i < BOOM;) { - char k, l; - k = rand() % ROW; - l = rand() % COL; - if (!global.context.map[k][l]) { - global.context.map[k][l] = -1; - i++; - } - } - - // 把以 -1 为中心的九宫格数据都+1,-1除外 - for (int i = 0; i < ROW; i++) { - for (int j = 0; j < COL; j++) { - if (global.context.map[i][j] == -1) { - for (int k = i-1; k <= i+1; k++) { - for (int l = j-1; l <= j+1; l++) { - if ((k >= 0 && k < ROW && l >= 0 && l < COL ) && global.context.map[k][l] != -1) { - global.context.map[k][l]++; - } - } - } - } - } - } -} - -void imgInit () { - for (int i = 0; i < sizeof(global.context.imgs)/sizeof(global.context.imgs[0]); i++) { - SDL_Surface * surface = IMG_Load(img_paths[i]); - global.context.imgs[i] = SDL_CreateTextureFromSurface(global.window.Renderer,surface); - SDL_FreeSurface(surface); - } -} diff --git a/src/data.c b/src/data.c index 71034ee..d0d736c 100644 --- a/src/data.c +++ b/src/data.c @@ -3,22 +3,28 @@ #include #include #include -#include #include +#include "../include/over.h" +static int isArrInint = 1; void initData(Data *data ,int x, int y, int boom) { + data->x = x; data->y = y; - data->arr = (int **)malloc( x * sizeof(int*)); - for (int i = 0; i < x; i++) { - data->arr[i] = (int *)malloc( y * sizeof(int)); - memset(data->arr[i], 0, y * sizeof(int)); + if (isArrInint) { + data->arrcopy = (int **)malloc( x * sizeof(int*)); + data->arr = (int **)malloc( x * sizeof(int*)); + for (int i = 0; i < x; i++) { + data->arr[i] = (int *)malloc( y * sizeof(int)); + data->arrcopy[i] = (int *)malloc( y * sizeof(int)); + } + isArrInint = 0; } - data->arrcopy = (int **)malloc( x * sizeof(int*)); for (int i = 0; i < x; i++) { - data->arrcopy[i] = (int *)malloc( y * sizeof(int)); + memset(data->arr[i], 0, y * sizeof(int)); memset(data->arrcopy[i], 0, y * sizeof(int)); } + data->boom = boom; } @@ -28,7 +34,7 @@ void getData(Data *data, int x, int y) { char i,j; i = rand() % data->x; j = rand() % data->y; - if (i != x && j != y && data->arr[i][j] != -1) { + if ((i != x || j != y) && data->arr[i][j] != -1) { data->arr[i][j] = -1; n++; } @@ -49,11 +55,18 @@ void getData(Data *data, int x, int y) { } } -void showData(Data *data){ +int checkData(Data *data) { + int num = 1; for (int i = 0; i < data->x; i++) { - for (int j = 0; j < data->y; j++ ) { - printf("%3d", data->arrcopy[i][j]); + for (int j = 0; j < data->y; j++) { + if ( data->arr[i][j] == -1 && data->arrcopy[i][j] == -1 ) { + return -1; + } + if (data->arr[i][j] != -1 && data->arrcopy[i][j] >= 0 && num) { + num =0; + } } - printf("\n"); } + + return num; } diff --git a/src/game.c b/src/game.c index 3fbb345..be597f3 100644 --- a/src/game.c +++ b/src/game.c @@ -1,17 +1,25 @@ #include "../include/game.h" #include "../include/data.h" #include "../include/img.h" +#include "../include/over.h" #include +#include #include -#include Game game; Data data; Img img; +Over over; +Font font; + static int running = 1; static int isfirst = 1; -const char * imgArr[] = { +static int flag = 0; +static int isInit = 1; +static int isOverInit = 0; + +const char *imgArr[] = { "./res/boom.png", "./res/1.png", "./res/2.png", @@ -22,72 +30,76 @@ const char * imgArr[] = { "./res/7.png", "./res/8.png", "./res/flag.png" - }; +const char *winTitle = "WC NB"; +const char *failureTitle = "G G"; -void initGame(const char* name,int width,int height) { +TTF_Font *fontKind = NULL; + +void initGame(const char *name, int width, int height) { SDL_Init(SDL_INIT_EVERYTHING); - IMG_Init(IMG_INIT_PNG); + IMG_Init(IMG_INIT_PNG ); + TTF_Init(); + game.title = name; game.window.width = width; game.window.height = height; - game.window.window = SDL_CreateWindow(name,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_SHOWN); - game.window.renderer = SDL_CreateRenderer(game.window.window,-1,SDL_RENDERER_ACCELERATED); - initImg(&img,imgArr,10,game.window.renderer); + game.window.window = SDL_CreateWindow(name,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, width, height, + SDL_WINDOW_SHOWN); + game.window.renderer = SDL_CreateRenderer(game.window.window, -1, SDL_RENDERER_ACCELERATED); + initImg(&img, imgArr, 10, game.window.renderer); } void windowClose() { SDL_DestroyRenderer(game.window.renderer); SDL_DestroyWindow(game.window.window); + IMG_Quit(); SDL_Quit(); } -void normalization (int* x, int* y) { - *x = *x / (game.window.width / data.x); - *y = *y / (game.window.height / data.y); +void normalization(int *x, int *y) { + *x = *x / (game.window.width / data.y); + *y = *y / (game.window.height / data.x); *x = *x ^ *y; *y = *y ^ *x; *x = *x ^ *y; } -void extend(int i,int j) { - if (i>=0 && i< data.x && j>=0 && j< data.y && data.arr[i][j] == 0 && data.arrcopy[i][j] != -1) { +void extend(int i, int j) { + if (i >= 0 && i < data.x && j >= 0 && j < data.y && data.arr[i][j] == 0 && data.arrcopy[i][j] != -1) { data.arrcopy[i][j] = -1; - extend(i - 1 ,j); - extend(i ,j - 1); - extend(i ,j + 1); + extend(i - 1, j); + extend(i, j - 1); + extend(i, j + 1); extend(i + 1, j); - } else if ( i>=0 && i< data.x && j>=0 && j< data.y && data.arr[i][j] > 0 ) { + } else if (i >= 0 && i < data.x && j >= 0 && j < data.y && data.arr[i][j] > 0) { data.arrcopy[i][j] = -1; } } -void eventProcess(SDL_Event event) { +void runningEventProcess(SDL_Event event) { while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: running = 0; break; case SDL_MOUSEBUTTONUP: - int x,y; - SDL_GetMouseState(&x,&y); - - normalization(&x,&y); - + int x, y; + SDL_GetMouseState(&x, &y); + normalization(&x, &y); if (event.button.button == SDL_BUTTON_LEFT) { - if (isfirst == 1 && data.arrcopy[x][y] == 0 ) { + if (isfirst == 1 && data.arrcopy[x][y] == 0) { getData(&data, x, y); isfirst = 0; } if (data.arr[x][y] == 0 && data.arrcopy[x][y] == 0) { - extend(x ,y ); + extend(x, y); } if (data.arrcopy[x][y] == 0) { data.arrcopy[x][y] = -1; } } - if (event.button.button == SDL_BUTTON_RIGHT) { if (data.arrcopy[x][y] == 0) { data.arrcopy[x][y] = 1; @@ -101,71 +113,138 @@ void eventProcess(SDL_Event event) { } void setColor(Color color) { - SDL_SetRenderDrawColor(game.window.renderer,color.r,color.g,color.b,color.a); + SDL_SetRenderDrawColor(game.window.renderer, color.r, color.g, color.b, color.a); } -SDL_Rect creatRect(int x,int y,Game* game,Data* data) { - +SDL_Rect creatRect(int x, int y, Game *game, Data *data) { SDL_Rect rect = { - x * game->window.width / data->x + 4, - y * game->window.width / data->y+ 4 , - game->window.width / data->x - 8, - game->window.height / data->y - 8 + x * game->window.width / data->y + 4, + y * game->window.height / data->x + 4, + game->window.width / data->y - 8, + game->window.height / data->x - 8 }; return rect; } -void drawRect(int x,int y, int kind,int isCilck) { +void drawRect(int x, int y, int kind, int isCilck) { if (isCilck == 0) { - setColor(RED); - SDL_Rect rect = creatRect(y,x,&game,&data); - SDL_RenderFillRect(game.window.renderer,&rect); + setColor(SQUARE); + SDL_Rect rect = creatRect(x, y, &game, &data); + SDL_RenderFillRect(game.window.renderer, &rect); } else if (isCilck == 1) { - SDL_Rect rect = creatRect(y,x,&game,&data); - SDL_RenderCopy(game.window.renderer,img.texture[9],NULL,&rect); + SDL_Rect rect = creatRect(x, y, &game, &data); + SDL_RenderCopy(game.window.renderer, img.texture[9],NULL, &rect); } else if (isCilck == -1) { - SDL_Rect rect = creatRect(y,x,&game,&data); + SDL_Rect rect = creatRect(x, y, &game, &data); if (kind > 0) { - SDL_RenderCopy(game.window.renderer,img.texture[kind],NULL,&rect); + SDL_RenderCopy(game.window.renderer, img.texture[kind],NULL, &rect); } else if (kind == -1) { - SDL_RenderCopy(game.window.renderer,img.texture[0],NULL,&rect); + SDL_RenderCopy(game.window.renderer, img.texture[0],NULL, &rect); } } } void drawMap(Data data) { - setColor(BLUE); + setColor(BACKGROUND); SDL_RenderClear(game.window.renderer); - for(int i = 0; i < data.x; i++) { - for(int j = 0; j < data.y; j++) { - drawRect(i, j,data.arr[i][j],data.arrcopy[i][j]); + for (int i = 0; i < data.x; i++) { + for (int j = 0; j < data.y; j++) { + drawRect(j, i, data.arr[i][j], data.arrcopy[i][j]); } } } void moveMouse() { - int x,y; - SDL_GetMouseState(&x,&y); - normalization(&x,&y); - if (data.arrcopy[x][y] == 0 && x > 0 && y > 0 && x < data.x && y < data.y) { - setColor(GREEN); - SDL_Rect rect = creatRect(y,x,&game,&data); - SDL_RenderDrawRect(game.window.renderer,&rect); + int x, y; + SDL_GetMouseState(&x, &y); + normalization(&x, &y); + if (data.arrcopy[x][y] == 0 && x >= 0 && y >= 0 && x < data.x && y < data.y) { + setColor(FLOW); + SDL_Rect rect = creatRect(y, x, &game, &data); + SDL_RenderFillRect(game.window.renderer, &rect); + } +} + +void showAll(Data *data) { + for (int i = 0; i < data->x; i++) { + memset(data->arrcopy[i], -1, data->y * sizeof(int)); + } +} + +void overEventProcess(SDL_Event event) { + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + running = 0; + break; + case SDL_MOUSEBUTTONUP: + int x, y; + SDL_GetMouseState(&x, &y); + if (event.button.button == SDL_BUTTON_LEFT && x > over.botton.x && y > over.botton.y && x < ( + over.botton.x + over.botton.w) && y < (over.botton.y + over.botton.h)) { + setColor(BACKGROUND); + SDL_RenderClear(game.window.renderer); + flag = 0; + isInit = 1; + isfirst = 1; + } + } } } void gameLoop() { - initData(&data,10,10,15); - + int time = 0; SDL_Event event; while (running) { - drawMap(data); - - eventProcess(event); - - moveMouse(); - + switch (flag) { + case 0: + if (isInit) { + initData(&data, 20, 20, 88); + isInit = 0; + } + runningEventProcess(event); + drawMap(data); + moveMouse(); + flag = checkData(&data); + if (flag != 0) { + isOverInit = 1; + time = 0; + } + break; + case 1: + if (time < 1000) { + time += 6; + if (isOverInit) { + initOver(&game, &over); + isOverInit = 0; + } + } else { + setColor(BACKGROUND); + SDL_RenderClear(game.window.renderer); + drawOver(102,22 ,&font, winTitle, SQUARE, game.window.renderer, &over); + overEventProcess(event); + } + break; + case -1: + if (time < 1000) { + time += 6; + showAll(&data); + drawMap(data); + if (isOverInit) { + initOver(&game, &over); + isOverInit = 0; + } + } else { + setColor(BACKGROUND); + SDL_RenderClear(game.window.renderer); + drawOver(102, 22, &font,failureTitle, SQUARE, game.window.renderer, &over); + overEventProcess(event); + } + break; + default: + return; + } SDL_RenderPresent(game.window.renderer); SDL_Delay(6); } -} \ No newline at end of file +} diff --git a/src/main.c b/src/main.c index 6e18fc7..515a87a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,11 +1,8 @@ #include "../include/game.h" #include -#include - -#include "../include/data.h" int main(int, char**) { - initGame("扫雷", 400, 400); + initGame("MineSweeper", 800, 800); gameLoop(); diff --git a/src/over.c b/src/over.c new file mode 100644 index 0000000..0839245 --- /dev/null +++ b/src/over.c @@ -0,0 +1,56 @@ +#include "../include/over.h" +#include "../include/game.h" +#include +#include + +const char *ttf_path = "../res/Minecraft.ttf"; +const char *buttonText = "PLAY GAME AGAIN"; +static SDL_Color fontColor = {0xE7, 0X4C, 0X3C, 255}; + +static SDL_Color buttonFontColor = { 0x3C, 0X4E, 0X60 }; + +void initOver(Game * game, Over * over) { + over->nameH = game->window.height * 7 / 10; + over->buttonH = game->window.height - over->nameH; + over->w = game->window.width; + + over->botton.h = over->buttonH * 1 / 4; + over->botton.w = over->w * 3 / 5; + over->botton.x= ( over->w - over->botton.w ) / 2; + over->botton.y = ( over->buttonH - over->botton.h ) / 2 + over->nameH; +} + +void initFont(int fontSize,Font * font, const char * text, SDL_Color color, SDL_Renderer * renderer) { + font->fontKind = TTF_OpenFont(ttf_path, fontSize); + SDL_Surface* Surface = TTF_RenderText_Blended(font->fontKind, text, color); + font->texture = SDL_CreateTextureFromSurface(renderer, Surface); + SDL_FreeSurface(Surface); + + int width, height; + SDL_QueryTexture(font->texture, NULL, NULL, &width, &height); + font->w = width; + font->h = height; +} + +void drawFont(Font * font, int x, int y, int width, int height, SDL_Renderer * renderer) { + SDL_Rect rect = { (width - font->w) / 2 + x, (height - font->h) / 2 + y, font->w, font->h }; + SDL_RenderCopy(renderer, font->texture, NULL, &rect); + SDL_DestroyTexture(font->texture); +} + +void drawButton(Font * font ,Over * over, SDL_Renderer * renderer ,Color color) { + setColor(color); + SDL_Rect rect = { over->botton.x, over->botton.y, over->botton.w, over->botton.h }; + SDL_RenderFillRect(renderer, &rect); + drawFont(font, rect.x, rect.y, rect.w, rect.h, renderer); +} + +void drawOver(int fontSizeName, int fontSizeButton, Font * font,const char * nameText, Color buttonColor, SDL_Renderer * renderer , Over* over) { + + initFont(fontSizeName, font, nameText, fontColor, renderer); + drawFont(font, 0,0,over->w,over->nameH, renderer); + + drawButton(font,over,renderer, buttonColor); + initFont(fontSizeButton, font, buttonText , buttonFontColor, renderer); + drawFont(font,over->botton.x,over->botton.y, over->botton.w, over->botton.h, renderer); +} diff --git a/src/resource/logo.rc b/src/resource/logo.rc new file mode 100644 index 0000000..1dc269e --- /dev/null +++ b/src/resource/logo.rc @@ -0,0 +1 @@ +id ICON "logo.ico" \ No newline at end of file diff --git a/src/window.c b/src/window.c deleted file mode 100644 index c4349f0..0000000 --- a/src/window.c +++ /dev/null @@ -1,167 +0,0 @@ -#include -#include - -#include "../include/Global.h" - -static int flag = 0; -// ʼԴڼȾ -void windowInit(int width,int height) { - SDL_Init(SDL_INIT_EVERYTHING); - IMG_Init(IMG_INIT_PNG); - global.window.Window = SDL_CreateWindow("ɨ",SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN); - global.window.Renderer = SDL_CreateRenderer(global.window.Window, -1, SDL_RENDERER_ACCELERATED); -} - -// ղرԴڼȾ -void windowShutdown() { - SDL_DestroyRenderer(global.window.Renderer); - SDL_DestroyWindow(global.window.Window); - IMG_Quit(); - SDL_Quit(); -} - -// ɫ -void setColor(Color color) { - SDL_SetRenderDrawColor(global.window.Renderer, color.r, color.g, color.b, color.a); -} - -// һָɫĿľ -void drawRect(SDL_Rect* rect,Color color) { - setColor(color); - SDL_RenderFillRect(global.window.Renderer, rect); -} - -// ʾͼƬ -void drawImg( int i,int j,SDL_Rect * rect) { - if (global.context.map[i][j] == -1 ) { - SDL_RenderCopy(global.window.Renderer,global.context.imgs[8],NULL,rect); - } else if (global.context.map[i][j] > 0) { - SDL_RenderCopy(global.window.Renderer,global.context.imgs[global.context.map[i][j] - 1 ],NULL,rect); - } else if (global.context.map[i][j] == 0) { - setColor(BACKGROUND); - SDL_RenderFillRect(global.window.Renderer,rect); - } -} - -// -void drawMap() { - // - setColor(BACKGROUND); - SDL_RenderClear(global.window.Renderer); - - for (int i = 0; i < ROW; i++) { - for (int j = 0; j < COL; j++) { - SDL_Rect rect = {j * rectWidth + 1,i * rectHeight + 1,rectWidth - 2,rectHeight - 2}; - if (global.context.mapcopy[i][j] == 0) { - drawRect(&rect,OVER); - } else if (global.context.mapcopy[i][j] == -1) { - drawImg(i,j,&rect); - } else if (global.context.mapcopy[i][j] == 1) { - SDL_RenderCopy(global.window.Renderer,global.context.imgs[ 9 ],NULL,&rect); - } - } - } -} - -// -void mouseMove() { - int x,y; - SDL_GetMouseState(&x,&y); - SDL_Rect rect = {x / 40 * 40 + 1,y / 40 * 40 + 1,40 - 2,40 - 2}; - if (global.context.mapcopy[y/40][x/40] == 0) { - drawRect(&rect,FLOW); - } -} - -// ˮ㷨 -void extend(int i, int j) { - if (i>=0 && j>=0 && i=0 && j>=0 && i 0) { - global.context.mapcopy[i][j] = -1; - } -} - -// ¼ -void eventsProcess() { - while (SDL_PollEvent(&global.window.Event)) { - switch (global.window.Event.type) { - case SDL_QUIT: - windowShutdown(); - case SDL_MOUSEBUTTONUP: - int x,y; - SDL_GetMouseState(&x,&y); - if (global.window.Event.button.button == SDL_BUTTON_LEFT) { - if ( global.context.map[y/40][x/40] == 0 && global.context.mapcopy[y/40][x/40] == 0) { - extend(y/40,x/40); - } - if ( global.context.mapcopy[y/40][x/40] == 0) { - global.context.mapcopy[y/40][x/40] = -1; - } - } else if (global.window.Event.button.button == SDL_BUTTON_RIGHT) { - if (global.context.mapcopy[y/40][x/40] == 1) { - global.context.mapcopy[y/40][x/40] = 0; - } else if (global.context.mapcopy[y/40][x/40] == 0) { - global.context.mapcopy[y/40][x/40] = 1; - } - } - } - } -} - -// ݼ -void check() { - int num = BOOM; - for (int i = 0; i < ROW; i++) { - for (int j = 0; j < COL; j++) { - if (global.context.mapcopy[i][j] == -1 && global.context.map[i][j] == -1) { - flag = -1; - return; - } else if (global.context.mapcopy[i][j] == 1 && global.context.map[i][j] == -1) { - num -= 1; - } - } - } - if (num == 0) { - flag = 1; - } -} - -// ʧ -void defend() { - printf(""); - - while (1); -} - -// ʤ -void win() { - - printf("Ӯ"); - while (1); -} - -void windowLoop() { - flag = 0; - - while ( 1 ) { - if (flag == -1) { - defend(); - break; - } else if (flag == 1) { - win(); - break; - } else if (flag == 0) { - drawMap(); - mouseMove(); - SDL_RenderPresent(global.window.Renderer); - eventsProcess(); - check(); - } - SDL_Delay(6); - } -}