File: SDLTools.h

package info (click to toggle)
moagg 0.18-6
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,924 kB
  • ctags: 4,059
  • sloc: cpp: 23,814; sh: 2,652; makefile: 283
file content (157 lines) | stat: -rw-r--r-- 5,582 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef SDLTOOLS_H
#define SDLTOOLS_H

#include "SDLCalls.h"


//----------------------------------------------------------------------------
/**
 * This namespace contains some common needed functions
 * related to SDL datastructures.
 */
namespace SDL_TOOLS
{
    //------------------------------------------------------------------------
    /**
     * @return The pixel x/y of the given surface.
     */
    Uint32 getPixel(const SDL_Surface *surface, Uint16 x, Uint16 y);

    //------------------------------------------------------------------------
    /**
     * Returns, whether the given point (x/y) lies inside the given rectangle.
     *
     * @param x The x coordinate of the point.
     * @param y The y coordinate of the point.
     * @param r The rectangle to check.
     *
     * @return true, if the point (x/y) lies inside r, else false.
     */
    bool inside(const Sint16 x, const Sint16 y, const SDL_Rect &r);

    //------------------------------------------------------------------------
    /**
     * Returns, whether the given point (x/y) lies inside the given surface.
     *
     * @param x The x coordinate of the point.
     * @param y The y coordinate of the point.
     * @param s The surface to check.
     *
     * @return true, if the point (x/y) lies inside s, else false.
     */
    bool inside(const Sint16 x, const Sint16 y, const SDL_Surface *s);

    //------------------------------------------------------------------------
    /**
     * Calculate the intersection of two rectangles.
     *
     * @param r1 A const reference to the first rectangle.
     * @param r2 A const reference to the second rectangle.
     * @param clip A reference to a rectangle,
     *             where the result will be stored.
     *
     * @return true, if there is an intersection, else false.
     */
    bool intersect(const SDL_Rect &r1, const SDL_Rect &r2, SDL_Rect &clip);

    //------------------------------------------------------------------------
    /**
     * Calculates the union of two rectangles.
     *
     * @param r1 A const reference to the first rectangle.
     * @param r2 A const reference to the second rectangle.
     * @param u A reference to a rectangle,
     *          where the result will be stored.
     */
    void unite(const SDL_Rect &r1, const SDL_Rect &r2, SDL_Rect &u);

    //------------------------------------------------------------------------
    /**
     * Checks, whether two SDL_Rects are equal.
     *
     * @param r1 A const reference to the first rectangle.
     * @param r2 A const reference to the first rectangle.
     *
     * @return true, if r1 is equal to r2, else false.
     */
    inline bool equal(const SDL_Rect &r1, const SDL_Rect &r2)
    {
        return r1.x == r2.x && r1.y == r2.y && r1.w == r2.w && r1.h == r2.h;
    }


    //------------------------------------------------------------------------
    /**
     * @param src The source surface to rotate.
     * @param angle The angle (0, 90, 180 or 270).
     *
     * @return A pointer to the new rotated surface.
     *
     * @throw SDLException if an SDL error occured.
     */
    SDL_Surface *rotate(const SDL_Surface *src, Uint16 angle);

    //------------------------------------------------------------------------
    /**
     * @param src The source surface to flip in x-direction.
     *
     * @return A pointer to the new flipped surface.
     *
     * @throw SDLException if an SDL error occured.
     */
    SDL_Surface *flipX(const SDL_Surface *src);

    //------------------------------------------------------------------------
    /**
     * @param src The source surface to flip in y-direction.
     *
     * @return A pointer to the new flipped surface.
     *
     * @throw SDLException if an SDL error occured.
     */
    SDL_Surface *flipY(const SDL_Surface *src);

    //------------------------------------------------------------------------
    /**
     * @param s1 The first surface.
     * @param p1 The position of the first surface.
     * @param s2 The second surface.
     * @param p2 The position of the second surface.
     *
     * @return true, if there is a collision between s1 and s2, else false.
     */
    bool isCollision(const SDL_Surface *s1, const SDL_Rect &p1,
                     const SDL_Surface *s2, const SDL_Rect &p2);

    //------------------------------------------------------------------------
    /**
     * Returns the angle between the mid-points
     * of the two given surface boxes relative to the y axis,
     * counted in clockwise order and inside the value range from [0..360).
     */
    int getAngle(const SDL_Rect &p1, const SDL_Rect &p2);

    //------------------------------------------------------------------------
    /**
     * Calculates the centre of the given SDL_Rect
     * and stores the x and y coordinate into the given integer variables.
     */
    void getCentre(const SDL_Rect &r, Sint16 &x, Sint16 &y);

    //------------------------------------------------------------------------
    /**
     * Calls SDL_CALLS::LoadBMP() and sets the SDL_SRCCOLORKEY
     * to the given rgb value, before returning the surface.
     */
    SDL_Surface *loadBMPWithColorKey(const char *file,
                                     Uint8 r = 0, Uint8 g = 0, Uint8 b = 0);

    //------------------------------------------------------------------------
    /**
     * @param s A pointer to the surface to clone.
     * @return A pointer to the cloned surface.
     */
    SDL_Surface *cloneSurface(const SDL_Surface *s);
}

#endif //SDLTOOLS_H