test/testshape.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 16:08:09 -0800
changeset 11730 ac6c607e065c
parent 11111 0841489fa4df
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Enable building the Metal renderer by default, and weak link the Metal framework so the SDL library is safe to use on older Macs
Also generate iOS versions of the Metal shaders
slouken@5535
     1
/*
slouken@10737
     2
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
eligottlieb@4799
    12
#include <stdlib.h>
eligottlieb@4799
    13
#include <math.h>
eligottlieb@4816
    14
#include <stdio.h>
eligottlieb@4816
    15
#include "SDL.h"
eligottlieb@4816
    16
#include "SDL_shape.h"
eligottlieb@4799
    17
eligottlieb@4799
    18
#define SHAPED_WINDOW_X 150
eligottlieb@4799
    19
#define SHAPED_WINDOW_Y 150
eligottlieb@4799
    20
#define SHAPED_WINDOW_DIMENSION 640
eligottlieb@4799
    21
eligottlieb@4808
    22
typedef struct LoadedPicture {
slouken@5147
    23
    SDL_Surface *surface;
slouken@5147
    24
    SDL_Texture *texture;
slouken@5147
    25
    SDL_WindowShapeMode mode;
urkle@8102
    26
    const char* name;
eligottlieb@4808
    27
} LoadedPicture;
eligottlieb@4808
    28
slouken@5147
    29
void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
slouken@5147
    30
{
gabomdq@7678
    31
    /* Clear render-target to blue. */
slouken@5147
    32
    SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
slouken@5147
    33
    SDL_RenderClear(renderer);
slouken@7191
    34
gabomdq@7678
    35
    /* Render the texture. */
slouken@5147
    36
    SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
slouken@7191
    37
slouken@5147
    38
    SDL_RenderPresent(renderer);
eligottlieb@4804
    39
}
eligottlieb@4804
    40
slouken@5147
    41
