diff --git a/LiberationSerif-Regular.ttf b/LiberationSerif-Regular.ttf new file mode 100644 index 0000000000000000000000000000000000000000..106e0c812bd23f8c009e95048117aa782802571c Binary files /dev/null and b/LiberationSerif-Regular.ttf differ diff --git a/bg.jpg b/bg.jpg new file mode 100644 index 0000000000000000000000000000000000000000..9d40322443b462c885324fe5a216ba103ad2f916 Binary files /dev/null and b/bg.jpg differ diff --git a/button.png b/button.png new file mode 100644 index 0000000000000000000000000000000000000000..68be830f4f29d2b79af120aefca9881ad80e911e Binary files /dev/null and b/button.png differ diff --git a/colors.c b/colors.c new file mode 100644 index 0000000000000000000000000000000000000000..21a7ef8c19e30367221d10e6e95efcf485aba045 --- /dev/null +++ b/colors.c @@ -0,0 +1,11 @@ +#include "colors.h" + +const SDL_Color white= {255,255,255,255}; +const SDL_Color blue= {0,0,255,255}; /*1*/ +const SDL_Color green= {0,255,0,255}; /*2*/ +const SDL_Color red= {255,0,0,255}; /*3*/ +const SDL_Color purple= {0,0,0,0}; /*4*/ +const SDL_Color maroon= {0,0,0,0}; /*5*/ +const SDL_Color turquoise= {0,0,0,0}; /*6*/ +const SDL_Color black= {0,0,0,255}; /*7*/ +const SDL_Color grey= {0,0,0,0}; /*8*/ diff --git a/enums.h b/enums.h new file mode 100644 index 0000000000000000000000000000000000000000..2945878578b9ef465288148106f2c07aa24633ef --- /dev/null +++ b/enums.h @@ -0,0 +1,37 @@ +#ifndef ENUMS_H_INCLUDED +#define ENUMS_H_INCLUDED + +enum +{ + CONTINUE, + NEW_GAME, + HIGH_SCORE, + QUIT_TO_OS +}; + +enum +{ + BACK, + NEXT +}; + +enum +{ + GAME, + PAUSE, + EXIT +}; + +enum +{ + COVERED, + UNCOVERED, + FLAG +}; + +enum +{ + EMPTY, + MINE=9 +}; +#endif diff --git a/field.c b/field.c new file mode 100644 index 0000000000000000000000000000000000000000..8b44cc0bce8a2884f1408ce92ad3bfa00751624f --- /dev/null +++ b/field.c @@ -0,0 +1,29 @@ +#include "field.h" +#include "button.h" +#include "colors.h" +#include "enums.h" + +void drawField(SDL_Surface* screen,SDL_Event* evt,Field* field){ + drawButton(screen,evt,&field->btnField); +} + +void setField(Field* field,int type) +{ + setState(field,COVERED); + field->type=type; +} +void setState(Field* field,int value) +{ + field->state=value; +} +void setFlag(Field* field) +{ + if(field->state==FLAG) + { + setState(field,COVERED); + } + else if(field->state!=UNCOVERED) + { + setState(field,FLAG); + } +} diff --git a/field.h b/field.h new file mode 100644 index 0000000000000000000000000000000000000000..1bdc1f25d2fe60b62255eb10ab6e3d432147ba1a --- /dev/null +++ b/field.h @@ -0,0 +1,18 @@ +#ifndef FIELD_H_INCLUDED +#define FIELD_H_INCLUDED + +#include "button.h" + +typedef struct +{ + Button btnField; + int state; + int type; + +} Field; + +void drawField(SDL_Surface*,SDL_Event*,Field*); +void setField(Field*,int); +void setState(Field*,int); + +#endif diff --git a/field.jpg b/field.jpg new file mode 100644 index 0000000000000000000000000000000000000000..4789ad7c2d68309232df55d709948443fd605701 Binary files /dev/null and b/field.jpg differ diff --git a/field20.png b/field20.png new file mode 100644 index 0000000000000000000000000000000000000000..bbd1bd2eab1566cb8936c696698822dff19b76c1 Binary files /dev/null and b/field20.png differ diff --git a/game.c b/game.c new file mode 100644 index 0000000000000000000000000000000000000000..64a93198038ee77bcbf2dc45837190da82022838 --- /dev/null +++ b/game.c @@ -0,0 +1,109 @@ +#include "game.h" +#include "button.h" +#include "input.h" +#include "enums.h" + +int drawGameSetting(Game* game,SDL_Surface* screen,TTF_Font* fontMain,TTF_Font* fontField,SDL_Surface* bgMain,SDL_Surface* bgButton,SDL_Surface* bgField) +{ + SDL_Rect rectBack= {200,380,200,50},rectNext= {420,380,200,50}, /*gombok*/ + rectWidth= {200,220,200,50},rectHeight= {420,220,200,50},rectMines= {200,290,200,50}; /*input mezok*/ + SDL_Event evt= {0}; + Button btnBack,btnNext; + Input inputWidth,inputHeight,inputMines; + + setButton(&btnBack,rectBack,bgButton,fontMain,"Back"); + setButton(&btnNext,rectNext,bgButton,fontMain,"Next"); + + setInput(&inputWidth,bgButton,rectWidth); + setInput(&inputHeight,bgButton,rectHeight); + setInput(&inputMines,bgButton,rectMines); + + while(/*TODO:Ez is egy ronda ciklus*/1) + { + + SDL_WaitEvent(&evt); + SDL_BlitSurface(bgMain,NULL,screen,NULL); + /*gombok*/ + drawButton(screen,&evt,&btnBack); + drawButton(screen,&evt,&btnNext); + /*gomb események*/ + setButtonEvents(&btnBack,&evt); + setButtonEvents(&btnNext,&evt); + /*beviteli mezők rajzolása*/ + drawInput(screen,&inputWidth); + drawInput(screen,&inputHeight); + drawInput(screen,&inputMines); + /*Beviteli események*/ + setInputEvents(&inputWidth,&evt,fontMain,0); + setInputEvents(&inputHeight,&evt,fontMain,0); + setInputEvents(&inputMines,&evt,fontMain,0); + + if(btnBack.leftButtonClicked==1) + { + return BACK;/*TODO:Felesleges*/ + } + if(btnNext.leftButtonClicked==1) + { + /*FIXME:Table table; nélkül*/ + createTable(&game->table,bgField,fontField,15/*game->width*/,15/*game->height*/); + + generateTable((game->table),15,15,20/*game->mines*/); + + return NEXT; + } + + + SDL_Flip(screen); + } +} + +void drawGame(Game* game,SDL_Surface* screen,TTF_Font* fontButton,TTF_Font* fontField,SDL_Surface* bgMain,SDL_Surface* bgButton) +{ + SDL_Rect rectPause= {420,420,200,50},rectReturn= {200,120,200,50},rectExit= {200,190,200,50}; + SDL_Event evt= {0}; + Button btnPause,btnReturn,btnExit; + int state=GAME; + + setButton(&btnPause,rectPause,bgButton,fontButton,"Pause"); + setButton(&btnReturn,rectReturn,bgButton,fontButton,"Return"); + setButton(&btnExit,rectExit,bgButton,fontButton,"Exit"); + + + while(state!=EXIT) + { + SDL_WaitEvent(&evt); + SDL_BlitSurface(bgMain,NULL,screen,NULL); + if(state==GAME) + { + drawButton(screen,&evt,&btnPause); + setButtonEvents(&btnPause,&evt); + + + drawTable(screen,game->table,fontField,&evt,15,15); + + if(btnPause.leftButtonClicked==1) + { + state=PAUSE; + } + + } + else /*TODO:Fügvény esetleg*/ + { + drawButton(screen,&evt,&btnReturn); + drawButton(screen,&evt,&btnExit); + + setButtonEvents(&btnReturn,&evt); + setButtonEvents(&btnExit,&evt); + if(btnReturn.leftButtonClicked==1)state=GAME; + if(btnExit.leftButtonClicked==1) + { + state=EXIT; + destroyTable(&game->table,15/*game->height*/); + /*TODO:Mentés*/ + } + } + + + SDL_Flip(screen); + } +} diff --git a/game.h b/game.h new file mode 100644 index 0000000000000000000000000000000000000000..fbe8aed1fdfaeaad807e17af7798a4a2c64ac7d6 --- /dev/null +++ b/game.h @@ -0,0 +1,19 @@ +#ifndef GAME_H_INCLUDED +#define GAME_H_INCLUDED + +#include <SDL.h> +#include <SDL_ttf.h> +#include "table.h" + +typedef struct Game +{ + int width; + int height; + int mines; + Table table; +} Game; + +int drawGameSetting(Game*,SDL_Surface*,TTF_Font*,TTF_Font*,SDL_Surface*,SDL_Surface*,SDL_Surface*); +void drawGame(Game*,SDL_Surface*,TTF_Font*,TTF_Font*,SDL_Surface*,SDL_Surface*); + +#endif diff --git a/highscore.c b/highscore.c new file mode 100644 index 0000000000000000000000000000000000000000..c64bd27ebf334adac236b1ce34fbfec84994dce1 --- /dev/null +++ b/highscore.c @@ -0,0 +1,22 @@ +#include "highscore.h" +#include "button.h" + +void drawHighscore(SDL_Surface* screen,TTF_Font* fontMain,SDL_Surface* bgMain,SDL_Surface* bgButton) +{ + SDL_Rect rectBack= {420,380,200,50}; + SDL_Event evt= {0}; + Button btnBack; + + setButton(&btnBack,rectBack,bgButton,fontMain,"Back"); + while(btnBack.leftButtonClicked!=1) + { + + SDL_WaitEvent(&evt); + SDL_BlitSurface(bgMain,NULL,screen,NULL); + drawButton(screen,&evt,&btnBack); + setButtonEvents(&btnBack,&evt); + + + SDL_Flip(screen); + } +} diff --git a/highscore.h b/highscore.h new file mode 100644 index 0000000000000000000000000000000000000000..c112a16171885dfe9c87df55b11b7cbf468a13f7 --- /dev/null +++ b/highscore.h @@ -0,0 +1,9 @@ +#ifndef HIGHSCORE_H_INCLUDED +#define HIGHSCORE_H_INCLUDED + +#include <SDL.h> +#include <SDL_ttf.h> + +void drawHighscore(SDL_Surface* screen,TTF_Font* font,SDL_Surface*bg,SDL_Surface* btnBg); + +#endif diff --git a/init.c b/init.c new file mode 100644 index 0000000000000000000000000000000000000000..c3848894510f4b839b320dfcc38f68f4516b3b52 --- /dev/null +++ b/init.c @@ -0,0 +1,29 @@ +#include "init.h" + +void init_framework(SDL_Surface** screen) +{ + SDL_Init(SDL_INIT_EVERYTHING); + IMG_Init(IMG_INIT_JPG); + + *screen=SDL_SetVideoMode(640,480,32,/*SDL_ANYFORMAT*/SDL_FULLSCREEN); + SDL_EnableKeyRepeat(500, 30); + +} + +void init_ttf(TTF_Font** font) +{ + + TTF_Init(); + *font = TTF_OpenFont("LiberationSerif-Regular.ttf", 20); + if (!*font) + { + fprintf(stderr, "Nem sikerult megnyitni a fontot! %s\n", TTF_GetError()); + exit(2); + } +} + +void quit_framework() +{ + TTF_Quit(); + SDL_Quit(); +} diff --git a/init.h b/init.h new file mode 100644 index 0000000000000000000000000000000000000000..565200bb000ff0833f9b8b2b372ba46ef32bc647 --- /dev/null +++ b/init.h @@ -0,0 +1,12 @@ +#ifndef INIT_H_INCLUDED +#define INIT_H_INCLUDED + +#include <SDL.h> +#include <SDL_ttf.h> +#include <SDL_image.h> + +void init_framework(SDL_Surface**); +void init_ttf(TTF_Font** font); +void quit_framework(); + +#endif diff --git a/menu.c b/menu.c new file mode 100644 index 0000000000000000000000000000000000000000..b8651f958378cf814bed6e64f216fd2c27c2566a --- /dev/null +++ b/menu.c @@ -0,0 +1,69 @@ +#include "menu.h" +#include "button.h" +#include "SDL_image.h" +#include "enums.h" + + +int drawMenu(SDL_Surface* screen,TTF_Font* font,SDL_Surface* bg,SDL_Surface* btnBg) +{ + + SDL_Rect rectContinue= {200,50,200,50}; + SDL_Rect rectNew= {200,120,200,50}; + SDL_Rect rectHighScore= {200,190,200,50}; + SDL_Rect rectExit= {200,410,200,50}; + + + SDL_Event evt={0}; + Button btnContinue,btnNew,btnHighScore,btnExit; + + + /*Menu gombok*/ + setButton(&btnContinue,rectContinue,btnBg,font,"Continue"); + setButton(&btnNew,rectNew,btnBg,font,"NewGame"); + setButton(&btnHighScore,rectHighScore,btnBg,font,"HighScore"); + setButton(&btnExit,rectExit,btnBg,font,"Exit"); + /*Focus*/ + setActive(&btnContinue,0); + /*setActive(&btnHighScore,0);*/ + while(/*btnExit.leftButtonClicked!=1*//*TODO:Ez egy ronda ciklus :D*/1) + { + /*TODO:Tömbe helyezni és enum*/ + SDL_BlitSurface(bg,NULL,screen,NULL); + + drawButton(screen,&evt,&btnContinue); + drawButton(screen,&evt,&btnNew); + drawButton(screen,&evt,&btnHighScore); + drawButton(screen,&evt,&btnExit); + + + setButtonEvents(&btnContinue,&evt); + setButtonEvents(&btnNew,&evt); + setButtonEvents(&btnHighScore,&evt); + setButtonEvents(&btnExit,&evt); + + + + if(btnContinue.leftButtonClicked==1) + { + return CONTINUE; + } + if(btnNew.leftButtonClicked==1) + { + return NEW_GAME; + } + if(btnHighScore.leftButtonClicked==1) + { + return HIGH_SCORE; + } + if(btnExit.leftButtonClicked==1) + { + return QUIT_TO_OS; + } + + SDL_Flip(screen); + SDL_WaitEvent(&evt); + + } + + return 1; +} diff --git a/menu.h b/menu.h new file mode 100644 index 0000000000000000000000000000000000000000..e328fde54a0bbc665609fbcf96711c3ad70893d0 --- /dev/null +++ b/menu.h @@ -0,0 +1,8 @@ +#ifndef MENU_H_INCLUDED +#define MENU_H_INCLUDED + +#include <SDL_ttf.h> + +int drawMenu(SDL_Surface*,TTF_Font*,SDL_Surface*,SDL_Surface*); + +#endif diff --git a/table.c b/table.c new file mode 100644 index 0000000000000000000000000000000000000000..d3c06dd3889ce92219563cfad72e95f9425b1c63 --- /dev/null +++ b/table.c @@ -0,0 +1,185 @@ +#include "table.h" + +#include <stdlib.h> + +#include "enums.h" +#include "colors.h" + +void drawTable(SDL_Surface* screen,Table table,TTF_Font* font,SDL_Event* evt,int width,int height)/*Át gondolni a fontot*/ +{ + int x,y; + for(y=0; y<height; y++) + { + for(x=0; x<width; x++) + { + drawField(screen,evt,&table[y][x]); + setButtonEvents(&table[y][x].btnField,evt); + if(table[y][x].btnField.leftButtonClicked==1){ + if(fieldRevealing(table,font,x,y,width,height))/*Vissza térés játék vége*/ + { + /*TODO:Game over*/ + } + } + if(table[y][x].btnField.rightButtonClicked==1){ + setFlag(table[y][x]); + setText(&table[y][x].btnField,font,"F",red); + } + } + } + +} + +void createTable(Table* table,SDL_Surface* bgField,TTF_Font* fieldFont,int width,int height) /*TODO:Méret védelem,képméret alapján*/ +{ + int i,j; + SDL_Rect rectFields= {0,0,20,20}; + /*TODO:Hiba kezelés*/ + *table=(Table)malloc(sizeof(Field*)*height); + + for(i=0; i<height; i++) + { + (*table)[i]=(Field*)malloc(sizeof(Field)*width); + + } + + /*TODO:Képmérethez szabni*/ + rectFields.x=320-10*width;/*(640-20*width)/2*/ + rectFields.y=240-10*height;/*(240-20*heght)/2*/ + + for(i=0; i<height; i++) + { + for(j=0; j<width; j++) + { + setButton(&((*table)[i][j]).btnField,rectFields,bgField,fieldFont," "); + (*table)[i][j].type=0; + (*table)[i][j].state=0; + rectFields.x+=20;/*széleség*/ + } + rectFields.x=320-10*width;/*(640-20*width)/2*/ + rectFields.y+=20;/*magasság*/ + } + +} + +void destroyTable(Table* table,int height)/*TODO:Vektoros módszer*/ +{ + int i; + + for(i=0; i<height; i++) + { + /*free(table[i]);*/ + } + /*free(table);*/ +} + +void generateTable(Table table,int width,int height,int mines) +{ + int i,j,k,x,y; + + for(i=0; i<mines; i++) + { + x=rand()%width; + y=rand()%height; + + if(table[y][x].type!=MINE) + { + table[y][x].type=MINE; + for(j=-1; j<2; j++)/*Y*/ + { + for(k=-1; k<2; k++)/*X*/ + { + if( (0<=x+k && x+k < width) && (0<=y+j && y+j < height) ) + { + if(table[y+j][x+k].type!=MINE) + { + table[y+j][x+k].type++; + } + } + } + } + } + else + { + i--; + } + } + +} + + + +void allFieldRevealing(Table table,TTF_Font* font,int width,int height){ + int x,y; + char puffer[2]; + + for(y=0;y<height;y++){ + for(x=0;x<width;x++){ + if(table[y][x].state!=UNCOVERED){ + table[y][x].state=UNCOVERED; + if(table[y][x].type==MINE){ + setText(&table[y][x].btnField,font,"*",red); + table[y][x].btnField.isActive=0; + } + else{ + if(table[y][x].type==EMPTY){ + table[y][x].btnField.isActive=0; + + }else{ + table[y][x].btnField.isActive=0; + sprintf(puffer,"%d",table[y][x].type); + setText(&table[y][x].btnField,font,puffer,white); + } + + } + } + } + } + +} + + + + + + + + + + +int fieldRevealing(Table table,TTF_Font* font,int x,int y,int width,int height){ + int i,j,ret=0; + char puffer[2]; + + if(table[y][x].type==9) + { + ret=1; + allFieldRevealing(table,font,width,height); + } + else + { + table[y][x].state=1; + table[y][x].btnField.isActive=0; + if(table[y][x].type!=0){ + sprintf(puffer,"%d",table[y][x].type); + setText(&table[y][x].btnField,font,puffer,white); + } + + for(i=-1; i<2; i++) + { + for(j=-1; j<2; j++) + { + if( (0<=x+j && x+j<width) && (0<=y+i && y+i<height)) + { + if(table[y][x].type==0 && table[y+i][x+j].state!=2 && table[y+i][x+j].state!=1) + { + ret=fieldRevealing(table,font,x+j,y+i,width,height); + } + } + } + } + } + + return ret; +} + + diff --git a/table.h b/table.h new file mode 100644 index 0000000000000000000000000000000000000000..e98addbc173ddf183837e1ced61d713306ecb99a --- /dev/null +++ b/table.h @@ -0,0 +1,20 @@ +#ifndef TABLE_H_INCLUDED +#define TABLE_H_INCLUDED + +#include "field.h" + +typedef Field** Table; + +void drawTable(SDL_Surface*,Table,TTF_Font*,SDL_Event*,int width,int height); + +/*Mentés és visszatöltés*/ +void saveTable(); +void loadTable(); +/*Példányosítás és eltörlés*/ +void createTable(Table* table,SDL_Surface*,TTF_Font*,int,int); +void destroyTable(Table*,int); +/*Pálya generálás*/ +void generateTable(Table,int width,int height,int mines); + + +#endif