You can choose to download to a string or to a file by changing the value of tostring in the code below.
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(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;
(In order to see file download working, be sure to have a PNG file called Icon.png in the same folder where you'll be running this example code.)
What this code does is first initialize libcurl, remember to save the returned value as it'll be the transfer id you'll pass to the rest of the functions.
curl = curl_init();
Next, you must set some options like where to save the data you'll fetch (to a string or to a location on your disk) and give mod_curl the URL that you want to fetch.
curl_setopt(curl, CURLOPT_WRITEDATA, "classicplus.png");
curl_setopt(curl, CURLOPT_URL, "http://www.google.es/logos/classicplus.png");
See, we're identifying the curl transfer by the id given above by curl_init. You might perform up to 16 parallel transactions. Then you'll actually want to perform the transaction AND WAIT FOR IT TO FINISH. Transfers are performed in the background so that your game doesn't freeze, you can show a Downloading dialog, or something like that. You'd do that with:
curl_perform(curl, &status);
// Wait for the transfer to finish
while(status != 0)
FRAME;
end;
status will be -2 until the download has finishes, then it'll change to 0. When done with the transfer, you must close libcurl or otherwise you'll be wasting valuable memory:
curl_cleanup(curl);
In a next article, I'll show you how to perform basic client/server communication with a POST form.
Hope it's useful!
0 komentar:
Post a Comment