SDL  2.0
testshape.c File Reference
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include "SDL.h"
#include "SDL_shape.h"
+ Include dependency graph for testshape.c:

Go to the source code of this file.

Data Structures

struct  LoadedPicture
 

Macros

#define SHAPED_WINDOW_X   150
 
#define SHAPED_WINDOW_Y   150
 
#define SHAPED_WINDOW_DIMENSION   640
 

Functions

void render (SDL_Renderer *renderer, SDL_Texture *texture, SDL_Rect texture_dimensions)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

◆ SHAPED_WINDOW_DIMENSION

#define SHAPED_WINDOW_DIMENSION   640

Definition at line 20 of file testshape.c.

Referenced by main().

◆ SHAPED_WINDOW_X

#define SHAPED_WINDOW_X   150

Definition at line 18 of file testshape.c.

Referenced by main().

◆ SHAPED_WINDOW_Y

#define SHAPED_WINDOW_Y   150

Definition at line 19 of file testshape.c.

Referenced by main().

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 41 of file testshape.c.

References SDL_WindowShapeParams::binarizationCutoff, SDL_WindowShapeParams::colorKey, SDL_Surface::format, SDL_PixelFormat::format, SDL_Rect::h, i, j, SDL_Event::key, SDL_KeyboardEvent::keysym, LoadedPicture::mode, SDL_WindowShapeMode::mode, LoadedPicture::name, NULL, SDL_WindowShapeMode::parameters, render(), renderer, SDL_CreateRenderer, SDL_CreateShapedWindow, SDL_CreateTextureFromSurface, SDL_Delay, SDL_DestroyRenderer, SDL_DestroyTexture, SDL_DestroyWindow, SDL_free(), SDL_FreeSurface, SDL_ISPIXELFORMAT_ALPHA, SDL_KEYDOWN, SDL_KEYUP, SDL_LoadBMP, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogInfo, SDL_LogSetPriority, SDL_malloc, SDL_PollEvent, SDL_QueryTexture, SDL_QUIT, SDL_SetWindowPosition, SDL_SetWindowShape, SDL_SetWindowSize, SDL_VideoInit, SDL_VideoQuit, SDLK_ESCAPE, SHAPED_WINDOW_DIMENSION, SHAPED_WINDOW_X, SHAPED_WINDOW_Y, ShapeModeBinarizeAlpha, ShapeModeColorKey, LoadedPicture::surface, SDL_Keysym::sym, LoadedPicture::texture, SDL_Event::type, SDL_Rect::w, window, SDL_Rect::x, and SDL_Rect::y.

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  if (!pictures) {
75  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not allocate memory.");
76  exit(1);
77  }
78  for(i=0;i<num_pictures;i++)
79  pictures[i].surface = NULL;
80  for(i=0;i<num_pictures;i++) {
81  pictures[i].surface = SDL_LoadBMP(argv[i+1]);
82  pictures[i].name = argv[i+1];
83  if(pictures[i].surface == NULL) {
84  j = 0;
85  for(j=0;j<num_pictures;j++)
86  SDL_FreeSurface(pictures[j].surface);
87  SDL_free(pictures);
88  SDL_VideoQuit();
89  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file: %s", argv[i+1]);
90  exit(-3);
91  }
92 
93  format = pictures[i].surface->format;
94  if(SDL_ISPIXELFORMAT_ALPHA(format->format)) {
95  pictures[i].mode.mode = ShapeModeBinarizeAlpha;
96  pictures[i].mode.parameters.binarizationCutoff = 255;
97  }
98  else {
99  pictures[i].mode.mode = ShapeModeColorKey;
100  pictures[i].mode.parameters.colorKey = black;
101  }
102  }
103 
104  window = SDL_CreateShapedWindow("SDL_Shape test",
107  0);
109  if(window == NULL) {
110  for(i=0;i<num_pictures;i++)
111  SDL_FreeSurface(pictures[i].surface);
112  SDL_free(pictures);
113  SDL_VideoQuit();
114  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create shaped window for SDL_Shape.");
115  exit(-4);
116  }
117  renderer = SDL_CreateRenderer(window,-1,0);
118  if (!renderer) {
119  SDL_DestroyWindow(window);
120  for(i=0;i<num_pictures;i++)
121  SDL_FreeSurface(pictures[i].surface);
122  SDL_free(pictures);
123  SDL_VideoQuit();
124  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create rendering context for SDL_Shape window.");
125  exit(-5);
126  }
127 
128  for(i=0;i<num_pictures;i++)
129  pictures[i].texture = NULL;
130  for(i=0;i<num_pictures;i++) {
131  pictures[i].texture = SDL_CreateTextureFromSurface(renderer,pictures[i].surface);
132  if(pictures[i].texture == NULL) {
133  j = 0;
134  for(j=0;j<num_pictures;i++)
135  if(pictures[i].texture != NULL)
136  SDL_DestroyTexture(pictures[i].texture);
137  for(i=0;i<num_pictures;i++)
138  SDL_FreeSurface(pictures[i].surface);
139  SDL_free(pictures);
140  SDL_DestroyRenderer(renderer);
141  SDL_DestroyWindow(window);
142  SDL_VideoQuit();
143  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create texture for SDL_shape.");
144  exit(-6);
145  }
146  }
147 
148  event_pending = 0;
149  should_exit = 0;
150  event_pending = SDL_PollEvent(&event);
151  current_picture = 0;
152  button_down = 0;
153  texture_dimensions.h = 0;
154  texture_dimensions.w = 0;
155  texture_dimensions.x = 0;
156  texture_dimensions.y = 0;
157  SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
158  SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
159  SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
160  SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
161  while(should_exit == 0) {
162  event_pending = SDL_PollEvent(&event);
163  if(event_pending == 1) {
164  if(event.type == SDL_KEYDOWN) {
165  button_down = 1;
166  if(event.key.keysym.sym == SDLK_ESCAPE) {
167  should_exit = 1;
168  break;
169  }
170  }
171  if(button_down && event.type == SDL_KEYUP) {
172  button_down = 0;
173  current_picture += 1;
174  if(current_picture >= num_pictures)
175  current_picture = 0;
176  SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
177  SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
178  SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
179  SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
180  }
181  if(event.type == SDL_QUIT)
182  should_exit = 1;
183  event_pending = 0;
184  }
185  render(renderer,pictures[current_picture].texture,texture_dimensions);
186  SDL_Delay(10);
187  }
188 
189  /* Free the textures. */
190  for(i=0;i<num_pictures;i++)
191  SDL_DestroyTexture(pictures[i].texture);
192  SDL_DestroyRenderer(renderer);
193  /* Destroy the window. */
194  SDL_DestroyWindow(window);
195  /* Free the original surfaces backing the textures. */
196  for(i=0;i<num_pictures;i++)
197  SDL_FreeSurface(pictures[i].surface);
198  SDL_free(pictures);
199  /* Call SDL_VideoQuit() before quitting. */
200  SDL_VideoQuit();
201 
202  return 0;
203 }
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
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:186
SDL_Texture * texture
Definition: testshape.c:24
static SDL_Window * window
GLenum GLenum GLuint texture
#define SDL_SetWindowSize
const char * name
Definition: testshape.c:26
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:154
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:159
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_CreateTextureFromSurface
#define SDL_VideoInit
#define SDL_FreeSurface
static SDL_Renderer * renderer
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
struct _cl_event * event
void SDL_free(void *mem)
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
#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:199
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:50
#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
SDL_KeyboardEvent key
Definition: SDL_events.h:530
#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:525
#define SDL_malloc
#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
Uint32 type
Definition: SDL_events.h:527

◆ render()

void render ( SDL_Renderer renderer,
SDL_Texture texture,
SDL_Rect  texture_dimensions 
)

Definition at line 29 of file testshape.c.

References SDL_RenderClear, SDL_RenderCopy, SDL_RenderPresent, and SDL_SetRenderDrawColor.

Referenced by main().

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 }
#define SDL_RenderCopy
#define SDL_RenderClear
#define SDL_SetRenderDrawColor
#define SDL_RenderPresent