Browse Source

Music, main menu buttons

Pabloader 5 years ago
parent
commit
181ea68a53
14 changed files with 140 additions and 20 deletions
  1. 3
    3
      Makefile
  2. 6
    0
      README.md
  3. 2
    2
      include/bonus.h
  4. 29
    0
      include/button.h
  5. 8
    9
      include/debuggers.h
  6. 15
    0
      include/game_state.h
  7. 1
    0
      pge/include/olcPGE_Common.h
  8. BIN
      res/bonuses.png
  9. BIN
      res/click.wav
  10. BIN
      res/hover.wav
  11. BIN
      res/music.wav
  12. 1
    1
      src/bonus.cpp
  13. 36
    0
      src/button.cpp
  14. 39
    5
      src/debuggers.cpp

+ 3
- 3
Makefile View File

@@ -3,7 +3,7 @@ LDFLAGS=
3 3
 INCLUDES=-Iinclude -Ipge/include
4 4
 ifeq ($(OS),Windows_NT)
5 5
 	LIBRARIES=-luser32 -lgdi32 -lopengl32 -lgdiplus -lwinmm
6
-	EXECUTABLE=bin/debuggers.exe
6
+	EXECUTABLE=debuggers.exe
7 7
 	PACKER=packer/packer.exe
8 8
 ifneq ($(DEBUG),1)
9 9
 	LDFLAGS+=--machine=windows
@@ -13,7 +13,7 @@ endif
13 13
 	MAKE=mingw32-make
14 14
 else
15 15
 	LIBRARIES=-lX11 -lGL -lpthread -lpng -lasound
16
-	EXECUTABLE=bin/debuggers
16
+	EXECUTABLE=debuggers
17 17
 	PACKER=packer/packer
18 18
 	CC=gcc
19 19
 	CPP=g++
@@ -30,7 +30,7 @@ OBJECTS=$(SOURCES:.cpp=.o)
30 30
 SPRITES=$(IMAGES:.png=.pgs)
