2021-03-06 05:40:13 +01:00
|
|
|
#include <SDL.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-12-01 07:30:16 +01:00
|
|
|
#include "Alloc.h"
|
2023-01-07 19:28:07 +01:00
|
|
|
#include "Constants.h"
|
2020-01-01 21:29:24 +01:00
|
|
|
#include "Graphics.h"
|
2021-03-06 05:40:13 +01:00
|
|
|
#include "Maths.h"
|
2023-01-07 19:28:07 +01:00
|
|
|
#include "Screen.h"
|
|
|
|
#include "UtilityClass.h"
|
|
|
|
#include "Vlogging.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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-01-07 19:28:07 +01:00
|
|
|
void DrawPixel(SDL_Surface* surface, const int x, const int y, const SDL_Color color)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
2023-01-02 01:36:43 +01:00
|
|
|
const SDL_PixelFormat* fmt = surface->format;
|
|
|
|
const int bpp = fmt->BytesPerPixel;
|
|
|
|
Uint32* pixel = (Uint32*) ((Uint8*) surface->pixels + y * surface->pitch + x * bpp);
|
|
|
|
|
|
|
|
switch (bpp)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2023-01-02 01:36:43 +01:00
|
|
|
SDL_assert(0 && "Non-32-bit colors not supported!");
|
|
|
|
return;
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
case 4:
|
2023-01-02 19:44:35 +01:00
|
|
|
*pixel = SDL_MapRGBA(fmt, color.r, color.g, color.b, color.a);
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 01:36:43 +01:00
|
|
|
SDL_Color ReadPixel(const SDL_Surface* surface, const int x, const int y)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
2023-01-02 01:36:43 +01:00
|
|
|
const SDL_PixelFormat* fmt = surface->format;
|
|
|
|
const int bpp = surface->format->BytesPerPixel;
|
|
|
|
const Uint32* pixel = (Uint32*) ((Uint8*) surface->pixels + y * surface->pitch + x * bpp);
|
2023-01-02 19:44:35 +01:00
|
|
|
SDL_Color color = {0, 0, 0, 0};
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2023-01-02 01:36:43 +01:00
|
|
|
switch (bpp)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2023-01-02 01:36:43 +01:00
|
|
|
SDL_assert(0 && "Non-32-bit colors not supported!");
|
2023-01-02 19:44:35 +01:00
|
|
|
break;
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
case 4:
|
2023-01-02 19:44:35 +01:00
|
|
|
SDL_GetRGBA(*pixel, fmt, &color.r, &color.g, &color.b, &color.a);
|
2023-01-02 01:36:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return color;
|
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
|
|
|
}
|
|
|
|
|
2023-03-04 23:02:47 +01:00
|
|
|
void ApplyFilter(SDL_Surface* src, SDL_Surface* dest)
|
2020-05-03 05:27:47 +02:00
|
|
|
{
|
2023-01-07 19:28:07 +01:00
|
|
|
if (src == NULL)
|
2023-03-04 23:02:47 +01:00
|
|
|
{
|
|
|
|
src = SDL_CreateRGBSurface(0, SCREEN_WIDTH_PIXELS, SCREEN_HEIGHT_PIXELS, 32, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
if (dest == NULL)
|
|
|
|
{
|
|
|
|
dest = SDL_CreateRGBSurface(0, SCREEN_WIDTH_PIXELS, SCREEN_HEIGHT_PIXELS, 32, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
if (src == NULL || dest == NULL)
|
2023-01-07 19:28:07 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int result = SDL_RenderReadPixels(gameScreen.m_renderer, NULL, 0, src->pixels, src->pitch);
|
|
|
|
if (result != 0)
|
|
|
|
{
|
|
|
|
SDL_FreeSurface(src);
|
|
|
|
WHINE_ONCE_ARGS(("Could not read pixels from renderer: %s", SDL_GetError()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-02 01:36:43 +01:00
|
|
|
const int red_offset = rand() % 4;
|
2020-04-02 22:35:43 +02:00
|
|
|
|
2023-01-07 19:28:07 +01:00
|
|
|
for (int x = 0; x < src->w; x++)
|
2020-04-02 22:35:43 +02:00
|
|
|
{
|
2023-01-07 19:28:07 +01:00
|
|
|
for (int y = 0; y < src->h; y++)
|
2020-04-02 22:35:43 +02:00
|
|
|
{
|
2023-01-02 01:36:43 +01:00
|
|
|
const int sampley = (y + (int) graphics.lerp(oldscrollamount, scrollamount)) % 240;
|
2020-04-02 22:35:43 +02:00
|
|
|
|
2023-01-02 01:36:43 +01:00
|
|
|
const SDL_Color pixel = ReadPixel(src, x, sampley);
|
2020-04-02 22:35:43 +02:00
|
|
|
|
2023-01-02 01:36:43 +01:00
|
|
|
Uint8 green = pixel.g;
|
|
|
|
Uint8 blue = pixel.b;
|
2020-04-02 22:35:43 +02:00
|
|
|
|
2023-01-02 01:36:43 +01:00
|
|
|
const SDL_Color pixel_offset = ReadPixel(src, SDL_min(x + red_offset, 319), sampley);
|
|
|
|
Uint8 red = pixel_offset.r;
|
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
|
|
|
double mult;
|
2021-12-23 06:49:08 +01:00
|
|
|
int tmp; /* needed to avoid char overflow */
|
2023-01-02 01:36:43 +01:00
|
|
|
if (isscrolling && sampley > 220 && ((rand() % 10) < 4))
|
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
|
|
|
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
|
|
|
|
2023-01-02 01:36:43 +01:00
|
|
|
if (y % 2 == 0)
|
2020-04-02 22:35:43 +02:00
|
|
|
{
|
2023-01-02 01:36:43 +01:00
|
|
|
red = (Uint8) (red / 1.2f);
|
|
|
|
green = (Uint8) (green / 1.2f);
|
|
|
|
blue = (Uint8) (blue / 1.2f);
|
2020-04-02 22:35:43 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 01:36:43 +01:00
|
|
|
int distX = (int) ((SDL_abs(160.0f - x) / 160.0f) * 16);
|
|
|
|
int distY = (int) ((SDL_abs(120.0f - y) / 120.0f) * 32);
|
2020-04-02 22:35:43 +02:00
|
|
|
|
2023-01-02 01:36:43 +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
|
|
|
|
2023-01-02 01:36:43 +01:00
|
|
|
const SDL_Color color = {red, green, blue, pixel.a};
|
2023-01-07 19:28:07 +01:00
|
|
|
DrawPixel(dest, x, y, color);
|
2020-04-02 22:35:43 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-02 01:36:43 +01:00
|
|
|
|
2023-01-07 19:28:07 +01:00
|
|
|
SDL_UpdateTexture(graphics.gameTexture, NULL, dest->pixels, dest->pitch);
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|