-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRendering.c
183 lines (169 loc) · 6.33 KB
/
Rendering.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "PacSnake.h"
#include "Ghosts.h"
#include "PowerUps.h"
#include "Player.h"
#include "Position.h"
#include "Map.h"
#include "Highscore.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
int selected = 0;
SDL_Rect kasten;
SDL_Color white = {255, 255, 255, 255};
SDL_Color red = {255, 0, 0, 255};
TTF_Font *font;
void renderTexture(SDL_Texture *texture, int x, int y, int h, int w)
{
SDL_Rect helperRect;
helperRect.x = x;
helperRect.y = y;
helperRect.h = (h != 0) ? h : fieldHeight;
helperRect.w = (w != 0) ? w : fieldWidth;
SDL_RenderCopy(renderer, texture, NULL, &helperRect);
}
void renderText(const char *text, int x, int y, SDL_Color color, int center, int size)
{
if (size == 0) size = (width / 10);
font = TTF_OpenFont("consola.ttf", size);
SDL_Surface *pauseText = TTF_RenderText_Blended(font, text, color);
SDL_Texture* textT = SDL_CreateTextureFromSurface(renderer, pauseText);
SDL_FreeSurface(pauseText);
SDL_Rect rect;
SDL_QueryTexture(textT, NULL, NULL, &rect.w, &rect.h);
switch (center)
{
case 0: rect.x = x; rect.y = y; break; // Not centered, use x and y
case 1: rect.y = height / 2 - rect.h / 2; rect.x = width / 2 - rect.w / 2; break; // Centered in right in the middle of the screen
case 2: rect.x = width / 2 - rect.w / 2; rect.y = y; break; // Centered horizontally
case 3: rect.x = width / 5; rect.y = y; break;
case 4: rect.x = width - width / 5 - rect.w; rect.y = y; break;
case 5: rect.x = width / 2 - rect.w / 2 + x; rect.y = height / 2 - rect.h / 2 + y; break; // Offset from center of the screen
}
SDL_RenderCopy(renderer, textT, NULL, &rect);
SDL_DestroyTexture(textT);
TTF_CloseFont(font);
}
void darkenBackground(bool black)
{
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, black ? 255 : 225);
SDL_Rect background = {0, 0, width, height};
SDL_RenderFillRect(renderer, &background);
}
void getName(struct GameState *state)
{
bool done = false;
char nam[64] = "";
SDL_StartTextInput();
while (!done) {
SDL_Event event;
if (SDL_PollEvent(&event)) {
if (event.key.keysym.sym == SDLK_RETURN && event.type == SDL_KEYUP)
{
if(nam[0] == 0)
continue;
done = true;
}
if (event.key.keysym.sym == SDLK_BACKSPACE && event.type == SDL_KEYDOWN) nam[strlen(nam) - 1] = 0;
if (event.key.keysym.sym == SDLK_ESCAPE && event.type == SDL_KEYDOWN)
{
state->running = false;
return;
}
if (event.type == SDL_TEXTINPUT) strcat(nam, event.text.text);
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
renderText("Name:", 0, height / 2 - (width / 7), white, 2, width / 10);
renderText(nam, 0, 0, white, 1, width / 15);
SDL_RenderPresent(renderer);
}
SDL_StopTextInput();
strcpy(name, nam);
}
void getDifficulty(struct GameState *state) {
bool done = false;
while (!done) {
SDL_Event event;
if (SDL_PollEvent(&event)) {
if (event.key.keysym.sym == SDLK_RETURN && event.type == SDL_KEYUP)
{
done = true;
}
if (event.key.keysym.sym == SDLK_ESCAPE && event.type == SDL_KEYDOWN)
{
state->running = false;
return;
}
if (event.key.keysym.sym == SDLK_LEFT && event.type == SDL_KEYDOWN)
{
if (state->difficulty != 1)
state->difficulty--;
}
if (event.key.keysym.sym == SDLK_RIGHT && event.type == SDL_KEYDOWN)
{
if (state->difficulty != 3)
state->difficulty++;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
renderText("Difficulty:", 0, height / 2 - (width / 7), white, 2, width / 10);
renderText("Easy", -width / 4, 0, state->difficulty == 1 ? red : white, 5, width / 15);
renderText("Normal", 0, 0, state->difficulty == 2 ? red : white, 5, width / 15);
renderText("Hard", width / 4, 0, state->difficulty == 3 ? red : white, 5, width / 15);
renderText("A higher difficulty means more points", 0, height / 2 + (width / 7), white, 2, width / 30);
renderText("but also higher speed", 0, height / 2 + (width / 7) + (width / 30), white, 2, width / 30);
SDL_RenderPresent(renderer);
}
}
void handleReturn(struct GameState *state)
{
if (state->alive == 0 && state->pauseTimeout == 0)
{
switch (selected)
{
case 0:
state->pauseTimeout = 30;
state = resetGame(state);
getName(state);
getDifficulty(state);
state->alive = 1;
break;
case 1:
state->alive = -2;
break;
case 2:
loadHS();
state->alive = -1;
break;
case 3:
state->running = false;
break;
}
}else if (state->alive > 1){
if (state->pauseTimeout == 0) { state->alive = 0; state->pauseTimeout = 30; }
}else{
if (state->pauseTimeout == 0)
{
state->pause = (state->pause) ? false : true;
state->pauseTimeout = 60;
printf("Game (un/)paused!\n");
}
}
}
void renderMenu()
{
darkenBackground(true);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDrawRect(renderer, &kasten);
renderText("PACSNAKE", -1, fieldHeight, white, 2, height / 5);
renderText("New Game", -1, height / 2 - (height / 5), (selected == 0) ? red : white, 2, width / 15);
renderText("Symbols", -1, height / 2 - height / 15, (selected == 1) ? red : white, 2, width / 15);
renderText("Game History", -1, height / 2 + height / 15, selected == 2 ? red : white, 2, width / 15);
renderText("Exit", -1, height / 2 + (height / 5), (selected == 3) ? red : white, 2, width / 15);
}