~mrms/tophat

35b1d64292e424938371b1a6a67d9cf443a3b9a7 — Marek Ma┼íkarinec 3 months ago 19895c8
Move away from our sokol fork and remove std libs
10 files changed, 5 insertions(+), 380 deletions(-)

M .gitmodules
M lib/miniaudio
M lib/sokol
M lib/stb
M lib/umka
M src/Makefile
D umka/std/LICENSE
D umka/std/fnc.um
D umka/std/mat.um
D umka/std/std.um
M .gitmodules => .gitmodules +1 -1
@@ 12,7 12,7 @@
	url = https://git.sr.ht/~mrms/umprof
[submodule "lib/sokol"]
	path = lib/sokol
	url = https://git.sr.ht/~mrms/sokol
	url = https://github.com/floooh/sokol
[submodule "cmd/mmdoc"]
	path = cmd/mmdoc
	url = https://git.sr.ht/~mrms/mmdoc

M lib/miniaudio => lib/miniaudio +1 -1
@@ 1,1 1,1 @@
Subproject commit b19cc09fd06b80f370ca4385d260df7e31925b50
Subproject commit 4a5b74bef029b3592c54b6048650ee5f972c1a48

M lib/sokol => lib/sokol +1 -1
@@ 1,1 1,1 @@
Subproject commit 1e1e7d6c46933f231e520f2e3ebc349818b69e77
Subproject commit 1b27ca96cad82414ce409ab160ed410eb77cbe61

M lib/stb => lib/stb +1 -1
@@ 1,1 1,1 @@
Subproject commit beebb24b945efdea3b9bba23affb8eb3ba8982e7
Subproject commit f4a71b13373436a2866c5d68f8f80ac6f0bc1ffe

M lib/umka => lib/umka +1 -1
@@ 1,1 1,1 @@
Subproject commit a482259e64ae60c46350d252923d32b1f219e25b
Subproject commit 7a8f13ed58958154dc749f7346acf84795d394f0

M src/Makefile => src/Makefile +0 -1
@@ 21,7 21,6 @@ UMKA_MODULES = \
	umka/rect.um \
	umka/tilemap.um \
	umka/window.um \
	umka/std/std.um \
	umka/particles.um \
	umka/lerp.um \
	umka/font.um \

D umka/std/LICENSE => umka/std/LICENSE +0 -25
@@ 1,25 0,0 @@
BSD 2-Clause License

Copyright (c) 2020, Vasiliy Tereshkov
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

D umka/std/fnc.um => umka/std/fnc.um +0 -32
@@ 1,32 0,0 @@
// Umka functional programming tools

type (
    Any* = interface{}
    AnyArray* = []Any
)

fn (a: ^AnyArray) map*(f: fn (x, ctx: Any): Any, ctx: Any): AnyArray {
    res := make(AnyArray, len(a^))
    for i := 0; i < len(a^); i++ {
        res[i] = f(a[i], ctx)
    }
    return res
}

fn (a: ^AnyArray) filter*(f: fn (x, ctx: Any): bool, ctx: Any): AnyArray {
    res := make(AnyArray, 0)
    for i := 0; i < len(a^); i++ {
        if f(a[i], ctx) {
            res = append(res, a[i])
        }
    }
    return res
}

fn (a: ^AnyArray) reduce*(f: fn (x, y, ctx: Any): Any, ctx: Any): Any {
    res := a[0]
    for i := 1; i < len(a^); i++ {
        res = f(res, a[i], ctx)
    }
    return res
}

D umka/std/mat.um => umka/std/mat.um +0 -100
@@ 1,100 0,0 @@
// Umka 3D vector/matrix library

type (
    Vec* = [3]real
    Mat* = [3]Vec
)


// Vector operations

fn (u: ^Vec) add*(v: Vec): Vec         {return Vec{u[0] + v[0], u[1] + v[1], u[2] + v[2]}}
fn (u: ^Vec) sub*(v: Vec): Vec         {return Vec{u[0] - v[0], u[1] - v[1], u[2] - v[2]}}
fn (u: ^Vec) mul*(a: real): Vec        {return Vec{u[0] * a, u[1] * a, u[2] * a}}
fn (u: ^Vec) div*(a: real): Vec        {return Vec{u[0] / a, u[1] / a, u[2] / a}}
fn (u: ^Vec) dot*(v: Vec): real        {return u[0] * v[0] + u[1] * v[1] + u[2] * v[2]}
fn (u: ^Vec) cross*(v: Vec): Vec       {return Vec{u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]}}
fn (u: ^Vec) elementwise*(v: Vec): Vec {return Vec{u[0] * v[0], u[1] * v[1], u[2] * v[2]}}
fn (v: ^Vec) norm*(): real             {return sqrt(v.dot(v^))}
fn (v: ^Vec) normalize*(): Vec         {return v.div(v.norm())}


