From 5d5bc3e446ee50601f98ca30d4f21172d6e75e29 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Sat, 22 Sep 2018 20:01:40 +0200 Subject: Initial commit --- main.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..5561051 --- /dev/null +++ b/main.c @@ -0,0 +1,117 @@ +// Standard libraries ( not written by me ) +#include +#include + +// SDL libraries ( not written by me ) +#include "SDL2/SDL.h" +#include "SDL2/SDL_image.h" + +#ifdef __WIN32__ + // Work around a TCC<->SDL bug regarding the redefintion of main. + // Note: the error messages from TCC make no sense + #undef main +#endif + + +// Window dimensions +#define SCREEN_WIDTH 640 +#define SCREEN_HEIGHT 480 +#define TILE_SIZE 32 // Width and height of 'squares' in the game + +#define SCREEN_WIDTH_SQUARES SCREEN_WIDTH / TILE_SIZE +#define SCREEN_HEIGHT_SQUARES SCREEN_HEIGHT / TILE_SIZE + +// Modules of the game I have written ( in directory with this source file ) +#include "level.c" // Level structures, loading, other levely stuff +#include "helpscreen.c" // Help-screen code +#include "echidna_ai.c" // Echidna movement A +#include "render.c" // Normal in-game rendering +#include "game.c" // Normal in-game code + +/* error_sdl is called when SDL 'throws' an error + * SDL can throw an error if the computer is for example incapable of displaying + * the game, because it is too old ( ie made before the year 1998 ) */ +void error_sdl( char* reason ) +{ + // Print the error message and then close the game + fprintf( stderr, "Error (SDL): %s: %s\n", reason, SDL_GetError() ); + exit(1); +} + +int main(int argc, char *argv[]) +{ + /// Initialisation + + /* First we need to tell SDL to create a window. A 'surface' is created called surf_screen, + * 'surfaces' are simply images of what we see on-screen. + * + * To make things appear on the screen, I copy ('blit') images onto this surface at different locations + */ + + // Initialise SDL + printf("Initialising SDL... "); + if ( SDL_Init( SDL_INIT_VIDEO ) == -1 ) error_sdl("Could not initialise SDL"); + atexit(SDL_Quit); + + // Set surf_screen + SDL_Surface* surf_screen = NULL; + SDL_Window * sdlWindow = SDL_CreateWindow("EchidneaMenace", SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL ); + if ( surf_screen == NULL ) error_sdl("Could not set videomode"); + SDL_Renderer *renderer = SDL_CreateRenderer(sdlWindow, -1, 0); + + printf("done\n"); + + + /// Menu + printf(" ==== Entered menu\n"); + + // Events are variables used to store user input + SDL_Event menuevent; + // Load the menu graphic into memory + SDL_Surface *surf_title = IMG_Load( "titlescreen.png" ); + + + /* We loop the menu continously for a number of reasons: + * 1) To make sure we capture and respond to user input + * 2) To continue redrawing the menu graphic. + * + * If we did not do number 2 then the window would appear blank if minimised + * and then de-minimised again, amongst other situations + */ + while ( 1 ) + { + SDL_BlitSurface( surf_title, NULL, surf_screen, NULL); // Draw menu + SDL_Flip( surf_screen ); // Update screen + + // The while statement attempts to go through every key the user has pressed + // it stops when the user has not pressed any more keys + // this is useful incase the user presses keys fast or multiple at once + while ( SDL_PollEvent( &menuevent ) ) + { + if ( menuevent.type == SDL_KEYDOWN ) + { + // Menu choice selection + if ( menuevent.key.keysym.sym == SDLK_p ) game_loop( surf_screen ); + else if ( menuevent.key.keysym.sym == SDLK_h ) help( surf_screen ); + else if ( menuevent.key.keysym.sym == SDLK_q || menuevent.key.keysym.sym == SDLK_ESCAPE ) exit(0); + } + } + + // If we do not 'wait' or sleep in every ieteration of the loop + // the game will loop as fast as it can, utilising 100% of the CPU + SDL_Delay( 200 ); + } + + + + + /* Technically the computer will never reach this point, because the while statement + * above will run forever until the user quits the game. + * + * The 'return' statement is still written here so the compiler does not complain + */ + + return 0; + +} -- cgit v1.2.1-18-gbd029