Add setting and info screen
- setting screen is only implemented, without features - info screen is completed currently
This commit is contained in:
parent
c10481021f
commit
a7714ec15f
@ -7,7 +7,9 @@
|
||||
|
||||
#define MAX_COLOURS 10
|
||||
#define MAX_LINES_TFF 20 //max lines (of ttf) for the whole project
|
||||
#define MAX_LINES_STARTSCREEN 5
|
||||
#define MAX_LINES_STARTSCREEN 6
|
||||
#define MAX_LINES_SETTINGS 7
|
||||
#define MAX_LINE_INFOSCREEN 17
|
||||
#define TIME_BETWEEN_UPDATE_MENU 1000 // [ms]
|
||||
|
||||
// Enum that defines the active menu
|
||||
@ -16,6 +18,7 @@ typedef enum menus_t
|
||||
NONE = 0,
|
||||
START,
|
||||
SETTINGS,
|
||||
INFOSCREEN,
|
||||
LEADERBOARD,
|
||||
PAUSE
|
||||
} menus_t;
|
||||
@ -23,19 +26,21 @@ typedef enum menus_t
|
||||
// Struct that include pointer for ttl-functions in menu.c and render.c
|
||||
typedef struct tllData_t
|
||||
{
|
||||
TTF_Font *ptrFont_20;
|
||||
TTF_Font *ptrFont_30; // used by menu.c and render.c
|
||||
TTF_Font *ptrFont_200; // used by menu.c and render.c
|
||||
SDL_Surface *textSurface; // used by menu.c and render.c
|
||||
SDL_Texture *textTexture; // used by menu.c and render.c
|
||||
SDL_Texture *textTextures[MAX_LINES_TFF];
|
||||
SDL_Color textColour[MAX_COLOURS]; // colour in which the text is printed
|
||||
const int fontSize_20;
|
||||
const int fontSize_30;
|
||||
const int fontSize_200;
|
||||
int64_t lastTimeStep;
|
||||
int lineHeight; // to print text in middle
|
||||
int totalHeight; // to print text in middle
|
||||
int textPrintPosition; // where first line is printed
|
||||
time_t cycleDuration;
|
||||
const time_t cycleDuration;
|
||||
bool showEnter;
|
||||
} ttlData_t;
|
||||
|
||||
@ -61,6 +66,12 @@ void showLeaderboard();
|
||||
void showSettings(); //optional
|
||||
//startet Settungs-Menü
|
||||
|
||||
|
||||
// show info screen
|
||||
void showInfoScreen();
|
||||
|
||||
void menuHandleInput(SDL_Event event); //als Übergabeparameter: int(?) event -> welcher Datentyp hängt von SDL ab
|
||||
//switch case für welcher Modus
|
||||
//switch case für welche Taste gedrückt wurde
|
||||
|
||||
|
||||
|
@ -13,3 +13,7 @@ int CreateSDLWindow();
|
||||
void DestroySDLWindow();
|
||||
|
||||
void renderStartMenu();
|
||||
|
||||
void renderSettings();
|
||||
|
||||
void renderInfoScreen();
|
131
src/menu.c
131
src/menu.c
@ -11,18 +11,18 @@
|
||||
//default values defined here: (note: gets modified by render.c or menu.c)
|
||||
ttlData_t ttlStorage =
|
||||
{
|
||||
.fontSize_30 = 30,
|
||||
.fontSize_200 = 200,
|
||||
.lastTimeStep = 0,
|
||||
.cycleDuration = 500,
|
||||
.fontSize_20 = 20, // size of font
|
||||
.fontSize_30 = 30, // size of font
|
||||
.fontSize_200 = 200, // size of font
|
||||
.cycleDuration = 500, // time between show and not showing ENTER in start screen
|
||||
.showEnter = false
|
||||
};
|
||||
|
||||
// Default
|
||||
menus_t activeMenu = START;
|
||||
|
||||
// is called by main function
|
||||
// choose between the selected menu-rendering functions
|
||||
// is called by main function if game.gameState == MENU
|
||||
// choose between the selected menu functions
|
||||
void manageMenu()
|
||||
{
|
||||
switch(activeMenu)
|
||||
@ -30,29 +30,45 @@ void manageMenu()
|
||||
case START:
|
||||
showStartScreen();
|
||||
break;
|
||||
|
||||
case SETTINGS:
|
||||
showSettings();
|
||||
break;
|
||||
|
||||
case INFOSCREEN:
|
||||
showInfoScreen();
|
||||
break;
|
||||
|
||||
case LEADERBOARD:
|
||||
showLeaderboard();
|
||||
break;
|
||||
|
||||
case PAUSE:
|
||||
showPauseScreen();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// shows start screen with blinking ENTER
|
||||
void showStartScreen()
|
||||
{
|
||||
LOGI("menu: showing start-screen\n");
|
||||
|
||||
time_t now = GET_TIME_MS();
|
||||
|
||||
// is used to make ENTER blink
|
||||
if(now > (ttlStorage.lastTimeStep + ttlStorage.cycleDuration))
|
||||
{
|
||||
ttlStorage.showEnter = !ttlStorage.showEnter; // is used to make ENTER blink
|
||||
ttlStorage.showEnter = !ttlStorage.showEnter;
|
||||
renderStartMenu();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void showLeaderboard(){
|
||||
void showLeaderboard()
|
||||
{
|
||||
LOGI("menu: showing leaderboard\n");
|
||||
|
||||
//--- play crash sound ---
|
||||
@ -66,17 +82,50 @@ void showLeaderboard(){
|
||||
return;
|
||||
}
|
||||
|
||||
void showPauseScreen(){
|
||||
void showPauseScreen()
|
||||
{
|
||||
LOGI("menu: showing leaderboard\n");
|
||||
game.gameState = PAUSED;
|
||||
return;
|
||||
}
|
||||
|
||||
void showSettings(){
|
||||
|
||||
void showSettings()
|
||||
{
|
||||
LOGI("menu: showing settings\n");
|
||||
|
||||
|
||||
time_t now = GET_TIME_MS();
|
||||
|
||||
// is used to make ENTER blink
|
||||
if(now > (ttlStorage.lastTimeStep + ttlStorage.cycleDuration))
|
||||
{
|
||||
ttlStorage.showEnter = !ttlStorage.showEnter;
|
||||
renderSettings();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void showInfoScreen()
|
||||
{
|
||||
LOGI("menu: showing info-screen\n");
|
||||
|
||||
time_t now = GET_TIME_MS();
|
||||
|
||||
// is used to make ENTER blink
|
||||
if(now > (ttlStorage.lastTimeStep + ttlStorage.cycleDuration))
|
||||
{
|
||||
ttlStorage.showEnter = !ttlStorage.showEnter;
|
||||
renderInfoScreen();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// handle input over keyboard
|
||||
// delete text at the and of one menu section
|
||||
void menuHandleInput(SDL_Event event){
|
||||
//compare 'handleInput_runningState(SDL_Event event)' in input.c
|
||||
switch(activeMenu)
|
||||
@ -89,10 +138,10 @@ void menuHandleInput(SDL_Event event){
|
||||
break;
|
||||
|
||||
case SDLK_RETURN: // Enter key
|
||||
game.gameState = RUNNING;
|
||||
activeMenu = NONE;
|
||||
activeMenu = SETTINGS;
|
||||
ttlStorage.lastTimeStep = 0;
|
||||
// delete text
|
||||
for (int i = 0; i < 5; ++i)
|
||||
for (int i = 0; i < MAX_LINES_TFF; ++i)
|
||||
{
|
||||
SDL_DestroyTexture(ttlStorage.textTextures[i]);
|
||||
}
|
||||
@ -101,9 +150,55 @@ void menuHandleInput(SDL_Event event){
|
||||
|
||||
|
||||
case SETTINGS:
|
||||
switch(event.key.keysym.sym)
|
||||
{
|
||||
case SDLK_q: // q: quit
|
||||
game.gameState = EXIT;
|
||||
break;
|
||||
|
||||
case SDLK_F1: // go to info screen
|
||||
activeMenu = INFOSCREEN;
|
||||
ttlStorage.lastTimeStep = 0;
|
||||
// delete text
|
||||
for (int i = 0; i < MAX_LINES_TFF; ++i)
|
||||
{
|
||||
SDL_DestroyTexture(ttlStorage.textTextures[i]);
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_RETURN: //start game
|
||||
activeMenu = NONE;
|
||||
game.gameState = RUNNING;
|
||||
// delete text
|
||||
for (int i = 0; i < MAX_LINES_TFF; ++i)
|
||||
{
|
||||
SDL_DestroyTexture(ttlStorage.textTextures[i]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case INFOSCREEN:
|
||||
switch(event.key.keysym.sym)
|
||||
{
|
||||
case SDLK_q: // q: quit
|
||||
game.gameState = EXIT;
|
||||
break;
|
||||
|
||||
case SDLK_RETURN: // go return to settings
|
||||
activeMenu = SETTINGS;
|
||||
ttlStorage.lastTimeStep = 0;
|
||||
// delete text
|
||||
for (int i = 0; i < MAX_LINES_TFF; ++i)
|
||||
{
|
||||
SDL_DestroyTexture(ttlStorage.textTextures[i]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
186
src/render.c
186
src/render.c
@ -120,15 +120,9 @@ void renderGame(){
|
||||
//--------------------------------------------------------------
|
||||
void renderStartMenu()
|
||||
{
|
||||
|
||||
|
||||
//--------------------
|
||||
// only first loop
|
||||
//--------------------
|
||||
//=========== only first loop ================
|
||||
if(ttlStorage.lastTimeStep == 0)
|
||||
{
|
||||
|
||||
|
||||
ttlStorage.ptrFont_200 = TTF_OpenFont("../fonts/Quirkus.ttf", ttlStorage.fontSize_200);
|
||||
ttlStorage.ptrFont_30 = TTF_OpenFont("../fonts/Quirkus.ttf", ttlStorage.fontSize_30);
|
||||
|
||||
@ -140,16 +134,12 @@ void renderStartMenu()
|
||||
SDL_Color textColor5 = {0, 255, 255}; // türkiser Text
|
||||
SDL_Color textColor6 = {255, 255, 255}; // weißer Text
|
||||
|
||||
ttlStorage.textColour[0] = textColor1;
|
||||
ttlStorage.textColour[1] = textColor2;
|
||||
ttlStorage.textColour[2] = textColor3;
|
||||
ttlStorage.textColour[3] = textColor4;
|
||||
ttlStorage.textColour[4] = textColor5;
|
||||
ttlStorage.textColour[5] = textColor6;
|
||||
|
||||
|
||||
|
||||
|
||||
ttlStorage.textColour[0] = textColor1; // rosa
|
||||
ttlStorage.textColour[1] = textColor2; // orange
|
||||
ttlStorage.textColour[2] = textColor3; // grün
|
||||
ttlStorage.textColour[3] = textColor4; // rot
|
||||
ttlStorage.textColour[4] = textColor5; // türkis
|
||||
ttlStorage.textColour[5] = textColor6; // weiß
|
||||
|
||||
// text in start screen
|
||||
const char* textLines[] = {
|
||||
@ -168,7 +158,7 @@ void renderStartMenu()
|
||||
SDL_FreeSurface(ttlStorage.textSurface);
|
||||
|
||||
// render poem
|
||||
for (int i = 1; i < (MAX_LINES_STARTSCREEN); ++i)
|
||||
for (int i = 1; i < (MAX_LINES_STARTSCREEN - 1); ++i)
|
||||
{
|
||||
ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_30, textLines[i], ttlStorage.textColour[i]);
|
||||
ttlStorage.textTextures[i] = SDL_CreateTextureFromSurface(game.renderer, ttlStorage.textSurface);
|
||||
@ -176,22 +166,17 @@ void renderStartMenu()
|
||||
}
|
||||
|
||||
// render ENTER
|
||||
|
||||
|
||||
ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_30, textLines[MAX_LINES_STARTSCREEN], ttlStorage.textColour[5]);
|
||||
ttlStorage.textTextures[MAX_LINES_STARTSCREEN] = SDL_CreateTextureFromSurface(game.renderer, ttlStorage.textSurface);
|
||||
ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_30, textLines[MAX_LINES_STARTSCREEN-1], ttlStorage.textColour[5]);
|
||||
ttlStorage.textTextures[MAX_LINES_STARTSCREEN-1] = SDL_CreateTextureFromSurface(game.renderer, ttlStorage.textSurface);
|
||||
SDL_FreeSurface(ttlStorage.textSurface);
|
||||
}
|
||||
|
||||
|
||||
//------------------------------
|
||||
// is always performed
|
||||
//-----------------------------
|
||||
//=========== is always performerd ================
|
||||
SDL_RenderClear(game.renderer);
|
||||
|
||||
int textWidth, textHeight;
|
||||
|
||||
|
||||
// print game name
|
||||
ttlStorage.textPrintPosition = (config.windowSize) / 9; // print position for game name (SNAKE++)
|
||||
SDL_QueryTexture(ttlStorage.textTextures[0], NULL, NULL, &textWidth, &textHeight);
|
||||
@ -200,35 +185,165 @@ void renderStartMenu()
|
||||
|
||||
// print poem
|
||||
ttlStorage.textPrintPosition = (config.windowSize) / 2.7; // change print position for poem
|
||||
for (int i = 1; i < (MAX_LINES_STARTSCREEN); ++i) {
|
||||
int textWidth, textHeight;
|
||||
for (int i = 1; i < (MAX_LINES_STARTSCREEN-1); ++i) {
|
||||
SDL_QueryTexture(ttlStorage.textTextures[i], NULL, NULL, &textWidth, &textHeight);
|
||||
|
||||
|
||||
SDL_Rect dstRect = { (config.windowSize - textWidth) / 2, ttlStorage.textPrintPosition, textWidth, textHeight };
|
||||
SDL_RenderCopy(game.renderer, ttlStorage.textTextures[i], NULL, &dstRect);
|
||||
ttlStorage.textPrintPosition += textHeight; // increase print position
|
||||
|
||||
|
||||
}
|
||||
//print ENTER every second cycle
|
||||
if(ttlStorage.showEnter)
|
||||
{
|
||||
ttlStorage.textPrintPosition = (config.windowSize / 1.5); // print position for ENTER
|
||||
SDL_QueryTexture(ttlStorage.textTextures[MAX_LINES_STARTSCREEN], NULL, NULL, &textWidth, &textHeight);
|
||||
SDL_QueryTexture(ttlStorage.textTextures[MAX_LINES_STARTSCREEN-1], NULL, NULL, &textWidth, &textHeight);
|
||||
SDL_Rect dstRect1 = { (config.windowSize - textWidth) / 2, ttlStorage.textPrintPosition, textWidth, textHeight };
|
||||
SDL_RenderCopy(game.renderer, ttlStorage.textTextures[MAX_LINES_STARTSCREEN], NULL, &dstRect1);
|
||||
SDL_RenderCopy(game.renderer, ttlStorage.textTextures[MAX_LINES_STARTSCREEN-1], NULL, &dstRect1);
|
||||
}
|
||||
|
||||
// update screen
|
||||
SDL_RenderPresent(game.renderer);
|
||||
ttlStorage.lastTimeStep = GET_TIME_MS();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------
|
||||
//-------------------SETTINGS-----------------------------------
|
||||
//--------------------------------------------------------------
|
||||
void renderSettings()
|
||||
{
|
||||
//=========== only first loop ================
|
||||
if(ttlStorage.lastTimeStep == 0)
|
||||
{
|
||||
ttlStorage.ptrFont_20 = TTF_OpenFont("../fonts/Prototype.ttf", ttlStorage.fontSize_20);
|
||||
|
||||
// text in start screen
|
||||
const char* textLines[] = {
|
||||
"Fuer Infos zum Gameplay, sowie einigen Shortcuts druecken Sie bitte F1",
|
||||
" ",
|
||||
//"Bitte geben Sie ihren Spielnamen ein: ",
|
||||
"test",
|
||||
"test",
|
||||
"test",
|
||||
"test",
|
||||
"-- ENTER --"
|
||||
};
|
||||
|
||||
// render setting screen
|
||||
for (int i = 0; i < MAX_LINES_SETTINGS; ++i)
|
||||
{
|
||||
ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_20, textLines[i], ttlStorage.textColour[5]);
|
||||
ttlStorage.textTextures[i] = SDL_CreateTextureFromSurface(game.renderer, ttlStorage.textSurface);
|
||||
SDL_FreeSurface(ttlStorage.textSurface);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//=========== is always performerd ================
|
||||
SDL_RenderClear(game.renderer);
|
||||
|
||||
int textWidth, textHeight;
|
||||
|
||||
// print settings
|
||||
ttlStorage.textPrintPosition = 0; // first print position
|
||||
for (int i = 0; i < (MAX_LINES_SETTINGS - 1); ++i) {
|
||||
|
||||
SDL_QueryTexture(ttlStorage.textTextures[i], NULL, NULL, &textWidth, &textHeight);
|
||||
|
||||
SDL_Rect dstRect = { 1, ttlStorage.textPrintPosition, textWidth, textHeight };
|
||||
SDL_RenderCopy(game.renderer, ttlStorage.textTextures[i], NULL, &dstRect);
|
||||
ttlStorage.textPrintPosition += textHeight; // increase print position
|
||||
}
|
||||
|
||||
//print ENTER every second cycle
|
||||
if(ttlStorage.showEnter)
|
||||
{
|
||||
ttlStorage.textPrintPosition = (config.windowSize / 1.5); // print position for ENTER
|
||||
SDL_QueryTexture(ttlStorage.textTextures[MAX_LINES_SETTINGS-1], NULL, NULL, &textWidth, &textHeight);
|
||||
SDL_Rect dstRect1 = { (config.windowSize - textWidth) / 2, ttlStorage.textPrintPosition, textWidth, textHeight };
|
||||
SDL_RenderCopy(game.renderer, ttlStorage.textTextures[MAX_LINES_SETTINGS-1], NULL, &dstRect1);
|
||||
}
|
||||
|
||||
// update screen
|
||||
SDL_RenderPresent(game.renderer);
|
||||
ttlStorage.lastTimeStep = GET_TIME_MS();
|
||||
// show inital menu frame
|
||||
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------
|
||||
//-------------------INFO SCREEN--------------------------------
|
||||
//--------------------------------------------------------------
|
||||
void renderInfoScreen()
|
||||
{
|
||||
//=========== only first loop ================
|
||||
if(ttlStorage.lastTimeStep == 0)
|
||||
{
|
||||
// text in start screen
|
||||
const char* textLines[] = {
|
||||
" Steuerung: W (nach oben)",
|
||||
" A (nach links)",
|
||||
" S (nach unten)",
|
||||
" D (nach rechts) ",
|
||||
" oder: Pfeiltasten ",
|
||||
" ",
|
||||
" ",
|
||||
" Pause: p",
|
||||
" oder: Leertaste",
|
||||
" ",
|
||||
" ",
|
||||
"Spiel verlassen: q ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
"By Jonas Schoenberger, Johannes Graf und Julia Steinberger",
|
||||
"-- ENTER --"
|
||||
};
|
||||
|
||||
// render setting screen
|
||||
for (int i = 0; i < MAX_LINE_INFOSCREEN; ++i)
|
||||
{
|
||||
ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_20, textLines[i], ttlStorage.textColour[5]);
|
||||
ttlStorage.textTextures[i] = SDL_CreateTextureFromSurface(game.renderer, ttlStorage.textSurface);
|
||||
SDL_FreeSurface(ttlStorage.textSurface);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//=========== is always performerd ================
|
||||
SDL_RenderClear(game.renderer);
|
||||
|
||||
int textWidth, textHeight;
|
||||
|
||||
// print settings
|
||||
ttlStorage.textPrintPosition = 0; // first print position
|
||||
for (int i = 0; i < (MAX_LINE_INFOSCREEN - 1); ++i) {
|
||||
|
||||
SDL_QueryTexture(ttlStorage.textTextures[i], NULL, NULL, &textWidth, &textHeight);
|
||||
|
||||
SDL_Rect dstRect = { 1, ttlStorage.textPrintPosition, textWidth, textHeight };
|
||||
SDL_RenderCopy(game.renderer, ttlStorage.textTextures[i], NULL, &dstRect);
|
||||
ttlStorage.textPrintPosition += textHeight; // increase print position
|
||||
}
|
||||
|
||||
//print ENTER every second cycle
|
||||
if(ttlStorage.showEnter)
|
||||
{
|
||||
ttlStorage.textPrintPosition = (config.windowSize / 1.5); // print position for ENTER
|
||||
SDL_QueryTexture(ttlStorage.textTextures[MAX_LINE_INFOSCREEN-1], NULL, NULL, &textWidth, &textHeight);
|
||||
SDL_Rect dstRect1 = { (config.windowSize - textWidth) / 2, ttlStorage.textPrintPosition, textWidth, textHeight };
|
||||
SDL_RenderCopy(game.renderer, ttlStorage.textTextures[MAX_LINE_INFOSCREEN-1], NULL, &dstRect1);
|
||||
}
|
||||
|
||||
|
||||
// update screen
|
||||
SDL_RenderPresent(game.renderer);
|
||||
ttlStorage.lastTimeStep = GET_TIME_MS();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@ -252,6 +367,7 @@ int CreateSDLWindow(){
|
||||
void DestroySDLWindow(){
|
||||
// Zerstöre das Fenster und beende SDL
|
||||
|
||||
TTF_CloseFont(ttlStorage.ptrFont_20);
|
||||
TTF_CloseFont(ttlStorage.ptrFont_30);
|
||||
TTF_CloseFont(ttlStorage.ptrFont_200);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user