Skip to content

Commit

Permalink
[cleanup] Fix some MSVC warnings
Browse files Browse the repository at this point in the history
- C4127 conditional expression is constant
- C4189 '%var': local variable is initialized but not referenced
  • Loading branch information
Jarod42 committed Nov 11, 2024
1 parent e0a581b commit 85989b7
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 264 deletions.
173 changes: 77 additions & 96 deletions include/guisan/sdl/sdlpixel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,47 +73,38 @@ namespace gcn
*/
inline const Color SDLgetPixel(SDL_Surface* surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
const int bpp = surface->format->BytesPerPixel;

SDL_LockSurface(surface);

Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
const Uint8* p =
reinterpret_cast<const Uint8*>(surface->pixels) + y * surface->pitch + x * bpp;

unsigned int color = 0;

switch(bpp)
switch (bpp)
{
case 1:
color = *p;
break;

case 2:
color = *(Uint16 *)p;
break;

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
color = p[0] << 16 | p[1] << 8 | p[2];
}
else
{
color = p[0] | p[1] << 8 | p[2] << 16;
}
break;

case 4:
color = *(Uint32 *)p;
break;
case 1: color = *p; break;

case 2: color = *reinterpret_cast<const Uint16*>(p); break;

case 3:
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
color = p[0] << 16 | p[1] << 8 | p[2];
#else
color = p[0] | p[1] << 8 | p[2] << 16;
#endif
break;

case 4: color = *reinterpret_cast<const Uint32*>(p); break;
}

unsigned char r,g,b,a;
unsigned char r, g, b, a;

SDL_GetRGBA(color, surface->format, &r, &g, &b, &a);
SDL_UnlockSurface(surface);

return Color(r,g,b,a);
return Color(r, g, b, a);
}

