Last active
January 31, 2019 11:41
-
-
Save declank/be6cc92efae8a95e2284439d9ac6f922 to your computer and use it in GitHub Desktop.
SDL + bgfx Starting Point (no threading)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
//#define SDL_MAIN_HANDLED | |
#include <SDL.h> | |
#include <SDL_syswm.h> | |
#include <bgfx/bgfx.h> | |
#include <bgfx/platform.h> | |
inline bool setPlatformData(SDL_Window* _window) | |
{ | |
SDL_SysWMinfo wmi; | |
SDL_VERSION(&wmi.version); | |
if (!SDL_GetWindowWMInfo(_window, &wmi)) | |
{ | |
return false; | |
} | |
bgfx::PlatformData pd; | |
#if BX_PLATFORM_WINDOWS | |
pd.ndt = NULL; | |
pd.nwh = wmi.info.win.window; | |
#endif // TODO add other platforms | |
pd.context = NULL; | |
pd.backBuffer = NULL; | |
pd.backBufferDS = NULL; | |
bgfx::setPlatformData(pd); | |
printf("Platform data set.\n"); | |
return true; | |
} | |
bool initBgfx() | |
{ | |
using namespace bgfx; | |
Init init; | |
init.type = RendererType::Count; | |
init.vendorId = BGFX_PCI_ID_NONE; | |
init.resolution.width = 1280; | |
init.resolution.height = 720; | |
init.resolution.reset = BGFX_RESET_VSYNC; | |
//bool success = bgfx::init(); // TODO Cleanup | |
//bgfx::reset(1280, 720, BGFX_RESET_VSYNC); | |
return bgfx::init(init); | |
} | |
struct PosColorVertex | |
{ | |
float m_x; | |
float m_y; | |
float m_z; | |
uint32_t m_abgr; | |
static bgfx::VertexDecl ms_decl; | |
static void init() | |
{ | |
ms_decl | |
.begin() | |
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float) | |
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) | |
.end(); | |
} | |
}; | |
static PosColorVertex polygonVertices[] = | |
{ | |
{ 0.0f, 0.5f, 0.0f, 0xffff0000}, | |
{ 0.5f, -0.5f, 0.0f, 0xff00ff00}, | |
{-0.5f, -0.5f, 0.0f, 0xff0000ff}, | |
}; | |
bgfx::VertexBufferHandle vbh; | |
const char* vertexShaderCode = | |
"$input a_position\n" | |
"$input a_color0\n" | |
"$output v_color0\n" | |
"void main()\n" | |
"{\n" | |
" gl_Position = vec4(a_position, 1.0);\n" | |
" v_color0 = a_color0;\n" | |
"}\n" | |
"\0"; | |
const char* fragmentShaderCode = | |
"$input v_color0\n" | |
"void main()\n" | |
"{\n" | |
" gl_FragColor = v_color0;\n" | |
"}\n" | |
"\0"; | |
bgfx::ProgramHandle createShaderProgram(const char* vertexShaderCode, const char* fragmentShaderCode) | |
{ | |
ShaderHandle vsh = bgfx::createShader( | |
bgfx::makeRef(vertexShaderCode, sizeof(vertexShaderCode)) | |
); | |
ShaderHandle fsh = bgfx::createShader( | |
bgfx::makeRef(vertexShaderCode, sizeof(fragmentShaderCode)) | |
); | |
if (!bgfx::isValid(vsh)) | |
{ | |
printf("ERROR: Vertex Shader Invalid\n"); | |
return BGFX_INVALID_HANDLE; | |
} | |
if (!bgfx::isValid(fsh)) | |
{ | |
printf("ERROR: Fragment Shader Invalid\n"); | |
return BGFX_INVALID_HANDLE; | |
} | |
return bgfx::createProgram(vsh, fsh, true); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
SDL_Init(0 | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER); | |
SDL_Window* window = SDL_CreateWindow("ManaQuest", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, | |
1280, 720, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); | |
if (window != NULL) | |
printf("SDL Window Created\n"); | |
if (setPlatformData(window)) | |
printf("Platform data set\n"); | |
if (initBgfx()) | |
printf("BGFX Initialized\n"); | |
bgfx::setDebug(BGFX_DEBUG_TEXT | BGFX_DEBUG_STATS); | |
bgfx::setViewClear(0 | |
, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH | |
, 0x303030ff | |
, 1.0f | |
, 0 | |
); | |
// Create static VBO | |
vbh = bgfx::createVertexBuffer( | |
bgfx::makeRef(polygonVertices, sizeof(polygonVertices)), | |
PosColorVertex::ms_decl); | |
if (!bgfx::isValid(vbh)) | |
{ | |
printf("Invalid VertexBufferHandle\n"); | |
} | |
else | |
{ | |
printf("Vertex buffer created\n"); | |
} | |
// Create program for shaders | |
bgfx::ProgramHandle shaderProgram = createShaderProgram(vertexShaderCode, fragmentShaderCode); | |
if (!bgfx::isValid(shaderProgram)) | |
{ | |
printf("Invalid ProgramHandle\n"); | |
} | |
else | |
{ | |
printf("Shader program created\n"); | |
} | |
// TODO: Render a coloured cube | |
// TODO: Full screen | |
// TODO: Move renderer into separate thread | |
// TODO: Enable/disable debug overlay/text | |
//bgfx::renderFrame(); | |
bool exit = false; | |
SDL_Event event; | |
while (!exit) | |
{ | |
while (SDL_PollEvent(&event)) | |
{ | |
switch (event.type) | |
{ | |
case SDL_QUIT: | |
exit = true; | |
break; | |
case SDL_KEYDOWN: | |
{ | |
SDL_KeyboardEvent& keyEvent = event.key; | |
printf("Key press scancode %d\n", keyEvent.keysym.scancode); | |
if (keyEvent.keysym.scancode == SDL_SCANCODE_ESCAPE) | |
{ | |
exit = true; | |
} | |
} | |
break; | |
} | |
} | |
bgfx::setViewRect(0, 0, 0, 1280, 720); | |
bgfx::touch(0); | |
bgfx::dbgTextClear(); | |
bgfx::dbgTextPrintf(10, 10, 0x0f, "Hello World!"); | |
// TODO: Render polygon | |
bgfx::setVertexBuffer(0, vbh); | |
//bgfx::setIndexBuffer(ibh); | |
//bgfx::setState(state); | |
bgfx::submit(0, shaderProgram); | |
uint32_t currentFrame = bgfx::frame(); | |
//printf("Current Frame: %d\n", currentFrame); | |
} | |
//while (bgfx::RenderFrame::NoContext != bgfx::renderFrame()) {} | |
bgfx::destroy(vbh); | |
bgfx::destroy(shaderProgram); | |
bgfx::shutdown(); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment