-
-
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() successfulInitializes 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) voidStarts captain by initializing the window.
-
func captStart(in title *ubyte = '', in fullscreen bool = false) voidStarts 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() voidEnsures the captain API is initialized.
-
func captDefaultView() voidSets the view to the default view. The default view is a view with a height of
480.0funits. -
func captViewWidth() floatReturns the width (in units) of the current view.
-
func captViewHeight() floatReturns the height (in units) of the current view.
-
func captFrameWidth() intReturns the width (in pixels) of the window.
-
func captFrameHeight() intReturns the height (in pixels) of the window.
-
func captCustomView(width, height float) voidSets the view to a custom view that has a fixed width and height.
-
func captCustomViewBasedOnHeight(height float) voidSets the view to a custom view that has a fixed height and variable width.
-
func captMouseFramePosition(out x, y *float) voidSets
xandyto the current mouse position (in pixels). -
func captMouseFramePosition(out x, y *double) voidSets
xandyto the current mouse position (in pixels). -
func captMouseViewPosition(out x, y *float) voidSets
xandyto the current mouse position (in units) with respect to the current view. -
func captResizable(resizable bool) voidSets whether the window will be resizable. Must be done before
captStart. -
func captAlwaysOnTop(always_on_top bool) voidSets whether the window will be always be on top. Must be done before
captStart. -
func captMultisample(multisample_rate int) voidSets the target multisample rate of the window. Must be done before
captStart. -
func captHideCursor(hide bool) voidHides the mouse cursor. Cannot be done before
captStart. -
func captOnSetup(in on_setup_func func() void) voidSets 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) voidSets 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) voidSets 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) voidSets 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) voidSets the on-step function for the app.
The on-step function will be called each frame (
step_fpstimes a second) before the on-draw function. It should be used to perform app logic. Ifstep_fpsis 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) voidSets 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
xandyof 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_coordsistrue, 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) voidSets 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
xandyof 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_coordsistrue, 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) voidSets 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) voidSets 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) voidSets 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) voidSets the background clear-color of the window and then clears the window.
-
func captDrawOpacity(in value float) voidChanges the drawing opacity. The opacity
valueshould be in the range[0.0f, 1.0f]. -
func captKeyHeld(key int) boolReturns whether a key is currently held (e.g. GLFW_KEY_W).
-
func alias captRandomize() => randomizeInitializes random number generation.
-
func alias captRandom() => normalizedRandomReturns a random
doublein the range[0.0f, 1.0f).
struct CaptColor (r, g, b, a float)
-
func CaptColor(r, g, b float, a float = 1.0f) CaptColorConstructs a
CaptColorfrom 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) CaptColorConstructs a
CaptColorfrom 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) CaptTextureConstructs a
CaptTexture. Whenapproximateis true, smooth blending will be used. -
func CaptTexture(filename *ubyte, approximate bool = true) CaptTextureConstructs a
CaptTexture. Whenapproximateis true, smooth blending will be used. -
func destroy(this *CaptTexture) voidDestroys a
CaptTexture. -
func isValid(this *CaptTexture) boolReturns whether a
CaptTextureis a valid. -
func invalidate(this *CaptTexture) voidMakes a
CaptTextureinvalid. Any previous texture should be destroyed before invalidating the texture if it will become inaccessible. -
func load(this *CaptTexture, filename String, approximate bool = true) voidConstructs a
CaptTexture. Whenapproximateis true, smooth blending will be used. -
func load(this *CaptTexture, filename *ubyte, approximate bool = true) voidConstructs a
CaptTexture. Whenapproximateis true, smooth blending will be used. -
func captBindTexture(texture POD CaptTexture) voidManually binds a
CaptTexture. -
func captDrawTexture(texture POD CaptTexture, in x, y, w, h float) voidDraws a
CaptTextureat a given (x,y) position with a given width and height. Thexandycoords are from the top left, with positivexpointing right and positiveypointing down. -
func captDrawTextureUsingModel(model POD CaptModel, texture POD CaptTexture, in x, y float) voidDraws a
CaptTextureusing a model at a given (x,y) position. Thexandycoords are from the top left, with positivexpointing right and positiveypointing down. -
func captDrawTextureUsingModelAndTransformation(model POD CaptModel, texture POD CaptTexture, transformation Matrix4f) voidDraws a
CaptTextureusing 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) voidConstructs a
CaptModelfrom a primitive array of points and a primitive array of UV coords. -
func destroy(this *CaptModel) voidDestroys a
CaptModel. -
func draw(this *CaptModel) voidManually draws a
CaptModelwith the currently boundCaptTexture. -
func CaptModel(points <float> List, uvs <float> List) CaptModelConstructs a
CaptModelfrom a list of points and a list of UV coords. -
func CaptModel(in w, h float, in x_flip, y_flip bool = false) CaptModelConstructs 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) voidConstructs a
CaptShaderfrom GLSL vertex and fragment shader code. -
func destroy(this *CaptShader) voidDestroys a
CaptShader. -
func use(this *CaptShader) voidBinds a
CaptShader. -
func getUniformLocation(this *CaptShader, name *ubyte) GLintReturns the OpenGL uniform location of a uniform in a
CaptShader. -
func uploadFloat(this *CaptShader, location GLint, value float) voidUploads a
floatvalue for an OpenGL uniform variable. -
func uploadMatrix4f(this *CaptShader, location GLint, matrix *Matrix4f) voidUploads a
Matrix4fvalue for an OpenGL uniform variable. -
func uploadMatrix4f(this *CaptShader, location GLint, matrix Matrix4f) voidUploads a
Matrix4fvalue for an OpenGL uniform variable. -
func CaptShader(vertex_shader_code, fragment_shader_code String) CaptShaderConstructs a
CaptShaderfrom GLSL vertex and fragment shader code.