// Matrix operations

fn (m: ^Mat) add*(n: Mat): Mat         {return Mat{m[0].add(n[0]), m[1].add(n[1]), m[2].add(n[2])}}
fn (m: ^Mat) sub*(n: Mat): Mat         {return Mat{m[0].sub(n[0]), m[1].sub(n[1]), m[2].sub(n[2])}}
fn (m: ^Mat) mul*(a: real): Mat        {return Mat{m[0].mul(a), m[1].mul(a), m[2].mul(a)}}
fn (m: ^Mat) div*(a: real): Mat        {return Mat{m[0].div(a), m[1].div(a), m[2].div(a)}}
fn (m: ^Mat) mulv*(v: Vec): Vec        {return Vec{m[0].dot(v), m[1].dot(v), m[2].dot(v)}}

fn (m: ^Mat) mulm*(n: Mat): Mat {
    var res: Mat
    for i := 0; i < 3; i++ {
        for j := 0; j < 3; j++ {
            for k := 0; k < 3; k++ {
                res[i][j] += m[i][k] * n[k][j]
            }
        }
    }
    return res
}

fn identity*(): Mat {
    var res: Mat
    for i := 0; i < 3; i++ {
        res[i][i] = 1
    }
    return res
}

fn (m: ^Mat) transpose*(): Mat {
    var res: Mat
    for i := 0; i < 3; i++ {
        for j := 0; j < 3; j++ {
            res[i][j] = m[j][i]
        }
    }
    return res
}

fn (m: ^Mat) normalize*(): Mat {
    return m.add(m.mulm(identity().sub(m.transpose().mulm(m^))).mul(0.5))
}


// Vector/matrix conversions

fn (v: ^Vec) toRateMat*(): Mat {
    return Mat{Vec{ 0,    -v[2],  v[1]},
               Vec{ v[2],  0,    -v[0]},
               Vec{-v[1],  v[0],  0   }}
}

fn (v: ^Vec) toAttMat*(): Mat {
    // v = {roll, pitch, yaw}

    sr := sin(v[0]);  cr := cos(v[0])
    sp := sin(v[1]);  cp := cos(v[1])
    sy := sin(v[2]);  cy := cos(v[2])

    rollMat  := Mat{Vec{ 1,   0,   0 },
                    Vec{ 0,   cr, -sr},
                    Vec{ 0,   sr,  cr}}

    pitchMat := Mat{Vec{ cp,  0,   sp},
                    Vec{ 0,   1,   0 },
                    Vec{-sp,  0,   cp}}

    yawMat   := Mat{Vec{ cy, -sy,  0 },
                    Vec{ sy,  cy,  0 },
                    Vec{ 0,   0,   1 }}

    return yawMat.mulm(pitchMat).mulm(rollMat)
}

fn (m: ^Mat) toAttAngles*(): Vec {
    roll  := atan2( m[2][1], m[2][2])
    pitch := atan2(-m[2][0], sqrt(m[2][1] * m[2][1] + m[2][2] * m[2][2]))
    yaw   := atan2( m[1][0], m[0][0])
    return Vec{roll, pitch, yaw}
}
\ No newline at end of file

D umka/std/std.um => umka/std/std.um +0 -217
@@ 1,217 0,0 @@
// Umka standard library

// Memory

fn rtlmemcpy(dest, src: ^void, count: int)

fn tobytes*(buf: interface{}): []uint8 {
    if buf.__self == null {
        error("Buffer is null")
    }
    if selfhasptr(buf) {
        error("Cannot access reference types")
    }
    bytes := make([]uint8, sizeofself(buf))
    rtlmemcpy(&bytes[0], buf.__self, sizeofself(buf))
    return bytes
}

fn frombytes*(buf: interface{}, bytes: []uint8) {
    if buf.__self == null {
        error("Buffer is null")
    }    
    if selfhasptr(buf) {
        error("Cannot access reference types")
    }
    if sizeofself(buf) != len(bytes) {
        error("Illegal buffer size")
    }
    rtlmemcpy(buf.__self, &bytes[0], sizeofself(buf))    
}

