2 // C Implementation: texture
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
9 // Copyright: See COPYING file that comes with this distribution
15 #include "SDL_image.h"
20 Surface::Surfaces Surface::surfaces;
22 SurfaceData::SurfaceData(SDL_Surface* surf, int use_alpha_)
23 : type(SURFACE), surface(surf), use_alpha(use_alpha_)
27 SurfaceData::SurfaceData(const std::string& file_, int use_alpha_)
28 : type(LOAD), file(file_), use_alpha(use_alpha_)
32 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_)
33 : type(LOAD_PART), file(file_), use_alpha(use_alpha_),
34 x(x_), y(y_), w(w_), h(h_)
42 return create_SurfaceOpenGL();
44 return create_SurfaceSDL();
48 SurfaceData::create_SurfaceSDL()
53 return new SurfaceSDL(file, use_alpha);
55 return new SurfaceSDL(file, x, y, w, h, use_alpha);
57 return 0; //new SurfaceSDL(surface, use_alpha);
63 SurfaceData::create_SurfaceOpenGL()
68 return new SurfaceOpenGL(file, use_alpha);
70 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
72 return 0; //new SurfaceOpenGL(surface, use_alpha);
78 /* Quick utility function for texture creation */
79 static int power_of_two(int input)
83 while ( value < input ) {
89 Surface::Surface(SDL_Surface* surf, int use_alpha)
90 : data(surf, use_alpha), w(0), h(0)
98 surfaces.push_back(this);
101 Surface::Surface(const std::string& file, int use_alpha)
102 : data(file, use_alpha), w(0), h(0)
104 impl = data.create();
110 surfaces.push_back(this);
113 Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha)
114 : data(file, x, y, w, h, use_alpha), w(0), h(0)
116 impl = data.create();
122 surfaces.push_back(this);
129 impl = data.create();
139 surfaces.remove(this);
144 Surface::reload_all()
146 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
153 Surface::draw(float x, float y, Uint8 alpha, bool update)
155 if (impl) impl->draw(x, y, alpha, update);
159 Surface::draw_bg(Uint8 alpha, bool update)
161 if (impl) impl->draw_bg(alpha, update);
165 Surface::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
167 if (impl) impl->draw_part(sx, sy, x, y, w, h, alpha, update);
171 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
174 SDL_Surface * sdl_surface;
178 temp = IMG_Load(file.c_str());
181 st_abort("Can't load", file);
183 /* Set source rectangle for conv: */
190 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
194 temp->format->Amask);
196 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
197 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
200 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
203 SDL_SetAlpha(temp,0,0);
205 SDL_BlitSurface(temp, &src, conv, NULL);
206 if(use_alpha == IGNORE_ALPHA && !use_gl)
207 sdl_surface = SDL_DisplayFormat(conv);
209 sdl_surface = SDL_DisplayFormatAlpha(conv);
211 if (sdl_surface == NULL)
212 st_abort("Can't covert to display format", file);
214 if (use_alpha == IGNORE_ALPHA && !use_gl)
215 SDL_SetAlpha(sdl_surface, 0, 0);
217 SDL_FreeSurface(temp);
218 SDL_FreeSurface(conv);
224 sdl_surface_from_file(const std::string& file, int use_alpha)
226 SDL_Surface* sdl_surface;
229 temp = IMG_Load(file.c_str());
232 st_abort("Can't load", file);
234 if(use_alpha == IGNORE_ALPHA && !use_gl)
235 sdl_surface = SDL_DisplayFormat(temp);
237 sdl_surface = SDL_DisplayFormatAlpha(temp);
239 if (sdl_surface == NULL)
240 st_abort("Can't covert to display format", file);
242 if (use_alpha == IGNORE_ALPHA && !use_gl)
243 SDL_SetAlpha(sdl_surface, 0, 0);
245 SDL_FreeSurface(temp);
251 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
253 SDL_Surface* sdl_surface;
257 /* Save the alpha blending attributes */
258 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
259 saved_alpha = sdl_surf->format->alpha;
260 if ( (saved_flags & SDL_SRCALPHA)
263 SDL_SetAlpha(sdl_surf, 0, 0);
266 if(use_alpha == IGNORE_ALPHA && !use_gl)
267 sdl_surface = SDL_DisplayFormat(sdl_surf);
269 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
271 /* Restore the alpha blending attributes */
272 if ( (saved_flags & SDL_SRCALPHA)
275 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
278 if (sdl_surface == NULL)
279 st_abort("Can't covert to display format", "SURFACE");
281 if (use_alpha == IGNORE_ALPHA && !use_gl)
282 SDL_SetAlpha(sdl_surface, 0, 0);
288 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
290 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
291 create_gl(sdl_surface,&gl_texture);
297 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
299 sdl_surface = sdl_surface_from_file(file, use_alpha);
300 create_gl(sdl_surface,&gl_texture);
306 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
308 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
309 create_gl(sdl_surface, &gl_texture);
315 SurfaceOpenGL::~SurfaceOpenGL()
317 SDL_FreeSurface(sdl_surface);
318 glDeleteTextures(1, &gl_texture);
322 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
329 w = power_of_two(surf->w);
330 h = power_of_two(surf->h),
332 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
333 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
334 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
336 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
337 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
340 /* Save the alpha blending attributes */
341 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
342 saved_alpha = surf->format->alpha;
343 if ( (saved_flags & SDL_SRCALPHA)
346 SDL_SetAlpha(surf, 0, 0);
349 SDL_BlitSurface(surf, 0, conv, 0);
351 /* Restore the alpha blending attributes */
352 if ( (saved_flags & SDL_SRCALPHA)
355 SDL_SetAlpha(surf, saved_flags, saved_alpha);
358 glGenTextures(1, &*tex);
359 glBindTexture(GL_TEXTURE_2D , *tex);
360 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
361 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
362 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
363 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
364 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
365 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
366 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
368 SDL_FreeSurface(conv);
372 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
374 float pw = power_of_two(w);
375 float ph = power_of_two(h);
377 glEnable(GL_TEXTURE_2D);
379 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
381 glColor4ub(alpha, alpha, alpha, alpha);
383 glBindTexture(GL_TEXTURE_2D, gl_texture);
388 glTexCoord2f((float)w / pw, 0);
389 glVertex2f((float)w+x, y);
390 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)w+x, (float)h+y);
391 glTexCoord2f(0, (float)h / ph);
392 glVertex2f(x, (float)h+y);
395 glDisable(GL_TEXTURE_2D);
398 /* Avoid compiler warnings */
404 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
406 float pw = power_of_two(w);
407 float ph = power_of_two(h);
409 glColor3ub(alpha, alpha, alpha);
411 glEnable(GL_TEXTURE_2D);
412 glBindTexture(GL_TEXTURE_2D, gl_texture);
417 glTexCoord2f((float)w / pw, 0);
418 glVertex2f(screen->w, 0);
419 glTexCoord2f((float)w / pw, (float)h / ph);
420 glVertex2f(screen->w, screen->h);
421 glTexCoord2f(0, (float)h / ph);
422 glVertex2f(0, screen->h);
425 glDisable(GL_TEXTURE_2D);
427 /* Avoid compiler warnings */
433 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
435 float pw = power_of_two(int(this->w));
436 float ph = power_of_two(int(this->h));
438 glBindTexture(GL_TEXTURE_2D, gl_texture);
441 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
443 glColor4ub(alpha, alpha, alpha, alpha);
445 glEnable(GL_TEXTURE_2D);
449 glTexCoord2f(sx / pw, sy / ph);
451 glTexCoord2f((float)(sx + w) / pw, sy / ph);
453 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
454 glVertex2f(w +x, h+y);
455 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
459 glDisable(GL_TEXTURE_2D);
462 /* Avoid compiler warnings */
468 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
470 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
475 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
477 sdl_surface = sdl_surface_from_file(file, use_alpha);
482 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
484 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
490 SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
499 if(alpha != 255) /* SDL isn't capable of this kind of alpha :( therefore we'll leave now. */
502 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
503 SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
505 if (update == UPDATE)
506 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
510 SurfaceSDL::draw_bg(Uint8 alpha, bool update)
520 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
521 SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
523 if (update == UPDATE)
524 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
528 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
543 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
545 SDL_BlitSurface(sdl_surface, &src, screen, &dest);
547 if (update == UPDATE)
548 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
551 SurfaceSDL::~SurfaceSDL()
553 SDL_FreeSurface(sdl_surface);