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.
This commit is contained in:
parent
ba797d0fea
commit
6b18ec91be
3 changed files with 17 additions and 35 deletions
|
@ -20,8 +20,7 @@ RenderBackend_Surface* RenderBackend_CreateSurface(size_t width, size_t height,
|
||||||
void RenderBackend_FreeSurface(RenderBackend_Surface *surface);
|
void RenderBackend_FreeSurface(RenderBackend_Surface *surface);
|
||||||
bool RenderBackend_IsSurfaceLost(RenderBackend_Surface *surface);
|
bool RenderBackend_IsSurfaceLost(RenderBackend_Surface *surface);
|
||||||
void RenderBackend_RestoreSurface(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_UploadSurface(RenderBackend_Surface *surface, const unsigned char *pixels, size_t width, size_t height);
|
||||||
void RenderBackend_UnlockSurface(RenderBackend_Surface *surface, 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_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);
|
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);
|
RenderBackend_GlyphAtlas* RenderBackend_CreateGlyphAtlas(size_t width, size_t height);
|
||||||
|
|
|
@ -108,23 +108,10 @@ void RenderBackend_RestoreSurface(RenderBackend_Surface *surface)
|
||||||
(void)surface;
|
(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;
|
for (size_t y = 0; y < height; ++y)
|
||||||
(void)height;
|
memcpy(&surface->pixels[y * surface->pitch], &pixels[y * width * 3], width * 3);
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ATTRIBUTE_HOT void RenderBackend_Blit(RenderBackend_Surface *source_surface, const RenderBackend_Rect *rect, RenderBackend_Surface *destination_surface, long x, long y, bool colour_key)
|
ATTRIBUTE_HOT void RenderBackend_Blit(RenderBackend_Surface *source_surface, const RenderBackend_Rect *rect, RenderBackend_Surface *destination_surface, long x, long y, bool colour_key)
|
||||||
|
|
30
src/Draw.cpp
30
src/Draw.cpp
|
@ -1,6 +1,7 @@
|
||||||
#include "Draw.h"
|
#include "Draw.h"
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
@ -150,30 +151,23 @@ void ReleaseSurface(SurfaceID s)
|
||||||
static BOOL ScaleAndUploadSurface(const unsigned char *image_buffer, size_t width, size_t height, SurfaceID surf_no)
|
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
|
// 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)
|
if (mag == 1)
|
||||||
{
|
{
|
||||||
// Just copy the pixels the way they are
|
// Just copy the pixels the way they are
|
||||||
for (size_t y = 0; y < height; ++y)
|
RenderBackend_UploadSurface(surf[surf_no], image_buffer, width, height);
|
||||||
{
|
|
||||||
const unsigned char *src_row = &image_buffer[y * width * 3];
|
|
||||||
unsigned char *dst_row = &pixels[y * pitch];
|
|
||||||
|
|
||||||
memcpy(dst_row, src_row, width * 3);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
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
|
// Upscale the bitmap to the game's internal resolution
|
||||||
for (size_t y = 0; y < height; ++y)
|
for (size_t y = 0; y < height; ++y)
|
||||||
{
|
{
|
||||||
const unsigned char *src_row = &image_buffer[y * width * 3];
|
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;
|
const unsigned char *src_ptr = src_row;
|
||||||
unsigned char *dst_ptr = dst_row;
|
unsigned char *dst_ptr = dst_row;
|
||||||
|
@ -191,11 +185,13 @@ static BOOL ScaleAndUploadSurface(const unsigned char *image_buffer, size_t widt
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 1; i < mag; ++i)
|
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;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue