2018-01-07 14:19:35 +01:00
# include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>
# include <math.h>
2018-01-09 13:22:10 +01:00
# include <time.h>
2018-01-07 14:19:35 +01:00
# include <SDL2/SDL.h>
2018-01-09 13:22:10 +01:00
# include <SDL2/SDL_image.h>
# include <SDL2/SDL_ttf.h>
2018-01-10 15:37:55 +01:00
# include "breakout.h"
2018-01-09 13:22:10 +01:00
# include "vector.h"
2018-01-15 13:19:51 +01:00
# include "startmenu.h"
2018-01-15 15:34:43 +01:00
# include "gamestate.h"
2018-01-15 23:47:39 +01:00
# include "highscores.h"
2018-01-07 14:19:35 +01:00
# ifndef __nullptr__
# define Nullptr(type) (type *)0
# endif // __nullptr__
2018-01-15 15:34:43 +01:00
# define ae "\204"
# define oe "\224"
# define ue "\201"
# define ss "\341"
2018-01-15 13:54:17 +01:00
2018-01-16 12:10:37 +01:00
// Prototypes
void GAME_ChangeState ( GameState state ) ;
2018-01-15 20:28:47 +01:00
void HandleSDLEvents ( ) ;
2018-01-07 14:19:35 +01:00
void mousePress ( SDL_MouseButtonEvent b ) ;
2018-01-07 14:45:07 +01:00
void keyPress ( SDL_KeyboardEvent b ) ;
2018-01-08 00:45:32 +01:00
void toggleFullscreen ( ) ;
2018-01-16 12:10:37 +01:00
void windowChanged ( SDL_WindowEvent b ) ;
void DrawBackground ( SDL_Renderer * renderer ) ;
void INITIALIZE ( ) ;
void QUIT ( ) ;
2018-01-15 15:34:43 +01:00
int readIntFromIO ( char * m1 , char * m2 , char * m3 , int min , int max ) ;
2018-01-16 12:10:37 +01:00
// End Prototypes
2018-01-07 14:19:35 +01:00
2018-01-16 12:10:37 +01:00
// Default Render Size (Upscaled for bigger monitors)
2018-01-15 13:54:17 +01:00
const int width = 1920 ;
const int height = 1080 ;
float XScale = 1.0f , YScale = 1.0f ;
2018-01-16 12:10:37 +01:00
// End render properties
2018-01-07 14:19:35 +01:00
2018-01-15 22:01:58 +01:00
int numKeys ;
const Uint8 * keystate ; // TODO: export all this into scenery and enemy waves
2018-01-07 14:19:35 +01:00
SDL_Window * window ;
SDL_Renderer * renderer ;
SDL_Event event ;
2018-01-08 00:45:32 +01:00
bool running = true , fullscreen = false ;
2018-01-15 15:34:43 +01:00
GameState gameState = Game ;
2018-01-15 22:01:58 +01:00
Scenery scenery ;
2018-01-10 15:37:55 +01:00
2018-01-07 14:19:35 +01:00
int main ( int argc , char * args [ ] ) {
2018-01-15 15:34:43 +01:00
printf ( " Spielbereiche \n \t - 1: Hauptmen " ue " \n \t - 2: Spiel \n \t - 3: Level Select \n \t - 4: Settings \n \t - 5: Highscores \n " ) ;
2018-01-16 12:10:37 +01:00
GAME_ChangeState ( readIntFromIO ( " W " ae " hle einen Spielbereich aus, den du testen m " oe " chtest: " , " Fehlerhafte Eingabe! \n " , " %d ist kein g " ue " ltiger Spielbereich! \n " , 1 , 5 ) ) ;
2018-01-07 14:19:35 +01:00
INITIALIZE ( ) ;
2018-01-10 15:37:55 +01:00
while ( running ) { // Gameloop
2018-01-15 20:28:47 +01:00
HandleSDLEvents ( ) ;
2018-01-15 13:54:17 +01:00
DrawBackground ( renderer ) ;
switch ( gameState ) {
case Game :
2018-01-15 22:01:58 +01:00
BREAKOUT_Update ( & scenery , keystate ) ;
BREAKOUT_Draw ( & scenery , renderer ) ;
2018-01-15 13:54:17 +01:00
break ;
case MainMenu :
2018-01-15 15:34:43 +01:00
// Startmenu_Update(keystate);
2018-01-15 13:54:17 +01:00
Startmenu_Draw ( renderer ) ;
break ;
2018-01-15 23:47:39 +01:00
case Highscores :
HIGHSCORES_Draw ( renderer ) ;
break ;
2018-01-15 13:54:17 +01:00
default :
printf ( " Unknow state was updated: %d \n " , gameState ) ;
2018-01-16 12:10:37 +01:00
break ;
2018-01-15 13:54:17 +01:00
}
2018-01-15 15:34:43 +01:00
// DrawText(renderer, "FICK DICH");
2018-01-15 13:54:17 +01:00
SDL_RenderPresent ( renderer ) ;
2018-01-07 14:19:35 +01:00
}
QUIT ( ) ;
return 0 ;
} /* main */
2018-01-16 12:10:37 +01:00
void GAME_ChangeState ( GameState state ) {
gameState = state ;
switch ( gameState ) {
case Highscores :
HIGHSCORES_ReloadList ( ) ;
printf ( " State was changed to Highscores! \n " ) ;
break ;
default :
printf ( " State was changed to %d! \n " , gameState ) ;
break ;
}
}
2018-01-15 20:28:47 +01:00
void HandleSDLEvents ( ) {
while ( SDL_PollEvent ( & event ) ) {
switch ( event . type ) {
case SDL_QUIT :
2018-01-15 23:47:39 +01:00
printf ( " NOTICE: User manually quit window! \n " ) ;
2018-01-15 20:28:47 +01:00
running = false ;
break ;
case SDL_KEYDOWN :
2018-01-18 11:45:19 +01:00
// if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = false;
// else
keyPress ( event . key ) ;
2018-01-15 20:28:47 +01:00
break ;
case SDL_MOUSEBUTTONDOWN :
mousePress ( event . button ) ;
break ;
case SDL_WINDOWEVENT :
windowChanged ( event . window ) ;
break ;
}
}
2018-01-18 11:45:19 +01:00
} /* HandleSDLEvents */
2018-01-15 20:28:47 +01:00
2018-01-07 14:19:35 +01:00
void mousePress ( SDL_MouseButtonEvent b ) { // Debug prop
if ( b . button = = SDL_BUTTON_LEFT ) {
2018-01-10 15:37:55 +01:00
printf ( " Left mouse pressed at %d, %d \n " , b . x , b . y ) ;
2018-01-07 14:19:35 +01:00
} else if ( b . button = = SDL_BUTTON_RIGHT ) {
printf ( " Right mouse pressed... \n " ) ;
} else {
printf ( " Unknown mouse button pressed: %d \n " , b . button ) ;
}
}
2018-01-07 14:45:07 +01:00
void keyPress ( SDL_KeyboardEvent b ) { // Debug prop
printf ( " Key pressed: ID is %d \n " , b . keysym . scancode ) ;
2018-01-11 18:45:00 +01:00
if ( b . keysym . scancode = = SDL_SCANCODE_F11 | | b . keysym . scancode = = SDL_SCANCODE_5 ) {
2018-01-08 00:45:32 +01:00
toggleFullscreen ( ) ;
}
}
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 ) ;
2018-01-15 13:54:17 +01:00
// 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 ) ;
2018-01-08 00:45:32 +01:00
break ;
}
2018-01-07 14:19:35 +01:00
}
2018-01-15 13:54:17 +01:00
void DrawBackground ( SDL_Renderer * renderer ) {
2018-01-10 15:37:55 +01:00
SDL_SetRenderDrawColor ( renderer , 0 , 0 , 0 , 255 ) ;
SDL_RenderClear ( renderer ) ;
2018-01-15 13:54:17 +01:00
} /* DrawFrame */
2018-01-14 13:47:05 +01:00
2018-01-07 14:19:35 +01:00
void INITIALIZE ( ) {
2018-01-15 22:01:58 +01:00
printf ( " Initializing started... \n " ) ;
2018-01-07 14:19:35 +01:00
srand ( time ( NULL ) ) ;
2018-01-14 13:47:05 +01:00
if ( SDL_Init ( SDL_INIT_EVERYTHING ) ! = 0 ) printf ( " SDL could not initialize! Error: %s \n " , SDL_GetError ( ) ) ;
2018-01-07 14:19:35 +01:00
else printf ( " SDL was successfully initialized! \n " ) ;
2018-01-15 22:01:58 +01:00
keystate = SDL_GetKeyboardState ( & numKeys ) ;
2018-01-14 13:47:05 +01:00
if ( IMG_Init ( IMG_INIT_PNG ) ! = IMG_INIT_PNG ) printf ( " IMG could not initialize! Error: %s \n " , IMG_GetError ( ) ) ;
2018-01-10 15:37:55 +01:00
else printf ( " IMG was successfully initialized! \n " ) ;
2018-01-14 13:47:05 +01:00
if ( TTF_Init ( ) = = - 1 ) printf ( " TTF could not initialize! Error: %s \n " , TTF_GetError ( ) ) ;
else printf ( " TTF was successfully initialized! \n " ) ;
2018-01-09 14:36:57 +01:00
window = SDL_CreateWindow ( " BreakING " , SDL_WINDOWPOS_UNDEFINED , SDL_WINDOWPOS_UNDEFINED , width , height , SDL_WINDOW_OPENGL ) ;
2018-01-08 00:45:32 +01:00
SDL_SetWindowResizable ( window , true ) ;
2018-01-07 14:19:35 +01:00
printf ( " Window was created! \n " ) ;
renderer = SDL_CreateRenderer ( window , - 1 , SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC ) ;
printf ( " Renderer was created! \n " ) ;
2018-01-10 23:56:47 +01:00
BREAKOUT_INITIALIZE ( renderer , width , height ) ;
2018-01-15 22:01:58 +01:00
scenery = BREAKOUT_CreateDefault ( ) ;
2018-01-15 13:19:51 +01:00
Load_Textures ( renderer ) ;
2018-01-15 23:47:39 +01:00
HIGHSCORES_Initialize ( ) ;
2018-01-15 22:01:58 +01:00
printf ( " Initializing finished! \n " ) ;
2018-01-07 14:19:35 +01:00
} /* INITIALIZE */
void QUIT ( ) {
printf ( " De-initializing started... \n " ) ;
2018-01-15 23:47:39 +01:00
HIGHSCORES_Deinitialize ( ) ;
2018-01-15 22:01:58 +01:00
BREAKOUT_DestroyObject ( & scenery ) ;
2018-01-10 23:56:47 +01:00
BREAKOUT_DEINITIALIZE ( ) ;
2018-01-10 15:37:55 +01:00
TTF_Quit ( ) ;
IMG_Quit ( ) ;
2018-01-07 14:19:35 +01:00
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 */
2018-01-15 15:34:43 +01:00
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 */