some fixes to the testshape.c code so it more correctly works.

This commit is contained in:
Edward Rudd 2014-01-08 14:18:11 -05:00
parent d75665c776
commit 93fd65f762
1 changed files with 15 additions and 20 deletions

View File

@ -19,12 +19,11 @@
#define SHAPED_WINDOW_Y 150 #define SHAPED_WINDOW_Y 150
#define SHAPED_WINDOW_DIMENSION 640 #define SHAPED_WINDOW_DIMENSION 640
#define TICK_INTERVAL 1000/10
typedef struct LoadedPicture { typedef struct LoadedPicture {
SDL_Surface *surface; SDL_Surface *surface;
SDL_Texture *texture; SDL_Texture *texture;
SDL_WindowShapeMode mode; SDL_WindowShapeMode mode;
const char* name;
} LoadedPicture; } LoadedPicture;
void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions) void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
@ -39,17 +38,6 @@ void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensi
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }
static Uint32 next_time;
Uint32 time_left()
{
Uint32 now = SDL_GetTicks();
if(next_time <= now)
return 0;
else
return next_time - now;
}
int main(int argc,char** argv) int main(int argc,char** argv)
{ {
Uint8 num_pictures; Uint8 num_pictures;
@ -87,18 +75,19 @@ int main(int argc,char** argv)
pictures[i].surface = NULL; pictures[i].surface = NULL;
for(i=0;i<num_pictures;i++) { for(i=0;i<num_pictures;i++) {
pictures[i].surface = SDL_LoadBMP(argv[i+1]); pictures[i].surface = SDL_LoadBMP(argv[i+1]);
pictures[i].name = argv[i+1];
if(pictures[i].surface == NULL) { if(pictures[i].surface == NULL) {
j = 0; j = 0;
for(j=0;j<num_pictures;j++) for(j=0;j<num_pictures;j++)
SDL_FreeSurface(pictures[j].surface); SDL_FreeSurface(pictures[j].surface);
SDL_free(pictures); SDL_free(pictures);
SDL_VideoQuit(); SDL_VideoQuit();
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file."); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file: %s", argv[i+1]);
exit(-3); exit(-3);
} }
format = pictures[i].surface->format; format = pictures[i].surface->format;
if(format->Amask != 0) { if(SDL_ISPIXELFORMAT_ALPHA(format->format)) {
pictures[i].mode.mode = ShapeModeBinarizeAlpha; pictures[i].mode.mode = ShapeModeBinarizeAlpha;
pictures[i].mode.parameters.binarizationCutoff = 255; pictures[i].mode.parameters.binarizationCutoff = 255;
} }
@ -108,7 +97,11 @@ int main(int argc,char** argv)
} }
} }
window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE); 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) { if(window == NULL) {
for(i=0;i<num_pictures;i++) for(i=0;i<num_pictures;i++)
SDL_FreeSurface(pictures[i].surface); SDL_FreeSurface(pictures[i].surface);
@ -157,23 +150,26 @@ int main(int argc,char** argv)
texture_dimensions.w = 0; texture_dimensions.w = 0;
texture_dimensions.x = 0; texture_dimensions.x = 0;
texture_dimensions.y = 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_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_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode); SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
next_time = SDL_GetTicks() + TICK_INTERVAL;
while(should_exit == 0) { while(should_exit == 0) {
event_pending = SDL_PollEvent(&event); event_pending = SDL_PollEvent(&event);
if(event_pending == 1) { if(event_pending == 1) {
if(event.type == SDL_KEYDOWN) { if(event.type == SDL_KEYDOWN) {
button_down = 1; button_down = 1;
if(event.key.keysym.sym == SDLK_ESCAPE) if(event.key.keysym.sym == SDLK_ESCAPE) {
should_exit = 1; should_exit = 1;
break;
}
} }
if(button_down && event.type == SDL_KEYUP) { if(button_down && event.type == SDL_KEYUP) {
button_down = 0; button_down = 0;
current_picture += 1; current_picture += 1;
if(current_picture >= num_pictures) if(current_picture >= num_pictures)
current_picture = 0; 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_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_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode); SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
@ -183,8 +179,7 @@ int main(int argc,char** argv)
event_pending = 0; event_pending = 0;
} }
render(renderer,pictures[current_picture].texture,texture_dimensions); render(renderer,pictures[current_picture].texture,texture_dimensions);
SDL_Delay(time_left()); SDL_Delay(10);
next_time += TICK_INTERVAL;
} }
/* Free the textures. */ /* Free the textures. */