Rev 5 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4 | tom | 1 | #include "SDL/SDL_image.h" |
2 | #include "SDL/SDL_opengl.h" |
||
3 | |||
4 | #include <iostream> |
||
5 | |||
6 | using namespace std; |
||
7 | |||
8 | #include "texture.hpp" |
||
9 | |||
10 | Texture::Texture(string filename) |
||
11 | { |
||
12 | loaded = false; |
||
13 | |||
10 | tom | 14 | GLenum textureFormat = GL_RGBA; |
4 | tom | 15 | |
16 | SDL_Surface *surface = IMG_Load(filename.c_str()); |
||
17 | if (surface) |
||
18 | { |
||
19 | GLint noOfColours = surface->format->BytesPerPixel; |
||
20 | if (noOfColours == 4) |
||
21 | { |
||
22 | if (surface->format->Rmask == 0x000000ff) |
||
23 | { |
||
24 | textureFormat = GL_RGBA; |
||
25 | } |
||
26 | else |
||
27 | { |
||
28 | textureFormat = GL_BGRA; |
||
29 | } |
||
30 | } |
||
31 | else if (noOfColours == 3) |
||
32 | { |
||
33 | if (surface->format->Rmask == 0x000000ff) |
||
34 | { |
||
35 | textureFormat = GL_RGB; |
||
36 | } |
||
37 | else |
||
38 | { |
||
39 | textureFormat = GL_BGR; |
||
40 | } |
||
41 | } |
||
42 | else |
||
43 | { |
||
44 | // Big Problem! |
||
45 | } |
||
46 | |||
47 | glGenTextures(1, &texture); |
||
48 | |||
49 | glBindTexture(GL_TEXTURE_2D, texture); |
||
50 | |||
51 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
||
52 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
||
53 | |||
54 | width = surface->w; |
||
55 | height = surface->h; |
||
56 | halfWidth = width * 0.5; |
||
57 | halfHeight = height * 0.5; |
||
58 | |||
59 | glTexImage2D(GL_TEXTURE_2D, 0, noOfColours, width, height, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels); |
||
60 | |||
61 | SDL_FreeSurface(surface); |
||
62 | |||
63 | loaded = true; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | Texture::~Texture() |
||
68 | { |
||
69 | if (loaded) |
||
70 | { |
||
71 | glDeleteTextures(1, &texture); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | void Texture::draw(int x, int y) |
||
76 | { |
||
77 | if (loaded) |
||
78 | { |
||
79 | glBindTexture(GL_TEXTURE_2D, texture); |
||
80 | |||
81 | int x1 = x - halfWidth; |
||
82 | int x2 = x + halfWidth; |
||
83 | int y1 = y - halfHeight; |
||
84 | int y2 = y + halfHeight; |
||
85 | |||
86 | glBegin(GL_QUADS); |
||
87 | //Bottom-left vertex (corner) |
||
88 | glTexCoord2i(0, 0); |
||
89 | glVertex3f(x1, y2, 0.0f); |
||
90 | |||
91 | //Bottom-right vertex (corner) |
||
92 | glTexCoord2i(1, 0); |
||
93 | glVertex3f(x2, y2, 0.f); |
||
94 | |||
95 | //Top-right vertex (corner) |
||
96 | glTexCoord2i(1, 1); |
||
97 | glVertex3f(x2, y1, 0.f); |
||
98 | |||
99 | //Top-left vertex (corner) |
||
100 | glTexCoord2i(0, 1); |
||
101 | glVertex3f(x1, y1, 0.f); |
||
102 | glEnd(); |
||
103 | } |
||
104 | } |
||
5 | tom | 105 | |
106 | void drawTexture(Texture *texture, int x, int y) |
||
107 | { |
||
108 | if (texture) |
||
109 | { |
||
110 | texture->draw(x, y); |
||
111 | } |
||
112 | } |