To The Flag by ML (mlefebvre.fr)
main.c
Go to the documentation of this file.
1 
9 #include <stdio.h>
10 
11 #include <SDL/SDL.h>
12 #include <SDL/SDL_image.h>
13 #include <SDL/SDL_ttf.h>
14 
15 #include "constants.h"
16 
17 int main(int argc, char* argv[])
18 {
19  // --- DECLARE VARIABLES ---
20  SDL_Surface *window = NULL;
21  SDL_Surface *menu = IMG_Load("art/menu.png");
22  SDL_Rect position;
23  SDL_Event event;
24 
25  // --- CONTROL VARIABLE ---
26  int run = 1;
27 
28  // --- INIT APPLICATION ---
29  if(SDL_Init(SDL_INIT_VIDEO) == -1) // Load SDL video mode
30  {
31  fprintf(stderr, "ERROR loading SDL video mode"); // If not load properly, save error
32  exit(EXIT_FAILURE); // Exit application
33  }
34 
35  if(TTF_Init() != NULL) // Load TTF library for font handling
36  {
37  fprintf(stderr, "ERROR loading the TFF library"); // If not load properly, save error
38  exit(EXIT_FAILURE); // Exit application
39  }
40 
41  SDL_EnableKeyRepeat(100, 100); // Enable key repeat for the user
42 
43  SDL_WM_SetIcon(IMG_Load("art/icon.png"), NULL); // Set window icon
44  window = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); // Create main window
45  SDL_WM_SetCaption("To The Flag by ML (mlefebvre.fr)", NULL); // Change window caption
46 
47  position.x = 0; // Set menu position, relative to
48  position.y = 0; // the top left corner of the window
49 
50  // --- EXECUTE APPLICATION CONTINUOUSLY ---
51  while(run)
52  {
53  // --- DRAW MENU ---
54  SDL_FillRect(window, NULL, SDL_MapRGB(window->format, 0, 0, 0)); // Reset main window
55  SDL_BlitSurface(menu, NULL, window, &position); // Add menu
56  SDL_Flip(window); // Redraw everything
57 
58  SDL_WaitEvent(&event); // Wait for the next user event
59  switch(event.type) // Test event type value
60  {
61  case SDL_QUIT: // If the user wants to close the window
62  run = 0; // Stop the application
63  break;
64  case SDL_KEYDOWN: // If the user pressed a keyboard key
65  switch(event.key.keysym.sym) // Test key value
66  {
67  case SDLK_RETURN: // If the user pressed the carriage return key
68  playGame(window); // Load the game
69  break;
70  case SDLK_ESCAPE: // If the user pressed the escape key
71  run = 0; // Stop the application
72  break;
73  default: // In any other case, do nothing
74  break;
75  }
76  default: // In any other case, do nothing
77  break;
78  }
79  }
80 
81  // --- EXIT APPLICATION ---
82  SDL_FreeSurface(menu); // Free the allocated memory for the menu surface
83 
84  TTF_Quit(); // Unload TFF library
85  SDL_QUIT; // Unload SDL video mode
86 
87  return EXIT_SUCCESS; // Return 0
88 }
void playGame(SDL_Surface *w)
Definition: play.c:23
Contains every application constants.