2 // C Implementation: texture
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
9 // Copyright: See COPYING file that comes with this distribution
16 #include "SDL_image.h"
21 Surface::Surfaces Surface::surfaces;
23 SurfaceData::SurfaceData(SDL_Surface* temp, int use_alpha_)
24 : type(SURFACE), use_alpha(use_alpha_)
26 // Copy the given surface and make sure that it is not stored in
28 surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
30 temp->format->BitsPerPixel,
35 SDL_SetAlpha(temp,0,0);
36 SDL_BlitSurface(temp, NULL, surface, NULL);
39 SurfaceData::SurfaceData(const std::string& file_, int use_alpha_)
40 : type(LOAD), file(file_), use_alpha(use_alpha_)
44 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_)
45 : type(LOAD_PART), file(file_), use_alpha(use_alpha_),
46 x(x_), y(y_), w(w_), h(h_)
50 SurfaceData::~SurfaceData()
59 return create_SurfaceOpenGL();
61 return create_SurfaceSDL();
65 SurfaceData::create_SurfaceSDL()
70 return new SurfaceSDL(file, use_alpha);
72 return new SurfaceSDL(file, x, y, w, h, use_alpha);
74 return new SurfaceSDL(surface, use_alpha);
80 SurfaceData::create_SurfaceOpenGL()
85 return new SurfaceOpenGL(file, use_alpha);
87 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
89 return new SurfaceOpenGL(surface, use_alpha);
95 /* Quick utility function for texture creation */
96 static int power_of_two(int input)
100 while ( value < input ) {
106 Surface::Surface(SDL_Surface* surf, int use_alpha)
107 : data(surf, use_alpha), w(0), h(0)
109 impl = data.create();
115 surfaces.push_back(this);
118 Surface::Surface(const std::string& file, int use_alpha)
119 : data(file, use_alpha), w(0), h(0)
121 impl = data.create();
127 surfaces.push_back(this);
130 Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha)
131 : data(file, x, y, w, h, use_alpha), w(0), h(0)
133 impl = data.create();
139 surfaces.push_back(this);
146 impl = data.create();
156 surfaces.remove(this);
161 Surface::reload_all()
163 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
170 Surface::draw(float x, float y, Uint8 alpha, bool update)
174 if (impl->draw(x, y, alpha, update) == -2)
180 Surface::draw_bg(Uint8 alpha, bool update)
184 if (impl->draw_bg(alpha, update) == -2)
190 Surface::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
194 if (impl->draw_part(sx, sy, x, y, w, h, alpha, update) == -2)
200 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
203 SDL_Surface * sdl_surface;
207 temp = IMG_Load(file.c_str());
210 st_abort("Can't load", file);
212 /* Set source rectangle for conv: */
219 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
223 temp->format->Amask);
225 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
226 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
229 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
232 SDL_SetAlpha(temp,0,0);
234 SDL_BlitSurface(temp, &src, conv, NULL);
235 if(use_alpha == IGNORE_ALPHA && !use_gl)
236 sdl_surface = SDL_DisplayFormat(conv);
238 sdl_surface = SDL_DisplayFormatAlpha(conv);
240 if (sdl_surface == NULL)
241 st_abort("Can't covert to display format", file);
243 if (use_alpha == IGNORE_ALPHA && !use_gl)
244 SDL_SetAlpha(sdl_surface, 0, 0);
246 SDL_FreeSurface(temp);
247 SDL_FreeSurface(conv);
253 sdl_surface_from_file(const std::string& file, int use_alpha)
255 SDL_Surface* sdl_surface;
258 temp = IMG_Load(file.c_str());
261 st_abort("Can't load", file);
263 if(use_alpha == IGNORE_ALPHA && !use_gl)
264 sdl_surface = SDL_DisplayFormat(temp);
266 sdl_surface = SDL_DisplayFormatAlpha(temp);
268 if (sdl_surface == NULL)
269 st_abort("Can't covert to display format", file);
271 if (use_alpha == IGNORE_ALPHA && !use_gl)
272 SDL_SetAlpha(sdl_surface, 0, 0);
274 SDL_FreeSurface(temp);
280 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
282 SDL_Surface* sdl_surface;
286 /* Save the alpha blending attributes */
287 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
288 saved_alpha = sdl_surf->format->alpha;
289 if ( (saved_flags & SDL_SRCALPHA)
292 SDL_SetAlpha(sdl_surf, 0, 0);
295 if(use_alpha == IGNORE_ALPHA && !use_gl)
296 sdl_surface = SDL_DisplayFormat(sdl_surf);
298 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
300 /* Restore the alpha blending attributes */
301 if ( (saved_flags & SDL_SRCALPHA)
304 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
307 if (sdl_surface == NULL)
308 st_abort("Can't covert to display format", "SURFACE");
310 if (use_alpha == IGNORE_ALPHA && !use_gl)
311 SDL_SetAlpha(sdl_surface, 0, 0);
317 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
319 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
320 create_gl(sdl_surface,&gl_texture);
326 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
328 sdl_surface = sdl_surface_from_file(file, use_alpha);
329 create_gl(sdl_surface,&gl_texture);
335 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
337 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
338 create_gl(sdl_surface, &gl_texture);
344 SurfaceOpenGL::~SurfaceOpenGL()
346 SDL_FreeSurface(sdl_surface);
347 glDeleteTextures(1, &gl_texture);
351 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
358 w = power_of_two(surf->w);
359 h = power_of_two(surf->h),
361 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
362 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
363 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
365 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
366 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
369 /* Save the alpha blending attributes */
370 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
371 saved_alpha = surf->format->alpha;
372 if ( (saved_flags & SDL_SRCALPHA)
375 SDL_SetAlpha(surf, 0, 0);
378 SDL_BlitSurface(surf, 0, conv, 0);
380 /* Restore the alpha blending attributes */
381 if ( (saved_flags & SDL_SRCALPHA)
384 SDL_SetAlpha(surf, saved_flags, saved_alpha);
387 glGenTextures(1, &*tex);
388 glBindTexture(GL_TEXTURE_2D , *tex);
389 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
390 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
391 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
392 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
393 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
394 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
395 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
397 SDL_FreeSurface(conv);
401 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
403 float pw = power_of_two(w);
404 float ph = power_of_two(h);
406 glEnable(GL_TEXTURE_2D);
408 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
410 glColor4ub(alpha, alpha, alpha, alpha);
412 glBindTexture(GL_TEXTURE_2D, gl_texture);
417 glTexCoord2f((float)w / pw, 0);
418 glVertex2f((float)w+x, y);
419 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)w+x, (float)h+y);
420 glTexCoord2f(0, (float)h / ph);
421 glVertex2f(x, (float)h+y);
424 glDisable(GL_TEXTURE_2D);
427 /* Avoid compiler warnings */
435 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
437 float pw = power_of_two(w);
438 float ph = power_of_two(h);
440 glColor3ub(alpha, alpha, alpha);
442 glEnable(GL_TEXTURE_2D);
443 glBindTexture(GL_TEXTURE_2D, gl_texture);
448 glTexCoord2f((float)w / pw, 0);
449 glVertex2f(screen->w, 0);
450 glTexCoord2f((float)w / pw, (float)h / ph);
451 glVertex2f(screen->w, screen->h);
452 glTexCoord2f(0, (float)h / ph);
453 glVertex2f(0, screen->h);
456 glDisable(GL_TEXTURE_2D);
458 /* Avoid compiler warnings */
466 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
468 float pw = power_of_two(int(this->w));
469 float ph = power_of_two(int(this->h));
471 glBindTexture(GL_TEXTURE_2D, gl_texture);
474 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
476 glColor4ub(alpha, alpha, alpha, alpha);
478 glEnable(GL_TEXTURE_2D);
482 glTexCoord2f(sx / pw, sy / ph);
484 glTexCoord2f((float)(sx + w) / pw, sy / ph);
486 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
487 glVertex2f(w +x, h+y);
488 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
492 glDisable(GL_TEXTURE_2D);
495 /* Avoid compiler warnings */
502 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
504 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
509 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
511 sdl_surface = sdl_surface_from_file(file, use_alpha);
516 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
518 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
524 SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
533 if(alpha != 255) /* SDL isn't capable of this kind of alpha :( therefore we'll leave now. */
536 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
537 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
539 if (update == UPDATE)
540 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
546 SurfaceSDL::draw_bg(Uint8 alpha, bool update)
556 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
558 int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
560 if (update == UPDATE)
561 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
567 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
582 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
584 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
586 if (update == UPDATE)
587 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
592 SurfaceSDL::~SurfaceSDL()
594 SDL_FreeSurface(sdl_surface);