Mercurial > molko
comparison libmlk-core/mlk/core/window.c @ 431:8f59201dc76b
core: cleanup hierarchy
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 15 Oct 2022 20:23:14 +0200 |
parents | src/libmlk-core/core/window.c@1645433e008d |
children | e1eebc6bf25d |
comparison
equal
deleted
inserted
replaced
430:1645433e008d | 431:8f59201dc76b |
---|---|
1 /* | |
2 * window.c -- basic window management | |
3 * | |
4 * Copyright (c) 2020-2022 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 #include <string.h> | |
21 | |
22 #include <SDL.h> | |
23 | |
24 #include "err.h" | |
25 #include "util.h" | |
26 #include "window.h" | |
27 #include "window_p.h" | |
28 | |
29 static struct window_handle handle = { | |
30 .win = NULL, | |
31 .renderer = NULL | |
32 }; | |
33 | |
34 static SDL_Cursor *cursors[WINDOW_CURSOR_LAST]; | |
35 | |
36 struct window window = { | |
37 .handle = &handle | |
38 }; | |
39 | |
40 static void | |
41 load_cursors(void) | |
42 { | |
43 cursors[WINDOW_CURSOR_ARROW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW); | |
44 cursors[WINDOW_CURSOR_EDIT] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM); | |
45 cursors[WINDOW_CURSOR_WAIT] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAIT); | |
46 cursors[WINDOW_CURSOR_CROSSHAIR] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR); | |
47 cursors[WINDOW_CURSOR_SIZE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL); | |
48 cursors[WINDOW_CURSOR_NO] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO); | |
49 cursors[WINDOW_CURSOR_HAND] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND); | |
50 } | |
51 | |
52 static void | |
53 load_framerate(void) | |
54 { | |
55 SDL_DisplayMode mode; | |
56 | |
57 if (SDL_GetWindowDisplayMode(handle.win, &mode) == 0) | |
58 window.framerate = mode.refresh_rate; | |
59 } | |
60 | |
61 static void | |
62 finish_cursors(void) | |
63 { | |
64 for (size_t i = 0; i < UTIL_SIZE(cursors); ++i) | |
65 if (cursors[i]) | |
66 SDL_FreeCursor(cursors[i]); | |
67 } | |
68 | |
69 static int | |
70 load_window(const char *title, unsigned int w, unsigned int h) | |
71 { | |
72 return (handle.win = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, 0)) != NULL; | |
73 } | |
74 | |
75 static int | |
76 load_renderer(void) | |
77 { | |
78 return (handle.renderer = SDL_CreateRenderer(handle.win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)) != NULL; | |
79 } | |
80 | |
81 int | |
82 window_open(const char *title, unsigned int w, unsigned int h) | |
83 { | |
84 assert(title); | |
85 | |
86 if (!load_window(title, w, h) || !load_renderer()) | |
87 return ERR_SDL; | |
88 | |
89 window.w = w; | |
90 window.h = h; | |
91 | |
92 load_framerate(); | |
93 load_cursors(); | |
94 | |
95 return 0; | |
96 } | |
97 | |
98 void | |
99 window_set_cursor(enum window_cursor cursor) | |
100 { | |
101 assert(cursor < WINDOW_CURSOR_LAST); | |
102 | |
103 SDL_SetCursor(cursors[cursor]); | |
104 } | |
105 | |
106 void | |
107 window_finish(void) | |
108 { | |
109 if (handle.renderer) | |
110 SDL_DestroyRenderer(handle.renderer); | |
111 if (handle.win) | |
112 SDL_DestroyWindow(handle.win); | |
113 | |
114 finish_cursors(); | |
115 | |
116 memset(&handle, 0, sizeof (handle)); | |
117 } |