Procházet zdrojové kódy

Music, main menu buttons

Pabloader před 4 roky
rodič
revize
181ea68a53
14 změnil soubory, kde provedl 140 přidání a 20 odebrání
  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ární
      res/bonuses.png
  9. binární
      res/click.wav
  10. binární
      res/hover.wav
  11. binární
      res/music.wav
  12. 1
    1
      src/bonus.cpp
  13. 36
    0
      src/button.cpp
  14. 39
    5
      src/debuggers.cpp

+ 3
- 3
Makefile Zobrazit soubor

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

+ 6
- 0
README.md Zobrazit soubor

22
 # Make it!
22
 # Make it!
23
 
23
 
24
 Just run `make` command to build
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 Zobrazit soubor

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

+ 29
- 0
include/button.h Zobrazit soubor

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 Zobrazit soubor

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

+ 15
- 0
include/game_state.h Zobrazit soubor

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 Zobrazit soubor

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

binární
res/bonuses.png Zobrazit soubor


binární
res/click.wav Zobrazit soubor


binární
res/hover.wav Zobrazit soubor


binární
res/music.wav Zobrazit soubor


+ 1
- 1
src/bonus.cpp Zobrazit soubor

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

+ 36
- 0
src/button.cpp Zobrazit soubor

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 Zobrazit soubor

42
     }
42
     }
43
 
43
 
44
     if (!pack.LoadPack("debuggers.pgp")) {
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
         return false;
46
         return false;
47
     }
47
     }
48
 
48
 
112
             originalSourceCode.push_back(line);
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
     totalLines = originalSourceCode.size();
136
     totalLines = originalSourceCode.size();
116
 
137
 
117
     // Free Memory
138
     // Free Memory
119
 
140
 
120
     // Creating entities
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
     ResetGame();
147
     ResetGame();
123
     SwitchState(STATE_MAIN_MENU);
148
     SwitchState(STATE_MAIN_MENU);
149
+    olc::SOUND::PlaySample(bgMusic, true);
124
 
150
 
125
     return true;
151
     return true;
126
 }
152
 }
166
     DrawString(ScreenWidth() - 128, 64, "by Pabloader");
192
     DrawString(ScreenWidth() - 128, 64, "by Pabloader");
167
     DrawString(ScreenWidth() - 17 * 8 - 2, ScreenHeight() - 10, "OLC Code Jam 2019");
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
     if (GetKey(olc::SPACE).bPressed || (GetKey(olc::ESCAPE).bPressed && state == STATE_PAUSE_MENU)) {
209
     if (GetKey(olc::SPACE).bPressed || (GetKey(olc::ESCAPE).bPressed && state == STATE_PAUSE_MENU)) {
176
         SwitchState(STATE_GAME);
210
         SwitchState(STATE_GAME);

Loading…
Zrušit
Uložit