Mercurial > molko
view libmlk-core/mlk/core/window.c @ 646:7e1eb7f6c049 default tip @
misc: remove .clang
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 04 Feb 2024 15:24:37 +0100 |
parents | 19b195954ec3 |
children |
line wrap: on
line source
/* * window.c -- basic window management * * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <assert.h> #include <string.h> #include <SDL.h> #include "err.h" #include "util.h" #include "window.h" #include "window_p.h" static struct mlk__window_handle handle = { .win = NULL, .renderer = NULL }; static SDL_Cursor *cursors[MLK_WINDOW_CURSOR_LAST]; struct mlk_window mlk_window = { .handle = &handle, .theme_user = MLK_WINDOW_THEME_AUTO, .theme_effective = MLK_WINDOW_THEME_LIGHT, }; static void load_cursors(void) { cursors[MLK_WINDOW_CURSOR_ARROW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW); cursors[MLK_WINDOW_CURSOR_EDIT] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM); cursors[MLK_WINDOW_CURSOR_WAIT] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAIT); cursors[MLK_WINDOW_CURSOR_CROSSHAIR] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR); cursors[MLK_WINDOW_CURSOR_SIZE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL); cursors[MLK_WINDOW_CURSOR_NO] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO); cursors[MLK_WINDOW_CURSOR_HAND] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND); } static void load_framerate(void) { const SDL_DisplayMode *mode; if ((mode = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(handle.win)))) mlk_window.framerate = mode->refresh_rate; } static void finish_cursors(void) { for (size_t i = 0; i < MLK_UTIL_SIZE(cursors); ++i) if (cursors[i]) SDL_DestroyCursor(cursors[i]); } static int load_window(const char *title, unsigned int w, unsigned int h) { return (handle.win = SDL_CreateWindow(title, w, h, 0)) != NULL; } static int load_renderer(void) { return (handle.renderer = SDL_CreateRenderer(handle.win, NULL, 0)) != NULL; } int mlk_window_open(const char *title, unsigned int w, unsigned int h) { assert(title); assert(w); assert(h); if (!load_window(title, w, h) || !load_renderer()) return mlk_errf("%s", SDL_GetError()); mlk_window.w = w; mlk_window.h = h; load_framerate(); load_cursors(); mlk_window_set_theme(mlk_window.theme_user); return 0; } void mlk_window_set_cursor(enum mlk_window_cursor cursor) { assert(cursor < MLK_WINDOW_CURSOR_LAST); if (cursor == MLK_WINDOW_CURSOR_OFF) SDL_HideCursor(); else { SDL_ShowCursor(); SDL_SetCursor(cursors[cursor]); } } void mlk_window_set_theme(enum mlk_window_theme theme) { assert(theme >= 0 && theme < MLK_WINDOW_THEME_LAST); mlk_window.theme_user = theme; if (theme == MLK_WINDOW_THEME_AUTO) { if (SDL_GetSystemTheme() == SDL_SYSTEM_THEME_DARK) mlk_window.theme_effective = MLK_WINDOW_THEME_DARK; else mlk_window.theme_effective = MLK_WINDOW_THEME_LIGHT; } else mlk_window.theme_effective = mlk_window.theme_user; } void mlk_window_finish(void) { if (handle.renderer) SDL_DestroyRenderer(handle.renderer); if (handle.win) SDL_DestroyWindow(handle.win); finish_cursors(); memset(&handle, 0, sizeof (handle)); }