/* graficos.c: part of the 4bpp software. Here are the more complex functions, dealing with text fonts and figure drawing. Copyright (C) 2002 Agustin Ferrin Pozuelo gato.vze.com This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "graficos.h" #include "lcd.c" Tipo_letra tipo_letra_actual = NULL; int text_x=0,text_y=0; int text_izq=0,text_arr=0, text_der=LCD_ANCHO_DEFECTO, text_aba=LCD_ALTO_DEFECTO; void snap(char *nomarchivo) { FILE *ficheropgm; unsigned char *puntero; long tam; PRINTD("Abriendo %s...\n",nomarchivo); if((ficheropgm=fopen(nomarchivo,"w"))==NULL){ PRINTD("Error abriendo %s\n",nomarchivo); return; } fprintf(ficheropgm,"P5\n#Creado por snap.c (AFP)\n320\n240\n15\n"); PRINTD("Tipo :P5 (RAW GRAY SCALE)\n"); puntero=LCD_DIRECCION_BASE8; tam=LCD_ANCHO_VIRTUAL*LCD_ALTO_VIRTUAL/(8/LCD_BPP); while(tam--){ fputc(((*puntero)&0xF0)/16,ficheropgm); fputc(((*puntero++)&0x0F),ficheropgm); } fclose(ficheropgm); } inline void outrectd(int x, int y, int dx, int dy, unsigned short color) { linea_vd(x,y+1,dy-2,color); linea_vd(x+dx-1,y+1,dy-2,color); linea_hd(x,y,dx,color); linea_hd(x,y+dy-1,dx,color); } #define ALGORITMO_LINEA(xx1,xx2,yy1,yy2,dx,dy,linea_hd)\ {\ if(xx1>xx2){xx1=xx2;a=yy1;yy1=yy2;yy2=a;}\ a=dx;\ b=dx/dy;\ c=b*dy;\ if(yy2>yy1){\ while(yy2>=yy1){\ a-=c;\ if(a>0){\ linea_hd(x1,y1,b+1,color);\ xx1+=b+1;\ a-=dy;\ }else{\ linea_hd(x1,y1,b,color);\ xx1+=b;\ }\ a+=dx;\ yy1++;\ }\ } else {\ while(yy2<=yy1){\ a-=c;\ if(a>0){\ linea_hd(x1,y1,b+1,color);\ xx1+=b+1;\ a-=dy;\ }else{\ linea_hd(x1,y1,b,color);\ xx1+=b;\ }\ a+=dx;\ yy1--;\ }\ }\ } inline void linea(int x1, int y1, int x2, int y2, unsigned short color) { register int dx,dy,a,b,c; if(x2dy) ALGORITMO_LINEA(x1,x2,y1,y2,dx,dy,linea_hd) else ALGORITMO_LINEA(y1,y2,x1,x2,dy,dx,linea_vd) } inline void textwindow(int x1,int y1,int x2,int y2){text_izq=x1,text_arr=y1, text_der=x2,text_aba=y2;} inline int textwidth(unsigned char *texto) { register int w; register short c; w=0; while((c=*(texto++))) w+=tipo_letra_actual->letra[c]->ancho; return w; } void textcenter(int x,int y,unsigned char *texto, enum modo_blit modo) { textgoto(x-(textwidth(texto)/2),y-((tipo_letra_actual->altura)/2)); textput(texto,modo); } void textput(unsigned char *texto, enum modo_blit modo) { register LCD_imagen letra_img; register int a,c; if(tipo_letra_actual == NULL) return; a=text_der-text_x; while((c=*(texto++))) { // PRINTD("%d,%d ",text_x,text_y); if(c=='\n'){ text_y+=tipo_letra_actual->altura; if(text_y>text_aba-tipo_letra_actual->altura){text_y=text_arr;} text_x=text_izq; a=text_der-text_x; } else { letra_img=tipo_letra_actual->letra[c]; if(letra_img != NULL) { if(a<=letra_img->ancho){ text_y+=tipo_letra_actual->altura; if(text_y>text_aba-tipo_letra_actual->altura){text_y=text_arr;} text_x=text_izq; a=text_der-text_x; if(c!=' ') { blitimage(text_x,text_y,letra_img,modo); a-=letra_img->ancho; text_x+=letra_img->ancho; } } else { blitimage(text_x,text_y,letra_img,modo); a-=letra_img->ancho; text_x+=letra_img->ancho; } } } } } Tipo_letra loadfont(char * archivo) { int fd,c; Tipo_letra T; unsigned char C; LCD_imagen img; fd=open(archivo, O_RDONLY); if(fd<0) return NULL; T=malloc(sizeof(Tipo_letra_struct)); T->nombre = archivo; T->altura = 0; // PRINTD("\nCargando la fuente %s...\n",archivo); c=TIPO_NLETRAS; while(c--){T->letra[c]=NULL;} while(read(fd,&C,1)==1) { // PRINTD("%c",C); img=loadimage_intern(fd); T->letra[C]=img; if(T->altura < img->alto) T->altura = img->alto; // putimage(0,0,T->letra[C]); // sleep(1); } tipo_letra_actual = T; close(fd); return T; }