view libmlk-core/mlk/core/window.c @ 613:f76cada0bbb2

misc: switch to SDL3
author David Demelier <markand@malikania.fr>
date Sun, 20 Aug 2023 11:14:58 +0200
parents 97af110e9e4d
children 281608524dd1
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
};

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));
}

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();

	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_ShowCursor();
	else {
		SDL_HideCursor();
		SDL_SetCursor(cursors[cursor]);
	}
}

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));
}