1 /* startscreen.c */
2 
3 # include <stdio.h>
4 # include <stdlib.h>
5 # include "SDL2/SDL.h"
6 # include "SDL2/SDL_image.h"
7 # include "SDL2/SDL_mixer.h"
8 
startscreen(SDL_Window * screen,uint * state,uint * grapset,uint * fullscreen)9 void startscreen(SDL_Window *screen,uint *state,uint *grapset,uint *fullscreen) {
10 
11 	/* Renderer (with VSync, nice !) */
12 	SDL_Renderer *renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_PRESENTVSYNC|SDL_RENDERER_ACCELERATED);
13 	SDL_SetHint("SDL_HINT_RENDER_SCALE_QUALITY", "0");
14 	SDL_RenderSetLogicalSize(renderer, 256, 192);
15 
16 	uint exit = 0;
17 	uint musicplay = 0;
18 
19 	SDL_Rect srcintro = {0,0,256,192};
20 	SDL_Rect desintro = {0,0,256,192};
21 
22 	SDL_Event keyp;
23 
24 	/* Loading PNG */
25 	SDL_Texture *intro = IMG_LoadTexture(renderer, DATADIR "/graphics/intro.png");
26 	SDL_Texture *intromd = IMG_LoadTexture(renderer, DATADIR "/graphics/intromd.png");
27 
28 	/* Load audio */
29 	Mix_Music *music = Mix_LoadMUS(DATADIR "/sounds/MainTitleN.ogg");
30 
31 	while (exit != 1) {
32 
33 		/* Cleaning the renderer */
34 		SDL_RenderClear(renderer);
35 
36 		/* Put image on renderer */
37 		if (*grapset == 0)
38 			SDL_RenderCopy(renderer, intro, &srcintro, &desintro);
39 		else
40 			SDL_RenderCopy(renderer, intromd, &srcintro, &desintro);
41 
42 		/* Flip ! */
43 		SDL_RenderPresent(renderer);
44 
45 		/* Play music if required */
46 		if (musicplay == 0) {
47 			musicplay = 1;
48 			Mix_PlayMusic(music, 0);
49 		}
50 
51 		/* Check keyboard */
52 		if ( SDL_PollEvent(&keyp) ) {
53 			if (keyp.type == SDL_KEYDOWN) { /* Key pressed */
54 				if (keyp.key.keysym.sym == SDLK_c) { /* Change graphic set */
55 					if (*grapset == 0)
56 						*grapset = 1;
57 					else
58 						*grapset = 0;
59 				}
60 				if (keyp.key.keysym.sym == SDLK_i) { /* Show instructions */
61 					if (srcintro.y == 0)
62 						srcintro.y = 192;
63 					else {
64 						srcintro.y = 0;
65 						musicplay = 0;
66 					}
67 				}
68 				if (keyp.key.keysym.sym == SDLK_f) { /* Switch fullscreen/windowed */
69 					if (*fullscreen == 0) {
70 						SDL_SetWindowFullscreen(screen,SDL_WINDOW_FULLSCREEN_DESKTOP);
71 						*fullscreen = 1;
72 					}
73 					else {
74 						SDL_SetWindowFullscreen(screen,0);
75 						*fullscreen = 0;
76 					}
77 				}
78 				if (keyp.key.keysym.sym == SDLK_SPACE) { /* Start game */
79 					*state = 1;
80 					exit = 1;
81 				}
82 				if (keyp.key.keysym.sym == SDLK_ESCAPE) { /* Exit game */
83       		exit = 1;
84 					*state = 6;
85 				}
86 			}
87 		}
88 
89 	}
90 
91 	/* Cleaning */
92 	SDL_DestroyTexture(intro);
93 	SDL_DestroyTexture(intromd);
94 	SDL_DestroyRenderer(renderer);
95 
96 }