Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/CSurface.cpp
blob: 645f089c951be33003c67f3c1dc02c4d0589cff3 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "CSurface.h"

CSurface::CSurface(void)
{
	mImage = NULL;
}

CSurface::CSurface(int aWidth, int aHeight)
{
	// create_blank_surface: Generates a new surface based on the
//      current video settings.

    // Acquire the settings in the surface.
    mImage = SDL_GetVideoSurface();

    // Create the new surface using the Right-To_Left principle.
    mImage = SDL_CreateRGBSurface (mImage->flags, aWidth, aHeight, 
									mImage->format->BitsPerPixel,
                                    mImage->format->Rmask,
                                    mImage->format->Gmask,
                                    mImage->format->Bmask,
									mImage->format->Amask);
	
	SDL_SetColorKey(mImage, SDL_SRCCOLORKEY, SDL_MapRGB(mImage->format, 255, 0, 255));
}

CSurface::CSurface(const char *aFile)
{
	mImage = CSurface::loadImage(aFile);
}

CSurface::CSurface(SDL_Surface *aSDLSurface)
{
	mImage = aSDLSurface;
}

CSurface::CSurface(SDL_Surface *aSDLSurface, int aFlipFlags)
{
	mImage = flipSurface(aSDLSurface, aFlipFlags);
}

CSurface::~CSurface(void)
{
	destroy();
}

void CSurface::destroy(void)
{
	if (mImage != NULL)
	{
		SDL_FreeSurface(mImage);
		mImage = NULL;
	}
}

//--------------------------------------------------------------------------------------------------
// getImage()
// Returns the image. 
//
// Arguments:
// None.
//
// Returns: Pointer to the image.
//--------------------------------------------------------------------------------------------------
SDL_Surface *CSurface::getImage(void)
{
	return mImage;
}

//--------------------------------------------------------------------------------------------------
// draw()
// Blits the entire image on (x,y) coordinates. 
//
// Arguments:
// aImgDst: Surface in which we draw the image.
// aX, aY: Coordinates where to draw.
//
// Returns: Nothing.
//--------------------------------------------------------------------------------------------------
void CSurface::draw(SDL_Surface *aImgDst, int aX, int aY)
{
	CSurface::drawImage(aImgDst, mImage, aX, aY);
}

//--------------------------------------------------------------------------------------------------
// loadImage()
// It loads an image and returns a pointer to the image converted to the screen format.
//
// Arguments:
// aFile: Name of the image file to load.
//
// Returns: Pointer to the created surface. NULL if there is an error.
//--------------------------------------------------------------------------------------------------
SDL_Surface *CSurface::loadImage(const char *aFile)
{
	SDL_Surface *loadedImg = NULL;
	SDL_Surface *optimizedImg = NULL;

	//loadedImg = SDL_LoadBMP(file);
    
	// Load the image using SDL_image.
    return loadedImg = IMG_Load(aFile);

	if (loadedImg != NULL)
	{
		optimizedImg = SDL_DisplayFormat(loadedImg);
		// If the conversion fails or runs out of memory...
		if (optimizedImg == NULL)
		{
			fprintf(stderr, "SDL_DisplayFormat() failed!\n");
			fprintf(stderr, "Error: %s\n", SDL_GetError());
		}
		//else
		//{
		//	SDL_SetColorKey(optimizedImg, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImg->format, 0, 0xFF, 0xFF));
		//}

		SDL_FreeSurface(loadedImg);
	}
	else
	{
		fprintf(stderr, "loadImage() failed!\n");
		fprintf(stderr, "Error: %s\n", SDL_GetError());
	}

	return optimizedImg;
}

//--------------------------------------------------------------------------------------------------
// drawImage()
// Blits the entire image on (x,y) coordinates.
//
// Arguments:
// aImgDst: Surface in which we draw the image.
// aImgSrc: Image to be drawn.
// aX, aY: Coordinates where to draw.
//
// Returns: Nothing.
//--------------------------------------------------------------------------------------------------
void CSurface::drawImage(SDL_Surface *aImgDst, SDL_Surface *aImgSrc, int aX, int aY)
{
	SDL_Rect dest;
	dest.x = aX;
	dest.y = aY;
	SDL_BlitSurface(aImgSrc, NULL, aImgDst, &dest);
}

//--------------------------------------------------------------------------------------------------
// drawImage()
// Blits the rectangle (xSrc,ySrc,wSrc,hSrc) of the source image on (x,y) coordinates of the 
// destination image.
//
// Arguments:
// aImgDst: Surface in which we draw the image.
// aImgSrc: Source image where the rectangle to be drawn resides in.
// xDest, yDest: Coordinates where to draw in the destination image.
// xSrc, ySrc: Coordinates of the rectangle to be drawn (in the source image).
// wSrc, hSrc: Width and height of the rectangle to be drawn (in the source image).
//
// Returns: Nothing.
//--------------------------------------------------------------------------------------------------
void CSurface::drawImage(SDL_Surface *aImgDst, SDL_Surface *aImgSrc, int aXDest, int aYDest, int aXSrc, int aYSrc, int aWSrc, int aHSrc)
{
	SDL_Rect srcRect;
	srcRect.x = aXSrc;
	srcRect.y = aYSrc;
	srcRect.w = aWSrc;
	srcRect.h = aHSrc;

	SDL_Rect srcDest;
	srcDest.x = aXDest;
	srcDest.y = aYDest;

	SDL_BlitSurface(aImgSrc, &srcRect, aImgDst, &srcDest);
}

