Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include "SDL/SDL.h"
- #include <string.h>
- #define TEXTFILE "txtdata.txt"
- #define WIDTH 500
- #define HIGHT 190
- #define NOCOLOURS 949
- SDL_Surface *screen;
- /********************************************/
- void window()
- /*** makes a window using surface screen ***/
- {
- screen = SDL_SetVideoMode(WIDTH, HIGHT, 16, SDL_SWSURFACE);
- SDL_WM_SetCaption("circle","circle");
- if ( screen == NULL ) {
- fprintf(stderr, "Unable to set the video mode: %s\n", SDL_GetError());
- exit(1);
- }
- }
- /*********************/
- int read_data(char names[NOCOLOURS][100],char colours[NOCOLOURS][100],int maxlength)
- /***** reads data from file "TEXTFILE" *****/
- {
- int x,y;
- char c;
- FILE *stream;
- stream = fopen (TEXTFILE, "r");
- for(x=0;x<maxlength;x++){
- for(y=0;y<100;y++){
- c=getc(stream);
- if(c=='#')
- {break;}
- if(c==EOF)
- {return(0);}
- names[x][y]=c;
- }
- for(y=0;y<100;y++){
- c=getc(stream);
- if(c==EOF)
- {return(0);}
- colours[x][y]=c;
- if(c=='\n')
- {break;}
- }
- }
- }
- /***************************************/
- int convert(char colours[NOCOLOURS][100],unsigned int colourdata[NOCOLOURS],int maxlength)
- /**** turn text rgb data into real numbers ****/
- {
- int x;
- for(x=0;x<maxlength;x++){
- sscanf(colours[x],"%x",&colourdata[x]);
- }
- }
- /******************************************************/
- void draw(int xco,int wit,int yco,int hig,Uint32 colour)
- /**** draw a square on the screen *****/
- {
- SDL_Rect dest;
- dest.x = xco;
- dest.y = yco;
- dest.w = wit;
- dest.h = hig;
- SDL_FillRect(screen, &dest, colour);
- }
- /*************************************************/
- int convert_RGB(unsigned int colourdata, unsigned int *red,
- unsigned int *green, unsigned int *blue)
- {
- *blue=colourdata & 255;
- *green=(colourdata >>8)&255;
- *red =(colourdata >>16)&255;
- }
- /**************************************************/
- int createsplashscreen(unsigned int colourdata[NOCOLOURS],int maxcolours)
- /***** create screen with all the colours on it *******/
- {unsigned int red,green,blue;
- int s,y,x;
- Uint32 colour;
- for(s=0;s<maxcolours;s++){
- convert_RGB(colourdata[s], &red,&green, &blue);
- y=s/50;
- x=s%50;
- colour=SDL_MapRGB(screen->format, red, green, blue);
- draw(x*10,10,y*10,10,colour);
- }
- SDL_Flip(screen);
- }
- /************************************************/
- void get_input(char names[NOCOLOURS][100], unsigned int colourdata[1000])
- {
- SDL_Event test_event;
- SDL_PumpEvents();
- Uint8* KEYS = SDL_GetKeyState(NULL);
- int Xloc,Yloc,XSqr,YSqr;
- int nocol;
- if (KEYS[SDLK_ESCAPE])
- {
- exit(0);}
- while(SDL_PollEvent(&test_event)) {
- if(test_event.type==SDL_QUIT) {exit(0);}
- }
- if(SDL_GetMouseState(&Xloc, &Yloc) == SDL_BUTTON(1)){
- XSqr=Xloc/10;
- YSqr=Yloc/10;
- nocol=XSqr+YSqr*50;
- fill_screen(names, colourdata,nocol);
- }
- }
- /************************************************/
- int fill_screen(char names[NOCOLOURS][100], unsigned int colourdata[NOCOLOURS],int colourchoice)
- {
- unsigned int red, green, blue;
- Uint32 colour;
- char command[200];
- convert_RGB(colourdata[colourchoice], &red,&green, &blue);
- colour=SDL_MapRGB(screen->format, red, green, blue);
- draw(0,WIDTH,0,HIGHT,colour);
- SDL_Flip(screen);
- sprintf(command,"espeak \"%s\"",names[colourchoice]);
- system(command);
- printf("%d: %s r;%d g;%d b;%d\n",colourchoice,names[colourchoice],red,green,blue);
- createsplashscreen(colourdata,NOCOLOURS);
- }
- /************************************************/
- int main()
- {
- char names[NOCOLOURS][100];
- //contains names of colours
- char colours[NOCOLOURS][100];
- //contains colourdata in HEX
- unsigned int colourdata[NOCOLOURS];
- //contains colourdata in RGB
- int x,y;
- read_data(names,colours,NOCOLOURS);
- convert(colours,colourdata,NOCOLOURS);
- for(x=0;x<NOCOLOURS;x++){
- printf(names[x]);
- printf("%x\n",colourdata[x]);
- }
- window();
- createsplashscreen(colourdata,NOCOLOURS);
- while(1){
- get_input(names, colourdata);
- usleep(10000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment