src/video/windows/SDL_windowsshape.c
author Sam Lantinga
Tue, 08 Feb 2011 16:50:51 -0800
changeset 5229 c015d3e63631
parent 5062 e8916fe9cfc8
child 5535 96594ac5fd1a
permissions -rw-r--r--
Fixed setting the texture unit, still doesn't work.
eligottlieb@4777
     1
/*
eligottlieb@4777
     2
    SDL - Simple DirectMedia Layer
eligottlieb@4777
     3
    Copyright (C) 2010 Eli Gottlieb
eligottlieb@4777
     4
eligottlieb@4777
     5
    This library is free software; you can redistribute it and/or
eligottlieb@4777
     6
    modify it under the terms of the GNU Lesser General Public
eligottlieb@4777
     7
    License as published by the Free Software Foundation; either
eligottlieb@4777
     8
    version 2.1 of the License, or (at your option) any later version.
eligottlieb@4777
     9
eligottlieb@4777
    10
    This library is distributed in the hope that it will be useful,
eligottlieb@4777
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
eligottlieb@4777
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
eligottlieb@4777
    13
    Lesser General Public License for more details.
eligottlieb@4777
    14
eligottlieb@4777
    15
    You should have received a copy of the GNU Lesser General Public
eligottlieb@4777
    16
    License along with this library; if not, write to the Free Software
eligottlieb@4777
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
eligottlieb@4777
    18
eligottlieb@4777
    19
    Eli Gottlieb
eligottlieb@4777
    20
    eligottlieb@gmail.com
eligottlieb@4777
    21
*/
eligottlieb@4777
    22
egottlieb@4849
    23
#include <stdio.h>
egottlieb@4850
    24
#include "SDL_assert.h"
slouken@5062
    25
#include "SDL_windowsshape.h"
slouken@5062
    26
#include "SDL_windowsvideo.h"
eligottlieb@4785
    27
eligottlieb@4838
    28
SDL_WindowShaper*
eligottlieb@4838
    29
Win32_CreateShaper(SDL_Window * window) {
eligottlieb@4838
    30
    int resized_properly;
eligottlieb@4838
    31
    SDL_WindowShaper* result = (SDL_WindowShaper *)SDL_malloc(sizeof(SDL_WindowShaper));
eligottlieb@4838
    32
    result->window = window;
eligottlieb@4838
    33
    result->mode.mode = ShapeModeDefault;
eligottlieb@4838
    34
    result->mode.parameters.binarizationCutoff = 1;
eligottlieb@4851
    35
    result->userx = result->usery = 0;
eligottlieb@4838
    36
    result->driverdata = (SDL_ShapeData*)SDL_malloc(sizeof(SDL_ShapeData));
eligottlieb@4838
    37
    ((SDL_ShapeData*)result->driverdata)->mask_tree = NULL;
eligottlieb@4838
    38
    //Put some driver-data here.
eligottlieb@4838
    39
    window->shaper = result;
eligottlieb@4838
    40
    resized_properly = Win32_ResizeWindowShape(window);
eligottlieb@4838
    41
    if (resized_properly != 0)
eligottlieb@4838
    42
            return NULL;
egottlieb@4856
    43
    
eligottlieb@4838
    44
    return result;
eligottlieb@4785
    45
}
eligottlieb@4777
    46
eligottlieb@4838
    47
void
egottlieb@4844
    48
CombineRectRegions(SDL_ShapeTree* node,void* closure) {
egottlieb@4850
    49
    HRGN mask_region = *((HRGN*)closure),temp_region = NULL;
eligottlieb@4838
    50
    if(node->kind == OpaqueShape) {
egottlieb@4850
    51
        //Win32 API regions exclude their outline, so we widen the region by one pixel in each direction to include the real outline.
egottlieb@4858
    52
        temp_region = CreateRectRgn(node->data.shape.x,node->data.shape.y,node->data.shape.x + node->data.shape.w + 1,node->data.shape.y + node->data.shape.h + 1);
egottlieb@4850
    53
        if(mask_region != NULL) {
egottlieb@4850
    54
            CombineRgn(mask_region,mask_region,temp_region,RGN_OR);
egottlieb@4850
    55
            DeleteObject(temp_region);
egottlieb@4850
    56
		}
egottlieb@4850
    57
		else
egottlieb@4850
    58
            *((HRGN*)closure) = temp_region;
egottlieb@4850
    59
	}
egottlieb@4844
    60
}
egottlieb@4844
    61
eligottlieb@4838
    62
int
egottlieb@4849
    63
Win32_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode) {
eligottlieb@4838
    64
    SDL_ShapeData *data;
egottlieb@4850
    65
    HRGN mask_region = NULL;
eligottlieb@4815
    66
eligottlieb@4838
    67
    if (shaper == NULL || shape == NULL)
eligottlieb@4838
    68
        return SDL_INVALID_SHAPE_ARGUMENT;
egottlieb@4849
    69
    if(shape->format->Amask == 0 && shape_mode->mode != ShapeModeColorKey || shape->w != shaper->window->w || shape->h != shaper->window->h)
eligottlieb@4838
    70
        return SDL_INVALID_SHAPE_ARGUMENT;
eligottlieb@4838
    71
    
eligottlieb@4838
    72
    data = (SDL_ShapeData*)shaper->driverdata;
eligottlieb@4838
    73
    if(data->mask_tree != NULL)
eligottlieb@4838
    74
        SDL_FreeShapeTree(&data->mask_tree);
egottlieb@4849
    75
    data->mask_tree = SDL_CalculateShapeTree(*shape_mode,shape);
eligottlieb@4838
    76
    
egottlieb@4850
    77
    SDL_TraverseShapeTree(data->mask_tree,&CombineRectRegions,&mask_region);
egottlieb@4850
    78
	SDL_assert(mask_region != NULL);
egottlieb@4844
    79
egottlieb@4844
    80
    SetWindowRgn(((SDL_WindowData *)(shaper->window->driverdata))->hwnd, mask_region, TRUE);
eligottlieb@4838
    81
    
eligottlieb@4838
    82
    return 0;
eligottlieb@4785
    83
}
eligottlieb@4785
    84
eligottlieb@4838
    85
int
eligottlieb@4838
    86
Win32_ResizeWindowShape(SDL_Window *window) {
eligottlieb@4838
    87
    SDL_ShapeData* data;
eligottlieb@4815
    88
eligottlieb@4838
    89
    if (window == NULL)
eligottlieb@4838
    90
        return -1;
eligottlieb@4838
    91
    data = (SDL_ShapeData *)window->shaper->driverdata;
eligottlieb@4838
    92
    if (data == NULL)
eligottlieb@4838
    93
        return -1;
eligottlieb@4838
    94
    
eligottlieb@4838
    95
    if(data->mask_tree != NULL)
eligottlieb@4838
    96
        SDL_FreeShapeTree(&data->mask_tree);
egottlieb@4852
    97
    if(window->shaper->hasshape == SDL_TRUE) {
egottlieb@4852
    98
        window->shaper->userx = window->x;
egottlieb@4852
    99
        window->shaper->usery = window->y;
egottlieb@4852
   100
        SDL_SetWindowPosition(window,-1000,-1000);
egottlieb@4852
   101
	}
eligottlieb@4838
   102
    
eligottlieb@4838
   103
    return 0;
eligottlieb@4785
   104
}