//#include "iostream"
#include <SDL/SDL.h>
#include "math.h"
//using namespace std;
float ralign=360.0;// For the rotation function, changes starting position to calculate from.
void rotate(float xp,float yp,float& ux,float& uy,float degrees){float bt,lx,ly,rx,ry,nnx,a;
if(uy<=yp&&ux>=xp){a=atan((yp-uy)/((ux-xp)))*57.295779579;lx=ux-xp;ly=yp-uy;}if(uy<=yp&&xp>ux)
{a=90.0+(90.0-acos((xp-ux)/(sqrt(((yp-uy)*(yp-uy))+((xp-ux)*(xp-ux)))))*57.295779579);lx=xp-ux;ly=yp-uy;}
if(yp<uy&&xp>ux){a=270.0-(90.0-(atan((uy-yp)/((xp-ux)))*57.295779579));lx=xp-ux;ly=uy-yp;}if(yp<uy&&ux>=xp)
{a=360.0-(acos((ux-xp)/(sqrt(((uy-yp)*(uy-yp))+((ux-xp)*(ux-xp)))))*57.295779579);lx=ux-xp;ly=uy-yp;}
a=((ralign-a)+degrees)+45.0;rx=cos(a*0.017453292)+sin(a*0.017453292);ry=-sin(a*0.017453292)+cos(a*0.017453292);
nnx=sqrt((rx*rx)+(ry*ry));bt=sqrt((lx*lx)+(ly*ly));rx/=nnx;ux=xp+(rx*bt);ry/=nnx;uy=yp-(ry*bt);}
int main(int argc,char *argv[]){
unsigned short screen_width=640,screen_height=480,colorBPP=16;
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);
SDL_Surface* dis=SDL_SetVideoMode(screen_width,screen_height,colorBPP,SDL_SWSURFACE);
SDL_Event game;SDL_Rect temp;Uint8* key;int quit=0;
int ct=0,ot=0,fr=16;// FPS limiter,(current time, old time, frame rate) 1000(TICKS (1000 per second))/16 = 62,5 fps
float xpos,zpos,xv=0.0,xv2=0.0,xv3;
float enemyX=420.0,enemyY;
float time=0.0,rpx,rpy,px0,px1,px2,py0,py1,py2;
while (quit==0){while (SDL_PollEvent(&game)){//********************* START OF GAME LOOP
if(game.type==SDL_QUIT){quit=1;}}
key=SDL_GetKeyState(NULL);
if(key[SDLK_ESCAPE]){quit=1;}
if(key[SDLK_x]){time+=0.01;if(time>1.0){time=1.0;}}
if(key[SDLK_z]){time-=0.01;if(time<0.0){time=0.0;}}
if(key[SDLK_w]){xv=0.0f;}
if(key[SDLK_q]){xv-=1.0f;while(xv<0.0f){xv+=360.0f;}}
if(key[SDLK_e]){xv+=1.0f;while(xv>=360.0f){xv-=360.0f;}}
if(key[SDLK_UP]){enemyX=420.0f;}
if(key[SDLK_DOWN]){enemyX=420.0f;}
if(key[SDLK_LEFT]){enemyX-=1.5f;}
if(key[SDLK_RIGHT]){enemyX+=1.5f;}
xpos=320;zpos=40;
rotate(screen_width/2,screen_height/2,xpos,zpos,xv);
xv2+=0.05;// SPEED OF MOVING UP / DOWN
xv3=100.0;// HEIGHT DIFFERENCE VALUE
//enemyX=420;
enemyY=240+(sin(xv2)*xv3);
// BEZIER CURVE
px0=140;py0=440;
px1=80;py1=360;
px2=240;py2=420;
rpx=px0;rpy=py0;
rpx+=((px2-px1)*time)*time;
rpx-=((px1-px0)*(1.0-time))*(1.0-time);
rpy-=((py1-py0)*(1.0-time))*(1.0-time);
rpy+=((py2-py1)*(time))*(time);
rpx+=px1-px0;rpy-=py0-py1;
temp.x=rpx;temp.y=rpy;temp.w=1;temp.h=1;
SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,250,0,0));
temp.x=px0;temp.y=py0;temp.w=4;temp.h=4;
SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,0,250,90));
temp.x=px1;temp.y=py1;temp.w=4;temp.h=4;
SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,200,250,0));
temp.x=px2;temp.y=py2;temp.w=4;temp.h=4;
SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,200,250,200));
// ********* R E N D E R I N G *******************
temp.x=xpos;temp.y=zpos;temp.w=1;temp.h=1;
SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,250,250,250));
temp.x=screen_width/2;temp.y=40;temp.w=1;temp.h=200;
SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,250,0,250));
temp.x=screen_width/2;temp.y=240;temp.w=200;temp.h=1;
SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,250,0,250));
temp.x=enemyX-20;temp.y=enemyY-20;temp.w=40;temp.h=40;
SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,250,0,0));
SDL_Flip(dis);
// CLEAN THE SCREEN (blank out the drawing to see the circle and bezier lines form permanently)
temp.x=0;temp.y=0;temp.w=screen_width/2;temp.h=screen_height;
SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,0,0,25));// Blank this one
temp.x=screen_width/2;temp.y=0;temp.w=screen_width/2;temp.h=screen_height;
SDL_FillRect(dis,&temp,SDL_MapRGB(dis->format,25,0,0));// And blank this one to
// LIMIT FRAMERATE
ct=SDL_GetTicks();if (ct-ot<fr){SDL_Delay(fr-(ct-ot));}ot=SDL_GetTicks();
}// **************************************************** END OF GAME LOOP
SDL_Quit();
return 0;
}