summaryrefslogtreecommitdiff
path: root/main.c
blob: fbb130d6d05f809ddac3c0120667e8111157340f (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// 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);

	// Set surf_screen
	SDL_Texture* surf_screen = SDL_CreateTexture(renderer,
															SDL_PIXELFORMAT_ARGB8888,
															SDL_TEXTUREACCESS_STREAMING,
															640, 480);
	if ( surf_screen == NULL ) error_sdl("Could not set videomode");
	
	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" );
	if (!surf_title) {
		error_sdl("could not get title surface\n");
	}
	surf_title = SDL_ConvertSurfaceFormat(surf_title,
			SDL_PIXELFORMAT_ARGB8888, 0);

	SDL_Texture *surf_title_tex = SDL_CreateTextureFromSurface(renderer, surf_title);
	if (!surf_title_tex) {
		error_sdl("could not get title texture from surface\n");
	}

	/* 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_RenderClear(renderer);
		SDL_RenderCopy(renderer, surf_title_tex, NULL, NULL);
		SDL_RenderPresent(renderer);

		// 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(renderer, surf_screen);
				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);
			}
		}

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