shithub: cstory

Download patch

ref: 74c9931ebbfb73c013a808c17e07f277752dc2d3
parent: 2fe0c44971a17df513ac2d2d4b26ea8d99ecba45
author: Clownacy <Clownacy@users.noreply.github.com>
date: Fri Jul 19 04:45:59 EDT 2019

Change the renderer backend API for uploading pixels

Also fix some blatant build errors. I must be going mad - I swear
I've fixed that typedef thing like twice already.

--- a/src/Backends/Rendering.h
+++ b/src/Backends/Rendering.h
@@ -13,7 +13,8 @@
 void Backend_DrawScreen(void);
 Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height);
 void Backend_FreeSurface(Backend_Surface *surface);
-void Backend_LoadPixels(Backend_Surface *surface, const unsigned char *pixels, unsigned int width, unsigned int height, unsigned int pitch);
+unsigned char* Backend_Lock(Backend_Surface *surface, unsigned int *pitch);
+void Backend_Unlock(Backend_Surface *surface);
 void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key);
 void Backend_BlitToScreen(Backend_Surface *source_surface, const RECT *rect, long x, long y, BOOL colour_key);
 void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue);
--- a/src/Backends/Rendering/SDLSurface.cpp
+++ b/src/Backends/Rendering/SDLSurface.cpp
@@ -82,15 +82,15 @@
 	free(surface);
 }
 
-void Backend_LoadPixels(Backend_Surface *surface, const unsigned char *pixels, unsigned int width, unsigned int height, unsigned int pitch)
+unsigned char* Backend_Lock(Backend_Surface *surface, unsigned int *pitch)
 {
-	for (unsigned int i = 0; i < height; ++i)
-	{
-		const unsigned char *src_row = &pixels[i * pitch];
-		unsigned char *dst_row = (unsigned char*)surface->sdl_surface->pixels + i * surface->sdl_surface->pitch;
+	*pitch = surface->sdl_surface->pitch;
+	return (unsigned char*)surface->sdl_surface->pixels;
+}
 
-		memcpy(dst_row, src_row, width * 3);
-	}
+void Backend_Unlock(Backend_Surface *surface)
+{
+	
 }
 
 void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key)
--- a/src/Backends/Rendering/SDLTexture.cpp
+++ b/src/Backends/Rendering/SDLTexture.cpp
@@ -34,7 +34,7 @@
 	{
 		unsigned char *src_pixel = (unsigned char*)surface->sdl_surface->pixels + (y * surface->sdl_surface->pitch);
 
-		for (int x = 0; x < surface->sdl_surface->x; ++x)
+		for (int x = 0; x < surface->sdl_surface->w; ++x)
 		{
 			*buffer_pointer++ = src_pixel[0];
 			*buffer_pointer++ = src_pixel[1];
@@ -146,16 +146,14 @@
 	free(surface);
 }
 
-void Backend_LoadPixels(Backend_Surface *surface, const unsigned char *pixels, unsigned int width, unsigned int height, unsigned int pitch)
+unsigned char* Backend_Lock(Backend_Surface *surface, unsigned int *pitch)
 {
-	for (unsigned int i = 0; i < height; ++i)
-	{
-		const unsigned char *src_row = &pixels[i * pitch];
-		unsigned char *dst_row = (unsigned char*)surface->sdl_surface->pixels + i * surface->sdl_surface->pitch;
+	*pitch = surface->sdl_surface->pitch;
+	return (unsigned char*)surface->sdl_surface->pixels;
+}
 
-		memcpy(dst_row, src_row, width * 3);
-	}
-
+void Backend_Unlock(Backend_Surface *surface)
+{
 	surface->needs_syncing = TRUE;
 }
 
--- a/src/Backends/Rendering/Software.cpp
+++ b/src/Backends/Rendering/Software.cpp
@@ -9,7 +9,7 @@
 
 #include "../../Font.h"
 
-struct Backend_Surface
+typedef struct Backend_Surface
 {
 	unsigned char *pixels;
 	unsigned int width;
@@ -81,15 +81,15 @@
 	free(surface);
 }
 
-void Backend_LoadPixels(Backend_Surface *surface, const unsigned char *pixels, unsigned int width, unsigned int height, unsigned int pitch)
+unsigned char* Backend_Lock(Backend_Surface *surface, unsigned int *pitch)
 {
-	for (unsigned int i = 0; i < height; ++i)
-	{
-		const unsigned char *src_row = &pixels[i * pitch];
-		unsigned char *dst_row = &surface->pixels[i * surface->pitch];
+	*pitch = surface->pitch;
+	return surface->pixels;
+}
 
-		memcpy(dst_row, src_row, width * 3);
-	}
+void Backend_Unlock(Backend_Surface *surface)
+{
+	
 }
 
 void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key)
--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -211,25 +211,32 @@
 					else
 					{
 						// IF YOU WANT TO ADD HD SPRITES, THIS IS THE CODE YOU SHOULD EDIT
+						unsigned int pitch;
+						unsigned char *pixels = Backend_Lock(surf[surf_no].backend, &pitch);
+
 						if (magnification == 1)
 						{
 							// Just copy the pixels the way they are
-							Backend_LoadPixels(surf[surf_no].backend, (unsigned char*)converted_surface->pixels, converted_surface->w, converted_surface->h, converted_surface->pitch);
+							for (int y = 0; y < converted_surface->h; ++y)
+							{
+								const unsigned char *src_row = (unsigned char*)converted_surface->pixels + y * converted_surface->pitch;
+								unsigned char *dst_row = &pixels[y * pitch];
+
+								memcpy(dst_row, src_row, converted_surface->w * 3);
+							}
 						}
 						else
 						{
 							// Upscale the bitmap to the game's internal resolution
-							unsigned char *pixels = (unsigned char*)malloc((converted_surface->w * magnification) * (converted_surface->h * magnification) * 3);
-
-							for (int h = 0; h < converted_surface->h; ++h)
+							for (int y = 0; y < converted_surface->h; ++y)
 							{
-								const unsigned char *src_row = (unsigned char*)converted_surface->pixels + h * converted_surface->pitch;
-								unsigned char *dst_row = pixels + h * (converted_surface->w * magnification * 3) * magnification;
+								const unsigned char *src_row = (unsigned char*)converted_surface->pixels + y * converted_surface->pitch;
+								unsigned char *dst_row = &pixels[y * pitch * magnification];
 
 								const unsigned char *src_ptr = src_row;
 								unsigned char *dst_ptr = dst_row;
 
-								for (int w = 0; w < converted_surface->w; ++w)
+								for (int x = 0; x < converted_surface->w; ++x)
 								{
 									for (int i = 0; i < magnification; ++i)
 									{
@@ -242,13 +249,11 @@
 								}
 
 								for (int i = 1; i < magnification; ++i)
-									memcpy(dst_row + i * converted_surface->w * magnification * 3, dst_row, converted_surface->w * magnification * 3);
+									memcpy(dst_row + i * pitch, dst_row, converted_surface->w * magnification * 3);
 							}
-
-							Backend_LoadPixels(surf[surf_no].backend, pixels, converted_surface->w * magnification, converted_surface->h * magnification, converted_surface->w * magnification * 3);
-							free(pixels);
 						}
 
+						Backend_Unlock(surf[surf_no].backend);
 						SDL_FreeSurface(converted_surface);
 						printf(" ^ Successfully loaded\n");
 						success = TRUE;