SDL  2.0
testshape.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2016 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 
22 typedef struct LoadedPicture {
26  const char* name;
28 
30 {
31  /* Clear render-target to blue. */
32  SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
33  SDL_RenderClear(renderer);
34 
35  /* Render the texture. */
36  SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
37 
38  SDL_RenderPresent(renderer);
39 }
40 
41 int 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 event_pending = 0;
52  int should_exit = 0;
53  unsigned int current_picture;
54  int button_down;
55  Uint32 pixelFormat = 0;
56  int access = 0;
57  SDL_Rect texture_dimensions;
58 
59  /* Enable standard application logging */
61 
62  if(argc < 2) {
63  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Shape requires at least one bitmap file as argument.");
64  exit(-1);
65  }
66 
67  if(SDL_VideoInit(NULL) == -1) {
68  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not initialize SDL video.");
69  exit(-2);
70  }
71 
72  num_pictures = argc - 1;
73  pictures = (LoadedPicture *)SDL_malloc(sizeof(LoadedPicture)*num_pictures);
74  for(i=0;i<num_pictures;i++)
75  pictures[i].surface = NULL;
76  for(i=0;i<num_pictures;i++) {
77  pictures[i].surface = SDL_LoadBMP(argv[i+1]);
78  pictures[i].name = argv[i+1];
79  if(pictures[i].surface == NULL) {
80  j = 0;
81  for(j=0;j<num_pictures;j++)
82  SDL_FreeSurface(pictures[j].surface);
83  SDL_free(pictures);
84  SDL_VideoQuit();
85  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file: %s", argv[i+1]);
86  exit(-3);
87  }
88 
89  format = pictures[i].surface->format;
90  if(SDL_ISPIXELFORMAT_ALPHA(format->format)) {
91  pictures[i].mode.mode = ShapeModeBinarizeAlpha;
92  pictures[i].mode.parameters.binarizationCutoff = 255;
93  }
94  else {
95  pictures[i].mode.mode = ShapeModeColorKey;
96  pictures[i].mode.parameters.colorKey = black;
97  }
98  }
99 
100  window = SDL_CreateShapedWindow("SDL_Shape test",
103  0);
105  if(window == NULL) {
106  for(i=0;i<num_pictures;i++)
107  SDL_FreeSurface(pictures[i].surface);
108  SDL_free(pictures);
109  SDL_VideoQuit();
110  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create shaped window for SDL_Shape.");
111  exit(-4);
112  }
113  renderer = SDL_CreateRenderer(window,-1,0);
114  if (!renderer) {
115  SDL_DestroyWindow(window);
116  for(i=0;i<num_pictures;i++)
117  SDL_FreeSurface(pictures[i].surface);
118  SDL_free(pictures);
119  SDL_VideoQuit();
120  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create rendering context for SDL_Shape window.");
121  exit(-5);
122  }
123 
124  for(i=0;i<num_pictures;i++)
125  pictures[i].texture = NULL;
126  for(i=0;i<num_pictures;i++) {
127  pictures[i].texture = SDL_CreateTextureFromSurface(renderer,pictures[i].surface);
128  if(pictures[i].texture == NULL) {
129  j = 0;
130  for(j=0;j<num_pictures;i++)
131  if(pictures[i].texture != NULL)
132  SDL_DestroyTexture(pictures[i].texture);
133  for(i=0;i<num_pictures;i++)
134  SDL_FreeSurface(pictures[i].surface);
135  SDL_free(pictures);
136  SDL_DestroyRenderer(renderer);
137  SDL_DestroyWindow(window);
138  SDL_VideoQuit();
139  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create texture for SDL_shape.");
140  exit(-6);
141  }
142  }
143 
144  event_pending = 0;
145  should_exit = 0;
146  event_pending = SDL_PollEvent(&event);
147  current_picture = 0;
148  button_down = 0;
149  texture_dimensions.h = 0;
150  texture_dimensions.w = 0;
151  texture_dimensions.x = 0;
152  texture_dimensions.y = 0;
153  SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
154  SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
155  SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
156  SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
157  while(should_exit == 0) {
158  event_pending = SDL_PollEvent(&event);
159  if(event_pending == 1) {
160  if(event.type == SDL_KEYDOWN) {
161  button_down = 1;
162  if(event.key.keysym.sym == SDLK_ESCAPE) {
163  should_exit = 1;
164  break;
165  }
166  }
167  if(button_down && event.type == SDL_KEYUP) {
168  button_down = 0;
169  current_picture += 1;
170  if(current_picture >= num_pictures)
171  current_picture = 0;
172  SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
173  SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
174  SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
175  SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
176  }
177  if(event.type == SDL_QUIT)
178  should_exit = 1;
179  event_pending = 0;
180  }
181  render(renderer,pictures[current_picture].texture,texture_dimensions);
182  SDL_Delay(10);
183  }
184 
185  /* Free the textures. */
186  for(i=0;i<num_pictures;i++)
187  SDL_DestroyTexture(pictures[i].texture);
188  SDL_DestroyRenderer(renderer);
189  /* Destroy the window. */
190  SDL_DestroyWindow(window);
191  /* Free the original surfaces backing the textures. */
192  for(i=0;i<num_pictures;i++)
193  SDL_FreeSurface(pictures[i].surface);
194  SDL_free(pictures);
195  /* Call SDL_VideoQuit() before quitting. */
196  SDL_VideoQuit();
197 
198  return 0;
199 }
200 
201 /* vi: set ts=4 sw=4 expandtab: */
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
#define SDL_PollEvent
SDL_Surface * surface
Definition: testshape.c:23
#define SDL_SetWindowShape
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:182
SDL_Texture * texture
Definition: testshape.c:24
SDL_Window * window
GLenum GLenum GLuint texture
#define SDL_SetWindowSize
const char * name
Definition: testshape.c:26
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
A color key is applied.
Definition: SDL_shape.h:87
#define SHAPED_WINDOW_X
Definition: testshape.c:18
GLuint const GLchar * name
#define SDL_ISPIXELFORMAT_ALPHA(format)
Definition: SDL_pixels.h:153
GLuint GLint GLboolean GLint GLenum access
void render(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Rect texture_dimensions)
Definition: testshape.c:29
#define SDL_LogError
#define SDL_RenderCopy
SDL_Renderer * renderer
#define SDL_CreateTextureFromSurface
#define SDL_VideoInit
#define SDL_FreeSurface
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
struct _cl_event * event
void SDL_free(void *mem)
#define SDL_QueryTexture
GLenum mode
SDL_WindowShapeParams parameters
Window-shape parameters.
Definition: SDL_shape.h:104
int x
Definition: SDL_rect.h:66
SDL_Keysym keysym
Definition: SDL_events.h:196
int w
Definition: SDL_rect.h:67
#define SDL_Delay
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:42
#define SDL_LogSetPriority
#define NULL
Definition: begin_code.h:143
#define SHAPED_WINDOW_Y
Definition: testshape.c:19
A binarized alpha cutoff with a given integer value.
Definition: SDL_shape.h:83
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_RenderClear
SDL_KeyboardEvent key
Definition: SDL_events.h:526
#define SDL_DestroyTexture
#define SDL_LogInfo
int h
Definition: SDL_rect.h:67
SDL_Color colorKey
Definition: SDL_shape.h:96
#define SDL_CreateShapedWindow
The type used to identify a window.
Definition: SDL_sysvideo.h:71
#define SDL_VideoQuit
SDL_Keycode sym
Definition: SDL_keyboard.h:50
#define SHAPED_WINDOW_DIMENSION
Definition: testshape.c:20
Uint8 binarizationCutoff
a cutoff alpha value for binarization of the window shape&#39;s alpha channel.
Definition: SDL_shape.h:95
General event structure.
Definition: SDL_events.h:521
#define SDL_malloc
A struct that tags the SDL_WindowShapeParams union with an enum describing the type of its contents...
Definition: SDL_shape.h:100
#define SDL_SetRenderDrawColor
int main(int argc, char **argv)
Definition: testshape.c:41
#define SDL_DestroyRenderer
SDL_WindowShapeMode mode
Definition: testshape.c:25
WindowShapeMode mode
The mode of these window-shape parameters.
Definition: SDL_shape.h:102
#define SDL_DestroyWindow
int y
Definition: SDL_rect.h:66
#define SDL_SetWindowPosition
#define SDL_CreateRenderer
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
#define SDL_RenderPresent
Uint32 type
Definition: SDL_events.h:523
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:42