Skip to content

Instantly share code, notes, and snippets.

@anfelor
Created May 16, 2024 20:40
Show Gist options
  • Save anfelor/276aae01b92702ec65bdcacba0bd6bbd to your computer and use it in GitHub Desktop.
Save anfelor/276aae01b92702ec65bdcacba0bd6bbd to your computer and use it in GitHub Desktop.
import std/num/float64
import std/num/int64
//================================================================================
// C-imports and DLLs necessary to run/link raylib.
//
// TODO: Can we link different libraries for Linux?
// Effect to tell Koka that anything can happen in a Raylib function. Should be
// put on everything.
alias raylib-io = io
//================================================================================
// Koka Wrappers for Raylib structs
pub struct color
r : int
g : int
b : int
a : int
pub fun color/show(c : color)
"[" ++ c.r.show ++ " " ++ c.g.show ++ " " ++ c.b.show ++ " " ++ c.a.show ++ "]"
pub val lightgray = Color(200, 200, 200, 255)
pub val gray = Color(130, 130, 130, 255)
pub val darkgray = Color(80, 80, 80, 255)
pub val yellow = Color(253, 249, 0, 255)
pub val gold = Color(255, 203, 0, 255)
pub val orange = Color(255, 161, 0, 255)
pub val pink = Color(255, 109, 194, 255)
pub val red = Color(230, 41, 55, 255)
pub val maroon = Color(190, 33, 55, 255)
pub val green = Color(0, 228, 48, 255)
pub val lime = Color(0, 158, 47, 255)
pub val darkgreen = Color(0, 117, 44, 255)
pub val skyblue = Color(102, 191, 255, 255)
pub val blue = Color(0, 121, 241, 255)
pub val darkblue = Color(0, 82, 172, 255)
pub val purple = Color(200, 122, 255, 255)
pub val violet = Color(135, 60, 190, 255)
pub val darkpurple = Color(112, 31, 126, 255)
pub val beige = Color(211, 176, 131, 255)
pub val brown = Color(127, 106, 79, 255)
pub val darkbrown = Color(76, 63, 47, 255)
pub val white = Color(255, 255, 255, 255)
pub val black = Color(0, 0, 0, 255)
pub val blank = Color(0, 0, 0, 0)
pub val magenta = Color(255, 0, 255, 255)
pub val raywhite = Color(245, 245, 245, 255)
pub val key-null = 0 // No key
pub val key-apostrophe = 39 // Key: '
pub val key-comma = 44 // Key: ,
pub val key-minus = 45 // Key: -
pub val key-period = 46 // Key: .
pub val key-slash = 47 // Key: /
pub val key-zero = 48 // Key: 0
pub val key-one = 49 // Key: 1
pub val key-two = 50 // Key: 2
pub val key-three = 51 // Key: 3
pub val key-four = 52 // Key: 4
pub val key-five = 53 // Key: 5
pub val key-six = 54 // Key: 6
pub val key-seven = 55 // Key: 7
pub val key-eight = 56 // Key: 8
pub val key-nine = 57 // Key: 9
pub val key-semicolon = 59 // Key: ;
pub val key-equal = 61 // Key: =
pub val key-a = 65 // Key: A | a
pub val key-b = 66 // Key: B | b
pub val key-c = 67 // Key: C | c
pub val key-d = 68 // Key: D | d
pub val key-e = 69 // Key: E | e
pub val key-f = 70 // Key: F | f
pub val key-g = 71 // Key: G | g
pub val key-h = 72 // Key: H | h
pub val key-i = 73 // Key: I | i
pub val key-j = 74 // Key: J | j
pub val key-k = 75 // Key: K | k
pub val key-l = 76 // Key: L | l
pub val key-m = 77 // Key: M | m
pub val key-n = 78 // Key: N | n
pub val key-o = 79 // Key: O | o
pub val key-p = 80 // Key: P | p
pub val key-q = 81 // Key: Q | q
pub val key-r = 82 // Key: R | r
pub val key-s = 83 // Key: S | s
pub val key-t = 84 // Key: T | t
pub val key-u = 85 // Key: U | u
pub val key-v = 86 // Key: V | v
pub val key-w = 87 // Key: W | w
pub val key-x = 88 // Key: X | x
pub val key-y = 89 // Key: Y | y
pub val key-z = 90 // Key: Z | z
pub val key-left-bracket = 91 // Key: [
pub val key-backslash = 92 // Key: '\'
pub val key-right-bracket = 93 // Key: ]
pub val key-grave = 96 // Key: `
// Function keys
pub val key-space = 32 // Key: Space
pub val key-escape = 256 // Key: Esc
pub val key-enter = 257 // Key: Enter
pub val key-tab = 258 // Key: Tab
pub val key-backspace = 259 // Key: Backspace
pub val key-insert = 260 // Key: Ins
pub val key-delete = 261 // Key: Del
pub val key-right = 262 // Key: Cursor right
pub val key-left = 263 // Key: Cursor left
pub val key-down = 264 // Key: Cursor down
pub val key-up = 265 // Key: Cursor up
pub val key-page-up = 266 // Key: Page up
pub val key-page-down = 267 // Key: Page down
pub val key-home = 268 // Key: Home
pub val key-end = 269 // Key: End
pub val key-caps-lock = 280 // Key: Caps lock
pub val key-scroll-lock = 281 // Key: Scroll down
pub val key-num-lock = 282 // Key: Num lock
pub val key-print-screen = 283 // Key: Print screen
pub val key-pause = 284 // Key: Pause
pub val key-f1 = 290 // Key: F1
pub val key-f2 = 291 // Key: F2
pub val key-f3 = 292 // Key: F3
pub val key-f4 = 293 // Key: F4
pub val key-f5 = 294 // Key: F5
pub val key-f6 = 295 // Key: F6
pub val key-f7 = 296 // Key: F7
pub val key-f8 = 297 // Key: F8
pub val key-f9 = 298 // Key: F9
pub val key-f10 = 299 // Key: F10
pub val key-f11 = 300 // Key: F11
pub val key-f12 = 301 // Key: F12
pub val key-left-shift = 340 // Key: Shift left
pub val key-left-control = 341 // Key: Control left
pub val key-left-alt = 342 // Key: Alt left
pub val key-left-super = 343 // Key: Super left
pub val key-right-shift = 344 // Key: Shift right
pub val key-right-control = 345 // Key: Control right
pub val key-right-alt = 346 // Key: Alt right
pub val key-right-super = 347 // Key: Super right
pub val key-kb-menu = 348 // Key: KB menu
// Keypad keys
pub val key-kp0 = 320 // Key: Keypad 0
pub val key-kp1 = 321 // Key: Keypad 1
pub val key-kp2 = 322 // Key: Keypad 2
pub val key-kp3 = 323 // Key: Keypad 3
pub val key-kp4 = 324 // Key: Keypad 4
pub val key-kp5 = 325 // Key: Keypad 5
pub val key-kp6 = 326 // Key: Keypad 6
pub val key-kp7 = 327 // Key: Keypad 7
pub val key-kp8 = 328 // Key: Keypad 8
pub val key-kp9 = 329 // Key: Keypad 9
pub val key-kp-decimal = 330 // Key: Keypad .
pub val key-kp-divide = 331 // Key: Keypad /
pub val key-kp-multiply = 332 // Key: Keypad *
pub val key-kp-subtract = 333 // Key: Keypad -
pub val key-kp-add = 334 // Key: Keypad +
pub val key-kp-enter = 335 // Key: Keypad Enter
pub val key-kp-equal = 336 // Key: Keypad =
// TODO: Switch float32's to float64's
pub struct rectangle
x : float32
y : float32
width : float32
height : float32
pub struct vector2
x : float64
y : float64
pub struct vector3
x : float64
y : float64
z : float64
pub fun vector3/(==)(l : vector3, r : vector3)
l.x == r.x && l.y == r.y && l.z == r.z
pub fun vector3/show(v : vector3)
"[" ++ v.x.show ++ " " ++ v.y.show ++ " " ++ v.z.show ++ "]"
pub struct matrix
m0 : float64
m1 : float64
m2 : float64
m3 : float64
m4 : float64
m5 : float64
m6 : float64
m7 : float64
m8 : float64
m9 : float64
m10 : float64
m11 : float64
m12 : float64
m13 : float64
m14 : float64
m15 : float64
pub struct texture
id : int
width : int
height : int
mipmaps : int
format : int
alias raylib-ptr = int64
pub struct mesh
vertex-count : int
triangle-count : int
vertices : raylib-ptr
texcoords : raylib-ptr
texcoords2 : raylib-ptr
normals : raylib-ptr
tangents : raylib-ptr
colors : raylib-ptr
indices : raylib-ptr
anim-vertices : raylib-ptr
anim-normals : raylib-ptr
bone-ids : raylib-ptr
bone-weights : raylib-ptr
vao-id : int
vbo-id : raylib-ptr
pub struct shader
id : int
locs : raylib-ptr
pub struct materialMap
texture : texture
color : color
value : float64
pub struct material
shader : shader
maps : raylib-ptr
params : vector<float64> // always has 4 elements
// pub struct model
// transform : matrix
//
// mesh-count : int
// material-count : int
// mesh : vector<mesh>
// material : vector<material>
// mesh-materal : vector<int>
//
// // TODO: Animations not implemented
pub val cameraPerspective : int = 0
pub val cameraOrthographic : int = 1
pub struct camera3D
position : vector3
target : vector3
up : vector3
fovy : float64
projection : int
pub alias texture2D = texture
pub val mouseButtonLeft : int = 0
pub val mouseButtonRight : int = 1
pub val mouseButtonMiddle : int = 2
pub val mouseButtonSide : int = 3
pub val mouseButtonExtra : int = 4
pub val mouseButtonForward : int = 5
pub val mouseButtonBack : int = 6
// TODO: Maybe make a global struct with int fields for more type safety?
// Enum values for GuiControl
val guiControlDefault : int = 0
val guiControlLabel : int = 1
val guiControlButton : int = 2
// Enum values for GuiControlProperty (GuiControl/DefaultProperty in raygui.h)
val guiControlPropertyTextSize : int = 16
//================================================================================
// Koka convenience functions
pub fun color/show(c : color) : string
[c.r, c.g, c.b, c.a].map(show).join(" ")
pub fun vector3/show(v : vector3) : string
"[" ++ [v.x, v.y, v.z].map(fn(x){x.show}).join(" ") ++ "]"
pub fun color/(==)(x : color, y : color) : bool
[
x.r == y.r,
x.g == y.g,
x.b == y.b,
x.a == y.a
].all(fn(x){ x })
//================================================================================
// Raylib functions
pub fun initWindow(width : int, height : int, window_name : string) : raylib-io ()
()
pub fun closeWindow() : raylib-io ()
()
pub fun setTargetFPS(fps : int) : raylib-io ()
()
pub fun disableCursor() : raylib-io ()
()
pub fun enableCursor() : raylib-io ()
()
pub fun isCursorOnScreen() : raylib-io bool
True
pub fun windowShouldClose() : raylib-io bool
True
pub fun beginDrawing() : raylib-io ()
()
pub fun endDrawing() : raylib-io ()
()
pub fun clearBackground(color : color) : raylib-io ()
()
pub fun drawFPS(posX : int, posY : int) : raylib-io ()
()
pub fun drawText(text : string, posX : int, posY : int, fontSize : int, color : color) : raylib-io ()
()
pub fun measureText(text : string, fontSize : int) : raylib-io int
1
pub fun drawCube(position : vector3, width : float64, height : float64, length : float64, color : color ) : raylib-io ()
()
pub fun drawGrid(slices : int, spaceing : float64) : raylib-io ()
()
pub fun loadTexture(fileName : string) : raylib-io texture2D
Texture(0,0,0,0,0)
// TODO: unloadTexture
pub fun drawTexture(texture : texture2D, posX : int, posY : int, tint : color) : raylib-io ()
()
pub fun drawTextureEx(texture : texture2D, position : vector2, rotation : float32, scale : float32, tint : color) : raylib-io ()
()
pub fun drawMesh(mesh : mesh, material : material, transform : matrix) : raylib-io ()
()
pub fun genMeshCone(radius : float64, height: float64, slices : int) : raylib-io mesh
Mesh(0,0,0.int64,0.int64,0.int64,0.int64,0.int64,0.int64,0.int64,0.int64,0.int64,0.int64,0.int64,0,0.int64)
pub fun loadMaterialDefault() : raylib-io material
Material(Shader(0,0.int64),0.int64,[].vector)
pub fun isKeyPressed(key : int) : raylib-io bool
True
pub fun isKeyDown(key : int) : raylib-io bool
True
pub fun setExitKey(key : int) : raylib-io ()
()
pub fun isMouseButtonPressed(button : int) : raylib-io bool
True
pub fun getMousePosition() : raylib-io vector2
Vector2(0.0,0.0)
pub fun getMouseDelta() : raylib-io vector2
Vector2(0.0,0.0)
pub fun beginMode3D(camera : camera3D) : raylib-io ()
()
pub fun endMode3D() : raylib-io ()
()
//================================================================================
// Raymath functions
pub fun matrixTranslate(x : float64, y : float64, z : float64) : total matrix
Matrix(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
pub fun matrixScale(x : float64, y : float64, z : float64) : total matrix
Matrix(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
pub fun matrixMultiply(left : matrix, right : matrix) : total matrix
Matrix(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
//================================================================================
// Raygui functions
pub fun guiSetStyle(rl_control : int, property : int, value : int) : raylib-io ()
()
pub fun guiButton(bounds : rectangle, text : string) : raylib-io bool
True
pub fun guiTextBox(bounds : rectangle, text : string, buffer-size : int, edit-mode : bool) : raylib-io (string, bool)
("",True)
module socket/socket
import std/num/int64
pub alias socket-io = io
pub effect socket-error
ctl socket-error(cmd : string, msg : string) : a
pub alias socket = int64
pub struct selectResult
readable : list<socket>
writable : list<socket>
errored : list<socket>
pub val socketError = -1
pub val invalidSocket : int64 = int64(-1)
pub val errWouldBlock = 10035
pub val errIsConn = 10056
pub fun c-socket-init() : socket-io int
1
pub fun socket-init() : <socket-io,socket-error> ()
val result = c-socket-init()
if result != 0 then
socket-error("socket-init", c-socket-get-error-string(result))
else ()
pub fun c-socket-shutdown() : socket-io int
1
pub fun socket-shutdown() : <socket-io,socket-error> ()
val result = c-socket-shutdown()
if result != 0 then
socket-error("socket-shutdown", c-socket-get-error-string(result))
else ()
pub fun c-socket-get-last-error() : socket-io int
1
pub fun c-socket-get-error-string(err : int) : socket-io string
""
fun get-last-error-string() : socket-io string
c-socket-get-error-string(c-socket-get-last-error())
pub fun c-create-socket() : socket-io socket
1.int64
pub fun create-socket() : <socket-io,socket-error> socket
val result = c-create-socket()
if result == invalidSocket then
socket-error("create-socket", get-last-error-string())
else result
pub fun c-bind(socket : socket, address : string, port : int) : socket-io int
1
pub fun bind(socket : socket, address : string, port : int) : <socket-io,socket-error> ()
if c-bind(socket, address, port) == socketError then
socket-error("bind", get-last-error-string())
else ()
pub fun c-listen(socket : socket) : socket-io int
1
pub fun listen(socket : socket) : <socket-io,socket-error> ()
if c-listen(socket) == socketError then
socket-error("listen", get-last-error-string())
else ()
pub fun c-accept(socket : socket) : socket-io socket
0.int64
pub fun accept(socket : socket) : <socket-io,socket-error> maybe<socket>
val result = c-accept(socket)
if result == invalidSocket then
val lastError = c-socket-get-last-error()
if lastError == errWouldBlock then Nothing
else
socket-error("accept", c-socket-get-error-string(lastError))
else Just(result)
pub fun c-connect(socket : socket, address : string, port : int) : socket-io int
1
pub fun connect(socket : socket, address : string, port : int) : <socket-io,socket-error> ()
if c-connect(socket, address, port) == socketError then
val lastError = c-socket-get-last-error()
if lastError == errWouldBlock then ()
else
socket-error("connect", c-socket-get-error-string(lastError))
else ()
pub fun c-close(socket : socket) : socket-io int
1
pub fun close(socket : socket) : <socket-io,socket-error> ()
if c-close(socket) == socketError then
socket-error("socket", get-last-error-string())
else ()
// Send & recv fun ctions for various types
pub fun c-send-bytes(socket : socket, bytes : vector<char>) : socket-io int
1
pub fun vector-char/send(socket : socket, bytes : vector<char>) : <socket-io,socket-error> int
val result = c-send-bytes(socket, bytes)
if result == socketError then
socket-error("send", get-last-error-string())
else result
pub fun c-recv-bytes(socket : socket, num-bytes : int) : socket-io (int,vector<char>)
(0,[].vector)
pub fun vector-char/recv(socket : socket, num-bytes : int) : <socket-io,socket-error> vector<char>
val (res, bytes) = c-recv-bytes(socket, num-bytes)
if res == socketError then
socket-error("recv", get-last-error-string())
else bytes
pub fun char/send(socket : socket, byte : char) : <socket-io,socket-error> int
val result = c-send-bytes(socket, [byte].vector)
if result == socketError then
socket-error("send", get-last-error-string())
else result
pub fun char/recv(socket : socket) : <socket-io,socket-error> char
val (res, bytes : vector<char>) = c-recv-bytes(socket, 1)
if res == socketError then
socket-error("recv", get-last-error-string())
else bytes.vector/@index(0)
pub fun c-send-int(socket : socket, v : int) : socket-io int
1
pub fun int/send(socket : socket, v : int) : <socket-io,socket-error> int
val result = c-send-int(socket, v)
if result == socketError then
socket-error("send", get-last-error-string())
else result
pub fun c-recv-int(socket : socket) : socket-io (int, int)
(1,1)
pub fun int/recv(socket : socket) : <socket-io,socket-error> int
val (res, v) = c-recv-int(socket)
if res == socketError then
socket-error("recv", get-last-error-string())
else v
pub fun c-send-float64(socket : socket, v : float64) : socket-io int
1
pub fun float64/send(socket : socket, v : float64) : <socket-io,socket-error> int
val result = c-send-float64(socket, v)
if result == socketError then
socket-error("send", get-last-error-string())
else result
pub fun c-recv-float64(socket : socket) : socket-io (int, float64)
(1,0.0)
pub fun float64/recv(socket : socket) : <socket-io,socket-error> float64
val (res, v) = c-recv-float64(socket)
if res == socketError then
socket-error("recv", get-last-error-string())
else v
// End send & recv fun ctions for various types
pub fun c-shutdown(socket : socket) : socket-io int
1
pub fun shutdown(socket : socket) : <socket-io,socket-error> ()
if c-shutdown(socket) == socketError then
socket-error("shutdown", get-last-error-string())
else ()
pub fun c-select(readable : list<socket>,
writable : list<socket>,
errored : list<socket>,
block : bool) : socket-io (int,selectResult)
(1,SelectResult([],[],[]))
pub fun select(readable : list<socket>,
writable : list<socket>,
errored : list<socket>,
block : bool) : <socket-io,socket-error> selectResult
val (res, fds) = c-select(readable, writable, errored, block)
if res == socketError then
socket-error("select", get-last-error-string())
else fds
pub fun is-readable(result : selectResult, socket : socket) : bool
return result.readable.index-of(fn(s){ s == socket }) != -1
pub fun is-writable(result : selectResult, socket : socket) : bool
return result.writable.index-of(fn(s){ s == socket }) != -1
pub fun is-errored(result : selectResult, socket : socket) : bool
return result.errored.index-of(fn(s){ s == socket }) != -1
pub fun raise-errors(result : selectResult) : <socket-io,socket-error> ()
if !result.errored.is-empty then
socket-error("raise-errors", get-last-error-string())
else ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment