Flash++.h

Flash++.h is designed for C++ developers who want to interface with the Flash Runtime API without writing inline ActionScript. This library exposes access to the classes and methods of the Flash Runtime API. Read more about how Flash++.h works in the Crossbridge reference guide.

In order to use the Flash++ library you will need to #include <Flash++.h> and pass the -lFlash++ -lAS3++ compiler arguments to g++, for example:

/Crossbridge/sdk/usr/bin/g++ sample.cpp -emit-swf -o sample.swf -lFlash++ -lAS3++

Examples

This section lists a series of ActionScript sample applications from Flash Runtime API pages. Click the link to learn about the ActionScript API and see how you would write this code in a pure ActionScript application. Then look at the second column of this table to see how that same sample can be accomplished in C++ using Flash++.

ActionScript Example C++ Equivalent Using Flash++
Accessing System Capabilities
#include <Flash++.h>
#include <stdio.h>

// use the UI worker reference (see sample 2 for more info)
using namespace AS3::ui;

int main()
{
    // number
    int sx = flash::system::Capabilities::screenResolutionX;
    int sy = flash::system::Capabilities::screenResolutionY;
    printf("Resolution: %dx%d\n", sx, sy);
    
    // boolean
    bool d = flash::system::Capabilities::isDebugger;
    printf("Debugger: %s\n", d ? "true" : "false");
    
    // string
    String cpu = flash::system::Capabilities::cpuArchitecture;
    char* cpuStr = internal::utf8_toString(cpu);
    printf("CPU: %s\n", cpuStr);
    free(cpuStr);
    
    return 0;
}
Drawing Shapes
#include <Flash++.h>

// use the UI worker reference (see sample 2 for more info)
using namespace AS3::ui;

int main()
{
    // Setup the stage
    flash::display::Stage stage = internal::get_Stage();
    stage->scaleMode = flash::display::StageScaleMode::NO_SCALE;
    stage->align = flash::display::StageAlign::TOP_LEFT;
    stage->frameRate = 60;

    flash::display::Sprite mySprite = flash::display::Sprite::_new();
    stage->addChild(mySprite);

    flash::display::Graphics graphics = mySprite->graphics;
    // draw  simple filled circle
    graphics->beginFill(0x0000ff, 1.0);
    graphics->drawCircle(50.0, 50.0, 50.0);
    graphics->endFill();
}
Handling Mouse Input
#include <Flash++.h>
#include <stdio.h>

// use the UI worker reference (see sample 2 for more info)
using namespace AS3::ui;

var mouseDownHandler(void *arg, var as3Args){
    printf("mouseDown!\n");
    return internal::_undefined;
}

var mouseUpHandler(void *arg, var as3Args){
    printf("mouseUp!\n");
    return internal::_undefined;
}

int main()
{
    flash::display::Stage stage = internal::get_Stage();
    stage->scaleMode = flash::display::StageScaleMode::NO_SCALE;
    stage->align = flash::display::StageAlign::TOP_LEFT;
    
    flash::display::Sprite mySprite = flash::display::Sprite::_new();
    flash::display::Graphics graphics = mySprite->graphics;
    graphics->beginFill(0x0000ff, 1.0);
    graphics->drawCircle(50.0, 50.0, 50.0);
    graphics->endFill();
    
    stage->addChild(mySprite);
    
    // assign handlers to the mouseDown and mouseUp events
    mySprite->addEventListener(flash::events::MouseEvent::MOUSE_DOWN, 
                               Function::_new(mouseDownHandler, NULL));
    
    mySprite->addEventListener(flash::events::MouseEvent::MOUSE_UP, 
                               Function::_new(mouseUpHandler, NULL));
    
    // Throw an exception so main does not return normally
    // and cause the static destructors to be executed.
    AS3_GoAsync();
}
Handling Keyboard Input
#include <Flash++.h>
#include <stdio.h>

// use the UI worker reference (see sample 2 for more info)
using namespace AS3::ui;

var keyHandler(void *arg, var as3Args){
    // get the event object
    flash::events::KeyboardEvent event = (flash::events::KeyboardEvent) as3Args[0];
    
    // pull some information out of it
    String type = event->type;
    Object target = event->target;
    int keyCode = event->keyCode;
    
    // convert to std::string
    std::string typeStr = AS3::sz2stringAndFree(internal::utf8_toString(type));
    std::string targetStr = AS3::sz2stringAndFree(internal::utf8_toString(target));

    printf("Keyboard Event [%s, %s, %d]\n", targetStr.c_str(), typeStr.c_str(), keyCode);
    
    return internal::_undefined;
}

