Skip to content

Commit

Permalink
Update to version 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
makuke1234 committed Jan 15, 2022
1 parent 5bdf178 commit 31f71c2
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 18 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# SnakeD2D

![Release version](https://img.shields.io/badge/release-v1.0.1-green.svg)
![Release version](https://img.shields.io/badge/release-v1.0.2-green.svg)
![C++ version](https://img.shields.io/badge/version-C++20-blue.svg)

A snake fork written in C++ utilising Direct2D API.


# Obtaining

The Windows (7 and onwards) binaries can be downloaded [here](https://github.com/makuke1234/SnakeD2D/releases/tag/Release_1.0.1).
The Windows (7 and onwards) binaries can be downloaded [here](https://github.com/makuke1234/SnakeD2D/releases).


# Controls
Expand All @@ -22,6 +22,13 @@ The Windows (7 and onwards) binaries can be downloaded [here](https://github.com
| Escape | Pause/unpause |


# Changelog

* 1.0.2
* Add window resizing support
* Add fullscreen support


# License

This project uses MIT license.
16 changes: 16 additions & 0 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,19 @@ void snake::stopSndRscAsync() noexcept
{
::PlaySoundW(nullptr, nullptr, 0);
}

bool snake::getScreenSize(HWND window, SIZE & screen) noexcept
{
HMONITOR monitor = ::MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST);
MONITORINFO miw{};
miw.cbSize = sizeof miw;
if (::GetMonitorInfoW(monitor, &miw) == FALSE)
{
return false;
}

screen.cx = miw.rcMonitor.right - miw.rcMonitor.left;
screen.cy = miw.rcMonitor.bottom - miw.rcMonitor.top;

return true;
}
2 changes: 2 additions & 0 deletions src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ namespace snake
void playSndRsc(std::uint16_t resource, HINSTANCE hInst) noexcept;
void playSndRscAsync(std::uint16_t resource, HINSTANCE hInst) noexcept;
void stopSndRscAsync() noexcept;

bool getScreenSize(HWND window, SIZE & screen) noexcept;
}
2 changes: 2 additions & 0 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
#define IDW_SOUND_GAME_OVER 111
#define IDW_SOUND_WIN 112

#define VERSION_STR "1.0.2"
#define VERSION_SEQ 1,0,2

#endif
8 changes: 4 additions & 4 deletions src/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ IDW_SOUND_WIN WAVE "assets/tada.wav"

#include <winver.h>
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,1
PRODUCTVERSION 1,0,1
FILEVERSION VERSION_SEQ
PRODUCTVERSION VERSION_SEQ
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
#ifdef _DEBUG
FILEFLAGS VS_FF_DEBUG | VS_FF_PRERELEASE
Expand All @@ -39,12 +39,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "Maku Maku"
VALUE "FileDescription", "A version of Snake written in C++ and using Direct2D"
VALUE "FileVersion", "1.0.1"
VALUE "FileVersion", VERSION_STR
VALUE "InternalName", "Win32App"
VALUE "LegalCopyright", "\xA92022 Maku Maku"
VALUE "OriginalFileName", "SnakeD2D.exe"
VALUE "ProductName", "SnakeD2D"
VALUE "ProductVersion", "1.0.1"
VALUE "ProductVersion", VERSION_STR
END
END
BLOCK "VarFileInfo"
Expand Down
114 changes: 103 additions & 11 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ LRESULT CALLBACK snake::Application::sp_winProc(HWND hwnd, UINT uMsg, WPARAM wp,
}
case WM_SIZE:
{
This->onResize(LOWORD(lp), HIWORD(lp));
RECT r;
::GetClientRect(hwnd, &r);
This->onResize(r.right - r.left, r.bottom - r.top);
break;
}
case WM_DPICHANGED:
Expand Down Expand Up @@ -484,6 +486,82 @@ void snake::Application::p_calcDpiSpecific() noexcept
.height = s_tileSz - this->toDipy(dY)
};
}
void snake::Application::p_calcPositions() noexcept
{
RECT r{};
::GetClientRect(this->m_hwnd, &r);
auto realx = float(r.right - r.left);
auto realy = float(r.bottom - r.top);

auto x = this->fromDipxi<float>(s_tileSz, this->s_fieldWidth);
auto y = this->fromDipyi<float>(s_tileSz, this->s_fieldHeight);

auto factX = realx / x;
auto factY = realy / y;

this->m_factor = factX < factY ? factX : factY;
this->m_offsetX = this->toDipx((realx - x * this->m_factor) / 2.0f);
this->m_offsetY = this->toDipy((realy - y * this->m_factor) / 2.0f);
}
void snake::Application::p_toggleFullScreen() noexcept
{
this->m_isFullscreen ^= 1;

if (this->m_isFullscreen)
{
RECT r{};
::GetClientRect(this->m_hwnd, &r);
this->m_oldSize = {
.cx = r.right - r.left,
.cy = r.bottom - r.top
};
::GetWindowRect(this->m_hwnd, &r);
this->m_oldPos = {
.cx = r.left,
.cy = r.top
};
this->m_oldStyle = static_cast<DWORD>(::SetWindowLongPtrW(
this->m_hwnd,
GWL_STYLE,
WS_SYSMENU | WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE
));

// Get screen size
SIZE screen;
if (snake::getScreenSize(this->m_hwnd, screen)) [[likely]]
{
::MoveWindow(this->m_hwnd, 0, 0, screen.cx, screen.cy, TRUE);
}
else [[unlikely]]
{
this->error(L"Error going to fullscreen!");
}
}
else
{
RECT r = {
.left = 0,
.top = 0,
.right = this->m_oldSize.cx,
.bottom = this->m_oldSize.cy
};
::SetWindowLongPtrW(
this->m_hwnd,
GWL_STYLE,
this->m_oldStyle
);
::AdjustWindowRect(&r, this->m_oldStyle, FALSE);
::MoveWindow(
this->m_hwnd,
this->m_oldPos.cx,
this->m_oldPos.cy,
r.right - r.left,
r.bottom - r.top,
TRUE
);
this->p_calcDpiSpecific();
}
}
bool snake::Application::p_loadD2D1BitmapFromResource(
std::uint16_t resourceId,
dx::SzU const & bmSize,
Expand Down Expand Up @@ -616,7 +694,7 @@ bool snake::Application::initApp(HINSTANCE hInst, int nCmdShow)
0,
this->s_className.data(),
this->s_applicationName.data(),
WS_OVERLAPPEDWINDOW ^ (WS_SIZEBOX | WS_MAXIMIZEBOX),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
Expand Down Expand Up @@ -1001,37 +1079,45 @@ void snake::Application::onRender() noexcept

// Begin also render target painting
this->m_pRT->BeginDraw();
this->m_pRT->SetTransform(D2D1::Matrix3x2F::Identity());

auto scale = D2D1::Matrix3x2F::Scale(D2D1::SizeF(this->m_factor, this->m_factor), D2D1::Point2F());
auto translate = D2D1::Matrix3x2F::Translation(D2D1::SizeF(this->m_offsetX, this->m_offsetY));
this->m_pRT->SetTransform(scale * translate);

this->m_pRT->Clear(D2D1::ColorF(60.f / 255.f, 45.f / 255.f, 159.f / 255.f));

// Get width and height
/*auto [fwidth, fheight] = this->m_pRT->GetSize();
auto [width, height] = POINT{ LONG(fwidth), LONG(fheight) };
*/

// Render scoring first
this->m_text.onRender(this->m_tileSzF, this->m_pRT, this->m_snakeLogic.m_sInfo.scoring);


Tile::onRender(this->m_tiles.obstacleTiles , this->m_pRT);
// Render food tile first
this->m_tiles.snakeFoodTile.onRender(this->m_pRT);

Tile::onRender(this->m_tiles.snakeBodyTiles, this->m_pRT);
this->m_tiles.snakeHeadTile.onRender(this->m_pRT);

this->m_text.onRender(this->m_tileSzF, this->m_pRT, this->m_snakeLogic.m_sInfo.scoring);

if (this->m_pRT->EndDraw() == HRESULT(D2DERR_RECREATE_TARGET)) [[unlikely]]
this->destroyAssets();

::EndPaint(this->m_hwnd, &ps);
}
void snake::Application::onResize(UINT width, UINT height) const noexcept
void snake::Application::onResize(UINT width, UINT height) noexcept
{
if (this->m_pRT) [[likely]]
if (this->m_pRT == nullptr) [[unlikely]]
{
this->m_pRT->Resize(dx::SzU{ width, height });
return;
}

this->m_pRT->Resize(dx::SzU{ width, height });
this->p_calcPositions();
}

LRESULT snake::Application::onKeyPress(WPARAM wp, [[maybe_unused]] LPARAM lp) noexcept
LRESULT snake::Application::onKeyPress(WPARAM wp, LPARAM lp) noexcept
{
Logic::direction dir{};
switch (wp)
Expand All @@ -1046,14 +1132,20 @@ LRESULT snake::Application::onKeyPress(WPARAM wp, [[maybe_unused]] LPARAM lp) no
return 0;
}
break;

case VK_RETURN:
if (this->m_snakeLogic.m_sInfo.scoring.mode != Logic::SnakeInfo::modes::normal || this->m_snakeLogic.m_sInfo.scoring.paused)
{
this->restartGame();
return 0;
}
break;
case VK_F11:
// Act only if key was just pressed down, prevents spamming
if ((lp & 0x40000000) == 0)
{
this->p_toggleFullScreen();
}
break;
}

if (!this->m_snakeLogic.m_sInfo.scoring.paused)
Expand Down
10 changes: 9 additions & 1 deletion src/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ namespace snake
} m_text;

