Menu
Login
Language
DE EN FR ES IT CZ
Back

Lua libcurl

  • #1, by darren-beckett Wednesday, 15. March 2017, 17:58 9 years ago Quote
    Are you able to give an example of how to display an image loaded into memory using the new Libcurl functions?

    This example from the forum is incomplete:
    function draw()
    
    if sprite2 ~= nil then
    
    sprite.position = {x = posx, y = 0}
    
    graphics.drawSprite(sprite2)
    
    end
    
    if sprite ~= nil then
    
    graphics.drawSprite(sprite)
    
    end
    
    -- Font switch
    
    -- graphics.font = Fonts.blocked
    
    graphics.drawFont(" Test\nTest", 10, 10)
    
    end
    
    
    
    graphics.setDrawFunc("draw()")
    
    
    
    startTween("posx", posx, 300, 3000, easeQuadInOut, true, true)

    Great Poster

    386 Posts

  • #2, by darren-beckett Thursday, 16. March 2017, 16:51 9 years ago Quote
    Also:
    1) Is is possible to display the HTML returned from a libcurl Get statement?
    2) Can the object be clickable? - Can it open a webpage?

    Great Poster

    386 Posts

  • #3, by SimonS Friday, 17. March 2017, 00:30 9 years ago Quote
    At best use this script:
    --[[
    Lua-Curl script for Visionaire
    (c) 13/6/2015
    ]]
    
    curl_multi = curl.new_multi()
    curls = {}
    handles_count = 1
    loadcount = 0
    max_loadcount = 10
    loadlist = {}
    
    function GET(url, func,param, buf, startoffset, endoffset)
     if buf~=nil then
      table.insert(curls,{curl.new(),buf, func,param})
     else
      table.insert(curls,{curl.new(),{}, func,param})
     end
        c = curls[#curls][1]
        c:setopt(curl.OPT_URL, url)
     if startoffset ~= nil then
      c:setopt(curl.OPT_RANGE, startoffset.."-"..endoffset)
     end
        c:setopt(curl.OPT_FOLLOWLOCATION, true)
        c:setopt(curl.OPT_TIMEOUT, 300)
        c:setopt(curl.OPT_IOCTLFUNCTION, function (param, buf) return #buf end)
        c:setopt(curl.OPT_HEADERFUNCTION, function (param, buf) return #buf end)
        c:setopt(curl.OPT_WRITEDATA, {curls[#curls]})
        c:setopt(curl.OPT_WRITEFUNCTION, function (param, buf)
            table.insert(param[1][2], buf) -- store a chunk of data received
            return #buf
        end)
        c:setopt(curl.OPT_PROGRESSDATA, {curls[#curls]})
        c:setopt(curl.OPT_PROGRESSFUNCTION, function(param, dltotal, dlnow)
            if param[1][4][2]~=nil then
                param[1][4][2](dtotal, dnow)
            end
        end)
        c:setopt(curl.OPT_NOPROGRESS, false) -- use this to activate progress
        curl_multi:add(c)
        curl_multi:perform()
    end
    
    function POST(url,data,func,param, buf, startoffset, endoffset, userPassword)
        if buf~=nil then
            table.insert(curls,{curl.new(),buf, func,param})
        else
            table.insert(curls,{curl.new(),{}, func,param})
        end
        c = curls[#curls][1]
        c:setopt(curl.OPT_URL, url)
        if startoffset ~= nil then
            c:setopt(curl.OPT_RANGE, startoffset.."-"..endoffset)
        end
        c:setopt(curl.OPT_FOLLOWLOCATION, true)
        c:setopt(curl.OPT_TIMEOUT, 300)
        c:setopt(curl.OPT_WRITEDATA, {curls[#curls]})
        c:setopt(curl.OPT_WRITEFUNCTION, function (param, buf)
            table.insert(param[1][2], buf) -- store a chunk of data received
            return #buf
        end)
     c:setopt(curl.OPT_POST, true)
        c:setopt(curl.OPT_POSTFIELDS, data)
        c:setopt(curl.OPT_NOPROGRESS, false) -- use this to activate progress
     c:setopt(curl.OPT_HTTPHEADER, "Content-Type: application/json")
     if userPassword ~= nil then
      c:setopt(curl.OPT_USERPWD, userPassword)
     end
        curl_multi:add(c)
        curl_multi:perform()
    end
    
    function postRequest(httpurl, data, onFinish, onProgress, userPassword)
        POST(httpurl, data, onFinish, {nil, onProgress}, nil, nil, nil, userPassword)
    end
    
    function downloadFile(httpurl, file, onFinish, onProgress)
        GET(httpurl, function(s, param) 
            local file = io.open(param[1], "wb");file:write(s);file:close() 
            onFinish() 
        end, {file, onProgress})
    end
    
    function downloadMemory(httpurl, onFinish, onProgress)
        GET(httpurl, onFinish, {nil, onProgress})
    end
    
    function update_curl()
        while loadcount < max_loadcount and #loadlist > 0 do
            loadcount = loadcount + 1 
            GET(loadlist[1][1], function(s,param) loadcount = loadcount - 1; param[2](s,param[3]) end, loadlist[1])
            table.remove(loadlist, 1)
        end
        local handles = curl_multi:perform()
        local result = curl_multi:info()
        if result ~= -1 then
            for k,v in pairs(curls) do
                if tostring(v[1]) == tostring(result) then
                    local size = v[1]:getinfo(curl.INFO_CONTENT_LENGTH_DOWNLOAD)
                    local downsize = v[1]:getinfo(curl.INFO_SIZE_DOWNLOAD)
                    if(size~=-1 and size~=downsize)then
                        print("not complete")
                        local element = table.remove(curls, k)
                    else
                        v[3](table.concat(v[2]),v[4])
                        table.remove(curls, k)
                    end
                    break
                end
            end
        end
    end
    
    registerEventHandler("mainLoop", "update_curl")

    You can then do something like this:

    downloadMemory("http://url...", function(s,code,param) 
      sprite = graphics.loadMemoryJPG(s)
    -- sprite = graphics.loadMemoryPNG(s)
    -- sprite = graphics.loadMemoryWEBP(s)
    end)
    
    downloadMemory("http://url...", function(s,code,param) 
      print(s) -- html
    end) 
    

    If you want to open a webpage, use startDefaultBrowser, may not work everywhere.

    Thread Captain

    1621 Posts

  • #4, by darren-beckett Friday, 17. March 2017, 10:44 9 years ago Quote
    Thanks Simon

    Great Poster

    386 Posts

  • #5, by darren-beckett Friday, 17. March 2017, 10:48 9 years ago Quote
    Are you able to provide a link to some documentation relating to the specific implementation of libcurl?

    Great Poster

    386 Posts

  • #6, by SimonS Friday, 17. March 2017, 10:56 9 years ago Quote
    We're using this implementation: http://luacurl.luaforge.net/
    It's modified for curl_multi, so I'd recommend sticking to the script, I already did all the heavy lifting there. Also because curl is not very easy.

    Thread Captain

    1621 Posts

  • #7, by darren-beckett Friday, 17. March 2017, 10:57 9 years ago Quote
    This is great, we shouldn't need to bug you guys so much now :-)
    Thanks again.

    Great Poster

    386 Posts

  • #8, by darren-beckett Friday, 17. March 2017, 14:22 9 years ago Quote
    I can't see how to assign the downloaded picture to an objects sprite, is there no SetSprite method?



    Also, The script isn't showing correctly, the LessThan "<" sign makes it show wrongly:
    function update_curl()
       <i>--while loadcount 0 do</i>
       while loadcount < max_loadcount and #loadlist > 0 do
          loadcount = loadcount + 1

    Great Poster

    386 Posts

  • #9, by SimonS Saturday, 18. March 2017, 19:58 9 years ago Quote
    You need to draw the sprite yourself like you did in the initial code:
    graphics.drawSprite(sprite)

    Thread Captain

    1621 Posts

  • #10, by sebastian Saturday, 18. March 2017, 20:45 9 years ago Quote
    I hope there will be a tutorial for these lua lib curl stuff some time after VS5 release. 
    It seems very weird if you guys tell "you need to ..." and all the above code is a level to high for me grin 
    I could assume now that the "sprite" was the previousely fetched png/jpg/webp image from the downloadMemory() function, right?

    Often it sounds like:

    "you need to program to get a game" .... yeah... but how do i start here? razz


    Thread Captain

    2346 Posts

  • #11, by darren-beckett Sunday, 19. March 2017, 22:09 9 years ago Quote
    Don't worry Seb, you don't need to do any of this stuff to make an awesome game.

    Great Poster

    386 Posts