diff 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
line wrap: on
line diff
--- a/Renderer/Engine/SceneGraph.cc	Thu Jul 14 00:27:19 2011 +0900
+++ b/Renderer/Engine/SceneGraph.cc	Thu Jul 21 18:56:08 2011 +0900
@@ -11,6 +11,8 @@
 #include "texture.h"
 #include "TaskManager.h"
 #include "polygon_pack.h"
+#include <ft2build.h>
+#include <freetype/freetype.h>
 
 using namespace std;
 
@@ -219,6 +221,199 @@
 
 }
 
+SceneGraph::SceneGraph(TaskManager *manager,char *font,int pixels,Uint32 color,char *string_name) {
+
+    init();
+
+    this->matrix = (float*)manager->allocate(sizeof(float)*16);
+    this->real_matrix = (float*)manager->allocate(sizeof(float)*16);
+    this->texture_info = (texture_list*)manager->allocate(sizeof(texture_list));
+    texture_info->texture_id = -1;
+    
+    //size : 頂点の数かな
+    size = 6;
+    parent_name = NULL;
+    name = string_name;
+    for (int i = 0; i < 16; i++) {
+      matrix[i]      = 0;
+      real_matrix[i] = 0;
+    }
+
+    if (size % 3 != 0) {
+      printf("vertex size is error. size %% 3 = %lld\n", size % 3);
+    }
+
+    if (size > 0) {
+      pp_num = (size/3 + MAX_SIZE_TRIANGLE - 1) / MAX_SIZE_TRIANGLE;
+      pp = (PolygonPack*)manager->allocate(sizeof(PolygonPack)*pp_num);
+    } else {
+      pp_num = 0;
+      pp = NULL;
+    }
+
+    create_font_data(manager, font, pixels, color, string_name);
+
+    finalize = &SceneGraph::finalize_original;
+}
+
+void
+SceneGraph::create_font_data(TaskManager *manager,char *font ,int pixels, Uint32 color, char *string_name)
+{
+            //font_coordinate(pixels/2,pixels);
+ 	    font_normal();
+            font_model();
+            //font_texture();
+
+            get_font_image(manager, font, pixels, color, string_name);
+}
+
+void
+SceneGraph::get_font_image(TaskManager *manager,char *font ,int pixels ,Uint32 color, char *string_name)
+{
+  int tex_id;
+  if (texture_hash.hash_regist(string_name, tex_id)) {
+    SDL_Surface *texture_image = load_font_image(font,pixels,color,string_name);
+    if(!texture_image){
+      printf("Can't load image %s\n",string_name);
+      exit(0);
+    }
+    texture_info->texture_id = makeTapestries(manager, texture_image,tex_id);
+    printf("%d\n",texture_info->texture_id);
+    tex_id = texture_info->texture_id;
+
+  } else {
+    texture_info->texture_id = tex_id;
+  }
+
+  texture_info->t_w = list[tex_id].t_w;
+  texture_info->t_h = list[tex_id].t_h;
+  texture_info->pixels_orig = list[tex_id].pixels_orig;
+  texture_info->pixels = list[tex_id].pixels;
+  texture_info->scale_max = list[tex_id].scale_max;
+  texture_info->texture_image = list[tex_id].texture_image;
+}
+
+SDL_Surface*
+SceneGraph::load_font_image(char *font ,int pixel,Uint32 color , char *string_name)
+{
+  //printf("laod_font_iamge");
+  FT_Library library;
+  FT_Error err;
+  err = FT_Init_FreeType(&library);
+  if(err){
+    exit(1);
+  }
+  FT_Face face;
+  err = FT_New_Face(library,font,0,&face);//font:フォントファイルのパス
+  if(err){
+    exit(1);
+  }
+  
+  err = FT_Set_Pixel_Sizes(face,pixel,pixel);
+  if(err){
+    exit(1);
+  }
+
+  u_int32_t changecode[256] = {0};
+
+  conv(string_name+5,strlen(string_name+5),changecode);
+
+  unsigned int characode = changecode[0];
+
+  err = FT_Load_Char(face,characode,0);
+  if(err){
+    exit(1);
+  }
+    
+  err = FT_Render_Glyph(face->glyph,FT_RENDER_MODE_MONO);
+  if(err){
+    exit(1);
+  }
+
+  FT_Bitmap *bm = &face->glyph->bitmap;
+
+  //baseline計算 y_ppem(nominal height) - bitmap_top(topからのbaseline)
+  float baseline = face->size->metrics.y_ppem - face->glyph->bitmap_top;
+  
+  float row =  1;
+  float width = 1;
+
+  font_coordinate(baseline,face->glyph->bitmap.rows,face->glyph->bitmap.width);
+  this->seq = face->glyph->bitmap.width;
+  font_texture(row,width);
+
+  int index = 0;
+  Uint32 *pixels = (Uint32*)malloc(bm->rows*bm->pitch*8*4);
+
+  for (int row = 0; row < bm->rows; row ++) {
+      for (int col = 0; col < bm->pitch; col ++) {
+          int c = bm->buffer[bm->pitch * row + col];
+          for (int bit = 7; bit >= 0; bit --) {
+
+              if (((c >> bit) & 1) == 0) {
+		//printf("  ");
+                  pixels[index++] = 0x0000000;
+              } else {
+		//printf("##");
+                  //pixels[index++] = 0x00ffffff;
+		  pixels[index++] = color;
+
+              }
+
+          }
+      }
+
+  }
+
+  SDL_Surface *texture_image = SDL_CreateRGBSurfaceFrom(pixels, bm->pitch*8, bm->rows, 
+                                                        32, bm->pitch*8*4, redMask,
+                                                        greenMask, blueMask, alphaMask);
+
+
+
+  if (!texture_image) {
+    printf("error\n");
+    return 0;
+  }
+  SDL_Surface *tmpImage
+    = SDL_CreateRGBSurface(SDL_HWSURFACE, texture_image->w,
+			   texture_image->h, 32, redMask,
+			   greenMask, blueMask, alphaMask);
+
+
+  SDL_Surface *converted;
+  converted = SDL_ConvertSurface(texture_image, tmpImage->format,
+				 SDL_HWSURFACE);
+  if (converted != NULL) {
+    SDL_FreeSurface(texture_image);
+    texture_image = converted;
+  }
+
+  return texture_image;
+}
+
+void
+SceneGraph::conv(char *str, int length, u_int32_t *out) {
+  int oindex = 0;
+  int i = 0;
+  while (i < length) {
+    out[oindex] = str[i++] & 0xff;
+    int len = 0;
+    u_int32_t mask;
+    for (mask = 0x80; out[oindex] & mask; mask >>= 1) {
+      out[oindex] -= mask;
+      len++;
+    }
+    int j;
+    for (j = 1; j < len; j++)
+      out[oindex] = (out[oindex] << 6) | (str[i++] & 0x3f);
+    oindex++;
+  }
+}
+
+/*文字のSceneGraphを生成する*/
+
+
 void
 SceneGraph::init()
 {