1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "fsCDisplay.h"

#include <cstdlib>

#include <bgfx/bgfx.h>

#include <GLFW/glfw3.h>
#if defined __APPLE__
	#define GLFW_EXPOSE_NATIVE_COCOA
#elif defined _WIN32
	#define GLFW_EXPOSE_NATIVE_WIN32
#endif
#include <GLFW/glfw3native.h>

#include <fsCore/src/debug/fsAssert.h>
#include <fsCore/fsIHardware.h>
#include <fsCore/src/fsCAppSettings.h>
#include <fsCore/impl/bgfx/fsCBgfxWindow.h>
#include "native/fsCBgfx2d.h"

#include <fsAppCore/src/fsCAppCoreMain.h>

#include <fsAudio/fsIAudioManager.h>


namespace
{
	void* nativeWindowHandleGet(GLFWwindow* const pWindow)<--- Parameter 'pWindow' can be declared as pointer to const
	{
#if defined __APPLE__
		return glfwGetCocoaWindow(pWindow);
#elif defined _WIN32
		return glfwGetWin32Window(pWindow);
#else
		return nullptr;
#endif
	}
}

fsBool fsCDisplay::shouldCloseGet()
{
	GLFWwindow* const window = fsNBgfxWindow::windowGet();
	return window ? glfwWindowShouldClose(window) : false;
}

fsBool fsCDisplay::displayTypeIsDesktopGet() const
{
	return true;
}

fsPoint fsCDisplay::fixedDisplaySizeGet() const
{
	return fsPoint(640, 480);
}

void fsCDisplay::desktopScreenModesEnumerateDo()
{
	screenModeAdd(1366,768,32);
}

void fsCDisplay::buffersSwap()<--- Either there is a missing 'override', or the member function 'fsCDisplay::buffersSwap' can be static.
{
}

fsBool fsCDisplay::desktopScreenModeSetDo(const fsCDisplayMode* const pDisplayMode, fsBool pFullScreen)
{
	openGLViewCreate(pDisplayMode, pFullScreen);

	return true;
}

fsStr fsCDisplay::nameGet()
{
	const fsCAppSettings* const settings = fsCAppCoreMain::instanceGet()->settingsGet();
	fsStr name = settings->strGetWithDefault("installedName", "testApp");
	if (settings->buildFlavourGet() == "CE")
	{
		name += " Collectors Edition";
	}
	return name;
}

void fsCDisplay::openGLViewCreate(const fsCDisplayMode* const pDisplayMode, fsBool pFullScreen)
{
	glfwSetWindowSize(mWindow, pDisplayMode->widthGet(), pDisplayMode->heightGet());

	if (GLFWmonitor* const monitor = glfwGetPrimaryMonitor())
	{
		const GLFWvidmode* const mode = glfwGetVideoMode(monitor);
		glfwSetWindowPos(mWindow,
			(mode->width - pDisplayMode->widthGet()) / 2,
			(mode->height - pDisplayMode->heightGet()) / 2);
	}

	int fbWidth = 0;
	int fbHeight = 0;
	glfwGetFramebufferSize(mWindow, &fbWidth, &fbHeight);
	bgfx::reset(static_cast<uint32_t>(fbWidth), static_cast<uint32_t>(fbHeight), fsNBgfx2d::resetFlagsGet());
}

void fsCDisplay::windowCreate()
{
	glfwInit();

	// bgfx owns the graphics device - no GL context on the window
	glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
	glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
#if defined __APPLE__
	// Keep framebuffer pixels 1:1 with window coordinates so the 2d ortho
	// matches mouse coordinates without retina scaling
	glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_FALSE);
#endif

	mWindow = glfwCreateWindow(640, 480, nameGet().utf8Get(), nullptr, nullptr);
	fsAssert(mWindow != nullptr, "Failed to create GLFW window.\n");

	fsNBgfxWindow::windowSet(mWindow);
}

void fsCDisplay::bgfxInitialise()
{
	// bgfx runs its own render thread (its default). Don't call
	// bgfx::renderFrame() here - that forces single-threaded mode, where
	// frame() renders and presents inline and the whole game serialises
	// against vsync (halving the frame rate whenever a frame doesn't fit
	// in one vsync interval).

	int fbWidth = 0;
	int fbHeight = 0;
	glfwGetFramebufferSize(mWindow, &fbWidth, &fbHeight);

	bgfx::Init init;
	init.platformData.nwh = nativeWindowHandleGet(mWindow);
	init.resolution.width = static_cast<uint32_t>(fbWidth);
	init.resolution.height = static_cast<uint32_t>(fbHeight);
	init.resolution.reset = fsNBgfx2d::resetFlagsGet();

	const bool initialised = bgfx::init(init);
	fsAssert(initialised, "Failed to initialise bgfx.\n");

	// FSBGFX_STATS=1 overlays bgfx's built-in CPU/GPU timing + draw stats
	if (std::getenv("FSBGFX_STATS"))
	{
		bgfx::setDebug(BGFX_DEBUG_STATS);
	}
}

fsCDisplay::fsCDisplay()
{
	windowCreate();
	bgfxInitialise();
}

fsCDisplay::~fsCDisplay()
{
	bgfx::shutdown();

	if (mWindow)
	{
		fsNBgfxWindow::windowSet(nullptr);
		glfwDestroyWindow(mWindow);
		mWindow = nullptr;
	}
	glfwTerminate();
}