-
-
Notifications
You must be signed in to change notification settings - Fork 9
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.
This app creates an empty window with no title.
import captain
func main {
captStart()
}
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)
}
-
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
. -
func captStart(in fullscreen bool = false) void
Starts captain by initializing the window.
-
func captStart(in title *ubyte = '', in fullscreen bool = false) void
Starts captain by initializing the window.
-
func captStart(in title *ubyte, in width, height int, in fullscreen bool)
Starts captain by initializing the window.
-
func captEnsurePrepared() void
Ensures the captain API is initialized.
-
func captDefaultView() void
Sets the view to the default view. The default view is a view with a height of
480.0f
units. -
func captViewWidth() float
Returns the width (in units) of the current view.
-
func captViewHeight() float
Returns the height (in units) of the current view.
-
func captFrameWidth() int
Returns the width (in pixels) of the window.
-
func captFrameHeight() int
Returns the height (in pixels) of the window.
-
func captCustomView(width, height float) void
Sets the view to a custom view that has a fixed width and height.
-
func captCustomViewBasedOnHeight(height float) void
Sets the view to a custom view that has a fixed height and variable width.
-
func captMouseFramePosition(out x, y *float) void
Sets
x
andy
to the current mouse position (in pixels). -
func captMouseFramePosition(out x, y *double) void
Sets
x
andy
to the current mouse position (in pixels). -
func captMouseViewPosition(out x, y *float) void
Sets
x
andy
to the current mouse position (in units) with respect to the current view. -
func captResizable(resizable bool) void
Sets whether the window will be resizable. Must be done before
captStart
. -
func captAlwaysOnTop(always_on_top bool) void
Sets whether the window will be always be on top. Must be done before
captStart
. -
func captMultisample(multisample_rate int) void
Sets the target multisample rate of the window. Must be done before
captStart
. -
func captHideCursor(hide bool) void
Hides the mouse cursor. Cannot be done before
captStart
. -
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.
-
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.
-
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.
-
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.
-
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. Ifstep_fps
is zero, then the step fps will be practically unbounded. -
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
andy
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.). Ifuse_mouse_view_coords
istrue
, then the mouse coordinates supplied to the callback will be in view coords (units) instead of frame coords (pixels). -
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
andy
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.). Ifuse_mouse_view_coords
istrue
, then the mouse coordinates supplied to the callback will be in view coords (units) instead of frame coords (pixels). -
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)
-
-
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
-
-
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
-
-
func captClearColor(in color CaptColor) void
Sets the background clear-color of the window and then clears the window.
-
func captDrawOpacity(in value float) void
Changes the drawing opacity. The opacity
value
should be in the range[0.0f, 1.0f]
. -
func captKeyHeld(key int) bool
Returns whether a key is currently held (e.g. GLFW_KEY_W).
-
func alias captRandomize() => randomize
Initializes random number generation.
-
func alias captRandom() => normalizedRandom
Returns a random
double
in the range[0.0f, 1.0f)
.
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]
. -
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]
.
struct CaptTexture (id uint)
-
func CaptTexture(filename String, approximate bool = true) CaptTexture
Constructs a
CaptTexture
. Whenapproximate
is true, smooth blending will be used. -
func CaptTexture(filename *ubyte, approximate bool = true) CaptTexture
Constructs a
CaptTexture
. Whenapproximate
is true, smooth blending will be used. -
func destroy(this *CaptTexture) void
Destroys a
CaptTexture
. -
func isValid(this *CaptTexture) bool
Returns whether a
CaptTexture
is a valid. -
func invalidate(this *CaptTexture) void
Makes a
CaptTexture
invalid. Any previous texture should be destroyed before invalidating the texture if it will become inaccessible. -
func load(this *CaptTexture, filename String, approximate bool = true) void
Constructs a
CaptTexture
. Whenapproximate
is true, smooth blending will be used. -
func load(this *CaptTexture, filename *ubyte, approximate bool = true) void
Constructs a
CaptTexture
. Whenapproximate
is true, smooth blending will be used. -
func captBindTexture(texture POD CaptTexture) void
Manually binds a
CaptTexture
. -
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. Thex
andy
coords are from the top left, with positivex
pointing right and positivey
pointing down. -
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. Thex
andy
coords are from the top left, with positivex
pointing right and positivey
pointing down. -
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.
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. -
func destroy(this *CaptModel) void
Destroys a
CaptModel
. -
func draw(this *CaptModel) void
Manually draws a
CaptModel
with the currently boundCaptTexture
. -
func CaptModel(points <float> List, uvs <float> List) CaptModel
Constructs a
CaptModel
from a list of points and a list of UV coords. -
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.
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. -
func destroy(this *CaptShader) void
Destroys a
CaptShader
. -
func use(this *CaptShader) void
Binds a
CaptShader
. -
func getUniformLocation(this *CaptShader, name *ubyte) GLint
Returns the OpenGL uniform location of a uniform in a
CaptShader
. -
func uploadFloat(this *CaptShader, location GLint, value float) void
Uploads a
float
value for an OpenGL uniform variable. -
func uploadMatrix4f(this *CaptShader, location GLint, matrix *Matrix4f) void
Uploads a
Matrix4f
value for an OpenGL uniform variable. -
func uploadMatrix4f(this *CaptShader, location GLint, matrix Matrix4f) void
Uploads a
Matrix4f
value for an OpenGL uniform variable. -
func CaptShader(vertex_shader_code, fragment_shader_code String) CaptShader
Constructs a
CaptShader
from GLSL vertex and fragment shader code.