Page 1 of 1

Noesis and GLFW or other OpenGL window framework

Posted: 13 May 2017, 13:14
by Wanderer
I am trying integrate Noesis in to GLFW. Documentation is different with OpenGL sample and then I try two projects.

I don't know if GLRenderDevice is need for render Noesis inside GLFW or not, this is reason why I create two projects.

1. Project which use GLRenderDevice.h and GLRenderDevice.cpp and GLFW. I succesfull render Button.xaml with it, without any interaction. Problem with it is when I close window, program crash. I think it is maybe bad written code (on my side).
Here is full source code:
#include <NoesisGUI.h>
#include <GLRenderDevice.h>

// GLFW
#include <GLFW\glfw3.h>

using namespace Noesis;
Ptr<Noesis::IView> xamlView;

static void error_callback(int error, const char * description) { fprintf(stderr, "Error: %s\n", description); }
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
	if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
		glfwSetWindowShouldClose(window, GLFW_TRUE);
} 
/// =========================================================================================
// Noesis GUI
/// =========================================================================================
void NoesisErrorHandler(const NsChar* filename, NsSize line, const NsChar* desc, NsBool fatal) { printf("\nERROR: %s\n\n", desc); }
void shutdown()
{
	xamlView.Reset();
	Noesis::GUI::Shutdown();
}
/// =========================================================================================
int main(int argc, char * argv[])
{
	// GLFW
	GLFWwindow * window = nullptr;
	glfwSetErrorCallback(error_callback);
	if (!glfwInit()) return -1;
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
	
	// GLFW
	window = glfwCreateWindow(640, 480, "Noesis Button Test", NULL, NULL);
	if (!window)
	{
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);

	// USER
	double mouse_x_pos, mouse_y_pos;
	int windowWidth, windowHeight, bufWinWidth, bufWinHeight;
	/// =========================================================================================
	// Noesis GUI
	/// =========================================================================================
	Noesis::GUI::Init(NoesisErrorHandler);
	Noesis::GUI::SetResourceProvider("");

	Ptr<FrameworkElement> xaml = Noesis::GUI::LoadXaml<FrameworkElement>("Button.xaml");
	xamlView = Noesis::GUI::CreateView(xaml.GetPtr());
	xamlView->SetAntialiasingMode(Noesis::Gui::AntialiasingMode_MSAA);

	Ptr<GLRenderDevice> device = *new GLRenderDevice();
	Ptr<VGContext> context = Noesis::GUI::CreateVGContext(device.GetPtr(), Noesis::VGOptions());
	xamlView->GetRenderer()->Init(context.GetPtr());
	/// =========================================================================================
	
	// Main Loop
	while (!glfwWindowShouldClose(window))
	{
		// window and mouse data
		glfwGetWindowSize(window, &windowWidth, &windowHeight);
		glfwGetFramebufferSize(window, &bufWinWidth, &bufWinHeight);
		glfwGetCursorPos(window, &mouse_x_pos, &mouse_y_pos);

		// Noesis GUI
		xamlView->Update(glfwGetTimerValue() / 1000.0f);
		Noesis::IRenderer * nRender = xamlView->GetRenderer();
		nRender->UpdateRenderTree();

		if (nRender->NeedsOffscreen())
		{
			nRender->RenderOffscreen();
		}

		glViewport(0, 0, bufWinWidth, bufWinHeight);
		glClear(GL_COLOR_BUFFER_BIT);

		nRender->Render();
		// GLFW
		glfwSwapBuffers(window);
		glfwPollEvents();
		xamlView->SetSize(windowWidth, windowHeight);
	}
	shutdown();
	glfwTerminate();
}
2. Second project use
Ptr<RenderDevice> device;
//  instead
Ptr<GLRenderDevice> device = *new GLRenderDevice();
Then program crash before something render.

Re: Noesis and GLFW or other OpenGL window framework

Posted: 13 May 2017, 16:08
by Wanderer
I found solution for my 1. project where I use GLRenderDevice.
If this code is added after loop end:
	while (!glfwWindowShouldClose(window))
	{
		// code...
	}
	xaml.Reset();      // +
	device.Reset();    // +
	context.Reset();   // +
	shutdown();
	glfwTerminate();
Then program close without any crash or problem. Anyway I want ask that, is my code is ok?

Re: Noesis and GLFW or other OpenGL window framework

Posted: 17 May 2017, 03:46
by jsantos
yes, that's correct. You must destroy all noesis objects before invoking shutdown.