Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/src/font.cpp
00001 /*
00002  * font.cpp - Kukatz 3D
00003  * Copyright (c) 2011 - TÖRÖK Attila (torokati44@gmail.com)
00004  *
00005  * This software is provided 'as-is', without any express or implied
00006  * warranty. In no event will the authors be held liable for any damages
00007  * arising from the use of this software.
00008  *
00009  * Permission is granted to anyone to use this software for any purpose,
00010  * including commercial applications, and to alter it and redistribute it
00011  * freely, subject to the following restrictions:
00012  *
00013  *       1. The origin of this software must not be misrepresented; you must not
00014  *                      claim that you wrote the original software. If you use this software
00015  *                      in a product, an acknowledgment in the product documentation would be
00016  *                      appreciated but is not required.
00017  *
00018  *       2. Altered source versions must be plainly marked as such, and must not be
00019  *                      misrepresented as being the original software.
00020  *
00021  *       3. This notice may not be removed or altered from any source
00022  *                      distribution.
00023  */
00024 
00025 #include "font.hpp"
00026 
00027 #include <cstring>
00028 #include <iostream>
00029 
00030 #include "resourcemanager.hpp"
00031 
00032 Font::Font(std::string identifier, std::string texture,
00033                 const unsigned char* data, sf::Uint32 data_size):
00034         Resource(identifier),
00035         texture_ptr(ResourceManager::instance().textures[texture]),
00036         texture_id(texture)
00037 {
00038         unsigned char* current = (unsigned char*)data;
00039         unsigned char* end = current;
00040         end += data_size; // AFTER the last byte
00041         
00042         assert(data_size >= 9);
00043         assert(!strncmp((char*)current, "BMF\003", 4));
00044         current += 4;
00045         
00046         while (current < end)
00047         {
00048                 sf::Uint8 block_type = *((sf::Uint8*)current);
00049                 current += 1;
00050                 sf::Uint32 block_size = *((sf::Uint32*)current);
00051                 current += 4;
00052                 unsigned char* block_end = current;
00053                 block_end += block_size;
00054                 
00055                 switch (block_type)
00056                 {
00057                         case 1: // info
00058                         {
00059                                 size = *((sf::Uint16*)current); // font size
00060                                 current += block_size;
00061                         }
00062                         break;
00063                         case 2: // common
00064                         {
00065                                 line_height = *((sf::Uint16*)current);
00066                                 current += 2;
00067                                 
00068                                 base = *((sf::Uint16*)current);
00069                                 current += 2;
00070                                 
00071                                 current += 11; // channels, size, etc... we don't need it
00072                         }
00073                         break;
00074                         case 3: // pages
00075                         {
00076                                 current += block_size; // we don't need anything from this block...
00077                         }
00078                         break;
00079                         case 4: // chars
00080                         {
00081                                 while (current < block_end)
00082                                 {
00083                                         sf::Uint32 id;
00084                                         
00085                                         id = *((sf::Uint32*)current);
00086                                         current += 4;
00087                                         
00088                                         glyph_t glyph;
00089                                         
00090                                         glyph.x = *((sf::Uint16*)current);
00091                                         current += 2;
00092                                         
00093                                         glyph.y = *((sf::Uint16*)current);
00094                                         current += 2;
00095                                         
00096                                         glyph.w = *((sf::Uint16*)current);
00097                                         current += 2;
00098                                         
00099                                         glyph.h = *((sf::Uint16*)current);
00100                                         current += 2;
00101                                         
00102                                         glyph.xo = *((sf::Int16*)current);
00103                                         current += 2;
00104                                         
00105                                         glyph.yo = *((sf::Int16*)current);
00106                                         current += 2;
00107                                         
00108                                         glyph.xa = *((sf::Int16*)current);
00109                                         current += 2;
00110                                         
00111                                         current += 2; // page, channel, not interesting...
00112                                         
00113                                         glyphs.insert(std::make_pair< sf::Uint32, glyph_t >(id, glyph));
00114                                 }
00115                         }
00116                         break;
00117                         case 5: // kerning pairs
00118                         {
00119                                 while (current < block_end)
00120                                 {
00121                                         sf::Uint32 first = *((sf::Uint32*)current);
00122                                         current += 4;
00123                                         
00124                                         sf::Uint32 second = *((sf::Uint32*)current);
00125                                         current += 4;
00126                                         
00127                                         sf::Int16 amount = *((sf::Int16*)current);
00128                                         current += 2;
00129                                         
00130                                         glyphs[first].kernings.push_back(std::make_pair< sf::Uint32, sf::Int16 >(second, amount));
00131                                 }
00132                         }
00133                         break;
00134                         default:
00135                         {
00136                                 std::cerr << "Unexpected block type (" << (sf::Uint16)block_type << ") when loading font \"" << identifier << "\"\n";
00137                         }
00138                         break;
00139                 }
00140                 
00141                 if (current != block_end)
00142                 {
00143                         std::cerr << "Block size mismatch during loading font: \"" << identifier << "\".";
00144                 }
00145         }
00146         
00147         if (current != end)
00148         {
00149                 std::cerr << "Data size mismatch during loading font: \"" << identifier << "\".";
00150         }
00151 }
00152 
00153 Font::~Font()
00154 {
00155         
00156 }
 Összes Osztályok