1 / 15

VO2 Laden und Initialisieren der Sounds

VO2 Laden und Initialisieren der Sounds. Wir wollen Sounds in unsere Applikation laden Menü erweitern um den Menüpunkt Sound mit dem Identifier ID_ULTRIS_SOUND Akzellerator hinzufügen Akzelleratortabelle erweitern. Sounds sollen später in einem Array gespeichert werden

tulia
Télécharger la présentation

VO2 Laden und Initialisieren der Sounds

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VO2 Laden und Initialisieren der Sounds

  2. Wir wollen Sounds in unsere Applikation laden • Menü erweitern • um den Menüpunkt Sound • mit dem Identifier ID_ULTRIS_SOUND • Akzellerator hinzufügen • Akzelleratortabelle erweitern

  3. Sounds sollen später in einem Array gespeichert werden • Für den Zugriff auf diesen Array werden Konstanten angelegt const int sound_start = 0; const int sound_dreh = 1; const int sound_move = 2; const int sound_down = 3; const int sound_row1 = 4; const int sound_row2 = 5; const int sound_ende = 6; const int sound_win = 7; const int anzahl_sounds = 8;

  4. char *soundfiles[anzahl_sounds] = { “ul_start.wav“,“ul_dreh.wav“, "ul_move.wav", "ul_down.wav", "ul_row1.wav", "ul_row2.wav", "ul_ende.wav", "ul_win.wav" }; • Das Programm benötigt auch die Namen der Sounds

  5. Die Klasse CSoundManager • Wird in den Microsoft-Datein Dsutil.h und Dsutil.cpp bereitgestellt • Dient zur Organisation von Sounds class CSoundManager { public: HRESULT Initialize(…); HRESULT Create(…); };

  6. Die Klasse CSound • Wird in den Microsoft-Datein Dsutil.h und Dsutil.cpp bereitgestellt • Repräsentiert einen konkreten Sound class CSound { public: HRESULT Play(…); HRESULT Stop(); HRESULT Reset(); BOOL IsSoundPlaying(); };

  7. class sounds { private: CSoundManager smgr; CSound *snd[anzahl_sounds];//Array mit Zeigern //auf die //erforderlichen //Sounds public: int on;//Ein- Ausschalter sounds();//Konstruktor int init(HWND wnd); void play (int snr); ~sounds(); //Destruktor }; • Eine Klasse sounds wird angelegt.

  8. Konstruktor • Im Konstruktor werden alle Zeiger im Array snd mit 0 initialisiert, um einen konsistenten Initialzustand zu erhalten • Jeder Sound wird eingeschalten sounds::sounds() { int i; for (i=0; i<anzahl_sounds; i++) snd[i] = 0; //Zeiger mit 0 initialisiert on = 1; //Sound eingeschaltet }

  9. Die Init-Funktion • Auch Sounds werden einem Fenster zugeordnet, desshalb wird für die Initialisierung ein Fenster als Parameter übergeben. Es handelt sich um das Hauptfenster (ultris_window) int sounds::init(HWND wnd) { HRESULT ret; int i; ret=smgr.Initialize(wnd, DSSCL_PRIORITY, 2, 22050, 16); if (ret<0) return ret; for (i=0;i<anzahl_sounds;i++) { ret=smgr.Create(snd+i, soundfiles[i]); if (ret<0) return ret; } return S_OK; }

  10. smgr.Initialize(wnd, DSSCL_PRIORITY, 2, 22050, 16); • wnd: Das Fenster mit dem der Sound verknüpft wird • DSSCL_PRIORITY: Kooperation der Applikation bei konkurierenden Zugriff auf Soundkarte mit anderen Applikationen wird festgelegt • 2: zwei Primärkanäle (Stereo-Sound) • 22050: Abtastfrequenz von 22,05 kHz • 16: Abtastung (16 Bits pro Sample)

  11. Der Destruktor • Die Funktion Create des Soundmanagers allokiert den benötigten Speicher für die Sounds. Der Destruktor gibt diesen Speicher der Klasse wieder frei sounds::~sounds() { for (int i=0;i<anzahl_sounds; i++) { if(snd[i]) delete snd[i];//Speicher wird freigegeben } }

  12. Die Play-Funktion Void sounds::play(int i) { if(!on) //Prüfen ob Sound ausgeschaltet ist return; if(snd[i]->IsSoundPlaying()) //Prüfen ob Sound bereits //abgespielt wird { snd[i]->Stop(); snd[i]->Reset(); } snd[i]->Play(0,0); }

  13. Es wird eine Instanz der Klasse sounds mit dem Namen ultris_sounds wird angelegt • Um ultris_sounds in die Ablikation einzubetten muss • das Objekt bei Applikationsstart initialisiert werden • der Sound muss bei dem Menüpunkt Sound oder F5 ein- bzw ausgeschalten werden • Der Checkmark des Menüpunktes muss konsistent mit dem Ein-/Ausschalter gehalten werden

  14. int APIENTRY WinMain(…) { … ultris_menu = GetMenu(ultris_window); if (ultris_sounds.init(ultris_window) < 0) { MessageBox(ultris_window, “Fehler beim Initialisieren der sounds“, “Ultris Fehlermeldung“, MB_OK | MB_ICONERROR | MB_SETFORGROUND); return 0; } CheckMenuItem(ultris_menu, ID_ULTRIS_SOUND, ultris_sounds.on ? MF_CHECKED:MF_UNCHECKED); … while (TRUE) { … } }

  15. LRESULT CALLBACK ultris_windowhandler(…) { switch (msg) { case WM_COMMAND: switch(LWORD (wParam)) {… case ID_ULTRIS_SOUND: ultris_sounds.on = !ultris_sounds.on; CheckMenuItem(ultris_menu, ID_ULTRIS_SOUND, ultris_sounds.on ? MF_CHECKED:MF_UNCHECKED); return 0; case IDM_TEST: static int testno=0; ultris_sounds.play(testno % anzahl_sounds); testno++; return 0; } break; … } }

More Related