testshape.c
Go to the documentation of this file.
/*
Copyright (C) 1997-2018 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
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include "SDL.h"
#include "SDL_shape.h"
#define SHAPED_WINDOW_X 150
#define SHAPED_WINDOW_Y 150
#define SHAPED_WINDOW_DIMENSION 640
typedef struct LoadedPicture {
SDL_Surface *surface;
SDL_Texture *texture;
SDL_WindowShapeMode mode;
const char* name;
} LoadedPicture;
void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
{
/* Clear render-target to blue. */
SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
SDL_RenderClear(renderer);
/* Render the texture. */
SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
SDL_RenderPresent(renderer);
}
int main(int argc,char** argv)
{
Uint8 num_pictures;
LoadedPicture* pictures;
int i, j;
SDL_PixelFormat* format = NULL;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Color black = {0,0,0,0xff};
SDL_Event event;
int should_exit = 0;
unsigned int current_picture;
int button_down;
Uint32 pixelFormat = 0;
int access = 0;
SDL_Rect texture_dimensions;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
if(argc < 2) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Shape requires at least one bitmap file as argument.");
exit(-1);
}
if(SDL_VideoInit(NULL) == -1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not initialize SDL video.");
exit(-2);
}
num_pictures = argc - 1;
pictures = (LoadedPicture *)SDL_malloc(sizeof(LoadedPicture)*num_pictures);
if (!pictures) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not allocate memory.");
exit(1);
}
for(i=0;i<num_pictures;i++)
pictures[i].surface = NULL;
for(i=0;i<num_pictures;i++) {
pictures[i].surface = SDL_LoadBMP(argv[i+1]);
pictures[i].name = argv[i+1];
if(pictures[i].surface == NULL) {
for(j=0;j<num_pictures;j++)
SDL_FreeSurface(pictures[j].surface);
SDL_free(pictures);
SDL_VideoQuit();
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file: %s", argv[i+1]);
exit(-3);
}
format = pictures[i].surface->format;
if(SDL_ISPIXELFORMAT_ALPHA(format->format)) {
pictures[i].mode.mode = ShapeModeBinarizeAlpha;
pictures[i].mode.parameters.binarizationCutoff = 255;
}
else {
pictures[i].mode.mode = ShapeModeColorKey;
pictures[i].mode.parameters.colorKey = black;
}
}
window = SDL_CreateShapedWindow("SDL_Shape test",
SHAPED_WINDOW_X, SHAPED_WINDOW_Y,
SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,
0);
SDL_SetWindowPosition(window, SHAPED_WINDOW_X, SHAPED_WINDOW_Y);
if(window == NULL) {
for(i=0;i<num_pictures;i++)
SDL_FreeSurface(pictures[i].surface);
SDL_free(pictures);
SDL_VideoQuit();
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create shaped window for SDL_Shape.");
exit(-4);
}
renderer = SDL_CreateRenderer(window,-1,0);
if (!renderer) {
SDL_DestroyWindow(window);
for(i=0;i<num_pictures;i++)
SDL_FreeSurface(pictures[i].surface);
SDL_free(pictures);
SDL_VideoQuit();
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create rendering context for SDL_Shape window.");
exit(-5);
}
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) {
for(i=0;i<num_pictures;i++)
if(pictures[i].texture != NULL)
SDL_DestroyTexture(pictures[i].texture);
for(i=0;i<num_pictures;i++)
SDL_FreeSurface(pictures[i].surface);
SDL_free(pictures);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_VideoQuit();
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create texture for SDL_shape.");
exit(-6);
}
}
should_exit = 0;
current_picture = 0;
button_down = 0;
texture_dimensions.h = 0;
texture_dimensions.w = 0;
texture_dimensions.x = 0;
texture_dimensions.y = 0;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
while(should_exit == 0) {
while (SDL_PollEvent(&event)) {
if(event.type == SDL_KEYDOWN) {
button_down = 1;
if(event.key.keysym.sym == SDLK_ESCAPE) {
should_exit = 1;
break;
}
}
if(button_down && event.type == SDL_KEYUP) {
button_down = 0;
current_picture += 1;
if(current_picture >= num_pictures)
current_picture = 0;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
}
if (event.type == SDL_QUIT) {
should_exit = 1;
break;
}
}
render(renderer,pictures[current_picture].texture,texture_dimensions);
SDL_Delay(10);
}
/* Free the textures. */
for(i=0;i<num_pictures;i++)
SDL_DestroyTexture(pictures[i].texture);
SDL_DestroyRenderer(renderer);
/* Destroy the window. */
SDL_DestroyWindow(window);
/* Free the original surfaces backing the textures. */
for(i=0;i<num_pictures;i++)
SDL_FreeSurface(pictures[i].surface);
SDL_free(pictures);
/* Call SDL_VideoQuit() before quitting. */
SDL_VideoQuit();
return 0;
}
/* vi: set ts=4 sw=4 expandtab: */
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:122
SDL_KEYDOWN
Definition: SDL_events.h:93
SDL_Color
Definition: SDL_pixels.h:295
LoadedPicture::surface
SDL_Surface * surface
Definition: testshape.c:23
SDL_SetWindowShape
#define SDL_SetWindowShape
Definition: SDL_dynapi_overrides.h:372
SDL_LoadBMP
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:200
LoadedPicture::texture
SDL_Texture * texture
Definition: testshape.c:24
SDL_SetWindowSize
#define SDL_SetWindowSize
Definition: SDL_dynapi_overrides.h:526
LoadedPicture::name
const char * name
Definition: testshape.c:26
surface
EGLSurface surface
Definition: eglext.h:248
LoadedPicture
Definition: testshape.c:22
SDL_Surface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
SDL_QUIT
Definition: SDL_events.h:60
ShapeModeColorKey
A color key is applied.
Definition: SDL_shape.h:88
SDL_Renderer
Definition: SDL_sysrender.h:76
SHAPED_WINDOW_X
#define SHAPED_WINDOW_X
Definition: testshape.c:18
SDL_ISPIXELFORMAT_ALPHA
#define SDL_ISPIXELFORMAT_ALPHA(format)
Definition: SDL_pixels.h:154
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:181
access
GLuint GLint GLboolean GLint GLenum access
Definition: SDL_opengl_glext.h:2162
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660
SDL_Texture
Definition: SDL_sysrender.h:49
render
void render(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Rect texture_dimensions)
Definition: testshape.c:29
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
format
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
SDL_RenderCopy
#define SDL_RenderCopy
Definition: SDL_dynapi_overrides.h:343
texture
GLenum GLenum GLuint texture
Definition: SDL_opengl_glext.h:1178
SDL_shape.h
SDL_CreateTextureFromSurface
#define SDL_CreateTextureFromSurface
Definition: SDL_dynapi_overrides.h:307
SDL_VideoInit
#define SDL_VideoInit
Definition: SDL_dynapi_overrides.h:498
SDL_FreeSurface
#define SDL_FreeSurface
Definition: SDL_dynapi_overrides.h:446
renderer
static SDL_Renderer * renderer
Definition: testaudiocapture.c:21
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:157
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_KEYUP
Definition: SDL_events.h:94
j
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
Definition: SDL_x11sym.h:50
SDL_QueryTexture
#define SDL_QueryTexture
Definition: SDL_dynapi_overrides.h:308
mode
GLenum mode
Definition: SDL_opengl_glext.h:1122
SDL_WindowShapeMode::parameters
SDL_WindowShapeParams parameters
Window-shape parameters.
Definition: SDL_shape.h:105
SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_KeyboardEvent::keysym
SDL_Keysym keysym
Definition: SDL_events.h:199
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:486
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
NULL
#define NULL
Definition: begin_code.h:164
SHAPED_WINDOW_Y
#define SHAPED_WINDOW_Y
Definition: testshape.c:19
ShapeModeBinarizeAlpha
A binarized alpha cutoff with a given integer value.
Definition: SDL_shape.h:84
SDL_Surface::format
SDL_PixelFormat * format
Definition: SDL_surface.h:72
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:334
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_Event::key
SDL_KeyboardEvent key
Definition: SDL_events.h:530
SDL_DestroyTexture
#define SDL_DestroyTexture
Definition: SDL_dynapi_overrides.h:347
SDL_LogInfo
#define SDL_LogInfo
Definition: SDL_dynapi_overrides.h:34
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDL_WindowShapeParams::colorKey
SDL_Color colorKey
Definition: SDL_shape.h:97
SDL_CreateShapedWindow
#define SDL_CreateShapedWindow
Definition: SDL_dynapi_overrides.h:370
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:73
SDL_VideoQuit
#define SDL_VideoQuit
Definition: SDL_dynapi_overrides.h:499
SDL_PixelFormat
Definition: SDL_pixels.h:315
SDL_Keysym::sym
SDL_Keycode sym
Definition: SDL_keyboard.h:50
SHAPED_WINDOW_DIMENSION
#define SHAPED_WINDOW_DIMENSION
Definition: testshape.c:20
SDL_WindowShapeParams::binarizationCutoff
Uint8 binarizationCutoff
A cutoff alpha value for binarization of the window shape&#39;s alpha channel.
Definition: SDL_shape.h:96
SDL_Event
General event structure.
Definition: SDL_events.h:525
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
SDL_WindowShapeMode
A struct that tags the SDL_WindowShapeParams union with an enum describing the type of its contents...
Definition: SDL_shape.h:101
SDL_SetRenderDrawColor
#define SDL_SetRenderDrawColor
Definition: SDL_dynapi_overrides.h:330
main
int main(int argc, char **argv)
Definition: testshape.c:41
SDL_DestroyRenderer
#define SDL_DestroyRenderer
Definition: SDL_dynapi_overrides.h:348
LoadedPicture::mode
SDL_WindowShapeMode mode
Definition: testshape.c:25
SDL_PixelFormat::format
Uint32 format
Definition: SDL_pixels.h:317
SDL_WindowShapeMode::mode
WindowShapeMode mode
The mode of these window-shape parameters.
Definition: SDL_shape.h:103
SDLK_ESCAPE
Definition: SDL_keycode.h:55
SDL.h
SDL_DestroyWindow
#define SDL_DestroyWindow
Definition: SDL_dynapi_overrides.h:549
SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
SDL_Rect::y
int y
Definition: SDL_rect.h:66
SDL_SetWindowPosition
#define SDL_SetWindowPosition
Definition: SDL_dynapi_overrides.h:524
SDL_CreateRenderer
#define SDL_CreateRenderer
Definition: SDL_dynapi_overrides.h:301
SDL_Rect
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
SDL_RenderPresent
#define SDL_RenderPresent
Definition: SDL_dynapi_overrides.h:346
SDL_Event::type
Uint32 type
Definition: SDL_events.h:527