From 74c9931ebbfb73c013a808c17e07f277752dc2d3 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Fri, 19 Jul 2019 08:45:59 +0100 Subject: [PATCH] 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. --- src/Backends/Rendering.h | 3 ++- src/Backends/Rendering/SDLSurface.cpp | 14 +++++++------- src/Backends/Rendering/SDLTexture.cpp | 16 +++++++--------- src/Backends/Rendering/Software.cpp | 16 ++++++++-------- src/Draw.cpp | 27 ++++++++++++++++----------- 5 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/Backends/Rendering.h b/src/Backends/Rendering.h index 5670ac78..e3632006 100644 --- a/src/Backends/Rendering.h +++ b/src/Backends/Rendering.h @@ -13,7 +13,8 @@ void Backend_Deinit(void); 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); diff --git a/src/Backends/Rendering/SDLSurface.cpp b/src/Backends/Rendering/SDLSurface.cpp index 79218bc0..c7e173f6 100644 --- a/src/Backends/Rendering/SDLSurface.cpp +++ b/src/Backends/Rendering/SDLSurface.cpp @@ -82,15 +82,15 @@ void Backend_FreeSurface(Backend_Surface *surface) 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) diff --git a/src/Backends/Rendering/SDLTexture.cpp b/src/Backends/Rendering/SDLTexture.cpp index a35770e4..0c962888 100644 --- a/src/Backends/Rendering/SDLTexture.cpp +++ b/src/Backends/Rendering/SDLTexture.cpp @@ -34,7 +34,7 @@ static void FlushSurface(Backend_Surface *surface) { 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 @@ void Backend_FreeSurface(Backend_Surface *surface) 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; - - memcpy(dst_row, src_row, width * 3); - } + *pitch = surface->sdl_surface->pitch; + return (unsigned char*)surface->sdl_surface->pixels; +} +void Backend_Unlock(Backend_Surface *surface) +{ surface->needs_syncing = TRUE; } diff --git a/src/Backends/Rendering/Software.cpp b/src/Backends/Rendering/Software.cpp index 6f6f1612..66d22946 100644 --- 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 @@ void Backend_FreeSurface(Backend_Surface *surface) 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) diff --git a/src/Draw.cpp b/src/Draw.cpp index 20e6569a..c8f7a32c 100644 --- a/src/Draw.cpp +++ b/src/Draw.cpp @@ -211,25 +211,32 @@ static BOOL LoadBitmap(SDL_RWops *fp, Surface_Ids surf_no, BOOL create_surface) 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 @@ static BOOL LoadBitmap(SDL_RWops *fp, Surface_Ids surf_no, BOOL create_surface) } 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;