31 31
 RESOURCES=$(SPRITES) $(wildcard res/*.wav) src/programmer.cpp
32 32
 
33
-PACK=bin/debuggers.pgp
33
+PACK=debuggers.pgp
34 34
 
35 35
 all: $(SOURCES) $(PACK) $(EXECUTABLE)
36 36
 

+ 6
- 0
README.md View File

@@ -22,3 +22,9 @@ For Debian/Ubuntu:
22 22
 # Make it!
23 23
 
24 24
 Just run `make` command to build
25
+
26
+# Info
27
+
28
+Music from https://filmmusic.io
29
+"Werq" by Kevin MacLeod (https://incompetech.com)
30
+License: CC BY (http://creativecommons.org/licenses/by/4.0/)

+ 2
- 2
include/bonus.h View File

@@ -6,9 +6,9 @@
6 6
 namespace pabloader {
7 7
 
8 8
 enum BonusType {
9
-    BONUS_DEBUGGER = 0,
9
+    BONUS_DOCUMENTATION = 0,
10 10
     BONUS_UNIT_TEST = 1,
11
-    BONUS_DOCUMENTATION = 2
11
+    BONUS_DEBUGGER = 2
12 12
 };
13 13
 
14 14
 class Bonus : public Entity {

+ 29
- 0
include/button.h View File

@@ -0,0 +1,29 @@
1
+#ifndef BUTTON_H
2
+#define BUTTON_H
3
+
4
+#include "olcPixelGameEngine.h"
5
+#include "game_state.h"
6
+
7
+#include <string>
8
+
9
+namespace pabloader {
10
+class Debuggers;
11
+typedef void (*ButtonAction)();
12
+class Button {
13
+public:
14
+    Button(Debuggers* game, int32_t x, int32_t y, int32_t w, int32_t h, std::string txt, GameState state);
15
+    void Draw();
16
+    void Update();
17
+    void SetText(std::string txt) { text = txt; }
18
+
19
+private:
20
+    Debuggers* game;
21
+    olc::vi2d pos;
22
+    olc::vi2d size;
23
+    std::string text;
24
+    GameState state;
25
+    bool hover = false;
26
+};
27
+}
28
+
29
+#endif

+ 8
- 9
include/debuggers.h View File

@@ -5,9 +5,11 @@
5 5
 #include "olcPixelGameEngine.h"
6 6
 
7 7
 #include "bonus.h"
8
+#include "button.h"
8 9
 #include "bug.h"
9 10
 #include "player.h"
10 11
 #include "programmer.h"
12
+#include "game_state.h"
11 13
 
12 14
 #include <chrono>
13 15
 #include <list>
@@ -17,19 +19,12 @@
17 19
 #include <vector>
18 20
 
19 21
 namespace pabloader {
20
-enum GameState {
21
-    STATE_MAIN_MENU,
22
-    STATE_GAME,
23
-    STATE_PAUSE_MENU,
24
-    STATE_GAME_OVER,
25
-    STATE_HELP,
26
-    STATE_QUIT
27
-};
28 22
 
29 23
 class Debuggers : public olc::PixelGameEngine {
30 24
 private:
31 25
     std::list<Programmer*> programmers;
32 26
     std::list<Bonus*> bonuses;
27
+    std::list<Button*> buttons;
33 28
 
34 29
     std::map<BonusType, uint32_t> bonusesCatched;
35 30
 
@@ -59,13 +54,13 @@ public:
59 54
     bool OnUserDestroy() override;
60 55
     int GameScreenHeight() { return ScreenHeight() - linesOfCode * 10; }
61 56
     GameState GetState() { return state; }
57
+    void SwitchState(GameState newState);
62 58
 
63 59
     template <typename T>
64 60
     T GetRandom(T from = 0, T to = 1);
65 61
 
66 62
 private:
67 63
     void ResetGame();
68
-    void SwitchState(GameState newState);
69 64
     void DrawMenu();
70 65
 
71 66
     void SpawnPlayer();
@@ -92,9 +87,13 @@ public:
92 87
     olc::Sprite bonusesSprite;
93 88
     olc::Sprite backgroundSprite;
94 89
 
90
+    int bgMusic;
91
+
95 92
     int playerFallSample;
96 93
     int bugFallSample;
97 94
     int bugCatchSample;
95
+    int hoverSample;
96
+    int clickSample;
98 97
 
99 98
     const int gravity = 25;
100 99
 #ifdef _DEBUG

+ 15
- 0
include/game_state.h View File

@@ -0,0 +1,15 @@
1
+#ifndef GAME_STATE_H
2
+#define GAME_STATE_H
3
+
4
+namespace pabloader {
5
+enum GameState {
6
+    STATE_MAIN_MENU,
7
+    STATE_GAME,
8
+    STATE_PAUSE_MENU,
9
+    STATE_GAME_OVER,
10
+    STATE_HELP,
11
+    STATE_QUIT
12
+};
13
+}
14
+
15
+#endif

+ 1
- 0
pge/include/olcPGE_Common.h View File

@@ -25,6 +25,7 @@
25 25
 #endif
26 26
 
27 27
 #include <string>
28
+#include <cmath>
28 29
 
29 30
 namespace olc {
30 31
     enum rcode {

BIN
res/bonuses.png View File


BIN
res/click.wav View File


BIN
res/hover.wav View File


BIN
res/music.wav View File


+ 1
- 1
src/bonus.cpp View File

@@ -7,7 +7,7 @@ namespace pabloader {
7 7
 Bonus::Bonus(Debuggers* game)
8 8
     : Entity(game, &game->bonusesSprite)
9 9
 {
10
-    tileDim = { 1, 3 };
10
+    tileDim = { 1, 1 };
11 11
 #ifdef _DEBUG
12 12
     DEBUG_COLOR = olc::YELLOW;
13 13
 #endif

+ 36
- 0
src/button.cpp View File

@@ -0,0 +1,36 @@
1
+#include "button.h"
2
+#include "debuggers.h"
3
+
4
+namespace pabloader {
5
+Button::Button(Debuggers* game, int32_t x, int32_t y, int32_t w, int32_t h, std::string text, GameState state)
6
+    : game(game)
7
+    , pos(x, y)
8
+    , size(w, h)
9
+    , text(text)
10
+    , state(state)
11
+{
12
+}
13
+
14
+void Button::Update()
15
+{
16
+    auto x = game->GetMouseX();
17
+    auto y = game->GetMouseY();
18
+    auto newHover = x >= pos.x && x <= pos.x + size.x && y >= pos.y && y <= pos.y + size.y;
19
+    if (!hover && newHover) {
20
+        olc::SOUND::PlaySample(game->hoverSample);
21
+    }
22
+    hover = newHover;
23
+    if (hover && game->GetMouse(0).bPressed) {
24
+        olc::SOUND::PlaySample(game->clickSample);
25
+        game->SwitchState(state);
26
+    }
27
+}
28
+
29
+void Button::Draw()
30
+{
31
+    auto sw = 8 * text.size();
32
+    auto sh = 8;
33
+    game->DrawRect(pos.x, pos.y, size.x, size.y, hover ? olc::WHITE : olc::GREY);
34
+    game->DrawString(pos.x + (size.x - sw) / 2, pos.y + (size.y - sh) / 2, text);
35
+}
36
+}

+ 39
- 5
src/debuggers.cpp View File

@@ -42,7 +42,7 @@ bool Debuggers::OnUserCreate()
42 42
     }
43 43
 
44 44
     if (!pack.LoadPack("debuggers.pgp")) {
45
-        std::cerr << "Cannot load resource pack res/debuggers.pgp" << std::endl;
45
+        std::cerr << "Cannot load resource pack debuggers.pgp" << std::endl;
46 46
         return false;
47 47
     }
48 48
 
@@ -112,6 +112,27 @@ bool Debuggers::OnUserCreate()
112 112
             originalSourceCode.push_back(line);
113 113
     }
114 114
 
115
+    fileName = "res/hover.wav";
116
+    hoverSample = olc::SOUND::LoadAudioSample(fileName, &pack);
117
+    if (hoverSample < 0) {
118
+        std::cerr << "[Load hover] File " << fileName << " is not found" << std::endl;
119
+        return false;
120
+    }
121
+
122
+    fileName = "res/click.wav";
123
+    clickSample = olc::SOUND::LoadAudioSample(fileName, &pack);
124
+    if (clickSample < 0) {
125
+        std::cerr << "[Load click] File " << fileName << " is not found" << std::endl;
126
+        return false;
127
+    }
128
+
129
+    fileName = "res/music.wav";
130
+    bgMusic = olc::SOUND::LoadAudioSample(fileName, &pack);
131
+    if (bgMusic < 0) {
132
+        std::cerr << "[Load music] File " << fileName << " is not found" << std::endl;
133
+        return false;
134
+    }
135
+
115 136
     totalLines = originalSourceCode.size();
116 137
 
117 138
     // Free Memory
@@ -119,8 +140,13 @@ bool Debuggers::OnUserCreate()
119 140
 
120 141
     // Creating entities
121 142
 
143
+    buttons.push_back(new Button(this, ScreenWidth() / 2 - 64, 100, 128, 16, "Start", STATE_GAME));
144
+    buttons.push_back(new Button(this, ScreenWidth() / 2 - 64, 132, 128, 16, "Help", STATE_HELP));
145
+    buttons.push_back(new Button(this, ScreenWidth() / 2 - 64, 164, 128, 16, "Quit", STATE_QUIT));
146
+
122 147
     ResetGame();
123 148
     SwitchState(STATE_MAIN_MENU);
149
+    olc::SOUND::PlaySample(bgMusic, true);
124 150
 
125 151
     return true;
126 152
 }
@@ -166,11 +192,19 @@ void Debuggers::DrawMenu()
166 192
     DrawString(ScreenWidth() - 128, 64, "by Pabloader");
167 193
     DrawString(ScreenWidth() - 17 * 8 - 2, ScreenHeight() - 10, "OLC Code Jam 2019");
168 194
 
169
-    std::string resumeText = state == STATE_PAUSE_MENU ? "continue" : "start";
195
+    std::string resumeText = state == STATE_PAUSE_MENU ? "Continue" : "Start";
196
+    if (buttons.size() > 0) {
197
+        buttons.front()->SetText(resumeText);
198
+    }
199
+
200
+    // DrawString(ScreenWidth() / 2 - 144, 100, "SPACE to " + resumeText + " game");
201
+    // DrawString(ScreenWidth() / 2 - 144, 116, "H to view help");
202
+    // DrawString(ScreenWidth() / 2 - 144, 132, "X to quit");
170 203
 
171
-    DrawString(ScreenWidth() / 2 - 144, 100, "SPACE to " + resumeText + " game");
172
-    DrawString(ScreenWidth() / 2 - 144, 116, "H to view help");
173
-    DrawString(ScreenWidth() / 2 - 144, 132, "X to quit");
204
+    for (auto& button : buttons) {
205
+        button->Update();
206
+        button->Draw();
207
+    }
174 208
 
175 209
     if (GetKey(olc::SPACE).bPressed || (GetKey(olc::ESCAPE).bPressed && state == STATE_PAUSE_MENU)) {
176 210
         SwitchState(STATE_GAME);

Loading…
Cancel
Save