Skip to content

Commit

Permalink
修复了一些bug,添加OSD支持,添加音量叠加层
Browse files Browse the repository at this point in the history
  • Loading branch information
telecomadm1145 committed May 12, 2024
1 parent e11afb9 commit 999fd5b
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 18 deletions.
1 change: 1 addition & 0 deletions cmania/AudioManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class IAudioManager {
virtual IAudioStream* load(std::istream& stream) = 0;
virtual IAudioStream* load(const void* data, size_t size) = 0;
virtual ISample* loadSample(const void* data, size_t size) = 0;
virtual void SetMasterVolume(double) = 0;
virtual std::vector<IAudioDevice*> getAudioDevices() = 0;
virtual IAudioDevice* getCurrent() = 0;
virtual bool isDeviceOpened() const = 0;
Expand Down
7 changes: 3 additions & 4 deletions cmania/BassAudioManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ enum class StreamSystem {
BufferPush
};
enum class PlaybackState {
// Token: 0x040001F0 RID: 496
Stopped,
// Token: 0x040001F1 RID: 497
Playing,
// Token: 0x040001F2 RID: 498
Stalled,
// Token: 0x040001F3 RID: 499
Paused
};
inline float BASS_ChannelGetAttribute(DWORD handle, DWORD attrib) {
Expand Down Expand Up @@ -54,6 +50,9 @@ class BassAudioManager : public IAudioManager {
deviceOpened = false;
}
}
void SetMasterVolume(double f) override {
BASS_SetVolume(f);
}

IAudioManager::IAudioStream* load(std::istream& fileStream) override {
int sid;
Expand Down
1 change: 1 addition & 0 deletions cmania/Crc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ uint32_t GetCrc(const char* data, std::size_t s) {
}
#endif
#ifdef __linux__
// TODO: this is wrong impl which makes different crc value across windows and linux platform
#include "Crc.h"
#include <array>
// 生成CRC查询表
Expand Down
18 changes: 10 additions & 8 deletions cmania/GameBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,19 @@ void GameBuffer::SetPixel(int x, int y, PixelData pd) {
if ((x < Width && y < Height && y > -1 && x > -1) &&
(x <= b_right && y <= b_bottom && y >= b_top && x >= b_left)) {
auto& ref = PixelBuffer[y * Width + x];
if (ref.UcsChar == '\b') {
for (int i = x; i >= 0; i--) {
auto& ref2 = PixelBuffer[y * Width + i];
ref2.UcsChar = ' ';
if (ref2.UcsChar != '\b') {
break;
if (pd.UcsChar != '\0') {
if (ref.UcsChar == '\b') {
for (int i = x; i >= 0; i--) {
auto& ref2 = PixelBuffer[y * Width + i];
ref2.UcsChar = ' ';
if (ref2.UcsChar != '\b') {
break;
}
}
}
if (pd.UcsChar != '\1')
ref.UcsChar = pd.UcsChar;
}
if (pd.UcsChar != '\1')
ref.UcsChar = pd.UcsChar;
ref.Background = Color::Blend(ref.Background, pd.Background);
ref.Foreground = Color::Blend(ref.Foreground, pd.Foreground);
}
Expand Down
4 changes: 4 additions & 0 deletions cmania/MainMenuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "ScreenController.h"
#include "SettingsScreen.h"
#include "SongSelectScreen.h"
//#include "VolumeOverlay.h"

#ifdef _MSC_VER
#define _COMPILER_BANNER "Compiled with MSVC v" QUOTE(_MSC_VER)
Expand All @@ -17,15 +18,18 @@ class MainMenuScreen : public Screen {
virtual void Activate(bool y) override {
if (y) {
is_name_exists = !game->Settings["Name"].GetString().empty();
//parent->AddOverlay(MakeVolumeOverlay());
}
}
virtual void Render(GameBuffer& buf) {
// TODO: make a proper ui for this ?
if (!is_name_exists) {
buf.DrawString("输入您的大名(仅用于录像):", 0, 0, {}, {});
buf.DrawString(input_buf, 0, 1, {}, {});
return;
}
buf.DrawString("Cmania " GIT_LATEST_TAG "\n\n按下 Enter 进入选歌界面\n按下 O 键进行设置\n按下 D 键进入在线谱面下载", 0, 0, {}, {});
// TODO: move it to other places
buf.DrawString(_COMPILER_BANNER "(git-" GIT_COMMIT_HASH "@" GIT_COMMIT_DATE ")\nOriginal game by peppy( https://osu.ppy.sh )\nCopyright 2023-2024 telecomadm1145( https://github.com/telecomadm1145/cmania )", 0, buf.Height - 3, {}, {});
// buf.DrawString("Loading beatmap cache...", 0, 0, {}, {});
// buf.DrawString("正在播放", 0, 0, {}, {});
Expand Down
85 changes: 83 additions & 2 deletions cmania/ScreenController.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
#include "GameBuffer.h"
#include "ConsoleInput.h"

class Overlay {
public:
virtual ~Overlay() = default;
virtual void Render(GameBuffer& buf){};
virtual void Key(KeyEventArgs kea){};
virtual void MouseKey(MouseKeyEventArgs mkea){};
virtual bool HitTest(int x, int y) { return false; };
virtual void Tick(double fromRun){};
};


class Screen {
protected:
class ScreenController* parent = 0;
Expand All @@ -25,27 +36,81 @@ class Screen {
virtual void MouseKey(MouseKeyEventArgs mkea){};
virtual void ProcessEvent(const char* evt, const void* evtargs){};
};
class HotKeyHandler {
public:
virtual ~HotKeyHandler() = default;
/// <summary>
/// 处理按键,返回 true 则拦截事件
/// </summary>
/// <param name="kea">按键事件</param>
/// <returns>返回 true 则拦截事件</returns>
virtual bool Key(KeyEventArgs kea) { return false; };
};

class ScreenController : public GameComponent {
std::vector<Screen*> history;
int history_index = 0;
Screen* current = 0;

std::vector<Overlay*> overlays; // Overlay 列表
Overlay* topOverlay = nullptr; // 顶层 Overlay

std::vector<HotKeyHandler*> hotkeys;

// 通过 Component 继承
virtual void ProcessEvent(const char* evt, const void* evtargs) {
if (strcmp(evt, "navigate") == 0) {
Navigate((Screen*)evtargs);
return;
}
if (current != 0) {
current->ProcessEvent(evt, evtargs);
if (strcmp(evt, "draw") == 0) {
current->Render(*(GameBuffer*)evtargs);
}
if (strcmp(evt, "tick") == 0) {
current->Tick(*(double*)evtargs);
}
if (topOverlay) {
if (strcmp(evt, "draw") == 0) {
topOverlay->Render(*(GameBuffer*)evtargs);
}
if (strcmp(evt, "key") == 0) {
topOverlay->Key(*(KeyEventArgs*)evtargs);
return;
}
if (strcmp(evt, "mousekey") == 0) {
MouseKeyEventArgs& mkea = *(MouseKeyEventArgs*)evtargs;
// 分发鼠标按键事件给 Overlays
bool hitTest = false;
for (auto overlay : overlays) {
if (overlay->HitTest(mkea.X, mkea.Y)) {
overlay->MouseKey(mkea);
hitTest = true;
topOverlay = overlay; // 设置顶层 Overlay
break; // 如果命中 Overlay,则不再继续检测
}
}
// 如果未命中任何 Overlay,则自动关闭所有 Overlay
if (!hitTest && !mkea.Pressed) {
for (auto overlay : overlays) {
delete overlay;
}
overlays.clear();
topOverlay = nullptr;
}
return;
}
}
if (strcmp(evt, "key") == 0) {
current->Key(*(KeyEventArgs*)evtargs);
auto& kea = *(KeyEventArgs*)evtargs;
auto hit = false;
for (auto handler : hotkeys)
if (handler->Key(kea))
hit = true;

if (!hit)
current->Key(kea);
}
if (strcmp(evt, "wheel") == 0) {
current->Wheel(*(WheelEventArgs*)evtargs);
Expand All @@ -59,7 +124,6 @@ class ScreenController : public GameComponent {
if (strcmp(evt, "resize") == 0) {
current->Resize();
}
current->ProcessEvent(evt, evtargs);
}
}

Expand Down Expand Up @@ -110,6 +174,23 @@ class ScreenController : public GameComponent {
if (current)
current->Activate(true);
}

// 添加 Overlay 管理函数
void AddOverlay(Overlay* overlay) {
overlays.push_back(overlay);
topOverlay = overlay;
}

void RemoveOverlay(Overlay* overlay) {
auto it = std::find(overlays.begin(), overlays.end(), overlay);
if (it != overlays.end()) {
overlays.erase(it);
delete overlay;
}
}
void AddKeyHandler(HotKeyHandler* kh) {
hotkeys.push_back(kh);
}
};

inline GameComponent* MakeScreenController() {
Expand Down
38 changes: 38 additions & 0 deletions cmania/VolumeOverlay.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "VolumeOverlay.h"
#include "GameBuffer.h"
#include "Hpet.h"
#include "BassAudioManager.h"
#include "ScreenController.h"

class VolumeOverlay : public Overlay {
double volume = 100;
int w = 0, h = 0;
virtual void Render(GameBuffer& buf) {
w = buf.Width;
h = buf.Height;
auto vol = std::to_string(int(volume));
buf.FillRect(0, 0, buf.Width, buf.Height, { {}, { 150, 20, 20, 20 } });
buf.DrawString(vol, buf.Width - 5, buf.Height - 5, {}, {});
buf.DrawString("ÒôÁ¿", buf.Width - 5, buf.Height - 6, {}, {});
};
virtual void Key(KeyEventArgs kea) {
if (kea.Key == ConsoleKey::UpArrow && kea.Pressed) {
volume += 1;
GetBassAudioManager()->SetMasterVolume(volume / 100);
}
if (kea.Key == ConsoleKey::DownArrow && kea.Pressed) {
volume -= 1;
GetBassAudioManager()->SetMasterVolume(volume / 100);
}
};
virtual void MouseKey(MouseKeyEventArgs mkea){};
virtual bool HitTest(int x, int y) {
return false;
};
virtual void Tick(double fromRun){
};
};

Overlay* MakeVolumeOverlay() {
return new VolumeOverlay();
}
3 changes: 3 additions & 0 deletions cmania/VolumeOverlay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once
#include "ScreenController.h"
Overlay* MakeVolumeOverlay();
4 changes: 1 addition & 3 deletions cmania/cmania.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "KeepAwake.h"
#include "BassAudioManager.h"
#include "LogOverlay.h"
#include "VolumeOverlay.h"
#include "RulesetManager.h"
#include "ManiaRuleset.h"
#include "TaikoRuleset.h"
Expand All @@ -38,17 +39,14 @@ int main() {
#ifdef __linux__
game.Use(MakeLinuxConsoleComponent);
#endif

game.Use(MakeTickSource)
.Use(MakeBufferController)
.Use(MakeScreenController)
.Use(MakeBeatmapManagementService)
.Use(MakeLogOverlay)
.Use(MakeFpsOverlay)
.Use(MakeRulesetManager); // 注入组件依赖

game.Raise("start"); // 初始化组件

game.GetFeature<IRulesetManager>().Register(MakeManiaRuleset());
game.GetFeature<IRulesetManager>().Register(MakeTaikoRuleset());
game.GetFeature<IRulesetManager>().Register(MakeStdRuleset());
Expand Down
2 changes: 2 additions & 0 deletions cmania/cmania.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@
<ClCompile Include="StdRuleset.cpp" />
<ClCompile Include="TaikoRuleset.cpp" />
<ClCompile Include="TickSource.cpp" />
<ClCompile Include="VolumeOverlay.cpp" />
<ClCompile Include="Win32ConsoleComponent.cpp" />
<ClCompile Include="ManiaRuleset.cpp" />
</ItemGroup>
Expand Down Expand Up @@ -490,6 +491,7 @@
<ClInclude Include="TaikoScoreProcessor.h" />
<ClInclude Include="TickSource.h" />
<ClInclude Include="Unicode.h" />
<ClInclude Include="VolumeOverlay.h" />
<ClInclude Include="Win32ConsoleComponent.h" />
</ItemGroup>
<ItemGroup>
Expand Down
7 changes: 6 additions & 1 deletion cmania/cmania.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
<ClCompile Include="Debug.cpp">
<Filter>Core\Components</Filter>
</ClCompile>
<ClCompile Include="VolumeOverlay.cpp">
<Filter>Core\Components</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ConsoleInput.h">
Expand Down Expand Up @@ -301,6 +304,9 @@
<ClInclude Include="Debug.h">
<Filter>Core\Components</Filter>
</ClInclude>
<ClInclude Include="VolumeOverlay.h">
<Filter>Core\Components</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Core">
Expand Down Expand Up @@ -719,7 +725,6 @@
<None Include="Samples\*\*" />
<None Include="Samples\*\*" />
<None Include="Samples\*\*" />
<None Include="Samples\*\*" />
<None Include="Samples\Argon\drum-hitfinish.wav" />
<None Include="Samples\Argon\drum-hitnormal.wav" />
<None Include="Samples\Argon\drum-hitwhistle.wav" />
Expand Down

0 comments on commit 999fd5b

Please sign in to comment.