-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.c
69 lines (57 loc) · 1.83 KB
/
app.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_net.h>
#include "app.h"
#include "menu.h"
App *init_app() {
// Allocate memory for the core program features
App *app = malloc(sizeof(App));
// initialize window
app->window = SDL_CreateWindow("Snek", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
// handle for errors
if(app->window == NULL) {
// print error
fprintf(stderr, "Error while initializing window: %s\n", SDL_GetError());
// exit with failure
exit(EXIT_FAILURE);
}
// initialize renderer
app->renderer = SDL_CreateRenderer(app->window, -1, SDL_RENDERER_ACCELERATED);
// handle errors
if(app->renderer == NULL) {
// print error
fprintf(stderr, "Error while initializing renderer: %s\n", SDL_GetError());
// exit with failure
exit(EXIT_FAILURE);
}
// Collects information about current monitor.
if (SDL_GetCurrentDisplayMode(0, &app->display) != 0) {
// print error
fprintf(stderr, "Error while calling SDL_DisplayMode: %s\n", SDL_GetError());
// exit with failure
exit(EXIT_FAILURE);
}
char tmp[1] = "";
strcpy(app->ip, tmp);
strcpy(app->port, tmp);
// indicate that the app is running, used for main loop
app->running = true;
// Indicates wether applicatication is in fullscreen or not
app->fullscreen = false;
// return pointer
return app;
}
void quit_app(App *app) {
printf("Exiting...\n");
// destory window and renderer to free memory
SDL_DestroyWindow(app->window);
SDL_DestroyRenderer(app->renderer);
// cleanup subsystems before exiting
SDLNet_Quit();
TTF_Quit();
SDL_Quit();
// successfully exit
exit(EXIT_SUCCESS);
}