#include "serialplot_sdl.h" void draw (int melt, int n, int vals[], double scale[],int position[]) { int i; Uint32 color; static const Uint8 colors[][3] = { { 255, 255, 0 }, // jaune { 0, 255, 0 }, // vert { 0, 0, 255 }, // bleu { 255, 0, 0 }, // rouge { 255, 0, 255 }, // mauve { 0, 255, 255 }, // cyan { 255, 255, 255 }, // blanc }; int c = 0; static int x = 0; int y; //int ymin = 0; /* ystep = la hauteur de la surface / soit par le nb de courbe à * affiché ou par 1 */ //int ystep = screen->h / (melt ? 1 : n); x = (x + 1) % screen->w; /* Vérouille la surface pour y ecrire directement. */ if (SDL_MUSTLOCK (screen)) SDL_LockSurface (screen); clearcol (x); for (i = 0; i < n; i++) { int v = vals[i]; y = screen->h / 2 - v * scale[i] + position[i]; /* saturation des extremes */ if (y<0) y=0; else if(y > screen->h) y = screen->h; color = SDL_MapRGB (screen->format, colors[c][0], colors[c][1], colors[c][2]); putpixel(x,y,color); y = screen->h / 2 + position[i]; color = SDL_MapRGB (screen->format, colors[6][0], colors[6][1], colors[6][2]); putpixel(x,y,color); c = (c + 1) % (sizeof (colors) / sizeof (colors[0])); // double v = vals[i]; // y = ymin + ystep / 2 - v * ystep / 2; // y = y < 0 ? ymin : (y >= ymin + ystep ? ymin + ystep - 1 : y); // color = SDL_MapRGB (screen->format, colors[c][0], colors[c][1], colors[c][2]); // putpixel(x,y,color); // if (!melt) // { // ymin += ystep; // } // // c = (c + 1) % (sizeof (colors) / sizeof (colors[0])); } if (SDL_MUSTLOCK (screen)) SDL_UnlockSurface (screen); } void nextFrame (void) { // static int lastFrame = 0; // static const int minFrame = 1000 / 50; // int curFrame; /* Echange les deux tampons. */ SDL_Flip (screen); /* Freine le programme s'il est trops rapide. */ // curFrame = SDL_GetTicks (); // if (lastFrame && lastFrame + minFrame > curFrame) // SDL_Delay (lastFrame + minFrame - curFrame); /* Enregistre l'heure de dernier rafraichisement. */ // lastFrame = curFrame; } void putpixel(int x, int y, Uint32 pixel) { int bpp = screen ->format->BytesPerPixel; /*Ici p est l'adresse du pixel à colorier*/ Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + bpp * x; switch(bpp) { case 1: *p = pixel; break; case 2: *(Uint16 *)p = pixel; break; case 3: if(SDL_BYTEORDER == SDL_BIG_ENDIAN){ p[0] = (pixel >> 16 ) & 0xff; p[1] = (pixel >> 8) & 0xff; p[2] = pixel & 0xff; } else { p[0] = pixel & 0xff; p[1] = (pixel >> 8) & 0xff; p[2] = (pixel >> 16) & 0xff; } break; case 4: *(Uint32 *)p =pixel; break; } } void clearcol (int x) { int i; int bpp = screen ->format->BytesPerPixel; /*Ici p est l'adresse du pixel à colorier*/ Uint8 *p = (Uint8 *)screen->pixels + bpp * x; Uint32 pixel = SDL_MapRGB (screen->format, 0, 0, 0); switch(bpp) { case 1: for (i = 0; i < screen->h; i++) { *p = pixel; (Uint8 *)p += screen->pitch; } break; case 2: for (i = 0; i < screen->h; i++) { *(Uint16 *)p = pixel; (Uint8 *)p += screen->pitch; } break; case 3: for (i = 0; i < screen->h; i++) { if(SDL_BYTEORDER == SDL_BIG_ENDIAN){ p[0] = (pixel >> 16 ) & 0xff; p[1] = (pixel >> 8) & 0xff; p[2] = pixel & 0xff; } else { p[0] = pixel & 0xff; p[1] = (pixel >> 8) & 0xff; p[2] = (pixel >> 16) & 0xff; } (Uint8 *)p += screen->pitch; } break; case 4: for (i = 0; i < screen->h; i++) { *(Uint32 *)p =pixel; (Uint8 *)p += screen->pitch; } break; } } void draw_inf (int n, int vals[]) { int i; int h1; char chaine[255]; SDL_Surface *surf_text; SDL_Rect destpos; BFont_Info *Font1=NULL; Font1 = BFont_LoadFont("font.png"); if (Font1 == NULL) { fprintf(stderr, "Couldnt load %s\n","font.png"); exit(1); } h1 = BFont_FontHeight(Font1); BFont_SetCurrentFont(Font1); if (SDL_MUSTLOCK (screen)) SDL_LockSurface (screen); for (i = 0; i < n; i++) { double variance; double moyenne; double somme = 0; int j; // calcul de la moyenne cpt[i]++; // convertion des valeur négative de 2octect à 4octect if ( ((int)vals[i]) & 0x8000) old_value[cpt[i]%32+i*32] = 0xffff8000 | (int) (vals[i]); else old_value[cpt[i]%32+i*32] = (int) (vals[i]); for (j = 0; (j < cpt[i]) && (j < 32);j++) { somme += old_value[j+i*32]; } if (cpt[i] < 32) moyenne = somme / cpt[i]; else moyenne = somme / 32; variance = vals[i] * vals[i] - moyenne * moyenne; sprintf(chaine,"var : %f ",variance); surf_text = BFont_CreateSurfaceFont(Font1, chaine); if (surf_text != NULL) { // Blit it on the bottom right corner memset(&destpos, 0, sizeof(SDL_Rect)); destpos.x = 0; destpos.y = 480 - surf_text->h - i * 15 ; destpos.w = surf_text->w; destpos.h = surf_text->h; SDL_Surface *dest; dest = SDL_CreateRGBSurface(SDL_HWSURFACE, surf_text->w + 10, surf_text->h, surf_text->format->BitsPerPixel, surf_text->format->Rmask, surf_text->format->Gmask, surf_text->format->Bmask,0); SDL_FillRect(dest,&destpos,0); SDL_BlitSurface(dest, NULL, screen, &destpos); SDL_BlitSurface(surf_text, NULL, screen, &destpos); SDL_FreeSurface(dest); SDL_FreeSurface(surf_text); } sprintf(chaine,"moy : %f ", moyenne); surf_text = BFont_CreateSurfaceFont(Font1, chaine); if (surf_text != NULL) { // Blit it on the bottom right corner memset(&destpos, 0, sizeof(SDL_Rect)); destpos.x = 100; destpos.y = 480 - surf_text->h - i * 15 ; destpos.w = surf_text->w; destpos.h = surf_text->h; SDL_Surface *dest; dest = SDL_CreateRGBSurface(SDL_HWSURFACE, surf_text->w + 10, surf_text->h, surf_text->format->BitsPerPixel, surf_text->format->Rmask, surf_text->format->Gmask, surf_text->format->Bmask,0); SDL_FillRect(dest,&destpos,0); SDL_BlitSurface(dest, NULL, screen, &destpos); SDL_BlitSurface(surf_text, NULL, screen, &destpos); SDL_FreeSurface(dest); SDL_FreeSurface(surf_text); } } BFont_FreeFont(Font1); if (SDL_MUSTLOCK (screen)) SDL_UnlockSurface (screen); } void erreur (void) { fprintf (stderr, "Erreur SDL : %s\n", SDL_GetError ()); exit (1); }