annotate TaskManager/Test/test_render/texture.cpp @ 131:805d27efafd8

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