Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

cjxgm

1
Posts
A member registered Sep 03, 2018

Recent community posts

(2 edits)

In the latest version, I only need to hack the shader compilation to get the game running on Android. No slowdown experienced, just some weird mouse controlls.


-- Patch: Transpile to OpenGL ES shaders.
-- * Convert "extern Type var = initializer;" to "extern Type var;"
--    and send the initializer on load.
-- * Convert "1" to "1.0" unless in array subscriptions like "a[1]".
do
    local function load_if_looks_like_filename(code) --> code
        if code == nil then return nil end
        if code:match("[\n;]") then  -- looks like code, not path
            return code
        else  -- looks like path, not code
            -- The parenthesis are important, it keeps only
            -- the first result of love.filesystem.read(),
            -- which returns 2 results.
            return (love.filesystem.read(code))
        end
    end
    local function extract_extern_variable_initializers_into(
        code, inits
    ) --> code
        if code == nil then return nil end
        -- Remove comments, but keeps the newline.
        code = code:gsub("//[^\n]*", "")
        -- Search for "extern <type> <var> = <initializer> ;"
        for var, initializer in
            code:gmatch("extern%s+%S+%s+(%S+)%s*=%s*([^;]+);")
        do
            inits[var] = initializer
        end
        -- Remove initializers.
        code = code:gsub("(extern%s+%S+%s+%S+)%s*=%s*([^;]+);", "%1;")
        return code
    end
    local function send_initializers(shader, inits)
        local sends = {}
        for var, initializer in pairs(inits) do
            table.insert(sends, ("send(%q, %s)"):format(var, initializer))
        end
        local sendcode = table.concat(sends, "\n")
        print("SEND\n", sendcode)
        local chunk = load(sendcode, "send shader initializers", "t", {
            send = function(...)
                shader:send(...)
            end,
            vec2 = function(a, b) return {a, b} end,
            vec3 = function(a, b, c) return {a, b, c} end,
            vec4 = function(a, b, c, d) return {a, b, c, d} end,
        })
        chunk()
    end
    local function various_hacks(code) --> code
        if code == nil then return nil end
        -- Convert int literals to float literals.
        -- * Keep literals like "1.", "1.2" and ".2" intact,
        --    but replace "1" with "1.0".
        -- * Should also keep indexing literals like "a[1]" as integers.
        code = code
            :gsub("(%f[a-zA-Z0-9_:.][0-9]+%f[^0-9.])", "%1.0")
            :gsub("%[%s*([0-9]+)[.]0%s*%]", "[%1]")
        return code
    end
    local love_new_shader = love.graphics.newShader
    function love.graphics.newShader(pixelcode, vertexcode)
        pixelcode = load_if_looks_like_filename(pixelcode)
        vertexcode = load_if_looks_like_filename(vertexcode)
        pixelcode = various_hacks(pixelcode)
        vertexcode = various_hacks(vertexcode)
        local inits = {}
        pixelcode =
            extract_extern_variable_initializers_into(pixelcode, inits)
        vertexcode =
            extract_extern_variable_initializers_into(vertexcode, inits)
        local shader = love_new_shader(pixelcode, vertexcode)
        send_initializers(shader, inits)
        return shader
    end
end