Mercurial > hg > Members > kono > Cerium
comparison TaskManager/Test/simple_render/texture.cpp @ 26:3f68b2ef4fb0
*** empty log message ***
author | gongo |
---|---|
date | Sun, 10 Feb 2008 18:31:48 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
25:6f01d41a8cfb | 26:3f68b2ef4fb0 |
---|---|
1 #include <iostream> | |
2 #include <SDL.h> | |
3 #include <SDL_opengl.h> | |
4 #include <SDL_image.h> | |
5 #include <libxml/parser.h> | |
6 using namespace std; | |
7 | |
8 int power_of_two(int input) | |
9 { | |
10 int value = 1; | |
11 | |
12 while ( value < input ) | |
13 { | |
14 value <<= 1; | |
15 } | |
16 return value; | |
17 } | |
18 | |
19 GLuint SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord) | |
20 { | |
21 GLuint texture; | |
22 int w, h; | |
23 SDL_Surface *image; | |
24 SDL_Rect area; | |
25 Uint32 saved_flags; | |
26 Uint8 saved_alpha; | |
27 | |
28 /* Use the surface width and height expanded to powers of 2 */ | |
29 w = power_of_two(surface->w); | |
30 h = power_of_two(surface->h); | |
31 texcoord[0] = 0.0f; /* Min X */ | |
32 texcoord[1] = 0.0f; /* Min Y */ | |
33 texcoord[2] = (GLfloat)surface->w / w; /* Max X */ | |
34 texcoord[3] = (GLfloat)surface->h / h; /* Max Y */ | |
35 | |
36 image = SDL_CreateRGBSurface( | |
37 SDL_SWSURFACE, | |
38 w, h, | |
39 32, | |
40 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ | |
41 0x000000FF, | |
42 0x0000FF00, | |
43 0x00FF0000, | |
44 0xFF000000 | |
45 #else | |
46 0xFF000000, | |
47 0x00FF0000, | |
48 0x0000FF00, | |
49 0x000000FF | |
50 #endif | |
51 ); | |
52 if ( image == NULL ) | |
53 { | |
54 return 0; | |
55 } | |
56 | |
57 /* Save the alpha blending attributes */ | |
58 saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); | |
59 saved_alpha = surface->format->alpha; | |
60 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) | |
61 { | |
62 SDL_SetAlpha(surface, 0, 0); | |
63 } | |
64 /* Copy the surface into the GL texture image */ | |
65 area.x = 0; | |
66 area.y = 0; | |
67 area.w = surface->w; | |
68 area.h = surface->h; | |
69 SDL_BlitSurface(surface, &area, image, &area); | |
70 | |
71 /* Restore the alpha blending attributes */ | |
72 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) | |
73 { | |
74 SDL_SetAlpha(surface, saved_flags, saved_alpha); | |
75 } | |
76 | |
77 /* Create an OpenGL texture for the image */ | |
78 glGenTextures(1, &texture); | |
79 glBindTexture(GL_TEXTURE_2D, texture); | |
80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
82 glTexImage2D(GL_TEXTURE_2D, | |
83 0, | |
84 GL_RGBA, | |
85 w, h, | |
86 0, | |
87 GL_RGBA, | |
88 GL_UNSIGNED_BYTE, | |
89 image->pixels); | |
90 SDL_FreeSurface(image); /* No longer needed */ | |
91 | |
92 return texture; | |
93 } |