Skip to content

Commit

Permalink
Merge pull request #28 from BeomJinNa/main
Browse files Browse the repository at this point in the history
Enhance renderer functionality and input delay settings
  • Loading branch information
HongLabInc authored Jan 15, 2025
2 parents c1aaf61 + df48af4 commit 8f6bb9e
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 35 deletions.
77 changes: 77 additions & 0 deletions ColorManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "ColorManager.h"

ColorManager::ColorManager()
: mRandomEngine(std::random_device{}())
{
}

void ColorManager::AddColorToTable(ConsoleColor color)
{
auto it = std::find(mColorTable.begin(), mColorTable.end(), color);

if (it == mColorTable.end())
{
mColorTable.push_back(color);
}
}

void ColorManager::RemoveColorFromTable(ConsoleColor color)
{
auto it = std::find(mColorTable.begin(), mColorTable.end(), color);

if (it != mColorTable.end())
{
mColorTable.erase(it);
}
}

void ColorManager::ClearColors()
{
mColorTable.clear();
}

void ColorManager::AddAllColors()
{
for (int i = 0; i <= static_cast<int>(ConsoleColor::BrightWhite); ++i)
{
AddColorToTable(static_cast<ConsoleColor>(i));
}
}

void ColorManager::AddBrightColors()
{
AddColorToTable(ConsoleColor::BrightBlue);
AddColorToTable(ConsoleColor::BrightGreen);
AddColorToTable(ConsoleColor::BrightRed);
AddColorToTable(ConsoleColor::BrightCyan);
AddColorToTable(ConsoleColor::BrightMagenta);
AddColorToTable(ConsoleColor::BrightYellow);
AddColorToTable(ConsoleColor::BrightWhite);
}

void ColorManager::AddDarkColors()
{
AddColorToTable(ConsoleColor::Black);
AddColorToTable(ConsoleColor::Blue);
AddColorToTable(ConsoleColor::Green);
AddColorToTable(ConsoleColor::Red);
AddColorToTable(ConsoleColor::Cyan);
AddColorToTable(ConsoleColor::Magenta);
AddColorToTable(ConsoleColor::Yellow);
AddColorToTable(ConsoleColor::White);
}

ConsoleColor ColorManager::GetRandomColor() const
{
if (mColorTable.empty())
{
return ConsoleColor::BrightWhite;
}
std::uniform_int_distribution<size_t> dist(0, mColorTable.size() - 1);
return mColorTable[dist(mRandomEngine)];
}

WORD ColorManager::ToWord(ConsoleColor fg, ConsoleColor bg)
{
return static_cast<WORD>(fg) | (static_cast<WORD>(bg) << 4);
}
27 changes: 27 additions & 0 deletions ColorManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "ConsoleColor.h"
#include <vector>
#include <random>

class ColorManager
{
public:
ColorManager();
~ColorManager() = default;

void AddColorToTable(ConsoleColor color);
void RemoveColorFromTable(ConsoleColor color);
void ClearColors();
void AddAllColors();
void AddBrightColors();
void AddDarkColors();

ConsoleColor GetRandomColor() const;

static WORD ToWord(ConsoleColor fg, ConsoleColor bg = ConsoleColor::Black);

private:
std::vector<ConsoleColor> mColorTable;
mutable std::mt19937 mRandomEngine;
};
17 changes: 16 additions & 1 deletion ConsoleRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
#include "ConsoleRenderer.h"
#include "ConsoleFrame.h"

ConsoleRenderer::ConsoleRenderer(int width, int height)
ConsoleRenderer::ConsoleRenderer(int width, int height, float frameRate)
: mWidth(width)
, mHeight(height)
, mFrameRate(frameRate)
, mFrameTime(1.0f / frameRate)
{
mActualWidth = mWidth * CELL_WIDTH;

Expand Down Expand Up @@ -50,8 +52,21 @@ ConsoleFrame * ConsoleRenderer::GetMainFrame()
return mMainFrame;
}

float ConsoleRenderer::GetFrameRate() const
{
return mFrameRate;
}

float ConsoleRenderer::GetFrameTime() const
{
return mFrameTime;
}

