From bdf90e7ac9f516b7aa6425fb3246581dd1b3ce4b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Sat, 29 Apr 2023 15:47:29 -0400
Subject: [PATCH] Don't re-create/delete the bitmap for each frame.

Instead, only delete it if the size has changed since the last
frame.  This should make things snappier.
---
 src/conio/win32gdi.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/conio/win32gdi.c b/src/conio/win32gdi.c
index 0a1747bbbc..598548c20b 100644
--- a/src/conio/win32gdi.c
+++ b/src/conio/win32gdi.c
@@ -181,10 +181,11 @@ static LRESULT
 gdi_handle_wm_paint(HWND hwnd)
 {
 	static HDC memDC = NULL;
+	static HBITMAP di = NULL;
+	static int diw, dih;
 
 	PAINTSTRUCT ps;
 	struct rectlist *list;
-	HBITMAP di;
 	HDC winDC;
 	int w,h;
 	int aw,ah;
@@ -207,13 +208,25 @@ gdi_handle_wm_paint(HWND hwnd)
 	if (ciolib_scaling) {
 		calc_scaling_factors(&xscale, &yscale, w, h, aw, ah, sw, sh);
 		gb = do_scale(list, xscale, yscale, aw, ah);
+		if (diw != gb->w || dih != gb->h) {
+			DeleteObject(di);
+			di = NULL;
+		}
+		diw = gb->w;
 		b5hdr.bV5Width = gb->w;
+		dih = gb->h;
 		b5hdr.bV5Height = -gb->h;
 		b5hdr.bV5SizeImage = gb->w * gb->h * 4;
 		data = gb->data;
 	}
 	else {
+		if (diw != list->rect.width || dih != list->rect.height) {
+			DeleteObject(di);
+			di = NULL;
+		}
+		diw = list->rect.width;
 		b5hdr.bV5Width = list->rect.width;
+		dih = list->rect.height;
 		b5hdr.bV5Height = -list->rect.height;
 		b5hdr.bV5SizeImage = list->rect.width * list->rect.height * 4;
 		data = list->data;
@@ -222,7 +235,10 @@ gdi_handle_wm_paint(HWND hwnd)
 	if (memDC == NULL)
 		memDC = CreateCompatibleDC(winDC);
 	// Scale...
-	di = CreateDIBitmap(winDC, (BITMAPINFOHEADER *)&b5hdr, CBM_INIT, data, (BITMAPINFO *)&b5hdr, 0/*DIB_RGB_COLORS*/);
+	if (di == NULL)
+		di = CreateDIBitmap(winDC, (BITMAPINFOHEADER *)&b5hdr, CBM_INIT, data, (BITMAPINFO *)&b5hdr, 0/*DIB_RGB_COLORS*/);
+	else
+		SetDIBits(winDC, di, 0, -b5hdr.bV5Height, data, (BITMAPINFO *)&b5hdr, DIB_RGB_COLORS);
 	di = SelectObject(memDC, di);
 	if (ciolib_scaling) {
 		BitBlt(winDC, 0, 0, w, h, memDC, 0, 0, SRCCOPY);
@@ -232,7 +248,6 @@ gdi_handle_wm_paint(HWND hwnd)
 	}
 	EndPaint(hwnd, &ps);
 	di = SelectObject(memDC, di);
-	DeleteObject(di);
 	if (ciolib_scaling) {
 		release_buffer(gb);
 	}
-- 
GitLab