dx::F m_dpiX{ 96.f }, m_dpiY{ 96.f };

dx::F m_factor{ 1.0f }, m_offsetX{ 0.0f }, m_offsetY{ 0.0f };
bool m_isFullscreen{ false };
DWORD m_oldStyle{};
SIZE m_oldSize{}, m_oldPos{};

dx::SzU m_border{};
POINT m_minSize{ .x = 640, .y = 480 };
static constexpr dx::F s_tileSz{ 18.f }, s_fieldWidth{ 63.f }, s_fieldHeight{ 36.f };
Expand All @@ -95,6 +101,8 @@ namespace snake
static LRESULT CALLBACK sp_winProc(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp) noexcept;

void p_calcDpiSpecific() noexcept;
void p_calcPositions() noexcept;
void p_toggleFullScreen() noexcept;
bool p_loadD2D1BitmapFromResource(
std::uint16_t resourceId,
dx::SzU const & bmSize,
Expand Down Expand Up @@ -196,7 +204,7 @@ namespace snake
void destroyAssets() noexcept;

void onRender() noexcept;
void onResize(UINT width, UINT height) const noexcept;
void onResize(UINT width, UINT height) noexcept;

LRESULT onKeyPress(WPARAM wp, LPARAM lp) noexcept;
LRESULT onKeyRelease(WPARAM wp, LPARAM lp) noexcept;
Expand Down

0 comments on commit 31f71c2

Please sign in to comment.