Skip to content

captain

IsaacShelton edited this page Nov 16, 2022 · 7 revisions

captain

2.7/captain.adept is an API for doing basic 2D graphics.

This file is only supported on Windows, MacOS, and x86_64-ELF GNU-Linux.

Overview

Smallest App

This app creates an empty window with no title.

import captain

func main {
    captStart()
}

Example App

This app loads a texture sprite.png and draws it where the mouse cursor is.

pragma compiler_version '2.5'

import where
import basics
import captain

struct GlobalApplicationData (
    sprite CaptTexture,
    target_x float,
    target_y float
)
globals GlobalApplicationData

func main {
    // Configure Window
    captAlwaysOnTop(true)
    
    // Hook Callbacks
    captOnSetup(func &onSetup)
    captOnExit(func &onExit)
    captOnStep(func &onStep)
    captOnDraw(func &onDraw)
    
    // Start main application loop
    captStart('My Window Title', 640, 480, /*fullscreen*/ false)
}

func onSetup {
    // (Called before first iteration of main application loop)

    // Load texture from "sprite.png" in the current directory
    // NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    // For small pixel art,     using 'CaptTexture(filename, false)' is better
    // For large non-pixel art, using 'CaptTexture(filename, true) ' is better
    // We'll assume 'sprite.png' is pixel art, so we'll disable smooth blending with 'false'

    globals.sprite = CaptTexture(where() + "sprite.png", /*true == smooth blending, false == nearest pixel*/ false)
    
    // Show an error message if the texture couldn't be loaded
    unless globals.sprite.isValid() {
        printf("ERROR: Failed to load '%Ssprite.png'\n", where())
    }

    // Change the view to a custom view if desired
    // The most common is 'captCustomViewBasedOnHeight', which will
    // have a fixed amount of units represent the height of the view
    // and a variable number of units represent the width of the view.
    // So
    //    captViewWidth()  == 480.0f * aspect_ratio
    //    captViewHeight() == 480.0f
    captCustomViewBasedOnHeight(480.0f)

    // NOTE: 'captDefaultView()' is the same as
    // 'captCustomViewBasedOnHeight(480.0f)' when using '2.7/captain.adept'.
    // Other options include:
    // - captCustomView(width, height)

    // If no custom view is set, then captDefaultView() will be used.
    // The view can be changed even after `onSetup`.
}

func onExit {
    // Called when main application loop exits
    globals.sprite.destroy()
}

func onStep {
    // (Called once per frame before rendering)

    // Set globals.target_x and globals.target_y to the mouse position (in view coords)
    // We will use these values later inside `onDraw`
    captMouseViewPosition(&globals.target_x, &globals.target_y)

    // Don't allow globals.target_x and globals.target_y to be outside the window
    globals.target_x = clamp(globals.target_x, 0.0f, captViewWidth() as float)
    globals.target_y = clamp(globals.target_y, 0.0f, captViewHeight() as float)
}

func onDraw {
    // Called once per frame to draw graphics
    center_x float = globals.target_x
    center_y float = globals.target_y
    width float = 256.0f
    height float = 256.0f
    captDrawTexture(globals.sprite, center_x - width / 2.0f, center_y - height / 2.0f, width, height)
}

Functions

  • func captPrepare() successful

    Initializes the captain API.

    Invoking this is optional, since other setup-related procedures for captain will automatically take care of it.

    De-initialization of the API is handled automatically inside of captStart.

    [view src]

  • func captStart(in fullscreen bool = false) void

    Starts captain by initializing the window.

    [view src]

  • func captStart(in title *ubyte = '', in fullscreen bool = false) void

    Starts captain by initializing the window.

    [view src]

  • func captStart(in title *ubyte, in width, height int, in fullscreen bool)

    Starts captain by initializing the window.

    [view src]

  • func captEnsurePrepared() void

    Ensures the captain API is initialized.

    [view src]

  • func captDefaultView() void

    Sets the view to the default view. The default view is a view with a height of 480.0f units.

    [view src]

  • func captViewWidth() float

    Returns the width (in units) of the current view.

    [view src]

  • func captViewHeight() float

    Returns the height (in units) of the current view.

    [view src]

  • func captFrameWidth() int

    Returns the width (in pixels) of the window.

    [view src]

  • func captFrameHeight() int

    Returns the height (in pixels) of the window.

    [view src]

  • func captCustomView(width, height float) void

    Sets the view to a custom view that has a fixed width and height.

    [view src]

  • func captCustomViewBasedOnHeight(height float) void

    Sets the view to a custom view that has a fixed height and variable width.

    [view src]

  • func captMouseFramePosition(out x, y *float) void

    Sets x and y to the current mouse position (in pixels).

    [view src]

  • func captMouseFramePosition(out x, y *double) void

    Sets x and y to the current mouse position (in pixels).

    [view src]

  • func captMouseViewPosition(out x, y *float) void

    Sets x and y to the current mouse position (in units) with respect to the current view.

    [view src]

  • func captResizable(resizable bool) void

    Sets whether the window will be resizable. Must be done before captStart.

    [view src]

  • func captAlwaysOnTop(always_on_top bool) void

    Sets whether the window will be always be on top. Must be done before captStart.

    [view src]

  • func captMultisample(multisample_rate int) void

    Sets the target multisample rate of the window. Must be done before captStart.

    [view src]

  • func captHideCursor(hide bool) void

    Hides the mouse cursor. Cannot be done before captStart.

    [view src]

  • func captOnSetup(in on_setup_func func() void) void

    Sets the on-setup function for the app.

    The on-setup function will be called before the any other callbacks. It should be used for loading textures and other operations that require captain to be running. Resources allocated in the on-setup function that need to be destroyed should be destroyed in the on-exit function.

    [view src]

  • func captOnExit(in on_exit_func func() void) void

    Sets the on-exit function for the app.

    The on-exit function will be the last callback invoked. It should be used to destroy resources that need to be destroyed before captain exits.

    [view src]

  • func captOnDraw(in on_draw_func func() void) void

    Sets the on-draw function for the app.

    The on-draw function will be called each frame after the on-step function. It should be used to draw textures and other drawables.

    [view src]

  • func captOnStep(in on_step_func func() void) void

    Sets the on-step function for the app.

    The on-step function will be called each frame before the on-draw function. It should be used to perform app logic.

    [view src]

  • func captOnStep(in on_step_func func() void, in step_fps uint) void

    Sets the on-step function for the app.

    The on-step function will be called each frame (step_fps times a second) before the on-draw function. It should be used to perform app logic. If step_fps is zero, then the step fps will be practically unbounded.

    [view src]

  • func captOnClick(in on_click_func func(float, float, int) void, use_mouse_view_coords bool) void

    Sets the on-click function for the app.

    The on-click function will be called when a mouse button is clicked. The first two parameters of the callback are the x and y of the mouse click. The third parameter of the callback is which mouse button was pressed (1 is left, 2 is right, 3 is middle, etc.). If use_mouse_view_coords is true, then the mouse coordinates supplied to the callback will be in view coords (units) instead of frame coords (pixels).

    [view src]

  • func captOnRelease(in on_release_func func(float, float, int) void, use_mouse_view_coords bool) void

    Sets the on-release function for the app.

    The on-release function will be called when a mouse button is released. The first two parameters of the callback are the x and y of the mouse. The third parameter of the callback is which mouse button was released (1 is left, 2 is right, 3 is middle, etc.). If use_mouse_view_coords is true, then the mouse coordinates supplied to the callback will be in view coords (units) instead of frame coords (pixels).

    [view src]

  • func captOnKey(in on_key_func func(int, int, int, int) void) void

    Sets the on-key function for the app.

    The on-key function will be called when a key is pressed/released/repeated. The parameters to the callback are:

    • key - Which key was pressed (e.g. GLFW_KEY_W)
    • scancode - Platform-specific unique key identifier
    • action - How the key event was triggered (e.g. GLFW_PRESS, GLFW_RELEASE, GLFW_REPEAT)
    • mods - Modifier keys (e.g. GLFW_MOD_SHIFT, GLFW_MOD_CONTROL, GLFW_MOD_ALT, GLFW_MOD_SUPER, GLFW_MOD_CAPS_LOCK, GLFW_MOD_NUM_LOCK)

    [view src]

  • func captOnChar(in on_char_func func(uint) void) void

    Sets the on-character function for the app.

    The on-character function will be called when a character is typed. The parameters to the callback are:

    • codepoint - Native Endian UTF-32 Character

    [view src]

  • func captOnScroll(in on_scroll_func func(float, float) void) void

    Sets the on-scroll function for the app.

    The on-scroll function will be called when the scroll wheel is rotated. The parameters to the callback are:

    • xoffset - Scroll X-Offset
    • yoffset - Scroll Y-Offset

    [view src]

  • func captClearColor(in color CaptColor) void

    Sets the background clear-color of the window and then clears the window.

    [view src]

  • func captDrawOpacity(in value float) void

    Changes the drawing opacity. The opacity value should be in the range [0.0f, 1.0f].

    [view src]

  • func captKeyHeld(key int) bool

    Returns whether a key is currently held (e.g. GLFW_KEY_W).

    [view src]

  • func alias captRandomize() => randomize

    Initializes random number generation.

    [view src]

  • func alias captRandom() => normalizedRandom

    Returns a random double in the range [0.0f, 1.0f).

    [view src]

Structs

struct CaptColor (r, g, b, a float)
  • func CaptColor(r, g, b float, a float = 1.0f) CaptColor

    Constructs a CaptColor from red, green, blue values each in the range [0.0f, 1.0f]. An alpha value can optionally be supplied ranging from [0.0f, 1.0f].

    [view src]

  • func CaptColor(r, g, b int, a int = 255) CaptColor

    Constructs a CaptColor from red, green, blue values each in the range [0, 255]. An alpha value can optionally be supplied ranging from [0, 255].

    [view src]

struct CaptTexture (id uint)
  • func CaptTexture(filename String, approximate bool = true) CaptTexture

    Constructs a CaptTexture. When approximate is true, smooth blending will be used.

    [view src]

  • func CaptTexture(filename *ubyte, approximate bool = true) CaptTexture

    Constructs a CaptTexture. When approximate is true, smooth blending will be used.

    [view src]

  • func destroy(this *CaptTexture) void

    Destroys a CaptTexture.

    [view src]

  • func isValid(this *CaptTexture) bool

    Returns whether a CaptTexture is a valid.

    [view src]

  • func invalidate(this *CaptTexture) void

    Makes a CaptTexture invalid. Any previous texture should be destroyed before invalidating the texture if it will become inaccessible.

    [view src]

  • func load(this *CaptTexture, filename String, approximate bool = true) void

    Constructs a CaptTexture. When approximate is true, smooth blending will be used.

    [view src]

  • func load(this *CaptTexture, filename *ubyte, approximate bool = true) void

    Constructs a CaptTexture. When approximate is true, smooth blending will be used.

    [view src]

  • func captBindTexture(texture POD CaptTexture) void

    Manually binds a CaptTexture.

    [view src]

  • func captDrawTexture(texture POD CaptTexture, in x, y, w, h float) void

    Draws a CaptTexture at a given (x, y) position with a given width and height. The x and y coords are from the top left, with positive x pointing right and positive y pointing down.

    [view src]

  • func captDrawTextureUsingModel(model POD CaptModel, texture POD CaptTexture, in x, y float) void

    Draws a CaptTexture using a model at a given (x, y) position. The x and y coords are from the top left, with positive x pointing right and positive y pointing down.

    [view src]

  • func captDrawTextureUsingModelAndTransformation(model POD CaptModel, texture POD CaptTexture, transformation Matrix4f) void

    Draws a CaptTexture using a model and a transformation. This can be used to draw more complicated arrangements such as rotations, skewing, and 3D objects.

    [view src]

struct CaptModel (vao, vertices_vbo, uvs_vbo, texture_id GLuint, triangle_count int)
  • func create(this *CaptModel, points *float, points_length usize, uvs *float, uvs_length usize) void

    Constructs a CaptModel from a primitive array of points and a primitive array of UV coords.

    [view src]

  • func destroy(this *CaptModel) void

    Destroys a CaptModel.

    [view src]

  • func draw(this *CaptModel) void

    Manually draws a CaptModel with the currently bound CaptTexture.

    [view src]

  • func CaptModel(points <float> List, uvs <float> List) CaptModel

    Constructs a CaptModel from a list of points and a list of UV coords.

    [view src]

  • func CaptModel(in w, h float, in x_flip, y_flip bool = false) CaptModel

    Constructs a rectangular CaptModel. The UV coords can optionally be flipping horizontally/vertically for simple flipped textures.

    [view src]

struct CaptShader (program, vertex, fragment GLuint)
  • func create(this *CaptShader, vertex_code, fragment_code String) void

    Constructs a CaptShader from GLSL vertex and fragment shader code.

    [view src]

  • func destroy(this *CaptShader) void

    Destroys a CaptShader.

    [view src]

  • func use(this *CaptShader) void

    Binds a CaptShader.

    [view src]

  • func getUniformLocation(this *CaptShader, name *ubyte) GLint

    Returns the OpenGL uniform location of a uniform in a CaptShader.

    [view src]

  • func uploadFloat(this *CaptShader, location GLint, value float) void

    Uploads a float value for an OpenGL uniform variable.

    [view src]

  • func uploadMatrix4f(this *CaptShader, location GLint, matrix *Matrix4f) void

    Uploads a Matrix4f value for an OpenGL uniform variable.

    [view src]

  • func uploadMatrix4f(this *CaptShader, location GLint, matrix Matrix4f) void

    Uploads a Matrix4f value for an OpenGL uniform variable.

    [view src]

  • func CaptShader(vertex_shader_code, fragment_shader_code String) CaptShader

    Constructs a CaptShader from GLSL vertex and fragment shader code.

    [view src]

Clone this wiki locally