shithub: pt2-clone

Download patch

ref: fc625eb050e047e88da6cac4b3f897e89e482f2b
parent: c5819c6992037e30f3babd22b2564ab7905161b7
author: Olav Sørensen <olav.sorensen@live.no>
date: Mon Jul 11 18:02:29 EDT 2022

Fullscreen bugfixes + others + cleanup

--- a/src/pt2_audio.h
+++ b/src/pt2_audio.h
@@ -29,13 +29,13 @@
 
 	// internal values (don't modify directly!)
 	int8_t AUD_DAT[2]; // DMA data buffer
-	const int8_t* location; // current location
+	const int8_t *location; // current location
 	uint16_t lengthCounter; // current length
 	int32_t sampleCounter; // how many bytes left in AUD_DAT
 	double dSample; // current sample point
 
 	// registers modified by Paula functions
-	const int8_t* AUD_LC; // location
+	const int8_t *AUD_LC; // location
 	uint16_t AUD_LEN; // length (in words)
 	double AUD_PER_delta, AUD_PER_deltamul; // delta
 	double AUD_VOL; // volume
--- a/src/pt2_header.h
+++ b/src/pt2_header.h
@@ -14,7 +14,7 @@
 #include "pt2_unicode.h"
 #include "pt2_palette.h"
 
-#define PROG_VER_STR "1.49"
+#define PROG_VER_STR "1.50"
 
 #ifdef _WIN32
 #define DIR_DELIMITER '\\'
--- a/src/pt2_keyboard.c
+++ b/src/pt2_keyboard.c
@@ -364,7 +364,7 @@
 	// TOGGLE FULLSCREEN (should always react)
 	if (scancode == SDL_SCANCODE_F11 && !keyb.leftAltPressed)
 	{
-		toggleFullScreen();
+		toggleFullscreen();
 
 		// prevent fullscreen toggle from firing twice on certain SDL2 Linux ports
 #ifdef __unix__
@@ -2289,7 +2289,7 @@
 #ifdef __APPLE__
 			if (keyb.leftCommandPressed && keyb.leftCtrlPressed)
 			{
-				toggleFullScreen();
+				toggleFullscreen();
 			}
 			else
 #endif
--- a/src/pt2_main.c
+++ b/src/pt2_main.c
@@ -44,7 +44,7 @@
 
 module_t *song = NULL; // globalized
 
-static bool backupMadeAfterCrash;
+static bool backupMadeAfterCrash, didDropFile;
 
 #ifdef _WIN32
 #define SYSMSG_FILE_ARG (WM_USER + 1)
@@ -324,7 +324,7 @@
 	SDL_ShowWindow(video.window);
 
 	if (config.startInFullscreen)
-		toggleFullScreen();
+		toggleFullscreen();
 
 	changePathToHome(); // set path to home/user-dir now
 	diskOpSetInitPath(); // set path to custom path in config (if present)
@@ -400,7 +400,10 @@
 		{
 			loadDroppedFile(event.drop.file, (uint32_t)strlen(event.drop.file), false, true);
 			SDL_free(event.drop.file);
-			SDL_RaiseWindow(video.window); // set window focus
+
+			// kludge: allow focus-clickthrough after drag-n-drop
+			SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
+			didDropFile = true;
 		}
 		if (event.type == SDL_QUIT)
 		{
@@ -426,15 +429,18 @@
 
 				ui.introScreenShown = false;
 			}
+
+			// kludge: we drag-n-dropped a file before this mouse click release, restore focus-clickthrough mode
+			if (didDropFile)
+			{
+				didDropFile = false;
+				SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "0");
+			}
 		}
 		else if (event.type == SDL_MOUSEBUTTONDOWN)
 		{
-			if (ui.sampleMarkingPos == -1 &&
-				!ui.forceSampleDrag && !ui.forceVolDrag &&
-				!ui.forceSampleEdit)
-			{
+			if (ui.sampleMarkingPos == -1 && !ui.forceSampleDrag && !ui.forceVolDrag && !ui.forceSampleEdit)
 				mouseButtonDownHandler(event.button.button);
-			}
 		}
 
 		if (ui.throwExit)
