#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <math.h>
#define SCR_H 272
#define SCR_W 480
#define OFFSET 1
typedef struct
{
float amplitude,
degree,
freq;
}Sinus;
void EventControl(int *_done, SDL_Event *_event, Sinus *_sinus)
{
SDL_PollEvent(_event);
switch(_event->type)
{
case SDL_QUIT:
*_done = 1;
break;
case SDL_KEYDOWN:
switch(_event->key.keysym.sym)
{
case SDLK_ESCAPE:
*_done = 1;
break;
case SDLK_SPACE:
*_done = 1;
break;
case SDLK_UP:
_sinus->amplitude += OFFSET;
_sinus->amplitude = _sinus->amplitude >= (SCR_H/2 - 15) ? (SCR_H/2 - 15) : _sinus->amplitude;
break;
case SDLK_DOWN:
_sinus->amplitude -= OFFSET;
_sinus->amplitude = _sinus->amplitude <= 0 ? 0 : _sinus->amplitude;
break;
case SDLK_RIGHT:
_sinus->freq += OFFSET;
break;
case SDLK_LEFT:
_sinus->freq -= OFFSET;
_sinus->freq = _sinus->freq <= 0 ? 0 : _sinus->freq;
break;
}
break;
default:
*_done = 0;
}
}
void WaveVText(int _x, int _y, Sinus *_sinus, const char* _text, SDL_Surface *_source, TTF_Font *_font, SDL_Color _color)
{
SDL_Rect pos = {_x, _y};
int i = 0, textsize = strlen(_text);
for (i = 0; i < textsize; i++)
{
char help[2] = {0};
help[0] = _text[i];
_sinus->degree = _sinus->degree >= 360 ? _sinus->degree - 360 : _sinus->degree;
pos.y += sinf(_sinus->degree * M_PI / 180.0) * _sinus->amplitude;
SDL_Surface *text = NULL;
text = TTF_RenderText_Blended(_font, help, _color);
SDL_BlitSurface(text, NULL, _source, &pos);
_sinus->degree += _sinus->freq;
pos.x += text->w;
pos.y = _y;
SDL_FreeSurface(text);
}
}
void WaveHText(int _x, int _y, Sinus *_sinus, const char* _text, SDL_Surface *_source, TTF_Font *_font, SDL_Color _color)
{
SDL_Rect pos = {_x, _y}, cut = {0, 0, 0, 0};
int i = 0, textsize = strlen(_text);
SDL_Surface *text = NULL;
text = TTF_RenderText_Blended(_font, _text, _color);
cut.w = text->w;
cut.h = (text->h / (textsize - 1)) <= 1 ? 1 : (text->h / (textsize - 1));
for (i = 0; i < textsize; i++)
{
_sinus->degree = _sinus->degree >= 360 ? _sinus->degree - 360 : _sinus->degree;
pos.x += sinf(_sinus->degree * M_PI / 180.0) * _sinus->amplitude;
SDL_BlitSurface(text, &cut, _source, &pos);
_sinus->degree += _sinus->freq;
pos.x = _x;
pos.y += cut.h;
cut.y += cut.h;
}
SDL_FreeSurface(text);
}
int main ( int argc, char** argv )
{
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
SDL_Surface* screen = SDL_SetVideoMode(480, 272, 16, SDL_HWSURFACE|SDL_DOUBLEBUF);
if (!screen)
{
printf("Unable to set 480x272 video: %s\n", SDL_GetError());
return 1;
}
TTF_Init();
TTF_Font *police = NULL;
police = TTF_OpenFont("Arial.ttf", 16);
SDL_Color color = {0xFF, 0, 0};
SDL_Event event;
int x = 100, y = 272/2 - 20, done = 0, start = 1, delay = 80, A = 0, P = 0;
Sinus sinus = {0.0, 0.0, 10.0};
char *texte = "Arial Wave Effect SDL Hello World";
while (!done)
{
A = SDL_GetTicks();
if (A - P > delay)
{
EventControl(&done, &event, &sinus);
SDL_FillRect(screen, NULL, 0);
//WaveVText(x, y, &sinus, texte, screen, police, color);
WaveHText(x, y, &sinus, texte, screen, police, color);
SDL_Flip(screen);
P = A;
}
else
SDL_Delay(delay - (A - P));
}
SDL_FreeSurface(screen);
SDL_Quit();
return 0;
}