00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "mxf.h"
00016 #include<stdio.h>
00017 #include<stdlib.h>
00018
00019 int SDL_GetFX(struct SDL_Font *m,int x, int nw) {
00020 float xp = (float)x * (float)m->mx / (float)nw;
00021 return (int)xp;
00022 }
00023
00024 int SDL_GetFZ(struct SDL_Font *m, int y, int nh) {
00025 float yp = (float)y * (float)m->my / (float)nh;
00026 return (int)yp;
00027 }
00028
00029
00030 void InitFont(struct SDL_Font *m, int width, int height, int color) {
00031 int l,p,i,z;
00032 m->mx = width;
00033 m->my = height;
00034 for(l = 0; l <= 127; l++) {
00035 m->letters[l].fnt_ptr = (int**) malloc ( sizeof(int*) * width);
00036 for(p = 0; p < width; p++) {
00037 m->letters[l].fnt_ptr[p] = (int*) malloc ( sizeof(int) * height);
00038 }
00039 for(i = 0; i < width; i++) {
00040 for(z = 0; z < height; z++) {
00041 m->letters[l].fnt_ptr[i][z] = color;
00042 }
00043 }
00044 }
00045 m->tcolor = 0x0;
00046 }
00047
00048 void dump_font(struct SDL_Font *) {
00049
00050
00051
00052
00053
00054
00055 }
00056
00057
00058 int littleToBig(int i)
00059 {
00060 return((i&0xff)<<24)+((i&0xff00)<<8)+((i&0xff0000)>>8)+((i>>24)&0xff);
00061 }
00062
00063
00064
00065
00066 struct SDL_Font *SDL_InitFont(const char *src) {
00067 FILE *fptr = fopen(src, "rb");
00068 int q = 0,mx = 0,my = 0;
00069 struct SDL_Font *fnt;
00070 int i,z,p;
00071 if(!fptr) return 0;
00072 int mode = 0;
00073
00074
00075 fnt = (struct SDL_Font*)malloc(sizeof(struct SDL_Font));
00076
00077 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
00078 {
00079 mode = 1;
00080 }
00081
00082
00083 fread((char*)&q, sizeof(int), 1, fptr);
00084
00085 if(mode == 1)
00086 q = littleToBig(q);
00087
00088
00089 if(q == -100) {
00090 fread((char*)&mx, sizeof(int), 1, fptr);
00091 fread((char*)&my, sizeof(int), 1, fptr);
00092 fread((char*)&fnt->tcolor, sizeof(int), 1, fptr);
00093
00094
00095 if(mode == 1) {
00096
00097 mx = littleToBig(mx);
00098 my = littleToBig(my);
00099 fnt->tcolor = littleToBig(fnt->tcolor);
00100
00101 }
00102
00103 if(mx < 0 || mx < 0 || mx > 250 || my > 250) {
00104
00105
00106 free(fnt);
00107 return 0;
00108 }
00109 InitFont(fnt, mx, my, fnt->tcolor);
00110
00111 for(p = 0; p <= 127; p++)
00112 for(i = 0; i < fnt->mx; i++) {
00113 for(z = 0; z < fnt->my; z++) {
00114
00115 fread((char*)&fnt->letters[p].fnt_ptr[i][z], sizeof(int), 1, fptr);
00116
00117 if(mode == 1) fnt->letters[p].fnt_ptr[i][z] = littleToBig(fnt->letters[p].fnt_ptr[i][z]);
00118
00119 }
00120 }
00121 }
00122 fclose(fptr);
00123 return fnt;
00124 }
00125
00126
00127
00128 void SDL_FreeFont(struct SDL_Font *m) {
00129 int l,i;
00130 for(l = 0; l <= 127; l++) {
00131 if(m->letters[l].fnt_ptr) {
00132 for(i = 0; i < m->mx; i++)
00133 free(m->letters[l].fnt_ptr[i]);
00134
00135 free(m->letters[l].fnt_ptr);
00136 }
00137
00138 }
00139
00140 free(m);
00141 }
00142
00143 int SDL_PrintText(struct SDL_Surface *surf, struct SDL_Font *fnt, int x, int y, Uint32 color, const char *src) {
00144 int prev_x = x;
00145 int offset_x = prev_x, offset_y = y;
00146 int width = 0, height = 0;
00147 int i,z,p;
00148
00149 void *pbuf = lock(surf, surf->format->BitsPerPixel);
00150
00151
00152 for(p = 0; p < (int)strlen(src); ++p) {
00153 if(src[p] == '\n') {
00154 offset_x = prev_x;
00155 offset_y += height;
00156 continue;
00157 }
00158 for(i = 0; i < fnt->mx; ++i) {
00159 for(z = 0; z < fnt->my; ++z) {
00160 if(fnt->letters[(int)src[p]].fnt_ptr[i][z] != fnt->tcolor) {
00161 if(offset_y+z > 0 && offset_x+i > 0 && offset_y+z < surf->h && offset_x+i < surf->w)
00162 setpixel(pbuf, (Uint32)offset_x+i, (Uint32)offset_y+z, color, surf->format->BitsPerPixel, surf->pitch);
00163 width=i;
00164 if(height < z)
00165 height=z;
00166 }
00167 }
00168 }
00169 offset_x += width + 3;
00170 if(offset_x+width >= surf->w) {
00171 offset_x = prev_x;
00172 offset_y += height;
00173 }
00174 if(offset_y+height > surf->h)
00175 return 1;
00176
00177 }
00178 unlock(surf);
00179
00180 return 0;
00181 }
00182
00183 void SDL_PrintTextScaled(struct SDL_Surface *surf, struct SDL_Font *fnt, int x, int y, int w, int h, Uint32 color, const char *src) {
00184
00185 int prev_x = x;
00186 int offset_x = prev_x, offset_y = y;
00187 int width = 0, height = 0;
00188 int i,z,p;
00189
00190 void *pbuf = lock(surf, surf->format->BitsPerPixel);
00191
00192
00193 for(p = 0; p < (int)strlen(src); p++) {
00194 if(src[p] == '\n') {
00195 offset_x = prev_x;
00196 offset_y += height;
00197 continue;
00198 }
00199 for(i = 0; i < w; i++) {
00200 for(z = 0; z < h; z++) {
00201 if(fnt->letters[(int)src[p]].fnt_ptr[SDL_GetFX(fnt,i,w)][SDL_GetFZ(fnt,z,h)] != fnt->tcolor) {
00202 setpixel(pbuf, (Uint32)offset_x+i, (Uint32)offset_y+z, color, surf->format->BitsPerPixel, surf->pitch);
00203 width=i;
00204 if(height < z)
00205 height=z;
00206 }
00207 }
00208 }
00209 offset_x += width + 2;
00210 }
00211 unlock(surf);
00212
00213 }
00214
00215
00216
00217
00218 int SDL_PrintTextWidth(int *depth, struct SDL_Surface *surf, struct SDL_Font *fnt, int x, int y, int size_width, Uint32 color, const char *src) {
00219 int prev_x = x;
00220 int offset_x = prev_x, offset_y = y;
00221 int width = 0, height = 0;
00222 int i,z,p;
00223
00224 void *pbuf = lock(surf, surf->format->BitsPerPixel);
00225
00226
00227 for(p = 0; p < (int)strlen(src); p++) {
00228 if(src[p] == '\n') {
00229 offset_x = prev_x;
00230 offset_y += height;
00231 continue;
00232 }
00233 for(i = 0; i < fnt->mx; i++) {
00234 for(z = 0; z < fnt->my; z++) {
00235 if(fnt->letters[(int)src[p]].fnt_ptr[i][z] != fnt->tcolor) {
00236 if(offset_y+z > 0 && offset_x+i > 0 && offset_y+z < surf->h && offset_x+i < size_width)
00237 setpixel(pbuf, (Uint32)offset_x+i, (Uint32)offset_y+z, color, surf->format->BitsPerPixel, surf->pitch);
00238 width=i;
00239 if(height < z)
00240 height=z;
00241 }
00242 }
00243 }
00244 offset_x += width + 3;
00245 if(offset_x+width >= size_width) {
00246 offset_x = prev_x;
00247 offset_y += height;
00248 *depth = offset_x-x;
00249 return 1;
00250 }
00251
00252 if(offset_x > size_width) return 1;
00253
00254 if(offset_y+height > surf->h)
00255 return 2;
00256
00257 }
00258 unlock(surf);
00259 *depth = offset_x-x;
00260 return 0;
00261 }
00262
00263 int SDL_PrintTextIdle(struct SDL_Surface *surf, struct SDL_Font *fnt, int x, int y, int size_width,Uint32 color, const char *src) {
00264
00265 int prev_x = x;
00266 int offset_x = prev_x, offset_y = y;
00267 int width = 0, height = 0;
00268 int i,z,p;
00269
00270 void *pbuf = lock(surf, surf->format->BitsPerPixel);
00271
00272
00273 for(p = 0; p < (int)strlen(src); p++) {
00274 if(src[p] == '\n') {
00275 offset_x = prev_x;
00276 offset_y += height;
00277 continue;
00278 }
00279 for(i = 0; i < fnt->mx; i++) {
00280 for(z = 0; z < fnt->my; z++) {
00281
00282
00283
00284 if(fnt->letters[(int)src[p]].fnt_ptr[i][z] != fnt->tcolor) {
00285 if(offset_y+z > 0 && offset_x+i > 0 && offset_y+z < surf->h && offset_x+i < size_width)
00286 setpixel(pbuf, (Uint32)offset_x+i, (Uint32)offset_y+z, color, surf->format->BitsPerPixel, surf->pitch);
00287 width=i;
00288 if(height < z)
00289 height=z;
00290 }
00291 }
00292 }
00293 offset_x += width + 3;
00294
00295 if(offset_x+width >= size_width-10) {
00296 offset_x = prev_x;
00297 offset_y += height;
00298 return 1;
00299 }
00300
00301 if(offset_x > size_width) return 1;
00302
00303 if(offset_y+height > surf->h)
00304 return 2;
00305
00306 }
00307 unlock(surf);
00308
00309 return 0;
00310
00311 }
00312
00313
00314
00315
00316 int SDL_PrintTextDepth(int *depth, struct SDL_Surface *surf, struct SDL_Font *fnt, int x, int y, Uint32 color, const char *src) {
00317 int prev_x = x;
00318 int offset_x = prev_x, offset_y = y;
00319 int width = 0, height = 0;
00320 int i,z,p;
00321
00322 void *pbuf = lock(surf, surf->format->BitsPerPixel);
00323
00324
00325 for(p = 0; p < (int)strlen(src); p++) {
00326 if(src[p] == '\n') {
00327 offset_x = prev_x;
00328 offset_y += height;
00329 continue;
00330 }
00331 for(i = 0; i < fnt->mx; i++) {
00332 for(z = 0; z < fnt->my; z++) {
00333 if(fnt->letters[(int)src[p]].fnt_ptr[i][z] != fnt->tcolor) {
00334 if(offset_y+z > 0 && offset_x+i > 0 && offset_y+z < surf->h && offset_x+i < surf->w)
00335 setpixel(pbuf, (Uint32)offset_x+i, (Uint32)offset_y+z, color, surf->format->BitsPerPixel, surf->pitch);
00336 width=i;
00337 if(height < z)
00338 height=z;
00339 }
00340 }
00341 }
00342 offset_x += width + 3;
00343 if(offset_x+width >= surf->w) {
00344 offset_x = prev_x;
00345 offset_y += height;
00346 }
00347 if(offset_y+height > surf->h)
00348 return 1;
00349
00350 }
00351 unlock(surf);
00352
00353 *depth = offset_x-x;
00354 return 0;
00355 }