mirror of
https://github.com/sm64pc/sm64pc.git
synced 2025-04-09 12:25:42 +02:00
310 lines
11 KiB
C
310 lines
11 KiB
C
#ifdef __MINGW32__
|
|
#define FOR_WINDOWS 1
|
|
#else
|
|
#define FOR_WINDOWS 0
|
|
#endif
|
|
|
|
#if FOR_WINDOWS
|
|
#define GLEW_STATIC
|
|
#include <GL/glew.h>
|
|
#include <SDL2/SDL.h>
|
|
#define GL_GLEXT_PROTOTYPES 1
|
|
#include <SDL2/SDL_opengl.h>
|
|
#else
|
|
#include <SDL2/SDL.h>
|
|
#define GL_GLEXT_PROTOTYPES 1
|
|
|
|
#ifdef OSX_BUILD
|
|
#include <SDL2/SDL_opengl.h>
|
|
#else
|
|
#include <SDL2/SDL_opengles2.h>
|
|
#endif
|
|
|
|
#endif // End of OS-Specific GL defines
|
|
|
|
#include "gfx_window_manager_api.h"
|
|
#include "gfx_screen_config.h"
|
|
#include "../pc_main.h"
|
|
#include "../configfile.h"
|
|
#include "../cliopts.h"
|
|
|
|
#include "src/pc/controller/controller_keyboard.h"
|
|
|
|
// TODO: figure out if this shit even works
|
|
#ifdef VERSION_EU
|
|
# define FRAMERATE 25
|
|
#else
|
|
# define FRAMERATE 30
|
|
#endif
|
|
|
|
static SDL_Window *wnd;
|
|
static SDL_GLContext ctx = NULL;
|
|
static int inverted_scancode_table[512];
|
|
|
|
static bool window_fullscreen;
|
|
static bool window_vsync;
|
|
static int window_width, window_height;
|
|
|
|
static Uint64 frame_time = 0;
|
|
|
|
const SDL_Scancode windows_scancode_table[] =
|
|
{
|
|
/* 0 1 2 3 4 5 6 7 */
|
|
/* 8 9 A B C D E F */
|
|
SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_ESCAPE, SDL_SCANCODE_1, SDL_SCANCODE_2, SDL_SCANCODE_3, SDL_SCANCODE_4, SDL_SCANCODE_5, SDL_SCANCODE_6, /* 0 */
|
|
SDL_SCANCODE_7, SDL_SCANCODE_8, SDL_SCANCODE_9, SDL_SCANCODE_0, SDL_SCANCODE_MINUS, SDL_SCANCODE_EQUALS, SDL_SCANCODE_BACKSPACE, SDL_SCANCODE_TAB, /* 0 */
|
|
|
|
SDL_SCANCODE_Q, SDL_SCANCODE_W, SDL_SCANCODE_E, SDL_SCANCODE_R, SDL_SCANCODE_T, SDL_SCANCODE_Y, SDL_SCANCODE_U, SDL_SCANCODE_I, /* 1 */
|
|
SDL_SCANCODE_O, SDL_SCANCODE_P, SDL_SCANCODE_LEFTBRACKET, SDL_SCANCODE_RIGHTBRACKET, SDL_SCANCODE_RETURN, SDL_SCANCODE_LCTRL, SDL_SCANCODE_A, SDL_SCANCODE_S, /* 1 */
|
|
|
|
SDL_SCANCODE_D, SDL_SCANCODE_F, SDL_SCANCODE_G, SDL_SCANCODE_H, SDL_SCANCODE_J, SDL_SCANCODE_K, SDL_SCANCODE_L, SDL_SCANCODE_SEMICOLON, /* 2 */
|
|
SDL_SCANCODE_APOSTROPHE, SDL_SCANCODE_GRAVE, SDL_SCANCODE_LSHIFT, SDL_SCANCODE_BACKSLASH, SDL_SCANCODE_Z, SDL_SCANCODE_X, SDL_SCANCODE_C, SDL_SCANCODE_V, /* 2 */
|
|
|
|
SDL_SCANCODE_B, SDL_SCANCODE_N, SDL_SCANCODE_M, SDL_SCANCODE_COMMA, SDL_SCANCODE_PERIOD, SDL_SCANCODE_SLASH, SDL_SCANCODE_RSHIFT, SDL_SCANCODE_PRINTSCREEN,/* 3 */
|
|
SDL_SCANCODE_LALT, SDL_SCANCODE_SPACE, SDL_SCANCODE_CAPSLOCK, SDL_SCANCODE_F1, SDL_SCANCODE_F2, SDL_SCANCODE_F3, SDL_SCANCODE_F4, SDL_SCANCODE_F5, /* 3 */
|
|
|
|
SDL_SCANCODE_F6, SDL_SCANCODE_F7, SDL_SCANCODE_F8, SDL_SCANCODE_F9, SDL_SCANCODE_F10, SDL_SCANCODE_NUMLOCKCLEAR, SDL_SCANCODE_SCROLLLOCK, SDL_SCANCODE_HOME, /* 4 */
|
|
SDL_SCANCODE_UP, SDL_SCANCODE_PAGEUP, SDL_SCANCODE_KP_MINUS, SDL_SCANCODE_LEFT, SDL_SCANCODE_KP_5, SDL_SCANCODE_RIGHT, SDL_SCANCODE_KP_PLUS, SDL_SCANCODE_END, /* 4 */
|
|
|
|
SDL_SCANCODE_DOWN, SDL_SCANCODE_PAGEDOWN, SDL_SCANCODE_INSERT, SDL_SCANCODE_DELETE, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_NONUSBACKSLASH,SDL_SCANCODE_F11, /* 5 */
|
|
SDL_SCANCODE_F12, SDL_SCANCODE_PAUSE, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_LGUI, SDL_SCANCODE_RGUI, SDL_SCANCODE_APPLICATION, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN, /* 5 */
|
|
|
|
SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_F13, SDL_SCANCODE_F14, SDL_SCANCODE_F15, SDL_SCANCODE_F16, /* 6 */
|
|
SDL_SCANCODE_F17, SDL_SCANCODE_F18, SDL_SCANCODE_F19, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN, /* 6 */
|
|
|
|
SDL_SCANCODE_INTERNATIONAL2, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_INTERNATIONAL1, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN, /* 7 */
|
|
SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_INTERNATIONAL4, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_INTERNATIONAL5, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_INTERNATIONAL3, SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_UNKNOWN /* 7 */
|
|
};
|
|
|
|
const SDL_Scancode scancode_rmapping_extended[][2] = {
|
|
{SDL_SCANCODE_KP_ENTER, SDL_SCANCODE_RETURN},
|
|
{SDL_SCANCODE_RALT, SDL_SCANCODE_LALT},
|
|
{SDL_SCANCODE_RCTRL, SDL_SCANCODE_LCTRL},
|
|
{SDL_SCANCODE_KP_DIVIDE, SDL_SCANCODE_SLASH},
|
|
//{SDL_SCANCODE_KP_PLUS, SDL_SCANCODE_CAPSLOCK}
|
|
};
|
|
|
|
const SDL_Scancode scancode_rmapping_nonextended[][2] = {
|
|
{SDL_SCANCODE_KP_7, SDL_SCANCODE_HOME},
|
|
{SDL_SCANCODE_KP_8, SDL_SCANCODE_UP},
|
|
{SDL_SCANCODE_KP_9, SDL_SCANCODE_PAGEUP},
|
|
{SDL_SCANCODE_KP_4, SDL_SCANCODE_LEFT},
|
|
{SDL_SCANCODE_KP_6, SDL_SCANCODE_RIGHT},
|
|
{SDL_SCANCODE_KP_1, SDL_SCANCODE_END},
|
|
{SDL_SCANCODE_KP_2, SDL_SCANCODE_DOWN},
|
|
{SDL_SCANCODE_KP_3, SDL_SCANCODE_PAGEDOWN},
|
|
{SDL_SCANCODE_KP_0, SDL_SCANCODE_INSERT},
|
|
{SDL_SCANCODE_KP_PERIOD, SDL_SCANCODE_DELETE},
|
|
{SDL_SCANCODE_KP_MULTIPLY, SDL_SCANCODE_PRINTSCREEN}
|
|
};
|
|
|
|
static void gfx_sdl_set_fullscreen(bool fullscreen) {
|
|
if (fullscreen == window_fullscreen) return;
|
|
|
|
if (fullscreen) {
|
|
SDL_SetWindowFullscreen(wnd, SDL_WINDOW_FULLSCREEN_DESKTOP);
|
|
SDL_ShowCursor(SDL_DISABLE);
|
|
SDL_GetWindowSize(wnd, &window_width, &window_height);
|
|
} else {
|
|
SDL_SetWindowFullscreen(wnd, 0);
|
|
SDL_ShowCursor(SDL_ENABLE);
|
|
window_width = DESIRED_SCREEN_WIDTH;
|
|
window_height = DESIRED_SCREEN_HEIGHT;
|
|
SDL_SetWindowSize(wnd, window_width, window_height);
|
|
}
|
|
|
|
window_fullscreen = fullscreen;
|
|
}
|
|
|
|
static bool test_vsync(void) {
|
|
// Even if SDL_GL_SetSwapInterval succeeds, it doesn't mean that VSync actually works.
|
|
// A 60 Hz monitor should have a swap interval of 16.67 milliseconds.
|
|
// If it takes less than 12 milliseconds, assume that VSync is not working.
|
|
// SDL_GetTicks() probably does not offer enough precision for this kind of shit.
|
|
Uint32 start, end;
|
|
|
|
// do an extra swap, sometimes the first one takes longer (maybe creates buffers?)
|
|
SDL_GL_SwapWindow(wnd);
|
|
|
|
SDL_GL_SwapWindow(wnd);
|
|
start = SDL_GetTicks();
|
|
SDL_GL_SwapWindow(wnd);
|
|
end = SDL_GetTicks();
|
|
|
|
return (end - start >= 12);
|
|
}
|
|
|
|
static void gfx_sdl_init(void) {
|
|
SDL_Init(SDL_INIT_VIDEO);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
|
|
#ifdef USE_GLES
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); // These attributes allow for hardware acceleration on RPis.
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
|
#endif
|
|
|
|
//SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
|
//SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
|
|
|
|
if (gCLIOpts.FullScreen)
|
|
configFullscreen = true;
|
|
|
|
const char* window_title =
|
|
#ifndef USE_GLES
|
|
"Super Mario 64 PC port (OpenGL)";
|
|
#else
|
|
"Super Mario 64 PC port (OpenGL_ES2)";
|
|
#endif
|
|
|
|
Uint32 window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
|
|
|
|
window_width = DESIRED_SCREEN_WIDTH;
|
|
window_height = DESIRED_SCREEN_HEIGHT;
|
|
window_fullscreen = configFullscreen;
|
|
|
|
if (configFullscreen) {
|
|
window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
|
|
SDL_ShowCursor(SDL_DISABLE);
|
|
} else {
|
|
SDL_ShowCursor(SDL_ENABLE);
|
|
}
|
|
|
|
wnd = SDL_CreateWindow(window_title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
|
window_width, window_height, window_flags);
|
|
|
|
ctx = SDL_GL_CreateContext(wnd);
|
|
SDL_GL_SetSwapInterval(2);
|
|
|
|
window_vsync = test_vsync();
|
|
if (!window_vsync)
|
|
printf("Warning: VSync is not enabled or not working. Falling back to timer for synchronization\n");
|
|
|
|
for (size_t i = 0; i < sizeof(windows_scancode_table) / sizeof(SDL_Scancode); i++) {
|
|
inverted_scancode_table[windows_scancode_table[i]] = i;
|
|
}
|
|
|
|
for (size_t i = 0; i < sizeof(scancode_rmapping_extended) / sizeof(scancode_rmapping_extended[0]); i++) {
|
|
inverted_scancode_table[scancode_rmapping_extended[i][0]] = inverted_scancode_table[scancode_rmapping_extended[i][1]] + 0x100;
|
|
}
|
|
|
|
for (size_t i = 0; i < sizeof(scancode_rmapping_nonextended) / sizeof(scancode_rmapping_nonextended[0]); i++) {
|
|
inverted_scancode_table[scancode_rmapping_nonextended[i][0]] = inverted_scancode_table[scancode_rmapping_nonextended[i][1]];
|
|
inverted_scancode_table[scancode_rmapping_nonextended[i][1]] += 0x100;
|
|
}
|
|
}
|
|
|
|
static void gfx_sdl_main_loop(void (*run_one_game_iter)(void)) {
|
|
while (1)
|
|
run_one_game_iter();
|
|
}
|
|
|
|
static void gfx_sdl_get_dimensions(uint32_t *width, uint32_t *height) {
|
|
*width = window_width;
|
|
*height = window_height;
|
|
}
|
|
|
|
static int translate_scancode(int scancode) {
|
|
if (scancode < 512) {
|
|
return inverted_scancode_table[scancode];
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
static void gfx_sdl_onkeydown(int scancode) {
|
|
keyboard_on_key_down(translate_scancode(scancode));
|
|
|
|
const Uint8 *state = SDL_GetKeyboardState(NULL);
|
|
|
|
if (state[SDL_SCANCODE_LALT] && state[SDL_SCANCODE_RETURN])
|
|
configFullscreen = !configFullscreen;
|
|
else if (state[SDL_SCANCODE_ESCAPE] && configFullscreen)
|
|
configFullscreen = false;
|
|
}
|
|
|
|
static void gfx_sdl_onkeyup(int scancode) {
|
|
keyboard_on_key_up(translate_scancode(scancode));
|
|
}
|
|
|
|
static void gfx_sdl_handle_events(void) {
|
|
SDL_Event event;
|
|
while (SDL_PollEvent(&event)) {
|
|
switch (event.type) {
|
|
#ifndef TARGET_WEB
|
|
// Scancodes are broken in Emscripten SDL2: https://bugzilla.libsdl.org/show_bug.cgi?id=3259
|
|
case SDL_KEYDOWN:
|
|
gfx_sdl_onkeydown(event.key.keysym.scancode);
|
|
break;
|
|
case SDL_KEYUP:
|
|
gfx_sdl_onkeyup(event.key.keysym.scancode);
|
|
break;
|
|
#endif
|
|
case SDL_WINDOWEVENT:
|
|
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
|
|
window_width = event.window.data1;
|
|
window_height = event.window.data2;
|
|
}
|
|
break;
|
|
case SDL_QUIT:
|
|
game_exit();
|
|
break;
|
|
}
|
|
}
|
|
// just check if the fullscreen value has changed and toggle fullscreen if it has
|
|
if (configFullscreen != window_fullscreen)
|
|
gfx_sdl_set_fullscreen(configFullscreen);
|
|
}
|
|
|
|
static bool gfx_sdl_start_frame(void) {
|
|
return true;
|
|
}
|
|
|
|
static void sync_framerate_with_timer(void) {
|
|
// Number of milliseconds a frame should take (30 fps)
|
|
const Uint32 FRAME_TIME = 1000 / FRAMERATE;
|
|
static Uint32 last_time;
|
|
|
|
Uint32 elapsed = SDL_GetTicks() - last_time;
|
|
if (elapsed < FRAME_TIME)
|
|
SDL_Delay(FRAME_TIME - elapsed);
|
|
|
|
last_time = SDL_GetTicks();
|
|
}
|
|
|
|
static void gfx_sdl_swap_buffers_begin(void) {
|
|
if (!window_vsync)
|
|
sync_framerate_with_timer();
|
|
SDL_GL_SwapWindow(wnd);
|
|
}
|
|
|
|
static void gfx_sdl_swap_buffers_end(void) {
|
|
}
|
|
|
|
static double gfx_sdl_get_time(void) {
|
|
return 0.0;
|
|
}
|
|
|
|
|
|
static void gfx_sdl_shutdown(void) {
|
|
if (SDL_WasInit(0)) {
|
|
if (ctx) { SDL_GL_DeleteContext(ctx); ctx = NULL; }
|
|
if (wnd) { SDL_DestroyWindow(wnd); wnd = NULL; }
|
|
SDL_Quit();
|
|
}
|
|
}
|
|
|
|
struct GfxWindowManagerAPI gfx_sdl = {
|
|
gfx_sdl_init,
|
|
gfx_sdl_main_loop,
|
|
gfx_sdl_get_dimensions,
|
|
gfx_sdl_handle_events,
|
|
gfx_sdl_start_frame,
|
|
gfx_sdl_swap_buffers_begin,
|
|
gfx_sdl_swap_buffers_end,
|
|
gfx_sdl_get_time,
|
|
gfx_sdl_shutdown
|
|
};
|