shithub: cstory

Download patch

ref: 6b18ec91bec40a2d9c2cb766fb8d30ab72337a9d
parent: ba797d0fead03c555cf199250f6fb94a36fb15ad
author: Clownacy <Clownacy@users.noreply.github.com>
date: Mon Sep 14 19:01:59 EDT 2020

Replace LockSurface with UploadSurface

The old locking/unlocking API was plain inefficient. Soon I'll move
upscaling to the backend anyway, to minimise the overhead where
possible.

Currently only the software renderer has been updated.

--- a/src/Backends/Rendering.h
+++ b/src/Backends/Rendering.h
@@ -20,8 +20,7 @@
 void RenderBackend_FreeSurface(RenderBackend_Surface *surface);
 bool RenderBackend_IsSurfaceLost(RenderBackend_Surface *surface);
 void RenderBackend_RestoreSurface(RenderBackend_Surface *surface);
-unsigned char* RenderBackend_LockSurface(RenderBackend_Surface *surface, size_t *pitch, size_t width, size_t height);
-void RenderBackend_UnlockSurface(RenderBackend_Surface *surface, size_t width, size_t height);
+void RenderBackend_UploadSurface(RenderBackend_Surface *surface, const unsigned char *pixels, size_t width, size_t height);
 void RenderBackend_Blit(RenderBackend_Surface *source_surface, const RenderBackend_Rect *rect, RenderBackend_Surface *destination_surface, long x, long y, bool colour_key);
 void RenderBackend_ColourFill(RenderBackend_Surface *surface, const RenderBackend_Rect *rect, unsigned char red, unsigned char green, unsigned char blue);
 RenderBackend_GlyphAtlas* RenderBackend_CreateGlyphAtlas(size_t width, size_t height);
--- a/src/Backends/Rendering/Software.cpp
+++ b/src/Backends/Rendering/Software.cpp
@@ -108,23 +108,10 @@
 	(void)surface;
 }
 
-unsigned char* RenderBackend_LockSurface(RenderBackend_Surface *surface, size_t *pitch, size_t width, size_t height)
+void RenderBackend_UploadSurface(RenderBackend_Surface *surface, const unsigned char *pixels, size_t width, size_t height)
 {
-	(void)width;
-	(void)height;
-
-	if (surface == NULL)
-		return NULL;
-
-	*pitch = surface->pitch;
-	return surface->pixels;
-}
-
-void RenderBackend_UnlockSurface(RenderBackend_Surface *surface, size_t width, size_t height)
-{
-	(void)surface;
-	(void)width;
-	(void)height;
+	for (size_t y = 0; y < height; ++y)
+		memcpy(&surface->pixels[y * surface->pitch], &pixels[y * width * 3], width * 3);
 }
 
 ATTRIBUTE_HOT void RenderBackend_Blit(RenderBackend_Surface *source_surface, const RenderBackend_Rect *rect, RenderBackend_Surface *destination_surface, long x, long y, bool colour_key)
--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -1,6 +1,7 @@
 #include "Draw.h"
 
 #include <stddef.h>
+#include <stdlib.h>
 #include <string.h>
 #include <string>
 
@@ -150,30 +151,23 @@
 static BOOL ScaleAndUploadSurface(const unsigned char *image_buffer, size_t width, size_t height, SurfaceID surf_no)
 {
 	// IF YOU WANT TO ADD HD SPRITES, THIS IS THE CODE YOU SHOULD EDIT
-	size_t pitch;
-	unsigned char *pixels = RenderBackend_LockSurface(surf[surf_no], &pitch, width * mag, height * mag);
-
-	if (pixels == NULL)
-		return FALSE;
-
 	if (mag == 1)
 	{
 		// Just copy the pixels the way they are
-		for (size_t y = 0; y < height; ++y)
-		{
-			const unsigned char *src_row = &image_buffer[y * width * 3];
-			unsigned char *dst_row = &pixels[y * pitch];
-
-			memcpy(dst_row, src_row, width * 3);
-		}
+		RenderBackend_UploadSurface(surf[surf_no], image_buffer, width, height);
 	}
 	else
 	{
+		unsigned char *upscaled_image_buffer = (unsigned char*)malloc(width * mag * height * mag * 3);
+
+		if (upscaled_image_buffer == NULL)
+			return FALSE;
+
 		// Upscale the bitmap to the game's internal resolution
 		for (size_t y = 0; y < height; ++y)
 		{
 			const unsigned char *src_row = &image_buffer[y * width * 3];
-			unsigned char *dst_row = &pixels[y * pitch * mag];
+			unsigned char *dst_row = &upscaled_image_buffer[y * width * 3 * mag];
 
 			const unsigned char *src_ptr = src_row;
 			unsigned char *dst_ptr = dst_row;
@@ -191,11 +185,13 @@
 			}
 
 			for (int i = 1; i < mag; ++i)
-				memcpy(dst_row + i * pitch, dst_row, width * mag * 3);
+				memcpy(dst_row + i * width * 3, dst_row, width * mag * 3);
 		}
-	}
 
-	RenderBackend_UnlockSurface(surf[surf_no], width * mag, height * mag);
+		RenderBackend_UploadSurface(surf[surf_no], upscaled_image_buffer, width * mag, height * mag);
+
+		free(upscaled_image_buffer);
+	}
 
 	return TRUE;
 }