2021-03-06 05:40:13 +01:00
|
|
|
#include "GraphicsUtil.h"
|
|
|
|
|
|
|
|
#include <SDL.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-12-01 07:30:16 +01:00
|
|
|
#include "Alloc.h"
|
2020-01-01 21:29:24 +01:00
|
|
|
#include "Graphics.h"
|
2021-03-06 05:40:13 +01:00
|
|
|
#include "Maths.h"
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void setRect( SDL_Rect& _r, int x, int y, int w, int h )
|
|
|
|
{
|
|
|
|
_r.x = x;
|
|
|
|
_r.y = y;
|
|
|
|
_r.w = w;
|
|
|
|
_r.h = h;
|
|
|
|
}
|
|
|
|
|
2021-03-06 05:12:01 +01:00
|
|
|
static SDL_Surface* RecreateSurfaceWithDimensions(
|
|
|
|
SDL_Surface* surface,
|
|
|
|
const int width,
|
|
|
|
const int height
|
|
|
|
) {
|
|
|
|
SDL_Surface* retval;
|
Copy blend mode to recreated surface
This fixes a "root cause" bug (that's existed since 2.2 and below) where
recreated surfaces wouldn't preserve the blend mode of their original
surface.
The surface-level (pun genuinely unintended) bug that this root bug
fixes is the one where there's no background to the room name during the
map menu animation in Flip Mode.
This is because the room name background relies on graphics.backBuffer
being filled with complete black. This is achieved by a call to
ClearSurface() - however, ClearSurface() actually fills it with
transparent black (this is not a regression; in 2.2 and previous, this
was an "inlined" FillRect(backBuffer, 0x00000000)). This would be okay,
and indeed the room name background renders fine in unflipped mode - but
it suddenly breaks in Flip Mode.
Why? Because backBuffer gets fed through FlipSurfaceVerticle(), and
FlipSurfaceVerticle() creates a temporary surface with the same
dimensions and color masks as backBuffer - it, however, does NOT create
it with the same blend mode, and kind of sort of just forgets that the
original was SDL_BLENDMODE_NONE; the new surface is SDL_BLENDMODE_BLEND.
Thus, transparency applies on the new surface, and instead of the room
name being drawn against black, it gets drawn against transparency.
2021-03-06 05:17:56 +01:00
|
|
|
SDL_BlendMode blend_mode;
|
2021-03-06 05:12:01 +01:00
|
|
|
|
|
|
|
if (surface == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = SDL_CreateRGBSurface(
|
|
|
|
surface->flags,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
surface->format->BitsPerPixel,
|
|
|
|
surface->format->Rmask,
|
|
|
|
surface->format->Gmask,
|
|
|
|
surface->format->Bmask,
|
|
|
|
surface->format->Amask
|
|
|
|
);
|
|
|
|
|
|
|
|
if (retval == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
Copy blend mode to recreated surface
This fixes a "root cause" bug (that's existed since 2.2 and below) where
recreated surfaces wouldn't preserve the blend mode of their original
surface.
The surface-level (pun genuinely unintended) bug that this root bug
fixes is the one where there's no background to the room name during the
map menu animation in Flip Mode.
This is because the room name background relies on graphics.backBuffer
being filled with complete black. This is achieved by a call to
ClearSurface() - however, ClearSurface() actually fills it with
transparent black (this is not a regression; in 2.2 and previous, this
was an "inlined" FillRect(backBuffer, 0x00000000)). This would be okay,
and indeed the room name background renders fine in unflipped mode - but
it suddenly breaks in Flip Mode.
Why? Because backBuffer gets fed through FlipSurfaceVerticle(), and
FlipSurfaceVerticle() creates a temporary surface with the same
dimensions and color masks as backBuffer - it, however, does NOT create
it with the same blend mode, and kind of sort of just forgets that the
original was SDL_BLENDMODE_NONE; the new surface is SDL_BLENDMODE_BLEND.
Thus, transparency applies on the new surface, and instead of the room
name being drawn against black, it gets drawn against transparency.
2021-03-06 05:17:56 +01:00
|
|
|
SDL_GetSurfaceBlendMode(surface, &blend_mode);
|
|
|
|
SDL_SetSurfaceBlendMode(retval, blend_mode);
|
|
|
|
|
2021-03-06 05:12:01 +01:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_Surface* RecreateSurface(SDL_Surface* surface)
|
|
|
|
{
|
|
|
|
if (surface == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RecreateSurfaceWithDimensions(
|
|
|
|
surface,
|
|
|
|
surface->w,
|
|
|
|
surface->h
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-01 21:29:24 +01:00
|
|
|
SDL_Surface* GetSubSurface( SDL_Surface* metaSurface, int x, int y, int width, int height )
|
|
|
|
{
|
|
|
|
// Create an SDL_Rect with the area of the _surface
|
|
|
|
SDL_Rect area;
|
|
|
|
area.x = x;
|
|
|
|
area.y = y;
|
|
|
|
area.w = width;
|
|
|
|
area.h = height;
|
|
|
|
|
|
|
|
//Convert to the correct display format after nabbing the new _surface or we will slow things down.
|
2021-03-06 05:12:01 +01:00
|
|
|
SDL_Surface* preSurface = RecreateSurfaceWithDimensions(
|
|
|
|
metaSurface,
|
2020-08-23 03:52:42 +02:00
|
|
|
width,
|
2021-03-06 05:12:01 +01:00
|
|
|
height
|
2020-08-23 03:52:42 +02:00
|
|
|
);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
// Lastly, apply the area from the meta _surface onto the whole of the sub _surface.
|
|
|
|
SDL_BlitSurface(metaSurface, &area, preSurface, 0);
|
|
|
|
|
|
|
|
// Return the new Bitmap _surface
|
|
|
|
return preSurface;
|
|
|
|
}
|
|
|
|
|
2021-01-10 18:14:37 +01:00
|
|
|
static void DrawPixel( SDL_Surface *_surface, int x, int y, Uint32 pixel )
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
int bpp = _surface->format->BytesPerPixel;
|
|
|
|
/* Here p is the address to the pixel we want to set */
|
|
|
|
Uint8 *p = (Uint8 *)_surface->pixels + y * _surface->pitch + x * bpp;
|
|
|
|
|
|
|
|
switch(bpp)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
*p = pixel;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
*(Uint16 *)p = pixel;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2020-08-22 20:19:12 +02:00
|
|
|
p[0] = (pixel >> 16) & 0xff;
|
|
|
|
p[1] = (pixel >> 8) & 0xff;
|
|
|
|
p[2] = pixel & 0xff;
|
2020-01-01 21:29:24 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
*(Uint32 *)p = pixel;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Uint32 ReadPixel( SDL_Surface *_surface, int x, int y )
|
|
|
|
{
|
|
|
|
int bpp = _surface->format->BytesPerPixel;
|
|
|
|
/* Here p is the address to the pixel we want to retrieve */
|
|
|
|
Uint8 *p = (Uint8 *)_surface->pixels + y * _surface->pitch + x * bpp;
|
|
|
|
|
|
|
|
switch(bpp)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
return *p;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
return *(Uint16 *)p;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2020-08-22 20:19:12 +02:00
|
|
|
return p[0] | p[1] << 8 | p[2] << 16;
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
case 4:
|
|
|
|
return *(Uint32 *)p;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0; /* shouldn't happen, but avoids warnings */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Surface * ScaleSurface( SDL_Surface *_surface, int Width, int Height, SDL_Surface * Dest )
|
|
|
|
{
|
|
|
|
if(!_surface || !Width || !Height)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
SDL_Surface *_ret;
|
|
|
|
if(Dest == NULL)
|
|
|
|
{
|
2021-03-06 05:12:01 +01:00
|
|
|
_ret = RecreateSurfaceWithDimensions(_surface, Width, Height);
|
2020-01-01 21:29:24 +01:00
|
|
|
if(_ret == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_ret = Dest;
|
|
|
|
}
|
|
|
|
|
2021-09-06 05:06:28 +02:00
|
|
|
SDL_BlitScaled(_surface, NULL, _ret, NULL);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
return _ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Surface * FlipSurfaceVerticle(SDL_Surface* _src)
|
|
|
|
{
|
2021-03-06 05:12:01 +01:00
|
|
|
SDL_Surface * ret = RecreateSurface(_src);
|
2020-04-02 22:35:43 +02:00
|
|
|
if(ret == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
for(Sint32 y = 0; y < _src->h; y++)
|
|
|
|
{
|
|
|
|
for(Sint32 x = 0; x < _src->w; x++)
|
|
|
|
{
|
|
|
|
DrawPixel(ret, x ,(_src->h-1) - y ,ReadPixel(_src, x, y));
|
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
return ret;
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlitSurfaceStandard( SDL_Surface* _src, SDL_Rect* _srcRect, SDL_Surface* _dest, SDL_Rect* _destRect )
|
|
|
|
{
|
|
|
|
SDL_BlitSurface( _src, _srcRect, _dest, _destRect );
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlitSurfaceColoured(
|
|
|
|
SDL_Surface* _src,
|
|
|
|
SDL_Rect* _srcRect,
|
|
|
|
SDL_Surface* _dest,
|
|
|
|
SDL_Rect* _destRect,
|
|
|
|
colourTransform& ct
|
|
|
|
) {
|
|
|
|
SDL_Rect *tempRect = _destRect;
|
|
|
|
|
|
|
|
const SDL_PixelFormat& fmt = *(_src->format);
|
|
|
|
|
2021-03-06 05:12:01 +01:00
|
|
|
SDL_Surface* tempsurface = RecreateSurface(_src);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
for(int x = 0; x < tempsurface->w; x++)
|
|
|
|
{
|
|
|
|
for(int y = 0; y < tempsurface->h; y++)
|
|
|
|
{
|
|
|
|
Uint32 pixel = ReadPixel(_src, x, y);
|
|
|
|
Uint32 Alpha = pixel & fmt.Amask;
|
|
|
|
Uint32 result = ct.colour & 0x00FFFFFF;
|
2020-02-10 03:23:12 +01:00
|
|
|
Uint32 CTAlpha = ct.colour & fmt.Amask;
|
|
|
|
float div1 = ((Alpha >> 24) / 255.0f);
|
|
|
|
float div2 = ((CTAlpha >> 24) / 255.0f);
|
|
|
|
Uint32 UseAlpha = (div1 * div2) * 255.0f;
|
|
|
|
DrawPixel(tempsurface, x, y, result | (UseAlpha << 24));
|
2020-06-30 00:05:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_BlitSurface(tempsurface, _srcRect, _dest, tempRect);
|
2022-12-01 07:30:16 +01:00
|
|
|
VVV_freefunc(SDL_FreeSurface, tempsurface);
|
2020-06-30 00:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlitSurfaceTinted(
|
|
|
|
SDL_Surface* _src,
|
|
|
|
SDL_Rect* _srcRect,
|
|
|
|
SDL_Surface* _dest,
|
|
|
|
SDL_Rect* _destRect,
|
|
|
|
colourTransform& ct
|
|
|
|
) {
|
|
|
|
SDL_Rect *tempRect = _destRect;
|
|
|
|
|
|
|
|
const SDL_PixelFormat& fmt = *(_src->format);
|
|
|
|
|
2021-03-06 05:12:01 +01:00
|
|
|
SDL_Surface* tempsurface = RecreateSurface(_src);
|
2020-06-30 00:05:57 +02:00
|
|
|
|
|
|
|
for (int x = 0; x < tempsurface->w; x++) {
|
|
|
|
for (int y = 0; y < tempsurface->h; y++) {
|
|
|
|
Uint32 pixel = ReadPixel(_src, x, y);
|
|
|
|
|
|
|
|
Uint8 pixred = (pixel & _src->format->Rmask) >> 16;
|
|
|
|
Uint8 pixgreen = (pixel & _src->format->Gmask) >> 8;
|
|
|
|
Uint8 pixblue = (pixel & _src->format->Bmask) >> 0;
|
|
|
|
|
|
|
|
double temp_pixred = pixred * 0.299;
|
|
|
|
double temp_pixgreen = pixgreen * 0.587;
|
|
|
|
double temp_pixblue = pixblue * 0.114;
|
|
|
|
|
2021-03-06 05:46:19 +01:00
|
|
|
double gray = SDL_floor((temp_pixred + temp_pixgreen + temp_pixblue + 0.5));
|
2020-06-30 00:05:57 +02:00
|
|
|
|
|
|
|
Uint8 ctred = (ct.colour & graphics.backBuffer->format->Rmask) >> 16;
|
|
|
|
Uint8 ctgreen = (ct.colour & graphics.backBuffer->format->Gmask) >> 8;
|
|
|
|
Uint8 ctblue = (ct.colour & graphics.backBuffer->format->Bmask) >> 0;
|
|
|
|
|
|
|
|
temp_pixred = gray * ctred / 255.0;
|
|
|
|
temp_pixgreen = gray * ctgreen / 255.0;
|
|
|
|
temp_pixblue = gray * ctblue / 255.0;
|
|
|
|
|
|
|
|
if (temp_pixred > 255)
|
|
|
|
temp_pixred = 255;
|
|
|
|
if (temp_pixgreen > 255)
|
|
|
|
temp_pixgreen = 255;
|
|
|
|
if (temp_pixblue > 255)
|
|
|
|
temp_pixblue = 255;
|
|
|
|
|
|
|
|
pixred = temp_pixred;
|
|
|
|
pixgreen = temp_pixgreen;
|
|
|
|
pixblue = temp_pixblue;
|
|
|
|
|
|
|
|
Uint32 Alpha = pixel & fmt.Amask;
|
|
|
|
Uint32 result = (pixred << 16) + (pixgreen << 8) + (pixblue << 0);
|
|
|
|
Uint32 CTAlpha = ct.colour & fmt.Amask;
|
|
|
|
float div1 = ((Alpha >> 24) / 255.0f);
|
|
|
|
float div2 = ((CTAlpha >> 24) / 255.0f);
|
|
|
|
Uint32 UseAlpha = (div1 * div2) * 255.0f;
|
|
|
|
|
|
|
|
DrawPixel(tempsurface, x, y, result | (UseAlpha << 24));
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_BlitSurface(tempsurface, _srcRect, _dest, tempRect);
|
2022-12-01 07:30:16 +01:00
|
|
|
VVV_freefunc(SDL_FreeSurface, tempsurface);
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-10 18:14:37 +01:00
|
|
|
static int oldscrollamount = 0;
|
|
|
|
static int scrollamount = 0;
|
|
|
|
static bool isscrolling = 0;
|
2020-04-02 22:35:43 +02:00
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
void UpdateFilter(void)
|
2020-05-03 05:27:47 +02:00
|
|
|
{
|
2020-04-02 22:35:43 +02:00
|
|
|
if (rand() % 4000 < 8)
|
|
|
|
{
|
|
|
|
isscrolling = true;
|
|
|
|
}
|
|
|
|
|
2020-11-08 01:21:30 +01:00
|
|
|
oldscrollamount = scrollamount;
|
2020-04-02 22:35:43 +02:00
|
|
|
if(isscrolling == true)
|
|
|
|
{
|
|
|
|
scrollamount += 20;
|
|
|
|
if(scrollamount > 240)
|
|
|
|
{
|
|
|
|
scrollamount = 0;
|
2020-11-08 01:21:30 +01:00
|
|
|
oldscrollamount = 0;
|
2020-04-02 22:35:43 +02:00
|
|
|
isscrolling = false;
|
|
|
|
}
|
|
|
|
}
|
2020-05-03 05:27:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Surface* ApplyFilter( SDL_Surface* _src )
|
|
|
|
{
|
2021-03-06 05:12:01 +01:00
|
|
|
SDL_Surface* _ret = RecreateSurface(_src);
|
2020-04-02 22:35:43 +02:00
|
|
|
|
|
|
|
int redOffset = rand() % 4;
|
|
|
|
|
|
|
|
for(int x = 0; x < _src->w; x++)
|
|
|
|
{
|
|
|
|
for(int y = 0; y < _src->h; y++)
|
|
|
|
{
|
2020-11-08 01:21:30 +01:00
|
|
|
int sampley = (y + (int) graphics.lerp(oldscrollamount, scrollamount) )% 240;
|
2020-04-02 22:35:43 +02:00
|
|
|
|
|
|
|
Uint32 pixel = ReadPixel(_src, x,sampley);
|
|
|
|
|
|
|
|
Uint8 green = (pixel & _src->format->Gmask) >> 8;
|
|
|
|
Uint8 blue = (pixel & _src->format->Bmask) >> 0;
|
|
|
|
|
Remove `VVV_min/max` in favor of `SDL_min/max`
VVV_min/max are functions that only operate on ints, and SDL_min/max are
macros that operate on any type but double-evaluate everything.
I know I more-or-less said earlier that SDL_min/max were dumb but I've
changed my mind and think it's better to use them, taking care to make
sure you don't double-evaluate, rather than trying to generate your own
litany of functions with either your own hand-rolled generation macros,
C++ templates, C11 generics, or GCC extensions (that last one you'd
technically use in a macro but it doesn't really matter), all of which
have more downsides than just not double-evaluating.
And the upside of not double-evaluating is that you're disencouraged
from having really complicated single-line min/max expressions and
encouraged to precompute the values beforehand anyway so the final
min/max is more readable. And furthermore you'll notice when you
yourself end up doing double-evaluations anyway. I removed a couple
instances of Graphics::len() being double-evaluated in this commit (as
well as cleaned up some other min/max-using code). Although the only
downside to those double-evaluations was unnecessary computation,
rather than checking the wrong result or having multiple side effects,
thankfully, it's still good to minimize double-evaluations where
possible.
2021-12-23 01:43:31 +01:00
|
|
|
Uint32 pixelOffset = ReadPixel(_src, SDL_min(x+redOffset, 319), sampley) ;
|
2020-04-02 22:35:43 +02:00
|
|
|
Uint8 red = (pixelOffset & _src->format->Rmask) >> 16 ;
|
|
|
|
|
Remove `VVV_min/max` in favor of `SDL_min/max`
VVV_min/max are functions that only operate on ints, and SDL_min/max are
macros that operate on any type but double-evaluate everything.
I know I more-or-less said earlier that SDL_min/max were dumb but I've
changed my mind and think it's better to use them, taking care to make
sure you don't double-evaluate, rather than trying to generate your own
litany of functions with either your own hand-rolled generation macros,
C++ templates, C11 generics, or GCC extensions (that last one you'd
technically use in a macro but it doesn't really matter), all of which
have more downsides than just not double-evaluating.
And the upside of not double-evaluating is that you're disencouraged
from having really complicated single-line min/max expressions and
encouraged to precompute the values beforehand anyway so the final
min/max is more readable. And furthermore you'll notice when you
yourself end up doing double-evaluations anyway. I removed a couple
instances of Graphics::len() being double-evaluated in this commit (as
well as cleaned up some other min/max-using code). Although the only
downside to those double-evaluations was unnecessary computation,
rather than checking the wrong result or having multiple side effects,
thankfully, it's still good to minimize double-evaluations where
possible.
2021-12-23 01:43:31 +01:00
|
|
|
double mult;
|
2021-12-23 06:49:08 +01:00
|
|
|
int tmp; /* needed to avoid char overflow */
|
2020-04-02 22:35:43 +02:00
|
|
|
if(isscrolling && sampley > 220 && ((rand() %10) < 4))
|
|
|
|
{
|
Remove `VVV_min/max` in favor of `SDL_min/max`
VVV_min/max are functions that only operate on ints, and SDL_min/max are
macros that operate on any type but double-evaluate everything.
I know I more-or-less said earlier that SDL_min/max were dumb but I've
changed my mind and think it's better to use them, taking care to make
sure you don't double-evaluate, rather than trying to generate your own
litany of functions with either your own hand-rolled generation macros,
C++ templates, C11 generics, or GCC extensions (that last one you'd
technically use in a macro but it doesn't really matter), all of which
have more downsides than just not double-evaluating.
And the upside of not double-evaluating is that you're disencouraged
from having really complicated single-line min/max expressions and
encouraged to precompute the values beforehand anyway so the final
min/max is more readable. And furthermore you'll notice when you
yourself end up doing double-evaluations anyway. I removed a couple
instances of Graphics::len() being double-evaluated in this commit (as
well as cleaned up some other min/max-using code). Although the only
downside to those double-evaluations was unnecessary computation,
rather than checking the wrong result or having multiple side effects,
thankfully, it's still good to minimize double-evaluations where
possible.
2021-12-23 01:43:31 +01:00
|
|
|
mult = 0.6;
|
2020-04-02 22:35:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Remove `VVV_min/max` in favor of `SDL_min/max`
VVV_min/max are functions that only operate on ints, and SDL_min/max are
macros that operate on any type but double-evaluate everything.
I know I more-or-less said earlier that SDL_min/max were dumb but I've
changed my mind and think it's better to use them, taking care to make
sure you don't double-evaluate, rather than trying to generate your own
litany of functions with either your own hand-rolled generation macros,
C++ templates, C11 generics, or GCC extensions (that last one you'd
technically use in a macro but it doesn't really matter), all of which
have more downsides than just not double-evaluating.
And the upside of not double-evaluating is that you're disencouraged
from having really complicated single-line min/max expressions and
encouraged to precompute the values beforehand anyway so the final
min/max is more readable. And furthermore you'll notice when you
yourself end up doing double-evaluations anyway. I removed a couple
instances of Graphics::len() being double-evaluated in this commit (as
well as cleaned up some other min/max-using code). Although the only
downside to those double-evaluations was unnecessary computation,
rather than checking the wrong result or having multiple side effects,
thankfully, it's still good to minimize double-evaluations where
possible.
2021-12-23 01:43:31 +01:00
|
|
|
mult = 0.2;
|
2020-04-02 22:35:43 +02:00
|
|
|
}
|
|
|
|
|
2021-12-23 06:49:08 +01:00
|
|
|
tmp = red + fRandom() * mult * 254;
|
|
|
|
red = SDL_min(tmp, 255);
|
|
|
|
tmp = green + fRandom() * mult * 254;
|
|
|
|
green = SDL_min(tmp, 255);
|
|
|
|
tmp = blue + fRandom() * mult * 254;
|
|
|
|
blue = SDL_min(tmp, 255);
|
2020-04-02 22:35:43 +02:00
|
|
|
|
|
|
|
if(y % 2 == 0)
|
|
|
|
{
|
|
|
|
red = static_cast<Uint8>(red / 1.2f);
|
|
|
|
green = static_cast<Uint8>(green / 1.2f);
|
|
|
|
blue = static_cast<Uint8>(blue / 1.2f);
|
|
|
|
}
|
|
|
|
|
2020-11-08 01:23:48 +01:00
|
|
|
int distX = static_cast<int>((SDL_abs (160.0f -x ) / 160.0f) *16);
|
|
|
|
int distY = static_cast<int>((SDL_abs (120.0f -y ) / 120.0f)*32);
|
2020-04-02 22:35:43 +02:00
|
|
|
|
Remove `VVV_min/max` in favor of `SDL_min/max`
VVV_min/max are functions that only operate on ints, and SDL_min/max are
macros that operate on any type but double-evaluate everything.
I know I more-or-less said earlier that SDL_min/max were dumb but I've
changed my mind and think it's better to use them, taking care to make
sure you don't double-evaluate, rather than trying to generate your own
litany of functions with either your own hand-rolled generation macros,
C++ templates, C11 generics, or GCC extensions (that last one you'd
technically use in a macro but it doesn't really matter), all of which
have more downsides than just not double-evaluating.
And the upside of not double-evaluating is that you're disencouraged
from having really complicated single-line min/max expressions and
encouraged to precompute the values beforehand anyway so the final
min/max is more readable. And furthermore you'll notice when you
yourself end up doing double-evaluations anyway. I removed a couple
instances of Graphics::len() being double-evaluated in this commit (as
well as cleaned up some other min/max-using code). Although the only
downside to those double-evaluations was unnecessary computation,
rather than checking the wrong result or having multiple side effects,
thankfully, it's still good to minimize double-evaluations where
possible.
2021-12-23 01:43:31 +01:00
|
|
|
red = SDL_max(red - ( distX +distY), 0);
|
|
|
|
green = SDL_max(green - ( distX +distY), 0);
|
|
|
|
blue = SDL_max(blue - ( distX +distY), 0);
|
2020-04-02 22:35:43 +02:00
|
|
|
|
|
|
|
Uint32 finalPixel = ((red<<16) + (green<<8) + (blue<<0)) | (pixel &_src->format->Amask);
|
|
|
|
DrawPixel(_ret,x,y, finalPixel);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _ret;
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FillRect( SDL_Surface* _surface, const int _x, const int _y, const int _w, const int _h, const int r, int g, int b )
|
|
|
|
{
|
Remove unnecessary Sint16 casts
These casts are sprinkled all throughout the graphics code when creating
and initializing an SDL_Rect on the same line. Unfortunately, most of
these are unnecessary, and at worst are wasteful because they result in
narrowing a 4-byte integer into a 2-byte one when they don't need to
(SDL_Rects are made up of 4-byte integers).
Now, removing them reveals why they were placed there in the first place
- a warning is raised (-Wnarrowing) that implicit narrowing conversions
are prohibited in initializer lists in C++11. (Notably, if the
conversion wasn't narrowing, or implicit, or done in an initializer
list, it would be fine. This is a really specific prohibition that
doesn't apply if any of its sub-cases are true.)
We don't use C++11, but this warning can be easily vanquished by a
simple explicit cast to int (similar to the error of implicitly
converting void* to any other pointer in C++, which works just fine in
C), and we only need to do it when the warning is raised (not every
single time we make an SDL_Rect), so there we go.
2021-04-18 20:13:12 +02:00
|
|
|
SDL_Rect rect = {_x, _y, _w, _h};
|
2021-02-26 00:36:02 +01:00
|
|
|
Uint32 color = SDL_MapRGB(_surface->format, r, g, b);
|
2020-01-01 21:29:24 +01:00
|
|
|
SDL_FillRect(_surface, &rect, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FillRect( SDL_Surface* _surface, const int r, int g, int b )
|
|
|
|
{
|
2021-02-26 00:34:59 +01:00
|
|
|
Uint32 color = SDL_MapRGB(_surface->format, r, g, b);
|
|
|
|
SDL_FillRect(_surface, NULL, color);
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FillRect( SDL_Surface* _surface, const int color )
|
|
|
|
{
|
2021-02-26 00:34:59 +01:00
|
|
|
SDL_FillRect(_surface, NULL, color);
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FillRect( SDL_Surface* _surface, const int x, const int y, const int w, const int h, int rgba )
|
|
|
|
{
|
Remove unnecessary Sint16 casts
These casts are sprinkled all throughout the graphics code when creating
and initializing an SDL_Rect on the same line. Unfortunately, most of
these are unnecessary, and at worst are wasteful because they result in
narrowing a 4-byte integer into a 2-byte one when they don't need to
(SDL_Rects are made up of 4-byte integers).
Now, removing them reveals why they were placed there in the first place
- a warning is raised (-Wnarrowing) that implicit narrowing conversions
are prohibited in initializer lists in C++11. (Notably, if the
conversion wasn't narrowing, or implicit, or done in an initializer
list, it would be fine. This is a really specific prohibition that
doesn't apply if any of its sub-cases are true.)
We don't use C++11, but this warning can be easily vanquished by a
simple explicit cast to int (similar to the error of implicitly
converting void* to any other pointer in C++, which works just fine in
C), and we only need to do it when the warning is raised (not every
single time we make an SDL_Rect), so there we go.
2021-04-18 20:13:12 +02:00
|
|
|
SDL_Rect rect = {x, y, w, h};
|
2020-01-01 21:29:24 +01:00
|
|
|
SDL_FillRect(_surface, &rect, rgba);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FillRect( SDL_Surface* _surface, SDL_Rect& _rect, const int r, int g, int b )
|
|
|
|
{
|
2021-02-26 00:36:02 +01:00
|
|
|
Uint32 color = SDL_MapRGB(_surface->format, r, g, b);
|
2020-01-01 21:29:24 +01:00
|
|
|
SDL_FillRect(_surface, &_rect, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FillRect( SDL_Surface* _surface, SDL_Rect rect, int rgba )
|
|
|
|
{
|
|
|
|
SDL_FillRect(_surface, &rect, rgba);
|
|
|
|
}
|
|
|
|
|
2021-02-26 00:26:12 +01:00
|
|
|
void ClearSurface(SDL_Surface* surface)
|
|
|
|
{
|
|
|
|
SDL_FillRect(surface, NULL, 0x00000000);
|
|
|
|
}
|
|
|
|
|
2020-01-01 21:29:24 +01:00
|
|
|
void ScrollSurface( SDL_Surface* _src, int _pX, int _pY )
|
|
|
|
{
|
|
|
|
SDL_Surface* part1 = NULL;
|
|
|
|
|
|
|
|
SDL_Rect rect1;
|
2020-04-02 22:35:43 +02:00
|
|
|
SDL_Rect rect2;
|
2020-01-01 21:29:24 +01:00
|
|
|
//scrolling up;
|
|
|
|
if(_pY < 0)
|
|
|
|
{
|
|
|
|
setRect(rect2, 0, 0, _src->w, _src->h - _pY);
|
|
|
|
|
|
|
|
part1 = GetSubSurface(_src, rect2.x, rect2.y, rect2.w, rect2.h);
|
|
|
|
|
|
|
|
SDL_Rect destrect1;
|
|
|
|
|
|
|
|
SDL_SetSurfaceBlendMode(part1, SDL_BLENDMODE_NONE);
|
|
|
|
|
|
|
|
setRect(destrect1, 0, _pY, _pX, _src->h);
|
|
|
|
|
|
|
|
SDL_BlitSurface (part1, NULL, _src, &destrect1);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(_pY > 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
setRect(rect1, 0, 0, _src->w, _src->h - _pY);
|
|
|
|
|
|
|
|
part1 = GetSubSurface(_src, rect1.x, rect1.y, rect1.w, rect1.h);
|
|
|
|
|
|
|
|
SDL_Rect destrect1;
|
|
|
|
|
|
|
|
SDL_SetSurfaceBlendMode(part1, SDL_BLENDMODE_NONE);
|
|
|
|
|
|
|
|
setRect(destrect1, _pX, _pY, _src->w, _src->h - _pY);
|
|
|
|
|
|
|
|
SDL_BlitSurface (part1, NULL, _src, &destrect1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
//Right
|
|
|
|
else if(_pX <= 0)
|
|
|
|
{
|
|
|
|
setRect(rect2, 0, 0, _src->w - _pX, _src->h );
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
part1 = GetSubSurface(_src, rect2.x, rect2.y, rect2.w, rect2.h);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
SDL_Rect destrect1;
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
SDL_SetSurfaceBlendMode(part1, SDL_BLENDMODE_NONE);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
setRect(destrect1, _pX, 0, _src->w - _pX, _src->h);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
SDL_BlitSurface (part1, NULL, _src, &destrect1);
|
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
else if(_pX > 0)
|
|
|
|
{
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
setRect(rect1, _pX, 0, _src->w - _pX, _src->h );
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
part1 = GetSubSurface(_src, rect1.x, rect1.y, rect1.w, rect1.h);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
SDL_Rect destrect1;
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
SDL_SetSurfaceBlendMode(part1, SDL_BLENDMODE_NONE);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
setRect(destrect1, 0, 0, _src->w - _pX, _src->h);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
SDL_BlitSurface (part1, NULL, _src, &destrect1);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2020-04-02 22:35:43 +02:00
|
|
|
}
|
|
|
|
//Cleanup temp surface
|
2020-01-01 21:29:24 +01:00
|
|
|
if (part1)
|
|
|
|
{
|
2022-12-01 07:30:16 +01:00
|
|
|
VVV_freefunc(SDL_FreeSurface, part1);
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|