Skip to content

Commit

Permalink
[Window] Fixed resize + vsync support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Oct 29, 2023
1 parent 93bf957 commit c84ed93
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
20 changes: 15 additions & 5 deletions source/client/core/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <gk/core/Utils.hpp>
#include <gk/core/Exception.hpp>

#include "Config.hpp"
#include "BgfxView.hpp"
#include "CoreApplication.hpp"
#include "Window.hpp"
Expand Down Expand Up @@ -64,6 +65,16 @@ void Window::close() {

void Window::clear() {
bgfx::touch(0);

if (m_hasChanged) {
uint32_t flags = (m_isVerticalSyncEnabled) ? BGFX_RESET_VSYNC : BGFX_RESET_NONE;
bgfx::reset(m_size.x, m_size.y, flags);

for (bgfx::ViewId i = 0; i < BgfxView::Count; ++i)
bgfx::setViewRect(i, 0, 0, (u16)m_size.x, (u16)m_size.y);

m_hasChanged = false;
}
}

void Window::display() {
Expand All @@ -72,11 +83,10 @@ void Window::display() {

void Window::onEvent(const SDL_Event &event) {
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
for (bgfx::ViewId i = 0; i < BgfxView::Count; ++i)
bgfx::setViewRect(i, 0, 0, (uint16_t)event.window.data1, (uint16_t)event.window.data2);

m_size.x = (unsigned int)event.window.data1;
m_size.y = (unsigned int)event.window.data2;

m_hasChanged = true;
}

if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE) {
Expand All @@ -87,7 +97,7 @@ void Window::onEvent(const SDL_Event &event) {
void Window::setVerticalSyncEnabled(bool isVerticalSyncEnabled) {
m_isVerticalSyncEnabled = isVerticalSyncEnabled;

gkWarning() << "Vsync not implemented yet";
m_hasChanged = true;
}

void Window::setWindowMode(Mode mode) {
Expand Down Expand Up @@ -142,7 +152,7 @@ void Window::initBGFX() {
bgfx::Init init;
init.resolution.width = m_size.x;
init.resolution.height = m_size.y;
init.resolution.reset = BGFX_RESET_VSYNC;
init.resolution.reset = (Config::isVerticalSyncEnabled) ? BGFX_RESET_VSYNC : BGFX_RESET_NONE;
init.type = bgfx::RendererType::OpenGL;
init.callback = &m_callback;

Expand Down
2 changes: 2 additions & 0 deletions source/client/core/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class Window : public RenderTarget {
bool m_isVerticalSyncEnabled = false;

BgfxCallback m_callback;

bool m_hasChanged = false;
};

#endif // WINDOW_HPP_
10 changes: 5 additions & 5 deletions source/client/graphics/BgfxView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@

namespace BgfxView {
enum Enum {
Sky,
World,
Effect,
HUD,
UI,
Sky = 0,
World = 1,
Effect = 2,
HUD = 3,
UI = 4,

Count
};
Expand Down
14 changes: 8 additions & 6 deletions source/client/graphics/Framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,19 @@ void Framebuffer::init(u16 width, u16 height) {
if (!bgfx::isValid(m_textures[1]))
throw EXCEPTION("Framebuffer error: Unable to create depth texture with format D24S8");

if (bgfx::isValid(m_handle))
bgfx::destroy(m_handle);

m_handle = bgfx::createFrameBuffer(2, m_textures, true);

if (!bgfx::isValid(m_handle))
throw EXCEPTION("Failed to create framebuffer");

bgfx::setViewRect(BgfxView::Effect, 0, 0, width, height);
bgfx::setViewClear(BgfxView::Effect, BGFX_CLEAR_COLOR);

bgfx::setViewFrameBuffer(BgfxView::Sky, m_handle);
bgfx::setViewFrameBuffer(BgfxView::World, m_handle);
}

void Framebuffer::free() {
bgfx::setViewFrameBuffer(BgfxView::Sky, BGFX_INVALID_HANDLE);
bgfx::setViewFrameBuffer(BgfxView::World, BGFX_INVALID_HANDLE);

if (bgfx::isValid(m_handle)) {
bgfx::destroy(m_handle);
m_handle.idx = bgfx::kInvalidHandle;
Expand Down Expand Up @@ -124,6 +121,11 @@ void Framebuffer::loadShader(const std::string &name) {
m_shader.loadFromFile(name);
}

void Framebuffer::prepareDraw() const {
bgfx::setViewFrameBuffer(BgfxView::Sky, m_handle);
bgfx::setViewFrameBuffer(BgfxView::World, m_handle);
}

void Framebuffer::draw() const {
bgfx::setTexture(0, m_colorTextureSampler, m_textures[0]);
bgfx::setTexture(1, m_depthTextureSampler, m_textures[1]);
Expand Down
1 change: 1 addition & 0 deletions source/client/graphics/Framebuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Framebuffer : public gk::NonCopyable {

void loadShader(const std::string &name);

void prepareDraw() const;
void draw() const;

private:
Expand Down
2 changes: 2 additions & 0 deletions source/client/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ void GameState::draw(RenderTarget &target, RenderStates states) const {

target.setView(m_camera);

m_fbo.prepareDraw();

states.view = BgfxView::Sky;
{
target.draw(m_skybox, states);
Expand Down

0 comments on commit c84ed93

Please sign in to comment.