void ConsoleRenderer::RemoveFrame(ConsoleFrame* frame)
{
if(frame == nullptr)
return;

auto it = std::find(mFrames.begin(), mFrames.end(), frame);

if (it != mFrames.end())
Expand Down
7 changes: 6 additions & 1 deletion ConsoleRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ConsoleFrame;
class ConsoleRenderer
{
public:
ConsoleRenderer(int width, int height);
ConsoleRenderer(int width, int height, float frameRate);
~ConsoleRenderer();

void Clear();
Expand All @@ -19,6 +19,9 @@ class ConsoleRenderer

ConsoleFrame* GetMainFrame();

float GetFrameRate() const;
float GetFrameTime() const;

void SetBuffer(int row, int column, CHAR_INFO charInfo);
void Render();

Expand All @@ -33,6 +36,8 @@ class ConsoleRenderer
int mWidth;
int mHeight;
int mActualWidth; //공백(LEFT, RIGHT SPACE)포함 실제 너비
float mFrameRate;
float mFrameTime;
HANDLE mConsoleHandle;
CHAR_INFO* mBuffer;

Expand Down
6 changes: 4 additions & 2 deletions Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ void Engine::Run()
int64_t currentTime = 0;
int64_t previousTime = counter.QuadPart;

float targetFrameRate = 5.0f; // 목표 fps
float targetOneFrameTime = 1.0f / targetFrameRate;
float targetFrameRate = consoleRenderer.GetFrameRate();
float targetOneFrameTime = consoleRenderer.GetFrameTime();

// Main Game Loop
while(true)
Expand All @@ -60,6 +60,8 @@ void Engine::Run()

if(deltaTime >= targetOneFrameTime)
{
static int count = 0;
++count;
ProcessInput();
Update(deltaTime);
Draw();
Expand Down
7 changes: 3 additions & 4 deletions GameMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ GameMode::GameMode(ConsoleRenderer& renderer, GameModeType mode)
}
}


void GameMode::Update(InputManager* im)
{
for(auto& board : mBoards)
Expand All @@ -40,10 +39,10 @@ void GameMode::Draw()

void GameMode::SetupSingleMode()
{
mBoards.push_back(TetrisBoard(mRenderer, 3, 3, 12, 24));
mBoards.emplace_back(mRenderer, 3, 3, 12, 24);

//임시 출력 테스트
ShowExampleConsoleFrame();
//ShowExampleConsoleFrame();
}

void GameMode::SetupMultiplayerMode()
Expand All @@ -54,7 +53,7 @@ void GameMode::SetupMultiplayerMode()




// ConsoleFrame 출력 테스트용 예시, 추후 지워도 되는 함수입니다.
void GameMode::ShowExampleConsoleFrame()
{
ConsoleFrame* titleFrame = mRenderer.AddFrame(18, 5, 30, 10);
Expand Down
2 changes: 1 addition & 1 deletion HongLabTetris.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

int main()
{
ConsoleRenderer renderer(60, 40); // 가로, 세로
ConsoleRenderer renderer(60, 40, 240.0f); // 가로, 세로, 주사율
Engine engine(renderer);
engine.Run();

Expand Down
3 changes: 3 additions & 0 deletions HongLabTetris.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp14</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -129,6 +130,7 @@
<ItemGroup>
<ClCompile Include="Block.cpp" />
<ClCompile Include="Cell.cpp" />
<ClCompile Include="ColorManager.cpp" />
<ClCompile Include="ConsoleFrame.cpp" />
<ClCompile Include="ConsoleRenderer.cpp" />
<ClCompile Include="Engine.cpp" />
Expand All @@ -141,6 +143,7 @@
<ClInclude Include="Block.h" />
<ClInclude Include="BlockMap.h" />
<ClInclude Include="Cell.h" />
<ClInclude Include="ColorManager.h" />
<ClInclude Include="ConsoleFrame.h" />
<ClInclude Include="ConsoleRenderer.h" />
<ClInclude Include="Engine.h" />
Expand Down
12 changes: 9 additions & 3 deletions HongLabTetris.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
<ClCompile Include="TetrisBoard.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ColorManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Cell.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="Engine.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand All @@ -71,5 +71,11 @@
<ClInclude Include="TetrisBoard.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Cell.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ColorManager.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
Loading

0 comments on commit 8f6bb9e

Please sign in to comment.