/**
Expand All @@ -125,42 +116,33 @@ namespace gcn
*/
inline void SDLputPixel(SDL_Surface* surface, int x, int y, const Color& color)
{
int bpp = surface->format->BytesPerPixel;
const int bpp = surface->format->BytesPerPixel;

SDL_LockSurface(surface);

Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
Uint8* p = reinterpret_cast<Uint8*>(surface->pixels) + y * surface->pitch + x * bpp;

Uint32 pixel = SDL_MapRGB(surface->format, color.r, color.g, color.b);

switch(bpp)
switch (bpp)
{
case 1:
*p = pixel;
break;

case 2:
*(Uint16 *)p = pixel;
break;

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
}
else
{
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;

case 4:
*(Uint32 *)p = pixel;
break;
case 1: *p = pixel; break;

case 2: *reinterpret_cast<Uint16*>(p) = pixel; break;

case 3:
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
p[0] = static_cast<Uint8>((pixel >> 16) & 0xff);
p[1] = static_cast<Uint8>((pixel >> 8) & 0xff);
p[2] = static_cast<Uint8>(pixel & 0xff);
#else
p[0] = static_cast<Uint8>(pixel & 0xff);
p[1] = static_cast<Uint8>((pixel >> 8) & 0xff);
p[2] = static_cast<Uint8>((pixel >> 16) & 0xff);
#endif
break;

case 4: *reinterpret_cast<Uint32*>(p) = pixel; break;
}

SDL_UnlockSurface(surface);
Expand Down Expand Up @@ -189,13 +171,14 @@ namespace gcn
* @param dst the destination color.
* @param a alpha.
*/
inline unsigned short SDLAlpha16(unsigned short src, unsigned short dst, unsigned char a, const SDL_PixelFormat *f)
inline unsigned short
SDLAlpha16(unsigned short src, unsigned short dst, unsigned char a, const SDL_PixelFormat* f)
{
unsigned int b = ((src & f->Rmask) * a + (dst & f->Rmask) * (255 - a)) >> 8;
unsigned int g = ((src & f->Gmask) * a + (dst & f->Gmask) * (255 - a)) >> 8;
unsigned int r = ((src & f->Bmask) * a + (dst & f->Bmask) * (255 - a)) >> 8;

return (unsigned short)((b & f->Rmask) | (g & f->Gmask) | (r & f->Bmask));
return (unsigned short) ((b & f->Rmask) | (g & f->Gmask) | (r & f->Bmask));
}

/*
Expand All @@ -220,54 +203,52 @@ namespace gcn
*/
inline void SDLputPixelAlpha(SDL_Surface* surface, int x, int y, const Color& color)
{
int bpp = surface->format->BytesPerPixel;
const int bpp = surface->format->BytesPerPixel;

SDL_LockSurface(surface);

Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
Uint8* p = reinterpret_cast<Uint8*>(surface->pixels) + y * surface->pitch + x * bpp;

Uint32 pixel = SDL_MapRGB(surface->format, color.r, color.g, color.b);

switch(bpp)
switch (bpp)
{
case 1:
*p = pixel;
break;

case 2:
*(Uint16 *)p = SDLAlpha16(pixel, *(Uint32 *)p, color.a, surface->format);
break;

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
unsigned int r = (p[0] * (255 - color.a) + color.r * color.a) >> 8;
unsigned int g = (p[1] * (255 - color.a) + color.g * color.a) >> 8;
unsigned int b = (p[2] * (255 - color.a) + color.b * color.a) >> 8;

p[2] = b;
p[1] = g;
p[0] = r;
}
else
{
unsigned int r = (p[2] * (255 - color.a) + color.r * color.a) >> 8;
unsigned int g = (p[1] * (255 - color.a) + color.g * color.a) >> 8;
unsigned int b = (p[0] * (255 - color.a) + color.b * color.a) >> 8;

p[0] = b;
p[1] = g;
p[2] = r;
}
break;

case 4:
*(Uint32 *)p = SDLAlpha32(pixel, *(Uint32 *)p, color.a);
break;
case 1: *p = pixel; break;

case 2:
*reinterpret_cast<Uint16*>(p) =
SDLAlpha16(pixel, *reinterpret_cast<Uint32*>(p), color.a, surface->format);
break;

case 3:
{
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
const unsigned int r = (p[0] * (255 - color.a) + color.r * color.a) >> 8;
const unsigned int g = (p[1] * (255 - color.a) + color.g * color.a) >> 8;
const unsigned int b = (p[2] * (255 - color.a) + color.b * color.a) >> 8;

p[2] = b;
p[1] = g;
p[0] = r;
#else
const unsigned int r = (p[2] * (255 - color.a) + color.r * color.a) >> 8;
const unsigned int g = (p[1] * (255 - color.a) + color.g * color.a) >> 8;
const unsigned int b = (p[0] * (255 - color.a) + color.b * color.a) >> 8;

p[0] = b;
p[1] = g;
p[2] = r;
#endif
break;
}
case 4:
*reinterpret_cast<Uint32*>(p) =
SDLAlpha32(pixel, *reinterpret_cast<Uint32*>(p), color.a);
break;
}

SDL_UnlockSurface(surface);
}
}
} // namespace gcn

#endif // end GCN_SDLPIXEL_HPP
4 changes: 2 additions & 2 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ workspace "Guisan"
warnings "Extra"
--flags { "FatalWarnings"}

vpaths {['*'] = '*'}

filter { "action:vs*" }
nuget(nugetPackages)
defines { "_CRT_SECURE_NO_WARNINGS" } -- 4996: '$func': This function or variable may be unsafe. Consider using $func2 instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
Expand All @@ -67,8 +69,6 @@ workspace "Guisan"
disablewarnings {
"4018", -- '<': signed/unsigned mismatch
"4100", -- '%var': unreferenced formal parameter
"4127", -- conditional expression is constant
"4189", -- '%var': local variable is initialized but not referenced
"4244", -- '=': conversion from '$type1' to '$type2', possible loss of data
"4458", -- declaration of '$var' hides class member
}
Expand Down
2 changes: 1 addition & 1 deletion src/imagefont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ namespace gcn
int y = 0;
for (int i = glyphsFrom; i < glyphsTo + 1; i++)
{
mGlyph[i] = scanForGlyph(i, x, y, separator);
mGlyph[i] = scanForGlyph(static_cast<unsigned char>(i), x, y, separator);
// Update x and y with new coordinates.
x = mGlyph[i].x + mGlyph[i].width;
y = mGlyph[i].y;
Expand Down
Loading

0 comments on commit 85989b7

Please sign in to comment.