PageRenderTime 50ms CodeModel.GetById 18ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/visualizations/Goom/goom2k4-0/src/gfontlib.c

http://github.com/xbmc/xbmc
C | 231 lines | 195 code | 30 blank | 6 comment | 37 complexity | 42362a88122ccbf50b978aec0ecf10f3 MD5 | raw file
  1#include "goom_config.h"
  2#include "gfontrle.h"
  3#include "gfontlib.h"
  4#include <string.h>
  5#include <stdlib.h>
  6
  7static Pixel  ***font_chars;
  8static int    *font_width;
  9static int    *font_height;
 10static Pixel  ***small_font_chars;
 11static int    *small_font_width;
 12static int    *small_font_height;
 13
 14void gfont_load (void) {
 15	unsigned char *gfont;
 16	unsigned int i = 0, j = 0;
 17	unsigned int nba = 0;
 18	unsigned int current = 32;
 19        int    *font_pos;
 20	/* decompress le rle */
 21
 22        
 23        
 24	gfont = malloc (the_font.width*the_font.height*the_font.bytes_per_pixel);
 25	while (i<the_font.rle_size) {
 26		unsigned char c = the_font.rle_pixel [i++];
 27		if (c == 0) {
 28			unsigned int nb = the_font.rle_pixel [i++];
 29			while (nb--)
 30				gfont[j++] = 0;
 31		}
 32		else
 33			gfont [j++] = c;
 34	}
 35
 36	/* determiner les positions de chaque lettre. */
 37
 38        font_height = calloc (256,sizeof(int));
 39        small_font_height = calloc (256,sizeof(int));
 40        font_width = calloc (256,sizeof(int));
 41        small_font_width = calloc (256,sizeof(int));
 42        font_chars = calloc (256,sizeof(int**));
 43        small_font_chars = calloc (256,sizeof(int**));
 44        font_pos = calloc (256,sizeof(int));
 45
 46	for (i=0;i<the_font.width;i++) {
 47		unsigned char a = gfont [i*4 + 3];
 48		if (a)
 49			nba ++;
 50		else
 51			nba = 0;
 52		if (nba == 2) {
 53                    font_width [current] = i - font_pos [current];
 54                    small_font_width [current] = font_width [current]/2;
 55                        font_pos [++current] = i;
 56                        font_height [current] = the_font.height - 2;
 57                        small_font_height [current] = font_height [current]/2;
 58		}
 59	}
 60	font_pos [current] = 0;
 61        font_height [current] = 0;
 62        small_font_height [current] = 0;
 63	
 64	/* charger les lettres et convertir au format de la machine */
 65	
 66	for (i=33;i<current;i++) {
 67		int x; int y;
 68                font_chars [i] = malloc (font_height[i]*sizeof(int *));
 69                small_font_chars [i] = malloc (font_height[i]*sizeof(int *)/2);
 70                for (y = 0; y < font_height[i]; y++) {
 71                    font_chars [i][y] = malloc (font_width[i]*sizeof(int));
 72                    for (x = 0; x < font_width[i]; x++) {
 73                        unsigned int r,g,b,a;
 74                        r = gfont[(y+2)*(the_font.width*4)+(x*4+font_pos[i]*4)];
 75                        g = gfont[(y+2)*(the_font.width*4)+(x*4+font_pos[i]*4+1)];
 76                        b = gfont[(y+2)*(the_font.width*4)+(x*4+font_pos[i]*4+2)];
 77                        a = gfont[(y+2)*(the_font.width*4)+(x*4+font_pos[i]*4+3)];
 78                        font_chars [i][y][x].val =
 79                            (r<<(ROUGE*8))|(g<<(VERT*8))|(b<<(BLEU*8))|(a<<(ALPHA*8));
 80                    }
 81                }
 82                for (y = 0; y < font_height[i]/2; y++) {
 83                    small_font_chars [i][y] = malloc (font_width[i]*sizeof(int)/2);
 84                    for (x = 0; x < font_width[i]/2; x++) {
 85                        unsigned int r1,g1,b1,a1,r2,g2,b2,a2,r3,g3,b3,a3,r4,g4,b4,a4;
 86                        r1 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4)];
 87                        g1 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4+1)];
 88                        b1 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4+2)];
 89                        a1 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4+3)];
 90                        r2 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4+4)];
 91                        g2 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4+5)];
 92                        b2 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4+6)];
 93                        a2 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4+7)];
 94                        r3 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4)];
 95                        g3 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4+1)];
 96                        b3 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4+2)];
 97                        a3 = gfont[(2*y+3)*(the_font.width*4)+(x*8+font_pos[i]*4+3)];
 98                        r4 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4+4)];
 99                        g4 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4+5)];
