Skip to content

Instantly share code, notes, and snippets.

@jesusdesantos
Created January 8, 2019 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesusdesantos/b3623ffe0a1ec87b9424aa72617029ec to your computer and use it in GitHub Desktop.
Save jesusdesantos/b3623ffe0a1ec87b9424aa72617029ec to your computer and use it in GitHub Desktop.
NoesisGUI minimal sample using GLUT
#include <GLUT/glut.h>
#include <Noesis_pch.h>
static Noesis::IView* _view;
static void NoesisInit()
{
auto logHandler = [](const char*, uint32_t, uint32_t level, const char*, const char* message)
{
// [TRACE] [DEBUG] [INFO] [WARNING] [ERROR]
const char* prefixes[] = { "T", "D", "I", "W", "E" };
printf("[NOESIS/%s] %s\n", prefixes[level], message);
};
// Noesis initialization. This must be the first step before using any NoesisGUI functionality
Noesis::GUI::Init(nullptr, logHandler, nullptr);
// For simplicity purposes we are not using resource providers in this sample. ParseXaml() is
// enough if there is no extra XAML dependencies
Noesis::Ptr<Noesis::Grid> xaml(Noesis::GUI::ParseXaml<Noesis::Grid>(R"(
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Grid.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="#FF123F61"/>
<GradientStop Offset="0.6" Color="#FF0E4B79"/>
<GradientStop Offset="0.7" Color="#FF106097"/>
</LinearGradientBrush>
</Grid.Background>
<Viewbox>
<StackPanel Margin="50">
<Button Content="Hello World!" Margin="0,30,0,0"/>
<Rectangle Height="5" Margin="-10,20,-10,0">
<Rectangle.Fill>
<RadialGradientBrush>
<GradientStop Offset="0" Color="#40000000"/>
<GradientStop Offset="1" Color="#00000000"/>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
</StackPanel>
</Viewbox>
</Grid>
)"));
// View creation to render and interact with the user interface
// We transfer the ownership to a global pointer instead of a Ptr<> because there is no way
// in GLUT to do shutdown and we don't want the Ptr<> to be released at global time
_view = Noesis::GUI::CreateView(xaml).GiveOwnership();
_view->SetIsPPAAEnabled(true);
// Renderer initialization with an OpenGL device
_view->GetRenderer()->Init(NoesisApp::GLFactory::CreateDevice());
}
static void DisplayFunc(void)
{
// Update view (layout, animations, ...)
_view->Update(glutGet(GLUT_ELAPSED_TIME) / 1000.0);
// Offscreen rendering phase populates textures needed by the on-screen rendering
_view->GetRenderer()->UpdateRenderTree();
_view->GetRenderer()->RenderOffscreen();
// If you are going to render here with your own engine you need to restore the GPU state
// because noesis changes it. In this case only framebuffer and viewport need to be restored
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
glClearColor(0.0f, 0.0f, 0.25f, 0.0f);
glClearStencil(0);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Rendering is done in the active framebuffer
_view->GetRenderer()->Render();
glutSwapBuffers();
glutPostRedisplay();
}
static void ReshapeFunc(int width, int height)
{
_view->SetSize(width, height);
}
static void MouseMoveFunc(int x, int y)
{
_view->MouseMove(x, y);
}
static void MouseFunc(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
{
if (state == GLUT_DOWN)
{
_view->MouseButtonDown(x, y, Noesis::MouseButton_Left);
}
else
{
_view->MouseButtonUp(x, y, Noesis::MouseButton_Left);
}
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_STENCIL);
glutInitWindowSize(1000, 600);
glutCreateWindow("NoesisGUI - GLUT integration");
Noesis_Init();
glutDisplayFunc(&DisplayFunc);
glutReshapeFunc(&ReshapeFunc);
glutPassiveMotionFunc(&MouseMoveFunc);
glutMouseFunc(&MouseFunc);
glutMainLoop();
return 0;
}
@RedactedProfile
Copy link

RedactedProfile commented Sep 11, 2020

You're a lifesaver. I really dislike when official tutorials get so far up their own ass with their own example framework. I starve for simple minimalistic examples such as this one, to help get land me on my feet running with something.

@jesusdesantos
Copy link
Author

You welcome!

Latest version always available at https://github.com/Noesis/Tutorials/tree/master/Samples/IntegrationGLUT

@RedactedProfile
Copy link

You welcome!

Latest version always available at https://github.com/Noesis/Tutorials/tree/master/Samples/IntegrationGLUT

Awesome! Thank you again for your work in this :) will help quite a bit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment