Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 49 additions & 11 deletions src/aquarium-direct-map/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <ratio>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#define GLFW_INCLUDE_NONE
Expand Down Expand Up @@ -47,6 +48,29 @@

#define min(a, b) ((a) < (b) ? (a) : (b))

namespace {

class GLFWInitializer {
public:
GLFWInitializer() {
++sCounter;
glfwInit(); // Initialization failure can be detected with glfwGetError().
// On success, the next glfwInit() invocation will be a no-op.
}
~GLFWInitializer() {
if (--sCounter == 0) {
glfwTerminate(); // no effect if GLFW is not initialized
}
}

private:
static int sCounter;
};

int GLFWInitializer::sCounter = 0;

} // namespace

std::chrono::steady_clock::time_point getCurrentTimePoint();
void render();

Expand Down Expand Up @@ -215,6 +239,26 @@ std::chrono::steady_clock::time_point getCurrentTimePoint() {
#endif
}

std::pair<int, int> getMonitorResolution() {
GLFWInitializer initializer;
if (glfwGetError(nullptr) != GLFW_NO_ERROR) {
std::cout << "Failed to initialize GLFW" << std::endl;
return {};
}

GLFWmonitor *monitor = glfwGetPrimaryMonitor();
if (!monitor) {
return {};
}

const GLFWvidmode *mode = glfwGetVideoMode(monitor);
if (!mode) {
return {};
}

return {mode->width, mode->height};
}

void setGenericConstMatrix(GenericConst *genericConst) {
genericConst->viewProjection = &viewProjection;
genericConst->viewInverse = &viewInverse;
Expand Down Expand Up @@ -436,10 +480,9 @@ bool initialize(int argc, char **argv) {
}

int main(int argc, char **argv) {

// initialize GLFW
if (!glfwInit()) {
std::cout << stderr << "Failed to initialize GLFW" << std::endl;
GLFWInitializer initializer;
if (glfwGetError(nullptr) != GLFW_NO_ERROR) {
std::cout << "Failed to initialize GLFW" << std::endl;
return -1;
}

Expand All @@ -460,15 +503,12 @@ int main(int argc, char **argv) {
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);

GLFWmonitor *pMonitor = glfwGetPrimaryMonitor();
const GLFWvidmode *mode = glfwGetVideoMode(pMonitor);
clientWidth = mode->width;
clientHeight = mode->height;
clientWidth = getMonitorResolution().first;
clientHeight = getMonitorResolution().second;

window = glfwCreateWindow(clientWidth, clientHeight, "Aquarium", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to open GLFW window." << std::endl;
glfwTerminate();
return -1;
}
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
Expand Down Expand Up @@ -505,8 +545,6 @@ int main(int argc, char **argv) {

onDestroy();

glfwTerminate();

return 0;
}

Expand Down
38 changes: 38 additions & 0 deletions src/aquarium-optimized/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,52 @@

#include "Context.h"

#include <iostream>
#include <sstream>

#define GLFW_INCLUDE_NONE
#include "GLFW/glfw3.h"

#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_internal.h"

#include "Aquarium.h"

int Context::GLFWInitializer::sCounter = 0;

Context::GLFWInitializer::GLFWInitializer() {
++sCounter;
glfwInit(); // Initialization failure can be detected with glfwGetError(). On
// success, the next glfwInit() invocation will be a no-op.
}

Context::GLFWInitializer::~GLFWInitializer() {
if (--sCounter == 0) {
glfwTerminate(); // no effect if GLFW is not initialized
}
}

std::pair<int, int> Context::getMonitorResolution() {
GLFWInitializer initializer;
if (glfwGetError(nullptr) != GLFW_NO_ERROR) {
std::cout << "Failed to initialize GLFW" << std::endl;
return {};
}

GLFWmonitor *monitor = glfwGetPrimaryMonitor();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we cache them? (Assume call glfwGetPrimaryMonitor for the assumption that the user have multiple monitors?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was one of my initial candidate ideas, and I happened to choose the other considering it's only called during initialization. Personally I'd like to avoid introducing too many static variables in the code base, but let's see how others feel about this.

if (!monitor) {
return {};
}

const GLFWvidmode *mode = glfwGetVideoMode(monitor);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. It's a getter, concerns for so many glfw calls

if (!mode) {
return {};
}

return {mode->width, mode->height};
}

void Context::renderImgui(
const FPSTimer &fpsTimer,
int *fishCount,
Expand Down
13 changes: 13 additions & 0 deletions src/aquarium-optimized/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <bitset>
#include <string>
#include <utility>
#include <vector>

#include "Aquarium.h"
Expand All @@ -25,7 +26,18 @@ class Texture;
static char fishCountInputBuffer[64];

class Context {
class GLFWInitializer {
public:
GLFWInitializer();
~GLFWInitializer();

private:
static int sCounter;
};

public:
static std::pair<int, int> getMonitorResolution();

Context()
: mDisableControlPanel(false),
mMSAASampleCount(1),
Expand Down Expand Up @@ -111,6 +123,7 @@ class Context {
int mMSAASampleCount;

private:
GLFWInitializer mGLFWInitializer;
bool show_option_window;
};

Expand Down
16 changes: 5 additions & 11 deletions src/aquarium-optimized/d3d12/ContextD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ ContextD3D12::~ContextD3D12() {
destoryImgUI();
}
destoryFishResource();

glfwTerminate();
}

ContextD3D12 *ContextD3D12::create(BACKENDTYPE backendType) {
Expand All @@ -91,9 +89,8 @@ bool ContextD3D12::initialize(
mDisableControlPanel =
toggleBitset.test(static_cast<TOGGLE>(TOGGLE::DISABLECONTROLPANEL));

// initialise GLFW
if (!glfwInit()) {
std::cout << "Failed to initialise GLFW" << std::endl;
if (glfwGetError(nullptr) != GLFW_NO_ERROR) {
std::cout << "Failed to initialize GLFW" << std::endl;
return false;
}

Expand All @@ -102,24 +99,21 @@ bool ContextD3D12::initialize(
// set full screen
// glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);

GLFWmonitor *pMonitor = glfwGetPrimaryMonitor();
const GLFWvidmode *mode = glfwGetVideoMode(pMonitor);
mClientWidth = mode->width;
mClientHeight = mode->height;
mClientWidth = getMonitorResolution().first;
mClientHeight = getMonitorResolution().second;

setWindowSize(windowWidth, windowHeight);

if (toggleBitset.test(static_cast<size_t>(TOGGLE::ENABLEFULLSCREENMODE))) {
mWindow = glfwCreateWindow(mClientWidth, mClientHeight, "Aquarium",
pMonitor, nullptr);
glfwGetPrimaryMonitor(), nullptr);
} else {
mWindow = glfwCreateWindow(mClientWidth, mClientHeight, "Aquarium", nullptr,
nullptr);
}

if (mWindow == nullptr) {
std::cout << "Failed to open GLFW window." << std::endl;
glfwTerminate();
return false;
}

Expand Down
16 changes: 5 additions & 11 deletions src/aquarium-optimized/dawn/ContextDawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ ContextDawn::~ContextDawn() {
mSwapchain = nullptr;
queue = nullptr;
mDevice = nullptr;

glfwTerminate();
}

ContextDawn *ContextDawn::create(BACKENDTYPE backendType) {
Expand Down Expand Up @@ -151,9 +149,8 @@ bool ContextDawn::initialize(
mDisableControlPanel =
toggleBitset.test(static_cast<TOGGLE>(TOGGLE::DISABLECONTROLPANEL));

// initialise GLFW
if (!glfwInit()) {
std::cout << "Failed to initialise GLFW" << std::endl;
if (glfwGetError(nullptr) != GLFW_NO_ERROR) {
std::cout << "Failed to initialize GLFW" << std::endl;
return false;
}

Expand All @@ -163,24 +160,21 @@ bool ContextDawn::initialize(
// set full screen
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);

GLFWmonitor *pMonitor = glfwGetPrimaryMonitor();
const GLFWvidmode *mode = glfwGetVideoMode(pMonitor);
mClientWidth = mode->width;
mClientHeight = mode->height;
mClientWidth = getMonitorResolution().first;
mClientHeight = getMonitorResolution().second;

setWindowSize(windowWidth, windowHeight);

if (toggleBitset.test(static_cast<size_t>(TOGGLE::ENABLEFULLSCREENMODE))) {
mWindow = glfwCreateWindow(mClientWidth, mClientHeight, "Aquarium",
pMonitor, nullptr);
glfwGetPrimaryMonitor(), nullptr);
} else {
mWindow = glfwCreateWindow(mClientWidth, mClientHeight, "Aquarium", nullptr,
nullptr);
}

if (mWindow == nullptr) {
std::cout << "Failed to open GLFW window." << std::endl;
glfwTerminate();
return false;
}

Expand Down
16 changes: 5 additions & 11 deletions src/aquarium-optimized/opengl/ContextGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ ContextGL::~ContextGL() {
if (!mDisableControlPanel) {
destoryImgUI();
}

glfwTerminate();
}

ContextGL *ContextGL::create(BACKENDTYPE backendType) {
Expand All @@ -52,9 +50,8 @@ bool ContextGL::initialize(
const std::bitset<static_cast<size_t>(TOGGLE::TOGGLEMAX)> &toggleBitset,
int windowWidth,
int windowHeight) {
// initialise GLFW
if (!glfwInit()) {
std::cout << "Failed to initialise GLFW" << std::endl;
if (glfwGetError(nullptr) != GLFW_NO_ERROR) {
std::cout << "Failed to initialize GLFW" << std::endl;
return false;
}

Expand Down Expand Up @@ -87,25 +84,22 @@ bool ContextGL::initialize(
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
#endif

GLFWmonitor *pMonitor = glfwGetPrimaryMonitor();
const GLFWvidmode *mode = glfwGetVideoMode(pMonitor);
mClientWidth = mode->width;
mClientHeight = mode->height;
mClientWidth = getMonitorResolution().first;
mClientHeight = getMonitorResolution().second;

setWindowSize(windowWidth, windowHeight);

glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
if (toggleBitset.test(static_cast<size_t>(TOGGLE::ENABLEFULLSCREENMODE))) {
mWindow = glfwCreateWindow(mClientWidth, mClientHeight, "Aquarium",
pMonitor, nullptr);
glfwGetPrimaryMonitor(), nullptr);
} else {
mWindow = glfwCreateWindow(mClientWidth, mClientHeight, "Aquarium", nullptr,
nullptr);
}

if (mWindow == nullptr) {
std::cout << "Failed to open GLFW window." << std::endl;
glfwTerminate();
return false;
}

Expand Down