420 lines
13 KiB
C
420 lines
13 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <math.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <SDL2/SDL.h>
|
|
#include <SDL2/SDL_image.h>
|
|
#include <SDL2/SDL_ttf.h>
|
|
#include <SDL2/SDL_mixer.h>
|
|
|
|
#include "breakout.h"
|
|
#include "vector.h"
|
|
#include "startmenu.h"
|
|
#include "gamestate.h"
|
|
#include "highscores.h"
|
|
#include "gameover.h"
|
|
#include "settings.h"
|
|
#include "background.h"
|
|
|
|
#define MAIN_MenuMusicPath "assets/sounds/menu_music.wav"
|
|
#define MAIN_AccountSaveFilePath "account.cfg"
|
|
#define MAIN_FadeTime 1000
|
|
|
|
#include "main.h"
|
|
|
|
// Default Render Size (Upscaled for bigger monitors)
|
|
const int width = 1920;
|
|
const int height = 1080;
|
|
float XScale = 1.0f, YScale = 1.0f;
|
|
// End render properties
|
|
|
|
int numKeys;
|
|
const Uint8 * keystate; // TODO: export all this into scenery and enemy waves
|
|
SDL_Window * window;
|
|
SDL_Renderer * renderer;
|
|
SDL_Event event;
|
|
bool running = true, fullscreen = false, LoggedIn = false;
|
|
GameState gameState = MainMenu;
|
|
GameState previousGameState = MainMenu;
|
|
Scenery scenery;
|
|
Mix_Music * MenuLoop;
|
|
char * Username;
|
|
char * Password;
|
|
|
|
int main(int argc, char * args[]){
|
|
AttemptLogin();
|
|
INITIALIZE();
|
|
Uint32 fps_lasttime = SDL_GetTicks(); // the last recorded time.
|
|
Uint32 fps_current; // the current FPS.
|
|
Uint32 fps_frames = 0; // frames passed since the last recorded fps.
|
|
|
|
GAME_ChangeState(MainMenu);
|
|
while (running) { // Gameloop
|
|
HandleSDLEvents();
|
|
DrawBackground(renderer);
|
|
switch (gameState) {
|
|
case Game:
|
|
BREAKOUT_Update(&scenery, keystate);
|
|
BACKGROUND_Draw(renderer, 0);
|
|
BREAKOUT_Draw(&scenery, renderer);
|
|
break;
|
|
case MainMenu:
|
|
// Startmenu_Update(keystate);
|
|
Startmenu_Draw(renderer);
|
|
break;
|
|
case Highscores:
|
|
HIGHSCORES_Draw(renderer);
|
|
break;
|
|
case Settings:
|
|
Settings_Draw(renderer, &scenery);
|
|
break;
|
|
case GameOver:
|
|
GAMEOVER_Draw(renderer, &scenery);
|
|
break;
|
|
default:
|
|
printf("Unknow state was updated: %d\n", gameState);
|
|
break;
|
|
} /* switch */
|
|
SDL_RenderPresent(renderer);
|
|
fps_frames++;
|
|
if (fps_lasttime < SDL_GetTicks() - 1000) {
|
|
fps_lasttime = SDL_GetTicks();
|
|
fps_current = fps_frames;
|
|
fps_frames = 0;
|
|
printf("Frames/s: %u\n", fps_current);
|
|
}
|
|
}
|
|
QUIT();
|
|
return 0;
|
|
} /* main */
|
|
|
|
bool PushNewCredentialsToSaveFile(const char * filename){
|
|
FILE * file;
|
|
|
|
if (file = fopen(filename, "w")) {
|
|
fprintf(file, "%s\n%s", Username, Password);
|
|
fclose(file);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool GrabAccountFromSaveFile(const char * filename){
|
|
FILE * file;
|
|
|
|
if (file = fopen(filename, "r")) {
|
|
bool success = true;
|
|
success = (fscanf(file, "%s\n%s", Username, Password) > 0);
|
|
if (success) printf("Account save file found \"%s\"!\nAttempting automatic login for user \"%s\"...\n", MAIN_AccountSaveFilePath, Username);
|
|
else printf("Account save file \"%s\" is invalid!\n", MAIN_AccountSaveFilePath);
|
|
fclose(file);
|
|
return success;
|
|
}
|
|
return false;
|
|
} /* GrabAccountFromSaveFile */
|
|
|
|
void GAME_ReadCredentials(){
|
|
printf("Input your username: ");
|
|
gets(Username);
|
|
printf("Input your password: ");
|
|
gets(Password);
|
|
}
|
|
|
|
bool GAME_Login(){
|
|
printf("Login:\n");
|
|
bool loginSuccess = HIGHSCORES_Login(Username, Password);
|
|
if (loginSuccess) {
|
|
printf("Successfully logged in as %s!\n", Username);
|
|
LoggedIn = true;
|
|
} else
|
|
printf("Login failed!\n");
|
|
return loginSuccess;
|
|
}
|
|
|
|
bool GAME_Register(){
|
|
printf("Register:\n");
|
|
bool loginSuccess = HIGHSCORES_Register(Username, Password);
|
|
if (loginSuccess) {
|
|
printf("Successfully registered new account: %s!\n", Username);
|
|
LoggedIn = true;
|
|
} else
|
|
printf("Registration failed!\n");
|
|
return loginSuccess;
|
|
}
|
|
|
|
void AttemptLogin(){
|
|
Username = calloc(50, sizeof(char));
|
|
Password = calloc(50, sizeof(char));
|
|
int state;
|
|
bool loginSuccess = false;
|
|
|
|
if (GrabAccountFromSaveFile(MAIN_AccountSaveFilePath)) {
|
|
if (GAME_Login()) {
|
|
printf("Automatic login succeded!\n");
|
|
return;
|
|
} else {
|
|
printf("Automatic login failed! Try manually!\n");
|
|
system("pause");
|
|
}
|
|
}
|
|
while (!loginSuccess) {
|
|
system("cls");
|
|
printf("If you want to upload your score to the scoreboard you need to login! Enter\n\t- 1 for logging in with an existing account\n\t- 2 for creating a new account\n\t- 3 for playing unranked\n");
|
|
state = readIntFromIO("Input>", "Invalid input! Awaited a number from 1 to 3.\n", "%d is not a valid mode!\n", 1, 3);
|
|
switch (state) {
|
|
case 1:
|
|
GAME_ReadCredentials();
|
|
loginSuccess = GAME_Login();
|
|
break;
|
|
case 2:
|
|
GAME_ReadCredentials();
|
|
loginSuccess = GAME_Register();
|
|
break;
|
|
case 3:
|
|
printf("Skipping login!");
|
|
LoggedIn = false;
|
|
loginSuccess = true;
|
|
break;
|
|
default:
|
|
printf("This should not happen! State is %d...\n", state);
|
|
LoggedIn = false;
|
|
loginSuccess = false;
|
|
break;
|
|
} /* switch */
|
|
if (!loginSuccess) system("pause");
|
|
}
|
|
if (PushNewCredentialsToSaveFile(MAIN_AccountSaveFilePath)) printf("New login credentials were automatically saved!\n");
|
|
else printf("Login credentials could not be autosaved!\n");
|
|
} /* AttemptLogin */
|
|
|
|
int readIntFromIO(char * m1, char * m2, char * m3, int min, int max){
|
|
int nitems, num;
|
|
|
|
while (1) {
|
|
while (1) {
|
|
printf(m1);
|
|
nitems = scanf("%d", &num);
|
|
if (nitems == 0) {
|
|
printf(m2);
|
|
fflush(stdin);
|
|
continue;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
if ((num < min) || (num > max)) {
|
|
printf(m3, num);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
fflush(stdin);
|
|
return(num);
|
|
} /* readIntFromIO */
|
|
|
|
void GAME_Escape(){
|
|
running = false;
|
|
printf("GAME_Escape was called!\n");
|
|
}
|
|
|
|
void MENU_StartMusic(){
|
|
printf("Attempting to start menu music...\n");
|
|
if (!Mix_PlayingMusic())
|
|
Mix_FadeInMusic(MenuLoop, -1, MAIN_FadeTime);
|
|
else printf("Menu music is already playing!\n");
|
|
}
|
|
|
|
void MENU_PauseMusic(){
|
|
printf("Attempting to pause menu music...\n");
|
|
if (Mix_PlayingMusic())
|
|
Mix_HaltMusic();
|
|
// Mix_FadeOutMusic(MAIN_FadeTime);
|
|
else printf("There is no menu music to be paused!\n");
|
|
}
|
|
|
|
void GAME_ChangeState(GameState state){
|
|
if (state == Game) {
|
|
printf("Game music starting!\n");
|
|
MENU_PauseMusic();
|
|
BREAKOUT_StartMusic();
|
|
} else {
|
|
printf("Menu music starting!\n");
|
|
BREAKOUT_PauseMusic();
|
|
MENU_StartMusic();
|
|
}
|
|
if (gameState == state) {
|
|
printf("State wasn't changed!\n");
|
|
return;
|
|
}
|
|
previousGameState = gameState;
|
|
gameState = state;
|
|
switch (gameState) {
|
|
case Game:
|
|
BALL_ResetPosition(&(scenery.ball));
|
|
break;
|
|
case Highscores:
|
|
HIGHSCORES_ReloadList();
|
|
HIGHSCORES_GenerateTexture(renderer);
|
|
printf("State was changed to Highscores!\n");
|
|
break;
|
|
default:
|
|
printf("State was changed to %d!\n", gameState);
|
|
break;
|
|
}
|
|
} /* GAME_ChangeState */
|
|
void GAME_ReturnToLastScreen(){
|
|
if (previousGameState == gameState) {
|
|
printf("Cannot \"return\" to the same screen!\n");
|
|
} else {
|
|
GAME_ChangeState(previousGameState);
|
|
}
|
|
}
|
|
|
|
void HandleSDLEvents(){
|
|
while (SDL_PollEvent(&event)) {
|
|
switch (event.type) {
|
|
case SDL_QUIT:
|
|
printf("NOTICE: User manually quit window!\n");
|
|
running = false;
|
|
break;
|
|
case SDL_KEYDOWN:
|
|
// if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = false;
|
|
// else
|
|
keyPress(event.key);
|
|
break;
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
mousePress(event.button);
|
|
break;
|
|
case SDL_WINDOWEVENT:
|
|
windowChanged(event.window);
|
|
break;
|
|
}
|
|
}
|
|
} /* HandleSDLEvents */
|
|
|
|
void mousePress(SDL_MouseButtonEvent b){ // Debug prop
|
|
switch (gameState) {
|
|
case GameOver:
|
|
GAMEOVER_MouseClicked(b, &scenery);
|
|
break;
|
|
case MainMenu:
|
|
STARTMENU_ButtonClicked(event.button, gameState);
|
|
break;
|
|
case Highscores:
|
|
HIGHSCORES_MouseClicked(b);
|
|
break;
|
|
default:
|
|
printf("Gamestate currently ignores Mouse press event: %d!\n", gameState);
|
|
break;
|
|
}
|
|
if (b.button == SDL_BUTTON_LEFT) {
|
|
printf("Left mouse pressed at %d, %d\n", b.x, b.y);
|
|
} else if (b.button == SDL_BUTTON_RIGHT) {
|
|
printf("Right mouse pressed...\n");
|
|
} else {
|
|
printf("Unknown mouse button pressed: %d\n", b.button);
|
|
}
|
|
} /* mousePress */
|
|
|
|
void keyPress(SDL_KeyboardEvent b){ // Debug prop
|
|
printf("Key pressed: ID is %d\n", b.keysym.scancode);
|
|
if (b.keysym.scancode == SDL_SCANCODE_F11 || b.keysym.scancode == SDL_SCANCODE_5) {
|
|
toggleFullscreen();
|
|
} else {
|
|
switch (gameState) {
|
|
case Game:
|
|
BREAKOUT_KeyPressed(&scenery, &b);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void toggleFullscreen(){
|
|
if (fullscreen) {
|
|
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
|
|
} else {
|
|
SDL_SetWindowFullscreen(window, 0);
|
|
}
|
|
fullscreen = !fullscreen;
|
|
}
|
|
|
|
void windowChanged(SDL_WindowEvent b){ // Debug prop
|
|
switch (b.event) {
|
|
case SDL_WINDOWEVENT_SIZE_CHANGED:
|
|
printf("Window was resized to (%d|%d)!\n", event.window.data1, event.window.data2);
|
|
// BREAKOUT_ChangeSize(event.window.data1, event.window.data2);
|
|
XScale = ((double)(event.window.data1) / (double)width);
|
|
YScale = ((double)(event.window.data2) / (double)height);
|
|
SDL_RenderSetScale(renderer, XScale, YScale);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void DrawBackground(SDL_Renderer * renderer){
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|
SDL_RenderClear(renderer);
|
|
} /* DrawFrame */
|
|
|
|
void GAME_Restart(){
|
|
printf("Starting new game!\n");
|
|
scenery = BREAKOUT_CreateDefault();
|
|
}
|
|
|
|
void INITIALIZE(){
|
|
printf("Initializing started...\n");
|
|
srand(time(NULL));
|
|
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) printf("SDL could not initialize! Error: %s\n", SDL_GetError());
|
|
else printf("SDL was successfully initialized!\n");
|
|
keystate = SDL_GetKeyboardState(&numKeys);
|
|
if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) printf("IMG could not initialize! Error: %s\n", IMG_GetError());
|
|
else printf("IMG was successfully initialized!\n");
|
|
if (TTF_Init() == -1) printf("TTF could not initialize! Error: %s\n", TTF_GetError());
|
|
else printf("TTF was successfully initialized!\n");
|
|
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) printf("Mixer could not initialize! Error %s\n", Mix_GetError());
|
|
else printf("Mixer was successfully initialized!\n");
|
|
|
|
window = SDL_CreateWindow("BreakING", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);
|
|
SDL_SetWindowResizable(window, true);
|
|
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
|
|
printf("Window was created!\n");
|
|
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
|
printf("Renderer was created!\n");
|
|
BREAKOUT_INITIALIZE(renderer);
|
|
GAME_Restart();
|
|
Load_Textures(renderer);
|
|
HIGHSCORES_Initialize();
|
|
BACKGROUND_Initialize(renderer, width, height);
|
|
Settings_Initialize(renderer, &scenery);
|
|
GAMEOVER_Initialize(renderer);
|
|
MenuLoop = Mix_LoadMUS(MAIN_MenuMusicPath);
|
|
printf("Initializing finished!\n");
|
|
} /* INITIALIZE */
|
|
|
|
void QUIT(){
|
|
printf("De-initializing started...\n");
|
|
free(Username);
|
|
free(Password);
|
|
Mix_FreeMusic(MenuLoop);
|
|
GAMEOVER_Deinitialize();
|
|
BACKGROUND_Deinitialize();
|
|
Settings_Deinitialize();
|
|
HIGHSCORES_Deinitialize();
|
|
BREAKOUT_DestroyObject(&scenery);
|
|
BREAKOUT_DEINITIALIZE();
|
|
Mix_CloseAudio();
|
|
TTF_Quit();
|
|
IMG_Quit();
|
|
printf("Quitting SDL_IMG finished!\n");
|
|
SDL_DestroyRenderer(renderer);
|
|
printf("De-initializing renderer finished!\n");
|
|
SDL_DestroyWindow(window);
|
|
printf("De-initializing window finished!\n");
|
|
SDL_Quit();
|
|
printf("Quitting SDL finished!\n");
|
|
printf("De-initializing finished!\n");
|
|
} /* QUIT */
|