multiwin.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * src/multiwin.c
  3. * https://gitlab.com/bztsrc/smgui
  4. *
  5. * Copyright (C) 2024 bzt (bztsrc@gitlab), MIT license
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to
  9. * deal in the Software without restriction, including without limitation the
  10. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  11. * sell copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY
  20. * DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  22. * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * @brief State-Mode GUI multiple window demo program
  25. */
  26. /* GLFW uses a hidden global state. So to support multiple windows, we have to initialize and terminate outself once
  27. * SMGUI itself doesn't care how many contexts there are */
  28. #define UI_BACKEND_INITIALIZED
  29. #define UI_IMPLEMENTATION
  30. #include <ui.h>
  31. int main(int argc, char **argv)
  32. {
  33. /* strings are gathered into an array so that you can change the language on-the-fly */
  34. char *lang[] = { "Multiple windows demo" };
  35. /* variables to store game states */
  36. int win_closed = 0;
  37. /* form referencing those variables */
  38. ui_event_t *evt;
  39. ui_t ctx1, ctx2, ctx3;
  40. ui_form_t form[1] = { { .type = UI_END } };
  41. /* GLFW uses a hidden global state. So to support multiple windows, we have to initialize and terminate outself once */
  42. glfwInit();
  43. /* open multiple windows */
  44. ui_init(&ctx1, 1, lang, 640, 480, NULL);
  45. ui_init(&ctx2, 1, lang, 640, 480, NULL);
  46. ui_init(&ctx3, 1, lang, 640, 480, NULL);
  47. while(win_closed < 7) {
  48. /* if first window isn't closed yet, get events, and if user clicked X or Escape pressed, close it */
  49. if(!(win_closed & 1) && (!(evt = ui_event(&ctx1, form)) || (evt->type == UI_EVT_KEY && evt->key[0] == 0x1b))) {
  50. ui_free(&ctx1);
  51. win_closed |= 1;
  52. }
  53. /* if second window isn't closed yet, get events, and if user clicked X or Escape pressed, close it */
  54. if(!(win_closed & 2) && (!(evt = ui_event(&ctx2, form)) || (evt->type == UI_EVT_KEY && evt->key[0] == 0x1b))) {
  55. ui_free(&ctx2);
  56. win_closed |= 2;
  57. }
  58. /* if third window isn't closed yet, get events, and if user clicked X or Escape pressed, close it */
  59. if(!(win_closed & 4) && (!(evt = ui_event(&ctx3, form)) || (evt->type == UI_EVT_KEY && evt->key[0] == 0x1b))) {
  60. ui_free(&ctx3);
  61. win_closed |= 4;
  62. }
  63. }
  64. /* GLFW uses a hidden global state. So to support multiple windows, we have to initialize and terminate outself once */
  65. glfwTerminate();
  66. (void)argc; (void)argv;
  67. return 0;
  68. }