コード例 #1
0
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
bool CameraRendererRGB565GL2::CreateTextures()
{
   glGenTextures( 1, &(mTextureId[0]) );
   if(CameraUtil::checkGlError("glGenTextures-Y")) return false;      
   glBindTexture( GL_TEXTURE_2D, mTextureId[0] );
   if(CameraUtil::checkGlError("glBindTexture-Y")) return false;

   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR    );
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR    );
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,     GL_CLAMP_TO_EDGE );
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,     GL_CLAMP_TO_EDGE );


   // Use landscape mode per default
   for(size_t i=0; i<20; i++)
      mVertices[i] = mLandscapeVertices[i];
   
   if(!mNPOTTextures)
   {
      int texWidth = getNextPowerOfTwo(mWidth);
      int texHeight = getNextPowerOfTwo(mHeight);

      float texCoordWidth = mWidth / (float)texWidth;
      float texCoordHeight = mHeight / (float)texHeight;

      // Set the texture coords to the correct values
      // (t,r) are represented as their positions in the
      // combined vertex/texture coord array

      // (3,4) = (0,0) is already set
      // (8,9) = (0,1) -> (0, actual height)
      mVertices[9] = texCoordHeight;
      // (13,14) = (1,0) -> (actual width, 0)
      mVertices[13] = texCoordWidth;
      // (18,19) = (1,1) -> (actual width, actual height)
      mVertices[18] = texCoordWidth;
      mVertices[19] = texCoordHeight;
      
      glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 
                 texWidth, texHeight, 
                 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, NULL ); 
      if(CameraUtil::checkGlError("glTexImage2D-RGB")) 
      {
          EPRINTF("mWidth %d, mHeight %d", mWidth, mHeight);
          return false;
      }
   }

   return true;
}
コード例 #2
0
ファイル: memory_file.cpp プロジェクト: vancegroup/Audiere
  MemoryFile::MemoryFile(const void* buffer, int size) {
    m_capacity = getNextPowerOfTwo(size);
    m_size = size;
    m_buffer = new u8[m_capacity];
    memcpy(m_buffer, buffer, size);

    m_position = 0;
  }
コード例 #3
0
ファイル: TemplateBuilder.cpp プロジェクト: scrossuk/locic
		size_t TemplateBuilder::bitsRequired() const {
			if (templateUseMap_.empty()) {
				// When there are no uses, the path
				// terminates and no bits are required.
				return 0;
			}
			
			if (templateUseMap_.size() == 1) {
				// If there is one use, then we still
				// need a bit to determine whether or
				// not to take that path.
				return 1;
			}
			
			return getNextPowerOfTwo(templateUseMap_.size());
		}