int main(int argc,char** argv)
slouken@5147
    42
{
slouken@5147
    43
    Uint8 num_pictures;
slouken@5147
    44
    LoadedPicture* pictures;
slouken@5147
    45
    int i, j;
slouken@5147
    46
    SDL_PixelFormat* format = NULL;
slouken@5147
    47
    SDL_Window *window;
slouken@5147
    48
    SDL_Renderer *renderer;
slouken@5147
    49
    SDL_Color black = {0,0,0,0xff};
slouken@5147
    50
    SDL_Event event;
slouken@5147
    51
    int should_exit = 0;
slouken@5147
    52
    unsigned int current_picture;
slouken@5147
    53
    int button_down;
slouken@5147
    54
    Uint32 pixelFormat = 0;
slouken@5147
    55
    int access = 0;
philipp@8696
    56
    SDL_Rect texture_dimensions;
eligottlieb@4816
    57
aschiffler@7639
    58
    /* Enable standard application logging */
aschiffler@7639
    59
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    60
slouken@5147
    61
    if(argc < 2) {
philipp@9922
    62
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Shape requires at least one bitmap file as argument.");
slouken@5147
    63
        exit(-1);
slouken@5032
    64
    }
slouken@7191
    65
slouken@5147
    66
    if(SDL_VideoInit(NULL) == -1) {
aschiffler@7639
    67
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not initialize SDL video.");
slouken@5147
    68
        exit(-2);
slouken@5147
    69
    }
slouken@7191
    70
slouken@5147
    71
    num_pictures = argc - 1;
philipp@7479
    72
    pictures = (LoadedPicture *)SDL_malloc(sizeof(LoadedPicture)*num_pictures);
philipp@10165
    73
    if (!pictures) {
philipp@10165
    74
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not allocate memory.");
philipp@10165
    75
        exit(1);
philipp@10165
    76
    }
slouken@5147
    77
    for(i=0;i<num_pictures;i++)
slouken@5147
    78
        pictures[i].surface = NULL;
slouken@5147
    79
    for(i=0;i<num_pictures;i++) {
slouken@5147
    80
        pictures[i].surface = SDL_LoadBMP(argv[i+1]);
urkle@8102
    81
        pictures[i].name = argv[i+1];
slouken@5147
    82
        if(pictures[i].surface == NULL) {
slouken@5147
    83
            for(j=0;j<num_pictures;j++)
slouken@7720
    84
                SDL_FreeSurface(pictures[j].surface);
philipp@7479
    85
            SDL_free(pictures);
slouken@5147
    86
            SDL_VideoQuit();
urkle@8102
    87
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file: %s", argv[i+1]);
slouken@5147
    88
            exit(-3);
slouken@5147
    89
        }
eligottlieb@4816
    90
slouken@5147
    91
        format = pictures[i].surface->format;
urkle@8102
    92
        if(SDL_ISPIXELFORMAT_ALPHA(format->format)) {
slouken@5147
    93
            pictures[i].mode.mode = ShapeModeBinarizeAlpha;
slouken@5147
    94
            pictures[i].mode.parameters.binarizationCutoff = 255;
slouken@5147
    95
        }
slouken@5147
    96
        else {
slouken@5147
    97
            pictures[i].mode.mode = ShapeModeColorKey;
slouken@5147
    98
            pictures[i].mode.parameters.colorKey = black;
slouken@5147
    99
        }
slouken@5147
   100
    }
slouken@7191
   101
urkle@8102
   102
    window = SDL_CreateShapedWindow("SDL_Shape test",
urkle@8102
   103
        SHAPED_WINDOW_X, SHAPED_WINDOW_Y,
urkle@8102
   104
        SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,
urkle@8102
   105
        0);
urkle@8102
   106
    SDL_SetWindowPosition(window, SHAPED_WINDOW_X, SHAPED_WINDOW_Y);
slouken@5147
   107
    if(window == NULL) {
slouken@5147
   108
        for(i=0;i<num_pictures;i++)
slouken@5147
   109
            SDL_FreeSurface(pictures[i].surface);
philipp@7479
   110
        SDL_free(pictures);
slouken@5147
   111
        SDL_VideoQuit();
aschiffler@7639
   112
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create shaped window for SDL_Shape.");
slouken@5147
   113
        exit(-4);
slouken@5147
   114
    }
slouken@5147
   115
    renderer = SDL_CreateRenderer(window,-1,0);
slouken@5147
   116
    if (!renderer) {
slouken@5147
   117
        SDL_DestroyWindow(window);
slouken@5147
   118
        for(i=0;i<num_pictures;i++)
slouken@5147
   119
            SDL_FreeSurface(pictures[i].surface);
philipp@7479
   120
        SDL_free(pictures);
slouken@5147
   121
        SDL_VideoQuit();
aschiffler@7639
   122
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create rendering context for SDL_Shape window.");
slouken@5147
   123
        exit(-5);
slouken@5147
   124
    }
slouken@7191
   125
slouken@5147
   126
    for(i=0;i<num_pictures;i++)
slouken@5147
   127
        pictures[i].texture = NULL;
slouken@5147
   128
    for(i=0;i<num_pictures;i++) {
slouken@5158
   129
        pictures[i].texture = SDL_CreateTextureFromSurface(renderer,pictures[i].surface);
slouken@5147
   130
        if(pictures[i].texture == NULL) {
philipp@11058
   131
            for(i=0;i<num_pictures;i++)
slouken@5147
   132
                if(pictures[i].texture != NULL)
slouken@5147
   133
                    SDL_DestroyTexture(pictures[i].texture);
slouken@5147
   134
            for(i=0;i<num_pictures;i++)
slouken@5147
   135
                SDL_FreeSurface(pictures[i].surface);
philipp@7479
   136
            SDL_free(pictures);
slouken@5147
   137
            SDL_DestroyRenderer(renderer);
slouken@5147
   138
            SDL_DestroyWindow(window);
slouken@5147
   139
            SDL_VideoQuit();
aschiffler@7639
   140
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create texture for SDL_shape.");
slouken@5147
   141
            exit(-6);
slouken@5147
   142
        }
slouken@5147
   143
    }
slouken@7191
   144
slouken@5147
   145
    should_exit = 0;
slouken@5147
   146
    current_picture = 0;
slouken@5147
   147
    button_down = 0;
slouken@5147
   148
    texture_dimensions.h = 0;
slouken@5147
   149
    texture_dimensions.w = 0;
slouken@5147
   150
    texture_dimensions.x = 0;
slouken@5147
   151
    texture_dimensions.y = 0;
urkle@8102
   152
    SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
slouken@5147
   153
    SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
slouken@5147
   154
    SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
slouken@5147
   155
    SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
slouken@5147
   156
    while(should_exit == 0) {
philipp@11111
   157
        while (SDL_PollEvent(&event)) {
slouken@5147
   158
            if(event.type == SDL_KEYDOWN) {
slouken@5147
   159
                button_down = 1;
urkle@8102
   160
                if(event.key.keysym.sym == SDLK_ESCAPE) {
slouken@5147
   161
                    should_exit = 1;
urkle@8102
   162
                    break;
urkle@8102
   163
                }
slouken@5147
   164
            }
slouken@5147
   165
            if(button_down && event.type == SDL_KEYUP) {
slouken@5147
   166
                button_down = 0;
slouken@5147
   167
                current_picture += 1;
slouken@5147
   168
                if(current_picture >= num_pictures)
slouken@5147
   169
                    current_picture = 0;
urkle@8102
   170
                SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
slouken@5147
   171
                SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
slouken@5147
   172
                SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
slouken@5147
   173
                SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
slouken@5147
   174
            }
philipp@11111
   175
            if (event.type == SDL_QUIT) {
slouken@5147
   176
                should_exit = 1;
philipp@11111
   177
                break;
philipp@11111
   178
            }
slouken@5147
   179
        }
slouken@5147
   180
        render(renderer,pictures[current_picture].texture,texture_dimensions);
urkle@8102
   181
        SDL_Delay(10);
slouken@5147
   182
    }
slouken@7191
   183
gabomdq@7678
   184
    /* Free the textures. */
slouken@5147
   185
    for(i=0;i<num_pictures;i++)
slouken@5147
   186
        SDL_DestroyTexture(pictures[i].texture);
slouken@5147
   187
    SDL_DestroyRenderer(renderer);
gabomdq@7678
   188
    /* Destroy the window. */
slouken@5147
   189
    SDL_DestroyWindow(window);
gabomdq@7678
   190
    /* Free the original surfaces backing the textures. */
slouken@5147
   191
    for(i=0;i<num_pictures;i++)
slouken@5147
   192
        SDL_FreeSurface(pictures[i].surface);
philipp@7479
   193
    SDL_free(pictures);
gabomdq@7678
   194
    /* Call SDL_VideoQuit() before quitting. */
slouken@5147
   195
    SDL_VideoQuit();
eligottlieb@4816
   196
slouken@5147
   197
    return 0;
eligottlieb@4799
   198
}
slouken@5147
   199
slouken@5147
   200
/* vi: set ts=4 sw=4 expandtab: */