breakout/main.c
2018-01-27 18:48:45 +01:00

262 lines
8.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.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_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;
GameState gameState = MainMenu;
Scenery scenery;
Mix_Music * MenuLoop;
int main(int argc, char * args[]){
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 */
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;
}
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 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);
button_clicked(event.button, gameState);
break;
case SDL_WINDOWEVENT:
windowChanged(event.window);
break;
}
}
} /* HandleSDLEvents */
void mousePress(SDL_MouseButtonEvent b){ // Debug prop
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);
}
}
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 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);
scenery = BREAKOUT_CreateDefault();
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");
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 */