comparison tree_controll.c @ 0:435ac1cdb64e

create task dandy directry.
author koba <koba@cr.ie.u-ryukyu.ac.jp>
date Sat, 11 Dec 2010 21:25:28 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:435ac1cdb64e
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <math.h>
5 #include <SDL.h>
6 #include <SDL_opengl.h>
7 #include <SDL_image.h>
8 #include "object.h"
9 #include "tree_controll.h"
10 #include "LoadSprite.h"
11 #include "xml.h"
12
13
14 static int power_of_two(int input)
15 {
16 int value = 1;
17
18 while ( value < input )
19 {
20 value <<= 1;
21 }
22 return value;
23 }
24
25
26 GLuint SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord)
27 {
28 GLuint texture;
29 int w, h;
30 SDL_Surface *image;
31 SDL_Rect area;
32 Uint32 saved_flags;
33 Uint8 saved_alpha;
34
35 /* Use the surface width and height expanded to powers of 2 */
36 w = power_of_two(surface->w);
37 h = power_of_two(surface->h);
38 texcoord[0] = 0.0f; /* Min X */
39 texcoord[1] = 0.0f; /* Min Y */
40 texcoord[2] = (GLfloat)surface->w / w; /* Max X */
41 texcoord[3] = (GLfloat)surface->h / h; /* Max Y */
42
43 image = SDL_CreateRGBSurface(
44 SDL_SWSURFACE,
45 w, h,
46 32,
47 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
48 0x000000FF,
49 0x0000FF00,
50 0x00FF0000,
51 0xFF000000
52 #else
53 0xFF000000,
54 0x00FF0000,
55 0x0000FF00,
56 0x000000FF
57 #endif
58 );
59 if ( image == NULL )
60 {
61 return 0;
62 }
63
64 /* Save the alpha blending attributes */
65 saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
66 saved_alpha = surface->format->alpha;
67 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
68 {
69 SDL_SetAlpha(surface, 0, 0);
70 }
71
72 /* Copy the surface into the GL texture image */
73 area.x = 0;
74 area.y = 0;
75 area.w = surface->w;
76 area.h = surface->h;
77 SDL_BlitSurface(surface, &area, image, &area);
78
79 /* Restore the alpha blending attributes */
80 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
81 {
82 SDL_SetAlpha(surface, saved_flags, saved_alpha);
83 }
84
85 /* Create an OpenGL texture for the image */
86 glGenTextures(1, &texture);
87 glBindTexture(GL_TEXTURE_2D, texture);
88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
90 glTexImage2D(GL_TEXTURE_2D,
91 0,
92 GL_RGBA,
93 w, h,
94 0,
95 GL_RGBA,
96 GL_UNSIGNED_BYTE,
97 image->pixels);
98 SDL_FreeSurface(image); /* No longer needed */
99
100 return texture;
101 }
102
103
104 float tes_angle[3] = {0,0,0};
105
106
107 SURFACE *next_draw_node(SURFACE *surfaces)
108 {
109 SURFACE *node = surfaces;
110 if(node->child != NULL)
111 {
112 //glPushMatrix();
113 return node->child;
114 }
115 else if(node->brother != NULL)
116 {
117 //glPopMatrix();
118 //glPushMatrix();
119 return node->brother;
120 }
121 else
122 {
123 node = node->parent;
124 if(node != NULL)
125 {
126 //glPopMatrix();
127 }
128 while(node != NULL)
129 {
130 if(node->brother != NULL)
131 {
132 //glPopMatrix();
133 //glPushMatrix();
134 return node->brother;
135 }
136 else
137 {
138 node = node->parent;
139 /*
140 if(node != NULL)
141 {
142 //glPopMatrix();
143 }
144 */
145 }
146 }
147 return NULL;
148 }
149 }
150
151 SURFACE *search_node(OBJECT *top, const char *name)
152 {
153 SURFACE *node;
154 SURFACE *ret_node = NULL;
155 for(node = top->surfaces;node != NULL;node = next_node(node))
156 {
157 if(strcmp(node->name, name) == 0)
158 {
159 ret_node = node;
160 return ret_node;
161 }
162 }
163 printf("can't not find node\n");
164 return ret_node;
165 }
166
167 SURFACE *next_node(SURFACE *surfaces)
168 {
169 SURFACE *node = surfaces;
170 if(node->child != NULL)
171 {
172 return node->child;
173 }
174 else if(node->brother != NULL)
175 {
176 return node->brother;
177 }
178 else
179 {
180 node = node->parent;
181 while(node != NULL)
182 {
183 if(node->brother != NULL)
184 {
185 return node->brother;
186 }
187 else
188 {
189 node = node->parent;
190 }
191 }
192 return NULL;
193 }
194 }
195
196
197
198 void node_prameter_change(OBJECT *top, char *name, float x, float y, float z, float ax, float ay, float az)
199 {
200 SURFACE *node;
201 for(node=top->surfaces; node!=NULL; node=next_node(node))
202 {
203 if(!strcmp(node->name, name))
204 {
205 node->xyz[0] = x;
206 node->xyz[1] = y;
207 node->xyz[2] = z;
208 node->angle[0] = ax;
209 node->angle[1] = ay;
210 node->angle[2] = az;
211 }
212 }
213 }
214
215 void all_object_load_texture(OBJECT *top)
216 {
217 SURFACE *node;
218 SDL_Surface *image;
219 GLfloat texcoord[4];
220 for(node=top->surfaces; node!=NULL; node=next_node(node))
221 {
222 //image = SDL_LoadBMP(node->image_name);
223 //image = IMG_Load(node->image_name);
224 image = LoadSprite(node);
225 node->texture = (int *)SDL_GL_LoadTexture(image, texcoord);
226 SDL_FreeSurface(image);
227 }
228 }
229