Add start start screen
- many updates in some files
This commit is contained in:
parent
965372c7bf
commit
c10481021f
BIN
fonts/CenturyGothic.ttf
Normal file
BIN
fonts/CenturyGothic.ttf
Normal file
Binary file not shown.
BIN
fonts/Prototype.ttf
Normal file
BIN
fonts/Prototype.ttf
Normal file
Binary file not shown.
BIN
fonts/Quirkus.ttf
Normal file
BIN
fonts/Quirkus.ttf
Normal file
Binary file not shown.
BIN
fonts/ZIPERHEA.ttf
Normal file
BIN
fonts/ZIPERHEA.ttf
Normal file
Binary file not shown.
@ -1,6 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "SDL.h"
|
||||
#include "SDL_ttf.h"
|
||||
#include "common.h"
|
||||
|
||||
#define MAX_COLOURS 10
|
||||
#define MAX_LINES_TFF 20 //max lines (of ttf) for the whole project
|
||||
#define MAX_LINES_STARTSCREEN 5
|
||||
#define TIME_BETWEEN_UPDATE_MENU 1000 // [ms]
|
||||
|
||||
// Enum that defines the active menu
|
||||
typedef enum menus_t
|
||||
{
|
||||
NONE = 0,
|
||||
START,
|
||||
SETTINGS,
|
||||
LEADERBOARD,
|
||||
PAUSE
|
||||
} menus_t;
|
||||
|
||||
// Struct that include pointer for ttl-functions in menu.c and render.c
|
||||
typedef struct tllData_t
|
||||
{
|
||||
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_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;
|
||||
bool showEnter;
|
||||
} ttlData_t;
|
||||
|
||||
extern ttlData_t ttlStorage;
|
||||
|
||||
extern menus_t activeMenu;
|
||||
|
||||
// edit various menus
|
||||
// is called up in main.cpp
|
||||
void manageMenu();
|
||||
|
||||
|
||||
|
||||
void showStartScreen();
|
||||
//zum Starten Enter drücken
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include "game.h"
|
||||
#include "snake.h"
|
||||
#include "menu.h"
|
||||
#include "SDL.h"
|
||||
|
||||
void renderGame();
|
||||
@ -10,3 +11,5 @@ void renderGame();
|
||||
int CreateSDLWindow();
|
||||
|
||||
void DestroySDLWindow();
|
||||
|
||||
void renderStartMenu();
|
@ -16,7 +16,7 @@ gameData_t game = {
|
||||
.mapIsLoaded = false,
|
||||
.lifesRemaining = 1,
|
||||
.timestampLastCycle = 0,
|
||||
.gameState = RUNNING,
|
||||
.gameState = MENU,
|
||||
};
|
||||
|
||||
|
||||
|
15
src/main.cpp
15
src/main.cpp
@ -7,6 +7,7 @@ extern "C"
|
||||
#include "common.h"
|
||||
#include "input.h"
|
||||
#include "render.h"
|
||||
#include "menu.h"
|
||||
}
|
||||
|
||||
//initialize SDL window
|
||||
@ -37,8 +38,15 @@ int main(int argc, char *argv[])
|
||||
printf("SDL konnte nicht initialisiert werden! SDL_Error: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
CreateSDLWindow();
|
||||
|
||||
// Initialisiere SDL_ttl, um Text ausgeben zu können
|
||||
if (TTF_Init() == -1) {
|
||||
printf("SDL_ttf konnte nicht initialisiert werden! SDL_Error: %s\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
CreateSDLWindow();
|
||||
|
||||
|
||||
|
||||
@ -49,6 +57,11 @@ int main(int argc, char *argv[])
|
||||
int diff;
|
||||
|
||||
while(game.gameState != EXIT) {
|
||||
if(game.gameState == MENU)
|
||||
{
|
||||
manageMenu();
|
||||
}
|
||||
|
||||
if (game.gameState == RUNNING) {
|
||||
now = GET_TIME_MS(); // Timer startet
|
||||
diff = now-game.timestampLastCycle;
|
||||
|
70
src/menu.c
70
src/menu.c
@ -6,12 +6,52 @@
|
||||
//#include <Windows.h>
|
||||
|
||||
|
||||
void showStartScreen(){
|
||||
|
||||
//define global struct for tllStorage values
|
||||
//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,
|
||||
.showEnter = false
|
||||
};
|
||||
|
||||
// Default
|
||||
menus_t activeMenu = START;
|
||||
|
||||
// is called by main function
|
||||
// choose between the selected menu-rendering functions
|
||||
void manageMenu()
|
||||
{
|
||||
switch(activeMenu)
|
||||
{
|
||||
case START:
|
||||
showStartScreen();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void showStartScreen()
|
||||
{
|
||||
LOGI("menu: showing start-screen\n");
|
||||
game.gameState = RUNNING;
|
||||
time_t now = GET_TIME_MS();
|
||||
if(now > (ttlStorage.lastTimeStep + ttlStorage.cycleDuration))
|
||||
{
|
||||
ttlStorage.showEnter = !ttlStorage.showEnter; // is used to make ENTER blink
|
||||
renderStartMenu();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void showLeaderboard(){
|
||||
LOGI("menu: showing leaderboard\n");
|
||||
|
||||
@ -39,5 +79,31 @@ void showSettings(){
|
||||
|
||||
void menuHandleInput(SDL_Event event){
|
||||
//compare 'handleInput_runningState(SDL_Event event)' in input.c
|
||||
switch(activeMenu)
|
||||
{
|
||||
case START:
|
||||
switch (event.key.keysym.sym)
|
||||
{
|
||||
case SDLK_q: // q: quit
|
||||
game.gameState = EXIT;
|
||||
break;
|
||||
|
||||
case SDLK_RETURN: // Enter key
|
||||
game.gameState = RUNNING;
|
||||
activeMenu = NONE;
|
||||
// delete text
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
SDL_DestroyTexture(ttlStorage.textTextures[i]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case SETTINGS:
|
||||
break;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
127
src/render.c
127
src/render.c
@ -4,6 +4,8 @@
|
||||
#include "snake.h"
|
||||
#include "food.h"
|
||||
#include "config.h"
|
||||
#include "menu.h"
|
||||
#include "common.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
@ -113,6 +115,123 @@ void renderGame(){
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------
|
||||
//-----------------START MENU-----------------------------------
|
||||
//--------------------------------------------------------------
|
||||
void renderStartMenu()
|
||||
{
|
||||
|
||||
|
||||
//--------------------
|
||||
// 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);
|
||||
|
||||
|
||||
SDL_Color textColor1 = {255, 0, 255}; // rosa Text
|
||||
SDL_Color textColor2 = {255, 200, 0}; // oranger Text
|
||||
SDL_Color textColor3 = {0, 255, 0}; // grüner Text
|
||||
SDL_Color textColor4 = {255, 0, 0}; // rot Text
|
||||
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;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// text in start screen
|
||||
const char* textLines[] = {
|
||||
"Snake++",
|
||||
"In Pixeln bunt, sie schlaengelt flink,",
|
||||
"Durch Mauern, Portale, ohne Blink.",
|
||||
"Starte das Spiel, sei klug und schnell,",
|
||||
"Die Schlange wartet, auf dein Pixel-Ziel!",
|
||||
"-- ENTER --"
|
||||
};
|
||||
|
||||
|
||||
// render game name (SNAKE++) bigger than poem
|
||||
ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_200, textLines[0], ttlStorage.textColour[0]);
|
||||
ttlStorage.textTextures[0] = SDL_CreateTextureFromSurface(game.renderer, ttlStorage.textSurface);
|
||||
SDL_FreeSurface(ttlStorage.textSurface);
|
||||
|
||||
// render poem
|
||||
for (int i = 1; i < (MAX_LINES_STARTSCREEN); ++i)
|
||||
{
|
||||
ttlStorage.textSurface = TTF_RenderText_Solid(ttlStorage.ptrFont_30, textLines[i], ttlStorage.textColour[i]);
|
||||
ttlStorage.textTextures[i] = SDL_CreateTextureFromSurface(game.renderer, ttlStorage.textSurface);
|
||||
SDL_FreeSurface(ttlStorage.textSurface);
|
||||
}
|
||||
|
||||
// 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);
|
||||
SDL_FreeSurface(ttlStorage.textSurface);
|
||||
}
|
||||
|
||||
|
||||
//------------------------------
|
||||
// is always performed
|
||||
//-----------------------------
|
||||
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);
|
||||
SDL_Rect dstRect = { (config.windowSize - textWidth) / 2, ttlStorage.textPrintPosition, textWidth, textHeight };
|
||||
SDL_RenderCopy(game.renderer, ttlStorage.textTextures[0], NULL, &dstRect);
|
||||
|
||||
// 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;
|
||||
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_Rect dstRect1 = { (config.windowSize - textWidth) / 2, ttlStorage.textPrintPosition, textWidth, textHeight };
|
||||
SDL_RenderCopy(game.renderer, ttlStorage.textTextures[MAX_LINES_STARTSCREEN], NULL, &dstRect1);
|
||||
}
|
||||
|
||||
|
||||
SDL_RenderPresent(game.renderer);
|
||||
ttlStorage.lastTimeStep = GET_TIME_MS();
|
||||
// show inital menu frame
|
||||
|
||||
|
||||
return;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int CreateSDLWindow(){
|
||||
// Erstelle ein SDL-Fenster
|
||||
@ -132,6 +251,14 @@ int CreateSDLWindow(){
|
||||
|
||||
void DestroySDLWindow(){
|
||||
// Zerstöre das Fenster und beende SDL
|
||||
|
||||
TTF_CloseFont(ttlStorage.ptrFont_30);
|
||||
TTF_CloseFont(ttlStorage.ptrFont_200);
|
||||
|
||||
SDL_DestroyRenderer(game.renderer);
|
||||
SDL_DestroyWindow(game.window);
|
||||
|
||||
TTF_Quit();
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user