Advertisement
Guest User

fc3_with_sfont_test.c

a guest
Dec 27th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.57 KB | None | 0 0
  1. /*  flip SDL clock
  2. ******************************************************************
  3.   fc3.c
  4.   Nokia N810
  5.  
  6.   gcc -g -Wall `sdl-config --cflags` `sdl-config --libs` -L /usr/include/SDL -lSDL_image fc3.c -o fc
  7.   Ciro Ippolito 2009
  8.   ******************************************************************
  9. */
  10.  
  11. /*2DO
  12. one png for the entire number sets
  13. ...
  14. ...
  15. ********************************************************************
  16. */
  17.  
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include "SDL_image.h"
  21. #if defined(_MSC_VER)
  22. #include "SDL.h"
  23. #else
  24. #include "SDL/SDL.h"
  25. #endif
  26.  
  27. #ifdef ZIPIT_Z2
  28. #include "SFont.h"
  29.  
  30. SFont_Font* Font=NULL;
  31. SDL_Surface *font=NULL;
  32.  
  33. SDL_Surface* fade;
  34. SDL_Surface* fadeSurface;
  35.  
  36. #endif
  37.  
  38. SDL_Surface *screen;
  39. SDL_Surface *secs;
  40. SDL_Surface *fondo;
  41. SDL_Surface *zero;
  42. SDL_Surface *uno;
  43. SDL_Surface *due;
  44. SDL_Surface *tre;
  45. SDL_Surface *quattro;
  46. SDL_Surface *cinque;
  47. SDL_Surface *sei;
  48. SDL_Surface *sette;
  49. SDL_Surface *otto;
  50. SDL_Surface *nove;
  51. SDL_Surface *numbers[10];
  52. SDL_Surface *elements[10];
  53. void initBMP()
  54. {
  55.     elements[0] = IMG_Load("fondo.png");
  56.     elements[1] = IMG_Load("barra.png");
  57.     elements[2] = IMG_Load("secs.png");
  58.     numbers[0]  = IMG_Load("0.png");
  59.     numbers[1]  = IMG_Load("1.png");
  60.     numbers[2]  = IMG_Load("2.png");
  61.     numbers[3]  = IMG_Load("3.png");
  62.     numbers[4]  = IMG_Load("4.png");
  63.     numbers[5]  = IMG_Load("5.png");
  64.     numbers[6]  = IMG_Load("6.png");
  65.     numbers[7]  = IMG_Load("7.png");
  66.     numbers[8]  = IMG_Load("8.png");
  67.     numbers[9]  = IMG_Load("9.png");
  68. }
  69.  
  70. void LoadBMP(char *file, SDL_Surface *surface)
  71. {
  72.  surface= SDL_LoadBMP(file);
  73.    if ( surface == NULL ) {
  74.         fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
  75.         return;
  76.     }
  77.   return;
  78. }
  79.  
  80. void BMPShow(SDL_Surface *screen, SDL_Surface *pic,int x, int y)
  81. {
  82.   SDL_Rect dest;
  83.   dest.x = x;
  84.   dest.y = y;
  85.   dest.w = pic->w;
  86.   dest.h = pic->h;
  87.   SDL_BlitSurface(pic, NULL, screen, &dest);
  88.     // Update the changed portion of the screen
  89.     SDL_UpdateRects(screen, 1, &dest);
  90.  
  91.   return;
  92. }
  93. void BMPShowArray(SDL_Surface *screen, SDL_Surface *pic,int x, int y)
  94. {
  95.   SDL_Rect dest;
  96.   dest.x = x;
  97.   dest.y = y;
  98.   dest.w = pic->w;
  99.   dest.h = pic->h;
  100.   SDL_BlitSurface(pic, NULL, screen, &dest);
  101.   SDL_UpdateRects(screen, 1, &dest);
  102.   return;  
  103. }
  104. void ShowBMP(char *file, SDL_Surface *screen, int x, int y)
  105. {
  106.     SDL_Surface *image;
  107.     SDL_Rect dest;
  108.     image = SDL_LoadBMP(file);
  109.     if ( image == NULL ) {
  110.         fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
  111.         return;
  112.     }
  113.     dest.x = x;
  114.     dest.y = y;
  115.     dest.w = image->w;
  116.     dest.h = image->h;
  117.     SDL_BlitSurface(image, NULL, screen, &dest);
  118.     SDL_UpdateRects(screen, 1, &dest);
  119. }
  120.  
  121. Uint32 TimerFunc( Uint32 interval, void *p )
  122. {
  123.     printf("ciao modno\n");
  124.     return 1000;
  125. }
  126.  
  127. int checknumbers(char number,int x, int y){
  128.     switch (number){
  129.         case '0':
  130.       //if (x > 10)
  131.             BMPShow (screen,numbers[0] , x   ,y);
  132.             break;
  133.         case '1':
  134.             BMPShow (screen,numbers[1] , x   ,y);
  135.             break;
  136.         case '2':
  137.             BMPShow (screen,numbers[2] , x   ,y);
  138.             break;
  139.         case '3':
  140.             BMPShow (screen,numbers[3] , x   ,y);
  141.             break;
  142.         case '4':
  143.             BMPShow (screen,numbers[4] , x   ,y);
  144.             break;
  145.         case '5':
  146.             BMPShow (screen,numbers[5] , x   ,y);
  147.             break;
  148.         case '6':
  149.             BMPShow (screen,numbers[6] , x   ,y);
  150.             break;
  151.         case '7':
  152.             BMPShow (screen,numbers[7] , x   ,y);
  153.             break;
  154.         case '8':
  155.             BMPShow (screen,numbers[8] , x   ,y);
  156.             break;
  157.         case '9':
  158.             BMPShow (screen,numbers[9] , x   ,y);
  159.             break;
  160.     }    
  161. return 1;
  162.    
  163. }
  164.  
  165. Uint32 UpdateClock (Uint32 interval, void *p)
  166.   {
  167.     char s[30];
  168.     size_t i;
  169.     struct tm tim;
  170.     time_t now;
  171.     now = time(NULL);
  172.     tim = *(localtime(&now));
  173.  
  174. #ifdef ZIPIT_Z2
  175.     // Redraw the background if we allow spaces in strftime hours %l.
  176.     BMPShow (screen, elements[0] ,0   ,0);        //Fondo
  177.  
  178.     i = strftime(s,30,"%b %d, %Y; %l:%M:%S\n",&tim);
  179.     checknumbers(s[14],  0,8);                             //crappier time routine
  180.     checknumbers(s[15],77,8);                             //crappier time routine 2
  181.     checknumbers(s[17],166,8);                             //crappier time routine  3
  182.     checknumbers(s[18],243,8);                             //crappier time routine   4
  183.     BMPShow (screen,elements[1] ,0   ,0);                   //Redraw the top minute bar
  184.     BMPShow (screen,elements[2] , (tim.tm_sec*52/10)+4 ,0);     //Seconds arrow
  185.     if (Font)
  186.     {
  187.       SDL_Rect dest;
  188.       dest.x = 0;
  189.       dest.y = 200;
  190.       dest.w = 320; //SFont_TextWidth(Font, s);
  191.       dest.h = SFont_TextHeight(Font);
  192.       SDL_FillRect(screen, &dest, 0 );
  193.  
  194.       printf("Clock text test %s\n",s);
  195.       //SFont_Write(screen, Font, 0, 160, "Clock text test");
  196.       SFont_WriteCenter(screen,Font,200,s);
  197.  
  198.       // Cover the text with a dark alpha blended rect.
  199.       SDL_BlitSurface(fade, NULL, screen, &dest);
  200.  
  201.       //SDL_FillRect(screen, &dest, 0 );
  202.       //SDL_FillRect(screen, &dest, SDL_MapRGBA(screen->format,0,0,0,127));
  203.  
  204.       // Update the screen (Should just calculate the rect)
  205.       SDL_UpdateRect(screen, 0, 0, 0, 0);
  206.     }
  207. #else
  208.     i = strftime(s,30,"%b %d, %Y; %H:%M:%S\n",&tim);
  209.     checknumbers(s[14],  2,20);                             //crappier time routine
  210.     checknumbers(s[15],193,20);                             //crappier time routine 2
  211.     checknumbers(s[17],415,20);                             //crappier time routine  3
  212.     checknumbers(s[18],605,20);                             //crappier time routine   4
  213.     BMPShow (screen,elements[1] ,0   ,0);                   //Redraw the top minute bar
  214.     BMPShow (screen,elements[2] , tim.tm_sec*13+10 ,0);     //Seconds arrow
  215. #endif
  216.     return 1000;
  217.   }
  218. /* ----------------------------------------------------------------------------
  219. MAIN ()
  220.  ----------------------------------------------------------------------------*/
  221. int main(int argc, char *argv[])
  222. {
  223.   //    if ( SDL_Init(SDL_INIT_EVERYTHING) < 0 )
  224.     if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
  225.     {
  226.         fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
  227.         exit(1);
  228.     }
  229.     SDL_Init(SDL_INIT_TIMER);
  230.     // Register SDL_Quit to be called at exit; makes sure things are
  231.     atexit(SDL_Quit);                               // cleaned up when we quit.
  232. #ifdef ZIPIT_Z2
  233.     screen = SDL_SetVideoMode(320, 240, 16, SDL_SWSURFACE);                   // Window PC
  234. #else
  235.     //screen = SDL_SetVideoMode(800, 480, 16, SDL_SWSURFACE|SDL_FULLSCREEN);  // Full screen Tablet
  236.     screen = SDL_SetVideoMode(800, 480, 16, SDL_SWSURFACE);                   // Window PC
  237. #endif
  238.     if ( screen == NULL )                                                     // If fail, return error
  239.     {
  240.         fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
  241.         exit(1);
  242.     }
  243.  
  244. #ifdef ZIPIT_Z2
  245.  
  246.     // WARNING!!  Gotta fix sfont for utf8 bits or this crashes on gmenu2x font.
  247.     //            Maybe just stop at first utf8 (190?)
  248.  
  249.     //Font = SFont_InitFont(IMG_Load("font.png"));
  250.     font = IMG_Load("font.png");
  251.     Font = SFont_InitFont(font);
  252.     if(!Font) {
  253.       fprintf(stderr, "An error occured while loading the font.\n");
  254.     exit(1);
  255.     }
  256.     printf("sfont = %d, %d\n", font, Font);
  257.  
  258.     //SDL_SetAlpha(Font->Surface, SDL_SRCALPHA | SDL_RLEACCEL, 128);
  259.  
  260.     // Create a surface with alpha to use for fade effect.
  261.     fadeSurface = SDL_CreateRGBSurface( SDL_SWSURFACE | SDL_SRCALPHA, 320, 240, 32, 0xff000000,0x00ff0000,0x0000ff00,0x000000ff);
  262.  
  263.     // Convert it to the format of the screen
  264.     fade = SDL_DisplayFormat( fadeSurface );
  265.     SDL_SetAlpha(fade, SDL_SRCALPHA | SDL_RLEACCEL, 128);
  266.  
  267.     // Free the created surface
  268.     SDL_FreeSurface( fadeSurface );
  269.    
  270. #endif
  271.  
  272.     initBMP();
  273.     BMPShow (screen, elements[0] ,0   ,0);        //Fondo
  274.     BMPShow (screen, elements[1] ,0   ,0);        //barra minuti
  275.         char s[30];
  276.         size_t i;
  277.         struct tm tim;
  278.         time_t now;
  279.         now = time(NULL);
  280.         tim = *(localtime(&now));
  281. #ifdef ZIPIT_Z2
  282.         i = strftime(s,30,"%b %d, %Y; %l:%M:%S\n",&tim);
  283.         checknumbers(s[14],  0,8);
  284.         checknumbers(s[15],77,8);
  285.         checknumbers(s[17],166,8);
  286.         checknumbers(s[18],243,8);
  287.  
  288.     if (Font)
  289.     {
  290.       SDL_Rect dest;
  291.       dest.x = 0;
  292.       dest.y = 160;
  293.       dest.w = 320; //SFont_TextWidth(Font, s);
  294.       dest.h = SFont_TextHeight(Font);
  295.  
  296.       printf("Clock text test %s\n",s);
  297.       SFont_Write(screen, Font, 0, 160, "Clock text test");
  298.       //SFont_WriteCenter(screen,Font,200,s);
  299.  
  300.       // Fade it.
  301.       SDL_BlitSurface(fade, NULL, screen, &dest);
  302.       // Double the dark.
  303.       SDL_BlitSurface(fade, NULL, screen, &dest);
  304.  
  305.       // Update the screen (Should just calculate the rect)
  306.       SDL_UpdateRect(screen, 0, 0, 0, 0);
  307.     }
  308. #else
  309.         i = strftime(s,30,"%b %d, %Y; %H:%M:%S\n",&tim);
  310.         checknumbers(s[14],  2,20);
  311.         checknumbers(s[15],193,20);
  312.         checknumbers(s[17],415,20);
  313.         checknumbers(s[18],605,20);
  314. #endif
  315.     SDL_AddTimer (1000,UpdateClock,NULL); ///////////////////////////////////////////// ////
  316.     typedef enum { FALSE, TRUE } boolean;
  317.     boolean clockticking = TRUE;
  318.     while (clockticking)                            // Main loop: loop forever
  319.     {
  320.         SDL_Event event;                            // Poll for events
  321.         while (SDL_PollEvent(&event)) {
  322.             switch (event.type) {
  323.             case SDL_KEYDOWN:
  324. #ifdef ZIPIT_Z2
  325.             if (event.key.keysym.sym == SDLK_ESCAPE)    // If ESC is pressed, return (and quit)
  326.                 return 0;
  327. #else
  328.                 if (event.key.keysym.sym == SDLK_a) {
  329.                 printf("premuto A");
  330.             }
  331.             else if (event.key.keysym.sym == SDLK_s){
  332.                 printf("premuto S");
  333.             }
  334.             else if (event.key.keysym.sym == SDLK_d){
  335.                 printf("premuto D");
  336.             }
  337.             else if (event.key.keysym.sym == SDLK_f){
  338.                 printf("premuto F");
  339.             }
  340. #endif
  341.             else if (event.key.keysym.sym == SDLK_g)
  342.                 BMPShow (screen, elements[0]  ,0    ,0);
  343.         break;
  344.             case SDL_KEYUP:
  345.             if (event.key.keysym.sym == SDLK_q)     // If Q is pressed, return (and zen quit)
  346.                 return 0;
  347.                 break;    
  348.             case SDL_QUIT:
  349.                 return(0);
  350.             }
  351.         }
  352.     }
  353.     return 0;
  354. }
  355.  
  356.  
  357. /*let's check is I can still fly  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement