Guest User

colour_table

a guest
May 7th, 2010
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.00 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include "SDL/SDL.h"
  3. #include <string.h>
  4.  
  5. #define TEXTFILE "txtdata.txt"
  6. #define WIDTH 500
  7. #define HIGHT 190
  8. #define NOCOLOURS 949
  9. SDL_Surface *screen;
  10. /********************************************/
  11.  
  12.  
  13. void window()
  14. /***  makes a window using surface screen ***/
  15. {
  16.     screen = SDL_SetVideoMode(WIDTH, HIGHT, 16, SDL_SWSURFACE);
  17.     SDL_WM_SetCaption("circle","circle");
  18.     if ( screen == NULL ) {
  19.         fprintf(stderr, "Unable to set the video mode: %s\n", SDL_GetError());
  20.         exit(1);
  21.     }
  22. }
  23. /*********************/
  24.  
  25. int read_data(char names[NOCOLOURS][100],char colours[NOCOLOURS][100],int maxlength)
  26. /***** reads data from file "TEXTFILE" *****/
  27. {
  28. int x,y;
  29. char c;
  30. FILE *stream;
  31.  
  32.     stream = fopen (TEXTFILE, "r");
  33.  
  34.     for(x=0;x<maxlength;x++){
  35.         for(y=0;y<100;y++){
  36.             c=getc(stream);
  37.             if(c=='#')
  38.                 {break;}
  39.             if(c==EOF)
  40.                 {return(0);}
  41.             names[x][y]=c;
  42.             }
  43.         for(y=0;y<100;y++){
  44.             c=getc(stream);
  45.             if(c==EOF)
  46.                 {return(0);}
  47.             colours[x][y]=c;
  48.             if(c=='\n')
  49.                 {break;}
  50.             }
  51.         }
  52.        
  53. }
  54. /***************************************/
  55. int convert(char colours[NOCOLOURS][100],unsigned int colourdata[NOCOLOURS],int maxlength)
  56. /**** turn text rgb data into real numbers ****/
  57. {
  58.  int x;
  59.     for(x=0;x<maxlength;x++){
  60.             sscanf(colours[x],"%x",&colourdata[x]);
  61.     }
  62.  
  63. }
  64. /******************************************************/
  65. void draw(int xco,int wit,int yco,int hig,Uint32 colour)
  66.     /**** draw a square on the screen *****/
  67. {
  68.  SDL_Rect dest;
  69.  
  70.     dest.x = xco;
  71.     dest.y = yco;
  72.     dest.w = wit;
  73.     dest.h = hig;
  74.    
  75.     SDL_FillRect(screen, &dest, colour);   
  76. }
  77. /*************************************************/
  78. int convert_RGB(unsigned int colourdata, unsigned int *red,
  79.              unsigned int *green, unsigned int *blue)
  80. {
  81.     *blue=colourdata & 255;
  82.     *green=(colourdata >>8)&255;
  83.     *red =(colourdata >>16)&255;
  84. }
  85. /**************************************************/
  86. int createsplashscreen(unsigned int colourdata[NOCOLOURS],int maxcolours)
  87. /***** create screen with all the colours on it *******/
  88.  {unsigned int red,green,blue;
  89.  int s,y,x;
  90.  Uint32 colour;
  91.     for(s=0;s<maxcolours;s++){
  92.        
  93.         convert_RGB(colourdata[s], &red,&green, &blue);
  94.         y=s/50;
  95.         x=s%50;
  96.         colour=SDL_MapRGB(screen->format, red, green, blue);       
  97.         draw(x*10,10,y*10,10,colour);
  98.        
  99.     }
  100.     SDL_Flip(screen);
  101. }
  102. /************************************************/
  103. void get_input(char names[NOCOLOURS][100], unsigned int colourdata[1000])
  104. {
  105. SDL_Event test_event;
  106. SDL_PumpEvents();
  107. Uint8* KEYS = SDL_GetKeyState(NULL);
  108. int Xloc,Yloc,XSqr,YSqr;
  109. int nocol;
  110.     if (KEYS[SDLK_ESCAPE])
  111.     {  
  112.         exit(0);}
  113.  
  114.     while(SDL_PollEvent(&test_event)) {
  115.     if(test_event.type==SDL_QUIT) {exit(0);}
  116.     }  
  117.     if(SDL_GetMouseState(&Xloc, &Yloc) == SDL_BUTTON(1)){
  118.         XSqr=Xloc/10;
  119.         YSqr=Yloc/10;
  120.         nocol=XSqr+YSqr*50;
  121.         fill_screen(names, colourdata,nocol);
  122.     }
  123.  
  124. }
  125. /************************************************/
  126. int fill_screen(char names[NOCOLOURS][100], unsigned int colourdata[NOCOLOURS],int colourchoice)
  127. {
  128.  unsigned int red, green, blue;
  129.  Uint32 colour;
  130.  char command[200];
  131.     convert_RGB(colourdata[colourchoice], &red,&green, &blue);
  132.     colour=SDL_MapRGB(screen->format, red, green, blue);
  133.     draw(0,WIDTH,0,HIGHT,colour);
  134.     SDL_Flip(screen);
  135.     sprintf(command,"espeak \"%s\"",names[colourchoice]);
  136.     system(command);
  137.     printf("%d: %s r;%d g;%d b;%d\n",colourchoice,names[colourchoice],red,green,blue);
  138.     createsplashscreen(colourdata,NOCOLOURS);
  139. }
  140. /************************************************/
  141. int main()
  142. {
  143.  char names[NOCOLOURS][100];
  144.     //contains names of colours
  145.  char colours[NOCOLOURS][100];
  146.     //contains colourdata in HEX
  147.  unsigned int colourdata[NOCOLOURS];
  148.     //contains colourdata in RGB
  149.  
  150.  int x,y;
  151.     read_data(names,colours,NOCOLOURS);
  152.     convert(colours,colourdata,NOCOLOURS);
  153.     for(x=0;x<NOCOLOURS;x++){
  154.         printf(names[x]);
  155.         printf("%x\n",colourdata[x]);
  156.     }
  157.     window();
  158.     createsplashscreen(colourdata,NOCOLOURS);
  159.  
  160.     while(1){
  161.         get_input(names,  colourdata);
  162.         usleep(10000);
  163.         }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment