comparison Renderer/Engine/SceneGraph.cc @ 1205:b8adf4e95e96 draft

add createStringFont()
author Takao YONAMINE <e095763@ie.u-ryukyu.ac.jp>
date Thu, 21 Jul 2011 18:56:08 +0900
parents 5abf0ce8c71c
children 0ee23180d301
comparison
equal deleted inserted replaced
1197:726098b89619 1205:b8adf4e95e96
9 #include "matrix_calc.h" 9 #include "matrix_calc.h"
10 #include "TextureHash.h" 10 #include "TextureHash.h"
11 #include "texture.h" 11 #include "texture.h"
12 #include "TaskManager.h" 12 #include "TaskManager.h"
13 #include "polygon_pack.h" 13 #include "polygon_pack.h"
14 #include <ft2build.h>
15 #include <freetype/freetype.h>
14 16
15 using namespace std; 17 using namespace std;
16 18
17 SceneGraphPtr scene_graph = NULL; 19 SceneGraphPtr scene_graph = NULL;
18 SceneGraphPtr scene_graph_viewer = NULL; 20 SceneGraphPtr scene_graph_viewer = NULL;
216 get_data(manager, surface->children); 218 get_data(manager, surface->children);
217 219
218 finalize = &SceneGraph::finalize_original; 220 finalize = &SceneGraph::finalize_original;
219 221
220 } 222 }
223
224 SceneGraph::SceneGraph(TaskManager *manager,char *font,int pixels,Uint32 color,char *string_name) {
225
226 init();
227
228 this->matrix = (float*)manager->allocate(sizeof(float)*16);
229 this->real_matrix = (float*)manager->allocate(sizeof(float)*16);
230 this->texture_info = (texture_list*)manager->allocate(sizeof(texture_list));
231 texture_info->texture_id = -1;
232
233 //size : 頂点の数かな
234 size = 6;
235 parent_name = NULL;
236 name = string_name;
237 for (int i = 0; i < 16; i++) {
238 matrix[i] = 0;
239 real_matrix[i] = 0;
240 }
241
242 if (size % 3 != 0) {
243 printf("vertex size is error. size %% 3 = %lld\n", size % 3);
244 }
245
246 if (size > 0) {
247 pp_num = (size/3 + MAX_SIZE_TRIANGLE - 1) / MAX_SIZE_TRIANGLE;
248 pp = (PolygonPack*)manager->allocate(sizeof(PolygonPack)*pp_num);
249 } else {
250 pp_num = 0;
251 pp = NULL;
252 }
253
254 create_font_data(manager, font, pixels, color, string_name);
255
256 finalize = &SceneGraph::finalize_original;
257 }
258
259 void
260 SceneGraph::create_font_data(TaskManager *manager,char *font ,int pixels, Uint32 color, char *string_name)
261 {
262 //font_coordinate(pixels/2,pixels);
263 font_normal();
264 font_model();
265 //font_texture();
266
267 get_font_image(manager, font, pixels, color, string_name);
268 }
269
270 void
271 SceneGraph::get_font_image(TaskManager *manager,char *font ,int pixels ,Uint32 color, char *string_name)
272 {
273 int tex_id;
274 if (texture_hash.hash_regist(string_name, tex_id)) {
275 SDL_Surface *texture_image = load_font_image(font,pixels,color,string_name);
276 if(!texture_image){
277 printf("Can't load image %s\n",string_name);
278 exit(0);
279 }
280 texture_info->texture_id = makeTapestries(manager, texture_image,tex_id);
281 printf("%d\n",texture_info->texture_id);
282 tex_id = texture_info->texture_id;
283
284 } else {
285 texture_info->texture_id = tex_id;
286 }
287
288 texture_info->t_w = list[tex_id].t_w;
289 texture_info->t_h = list[tex_id].t_h;
290 texture_info->pixels_orig = list[tex_id].pixels_orig;
291 texture_info->pixels = list[tex_id].pixels;
292 texture_info->scale_max = list[tex_id].scale_max;
293 texture_info->texture_image = list[tex_id].texture_image;
294 }
295
296 SDL_Surface*
297 SceneGraph::load_font_image(char *font ,int pixel,Uint32 color , char *string_name)
298 {
299 //printf("laod_font_iamge");
300 FT_Library library;
301 FT_Error err;
302 err = FT_Init_FreeType(&library);
303 if(err){
304 exit(1);
305 }
306 FT_Face face;
307 err = FT_New_Face(library,font,0,&face);//font:フォントファイルのパス
308 if(err){
309 exit(1);
310 }
311
312 err = FT_Set_Pixel_Sizes(face,pixel,pixel);
313 if(err){
314 exit(1);
315 }
316
317 u_int32_t changecode[256] = {0};
318
319 conv(string_name+5,strlen(string_name+5),changecode);
320
321 unsigned int characode = changecode[0];
322
323 err = FT_Load_Char(face,characode,0);
324 if(err){
325 exit(1);
326 }
327
328 err = FT_Render_Glyph(face->glyph,FT_RENDER_MODE_MONO);
329 if(err){
330 exit(1);
331 }
332
333 FT_Bitmap *bm = &face->glyph->bitmap;
334
335 //baseline計算 y_ppem(nominal height) - bitmap_top(topからのbaseline)
336 float baseline = face->size->metrics.y_ppem - face->glyph->bitmap_top;
337
338 float row = 1;
339 float width = 1;
340
341 font_coordinate(baseline,face->glyph->bitmap.rows,face->glyph->bitmap.width);
342 this->seq = face->glyph->bitmap.width;
343 font_texture(row,width);
344
345 int index = 0;
346 Uint32 *pixels = (Uint32*)malloc(bm->rows*bm->pitch*8*4);
347
348 for (int row = 0; row < bm->rows; row ++) {
349 for (int col = 0; col < bm->pitch; col ++) {
350 int c = bm->buffer[bm->pitch * row + col];
351 for (int bit = 7; bit >= 0; bit --) {
352
353 if (((c >> bit) & 1) == 0) {
354 //printf(" ");
355 pixels[index++] = 0x0000000;
356 } else {
357 //printf("##");
358 //pixels[index++] = 0x00ffffff;
359 pixels[index++] = color;
360
361 }
362
363 }
364 }
365
366 }
367
368 SDL_Surface *texture_image = SDL_CreateRGBSurfaceFrom(pixels, bm->pitch*8, bm->rows,
369 32, bm->pitch*8*4, redMask,
370 greenMask, blueMask, alphaMask);
371
372
373
374 if (!texture_image) {
375 printf("error\n");
376 return 0;
377 }
378 SDL_Surface *tmpImage
379 = SDL_CreateRGBSurface(SDL_HWSURFACE, texture_image->w,
380 texture_image->h, 32, redMask,
381 greenMask, blueMask, alphaMask);
382
383
384 SDL_Surface *converted;
385 converted = SDL_ConvertSurface(texture_image, tmpImage->format,
386 SDL_HWSURFACE);
387 if (converted != NULL) {
388 SDL_FreeSurface(texture_image);
389 texture_image = converted;
390 }
391
392 return texture_image;
393 }
394
395 void
396 SceneGraph::conv(char *str, int length, u_int32_t *out) {
397 int oindex = 0;
398 int i = 0;
399 while (i < length) {
400 out[oindex] = str[i++] & 0xff;
401 int len = 0;
402 u_int32_t mask;
403 for (mask = 0x80; out[oindex] & mask; mask >>= 1) {
404 out[oindex] -= mask;
405 len++;
406 }
407 int j;
408 for (j = 1; j < len; j++)
409 out[oindex] = (out[oindex] << 6) | (str[i++] & 0x3f);
410 oindex++;
411 }
412 }
413
414 /*文字のSceneGraphを生成する*/
415
221 416
222 void 417 void
223 SceneGraph::init() 418 SceneGraph::init()
224 { 419 {
225 next = NULL; 420 next = NULL;