Bennu Game Development

English Forums => Extensions => Mensaje iniciado por: josebita en Noviembre 25, 2011, 12:15:39 am

Título: libcurl bindings [File downloading, server-client communications]
Publicado por: josebita en Noviembre 25, 2011, 12:15:39 am
I have created a very small libcurl binding that should be useful for those wanting to access online resources from BennuGD.
Right now it only supports one download at a time but that limitation will be lifted.


Here you can see a small video of the library downloading the main BennuGD forum code:
http://www.youtube.com/watch?v=2pm_sPxkqsc (http://www.youtube.com/watch?v=2pm_sPxkqsc)
The API is very simple right now:
[Edit] I've changed the API to closely mimic curl's easy API. I'll update the function description here ASAP. In the meanwhile, please use the provided example.

List of possible errors after doing curl_perform():
http://code.google.com/p/bennugd-monolithic/source/browse/trunk/3rdparty/curl/include/curl/curl.h#393

Hope it's useful. I'll upload binaries for other systems as I compile them.



[Edit] Obviously the idea is to get this into the iOS and Android ports, too.
Título: Re: libcurl bindings
Publicado por: josebita en Diciembre 25, 2011, 10:45:13 pm
win32 binaries added to the first post. Hope they're useful!
Título: Re: libcurl bindings [File downloading, server-client communications]
Publicado por: josebita en Enero 03, 2012, 03:07:39 am
OSX binary updated. Also, one no longer needs to use curl_fetch to download to a string, you just use:
Código: [Seleccionar]
curl_setopt(curl, CURLOPT_WRITEDATA, &output);Where output is the string where the download will be saved to, obviously.
Título: Re: libcurl bindings [File downloading, server-client communications]
Publicado por: josebita en Enero 24, 2012, 10:34:50 pm
Updated the win32 and OSX binaries and re-linked them, this time from Google Code hoping that the bad guys won't remove the files again.
The new version now changes the value of status (when used as curl_perform(curl, &status); ) in case of error to reflect the internal error emitted by libcurl. Possible errors are listed here:
http://code.google.com/p/bennugd-monolithic/source/browse/trunk/3rdparty/curl/include/curl/curl.h#393

Some example code for you to use:
Código: [Seleccionar]
import "mod_video"
import "mod_curl"
import "mod_say"
import "mod_mouse"
import "mod_text"
import "mod_map"
import "mod_file"

Global
int width = 1024;
int height = 768;
int quit=0;
end;

Process bouncer()
Private
    int vx=3, vy=3;

Begin
    graph = load_png("Icon.png");
    // Position the graphic onscreen
    x = 10+graphic_info(0, GRAPH, G_WIDTH);
    y = 10+graphic_info(0, GRAPH, G_HEIGHT);
    while(quit == 0)
        if(x + vx >= width || x+vx < 0)
            vx = -vx;
        end
        if(y+vy >= height || y+vy < 0)
            vy = -vy;
        end
        x += vx; y += vy;
        FRAME;
    End;
End;

Process main()
Private
    int i=0, status=0, curl=0;
    int tostring=0;     // Switch to 1 to download to a string
    string output;

Begin
    set_mode(width, height, 16);
   
    // Remove Google logo, if it exists already
    if(file_exists("classicplus.png"))
        fremove("classicplus.png");
        say("Removed existing logo");
    end;
   
    bouncer();
   
    say("Starting download");
   
    // Start libcurl, set options, perform transfer
    curl = curl_init();
    if(curl == -1)
        say("Curl initialisation failed, quitting");
        quit = 1;
        return;
    end;
   
    curl_setopt(curl, CURLOPT_NOPROGRESS,    1);
    if(tostring == 0)
        // Use this to write to a file
        curl_setopt(curl, CURLOPT_WRITEDATA, "classicplus.png");
        curl_setopt(curl, CURLOPT_URL, "http://www.google.es/logos/classicplus.png");
    else
        // Use this to download to a string
        curl_setopt(curl, CURLOPT_WRITEDATA, &output);
        curl_setopt(curl, CURLOPT_URL, "http://www.google.es/");
    end
   
    curl_perform(curl, &status);
   
    // Wait for the transfer to finish
    while(status < 0)
        FRAME;
    end;
   
    if(status > 0)
        say("Transfer failed with status "+status+", quitting");
        quit=1;
        return;
    end
   
    if(tostring == 0)
        // Replace the bouncer image by the google logo we just downloaded
        unload_map(0, son.graph);
        son.graph = load_png("classicplus.png");
    else
        write(0, 0, 0, 0, output);
    end
   
    curl_cleanup(curl);
   
    say("Download done!");

    while(! mouse.right)
        FRAME;
    end;
    quit=1;
End;