Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GLFW entry
#include "render.h"
#include <GLFW/glfw3.h>
#include <bgfxplatform.h>
#include <bx/thread.h>
#include <iostream>
#if !defined(ENTRY_DEFAULT_WIDTH) && !defined(ENTRY_DEFAULT_HEIGHT)
# define ENTRY_DEFAULT_WIDTH 1280
# define ENTRY_DEFAULT_HEIGHT 720
#elif !defined(ENTRY_DEFAULT_WIDTH) || !defined(ENTRY_DEFAULT_HEIGHT)
# error "Both ENTRY_DEFAULT_WIDTH and ENTRY_DEFAULT_HEIGHT must be defined."
#endif // ENTRY_DEFAULT_WIDTH
namespace render
{
struct RenderThreadEntry
{
int m_argc;
char** m_argv;
GLFWwindow* m_window;
static int32_t threadFunc(void* _userData);
};
struct RenderContext
{
RenderContext()
: m_width(ENTRY_DEFAULT_WIDTH)
, m_height(ENTRY_DEFAULT_HEIGHT)
, m_aspectRatio(16.0f/9.0f)
, m_mouseLock(false)
{
}
static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
void run(int _argc, char** _argv)
{
BX_TRACE("Here!...");
m_mte.m_argc = _argc;
m_mte.m_argv = _argv;
if (!glfwInit())
return; // freak the fuck out - syserror
m_window = glfwCreateWindow(m_width, m_height, "derelict", NULL, NULL);
if (!m_window)
{
glfwTerminate();
return; // ahhhhhhhhhhhh freak out - syserror
}
bgfx::glfwSetWindow(m_window);
bgfx::renderFrame();
glfwMakeContextCurrent(m_window);
glfwSetKeyCallback(m_window, keyCallback);
m_mte.m_window = m_window;
m_thread.init(RenderThreadEntry::threadFunc, &m_mte);
bool exit = false;
while (!exit)
{
bgfx::renderFrame();
while (!glfwWindowShouldClose(m_window))
{
glfwPollEvents();
}
exit = true;
}
while (bgfx::RenderFrame::NoContext != bgfx::renderFrame() ) {};
m_thread.shutdown();
glfwDestroyWindow(m_window);
glfwTerminate();
}
RenderThreadEntry m_mte;
bx::Thread m_thread;
// bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc;
// SDL_Window* m_window[ENTRY_CONFIG_MAX_WINDOWS];
// uint32_t m_flags[ENTRY_CONFIG_MAX_WINDOWS];
GLFWwindow* m_window;
uint32_t m_width;
uint32_t m_height;
float m_aspectRatio;
int32_t m_mx;
int32_t m_my;
bool m_mouseLock;
};
int32_t RenderThreadEntry::threadFunc(void* _userData)
{
RenderThreadEntry* self = (RenderThreadEntry*)_userData;
int32_t result = main(self->m_argc, self->m_argv);
glfwSetWindowShouldClose(self->m_window, true);
return result;
}
int main(int _argc, char** _argv)
{
int32_t result = ::_main_(_argc, _argv);
return result;
}
static RenderContext s_ctx;
}
int main(int _argc, char** _argv)
{
using namespace render;
s_ctx.run(_argc, _argv);
return 0;
}
#include "render.h"
#include <bgfx.h>
#include <iostream>
int _main_(int /*_argc*/, char** /*_argv*/)
{
uint32_t width = 1280;
uint32_t height = 720;
uint32_t debug = BGFX_DEBUG_TEXT;
uint32_t reset = BGFX_RESET_VSYNC;
bgfx::init();
bgfx::reset(width, height, reset);
// Enable debug text.
bgfx::setDebug(debug);
// Set view 0 clear state.
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
, 0x303030ff
, 1.0f
, 0
);
while (1)
{
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, width, height);
// This dummy draw call is here to make sure that view 0 is cleared
// if no other draw calls are submitted to view 0.
bgfx::submit(0);
// Use debug font to print information about this example.
bgfx::dbgTextClear();
bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/00-helloworld");
bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Initialization and debug text.");
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
}
// Shutdown bgfx.
bgfx::shutdown();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment