include/SDL_rect.h
author Ryan C. Gordon <icculus@icculus.org>
Thu, 28 May 2015 01:54:52 -0400
changeset 9671 0e1f57b051f4
parent 9619 b94b6d0bff0f
child 9998 f67cf37e9cd4
permissions -rw-r--r--
Windows GetBasePath should use GetModuleFileNameExW() and check for overflows.

Apparently you might get strange paths from GetModuleFileName(), such as
short path names or UNC filenames, so this avoids that problem. Since you have
to tapdance with linking different libraries and defining macros depending on
what Windows you plan to target, we dynamically load the API we need, which
works on all versions of Windows (on Win7, it'll load a compatibility wrapper
for the newer API location).

What a mess.

This also now does the right thing if there isn't enough space to store the
path, looping with a larger allocated buffer each try.

Fixes Bugzilla #2435.
slouken@2275
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
slouken@2275
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@2275
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@2275
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@2275
    20
*/
slouken@2275
    21
slouken@2275
    22
/**
slouken@3407
    23
 *  \file SDL_rect.h
slouken@7191
    24
 *
slouken@3407
    25
 *  Header file for SDL_rect definition and management functions.
slouken@2275
    26
 */
slouken@2275
    27
slouken@2275
    28
#ifndef _SDL_rect_h
slouken@2275
    29
#define _SDL_rect_h
slouken@2275
    30
slouken@2275
    31
#include "SDL_stdinc.h"
slouken@2275
    32
#include "SDL_error.h"
slouken@2275
    33
#include "SDL_pixels.h"
slouken@2275
    34
#include "SDL_rwops.h"
slouken@2275
    35
slouken@2275
    36
#include "begin_code.h"
slouken@2275
    37
/* Set up for C function definitions, even when using C++ */
slouken@2275
    38
#ifdef __cplusplus
slouken@2275
    39
