Browse Source

Add sound

Pabloader 5 years ago
parent
commit
9f324f35e8
7 changed files with 42 additions and 11 deletions
  1. 4
    0
      include/awoorwae.h
  2. 4
    1
      include/player.h
  3. 1
    1
      pge/include/olcPGEX_Sound.h
  4. 1
    1
      pge/src/olcPGEX_Sound.cpp
  5. BIN
      res/fall.wav
  6. 13
    2
      src/awoorwae.cpp
  7. 19
    6
      src/player.cpp

+ 4
- 0
include/awoorwae.h View File

2
 #define AWOORWAE_H
2
 #define AWOORWAE_H
3
 
3
 
4
 #include "olcPixelGameEngine.h"
4
 #include "olcPixelGameEngine.h"
5
+#include "olcPGEX_Sound.h"
5
 #include "player.h"
6
 #include "player.h"
6
 
7
 
7
 namespace pabloader {
8
 namespace pabloader {
24
 public:
25
 public:
25
     bool OnUserCreate() override;
26
     bool OnUserCreate() override;
26
     bool OnUserUpdate(float fElapsedTime) override;
27
     bool OnUserUpdate(float fElapsedTime) override;
28
+    bool OnUserDestroy() override;
29
+
30
+public:
27
     olc::ResourcePack pack;
31
     olc::ResourcePack pack;
28
 };
32
 };
29
 }
33
 }

+ 4
- 1
include/player.h View File

3
 
3
 
4
 #include "entity.h"
4
 #include "entity.h"
5
 #include "olcPGE_Common.h"
5
 #include "olcPGE_Common.h"
6
+#include "olcPGEX_Sound.h"
6
 
7
 
7
 namespace pabloader {
8
 namespace pabloader {
8
 
9
 
12
     olc::rcode Load() override;
13
     olc::rcode Load() override;
13
     void Update(float dt) override;
14
     void Update(float dt) override;
14
     void Draw() override;
15
     void Draw() override;
15
-};
16
 
16
 
17
+private:
18
+    int fallSample;
19
+};
17
 }
20
 }
18
 
21
 
19
 #endif
22
 #endif

+ 1
- 1
pge/include/olcPGEX_Sound.h View File

168
 
168
 
169
 	private:
169
 	private:
170
 #ifdef USE_WINDOWS // Windows specific sound management
170
 #ifdef USE_WINDOWS // Windows specific sound management
171
-		static void CALLBACK waveOutProc(HWAVEOUT hWaveOut, UINT uMsg, DWORD dwParam1, DWORD dwParam2);
171
+		static void CALLBACK waveOutProc(HWAVEOUT hWaveOut, UINT uMsg, DWORD_PTR dwInstance, DWORD dwParam1, DWORD dwParam2);
172
 		static unsigned int m_nSampleRate;
172
 		static unsigned int m_nSampleRate;
173
 		static unsigned int m_nChannels;
173
 		static unsigned int m_nChannels;
174
 		static unsigned int m_nBlockCount;
174
 		static unsigned int m_nBlockCount;

+ 1
- 1
pge/src/olcPGEX_Sound.cpp View File

286
 	}
286
 	}
287
 
287
 
288
 	// Handler for soundcard request for more data
288
 	// Handler for soundcard request for more data
289
-	void CALLBACK SOUND::waveOutProc(HWAVEOUT hWaveOut, UINT uMsg, DWORD dwParam1, DWORD dwParam2)
289
+	void CALLBACK SOUND::waveOutProc(HWAVEOUT hWaveOut, UINT uMsg, DWORD_PTR dwInstance, DWORD dwParam1, DWORD dwParam2)
290
 	{
290
 	{
291
 		if (uMsg != WOM_DONE) return;
291
 		if (uMsg != WOM_DONE) return;
292
 		m_nBlockFree++;
292
 		m_nBlockFree++;

BIN
res/fall.wav View File


+ 13
- 2
src/awoorwae.cpp View File

1
 #include "awoorwae.h"
1
 #include "awoorwae.h"
2
 #include <cmath>
2
 #include <cmath>
3
-#include <string>
4
 #include <iostream>
3
 #include <iostream>
4
+#include <string>
5
 
5
 
6
 namespace pabloader {
6
 namespace pabloader {
7
 
7
 
8
 bool Awoorwae::OnUserCreate()
8
 bool Awoorwae::OnUserCreate()
9
 {
9
 {
10
+    if (!olc::SOUND::InitialiseAudio()) {
11
+        std::cerr << "Cannot init audio" << std::endl;
12
+        return false;
13
+    }
14
+
10
     if (!pack.LoadPack("awoorwae.pgp")) {
15
     if (!pack.LoadPack("awoorwae.pgp")) {
11
         std::cerr << "Cannot load resource pack res/awoorwae.pgp" << std::endl;
16
         std::cerr << "Cannot load resource pack res/awoorwae.pgp" << std::endl;
12
         return false;
17
         return false;
13
     }
18
     }
19
+
14
     if (!player->Load())
20
     if (!player->Load())
15
         return false;
21
         return false;
16
 
22
 
17
     SetPixelMode(olc::Pixel::MASK);
23
     SetPixelMode(olc::Pixel::MASK);
18
-
19
     pack.ClearPack();
24
     pack.ClearPack();
20
     return true;
25
     return true;
21
 }
26
 }
28
     player->Draw();
33
     player->Draw();
29
     return true;
34
     return true;
30
 }
35
 }
36
+
37
+bool Awoorwae::OnUserDestroy()
38
+{
39
+    olc::SOUND::DestroyAudio();
40
+    return true;
41
+}
31
 }
42
 }

+ 19
- 6
src/player.cpp View File

10
 
10
 
11
 olc::rcode Player::Load()
11
 olc::rcode Player::Load()
12
 {
12
 {
13
-    const std::string fileName = "res/player.pgs";
14
-    olc::rcode result = img.LoadFromPGESprFile(fileName, &game->pack);
15
-    if (result) {
13
+    const std::string spriteFile = "res/player.pgs";
14
+    const std::string fallFile = "res/fall.wav";
15
+    if (img.LoadFromPGESprFile(spriteFile, &game->pack)) {
16
         w = img.width / 4;
16
         w = img.width / 4;
17
         h = img.height / 4;
17
         h = img.height / 4;
18
         x = (game->ScreenWidth() - w) / 2;
18
         x = (game->ScreenWidth() - w) / 2;
19
         y = game->ScreenHeight() / 2;
19
         y = game->ScreenHeight() / 2;
20
     } else {
20
     } else {
21
-        std::cerr << "File " << fileName << " is not found" << std::endl;
21
+        std::cerr << "File " << spriteFile << " is not found" << std::endl;
22
+        return olc::FAIL;
22
     }
23
     }
23
-    return result;
24
+    fallSample = olc::SOUND::LoadAudioSample(fallFile, &game->pack);
25
+    if (fallSample < 0) {
26
+        std::cerr << "File " << fallFile << " is not found" << std::endl;
27
+        return olc::FAIL;
28
+    }
29
+
30
+    return olc::OK;
24
 }
31
 }
25
 
32
 
26
 void Player::Update(float dt)
33
 void Player::Update(float dt)
69
     if (x > game->ScreenWidth() - w / 2) {
76
     if (x > game->ScreenWidth() - w / 2) {
70
         xv *= -0.3;
77
         xv *= -0.3;
71
         x = game->ScreenWidth() - w / 2;
78
         x = game->ScreenWidth() - w / 2;
79
+        olc::SOUND::PlaySample(fallSample);
72
     }
80
     }
73
 
81
 
74
     if (x < w / 2) {
82
     if (x < w / 2) {
75
         xv *= -0.3;
83
         xv *= -0.3;
76
         x = w / 2;
84
         x = w / 2;
85
+        olc::SOUND::PlaySample(fallSample);
77
     }
86
     }
78
 
87
 
79
     if (y > game->ScreenHeight() - h / 2) {
88
     if (y > game->ScreenHeight() - h / 2) {
85
         y = game->ScreenHeight() - h / 2;
94
         y = game->ScreenHeight() - h / 2;
86
     }
95
     }
87
 
96
 
88
-    onGround = y >= game->ScreenHeight() - h / 2;
97
+    bool newOnGround = y >= game->ScreenHeight() - h / 2;
98
+    if (newOnGround && !onGround) {
99
+        olc::SOUND::PlaySample(fallSample);
100
+    }
101
+    onGround = newOnGround;
89
 
102
 
90
     if (onGround) {
103
     if (onGround) {
91
         tile = 2;
104
         tile = 2;

Loading…
Cancel
Save