--- a/src/pt2_mouse.c
+++ b/src/pt2_mouse.c
@@ -272,11 +272,13 @@
 
 		// convert desktop coords to window coords
 		SDL_GetWindowPosition(video.window, &windowX, &windowY);
+
 		mx -= windowX;
 		my -= windowY;
 	}
 	else
 	{
+		// special mode for KMSDRM (XXX: Confirm that this still works...)
 		mouse.buttonState = SDL_GetMouseState(&mx, &my);
 	}
 
--- a/src/pt2_visuals.c
+++ b/src/pt2_visuals.c
@@ -2133,13 +2133,13 @@
 void updateSpectrumAnalyzer(int8_t vol, int16_t period)
 {
 	const uint8_t maxHeight = SPECTRUM_BAR_HEIGHT + 1; // +1 because of audio latency - allows full height to be seen
-	uint8_t scaledVol;
+	uint16_t scaledVol;
 	uint32_t scaledNote;
 
 	if (ui.visualizerMode != VISUAL_SPECTRUM || vol <= 0)
 		return;
 
-	scaledVol = (vol * 24600L) >> 16; // scaledVol = (vol * 256) / 682   = (x / 2.66)
+	scaledVol = ((uint16_t)vol * 24576) >> 16; // scaledVol = vol / 2.66667 (0..64 -> 0..24)
 
 	period = CLAMP(period, 113, 856);
 	period -= 113;
@@ -2149,7 +2149,7 @@
 	scaledNote /= 25093; // scaledNote now ranges 0..22, no need to clamp
 
 	// increment main spectrum bar
-	editor.spectrumVolumes[scaledNote] += scaledVol;
+	editor.spectrumVolumes[scaledNote] += (uint8_t)scaledVol;
 	if (editor.spectrumVolumes[scaledNote] > maxHeight)
 		editor.spectrumVolumes[scaledNote] = maxHeight;
 
@@ -2156,7 +2156,7 @@
 	// increment left side of spectrum bar with half volume
 	if (scaledNote > 0)
 	{
-		editor.spectrumVolumes[scaledNote-1] += scaledVol >> 1;
+		editor.spectrumVolumes[scaledNote-1] += (uint8_t)(scaledVol >> 1);
 		if (editor.spectrumVolumes[scaledNote-1] > maxHeight)
 			editor.spectrumVolumes[scaledNote-1] = maxHeight;
 	}
@@ -2164,7 +2164,7 @@
 	// increment right side of spectrum bar with half volume
 	if (scaledNote < SPECTRUM_BAR_NUM-1)
 	{
-		editor.spectrumVolumes[scaledNote+1] += scaledVol >> 1;
+		editor.spectrumVolumes[scaledNote+1] += (uint8_t)(scaledVol >> 1);
 		if (editor.spectrumVolumes[scaledNote+1] > maxHeight)
 			editor.spectrumVolumes[scaledNote+1] = maxHeight;
 	}
@@ -2202,22 +2202,25 @@
 
 void updateRenderSizeVars(void)
 {
-	int32_t di;
-#ifdef __APPLE__
-	int32_t actualScreenW, actualScreenH;
-	double dXUpscale, dYUpscale;
-#endif
 	float fXScale, fYScale;
-	SDL_DisplayMode dm;
 
-	di = SDL_GetWindowDisplayIndex(video.window);
-	if (di < 0)
-		di = 0; // return display index 0 (default) on error
+	if (video.useDesktopMouseCoords)
+	{
+		SDL_DisplayMode dm;
 
-	SDL_GetDesktopDisplayMode(di, &dm);
-	video.displayW = dm.w;
-	video.displayH = dm.h;
+		int32_t di = SDL_GetWindowDisplayIndex(video.window);
+		if (di < 0)
+			di = 0; // return display index 0 (default) on error
 
+		SDL_GetDesktopDisplayMode(di, &dm);
+		video.displayW = dm.w;
+		video.displayH = dm.h;
+	}
+	else
+	{
+		SDL_GetWindowSize(video.window, &video.displayW, &video.displayH);
+	}
+
 	if (video.fullscreen)
 	{
 		if (config.fullScreenStretch)
@@ -2234,20 +2237,25 @@
 			video.renderW = (int32_t)(SCREEN_W * fXScale);
 			video.renderH = (int32_t)(SCREEN_H * fYScale);
 
-#ifdef __APPLE__
-			// retina high-DPI hackery (SDL2 is bad at reporting actual rendering sizes on macOS w/ high-DPI)
+			// high-DPI hackery:
+			//  On high-DPI systems, the display w/h are given in logical pixels,
+			//  but the renderer size is given in physical pixels. Since our internal
+			//  render{X,Y,W,H} variables need to be in logical coordinates, as that's
+			//  what mouse input uses, scale them by the screen's DPI scale factor,
+			//  which is the physical (renderer) size / the logical (window) size.
+			//  On non high-DPI systems, this is effectively a no-op.
+			int32_t actualScreenW, actualScreenH;
 			SDL_GL_GetDrawableSize(video.window, &actualScreenW, &actualScreenH);
-			SDL_GetDesktopDisplayMode(0, &dm);
 
-			dXUpscale = (double)actualScreenW / video.displayW;
-			dYUpscale = (double)actualScreenH / video.displayH;
+			const double dXUpscale = (const double)actualScreenW / video.displayW;
+			const double dYUpscale = (const double)actualScreenH / video.displayH;
 
 			// downscale back to correct sizes
 			if (dXUpscale != 0.0) video.renderW = (int32_t)(video.renderW / dXUpscale);
 			if (dYUpscale != 0.0) video.renderH = (int32_t)(video.renderH / dYUpscale);
-#endif
-			video.renderX = (video.displayW - video.renderW) >> 1;
-			video.renderY = (video.displayH - video.renderH) >> 1;
+
+			video.renderX = (video.displayW - video.renderW) / 2;
+			video.renderY = (video.displayH - video.renderH) / 2;
 		}
 	}
 	else
@@ -2259,21 +2267,29 @@
 	}
 
 	// for mouse cursor creation
