Hi there,
Today we are going over something that I had to do so I could be able to use Window Letterbox (so we can have a fixed resolution in our game), with Raygui.
The first is easy, just follow the example in the website: window letterbox
Then the second is just put raygui.h inside your project and test it out:
BeginTextureMode(target);
ClearBackground(RAYWHITE);
// put some button there...
if (GuiButton((Rectangle){ 24, 24, 120, 30 }, "Print")){
TraceLog(LOG_INFO,"click!");
};
EndTextureMode();If you set the window to be resizable:
SetConfigFlags(FLAG_WINDOW_RESIZABLE); then after you change the window size you will notice that the mouse loses it’s coordinates when you try to click the ui button.
The quick fix is to redefine GUI_POINTER_POSITION (which is defined inside raygui.h) before including the gui lib itself.
We also need a static variable that we update every frame, because we must calculate the “virtual” mouse position (the mouse position mapped onto the virtual game screen). In the window letterbox example, the calculations are performed as follows:
// Update virtual mouse (clamped mouse value behind game screen)
Vector2 mouse = GetMousePosition();
Vector2 virtualMouse = { 0 };
virtualMouse.x = (mouse.x - (GetScreenWidth() - (gameScreenWidth*scale))*0.5f)/scale;
virtualMouse.y = (mouse.y - (GetScreenHeight() - (gameScreenHeight*scale))*0.5f)/scale;
virtualMouse = Vector2Clamp(virtualMouse, (Vector2){ 0, 0 }, (Vector2){ (float)gameScreenWidth, (float)gameScreenHeight });Now we connect that virtual mouse to Raygui by defining:
static Vector2 gui_virtual_mouse;
#define GUI_POINTER_POSITION gui_virtual_mouse
Place this before including raygui.h, then inside your main loop compute virtualMouse and assign it:
gui_virtual_mouse = virtualMouse; then everything should work flawlessly.
Bye!
Leave a Reply