Class Window

java.lang.Object
io.github.ocelot.window.Window
All Implemented Interfaces:
AutoCloseable, org.lwjgl.system.NativeResource

public class Window extends Object implements org.lwjgl.system.NativeResource

Manages and tracks certain key events for windows. Events are passed to all registered listeners.

In order to use graphics, create the capabilities for the desired API after calling create(CharSequence).

For example, to use LWJGL OpenGL use org.lwjgl.opengl.GL.createCapabilities()

See Also:
  • Method Details

    • addListener

      public void addListener(WindowEventListener listener)
      Adds the specified listener to the event list.
      Parameters:
      listener - The listener to add
    • removeListener

      public void removeListener(WindowEventListener listener)
      Removes the specified listener to the event list.
      Parameters:
      listener - The listener to add
    • create

      public Window create(CharSequence title)
      Creates this window with GLFW.
      Parameters:
      title - The title of the window
    • create

      public Window create(CharSequence title, long share)
      Creates this window with GLFW.
      Parameters:
      title - The title of the window
      share - The id off the window to share context with or 0L to create a new context
      See Also:
    • createMouseHandler

      public MouseHandler createMouseHandler()
      Creates the default implementation of mouse tracking and automatically adds it to the listeners.
      Returns:
      A new mouse handler that tracks events for this window
    • createKeyboardHandler

      public KeyboardHandler createKeyboardHandler()
      Creates the default implementation of key tracking and automatically adds it to the listeners.
      Returns:
      A new keyboard handler that tracks events for this window
    • swapBuffers

      public void swapBuffers()
      Updates the contents of the window. Called by WindowManager.update() automatically.
    • toggleFullscreen

      public void toggleFullscreen()
      Toggles the fullscreen flag.
    • free

      public void free()
      Specified by:
      free in interface org.lwjgl.system.NativeResource
    • getHandle

      public long getHandle()
      Returns:
      The GLFW id of the window
    • getX

      public int getX()
      Returns:
      The absolute x position of the window
    • getY

      public int getY()
      Returns:
      The absolute y position of the window
    • getWindowWidth

      public int getWindowWidth()
      Returns:
      The width of the physical window. getFramebufferWidth() should be used for drawing logic
    • getWindowHeight

      public int getWindowHeight()
      Returns:
      The width of the physical window. getFramebufferHeight() should be used for drawing logic
    • getFramebufferWidth

      public int getFramebufferWidth()
      Returns:
      The width of the canvas in the window
    • getFramebufferHeight

      public int getFramebufferHeight()
      Returns:
      The height of the canvas in the window
    • isFullscreen

      public boolean isFullscreen()
      Returns:
      Whether the window is currently full screen
    • getSwapInterval

      public int getSwapInterval()
      Returns:
      The number of monitor frames to wait before continuing execution when swapBuffers() is called
    • isVsync

      public boolean isVsync()
      Returns:
      Whether vsync is enabled
    • getTitle

      @Nullable public @Nullable CharSequence getTitle()
      Returns:
      The title of the window or null if the window has not been initialized yet
    • isFocused

      public boolean isFocused()
      Returns:
      If the window is currently focused
    • isClosed

      public boolean isClosed()
      Returns:
      If the window is requesting to close
    • getClipboard

      @Nullable public @Nullable String getClipboard()
      Returns:
      The current string on the clipboard or null if there is nothing
    • setFullscreen

      public void setFullscreen(boolean fullscreen)
      Sets the window fullscreen or not. Will automatically handle special mac handling.
      Parameters:
      fullscreen - Whether to enter fullscreen
    • setSwapInterval

      public void setSwapInterval(int swapInterval)
      Sets the number of monitor frames to wait before continuing execution when swapBuffers() is called.
      Parameters:
      swapInterval - The frame count
    • setVsync

      public void setVsync(boolean vsync)
      Sets the window to use vsync.
      Parameters:
      vsync - Whether vsync should be enabled
    • setTitle

      public void setTitle(CharSequence title)
      Updates the window title to the specified string.
      Parameters:
      title - The new window title
    • center

      public void center()
      Centers this window on the current monitor.
    • setPosition

      public void setPosition(int x, int y)
      Sets the absolute position of the window.
      Parameters:
      x - The new x position
      y - The new y position
    • setSize

      public void setSize(int width, int height)
      Sets the size of the window.
      Parameters:
      width - The new x size
      height - The new y size
    • setClosing

      public void setClosing(boolean closing)
      Marks the window as closing or not.
      Parameters:
      closing - Whether the window should close
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • setMacIcon

      public static void setMacIcon(InputStream stream) throws IOException
      Reads the icon from the stream and sends it to the OS to set the mac icon. This should only be called on a mac.
      Parameters:
      stream - The image file stream
      Throws:
      IOException - If any error occurs reading the image