#include #include #include #include #include #include #include #include #include #include #define XSIZE 1200 #define YSIZE 32 Uint32 gridcolor,xcolor,ocolor,white; int mousex,mousey; int bgr=0, bgg=00, bgb=0; int fgr=255, fgg=255, fgb=255; SDL_Surface *readppm(char *name) { char line[1024*3], *p; int width=0, height=0; int len; SDL_Surface *s; int x; unsigned char *put; FILE *f; void *res; f=fopen(name, "r"); if(!f) { fprintf(stderr, "Couldn't open %s\n", name); exit(-1); } p=fgets(line, sizeof(line), f); // P6 if(!p || strncmp(p, "P6", 2)) { fclose(f); fprintf(stderr, "Not ppm file %s\n", line); exit(-1); } res=fgets(line, sizeof(line), f); // 480 270 sscanf(line, "%d %d", &width, &height); res=fgets(line, sizeof(line), f); // 255 s = SDL_AllocSurface(SDL_SWSURFACE, width, height, 24, 0x0000ff, 0x00ff00, 0xff0000, 0x0); x=0; put = s->pixels; while((len = fread(line, 1, sizeof(line), f)) > 0) { p=line; while(len>0) { int r,g,b; r=*p++; g=*p++; b=*p++; put[x++] = r; put[x++] = g; put[x++] = b; if(x==width*3) { x = 0; put += s->pitch; } len-=3; } } fclose(f); return s; } SDL_Surface *thescreen; void clear(SDL_Surface *s) { int i,c; c = SDL_MapRGB(s->format, bgr, bgg, bgb); for(i=0;iw;++i) *(unsigned short *)(s->pixels+i*2) = c; for(i=1;ih;++i) memcpy(s->pixels + i*s->pitch, s->pixels, s->w*2); } inline void colordot(SDL_Surface *s, unsigned x,unsigned y,int c) { if(xw && yh) *((uint32_t *)s->pixels+y*thescreen->pitch/4+x)=c; } void scrlock(SDL_Surface *s) { if(SDL_MUSTLOCK(s)) { if ( SDL_LockSurface(s) < 0 ) { fprintf(stderr, "Couldn't lock display surface: %s\n", SDL_GetError()); return; } } } void scrunlock(SDL_Surface *s) { if(SDL_MUSTLOCK(s)) SDL_UnlockSurface(s); } void copyup(SDL_Surface *s) { SDL_UpdateRect(s, 0, 0, 0, 0); } Uint32 maprgb(int r,int g,int b) { return SDL_MapRGB(thescreen->format,r,g,b); } void circle(SDL_Surface *s, int cx,int cy,int radius,int c) { int x,y,e; x=0; y=radius; e=3-(radius<<1); while(x<=y) { colordot(s, cx+x,cy+y,c); colordot(s, cx-x,cy+y,c); colordot(s, cx+x,cy-y,c); colordot(s, cx-x,cy-y,c); colordot(s, cx+y,cy+x,c); colordot(s, cx-y,cy+x,c); colordot(s, cx+y,cy-x,c); colordot(s, cx-y,cy-x,c); if(e<0) e+=(x<<2)+6; else { e+=((x-y)<<2)+10; --y; } ++x; } } #define JUST_CENTER 0 #define JUST_LEFT 1 #define JUST_RIGHT 2 void text_to(SDL_Surface *dest, TTF_Font *font, int x, int y, int just, char *fmt, ...) { SDL_Surface *s; SDL_Rect r; char text[256]; va_list ap; SDL_Color tc = {fgr, fgg, fgb, 0}; va_start(ap, fmt); vsnprintf(text, sizeof(text), fmt, ap); va_end(ap); s = TTF_RenderUTF8_Blended(font, text, tc); r.x = x; if(just == JUST_CENTER) r.x -= s->w/2; else if(just == JUST_RIGHT) r.x -= s->w; r.y = y - s->h/2; SDL_BlitSurface(s, 0, dest, &r); SDL_FreeSurface(s); } struct state { SDL_Surface *surface; SDL_Surface *pictures; TTF_Font *bigfont, *smallfont; int mousex, mousey; }; int writeppm(SDL_Surface *thescreen, char *name) { int ofile; unsigned char text[8192],*p; char temp[128]; uint32_t *take; int i,j; int res; ofile=open(name,O_WRONLY|O_CREAT|O_TRUNC,0644); if(ofile<0) return -1; sprintf(temp,"P6\n"); res=write(ofile,temp,strlen(temp)); sprintf(temp,"%d %d\n", thescreen->w, thescreen->h); res=write(ofile,temp,strlen(temp)); sprintf(temp,"255\n"); res=write(ofile,temp,strlen(temp)); take=(void *) thescreen->pixels; j=thescreen->h; while(j--) { p = text; for(i=0;iw;++i) { SDL_GetRGB(take[i],thescreen->format,p,p+1,p+2); p+=3; } res=write(ofile,text,p-text); take+=thescreen->pitch / sizeof(*take); } //printf("%d\n", thescreen->format->BytesPerPixel); return 0; } void paint_all(struct state *st) { SDL_Surface *s = st->surface; int x, y; char *p; SDL_Surface *ts; SDL_Color tc = {255, 255, 255, 0}; SDL_Rect r; int gap = maprgb(255,0,255); scrlock(s); clear(s); p="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.!:>^@() + "; // p="uvwxyz0123456789.!:>^@() + "; // text_to(s, st->bigfont, 0, 15, JUST_LEFT, "foo!"); x = 0; while(*p) { char text[2]; text[0] = *p++; text[1] = 0; ts = TTF_RenderUTF8_Blended(st->bigfont, text, tc); r.x = x+1; r.y = 15 - ts->h / 2; SDL_BlitSurface(ts, 0, s, &r); for(y=1;yh-2;++y) colordot(s, x, y, gap); x += ts->w+1; SDL_FreeSurface(ts); } scrunlock(st->surface); copyup(st->surface); writeppm(s, "/tmp/font.ppm"); exit(0); } int main(int argc,char **argv) { int first; int code; SDL_Event event; Uint32 videoflags; struct state st; if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); exit(1); } videoflags = 0; thescreen = SDL_SetVideoMode(XSIZE, YSIZE, 32, videoflags); if ( thescreen == NULL ) { fprintf(stderr, "Couldn't set display mode: %s\n", SDL_GetError()); exit(5); } TTF_Init(); memset(&st, 0, sizeof(st)); st.surface = thescreen; // st.pictures = readppm("animals.ppm"); st.bigfont = TTF_OpenFont("hockey.ttf", 30); st.smallfont = TTF_OpenFont("hockey.ttf", XSIZE/16); first=0; for(;;) { usleep(50000); paint_all(&st); while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_MOUSEMOTION: mousex=event.motion.x; mousey=event.motion.y; break; case SDL_MOUSEBUTTONDOWN: break; case SDL_KEYDOWN: code=event.key.keysym.sym; if(code==SDLK_ESCAPE) exit(0); break; } } } return 0; }