Skip to content

Commit

Permalink
[#17] : Updated window handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ange-yaghi committed Jul 7, 2020
1 parent 9182d66 commit f27013b
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 20 deletions.
8 changes: 4 additions & 4 deletions engines/basic/include/delta_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace dbasic {

class DeltaEngine : public ysObject {
public:
static const int MAX_LAYERS = 256;
static const int MaxLayers = 256;

enum class DrawTarget {
Gui,
Expand All @@ -33,8 +33,8 @@ namespace dbasic {
};

struct DrawCall {
ysTexture *Texture = nullptr;
ShaderObjectVariables ObjectVariables;
ysTexture *Texture = nullptr;
ModelAsset *Model = nullptr;
};

Expand Down Expand Up @@ -240,8 +240,8 @@ namespace dbasic {
protected:
// Drawing queues
// TODO: these should not be on the stack
ysExpandingArray<DrawCall, 256> m_drawQueue[MAX_LAYERS];
ysExpandingArray<DrawCall, 256> m_drawQueueGui[MAX_LAYERS];
ysExpandingArray<DrawCall, 256> *m_drawQueue;
ysExpandingArray<DrawCall, 256> *m_drawQueueGui;
ysError ExecuteDrawQueue(DrawTarget target);
};

Expand Down
4 changes: 2 additions & 2 deletions engines/basic/src/asset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ ysError dbasic::AssetManager::LoadSceneFile(const char *fname, bool placeInVram)
CompiledHeader fileHeader;
file.read((char *)&fileHeader, sizeof(CompiledHeader));

unsigned short *indicesFile = new unsigned short[2 * 1024 * 1024 / 2]; // 4 MB
char *verticesFile = (char *)malloc(4 * 1024 * 1024); // 16 MB
unsigned short *indicesFile = new unsigned short[1024 * 1024]; // 1 MB
char *verticesFile = (char *)malloc(4 * 1024 * 1024); // 4 MB

int currentIndexOffset = 0;
int currentVertexByteOffset = 0;
Expand Down
5 changes: 4 additions & 1 deletion engines/basic/src/delta_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ dbasic::DeltaEngine::DeltaEngine() {
m_cameraFov = ysMath::Constants::PI / 3.0f;

ResetLights();

m_drawQueue = new ysExpandingArray<DrawCall, 256>[MaxLayers];
m_drawQueueGui = new ysExpandingArray<DrawCall, 256>[MaxLayers];
}

dbasic::DeltaEngine::~DeltaEngine() {
Expand Down Expand Up @@ -863,7 +866,7 @@ ysError dbasic::DeltaEngine::ExecuteDrawQueue(DrawTarget target) {
m_shaderScreenVariablesSync = false;
}

for (int i = 0; i < MAX_LAYERS; i++) {
for (int i = 0; i < MaxLayers; i++) {
int objectsAtLayer = 0;

if (target == DrawTarget::Main) objectsAtLayer = m_drawQueue[i].GetNumObjects();
Expand Down
2 changes: 1 addition & 1 deletion include/yds_dynamic_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ysDynamicArray {
ysDynamicArray() {
m_maxSize = START_SIZE;
m_nObjects = 0;
m_array = NULL;
m_array = nullptr;

Preallocate(m_maxSize);
}
Expand Down
6 changes: 3 additions & 3 deletions src/yds_color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ ysVector ysColor::linearToSrgb(const ysVector &srgb) {
}

ysVector ysColor::srgbiToLinear(unsigned int rgb, float a) {
int b = rgb & 0xFF; rgb >>= 1;
int g = rgb & 0xFF; rgb >>= 1;
int r = rgb & 0xFF; rgb >>= 1;
int b = rgb & 0xFF; rgb >>= 8;
int g = rgb & 0xFF; rgb >>= 8;
int r = rgb & 0xFF; rgb >>= 8;

return srgbToLinear(
r / 255.0f,
Expand Down
13 changes: 9 additions & 4 deletions src/yds_d3d11_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ ysError ysD3D11Device::InitializeDevice() {
HRESULT result;
D3D_FEATURE_LEVEL highestFeatureLevel;

UINT deviceCreationFlags = 0;
#ifdef _DEBUG
deviceCreationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif /* _DEBUG */

result = D3D11CreateDevice(nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
D3D11_CREATE_DEVICE_DEBUG,
deviceCreationFlags,
nullptr,
0,
D3D11_SDK_VERSION,
Expand Down Expand Up @@ -161,11 +166,11 @@ ysError ysD3D11Device::CreateRenderingContext(ysRenderingContext **context, ysWi
swapChainDesc.SampleDesc.Count = m_multisampleCount;
swapChainDesc.SampleDesc.Quality = m_multisampleQuality;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 2;
swapChainDesc.BufferCount = 1;
swapChainDesc.OutputWindow = windowsWindow->GetWindowHandle();
swapChainDesc.Windowed = window->GetStyle() != ysWindow::WindowStyle::FULLSCREEN;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
swapChainDesc.Flags = 0;

IDXGIFactory *factory = GetDXGIFactory();

Expand Down Expand Up @@ -286,7 +291,7 @@ ysError ysD3D11Device::UpdateRenderingContext(ysRenderingContext *context) {
YDS_NESTED_ERROR_CALL(DestroyD3D11RenderTarget(attachedTarget));
}

HRESULT result = d3d11Context->m_swapChain->ResizeBuffers(2, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH);
HRESULT result = d3d11Context->m_swapChain->ResizeBuffers(1, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH);

if (FAILED(result)) {
return YDS_ERROR_RETURN(ysError::YDS_API_ERROR);
Expand Down
2 changes: 1 addition & 1 deletion src/yds_opengl_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ ysError ysOpenGLDevice::UpdateRenderingContext(ysRenderingContext *context) {
ysRenderTarget *target = context->GetAttachedRenderTarget();
ysOpenGLVirtualContext *openglContext = static_cast<ysOpenGLVirtualContext *>(context);

if (target != NULL) {
if (target != nullptr) {
YDS_NESTED_ERROR_CALL(ResizeRenderTarget(target, context->GetWindow()->GetScreenWidth(), context->GetWindow()->GetScreenHeight()));
}

Expand Down
3 changes: 1 addition & 2 deletions src/yds_windows_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ATOM ysWindowsWindow::RegisterWindowsClass() {
wc.hInstance = m_instance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "GAME_ENGINE_WINDOW";
wc.hIconSm = NULL;
Expand All @@ -48,7 +48,6 @@ ysError ysWindowsWindow::InitializeWindow(ysWindow *parent, const char *title, W
RECT rc = { 0, 0, width, height };
AdjustWindowRect(&rc, win32Style, FALSE);

GetLastError();
m_hwnd = CreateWindow(
"GAME_ENGINE_WINDOW",
title,
Expand Down
22 changes: 20 additions & 2 deletions src/yds_windows_window_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ysWindowsWindowSystem::~ysWindowsWindowSystem() {
ysError ysWindowsWindowSystem::NewWindow(ysWindow **newWindow) {
YDS_ERROR_DECLARE("NewWindow");

if (newWindow == NULL) return YDS_ERROR_RETURN(ysError::YDS_INVALID_PARAMETER);
if (newWindow == nullptr) return YDS_ERROR_RETURN(ysError::YDS_INVALID_PARAMETER);
if (m_instance == NULL) return YDS_ERROR_RETURN(ysError::YDS_NO_CONTEXT);

ysWindowsWindow *windowsWindow = m_windowArray.NewGeneric<ysWindowsWindow>();
Expand Down Expand Up @@ -86,18 +86,36 @@ LRESULT WINAPI ysWindowsWindowSystem::WinProc(HWND hWnd, UINT msg, WPARAM wParam
ysWindowsInputSystem *inputSystem = static_cast<ysWindowsInputSystem *>(windowsSystem->GetInputSystem());

ysWindow *target = windowsSystem->FindWindowFromHandle(hWnd);

PAINTSTRUCT ps;
HDC hdc;

if (target) {
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_CLOSE:
target->OnCloseWindow();
ysWindowSystem::Get()->CloseWindow(target);
return 0;
case WM_SIZE:
target->OnResizeWindow(LOWORD(lParam), HIWORD(lParam));
if (wParam == SIZE_MAXIMIZED || wParam == SIZE_MINIMIZED || wParam == SIZE_RESTORED) {
target->OnResizeWindow(LOWORD(lParam), HIWORD(lParam));
}
return 0;
case WM_EXITSIZEMOVE:
{
RECT rect;
if (GetClientRect(hWnd, &rect)) {
target->OnResizeWindow(rect.right - rect.left, rect.bottom - rect.top);
}
return 0;
}
case WM_MOVE:
target->OnMoveWindow(LOWORD(lParam), HIWORD(lParam));
return 0;
Expand Down

0 comments on commit f27013b

Please sign in to comment.