1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER |
---|
17 | |
---|
18 | #include "texture.h" |
---|
19 | |
---|
20 | #include "debug.h" |
---|
21 | #include "graphics_engine.h" |
---|
22 | |
---|
23 | #include <SDL_image.h> |
---|
24 | |
---|
25 | /** |
---|
26 | \brief Constructor for a Texture |
---|
27 | */ |
---|
28 | Texture::Texture(const char* imageName) |
---|
29 | { |
---|
30 | bAlpha = false; |
---|
31 | this->texture = 0; |
---|
32 | if (imageName) |
---|
33 | this->loadImage(imageName); |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | \brief Destructor of a Texture |
---|
38 | |
---|
39 | Frees Data, and deletes the textures from GL |
---|
40 | */ |
---|
41 | Texture::~Texture(void) |
---|
42 | { |
---|
43 | if (this->texture) |
---|
44 | glDeleteTextures(1, &this->texture); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | \brief Loads a Texture to the openGL-environment. |
---|
49 | \param surface the Image to load to openGL |
---|
50 | \returns The ID of the texture. |
---|
51 | */ |
---|
52 | GLuint Texture::loadTexToGL (SDL_Surface* surface) |
---|
53 | { |
---|
54 | if (GraphicsEngine::texturesEnabled) |
---|
55 | { |
---|
56 | PRINTF(4)("Loading texture to OpenGL-Environment.\n"); |
---|
57 | |
---|
58 | GLuint texture; |
---|
59 | int w, h; |
---|
60 | SDL_Surface *image; |
---|
61 | SDL_Rect area; |
---|
62 | Uint32 saved_flags; |
---|
63 | Uint8 saved_alpha; |
---|
64 | |
---|
65 | w = surface->w; |
---|
66 | h = surface->h; |
---|
67 | |
---|
68 | image = SDL_CreateRGBSurface(SDL_SWSURFACE, |
---|
69 | w, h, |
---|
70 | 32, |
---|
71 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
---|
72 | 0x000000FF, |
---|
73 | 0x0000FF00, |
---|
74 | 0x00FF0000, |
---|
75 | 0xFF000000 |
---|
76 | #else |
---|
77 | 0xFF000000, |
---|
78 | 0x00FF0000, |
---|
79 | 0x0000FF00, |
---|
80 | 0x000000FF |
---|
81 | #endif |
---|
82 | ); |
---|
83 | if ( image == NULL ) { |
---|
84 | return 0; |
---|
85 | } |
---|
86 | |
---|
87 | /* Save the alpha blending attributes */ |
---|
88 | saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); |
---|
89 | saved_alpha = surface->format->alpha; |
---|
90 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
---|
91 | SDL_SetAlpha(surface, 0, 0); |
---|
92 | } |
---|
93 | |
---|
94 | /* Copy the surface into the GL texture image */ |
---|
95 | area.x = 0; |
---|
96 | area.y = 0; |
---|
97 | area.w = surface->w; |
---|
98 | area.h = surface->h; |
---|
99 | SDL_BlitSurface(surface, &area, image, &area); |
---|
100 | |
---|
101 | /* Restore the alpha blending attributes */ |
---|
102 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
---|
103 | SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha); |
---|
104 | this->bAlpha = true; |
---|
105 | } |
---|
106 | |
---|
107 | /* Create an OpenGL texture for the image */ |
---|
108 | glGenTextures(1, &texture); |
---|
109 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
110 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
111 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
112 | // build the Texture |
---|
113 | glTexImage2D(GL_TEXTURE_2D, |
---|
114 | 0, |
---|
115 | GL_RGBA, |
---|
116 | w, h, |
---|
117 | 0, |
---|
118 | GL_RGBA, |
---|
119 | GL_UNSIGNED_BYTE, |
---|
120 | image->pixels); |
---|
121 | // build the MipMaps |
---|
122 | gluBuild2DMipmaps(GL_TEXTURE_2D, |
---|
123 | GL_RGBA, |
---|
124 | w, |
---|
125 | h, |
---|
126 | GL_RGBA, |
---|
127 | GL_UNSIGNED_BYTE, |
---|
128 | image->pixels); |
---|
129 | |
---|
130 | SDL_FreeSurface(image); /* No longer needed */ |
---|
131 | |
---|
132 | return texture; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | \brief loads an Image from a file to a Texture |
---|
138 | \param imageName The image to load |
---|
139 | */ |
---|
140 | bool Texture::loadImage(const char* imageName) |
---|
141 | { |
---|
142 | if (GraphicsEngine::texturesEnabled) |
---|
143 | { |
---|
144 | if (imageName) |
---|
145 | { |
---|
146 | SDL_Surface* tmpSurf; |
---|
147 | if (this->texture) |
---|
148 | glDeleteTextures(1, &this->texture); |
---|
149 | // load the new Image to memory |
---|
150 | tmpSurf = IMG_Load(imageName); |
---|
151 | if(!tmpSurf) |
---|
152 | { |
---|
153 | PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); |
---|
154 | return false; |
---|
155 | } |
---|
156 | |
---|
157 | GLubyte* pixels = (GLubyte*)tmpSurf->pixels; |
---|
158 | |
---|
159 | PRINTF(3)("loading Image %s\n", imageName); |
---|
160 | if (tmpSurf) |
---|
161 | this->texture = loadTexToGL(tmpSurf); |
---|
162 | |
---|
163 | |
---|
164 | SDL_FreeSurface(tmpSurf); |
---|
165 | return true; |
---|
166 | } |
---|
167 | else |
---|
168 | { |
---|
169 | PRINTF(2)("Image not Found: %s\n", imageName); |
---|
170 | return false; |
---|
171 | } |
---|
172 | } |
---|
173 | } |
---|