-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
223 lines (199 loc) · 6.56 KB
/
main.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
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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include "include/render.h"
#include "include/input.h"
#include "include/cgame.h"
#include <SDL2/SDL_mixer.h>
#define FALSE 0
#define TRUE 1
Mix_Music* music = NULL; // Global music object
// VIDEO
AVFormatContext* formatContext = NULL;
AVCodecContext* codecContext = NULL;
AVFrame* frame = NULL;
AVPacket* packet = NULL;
struct SwsContext* swsContext = NULL;
SDL_Texture* videoTexture = NULL;
int videoStreamIndex = -1;
int game_is_running;
GameState currentState = WELCOME_PAGE;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
TTF_Font* smallFont = NULL;
TTF_Font* largeFont = NULL;
SDL_Texture* textTexture = NULL;
SDL_Texture* startTextTexture = NULL;
int mouseX, mouseY;
int selectedButton = -1;
bool isHovered;
char *ply_name = NULL; // Initialize ply_name
HighScoreBoard highScoreBoard = { .count = 0 };
Game game; // Declare the game variable
void create_file_if_not_exists(const char* filename) {
FILE* file = fopen(filename, "a"); // Open in append mode to create the file if it doesn't exist
if (!file) {
printf("Error creating file: %s\n", strerror(errno));
return;
}
fclose(file);
printf("File %s created or already exists\n", filename);
}
void load_high_scores(const char* filename) {
FILE* file = fopen(filename, "r");
if (!file) {
printf("Error opening file for reading: %s\n", strerror(errno));
return;
}
highScoreBoard.count = 0;
while (highScoreBoard.count < MAX_HIGH_SCORES) {
if (fscanf(file, "%31s %d %d %d",
highScoreBoard.highScores[highScoreBoard.count].name,
&highScoreBoard.highScores[highScoreBoard.count].score,
&highScoreBoard.highScores[highScoreBoard.count].time.mint,
&highScoreBoard.highScores[highScoreBoard.count].time.scnd) == 4) {
highScoreBoard.count++;
} else {
break;
}
}
fclose(file);
printf("High scores loaded from %s\n", filename);
}
void replace_high_score(const char* name, Game* game, int index, const char* filename) {
if (index < 0 || index >= MAX_HIGH_SCORES) {
printf("Error: Invalid index %d for highScoreBoard\n", index);
return;
}
// Read all high scores into memory
HighScoreBoard tempBoard = { .count = 0 };
FILE* file = fopen(filename, "r");
if (!file) {
printf("Error opening file for reading: %s\n", strerror(errno));
return;
}
while (tempBoard.count < MAX_HIGH_SCORES) {
if (fscanf(file, "%31s %d %d %d",
tempBoard.highScores[tempBoard.count].name,
&tempBoard.highScores[tempBoard.count].score,
&tempBoard.highScores[tempBoard.count].time.mint,
&tempBoard.highScores[tempBoard.count].time.scnd) == 4) {
tempBoard.count++;
} else {
break;
}
}
fclose(file);
// Replace the score at the specified index
strcpy(tempBoard.highScores[index].name, name);
tempBoard.highScores[index].score = game->score;
tempBoard.highScores[index].time = game->time;
// Write all high scores back to the file
file = fopen(filename, "w");
if (!file) {
printf("Error opening file for writing: %s\n", strerror(errno));
return;
}
for (int i = 0; i < tempBoard.count; i++) {
fprintf(file, "%s %d %d %d\n",
tempBoard.highScores[i].name,
tempBoard.highScores[i].score,
tempBoard.highScores[i].time.mint,
tempBoard.highScores[i].time.scnd);
}
fclose(file);
printf("High score replaced at index %d\n", index);
}
void cleanup() {
if (videoTexture) SDL_DestroyTexture(videoTexture);
if (swsContext) sws_freeContext(swsContext);
if (frame) av_frame_free(&frame);
if (packet) av_packet_free(&packet);
if (codecContext) avcodec_free_context(&codecContext);
if (formatContext) avformat_close_input(&formatContext);
if (startTextTexture) {
SDL_DestroyTexture(startTextTexture);
startTextTexture = NULL;
}
if (textTexture) {
SDL_DestroyTexture(textTexture);
textTexture = NULL;
}
if (largeFont) {
TTF_CloseFont(largeFont);
largeFont = NULL;
}
if (smallFont) {
TTF_CloseFont(smallFont);
smallFont = NULL;
}
if (renderer) {
SDL_DestroyRenderer(renderer);
renderer = NULL;
}
if (window) {
SDL_DestroyWindow(window);
window = NULL;
}
TTF_Quit();
SDL_Quit();
}
void play_music(const char* filepath) {
// Load the music file
Mix_Music* music = Mix_LoadMUS(filepath);
if (!music) {
printf("Failed to load music! SDL_mixer Error: %s\n", Mix_GetError());
return;
}
// Play the music
if (Mix_PlayMusic(music, -1) == -1) { // -1 means loop indefinitely
printf("Failed to play music! SDL_mixer Error: %s\n", Mix_GetError());
}
Mix_VolumeMusic(10);
}
void cleanup_music() {
Mix_HaltMusic(); // Stop any playing music
// Free the music file
Mix_CloseAudio(); // Close SDL_mixer
}
int main(int argc, char* argv[]) {
create_file_if_not_exists("score.txt");
load_high_scores("score.txt"); // Load high scores at startup
// Rest of your initialization code...
game_is_running = initialize_window();
if (!game_is_running) {
cleanup(); // Ensure cleanup is called if initialization fails
return -1;
}
const char* videoPath = "assets/cyberpunk.mp4";
if (!loadVideo(videoPath, renderer)) {
printf("Failed to load video\n");
cleanup();
return -1;
}
// Main loop
while (game_is_running) {
process_input(&game); // Pass the game variable
render(&game); // Pass the game variable
if (currentState == WELCOME_PAGE){
update_balls();
} else if (currentState == GAME_PAGE) {
update(&game, 0.016); // Update the game state
} else if (currentState == PLAYERVSMACHINE_PAGE) {
PlayerVSMachine(&game, 0.016); // Update the game state for Player vs Machine
update_balls();
} else if (currentState == MACHINE_PAGE){
Machine(&game, 0.016);
update_balls();
}
SDL_Delay(16); // Delay to limit CPU usage (~60 frames per second)
}
// Clean up resources
cleanup();
return 0;
}