コード例 #4
0
ファイル: MFreetypeLoader.cpp プロジェクト: UPO33/Maratis-4
bool M_loadFont(const char * filename, void * data, void * arg)
{
	MFont * font = (MFont *)data;
	
	int pen_x, pen_y, max_y;
	unsigned int n, max_code = 4096;
	unsigned int size = font->getFontSize();
	unsigned int space = 2;
	unsigned int width = 1024;
	unsigned int height = 0;

	MImage image;

	FT_GlyphSlot slot;
	FT_Library library;
	FT_Face face;
	FT_Byte * file_base;
	FT_Long file_size;


	// init
	FT_Error error = FT_Init_FreeType(&library); 
	if(error){
		printf("ERROR Load Font : unable to init FreeType\n");
		return false;
	}
	
	
	// open file
	MFile * file = M_fopen(filename, "rb");
	if(! file)
	{
		FT_Done_FreeType(library);
		printf("ERROR Load Font : can't read file %s\n", filename);
		return false;
	}
	
	M_fseek(file, 0, SEEK_END);
	file_size = M_ftell(file);
	M_rewind(file);
	
	file_base = new FT_Byte[file_size];
	if(file_size != M_fread(file_base, sizeof(FT_Byte), file_size, file))
	{
		M_fclose(file);
		FT_Done_FreeType(library);
		delete [] file_base;
		return false;
	}
	
	
	// read font
	error = FT_New_Memory_Face(library, file_base, file_size, 0, &face);
	M_fclose(file);
	
	if(error)
	{
		printf("ERROR Load Font : unable to read data %s\n", filename);
		FT_Done_FreeType(library);
		delete [] file_base;
		return false;
	}

	// set font size
	error = FT_Set_Pixel_Sizes(face, 0, size);
	if(error)
	{
		printf("ERROR Load Font : unable to size font\n");
		FT_Done_FreeType(library);
		delete [] file_base;
		return false;
	}


	// parse characters
	max_y = 0;
	slot = face->glyph;
	pen_x = space; pen_y = space; 
	for(n = 0; n<max_code; n++)
	{
		// load glyph image into the slot (erase previous one)
		error = FT_Load_Char(face, n, FT_LOAD_RENDER | FT_LOAD_NO_HINTING); 

		if(error)
			continue;

		if(FT_Get_Char_Index(face, n) == 0)
			continue;

		// max y
		max_y = MAX(max_y, slot->bitmap.rows);

		if((pen_x + slot->bitmap.width + space) > width)
		{
			pen_x = space;
			pen_y += max_y + space;
			height += max_y + space;
			max_y = 0;
		}

		// increment pen position 
		pen_x += slot->bitmap.width + space; 
	}

	if(height == 0)
	{
		printf("ERROR Load Font : unable to create font texture\n");
		FT_Done_FreeType(library);
		delete [] file_base;
		return false;
	}


	// create image
	height = getNextPowerOfTwo(height);
	image.create(M_UBYTE, width, height, 4);
	memset(image.getData(), 0, image.getSize()*sizeof(char));


	// init font
	font->setTextureWidth(width);
	font->setTextureHeight(height);


	// create font texture
	max_y = 0;
	slot = face->glyph;
	pen_x = space; pen_y = space; 
	for(n = 0; n<max_code; n++)
	{
		// load glyph image into the slot (erase previous one)
		error = FT_Load_Char(face, n, FT_LOAD_RENDER | FT_LOAD_NO_HINTING);

		if(error) 
			continue;

		if(FT_Get_Char_Index(face, n) == 0)
			continue;

		// max y
		max_y = MAX(max_y, slot->bitmap.rows);
		
		if((pen_x + slot->bitmap.width + space) > (int)image.getWidth()){
			pen_x = space;
			pen_y += max_y + space;
		}

		// get character properties
		float xAdvance = (slot->advance.x >> 6) / ((float)size);
		MVector2 offset = MVector2((float)slot->bitmap_left - 1, - (float)slot->bitmap_top - 1) / ((float)size);
		MVector2 pos = MVector2((float)(pen_x-1) / (float)width, (float)(pen_y-1) / (float)height);
		MVector2 scale = MVector2((float)(slot->bitmap.width+2) / (float)width, (float)(slot->bitmap.rows+2) / (float)height);

		// set character
		font->setCharacter(n, MCharacter(xAdvance, offset, pos, scale));

		// draw to image
		drawBitmap(&image, &slot->bitmap, pen_x, pen_y);

		// increment pen position 
		pen_x += slot->bitmap.width + space; 
	} 


	// send texture
	MEngine * engine = MEngine().getInstance();
	MRenderingContext * render = engine->getRenderingContext();
	
	// gen texture id
	unsigned int textureId = font->getTextureId();
	if(textureId == 0)
	{
		render->createTexture(&textureId);
		font->setTextureId(textureId);
	}
	
	// send texture image
	render->bindTexture(textureId);
	render->setTextureUWrapMode(M_WRAP_REPEAT);
	render->setTextureVWrapMode(M_WRAP_REPEAT);
	render->sendTextureImage(&image, 0, 1, 0);

	// finish
	FT_Done_FreeType(library);
	delete [] file_base;
	return true;
}