mod_multi example

I've had some multitouch sample code lying around for quite some time. I believe some of you might find it useful:
/**
 * Android test
 * Joseba GarcĂ­a Etxebarria - (C) 2012
 * Use as you wish
 */

import "mod_video"
import "mod_text"
import "mod_mouse"
import "mod_sound"
import "mod_wm"
import "mod_map"
import "mod_draw"
import "mod_say"
import "mod_file"
import "mod_text"
import "mod_multi"

GLOBAL
// Set to your device's native resolution
width  = SET ME;
height = SET ME;

Process Main()
Private
int song=0, num_fingers=0, i=0;

Begin
    set_mode(width, height, 32, MODE_FULLSCREEN|MODE_FRAMELESS);
   
    write(0, 0, 0, 0, "Width: "+width+" Height:"+height);
   
    graph = map_new(width, height, 16);
    x = width/2; y = height/2;

    write_var(0, 0, height, 6, num_fingers);
    drawing_map(0, graph);
    drawing_color(rgb(0, 255, 255));

    while(num_fingers != 5)
        // Store the total amount of fingers touching the screen
        num_fingers = multi_numpointers();
       
        for(i=0; i<10; i++)
            if(multi_info(i, "ACTIVE") > 0.0)
                draw_fcircle(multi_info(i, "Y"),
                             multi_info(i, "X"), 5);
                say_fast("Drawing fcircle for pointer "+i+" @ "+
                             multi_info(i, "Y")+
                         "x"+multi_info(i, "X")+
                         " active:"+multi_info(i, "ACTIVE"));
            end;
        end;

        frame;
    End;

    unload_map(0, graph);

    say("Quitting");
End;
As you can see, what this code does is paint a circle at the same position as you place your fingers.
t also writes the total number of screens as detected on the screen. IT'll qut when you place all the five fingers in your device's screen.

mod_curl examples (I)

For those of you interested in mod_curl, here's a small example that'll  fetch a website or an image from the internet and display it onscreen.
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!

Blog Archive