#include "animation.hpp"
Animation :: Animation (){
window = std :: make_unique < sf :: RenderWindow > (
sf :: VideoMode ( 1280 , 720 ),
"Animation :: C++ vs JS" ,
sf :: Style :: Titlebar | sf :: Style :: Close
);
background ();
time = 0. f ;
points . resize ( numpoints );
for ( size_t i {}; i < numpoints ; ++ i ){
points [ i ]. shape = sf :: CircleShape ( 3 );
points [ i ]. phase = i * 0.3 ;
}
font . loadFromFile ( "./font.ttf" );
h1 . setFont ( font );
h2 = h1 ;
h1 . setString ( "Start Game" );
h1 . setCharacterSize ( 30 );
h1 . setPosition ( window -> getSize (). x / 2. f - 150. f , window -> getSize (). y / 2. f );
h2 . setString ( "Play" );
h2 . setCharacterSize ( 18 );
h2 . setPosition ( window -> getSize (). x / 2. f - 50. f , window -> getSize (). y / 2. f + 50. f );
}
void Animation :: background (){
gradient . setPrimitiveType ( sf :: TriangleFan );
gradient . append (
sf :: Vertex (
sf :: Vector2f (
window -> getSize (). x / 2. f ,
window -> getSize (). y / 2. f ),
sf :: Color ( 63 , 94 , 251 )
)
);
for ( int angle {}; angle <= 360 ; angle += 5 ){
float rad = angle * 3.14159 f / 180. f ;
float x = window -> getSize (). x / 2. f + cos ( rad ) * window -> getSize (). x ;
float y = window -> getSize (). y / 2. f + sin ( rad ) * window -> getSize (). y ;
gradient . append ( sf :: Vertex ( sf :: Vector2f ( x , y ), sf :: Color ( 252 , 70 , 107 )));
}
}
void Animation :: events (){
sf :: Event event ;
while ( window -> pollEvent ( event )){
if ( event . type == sf :: Event :: Closed ){
window -> close ();
}
}
}
void Animation :: run (){
while ( window -> isOpen ()){
events ();
time = clock . getElapsedTime (). asSeconds () * 2.3 f ;
draw ();
}
}
void Animation :: draw (){
window -> clear ();
window -> draw ( gradient );
update_points ();
for ( const auto & var : points ){
window -> draw ( var . shape );
}
window -> draw ( h1 );
window -> draw ( h2 );
window -> display ();
}
void Animation :: update_points (){
for ( int i {}; i < numpoints ; ++ i ){
float x = std :: sin ( time + i * 0.3 f ) * 200 + window -> getSize (). x / 2. f ;
float y = std :: cos ( time + i * 0.5 f ) * 200 + window -> getSize (). y / 2. f ;
points [ i ]. shape . setPosition ( x , y );
float alpha = std :: sin ( i * 0.1 f ) * 0.5 f + 0.5 f ;
points [ i ]. shape . setFillColor ( sf :: Color ( 255 , 255 , 255 , static_cast < sf :: Uint8 > ( alpha * 255 )));
}
}