100                        b4 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4+6)];
101                        a4 = gfont[2*(y+1)*(the_font.width*4)+(x*8+font_pos[i]*4+7)];
102                        small_font_chars [i][y][x].val =
103                            (((r1 + r2 + r3 + r4)>>2)<<(ROUGE*8))|
104                            (((g1 + g2 + g3 + g4)>>2)<<(VERT*8))|
105                            (((b1 + b2 + b3 + b4)>>2)<<(BLEU*8))|
106                            (((a1 + a2 + a3 + a4)>>2)<<(ALPHA*8));
107                    }
108                }
109        }
110
111	/* definir les lettres restantes */ 
112	
113	for (i=0;i<256;i++) {
114		if (font_chars[i]==0) {
115                    font_chars[i]=font_chars[42];
116                    small_font_chars[i]=small_font_chars[42];
117                    font_width[i]=font_width[42];
118                    font_pos[i]=font_pos[42];
119                    font_height[i]=font_height[42];
120                    small_font_width[i]=small_font_width[42];
121                    small_font_height[i]=small_font_height[42];
122                }
123	}
124
125        font_width [32] = (the_font.height / 2) - 1;
126        small_font_width [32] = font_width [32]/2;
127        font_chars [32] = 0;
128        small_font_chars [32] = 0;
129        free( gfont );
130        free( font_pos );
131}
132
133void    goom_draw_text (Pixel * buf,int resolx,int resoly,
134												int x, int y,
135												const char *str, float charspace, int center) {
136	float   fx = (float) x;
137	int     fin = 0;
138
139        Pixel  ***cur_font_chars;
140        int    *cur_font_width;
141        int    *cur_font_height;
142
143        if (resolx>320)
144        {
145            /* printf("use big\n"); */
146            cur_font_chars = font_chars;
147            cur_font_width = font_width;
148            cur_font_height = font_height;
149        }
150        else
151        {
152            /* printf ("use small\n"); */
153            cur_font_chars = small_font_chars;
154            cur_font_width = small_font_width;
155            cur_font_height = small_font_height;
156        }
157
158        if (cur_font_chars == NULL)
159		return ;
160
161	if (center) {
162		unsigned char   *tmp = (unsigned char*)str;
163		float   lg = -charspace;
164
165		while (*tmp != '\0')
166			lg += cur_font_width[*(tmp++)] + charspace;
167
168		fx -= lg / 2;
169	}
170
171	while (!fin) {
172		unsigned char    c = *str;
173
174		x = (int) fx;
175
176		if (c == '\0')
177			fin = 1;
178		else if (cur_font_chars[c]==0) {
179			fx += cur_font_width[c] + charspace;
180		}
181		else {
182			int     xx, yy;
183			int     xmin = x;
184			int     xmax = x + cur_font_width[c];
185			int     ymin = y - cur_font_height[c];
186			int     ymax = y;
187
188			yy = ymin;
189
190			if (xmin < 0)
191				xmin = 0;
192
193			if (xmin >= resolx - 1)
194				return;
195
196			if (xmax >= (int) resolx)
197				xmax = resolx - 1;
198
199			if (yy < 0)
200				yy = 0;
201
202			if (yy <= (int) resoly - 1) {
203				if (ymax >= (int) resoly - 1)
204					ymax = resoly - 1;
205
206        for (; yy < ymax; yy++)
207          for (xx = xmin; xx < xmax; xx++)
208          {
209              Pixel color = cur_font_chars[c][yy - ymin][xx - x];
210              Pixel transparency;
211              transparency.val = color.val & A_CHANNEL;
212              if (transparency.val)
213              {
214                  if (transparency.val==A_CHANNEL) buf[yy * resolx + xx] = color;
215                  else
216                  {
217                      Pixel back =  buf[yy * resolx + xx];
218                      unsigned int a1 = color.channels.a;
219                      unsigned int a2 = 255 - a1;
220                      buf[yy * resolx + xx].channels.r = (unsigned char)((((unsigned int)color.channels.r * a1) + ((unsigned int)back.channels.r * a2)) >> 8);
221                      buf[yy * resolx + xx].channels.g = (unsigned char)((((unsigned int)color.channels.g * a1) + ((unsigned int)back.channels.g * a2)) >> 8);
222                      buf[yy * resolx + xx].channels.b = (unsigned char)((((unsigned int)color.channels.b * a1) + ((unsigned int)back.channels.b * a2)) >> 8);
223                  }
224              }
225          }
226			}
227			fx += cur_font_width[c] + charspace;
228		}
229		str++;
230	}
231}