-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtexture.cpp
More file actions
83 lines (67 loc) · 2.07 KB
/
Copy pathtexture.cpp
File metadata and controls
83 lines (67 loc) · 2.07 KB
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
#include "texture.h"
#include <iostream>
using namespace std;
/****************************************************************************/
/** \file texture.cpp
\brief The CTexture class implementation */
/****************************************************************************/
// LoadTexture()
/** Loads a texture given the filename.
The engine can load BMP, PNM (PPM/PGM/PBM), XPM, LBM, PCX, GIF, JPEG,
PNG, TGA, and TIFF formats.
*****************************************************************************/
void CTexture::LoadTexture(char *filename)
{
image = IMG_Load(filename);
if (!image) {
cerr << "Error al cargar la textura" << endl;
}
width = image->w;
height = image->h;
bitDepth = image->format->BitsPerPixel;
data = new GLubyte[width * height * (bitDepth/8)];
int pos = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Uint8 r, g, b ,a;
Uint32 color = getpixel(image, x, y);
if (bitDepth == 24) {
SDL_GetRGB(color, image->format, &r, &g, &b);
}
else {
SDL_GetRGBA(color, image->format, &r, &g, &b, &a);
}
data[pos] = r; pos++;
data[pos] = g; pos++;
data[pos] = b; pos++;
if (bitDepth == 32) {
data[pos] = a;
pos ++;
}
}
}
}
/****************************************************************************/
/** \return the pixel value at (x, y)
*****************************************************************************/
Uint32 CTexture::getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}