From 05c4bf15dd248d81284d1166ef301da29e5b30f4 Mon Sep 17 00:00:00 2001
From: Robert <robert.trololo@gmail.com>
Date: Fri, 30 Jul 2021 15:28:20 +0200
Subject: [PATCH] Fixed memory issue + windows clear screen bug

---
 src/main.cpp | 38 +++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)

diff --git a/src/main.cpp b/src/main.cpp
index 66b6f62..a655982 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -26,6 +26,20 @@ int main(int argc, char** argv)
 	SDL_Window* window = SDL_CreateWindow("Bytepusher++", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1000, 700, SDL_WINDOW_RESIZABLE);
 	SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
 
+	// There is a bug in ImGui where the background isn't cleared properly on Windows 10.
+	// The workaround is to create a texture with the background color and use that to clear the screen instead
+#ifdef _WIN32
+	SDL_Texture* windowsClearFix = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, 1, 1);
+	Uint8* pixels;
+	int pitch;
+	SDL_LockTexture(windowsClearFix, NULL, (void**)&pixels, &pitch);
+	pixels[0] = 114;
+	pixels[1] = 144;
+	pixels[2] = 154;
+	pixels[3] = 255;
+	SDL_UnlockTexture(windowsClearFix);
+#endif
+
 	// VM initialisation
 	Bus* bus = new Bus();
 
@@ -48,15 +62,6 @@ int main(int argc, char** argv)
 	ImGuiSDL::Initialize(renderer, 900, 700);
     ImGui::StyleColorsDark();
 
-	// Clears screen
-	SDL_Texture* clear = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, 100, 100);
-	{
-		SDL_SetRenderTarget(renderer, clear);
-		SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255);
-		SDL_RenderClear(renderer);
-		SDL_SetRenderTarget(renderer, nullptr);
-	}
-
 	std::time_t res = std::time(nullptr);
 	std::string date = std::ctime(&res);
 	date = date.substr(0, date.length() - 1);
@@ -216,7 +221,12 @@ int main(int argc, char** argv)
 		ImGui::End();
 
 		SDL_SetRenderDrawColor(renderer, 114, 144, 154, 255);
+
+#ifdef _WIN32
+		SDL_RenderCopy(renderer, windowsClearFix, NULL, NULL);
+#else
 		SDL_RenderClear(renderer);
+#endif
 
 		ImGui::Render();
 		ImGuiSDL::Render(ImGui::GetDrawData());
@@ -231,10 +241,6 @@ int main(int argc, char** argv)
 
 	ImGuiSDL::Deinitialize();
 
-	SDL_DestroyTexture(clear);
-	SDL_DestroyRenderer(renderer);
-	SDL_DestroyWindow(window);
-
     delete cpu;
 	delete memory;
 	delete display;
@@ -242,6 +248,12 @@ int main(int argc, char** argv)
 
 	delete bus;
 	ImGui::DestroyContext();
+
+#ifdef _WIN32
+	SDL_DestroyTexture(windowsClearFix);
+#endif
+	SDL_DestroyRenderer(renderer);
+	SDL_DestroyWindow(window);
 	
 	return EXIT_SUCCESS;
 }