-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.c
More file actions
335 lines (288 loc) · 10.4 KB
/
Copy pathmenu.c
File metadata and controls
335 lines (288 loc) · 10.4 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// 1) Install dependencies: sudo apt install build-essential libsdl2-dev libsdl2-image-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-gfx-dev libzip-dev
// 2) Compilation: gcc -o game menu.c -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_gfx -lSDL2_mixer
// 3) Run: ./game
// menu.c
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 720
#define NUM_SQUARES 20
typedef struct {
int x, y;
int size;
SDL_Color color;
} Square;
typedef struct {
SDL_Rect rect;
const char* label;
bool hover;
bool visible;
} Button;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
TTF_Font *titleFont = NULL, *menuFont = NULL, *textFont = NULL;
SDL_Texture* mascotTexture = NULL;
Mix_Chunk* hoverSound = NULL;
Mix_Chunk* clickSound = NULL;
Mix_Music* themeMusic = NULL;
Square squares[NUM_SQUARES];
Button buttons[6];
char helpText[8192] = {0};
bool showMenu = false, showHelp = false, reverseMenu = false, showmascotImage = false;
int buttonX = -300, helpTextX = -400;
int mascotY = SCREEN_HEIGHT;
int introStep = 0;
float introAlpha = 0.0f;
const char* introTexts[] = {
"Cooper Black presents...",
"a game written in C",
"Mistrust"
};
int titleY = 300;
bool subtitleVisible = false;
void drawText(const char* text, TTF_Font* font, SDL_Color color, int x, int y, float alpha) {
SDL_Surface* surface = TTF_RenderUTF8_Blended(font, text, color);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_SetTextureAlphaMod(texture, (Uint8)(alpha * 255));
SDL_Rect dst = { x, y, surface->w, surface->h };
SDL_RenderCopy(renderer, texture, NULL, &dst);
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
}
void drawIntro() {
if (introStep >= 3) return;
const char* msg = introTexts[introStep];
SDL_Color white = {255, 255, 255, 255};
int textW = 0, textH = 0;
TTF_SizeUTF8(menuFont, msg, &textW, &textH);
drawText(msg, introStep == 2 ? titleFont : menuFont, white,
(SCREEN_WIDTH - textW) / 2, SCREEN_HEIGHT / 2 - textH / 2, introAlpha);
if (introAlpha < 1.0f) {
introAlpha += 0.01f;
} else {
introAlpha = 0.0f;
introStep++;
if (introStep >= 3) {
showMenu = true;
showmascotImage = true;
}
}
}
void drawSquares() {
for (int i = 0; i < NUM_SQUARES; ++i) {
squares[i].x -= 2;
if (squares[i].x < -squares[i].size) {
squares[i].x = SCREEN_WIDTH;
squares[i].y = rand() % SCREEN_HEIGHT;
}
boxRGBA(renderer, squares[i].x, squares[i].y,
squares[i].x + squares[i].size,
squares[i].y + squares[i].size,
squares[i].color.r,
squares[i].color.g,
squares[i].color.b,
255);
}
}
void drawButtons() {
SDL_Color white = {255, 255, 255, 255};
for (int i = 0; i < 6; ++i) {
if (!buttons[i].visible) continue;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderFillRect(renderer, &buttons[i].rect);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDrawRect(renderer, &buttons[i].rect);
drawText(buttons[i].label, menuFont,
buttons[i].hover ? (SDL_Color){255, 255, 0, 255} : white,
buttons[i].rect.x + 10, buttons[i].rect.y + 5, 1.0f);
}
}
void updateButtonPositions(int x) {
for (int i = 0; i < 5; ++i) {
buttons[i].rect.x = x;
buttons[i].visible = !reverseMenu;
}
}
void drawHelpText() {
SDL_Color white = {255, 255, 255, 255};
int y = 200;
// Use a temporary buffer to avoid modifying helpText
char tempBuffer[sizeof(helpText)];
strncpy(tempBuffer, helpText, sizeof(tempBuffer) - 1);
tempBuffer[sizeof(tempBuffer) - 1] = '\0';
char* line = strtok(tempBuffer, "\n");
while (line) {
drawText(line, textFont, white, helpTextX, y, 1.0f);
y += 30; // Adjust spacing as needed
line = strtok(NULL, "\n");
}
}
void loadHelpContent() {
FILE* file = fopen("Assets/help.txt", "r");
if (file) {
size_t bytesRead = fread(helpText, sizeof(char), sizeof(helpText) - 1, file);
helpText[bytesRead] = '\0'; // null-terminate safely
fclose(file);
} else {
snprintf(helpText, sizeof(helpText), "Failed to load help.txt");
}
}
void playSound(Mix_Chunk* sound) {
if (sound) Mix_PlayChannel(-1, sound, 0);
}
void initButtons() {
const char* labels[] = {"Play", "Load Memory", "Settings", "Help", "Exit", "Back"};
for (int i = 0; i < 6; ++i) {
buttons[i].label = labels[i];
buttons[i].hover = false;
buttons[i].visible = (i < 5);
buttons[i].rect = (SDL_Rect){buttonX, 360 + i * 60, 250, 40};
}
buttons[5].visible = false; // Back button
}
void drawmascot() {
if (mascotTexture && showmascotImage) {
int w, h;
SDL_QueryTexture(mascotTexture, NULL, NULL, &w, &h);
float scale = (float)SCREEN_HEIGHT / h;
int newW = (int)(w * scale);
SDL_Rect dst = {SCREEN_WIDTH - newW, mascotY, newW, SCREEN_HEIGHT};
SDL_RenderCopy(renderer, mascotTexture, NULL, &dst);
if (mascotY > 0) mascotY -= 5;
}
}
bool directoryExists(const char* path) {
struct stat st;
return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
}
void extractAssetsIfNeeded() {
if (!directoryExists("Assets")) {
printf("Extracting assets.o...\n");
system("unzip -o assets.o"); // requires unzip command-line tool
}
}
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
TTF_Init();
IMG_Init(IMG_INIT_PNG);
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
extractAssetsIfNeeded();
window = SDL_CreateWindow("Mistrust (Game)",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN); // show window decorations
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Surface* cursorSurface = IMG_Load("Assets/cursor.png");
if (cursorSurface) {
SDL_Cursor* cursor = SDL_CreateColorCursor(cursorSurface, 0, 0); // hotspot at (0, 0)
SDL_SetCursor(cursor);
SDL_ShowCursor(SDL_ENABLE); // make sure cursor is visible
SDL_FreeSurface(cursorSurface); // no longer needed
} else {
printf("Failed to load cursor image: %s\n", IMG_GetError());
}
titleFont = TTF_OpenFont("Assets/eternal.ttf", 50);
menuFont = TTF_OpenFont("Assets/LeningradDisco.ttf", 24);
textFont = TTF_OpenFont("Assets/Gugi.ttf", 20);
mascotTexture = IMG_LoadTexture(renderer, "Assets/mascot.png");
themeMusic = Mix_LoadMUS("Assets/theme.wav");
hoverSound = Mix_LoadWAV("Assets/hover.wav");
clickSound = Mix_LoadWAV("Assets/click.wav");
if (themeMusic) Mix_PlayMusic(themeMusic, -1);
initButtons();
for (int i = 0; i < NUM_SQUARES; ++i) {
squares[i].x = SCREEN_WIDTH + i * 64;
squares[i].y = rand() % SCREEN_HEIGHT;
squares[i].size = 64;
squares[i].color = (SDL_Color){0,0,rand() % 256, 255};
}
bool running = true;
SDL_Event e;
Uint32 last = SDL_GetTicks();
while (running) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT)
running = false;
if (e.type == SDL_MOUSEMOTION) {
for (int i = 0; i < 6; ++i) {
if (!buttons[i].visible) continue;
SDL_Point p = {e.motion.x, e.motion.y};
bool hovered = SDL_PointInRect(&p, &buttons[i].rect);
if (hovered && !buttons[i].hover) playSound(hoverSound);
buttons[i].hover = hovered;
}
}
if (e.type == SDL_MOUSEBUTTONDOWN) {
for (int i = 0; i < 6; ++i) {
if (buttons[i].hover && buttons[i].visible) {
playSound(clickSound);
if (i == 3) { // Help
reverseMenu = true;
loadHelpContent();
showHelp = true;
buttons[5].visible = true;
} else if (i == 4) { // Exit
running = false;
} else if (i == 5) { // Back
showHelp = false;
reverseMenu = false;
buttonX = -300;
helpTextX = -400;
updateButtonPositions(buttonX);
buttons[5].visible = false;
loadHelpContent();
}
}
}
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Clear screen to black (the true background)
SDL_RenderClear(renderer);
if (showMenu) drawSquares(); // Only draw background after intro
if (introStep < 3) {
drawIntro();
} else {
if (titleY > 20) titleY -= 2;
drawText("Mistrust", titleFont,
(SDL_Color){255, 255, 255, 255}, 50, titleY, 1.0f);
if (titleY <= 20) {
subtitleVisible = true;
drawText("version 0.1 2025-06-02 early alpha", textFont,
(SDL_Color){255, 255, 255, 255}, 50, 80, 1.0f);
}
if (buttonX < 50 && !reverseMenu) {
buttonX += 2;
updateButtonPositions(buttonX);
} else if (reverseMenu && buttonX > -300) {
buttonX -= 5;
updateButtonPositions(buttonX);
}
drawButtons();
if (showHelp && helpTextX < 50) helpTextX += 5;
if (showHelp) {
drawHelpText();
buttons[5].rect.x = 100;
buttons[5].rect.y = 650;
}
}
drawmascot();
SDL_RenderPresent(renderer);
Uint32 now = SDL_GetTicks();
Uint32 delta = now - last;
if (delta < 30) SDL_Delay(30 - delta);
last = now;
}
Mix_Quit();
IMG_Quit();
TTF_Quit();
SDL_Quit();
return 0;
}