Looking for the proper way to resize an SDL2 window/canvas that is coming from Emscripten. Currently I'm adding an event listener on the JS 'resize' event and sending the Canvas parent's client width + height to Emscripten, then updating a width and a height variable which are called on each render.
This is producing weird results - the scale is always off, the actual usable SDL2 area isn't changed, and the pointer events no longer line up with SDL.
My window size variables are:
int canvasWidth = 800;
int canvasHeight = 600;
This is my init code:
void Init()
{
int i;
for (i = 0; i < 256; i++)
gPressed[i] = gWasPressed[i] = 0;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
{
fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
SDL_Quit();
exit(0);
}
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
gSDLWindow = SDL_CreateWindow(
"",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
800,
600,
flags);
SDL_SetRenderDrawColor(SDL_GetRenderer(gSDLWindow), 0, 0, 0, 0);
SDL_GLContext glcontext = SDL_GL_CreateContext(gSDLWindow);
SDL_GL_SetSwapInterval(1);
glViewport(0, 0, canvasWidth, canvasHeight);
glewInit();
InitImGui();
framework_init_flat();
framework_init_tex();
atexit(SDL_Quit);
}
And this is my per-frame update code:
void Update()
{
ImGuiIO& io = ImGui::GetIO();
// Setup resolution (every frame to accommodate for window resizing)
io.DisplaySize = ImVec2((float)canvasWidth, (float)canvasHeight);
SDL_SetWindowSize(gSDLWindow, canvasWidth, canvasHeight);
// Setup time step
static double time = 0.0f;
const double current_time = SDL_GetTicks() / 1000.0;
if (current_time == time)
return;
io.DeltaTime = (float)(current_time - time);
time = current_time;
io.MousePos = ImVec2((float)gUIState.mousex, (float)gUIState.mousey);
io.MouseDown[0] = gUIState.mousedown != 0;
io.MouseDown[1] = 0;
if (gUIState.scroll)
{
io.MouseWheel += (float)gUIState.scroll * 0.5f;
gUIState.scroll = 0;
}
if (gUIState.textinput[0])
{
io.AddInputCharactersUTF8(gUIState.textinput);
gUIState.textinput[0] = 0;
}
for (int n = 0; n < 256; n++)
io.KeysDown[n] = gPressed[n] != 0;
io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
}