Wanderer
Topic Author
Posts: 168
Joined: 08 May 2017, 18:36

Noesis and GLFW or other OpenGL window framework

13 May 2017, 13:14

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.
 
Wanderer
Topic Author
Posts: 168
Joined: 08 May 2017, 18:36

Re: Noesis and GLFW or other OpenGL window framework

13 May 2017, 16:08

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?
 
User avatar
jsantos
Site Admin
Posts: 2905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Noesis and GLFW or other OpenGL window framework

17 May 2017, 03:46

yes, that's correct. You must destroy all noesis objects before invoking shutdown.

Who is online

Users browsing this forum: No registered users and 1 guest