//--------------------------------------------------------------------------------------------------
// drawImage()
// Blits the entire image on (x,y) coordinates.
//
// Arguments:
// aImgDst: Surface in which we draw the image.
// aImgSrc: Image to be drawn.
// aX, aY: Coordinates where to draw.
//
// Returns: Nothing.
//--------------------------------------------------------------------------------------------------
void CSurface::drawImage(CSurface *aImgDst, CSurface *aImgSrc, int aX, int aY)
{
	CSurface::drawImage(aImgDst->getImage(), aImgSrc->getImage(), aX, aY);
}

//--------------------------------------------------------------------------------------------------
// drawImage()
// Blits the rectangle (xSrc,ySrc,wSrc,hSrc) of the source image on (x,y) coordinates of the 
// destination image.
//
// Arguments:
// aImgDst: Surface in which we draw the image.
// aImgSrc: Source image where the rectangle to be drawn resides in.
// xDest, yDest: Coordinates where to draw in the destination image.
// xSrc, ySrc: Coordinates of the rectangle to be drawn (in the source image).
// wSrc, hSrc: Width and height of the rectangle to be drawn (in the source image).
//
// Returns: Nothing.
//--------------------------------------------------------------------------------------------------
void CSurface::drawImage(CSurface *aImgDst, CSurface *aImgSrc, int aXDest, int aYDest, int aXSrc, int aYSrc, int aWSrc, int aHSrc)
{
	CSurface::drawImage(aImgDst->getImage(), aImgSrc->getImage(), aXDest, aYDest, aXSrc, aYSrc, aWSrc, aHSrc);
}

void CSurface::drawRect(SDL_Surface *aImgDst, int aX, int aY, int aW, int aH, int aColor)
{
	SDL_Rect dest;
	dest.x = aX;
	dest.y = aY;
	dest.w = aW;
	dest.h = aH;
	SDL_FillRect(aImgDst, &dest, aColor);
}

void CSurface::drawRect(CSurface *aImgDst, int aX, int aY, int aW, int aH, int aColor)
{
	CSurface::drawRect(aImgDst->getImage(), aX, aY, aW, aH, aColor);
}


Uint32 CSurface::get_pixel32(SDL_Surface *surface, int x, int y)
{
    //Convert the pixels to 32 bit
    Uint32 *pixels = (Uint32 *)surface->pixels;

    //Get the requested pixel
    return pixels[ ( y * surface->w ) + x ];
}

void CSurface::put_pixel32( SDL_Surface *surface, int x, int y, Uint32 pixel )
{
    //Convert the pixels to 32 bit
    Uint32 *pixels = (Uint32 *)surface->pixels;

    //Set the pixel
    pixels[ ( y * surface->w ) + x ] = pixel;
}

SDL_Surface *CSurface::flipSurface(SDL_Surface *surface, int flags)
{
    //Pointer to the soon to be flipped surface
    SDL_Surface *flipped = NULL;

    //If the image is color keyed
    if( surface->flags & SDL_SRCCOLORKEY )
    {
        flipped = SDL_CreateRGBSurface( SDL_SWSURFACE, surface->w, surface->h, surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, 0 );
    }
    //Otherwise
    else
    {
        flipped = SDL_CreateRGBSurface( SDL_SWSURFACE, surface->w, surface->h, surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask );
    }

    //If the surface must be locked
    if( SDL_MUSTLOCK( surface ) )
    {
        //Lock the surface
        SDL_LockSurface( surface );
    }

    //Go through columns
    for( int x = 0, rx = flipped->w - 1; x < flipped->w; x++, rx-- )
    {
        //Go through rows
        for( int y = 0, ry = flipped->h - 1; y < flipped->h; y++, ry-- )
        {
            //Get pixel
            Uint32 pixel = get_pixel32( surface, x, y );

            //Copy pixel
            if( ( flags & FLIP_VERTICAL ) && ( flags & FLIP_HORIZONTAL ) )
            {
                put_pixel32( flipped, rx, ry, pixel );
            }
            else if( flags & FLIP_HORIZONTAL )
            {
                put_pixel32( flipped, rx, y, pixel );
            }
            else if( flags & FLIP_VERTICAL )
            {
                put_pixel32( flipped, x, ry, pixel );
            }
        }
    }

    //Unlock surface
    if( SDL_MUSTLOCK( surface ) )
    {
        SDL_UnlockSurface( surface );
    }

    //Copy color key
    if( surface->flags & SDL_SRCCOLORKEY )
    {
        SDL_SetColorKey( flipped, SDL_RLEACCEL | SDL_SRCCOLORKEY, surface->format->colorkey );
    }

    //Return flipped surface
    return flipped;
}