// File I/O

type File* = ^struct {}

const (
    seekBegin* = 0
    seekCur*   = 1
    seekEnd*   = 2
)    

fn rtlfopen  (name: str, mode: str): File;  
fn fopen*    (name: str, mode: str): File {return rtlfopen(name, mode)}

fn rtlfclose (f: File): int
fn fclose*   (f: File): int {return rtlfclose(f)}

fn rtlfread(buf: ^void, size, cnt: int, f: File): int

fn fread*(f: File, buf: interface{}): int {
    if f == null {
        error("File is null")
    }
    if bytes := ^[]uint8(buf); bytes != null {
        return rtlfread(&bytes[0], len(bytes^), 1, f)
    }
    if selfhasptr(buf) {
        error("Cannot read reference types except ^[]uint8")
    }
    return rtlfread(buf.__self, sizeofself(buf), 1, f)
}

fn rtlfwrite(buf: ^void, size, cnt: int, f: File): int

fn fwrite*(f: File, buf: interface{}): int {
    if f == null {
        error("File is null")
    }
    if bytes := ^[]uint8(buf); bytes != null {
        return rtlfwrite(&bytes[0], len(bytes^), 1, f)
    }    
    if selfhasptr(buf) {
        error("Cannot write reference types except ^[]uint8")
    }
    return rtlfwrite(buf.__self, sizeofself(buf), 1, f)
}

fn rtlfseek  (f: File, offset, origin: int): int
fn fseek*    (f: File, offset, origin: int): int {
    if f == null {
        error("File is null")
    }
    return rtlfseek(f, offset, origin)
}

fn rtlftell (f: File): int
fn ftell*   (f: File): int {
    if f == null {
        error("File is null")
    }
    return rtlftell(f)
}

fn rtlremove (name: str): int
fn remove*   (name: str): int {return rtlremove(name)}

fn rtlfeof  (f: File): int
fn feof*    (f: File): bool {
    if f == null {
        error("File is null")
    }                
    return bool(rtlfeof(f))
}

// I/O utilities

fn println*(s: str): int {return printf("%s\n", s)}
fn fprintln*(f: File, s: str): int {return fprintf(f, "%s\n", s)}

fn getchar*(): char {
    var c: char
    scanf("%c", &c)
    return c
}

// Strings

fn ltrim*(s: str): str {
    start := -1
    slen := len(s)
    for i := 0; i < slen; i++ {
        if s[i] != ' ' && s[i] != '\t' {
            start = i
            break
        } 
    }
    if start == -1 {return ""}
    return slice(s, start)
}

fn rtrim*(s: str): str {
    stop := -1
    slen := len(s)
    for i := slen - 1; i >= 0; i-- {
        if s[i] != ' ' && s[i] != '\t' {
            stop = i
            break
        } 
    }
    if stop == -1 {return ""}
    return slice(s, 0, stop + 1)
}

fn trim*(s: str): str {
    return ltrim(rtrim(s))
}

// Conversions

fn atoi*(s: str): int {
    var x: int
    sscanf(s, "%lld", &x)
    return x
}

fn atof*(s: str): real {
    var x: real
    sscanf(s, "%lf", &x)
    return x
}

fn itoa*(x: int): str {
    s := str(make([]char, 256))
    sprintf(s, "%lld", x)
    return s
}

fn ftoa*(x: real, decimals: int): str {
    fmt := str(make([]char, 256))
    s := str(make([]char, 256))
    sprintf(fmt, "%%.%dlf", decimals)
    sprintf(s, fmt, x)
    return s
}

// Math

const pi* = 3.14159265358979323846

// Random numbers

const randMax* = 0x7FFFFFFF
randSeed := 0

fn srand*(seed: int) {randSeed = seed}

fn rand*(): int {
    randSeed = ((randSeed * 1103515245) + 12345) & 0x7FFFFFFF
    return randSeed
}

fn frand*(): real {return real(rand()) / randMax}

// Timer

fn rtltime(): int
fn time*(): int {return rtltime()}

fn rtlclock(): real
fn clock*(): real {return rtlclock()}

// Command line and environment

fn argc*(): int {return rtlargc}

fn argv*(i: int): str {
    if i < 0 || i >= rtlargc {
        error("Command line parameter not found")
    }
    return rtlargv[i]
}

fn rtlgetenv(name: str): str
fn getenv*(name: str): str {
    return "" + rtlgetenv(name)
}