mirror of
https://github.com/TerryCavanagh/VVVVVV.git
synced 2024-12-22 17:49:43 +01:00
Add inbounds pixel assertion to DrawPixel/ReadPixel
I was running the game through Valgrind and I noticed a memory error where the game was attempting to read a pixel that was just outside the image. Since this is an error that doesn't immediately result in a segfault, I figured that it would be prudent to put in an assertion to make it loud and clear that a memory error is, in fact, happening here. Similarly, drawing to a pixel just outside the surface wouldn't result in a crash, so I copy-pasted the check there too (with changes).
This commit is contained in:
parent
e1e5e43089
commit
6f435661e7
1 changed files with 29 additions and 1 deletions
|
@ -81,6 +81,20 @@ SDL_Surface* GetSubSurface( SDL_Surface* metaSurface, int x, int y, int width, i
|
|||
|
||||
void DrawPixel(SDL_Surface* surface, const int x, const int y, const SDL_Color color)
|
||||
{
|
||||
const SDL_Point point = {x, y};
|
||||
const SDL_Rect rect = {0, 0, surface->w, surface->h};
|
||||
const bool inbounds = SDL_PointInRect(&point, &rect);
|
||||
if (!inbounds)
|
||||
{
|
||||
WHINE_ONCE_ARGS((
|
||||
"Pixel draw is not inbounds: "
|
||||
"Attempted to draw to %i,%i in %ix%i surface",
|
||||
x, y, surface->w, surface->h
|
||||
));
|
||||
SDL_assert(0 && "Pixel draw is not inbounds!");
|
||||
return;
|
||||
}
|
||||
|
||||
const SDL_PixelFormat* fmt = surface->format;
|
||||
const int bpp = fmt->BytesPerPixel;
|
||||
Uint32* pixel = (Uint32*) ((Uint8*) surface->pixels + y * surface->pitch + x * bpp);
|
||||
|
@ -100,10 +114,24 @@ void DrawPixel(SDL_Surface* surface, const int x, const int y, const SDL_Color c
|
|||
|
||||
SDL_Color ReadPixel(const SDL_Surface* surface, const int x, const int y)
|
||||
{
|
||||
SDL_Color color = {0, 0, 0, 0};
|
||||
const SDL_Point point = {x, y};
|
||||
const SDL_Rect rect = {0, 0, surface->w, surface->h};
|
||||
const bool inbounds = SDL_PointInRect(&point, &rect);
|
||||
if (!inbounds)
|
||||
{
|
||||
WHINE_ONCE_ARGS((
|
||||
"Pixel read is not inbounds: "
|
||||
"Attempted to read %i,%i in %ix%i surface",
|
||||
x, y, surface->w, surface->h
|
||||
));
|
||||
SDL_assert(0 && "Pixel read is not inbounds!");
|
||||
return color;
|
||||
}
|
||||
|
||||
const SDL_PixelFormat* fmt = surface->format;
|
||||
const int bpp = surface->format->BytesPerPixel;
|
||||
const Uint32* pixel = (Uint32*) ((Uint8*) surface->pixels + y * surface->pitch + x * bpp);
|
||||
SDL_Color color = {0, 0, 0, 0};
|
||||
|
||||
switch (bpp)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue