Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git

window.h (2426B) · raw


      1 /// @file platform/window.h
      2 /// @brief Platform-independent window interface.
      3 ///
      4 /// The implementation is chosen at build time by the name of the file:
      5 /// `window_mac.mm` for macOS, `window_win.cpp` for Windows. A window is created
      6 /// and driven from the main thread only.
      7 #pragma once
      8 
      9 #include "core/types.h"
     10 
     11 namespace kumo {
     12 
     13 struct FrameBuffer;
     14 class Keyboard;
     15 
     16 /// @brief Opaque window handle; the definition lives in the platform file.
     17 struct Window;
     18 
     19 /// @brief Parameters for window_create().
     20 struct WindowDesc {
     21   const char* title;  ///< UTF-8 window title.
     22   int width;          ///< Content width in points.
     23   int height;         ///< Content height in points.
     24 };
     25 
     26 /// @brief Creates a window, shows it and brings it to the front.
     27 ///
     28 /// The first call also sets up the application: on macOS its menu (with Cmd+Q)
     29 /// and the rest of what a program needs to be a normal windowed application.
     30 ///
     31 /// @param desc Window parameters.
     32 /// @return The new window, or `nullptr` on failure.
     33 Window* window_create(const WindowDesc& desc);
     34 
     35 /// @brief Closes the window and frees it. Passing `nullptr` is allowed.
     36 void window_destroy(Window* window);
     37 
     38 /// @brief Processes every pending event of the window system without blocking.
     39 ///
     40 /// Call once per frame.
     41 void window_poll_events(Window* window);
     42 
     43 /// @brief Returns the keyboard state owned by the window.
     44 ///
     45 /// Use on the main thread. The reference is valid until window_destroy().
     46 Keyboard& window_keyboard(Window* window);
     47 
     48 /// @brief Tells whether the user asked to close the window.
     49 ///
     50 /// True after the close button is pressed or Escape is hit. The window stays
     51 /// open until window_destroy(), so the caller decides when to shut down.
     52 bool window_should_close(const Window* window);
     53 
     54 /// @brief Shows a frame in the window.
     55 ///
     56 /// The frame is scaled to fill the content of the window; the window system
     57 /// does the scaling, with nearest-neighbor filtering so that pixels stay sharp.
     58 /// The call waits for the display to be ready for the next frame, so a loop
     59 /// that presents once per iteration runs at the refresh rate of the display.
     60 ///
     61 /// @param framebuffer The pixels to show. Its size may change between calls.
     62 void window_present(Window* window, const FrameBuffer& framebuffer);
     63 
     64 /// @brief Changes the title of the window.
     65 ///
     66 /// @param title UTF-8 text.
     67 void window_set_title(Window* window, const char* title);
     68 
     69 }  // namespace kumo