Merge commit '852f2a6343518919e5ca8d3c1bbcab9f493e3cd8'

This commit is contained in:
2024-01-17 17:02:59 +01:00
1244 changed files with 50102 additions and 28146 deletions

View File

@ -1,5 +1,5 @@
/*
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -17,10 +17,42 @@
#define SHAPED_WINDOW_DIMENSION 640
/** An enum denoting the specific type of contents present in an SDL_WindowShapeParams union. */
typedef enum
{
/** The default mode, a binarized alpha cutoff of 1. */
ShapeModeDefault,
/** A binarized alpha cutoff with a given integer value. */
ShapeModeBinarizeAlpha,
/** A binarized alpha cutoff with a given integer value, but with the opposite comparison. */
ShapeModeReverseBinarizeAlpha,
/** A color key is applied. */
ShapeModeColorKey
} WindowShapeMode;
/** A union containing parameters for shaped windows. */
typedef union
{
/** A cutoff alpha value for binarization of the window shape's alpha channel. */
Uint8 binarizationCutoff;
SDL_Color colorKey;
} SDL_WindowShapeParams;
/** A struct that tags the SDL_WindowShapeParams union with an enum describing the type of its contents. */
typedef struct SDL_WindowShapeMode
{
/** The mode of these window-shape parameters. */
WindowShapeMode mode;
/** Window-shape parameters. */
SDL_WindowShapeParams parameters;
} SDL_WindowShapeMode;
typedef struct LoadedPicture
{
SDL_Surface *surface;
SDL_Texture *texture;
int num_textures;
SDL_Texture **textures;
SDL_Texture **shape_textures;
SDL_WindowShapeMode mode;
const char *name;
struct LoadedPicture *next;
@ -29,7 +61,6 @@ typedef struct LoadedPicture
static Uint8 *g_bitmap = NULL;
static int g_bitmap_w = 0, g_bitmap_h = 0;
static SDL_Surface *g_shape_surface = NULL;
static SDL_Texture *g_shape_texture = NULL;
static void log_usage(SDLTest_CommonState *state, char *progname) {
static const char *options[] = { "sample1.bmp [sample2.bmp [sample3.bmp ...]]", NULL };
@ -42,8 +73,7 @@ static void SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode, SDL_Surface *shap
int x = 0;
int y = 0;
Uint8 r = 0, g = 0, b = 0, alpha = 0;
Uint8 *pixel = NULL;
Uint32 pixel_value = 0, mask_value = 0;
Uint32 mask_value = 0;
size_t bytes_per_scanline = (size_t)(shape->w + (ppb - 1)) / ppb;
Uint8 *bitmap_scanline;
SDL_Color key;
@ -58,23 +88,10 @@ static void SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode, SDL_Surface *shap
bitmap_scanline = bitmap + y * bytes_per_scanline;
for (x = 0; x < shape->w; x++) {
alpha = 0;
pixel_value = 0;
pixel = (Uint8 *)(shape->pixels) + (y * shape->pitch) + (x * shape->format->BytesPerPixel);
switch (shape->format->BytesPerPixel) {
case (1):
pixel_value = *pixel;
break;
case (2):
pixel_value = *(Uint16 *)pixel;
break;
case (3):
pixel_value = *(Uint32 *)pixel & (~shape->format->Amask);
break;
case (4):
pixel_value = *(Uint32 *)pixel;
break;
if (SDLTest_ReadSurfacePixel(shape, x, y, &r, &g, &b, &alpha) != 0) {
continue;
}
SDL_GetRGBA(pixel_value, shape->format, &r, &g, &b, &alpha);
switch (mode.mode) {
case (ShapeModeDefault):
mask_value = (alpha >= 1 ? 1 : 0);
@ -99,16 +116,23 @@ static void SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode, SDL_Surface *shap
}
}
static int SDL3_SetWindowShape(SDL_Window *window, SDL_Surface *shape, SDL_WindowShapeMode *shape_mode)
static int SDL3_SetWindowShape(LoadedPicture *picture, SDL_WindowShapeMode *shape_mode)
{
if (g_bitmap) {
SDL_free(g_bitmap);
g_bitmap = NULL;
}
if (g_shape_texture) {
SDL_DestroyTexture(g_shape_texture);
g_shape_texture = NULL;
if (!picture) {
return SDL_SetError("picture");
}
if (picture->shape_textures[0]) {
int i;
for (i = 0; i < picture->num_textures; i++) {
SDL_DestroyTexture(picture->shape_textures[i]);
picture->shape_textures[i] = NULL;
}
}
if (g_shape_surface) {
@ -116,22 +140,18 @@ static int SDL3_SetWindowShape(SDL_Window *window, SDL_Surface *shape, SDL_Windo
g_shape_surface = NULL;
}
if (shape == NULL) {
return SDL_SetError("shape");
}
if (shape_mode == NULL) {
if (!shape_mode) {
return SDL_SetError("shape_mode");
}
g_bitmap_w = shape->w;
g_bitmap_h = shape->h;
g_bitmap = (Uint8*) SDL_malloc(shape->w * shape->h);
if (g_bitmap == NULL) {
return SDL_OutOfMemory();
g_bitmap_w = picture->surface->w;
g_bitmap_h = picture->surface->h;
g_bitmap = (Uint8*) SDL_malloc(picture->surface->w * picture->surface->h);
if (!g_bitmap) {
return -1;
}
SDL_CalculateShapeBitmap(*shape_mode, shape, g_bitmap, 1);
SDL_CalculateShapeBitmap(*shape_mode, picture->surface, g_bitmap, 1);
g_shape_surface = SDL_CreateSurface(g_bitmap_w, g_bitmap_h, SDL_PIXELFORMAT_ABGR8888);
if (g_shape_surface) {
@ -153,14 +173,14 @@ static int SDL3_SetWindowShape(SDL_Window *window, SDL_Surface *shape, SDL_Windo
return 0;
}
static void render(SDL_Renderer *renderer, SDL_Texture *texture)
static void render(int index, SDL_Renderer *renderer, LoadedPicture *picture)
{
/* Clear render-target to blue. */
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xff, 0xff);
SDL_RenderClear(renderer);
/* Render the texture. */
SDL_RenderTexture(renderer, texture, NULL, NULL);
SDL_RenderTexture(renderer, picture->textures[index], NULL, NULL);
/* Apply the shape */
if (g_shape_surface) {
@ -184,19 +204,19 @@ static void render(SDL_Renderer *renderer, SDL_Texture *texture)
SDL_SetRenderDrawColor(renderer, r, g, b, a);
}
} else {
if (g_shape_texture == NULL) {
if (!picture->shape_textures[index]) {
SDL_BlendMode bm;
g_shape_texture = SDL_CreateTextureFromSurface(renderer, g_shape_surface);
picture->shape_textures[index] = SDL_CreateTextureFromSurface(renderer, g_shape_surface);
/* if Alpha is 0, set all to 0, else leave unchanged. */
bm = SDL_ComposeCustomBlendMode(
SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(g_shape_texture, bm);
SDL_SetTextureBlendMode(picture->shape_textures[index], bm);
}
SDL_RenderTexture(renderer, g_shape_texture, NULL, NULL);
SDL_RenderTexture(renderer, picture->shape_textures[index], NULL, NULL);
}
}
@ -210,13 +230,11 @@ int main(int argc, char **argv)
LoadedPicture *picture_linked_list = NULL;
LoadedPicture **pictures = NULL;
int i;
int j;
const SDL_DisplayMode *mode;
SDL_PixelFormat *format = NULL;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Color black = { 0, 0, 0, 0xff };
SDL_Event event;
int should_exit = 0;
int done = 0;
int current_picture;
int button_down;
Uint32 pixelFormat = 0;
@ -225,11 +243,14 @@ int main(int argc, char **argv)
int rc;
/* Initialize test framework */
state = SDLTest_CommonCreateState(argv, 0);
if (state == NULL) {
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
if (!state) {
return 1;
}
state->window_flags |= SDL_WINDOW_TRANSPARENT;
state->window_flags &= ~SDL_WINDOW_RESIZABLE;
rc = 0;
/* SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software"); */
@ -261,14 +282,16 @@ int main(int argc, char **argv)
i += consumed;
}
if (!num_pictures) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Shape requires at least one bitmap file as argument.");
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testshape requires at least one bitmap file as argument.");
log_usage(state, argv[0]);
exit(-1);
rc = -1;
goto ret;
}
if (SDL_Init(SDL_INIT_VIDEO) == -1) {
if (!SDLTest_CommonInit(state)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not initialize SDL video.");
exit(-2);
rc = -2;
goto ret;
}
mode = SDL_GetDesktopDisplayMode(SDL_GetPrimaryDisplay());
@ -307,48 +330,42 @@ int main(int argc, char **argv)
}
}
window = SDL_CreateWindow("SDL_Shape test", SHAPED_WINDOW_DIMENSION, SHAPED_WINDOW_DIMENSION, SDL_WINDOW_TRANSPARENT);
if (window == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create shaped window for SDL_Shape.");
rc = -4;
goto ret;
}
renderer = SDL_CreateRenderer(window, NULL, 0);
if (renderer == NULL) {
SDL_DestroyWindow(window);
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create rendering context for SDL_Shape window.");
rc = -4;
goto ret;
}
for (i = 0; i < num_pictures; i++) {
pictures[i]->texture = NULL;
}
for (i = 0; i < num_pictures; i++) {
pictures[i]->texture = SDL_CreateTextureFromSurface(renderer, pictures[i]->surface);
if (pictures[i]->texture == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create texture for SDL_shape.");
rc = -6;
pictures[i]->textures = SDL_calloc(state->num_windows, sizeof(SDL_Texture *));
pictures[i]->shape_textures = SDL_calloc(state->num_windows, sizeof(SDL_Texture *));
if (!pictures[i]->textures || !pictures[i]->shape_textures) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create textures array(s).");
rc = -4;
goto ret;
}
for (j = 0; j < state->num_windows; j++) {
pictures[i]->textures[j] = SDL_CreateTextureFromSurface(state->renderers[j], pictures[i]->surface);
if (!pictures[i]->textures[j]) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create textures for SDL_shape.");
rc = -5;
goto ret;
}
}
}
should_exit = 0;
done = 0;
current_picture = 0;
button_down = 0;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture]->name);
SDL_QueryTexture(pictures[current_picture]->texture, &pixelFormat, &access, &w, &h);
/* We want to set the window size in pixels */
SDL_SetWindowSize(window, (int)SDL_ceilf(w / mode->pixel_density), (int)SDL_ceilf(h / mode->pixel_density));
SDL3_SetWindowShape(window, pictures[current_picture]->surface, &pictures[current_picture]->mode);
while (should_exit == 0) {
for (i = 0; i < state->num_windows; i++) {
SDL_QueryTexture(pictures[current_picture]->textures[i], &pixelFormat, &access, &w, &h);
/* We want to set the window size in pixels */
SDL_SetWindowSize(state->windows[i], (int)SDL_ceilf(w / mode->pixel_density), (int)SDL_ceilf(h / mode->pixel_density));
}
SDL3_SetWindowShape(pictures[current_picture], &pictures[current_picture]->mode);
while (!done) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
SDLTest_CommonEvent(state, &event, &done);
if (event.type == SDL_EVENT_KEY_DOWN) {
button_down = 1;
if (event.key.keysym.sym == SDLK_ESCAPE) {
should_exit = 1;
break;
}
}
if (button_down && event.type == SDL_EVENT_KEY_UP) {
button_down = 0;
@ -357,16 +374,16 @@ int main(int argc, char **argv)
current_picture = 0;
}
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture]->name);
SDL_QueryTexture(pictures[current_picture]->texture, &pixelFormat, &access, &w, &h);
SDL_SetWindowSize(window, (int)SDL_ceilf(w / mode->pixel_density), (int)SDL_ceilf(h / mode->pixel_density));
SDL3_SetWindowShape(window, pictures[current_picture]->surface, &pictures[current_picture]->mode);
}
if (event.type == SDL_EVENT_QUIT) {
should_exit = 1;
break;
for (i = 0; i < state->num_windows; i++) {
SDL_QueryTexture(pictures[current_picture]->textures[i], &pixelFormat, &access, &w, &h);
SDL_SetWindowSize(state->windows[i], (int)SDL_ceilf(w / mode->pixel_density),(int)SDL_ceilf(h / mode->pixel_density));
SDL3_SetWindowShape(pictures[current_picture], &pictures[current_picture]->mode);
}
}
}
render(renderer, pictures[current_picture]->texture);
for (i = 0; i < state->num_windows; i++) {
render(i, state->renderers[i], pictures[current_picture]);
}
SDL_Delay(10);
}
@ -374,20 +391,16 @@ ret:
/* Free the textures + original surfaces backing the textures. */
for (pic_i = picture_linked_list; pic_i; ) {
LoadedPicture *next = pic_i->next;
if (pic_i->texture) {
SDL_DestroyTexture(pic_i->texture);
for (j = 0; j < state->num_windows; j++) {
SDL_DestroyTexture(pic_i->textures[i]);
}
SDL_free(pic_i->textures);
SDL_DestroySurface(pic_i->surface);
SDL_free(pic_i);
pic_i = next;
}
SDL_free(pictures);
/* Destroy the renderer. */
SDL_DestroyRenderer(renderer);
/* Destroy the window. */
SDL_DestroyWindow(window);
if (g_bitmap) {
SDL_free(g_bitmap);
g_bitmap = NULL;
@ -397,8 +410,7 @@ ret:
g_shape_surface = NULL;
}
SDL_Quit();
SDLTest_CommonDestroyState(state);
SDLTest_CommonQuit(state);
return rc;
}