Created
January 2, 2023 20:30
-
-
Save realtradam/7455243ed338b8234fe62e503f3f7c7d to your computer and use it in GitHub Desktop.
Basic bgfx + SDL2 example in C
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> | |
#include <stdbool.h> | |
#include "SDL2/SDL.h" | |
#include "SDL2/SDL_syswm.h" | |
#include "bgfx/c99/bgfx.h" | |
const int SCREEN_WIDTH = 640; | |
const int SCREEN_HEIGHT = 480; | |
int | |
main() | |
{ | |
SDL_Window* window = NULL; | |
SDL_Surface* screenSurface = NULL; | |
if(SDL_Init(SDL_INIT_VIDEO) < 0) | |
{ | |
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); | |
exit(EXIT_FAILURE); | |
} | |
window = SDL_CreateWindow( | |
"SDL Tutorial", | |
SDL_WINDOWPOS_UNDEFINED, | |
SDL_WINDOWPOS_UNDEFINED, | |
SCREEN_WIDTH, | |
SCREEN_HEIGHT, | |
SDL_WINDOW_SHOWN | |
); | |
if(window == NULL) | |
{ | |
printf("Window could not be created! SDL_Error %s\n", SDL_GetError()); | |
exit(EXIT_FAILURE); | |
} | |
SDL_SysWMinfo wmi; | |
SDL_VERSION(&wmi.version); | |
if (!SDL_GetWindowWMInfo(window, &wmi)) { | |
printf("SDL_Error %s\n", SDL_GetError()); | |
exit(EXIT_FAILURE); | |
} | |
bgfx_render_frame(-1); | |
bgfx_platform_data_t pd; | |
memset(&pd, 0, sizeof(bgfx_platform_data_t)); | |
pd.ndt = wmi.info.x11.display; | |
pd.nwh = (void*)(uintptr_t)wmi.info.x11.window; | |
bgfx_init_t init = {0}; | |
bgfx_init_ctor(&init); | |
init.type = BGFX_RENDERER_TYPE_COUNT; // auto determine renderer | |
init.resolution.width = SCREEN_WIDTH; | |
init.resolution.height = SCREEN_HEIGHT; | |
init.resolution.reset = BGFX_RESET_VSYNC; | |
init.platformData = pd; | |
bgfx_init(&init); | |
bgfx_reset(SCREEN_WIDTH, SCREEN_HEIGHT, BGFX_RESET_VSYNC, init.resolution.format); | |
bgfx_set_debug(BGFX_DEBUG_TEXT); | |
bgfx_set_view_clear( | |
0, | |
BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, | |
0x443355FF, | |
1.0f, | |
0 | |
); | |
bgfx_set_view_rect(0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); | |
bgfx_touch(0); | |
bgfx_frame(false); | |
SDL_Event e; | |
bool quit = false; | |
while(!quit) | |
{ | |
while(SDL_PollEvent(&e)) | |
{ | |
if(e.type == SDL_QUIT) | |
{ | |
quit = true; | |
} | |
} | |
bgfx_dbg_text_printf(0, 2, 0x6f, "Description: Initialization and debug text."); | |
bgfx_touch(0); | |
bgfx_frame(false); | |
} | |
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