瀏覽代碼

Add sound

Pabloader 5 年之前
父節點
當前提交
9f324f35e8
共有 7 個文件被更改,包括 42 次插入11 次删除
  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. 二進制
      res/fall.wav
  6. 13
    2
      src/awoorwae.cpp
  7. 19
    6
      src/player.cpp

+ 4
- 0
include/awoorwae.h 查看文件

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

+ 4
- 1
include/player.h 查看文件

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

+ 1
- 1
pge/include/olcPGEX_Sound.h 查看文件

@@ -168,7 +168,7 @@ namespace olc
168 168
 
169 169
 	private:
170 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 172
 		static unsigned int m_nSampleRate;
173 173
 		static unsigned int m_nChannels;
174 174
 		static unsigned int m_nBlockCount;

+ 1
- 1
pge/src/olcPGEX_Sound.cpp 查看文件

@@ -286,7 +286,7 @@ namespace olc
286 286
 	}
287 287
 
288 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 291
 		if (uMsg != WOM_DONE) return;
292 292
 		m_nBlockFree++;

二進制
res/fall.wav 查看文件


+ 13
- 2
src/awoorwae.cpp 查看文件

@@ -1,21 +1,26 @@
1 1
 #include "awoorwae.h"
2 2
 #include <cmath>
3
-#include <string>
4 3
 #include <iostream>
4
+#include <string>
5 5
 
6 6
 namespace pabloader {
7 7
 
8 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 15
     if (!pack.LoadPack("awoorwae.pgp")) {
11 16
         std::cerr << "Cannot load resource pack res/awoorwae.pgp" << std::endl;
12 17
         return false;
13 18
     }
19
+
14 20
     if (!player->Load())
15 21
         return false;
16 22
 
17 23
     SetPixelMode(olc::Pixel::MASK);
18
-
19 24
     pack.ClearPack();
20 25
     return true;
21 26
 }
@@ -28,4 +33,10 @@ bool Awoorwae::OnUserUpdate(float fElapsedTime)
28 33
     player->Draw();
29 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 查看文件

@@ -10,17 +10,24 @@ Player::Player(Awoorwae* game_)
10 10
 
11 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 16
         w = img.width / 4;
17 17
         h = img.height / 4;
18 18
         x = (game->ScreenWidth() - w) / 2;
19 19
         y = game->ScreenHeight() / 2;
20 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 33
 void Player::Update(float dt)
@@ -69,11 +76,13 @@ void Player::Update(float dt)
69 76
     if (x > game->ScreenWidth() - w / 2) {
70 77
         xv *= -0.3;
71 78
         x = game->ScreenWidth() - w / 2;
79
+        olc::SOUND::PlaySample(fallSample);
72 80
     }
73 81
 
74 82
     if (x < w / 2) {
75 83
         xv *= -0.3;
76 84
         x = w / 2;
85
+        olc::SOUND::PlaySample(fallSample);
77 86
     }
78 87
 
79 88
     if (y > game->ScreenHeight() - h / 2) {
@@ -85,7 +94,11 @@ void Player::Update(float dt)
85 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 103
     if (onGround) {
91 104
         tile = 2;

Loading…
取消
儲存