SDL 2.0
testshape.c
Go to the documentation of this file.
1/*
2 Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11*/
12#include <stdlib.h>
13#include <math.h>
14#include <stdio.h>
15#include "SDL.h"
16#include "SDL_shape.h"
17
18#define SHAPED_WINDOW_X 150
19#define SHAPED_WINDOW_Y 150
20#define SHAPED_WINDOW_DIMENSION 640
21
22typedef struct LoadedPicture {
26 const char* name;
28
30{
31 /* Clear render-target to blue. */
32 SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
34
35 /* Render the texture. */
36 SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
37
39}
40
41int main(int argc,char** argv)
42{
43 Uint8 num_pictures;
44 LoadedPicture* pictures;
45 int i, j;
49 SDL_Color black = {0,0,0,0xff};
51 int should_exit = 0;
52 unsigned int current_picture;
53 int button_down;
54 Uint32 pixelFormat = 0;
55 int access = 0;
56 SDL_Rect texture_dimensions;
57
58 /* Enable standard application logging */
60
61 if(argc < 2) {
62 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Shape requires at least one bitmap file as argument.");
63 exit(-1);
64 }
65
66 if(SDL_VideoInit(NULL) == -1) {
67 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not initialize SDL video.");
68 exit(-2);
69 }
70
71 num_pictures = argc - 1;
72 pictures = (LoadedPicture *)SDL_malloc(sizeof(LoadedPicture)*num_pictures);
73 if (!pictures) {
74 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not allocate memory.");
75 exit(1);
76 }
77 for(i=0;i<num_pictures;i++)
78 pictures[i].surface = NULL;
79 for(i=0;i<num_pictures;i++) {
80 pictures[i].surface = SDL_LoadBMP(argv[i+1]);
81 pictures[i].name = argv[i+1];
82 if(pictures[i].surface == NULL) {
83 for(j=0;j<num_pictures;j++)
84 SDL_FreeSurface(pictures[j].surface);
85 SDL_free(pictures);
87 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file: %s", argv[i+1]);
88 exit(-3);
89 }
90
91 format = pictures[i].surface->format;
92 if(SDL_ISPIXELFORMAT_ALPHA(format->format)) {
94 pictures[i].mode.parameters.binarizationCutoff = 255;
95 }
96 else {
97 pictures[i].mode.mode = ShapeModeColorKey;
98 pictures[i].mode.parameters.colorKey = black;
99 }
100 }
101
102 window = SDL_CreateShapedWindow("SDL_Shape test",
105 0);
107 if(window == NULL) {
108 for(i=0;i<num_pictures;i++)
109 SDL_FreeSurface(pictures[i].surface);
110 SDL_free(pictures);
112 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create shaped window for SDL_Shape.");
113 exit(-4);
114 }
116 if (!renderer) {
118 for(i=0;i<num_pictures;i++)
119 SDL_FreeSurface(pictures[i].surface);
120 SDL_free(pictures);
122 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create rendering context for SDL_Shape window.");
123 exit(-5);
124 }
125
126 for(i=0;i<num_pictures;i++)
127 pictures[i].texture = NULL;
128 for(i=0;i<num_pictures;i++) {
130 if(pictures[i].texture == NULL) {
131 for(i=0;i<num_pictures;i++)
132 if(pictures[i].texture != NULL)
133 SDL_DestroyTexture(pictures[i].texture);
134 for(i=0;i<num_pictures;i++)
135 SDL_FreeSurface(pictures[i].surface);
136 SDL_free(pictures);
140 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create texture for SDL_shape.");
141 exit(-6);
142 }
143 }
144
145 should_exit = 0;
146 current_picture = 0;
147 button_down = 0;
148 texture_dimensions.h = 0;
149 texture_dimensions.w = 0;
150 texture_dimensions.x = 0;
151 texture_dimensions.y = 0;
152 SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
153 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
154 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
155 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
156 while(should_exit == 0) {
157 while (SDL_PollEvent(&event)) {
158 if(event.type == SDL_KEYDOWN) {
159 button_down = 1;
160 if(event.key.keysym.sym == SDLK_ESCAPE) {
161 should_exit = 1;
162 break;
163 }
164 }
165 if(button_down && event.type == SDL_KEYUP) {
166 button_down = 0;
167 current_picture += 1;
168 if(current_picture >= num_pictures)
169 current_picture = 0;
170 SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
171 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
172 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
173 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
174 }
175 if (event.type == SDL_QUIT) {
176 should_exit = 1;
177 break;
178 }
179 }
180 render(renderer,pictures[current_picture].texture,texture_dimensions);
181 SDL_Delay(10);
182 }
183
184 /* Free the textures. */
185 for(i=0;i<num_pictures;i++)
186 SDL_DestroyTexture(pictures[i].texture);
188 /* Destroy the window. */
190 /* Free the original surfaces backing the textures. */
191 for(i=0;i<num_pictures;i++)
192 SDL_FreeSurface(pictures[i].surface);
193 SDL_free(pictures);
194 /* Call SDL_VideoQuit() before quitting. */
196
197 return 0;
198}
199
200/* vi: set ts=4 sw=4 expandtab: */
#define SDL_QueryTexture
#define SDL_RenderPresent
#define SDL_CreateShapedWindow
#define SDL_DestroyWindow
#define SDL_PollEvent
#define SDL_DestroyRenderer
#define SDL_DestroyTexture
#define SDL_SetRenderDrawColor
#define SDL_CreateTextureFromSurface
#define SDL_VideoInit
#define SDL_SetWindowPosition
#define SDL_SetWindowShape
#define SDL_CreateRenderer
#define SDL_malloc
#define SDL_LogSetPriority
#define SDL_RenderClear
#define SDL_LogError
#define SDL_free
#define SDL_VideoQuit
#define SDL_Delay
#define SDL_RenderCopy
#define SDL_FreeSurface
#define SDL_SetWindowSize
#define SDL_LogInfo
@ SDL_QUIT
Definition: SDL_events.h:60
@ SDL_KEYDOWN
Definition: SDL_events.h:96
@ SDL_KEYUP
Definition: SDL_events.h:97
@ SDLK_ESCAPE
Definition: SDL_keycode.h:55
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
struct _cl_event * event
GLenum mode
GLuint const GLchar * name
GLuint GLint GLboolean GLint GLenum access
GLenum GLenum GLuint texture
#define SDL_ISPIXELFORMAT_ALPHA(format)
Definition: SDL_pixels.h:154
@ ShapeModeBinarizeAlpha
A binarized alpha cutoff with a given integer value.
Definition: SDL_shape.h:84
@ ShapeModeColorKey
A color key is applied.
Definition: SDL_shape.h:88
uint32_t Uint32
Definition: SDL_stdinc.h:203
uint8_t Uint8
Definition: SDL_stdinc.h:179
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:201
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
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
#define NULL
Definition: begin_code.h:167
EGLSurface surface
Definition: eglext.h:248
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_Texture * texture
Definition: testshape.c:24
SDL_WindowShapeMode mode
Definition: testshape.c:25
const char * name
Definition: testshape.c:26
SDL_Surface * surface
Definition: testshape.c:23
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
int h
Definition: SDL_rect.h:80
int w
Definition: SDL_rect.h:80
int y
Definition: SDL_rect.h:79
int x
Definition: SDL_rect.h:79
A collection of pixels used in software blitting.
Definition: SDL_surface.h:71
SDL_PixelFormat * format
Definition: SDL_surface.h:73
The type used to identify a window.
Definition: SDL_sysvideo.h:74
A struct that tags the SDL_WindowShapeParams union with an enum describing the type of its contents.
Definition: SDL_shape.h:101
SDL_WindowShapeParams parameters
Window-shape parameters.
Definition: SDL_shape.h:105
WindowShapeMode mode
The mode of these window-shape parameters.
Definition: SDL_shape.h:103
static SDL_Renderer * renderer
#define SHAPED_WINDOW_Y
Definition: testshape.c:19
int main(int argc, char **argv)
Definition: testshape.c:41
#define SHAPED_WINDOW_DIMENSION
Definition: testshape.c:20
void render(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Rect texture_dimensions)
Definition: testshape.c:29
#define SHAPED_WINDOW_X
Definition: testshape.c:18
General event structure.
Definition: SDL_events.h:558
Uint8 binarizationCutoff
A cutoff alpha value for binarization of the window shape's alpha channel.
Definition: SDL_shape.h:96
SDL_Color colorKey
Definition: SDL_shape.h:97