Guest
Private paste!

WaveHText & WaveVText with SDL by pyroesp

By: a guest | Jul 30th, 2010 | Syntax: C | Size: 4.22 KB | Hits: 5 | Expires: Never
Copy text to clipboard
  1. #include <SDL/SDL.h>
  2. #include <SDL/SDL_ttf.h>
  3.  
  4. #include <math.h>
  5.  
  6. #define SCR_H 272
  7. #define SCR_W 480
  8.  
  9. #define OFFSET 1
  10.  
  11. typedef struct
  12. {
  13.     float amplitude,
  14.     degree,
  15.     freq;
  16. }Sinus;
  17.  
  18. void EventControl(int *_done, SDL_Event *_event, Sinus *_sinus)
  19. {
  20.     SDL_PollEvent(_event);
  21.  
  22.     switch(_event->type)
  23.     {
  24.         case SDL_QUIT:
  25.             *_done = 1;
  26.             break;
  27.         case SDL_KEYDOWN:
  28.             switch(_event->key.keysym.sym)
  29.             {
  30.                 case SDLK_ESCAPE:
  31.                     *_done = 1;
  32.                     break;
  33.                 case SDLK_SPACE:
  34.                     *_done = 1;
  35.                     break;
  36.                 case SDLK_UP:
  37.                     _sinus->amplitude += OFFSET;
  38.                     _sinus->amplitude = _sinus->amplitude >= (SCR_H/2 - 15) ? (SCR_H/2 - 15) : _sinus->amplitude;
  39.                     break;
  40.                 case SDLK_DOWN:
  41.                     _sinus->amplitude -= OFFSET;
  42.                     _sinus->amplitude = _sinus->amplitude <= 0 ? 0 : _sinus->amplitude;
  43.                     break;
  44.                 case SDLK_RIGHT:
  45.                     _sinus->freq += OFFSET;
  46.                     break;
  47.                 case SDLK_LEFT:
  48.                     _sinus->freq -= OFFSET;
  49.                     _sinus->freq = _sinus->freq <= 0 ? 0 : _sinus->freq;
  50.                     break;
  51.             }
  52.             break;
  53.         default:
  54.             *_done = 0;
  55.     }
  56. }
  57.  
  58. void WaveVText(int _x, int _y, Sinus *_sinus, const char* _text, SDL_Surface *_source, TTF_Font *_font, SDL_Color _color)
  59. {
  60.     SDL_Rect pos = {_x, _y};
  61.     int i = 0, textsize = strlen(_text);
  62.  
  63.     for (i = 0; i < textsize; i++)
  64.     {
  65.         char help[2] = {0};
  66.         help[0] = _text[i];
  67.  
  68.         _sinus->degree = _sinus->degree >= 360 ? _sinus->degree - 360 : _sinus->degree;
  69.         pos.y += sinf(_sinus->degree * M_PI / 180.0) * _sinus->amplitude;
  70.  
  71.         SDL_Surface *text = NULL;
  72.         text = TTF_RenderText_Blended(_font, help, _color);
  73.         SDL_BlitSurface(text, NULL, _source, &pos);
  74.  
  75.         _sinus->degree += _sinus->freq;
  76.         pos.x += text->w;
  77.         pos.y = _y;
  78.  
  79.         SDL_FreeSurface(text);
  80.     }
  81. }
  82.  
  83. void WaveHText(int _x, int _y, Sinus *_sinus, const char* _text, SDL_Surface *_source, TTF_Font *_font, SDL_Color _color)
  84. {
  85.     SDL_Rect pos = {_x, _y}, cut = {0, 0, 0, 0};
  86.     int i = 0, textsize = strlen(_text);
  87.  
  88.     SDL_Surface *text = NULL;
  89.     text = TTF_RenderText_Blended(_font, _text, _color);
  90.  
  91.     cut.w = text->w;
  92.     cut.h = (text->h / (textsize - 1)) <= 1 ? 1 : (text->h / (textsize - 1));
  93.  
  94.     for (i = 0; i < textsize; i++)
  95.     {
  96.         _sinus->degree = _sinus->degree >= 360 ? _sinus->degree - 360 : _sinus->degree;
  97.         pos.x += sinf(_sinus->degree * M_PI / 180.0) * _sinus->amplitude;
  98.  
  99.         SDL_BlitSurface(text, &cut, _source, &pos);
  100.  
  101.         _sinus->degree += _sinus->freq;
  102.         pos.x = _x;
  103.         pos.y += cut.h;
  104.         cut.y += cut.h;
  105.     }
  106.  
  107.     SDL_FreeSurface(text);
  108. }
  109.  
  110. int main ( int argc, char** argv )
  111. {
  112.     if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  113.     {
  114.         printf( "Unable to init SDL: %s\n", SDL_GetError() );
  115.         return 1;
  116.     }
  117.  
  118.     SDL_Surface* screen = SDL_SetVideoMode(480, 272, 16, SDL_HWSURFACE|SDL_DOUBLEBUF);
  119.  
  120.     if (!screen)
  121.     {
  122.         printf("Unable to set 480x272 video: %s\n", SDL_GetError());
  123.         return 1;
  124.     }
  125.  
  126.     TTF_Init();
  127.  
  128.     TTF_Font *police = NULL;
  129.     police = TTF_OpenFont("Arial.ttf", 16);
  130.  
  131.     SDL_Color color = {0xFF, 0, 0};
  132.     SDL_Event event;
  133.  
  134.     int x = 100, y = 272/2 - 20, done = 0, start = 1, delay = 80, A = 0, P = 0;
  135.  
  136.     Sinus sinus = {0.0, 0.0, 10.0};
  137.     char *texte = "Arial Wave Effect SDL Hello World";
  138.  
  139.     while (!done)
  140.     {
  141.         A = SDL_GetTicks();
  142.  
  143.         if (A - P > delay)
  144.         {
  145.             EventControl(&done, &event, &sinus);
  146.             SDL_FillRect(screen, NULL, 0);
  147.             //WaveVText(x, y, &sinus, texte, screen, police, color);
  148.             WaveHText(x, y, &sinus, texte, screen, police, color);
  149.             SDL_Flip(screen);
  150.  
  151.             P = A;
  152.         }
  153.         else
  154.             SDL_Delay(delay - (A - P));
  155.     }
  156.  
  157.     SDL_FreeSurface(screen);
  158.     SDL_Quit();
  159.  
  160.     return 0;
  161. }