extern "C" {
slouken@2275
    40
#endif
slouken@2275
    41
slouken@2275
    42
/**
slouken@3536
    43
 *  \brief  The structure that defines a point
slouken@3536
    44
 *
slouken@3536
    45
 *  \sa SDL_EnclosePoints
icculus@8930
    46
 *  \sa SDL_PointInRect
slouken@3536
    47
 */
slouken@7729
    48
typedef struct SDL_Point
slouken@3536
    49
{
slouken@3536
    50
    int x;
slouken@3536
    51
    int y;
slouken@3536
    52
} SDL_Point;
slouken@3536
    53
slouken@3536
    54
/**
slouken@3407
    55
 *  \brief A rectangle, with the origin at the upper left.
slouken@7191
    56
 *
slouken@3407
    57
 *  \sa SDL_RectEmpty
slouken@3407
    58
 *  \sa SDL_RectEquals
slouken@3407
    59
 *  \sa SDL_HasIntersection
slouken@3407
    60
 *  \sa SDL_IntersectRect
slouken@3407
    61
 *  \sa SDL_UnionRect
slouken@3536
    62
 *  \sa SDL_EnclosePoints
slouken@2275
    63
 */
slouken@2275
    64
typedef struct SDL_Rect
slouken@2275
    65
{
slouken@2275
    66
    int x, y;
slouken@2275
    67
    int w, h;
slouken@2275
    68
} SDL_Rect;
slouken@2275
    69
slouken@2275
    70
/**
icculus@8930
    71
 *  \brief Returns true if point resides inside a rectangle.
icculus@8930
    72
 */
icculus@8930
    73
SDL_FORCE_INLINE SDL_bool SDL_PointInRect(const SDL_Point *p, const SDL_Rect *r)
icculus@8930
    74
{
icculus@8930
    75
    return ( (p->x >= r->x) && (p->x < (r->x + r->w)) &&
icculus@8943
    76
             (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
icculus@8930
    77
}
icculus@8930
    78
icculus@8930
    79
/**
slouken@3407
    80
 *  \brief Returns true if the rectangle has no area.
slouken@2275
    81
 */
icculus@7185
    82
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
icculus@7185
    83
{
icculus@7185
    84
    return ((!r) || (r->w <= 0) || (r->h <= 0)) ? SDL_TRUE : SDL_FALSE;
icculus@7185
    85
}
slouken@2275
    86
slouken@2275
    87
/**
slouken@3407
    88
 *  \brief Returns true if the two rectangles are equal.
slouken@2275
    89
 */
icculus@7185
    90
SDL_FORCE_INLINE SDL_bool SDL_RectEquals(const SDL_Rect *a, const SDL_Rect *b)
icculus@7185
    91
{
icculus@7185
    92
    return (a && b && (a->x == b->x) && (a->y == b->y) &&
icculus@7185
    93
            (a->w == b->w) && (a->h == b->h)) ? SDL_TRUE : SDL_FALSE;
icculus@7185
    94
}
slouken@2275
    95
slouken@2275
    96
/**
slouken@3407
    97
 *  \brief Determine whether two rectangles intersect.
slouken@7191
    98
 *
slouken@3407
    99
 *  \return SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
slouken@2275
   100
 */
slouken@2275
   101
extern DECLSPEC SDL_bool SDLCALL SDL_HasIntersection(const SDL_Rect * A,
slouken@2275
   102
                                                     const SDL_Rect * B);
slouken@2275
   103
slouken@2275
   104
/**
slouken@3407
   105
 *  \brief Calculate the intersection of two rectangles.
slouken@7191
   106
 *
slouken@3407
   107
 *  \return SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
slouken@2275
   108
 */
slouken@2275
   109
extern DECLSPEC SDL_bool SDLCALL SDL_IntersectRect(const SDL_Rect * A,
slouken@2275
   110
                                                   const SDL_Rect * B,
slouken@2275
   111
                                                   SDL_Rect * result);
slouken@2275
   112
slouken@2275
   113
/**
slouken@3407
   114
 *  \brief Calculate the union of two rectangles.
slouken@2275
   115
 */
slouken@2275
   116
extern DECLSPEC void SDLCALL SDL_UnionRect(const SDL_Rect * A,
slouken@2275
   117
                                           const SDL_Rect * B,
slouken@2275
   118
                                           SDL_Rect * result);
slouken@2275
   119
slouken@2909
   120
/**
slouken@3536
   121
 *  \brief Calculate a minimal rectangle enclosing a set of points
slouken@3536
   122
 *
slouken@3536
   123
 *  \return SDL_TRUE if any points were within the clipping rect
slouken@3536
   124
 */
slouken@3536
   125
extern DECLSPEC SDL_bool SDLCALL SDL_EnclosePoints(const SDL_Point * points,
slouken@3536
   126
                                                   int count,
slouken@3536
   127
                                                   const SDL_Rect * clip,
slouken@3536
   128
                                                   SDL_Rect * result);
slouken@3536
   129
slouken@3536
   130
/**
slouken@3407
   131
 *  \brief Calculate the intersection of a rectangle and line segment.
slouken@7191
   132
 *
slouken@3407
   133
 *  \return SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
slouken@2909
   134
 */
slouken@2909
   135
extern DECLSPEC SDL_bool SDLCALL SDL_IntersectRectAndLine(const SDL_Rect *
slouken@2909
   136
                                                          rect, int *X1,
slouken@2909
   137
                                                          int *Y1, int *X2,
slouken@2909
   138
                                                          int *Y2);
slouken@2909
   139
slouken@2275
   140
/* Ends C function definitions when using C++ */
slouken@2275
   141
#ifdef __cplusplus
slouken@2275
   142
}
slouken@2275
   143
#endif
slouken@2275
   144
#include "close_code.h"
slouken@2275
   145
slouken@2275
   146
#endif /* _SDL_rect_h */
slouken@2275
   147
slouken@2275
   148
/* vi: set ts=4 sw=4 expandtab: */