comparison src/window.c @ 0:efcc908bca21

core: implement basic windowing, closes #2437
author David Demelier <markand@malikania.fr>
date Mon, 06 Jan 2020 12:58:49 +0100
parents
children 03f6d572fd17
comparison
equal deleted inserted replaced
-1:000000000000 0:efcc908bca21
1 /*
2 * window.c -- basic window management
3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20
21 #include <SDL.h>
22
23 #include "window.h"
24
25 struct window {
26 SDL_Window *win;
27 SDL_Renderer *renderer;
28 };
29
30 struct window *
31 window_init(const char *title, unsigned width, unsigned height)
32 {
33 struct window *w;
34
35 assert(title);
36
37 if (!(w = calloc(1, sizeof (struct window))))
38 return NULL;
39 if (SDL_CreateWindowAndRenderer(width, height, SDL_WINDOW_OPENGL,
40 &w->win, &w->renderer) < 0)
41 goto fail;
42
43 SDL_SetWindowTitle(w->win, title);
44
45 return w;
46
47 fail:
48 free(w);
49 return NULL;
50 }
51
52 void
53 window_close(struct window *w)
54 {
55 assert(window);
56
57 SDL_DestroyRenderer(w->renderer);
58 SDL_DestroyWindow(w->win);
59 free(w);
60 }