Petit piano virtuel jouable, écrit en C ++ - (Poo, AN I, Sem. II)
Veuillez installer Zenity (boîte de dialogue) et SFML (GUI):
sudo apt install zenity libsfml-dev Ensuite, dans le dossier Root Project, exécutez make pour compiler le code.
Exécutez en utilisant make run .
Cerințe obligatorii:
? Interfața grafica (în sdl, sfml, qt, etc) Cu api pentru c ++ ☑️
? Rtti ☑️
piano.cpp
if ( dynamic_cast <WhiteKey *>(key)) {
key-> setPosition (whiteX, Y);
whiteX += 48 ;
}? Cours abstraits ☑️
Key.h
class Key {
// ...
virtual sf::Sprite& getSprite () = 0;
virtual void playSound () = 0;
virtual void setPosition ( int x, int y) = 0;
};? Operatori (Minim 4 * numărul oamenilor din echipa) ☑️
Settings.cpp
// Populates json with default settings, if empty :)
Settings& Settings:: operator ++( int ) {
if (std::ifstream f ( " config.txt " ); f. peek () == std::ifstream::traits_type::eof ())
generateConfig ();
return * this ;
}Piano.cpp
// Shows piano data.
std::ostream& operator <<(std::ostream& out, std::shared_ptr<Piano> piano) {
out << " Pian cu " << piano-> getKeys (). size () << " clape. n " ;
for ( auto &i : piano-> getKeys ()) {
// ...
}
return out;
}Piano.cpp
// Operator care acceseaza direct elementele vectorului vector<Key>piano, din clasa Piano
std::shared_ptr<Key> Piano:: operator []( int i) {
return piano[i];
}Key.cpp
// operator care returneaza id-ul notei.
int & Key:: operator [](std::string s)
{
if (s == " noteId " )
return noteId;
}? Attribution de la mémoire de tas ☑️
piano.cpp
pianoKeys.push_back(std::shared_ptr<WhiteKey>( new WhiteKey(tKeyWhite, tKeyWhitePressed,i,std::to_string(i)+ " .ogg " )));? Exceptions ☑️
ObjException.h
# include < exception >
class ObjException : public std :: exception
{
const char * what () const throw()
{
return " bad object in piano array " ;
}
};Piano.cpp
try
{
if ( dynamic_cast <WhiteKey *>(key. get ()))
{
// ...
}
else
throw ObjException ();
}
catch (std:: exception &e)
{
std::cerr << e. what ();
}
? STL ☑️
# include < string >
# include < vector >
# include < exception >
# include < unordered_set >
# include < memory >? Expressions de lambda ☑️
MainWindow.cpp
auto getKeyPressed = [=]() {
return piano-> findKeyPressed (settings. getKbKeys ()[ std::to_string (event. key . code )]);
};
// usage
if (event.type == sf::Event::KeyPressed)
{
if (settings. getKbKeys (). contains ( std::to_string (event. key . code )))
{
activeKey = getKeyPressed ();
Recorder::log ( " down " , activeKey-> getId ());
// ...
}
}? Modèles ☑️
MainWindow.cpp - Definition
template < class T >
void MainWindow::checkPressed (T &obj)
{
try
{
if (! dynamic_cast < Button *>(&obj)) // if not button
throw TypeException ();
if (obj. getSprite (). getGlobalBounds (). contains ( mapPixelToCoords ( sf::Mouse::getPosition ((* this )))))
obj. press ( true );
}
catch (std:: exception &e)
{
std::cerr << e. what ();
}
}MainWindow.cpp - Usage
if (activeKey == nullptr )
{
// Button click logic :D
checkPressed<ButtonRecord>(buttonRecord);
checkPressed<ButtonLoad>(buttonLoad);
checkPressed<ButtonPlay>(buttonPlay);
}
else
{
activeKey-> setPressed ( true );
Recorder::log ( " down " , activeKey-> getId ());
}? Poireurs intelligents - Minim 1 TIP / PROIECT ☑️
Piano.cpp
static std::shared_ptr<Piano> instance;
std::vector<std::shared_ptr<Key>> piano;
... and many more!? Modèles de conception - Minim 2 ☑️
/* *
* Piano: Singleton class that holds array of piano keys
* Calculates black/white key positions.
* Holds key textures.
*/
class Piano
{
// ...
public:
static Piano * getInstance ()
{
if (!instance){
instance = new Piano;
std::cout<< " pianul a fost creat " <<std::endl;
}
else {
std::cout<< " pianul a fost invocat " <<std::endl;
}
return instance;
} // Recorder.h - facade that hides all recording logic.
class Recorder {
static json songData;
static sf::Clock clock;
static bool recording;
public:
Recorder ();
static void start ();
static void stop ();
static void save ();
static void log (std::string type, int noteId);
static void play ();
static void load (std::string path);
static bool isRecording ();
};? Caractéristiques de C ++ 17/20 (constexpr, conscient, constinit, expressions de pli, instruction init pour If / Switch, etc.) ☑️
WhiteKey.cpp
// // Init statement for if (C++17) ////
if ( double seconds = clock.getElapsedTime().asMilliseconds(); seconds < 100 )
{
// Atenuare sunet :) onKeyReleased
c = std::lerp ( 100 , 0 , seconds / 100.0 );
// std::cout << seconds << " " << c << std::endl;
sound. setVolume (c);
if (c <= 5.0 )
break ;
} Opțional => bonus:
? Déplacer la sémantique
? Multithreading (La Echipe de 3-4 Oameni)
? Programmation de réseau informatique / socket (La Echipe de 2-4 Oameni)
? Bd