ref: a0eb646a1f2f2f4356eb25a37e9d86153ff25aab
parent: 664d76f94e35e77927739b63d50078c540238175
author: Clownacy <Clownacy@users.noreply.github.com>
date: Mon Jan 6 22:51:32 EST 2020
Add ability to lock certain parts of a surface This avoids conditional jumps based on uninitalised memory, and should be faster, and should be lighter on memory.
--- a/src/Backends/Rendering.h
+++ b/src/Backends/Rendering.h
@@ -22,8 +22,8 @@
void Backend_FreeSurface(Backend_Surface *surface);
BOOL Backend_IsSurfaceLost(Backend_Surface *surface);
void Backend_RestoreSurface(Backend_Surface *surface);
-unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch);
-void Backend_UnlockSurface(Backend_Surface *surface);
+unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height);
+void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height);
void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, 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);
BOOL Backend_SupportsSubpixelGlyphs(void);
--- a/src/Backends/Rendering/OpenGL3.cpp
+++ b/src/Backends/Rendering/OpenGL3.cpp
@@ -474,17 +474,17 @@
(void)surface;
}
-unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch)
+unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height)
{
if (surface == NULL)
return NULL;
- surface->pixels = (unsigned char*)malloc(surface->width * surface->height * 3);
- *pitch = surface->width * 3;
+ surface->pixels = (unsigned char*)malloc(width * height * 3);
+ *pitch = width * 3;
return surface->pixels;
}
-void Backend_UnlockSurface(Backend_Surface *surface)
+void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height)
{
if (surface == NULL)
return;
@@ -493,7 +493,7 @@
glGetIntegerv(GL_TEXTURE_BINDING_2D, &previously_bound_texture);
glBindTexture(GL_TEXTURE_2D, surface->texture_id);
- glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, surface->width, surface->height, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
+ glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
free(surface->pixels);
glBindTexture(GL_TEXTURE_2D, previously_bound_texture);
--- a/src/Backends/Rendering/SDLSurface.cpp
+++ b/src/Backends/Rendering/SDLSurface.cpp
@@ -105,7 +105,7 @@
(void)surface;
}
-unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch)
+unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height)
{
if (surface == NULL)
return NULL;
@@ -114,7 +114,7 @@
return (unsigned char*)surface->sdlsurface->pixels;
}
-void Backend_UnlockSurface(Backend_Surface *surface)
+void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height)
{
(void)surface;
}
--- a/src/Backends/Rendering/SDLTexture.cpp
+++ b/src/Backends/Rendering/SDLTexture.cpp
@@ -147,32 +147,32 @@
surface->lost = FALSE;
}
-unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch)
+unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height)
{
if (surface == NULL)
return NULL;
- *pitch = surface->width * 3;
+ *pitch = width * 3;
- surface->pixels = (unsigned char*)calloc(surface->width * surface->height * 3, 1); // Make sure these are initialized
+ surface->pixels = (unsigned char*)malloc(width * height * 3);
return surface->pixels;
}
-void Backend_UnlockSurface(Backend_Surface *surface)
+void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height)
{
if (surface == NULL)
return;
- unsigned char *buffer = (unsigned char*)malloc(surface->width * surface->height * 4);
- unsigned char *buffer_pointer = buffer;
+ unsigned char *buffer = (unsigned char*)malloc(width * height * 4);
const unsigned char *src_pixel = surface->pixels;
// Convert the SDL_Surface's colour-keyed pixels to RGBA32
- for (unsigned int y = 0; y < surface->height; ++y)
+ for (unsigned int y = 0; y < height; ++y)
{
+ unsigned char *buffer_pointer = &buffer[y * width * 4];
- for (unsigned int x = 0; x < surface->width; ++x)
+ for (unsigned int x = 0; x < width; ++x)
{
*buffer_pointer++ = src_pixel[0];
*buffer_pointer++ = src_pixel[1];
@@ -189,7 +189,8 @@
free(surface->pixels);
- SDL_UpdateTexture(surface->texture, NULL, buffer, surface->width * 4);
+ SDL_Rect rect = {0, 0, width, height};
+ SDL_UpdateTexture(surface->texture, &rect, buffer, width * 4);
free(buffer);
}
--- a/src/Backends/Rendering/Software.cpp
+++ b/src/Backends/Rendering/Software.cpp
@@ -112,8 +112,11 @@
(void)surface;
}
-unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch)
+unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height)
{
+ (void)width;
+ (void)height;
+
if (surface == NULL)
return NULL;
@@ -121,9 +124,11 @@
return surface->pixels;
}
-void Backend_UnlockSurface(Backend_Surface *surface)
+void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height)
{
(void)surface;
+ (void)width;
+ (void)height;
}
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
@@ -165,7 +165,7 @@
// IF YOU WANT TO ADD HD SPRITES, THIS IS THE CODE YOU SHOULD EDIT
unsigned int pitch;
- unsigned char *pixels = Backend_LockSurface(surf[surf_no], &pitch);
+ unsigned char *pixels = Backend_LockSurface(surf[surf_no], &pitch, converted_surface->w * magnification, converted_surface->h * magnification);
if (magnification == 1)
{
@@ -206,7 +206,7 @@
}
}
- Backend_UnlockSurface(surf[surf_no]);
+ Backend_UnlockSurface(surf[surf_no], converted_surface->w * magnification, converted_surface->h * magnification);
SDL_FreeSurface(converted_surface);
return TRUE;
--
⑨