int main()
{
    flash::display::Stage stage = internal::get_Stage();
    stage->scaleMode = flash::display::StageScaleMode::NO_SCALE;
    stage->align = flash::display::StageAlign::TOP_LEFT;
    stage->frameRate = 60;
    
    flash::display::Sprite mySprite = flash::display::Sprite::_new();
    flash::display::Graphics graphics = mySprite->graphics;
    graphics->beginFill(0x0000ff, 1.0);
    graphics->drawCircle(100.0, 50.0, 50.0);
    graphics->endFill();
    
    stage->addChild(mySprite);
    
    // handle keyboard events
    stage->addEventListener(flash::events::KeyboardEvent::KEY_DOWN, 
                            Function::_new(keyHandler, NULL));
    
    // Throw an exception so main does not return normally
    // and cause the static destructors to be executed.
    AS3_GoAsync();
}
Playing Sound
#include <Flash++.h>
#include <stdio.h>

// use the UI worker reference (see sample 2 for more info)
using namespace AS3::ui;

var soundEventHandler(void *arg, var as3Args){
    flash::events::Event event = (flash::events::Event) as3Args[0];
    String type = event->type;
    
    // convert to std::string
    std::string typeStr = AS3::sz2stringAndFree(internal::utf8_toString(type));

    printf("Event [%s]\n", typeStr.c_str());
    
    return internal::_undefined;
}

int main()
{
    flash::display::Stage stage = internal::get_Stage();
    stage->scaleMode = flash::display::StageScaleMode::NO_SCALE;
    stage->align = flash::display::StageAlign::TOP_LEFT;
    stage->frameRate = 60;
    
    String url = "song.mp3";
    
    flash::net::URLRequest request = flash::net::URLRequest::_new(url);
    flash::media::Sound soundFactory = flash::media::Sound::_new();
    flash::media::SoundChannel song = flash::media::SoundChannel::_new();
    
    soundFactory->addEventListener(flash::events::Event::COMPLETE,
                                   Function::_new(soundEventHandler, NULL));
    soundFactory->addEventListener(flash::events::Event::ID3,
                                   Function::_new(soundEventHandler, NULL));
    soundFactory->addEventListener(flash::events::IOErrorEvent::IO_ERROR,
                                   Function::_new(soundEventHandler, NULL));
    soundFactory->addEventListener(flash::events::ProgressEvent::PROGRESS,
                                   Function::_new(soundEventHandler, NULL));
    
    soundFactory->load(request);
    
    song = soundFactory->play();
    
    // Throw an exception so main does not return normally
    // and cause the static destructors to be executed.
    AS3_GoAsync();
}
Working with the Date Object
#include <Flash++.h>
#include <stdio.h>

// use the UI worker reference (see sample 2 for more info)
using namespace AS3::ui;

int main()
{
    flash::display::Stage stage = internal::get_Stage();
    stage->scaleMode = flash::display::StageScaleMode::NO_SCALE;
    stage->align = flash::display::StageAlign::TOP_LEFT;
    stage->frameRate = 60;
    
    Date myDate1 = Date::_new();
    std::string myDate1Str = AS3::sz2stringAndFree(internal::utf8_toString(myDate1));
    printf("%s\n", myDate1Str.c_str());
    
    // notice the Date constructor takes var objects 
    // (new_int is documented in the AS3++.h documentation page)
    Date myDate2 = Date::_new(internal::new_int(2000), 
                              internal::new_int(0), 
                              internal::new_int(1));
    
    std::string myDate2Str = AS3::sz2stringAndFree(internal::utf8_toString(myDate2));
    printf("%s\n", myDate2Str.c_str());
}
Accessing the Camera
#include <Flash++.h>
#include <stdio.h>

// use the UI worker reference (see sample 2 for more info)
using namespace AS3::ui;

int main()
{
    flash::display::Stage stage = internal::get_Stage();
    stage->scaleMode = flash::display::StageScaleMode::NO_SCALE;
    stage->align = flash::display::StageAlign::TOP_LEFT;
    stage->frameRate = 60;
    
    flash::media::Camera camera = flash::media::Camera::getCamera();
    flash::media::Video video;    
    
    if (camera != AS3::ui::internal::_null){
        
        int w = camera->width * 2;
        int h = camera->height * 2;
        
        video = flash::media::Video::_new(w, h);
        video->attachCamera(camera);
        stage->addChild(video);
        
        printf("camera added\n");
    } else {
        printf("no camera found\n");
    } 
}