summaryrefslogtreecommitdiff
path: root/main.c
blob: bc99a45f87aff7a523b1fa862708c26d591b24a9 (plain)
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
// Standard libraries ( not written by me )
#include <stdio.h>
#include <string.h>

// 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);

	SDL_Window * sdlWindow = SDL_CreateWindow("EchidneaMenace", SDL_WINDOWPOS_UNDEFINED,
			SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL );
	SDL_Renderer *renderer = SDL_CreateRenderer(sdlWindow, -1, 0);

	printf("done\n");
	
	/// Menu
	printf(" ==== Entered menu\n");

	// Load the menu graphic into memory
	SDL_Surface *surf_title = IMG_Load( "titlescreen.png" );
	if (!surf_title) {
		error_sdl("could not get title surface\n");
	}
	SDL_Texture *surf_title_tex = SDL_CreateTextureFromSurface(renderer, surf_title);
	if (!surf_title_tex) {
		error_sdl("could not get title texture from surface\n");
	}

	// Need to draw before the while loop otherwise we only do the
	// initial drawing after a keydown-event
	SDL_RenderClear(renderer);
	SDL_RenderCopy(renderer, surf_title_tex, NULL, NULL);
	SDL_RenderPresent(renderer);

	// Events are variables used to store user input
	SDL_Event menuevent;

	while ( SDL_WaitEvent( &menuevent ) >= 0)
	{
		SDL_RenderClear(renderer);
		SDL_RenderCopy(renderer, surf_title_tex, NULL, NULL);
		SDL_RenderPresent(renderer);

		if ( menuevent.type == SDL_KEYDOWN )
		{
			// Menu choice selection
			if      ( menuevent.key.keysym.sym == SDLK_p ) game_loop(renderer);
			else if ( menuevent.key.keysym.sym == SDLK_h ) help(renderer);
			else if ( menuevent.key.keysym.sym == SDLK_q || menuevent.key.keysym.sym == SDLK_ESCAPE ) exit(0);
		}
	}

	/* 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;
}