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.
This commit is contained in:
Clownacy 2020-01-07 03:51:32 +00:00
parent 664d76f94e
commit a0eb646a1f
6 changed files with 28 additions and 22 deletions

View file

@ -22,8 +22,8 @@ Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height);
void Backend_FreeSurface(Backend_Surface *surface); void Backend_FreeSurface(Backend_Surface *surface);
BOOL Backend_IsSurfaceLost(Backend_Surface *surface); BOOL Backend_IsSurfaceLost(Backend_Surface *surface);
void Backend_RestoreSurface(Backend_Surface *surface); void Backend_RestoreSurface(Backend_Surface *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 Backend_UnlockSurface(Backend_Surface *surface); 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_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); void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue);
BOOL Backend_SupportsSubpixelGlyphs(void); BOOL Backend_SupportsSubpixelGlyphs(void);

View file

@ -474,17 +474,17 @@ void Backend_RestoreSurface(Backend_Surface *surface)
(void)surface; (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) if (surface == NULL)
return NULL; return NULL;
surface->pixels = (unsigned char*)malloc(surface->width * surface->height * 3); surface->pixels = (unsigned char*)malloc(width * height * 3);
*pitch = surface->width * 3; *pitch = width * 3;
return surface->pixels; return surface->pixels;
} }
void Backend_UnlockSurface(Backend_Surface *surface) void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height)
{ {
if (surface == NULL) if (surface == NULL)
return; return;
@ -493,7 +493,7 @@ void Backend_UnlockSurface(Backend_Surface *surface)
glGetIntegerv(GL_TEXTURE_BINDING_2D, &previously_bound_texture); glGetIntegerv(GL_TEXTURE_BINDING_2D, &previously_bound_texture);
glBindTexture(GL_TEXTURE_2D, surface->texture_id); 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); free(surface->pixels);
glBindTexture(GL_TEXTURE_2D, previously_bound_texture); glBindTexture(GL_TEXTURE_2D, previously_bound_texture);

View file

@ -105,7 +105,7 @@ void Backend_RestoreSurface(Backend_Surface *surface)
(void)surface; (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) if (surface == NULL)
return NULL; return NULL;
@ -114,7 +114,7 @@ unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch
return (unsigned char*)surface->sdlsurface->pixels; 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; (void)surface;
} }

View file

@ -147,32 +147,32 @@ void Backend_RestoreSurface(Backend_Surface *surface)
surface->lost = FALSE; 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) if (surface == NULL)
return 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; return surface->pixels;
} }
void Backend_UnlockSurface(Backend_Surface *surface) void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height)
{ {
if (surface == NULL) if (surface == NULL)
return; return;
unsigned char *buffer = (unsigned char*)malloc(surface->width * surface->height * 4); unsigned char *buffer = (unsigned char*)malloc(width * height * 4);
unsigned char *buffer_pointer = buffer;
const unsigned char *src_pixel = surface->pixels; const unsigned char *src_pixel = surface->pixels;
// Convert the SDL_Surface's colour-keyed pixels to RGBA32 // 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[0];
*buffer_pointer++ = src_pixel[1]; *buffer_pointer++ = src_pixel[1];
@ -189,7 +189,8 @@ void Backend_UnlockSurface(Backend_Surface *surface)
free(surface->pixels); 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); free(buffer);
} }

View file

@ -112,8 +112,11 @@ void Backend_RestoreSurface(Backend_Surface *surface)
(void)surface; (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) if (surface == NULL)
return NULL; return NULL;
@ -121,9 +124,11 @@ unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch
return surface->pixels; return surface->pixels;
} }
void Backend_UnlockSurface(Backend_Surface *surface) void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height)
{ {
(void)surface; (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) void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key)

View file

@ -165,7 +165,7 @@ static BOOL ScaleAndUploadSurface(SDL_Surface *surface, SurfaceID surf_no)
// IF YOU WANT TO ADD HD SPRITES, THIS IS THE CODE YOU SHOULD EDIT // IF YOU WANT TO ADD HD SPRITES, THIS IS THE CODE YOU SHOULD EDIT
unsigned int pitch; 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) if (magnification == 1)
{ {
@ -206,7 +206,7 @@ static BOOL ScaleAndUploadSurface(SDL_Surface *surface, SurfaceID surf_no)
} }
} }
Backend_UnlockSurface(surf[surf_no]); Backend_UnlockSurface(surf[surf_no], converted_surface->w * magnification, converted_surface->h * magnification);
SDL_FreeSurface(converted_surface); SDL_FreeSurface(converted_surface);
return TRUE; return TRUE;