-	video.xScale = (int32_t)((video.renderW * (1.0 / SCREEN_W)) + 0.5);
-	video.yScale = (int32_t)((video.renderH * (1.0 / SCREEN_H)) + 0.5);
+	video.xScale = (int32_t)round(video.renderW / (double)SCREEN_W);
+	video.yScale = (int32_t)round(video.renderH / (double)SCREEN_H);
+
 	createMouseCursors();
 }
 
-void toggleFullScreen(void)
+void toggleFullscreen(void)
 {
-	SDL_DisplayMode dm;
-
 	video.fullscreen ^= 1;
 	if (video.fullscreen)
 	{
+		SDL_SetWindowFullscreen(video.window, SDL_WINDOW_FULLSCREEN_DESKTOP);
+
 		if (config.fullScreenStretch)
 		{
-			SDL_GetDesktopDisplayMode(0, &dm);
+			SDL_DisplayMode dm;
+
+			int32_t di = SDL_GetWindowDisplayIndex(video.window);
+			if (di < 0)
+				di = 0; // return display index 0 (default) on error
+
+			SDL_GetDesktopDisplayMode(di, &dm);
+
 			SDL_RenderSetLogicalSize(video.renderer, dm.w, dm.h);
 		}
 		else
@@ -2282,8 +2298,10 @@
 		}
 
 		SDL_SetWindowSize(video.window, SCREEN_W, SCREEN_H);
-		SDL_SetWindowFullscreen(video.window, SDL_WINDOW_FULLSCREEN_DESKTOP);
+
+#ifndef __unix__ // can be severely buggy on Linux... (at least when used like this)
 		SDL_SetWindowGrab(video.window, SDL_TRUE);
+#endif
 	}
 	else
 	{
@@ -2291,10 +2309,13 @@
 		SDL_RenderSetLogicalSize(video.renderer, SCREEN_W, SCREEN_H);
 		SDL_SetWindowSize(video.window, SCREEN_W * config.videoScaleFactor, SCREEN_H * config.videoScaleFactor);
 
-		// this is not sensible on a multi-monitor setup
-		//SDL_SetWindowPosition(video.window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
+#ifdef __unix__ // can be required on Linux... (or else the window keeps moving down every time you leave fullscreen)
+		SDL_SetWindowPosition(video.window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
+#endif
 
+#ifndef __unix__
 		SDL_SetWindowGrab(video.window, SDL_FALSE);
+#endif
 	}
 
 	updateRenderSizeVars();
@@ -2302,13 +2323,13 @@
 
 	if (video.fullscreen)
 	{
-		mouse.setPosX = video.displayW >> 1;
-		mouse.setPosY = video.displayH >> 1;
+		mouse.setPosX = video.displayW / 2;
+		mouse.setPosY = video.displayH / 2;
 	}
 	else
 	{
-		mouse.setPosX = video.renderW >> 1;
-		mouse.setPosY = video.renderH >> 1;
+		mouse.setPosX = video.renderW / 2;
+		mouse.setPosY = video.renderH / 2;
 	}
 
 	mouse.setPosFlag = true;
@@ -2316,15 +2337,11 @@
 
 bool setupVideo(void)
 {
-	int32_t screenW, screenH;
-	uint32_t rendererFlags;
-	SDL_DisplayMode dm;
+	int32_t screenW = SCREEN_W * config.videoScaleFactor;
+	int32_t screenH = SCREEN_H * config.videoScaleFactor;
 
-	screenW = SCREEN_W * config.videoScaleFactor;
-	screenH = SCREEN_H * config.videoScaleFactor;
+	uint32_t rendererFlags = 0;
 
-	rendererFlags = 0;
-
 #ifdef _WIN32
 #if SDL_PATCHLEVEL >= 4
 	SDL_SetHint(SDL_HINT_WINDOWS_NO_CLOSE_ON_ALT_F4, "1"); // this is for Windows only
@@ -2334,7 +2351,13 @@
 	video.vsync60HzPresent = false;
 	if (!config.vsyncOff)
 	{
-		SDL_GetDesktopDisplayMode(0, &dm);
+		SDL_DisplayMode dm;
+
+		int32_t di = SDL_GetWindowDisplayIndex(video.window);
+		if (di < 0)
+			di = 0; // return display index 0 (default) on error
+
+		SDL_GetDesktopDisplayMode(di, &dm);
 		if (dm.refresh_rate >= 59 && dm.refresh_rate <= 61)
 		{
 			video.vsync60HzPresent = true;
@@ -2407,6 +2430,12 @@
 	// we want an aligned pointer
 	video.frameBuffer = (uint32_t *)ALIGN_PTR(video.frameBufferUnaligned, 256);
 
+	// Workaround: SDL_GetGlobalMouseState() doesn't work with KMSDRM/Wayland
+	video.useDesktopMouseCoords = true;
+	const char *videoDriver = SDL_GetCurrentVideoDriver();
+	if (videoDriver != NULL && (strcmp("KMSDRM", videoDriver) == 0 || strcmp("wayland", videoDriver) == 0))
+		video.useDesktopMouseCoords = false;
+
 	updateRenderSizeVars();
 	updateMouseScaling();
 
@@ -2414,12 +2443,6 @@
 		SDL_ShowCursor(SDL_TRUE);
 	else
 		SDL_ShowCursor(SDL_FALSE);
-
-	// Workaround: SDL_GetGlobalMouseState() doesn't work with KMSDRM/Wayland
-	video.useDesktopMouseCoords = true;
-	const char *videoDriver = SDL_GetCurrentVideoDriver();
-	if (videoDriver != NULL && (strcmp("KMSDRM", videoDriver) == 0 || strcmp("wayland", videoDriver) == 0))
-		video.useDesktopMouseCoords = false;
 
 	SDL_SetRenderDrawColor(video.renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
 	return true;
--- a/src/pt2_visuals.h
+++ b/src/pt2_visuals.h
@@ -47,7 +47,7 @@
 void updatePosEd(void);
 void updateVisualizer(void);
 void updateEditOp(void);
-void toggleFullScreen(void);
+void toggleFullscreen(void);
 void videoClose(void);
 
 void displayMainScreen(void);