Click here to Skip to main content
15,881,852 members
Articles / Desktop Programming / MFC

SWFLIB - a free Flash authoring library

Rate me:
Please Sign up or sign in to vote.
4.75/5 (26 votes)
18 Jul 2006CPOL2 min read 169.1K   6.5K   93  
An article on a free Flash authoring library.
// SWFLIB TestProjectDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SWFLIB TestProject.h"
#include "SWFLIB TestProjectDlg.h"
#include "SWFMovie.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSWFLIBTestProjectDlg dialog

CSWFLIBTestProjectDlg::CSWFLIBTestProjectDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSWFLIBTestProjectDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSWFLIBTestProjectDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSWFLIBTestProjectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSWFLIBTestProjectDlg)
	DDX_Control(pDX, IDC_SHOCKWAVEFLASH1, m_SFWPlayer);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSWFLIBTestProjectDlg, CDialog)
	//{{AFX_MSG_MAP(CSWFLIBTestProjectDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_LOAD, OnButtonLoad)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSWFLIBTestProjectDlg message handlers

BOOL CSWFLIBTestProjectDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	// Create SWF movies
	CreateSWFMovie_Animation();
	CreateSWFMovie_Bitmap();
	CreateSWFMovie_Morphing();
	CreateSWFMovie_Sprite();
	CreateSWFMovie_Action();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSWFLIBTestProjectDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSWFLIBTestProjectDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSWFLIBTestProjectDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSWFLIBTestProjectDlg::OnButtonLoad() 
{
	// TODO: Add your control notification handler code here
	
	CFileDialog fileDialog(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "SWF Files (*.swf)|*.swf||", this);
	if (fileDialog.DoModal() == IDOK)
	{
		m_SFWPlayer.SetMovie(fileDialog.GetPathName());
	}
}

void CSWFLIBTestProjectDlg::CreateSWFMovie_Animation()
{
	// Set movie params
	SIZE_F movieSize = {400, 400};
	int frameRate = 40;
	POINT_F pt;

	// Create empty .SWF file
	CSWFMovie swfMovie;
	swfMovie.OpenSWFFile(_T("SWF Sample Movies/Sample1.swf"), movieSize, frameRate);
	SWF_RGB bgColor = {0, 0, 255};
	swfMovie.SetBackgroundColor(bgColor);

	// Define custom shape
	RECT_F shapeRect = {0, 0, 150, 150};
	CSWFShape shape(1, shapeRect, 1);
	SWF_RGBA lineColor = {0, 0, 0, 255};
	shape.AddLineStyle(3, lineColor);
	SWF_RGBA fillColor = {255, 0, 0, 255};
	shape.AddSolidFillStyle(fillColor);
	pt.x = 0;
	pt.y = 0;
	shape.ChangeStyle(1, 1, 0, &pt);
	shape.AddLineSegment(100, 0);
	shape.AddLineSegment(0, 100);
	shape.AddLineSegment(-100, 0);
	shape.AddLineSegment(0, -100);
	swfMovie.DefineObject(&shape, shape.m_Depth, true);

	// Custom animation
	for (float i=0; i<100; i++)
	{
		shape.Translate(i+2, 100);
		swfMovie.UpdateObject(&shape, shape.m_Depth, NULL, -1);
		swfMovie.ShowFrame();
	}

	// Close .SWF file
	swfMovie.CloseSWFFile();
}

void CSWFLIBTestProjectDlg::CreateSWFMovie_Bitmap()
{
	// Set movie params
	SIZE_F movieSize = {400, 400};
	int frameRate = 40;
	POINT_F pt;

	// Create empty .SWF file
	CSWFMovie swfMovie;
	swfMovie.OpenSWFFile(_T("SWF Sample Movies/Sample2.swf"), movieSize, frameRate);
	SWF_RGB bgColor = {255, 255, 255};
	swfMovie.SetBackgroundColor(bgColor);

	// Define bitmap object
	CSWFBitmap bitmap(2, (UCHAR*)"bm128.jpg");
	swfMovie.DefineObject(&bitmap, -1, false);

	// Define custom shape
	RECT_F shapeRect = {0, 0, 100, 100};
	CSWFShape shape(1, shapeRect, 1);
	SWF_RGBA lineColor = {0, 0, 0, 255};
	shape.AddLineStyle(3, lineColor);
	RECT_F bitmapRect = {0, 0, 128, 128};								// size of the bitmap
	RECT_F clipRect = {0, 0, 100, 100};									// where to fill
	shape.AddBitmapFillStyle(bitmap.m_ID, SWF_FILLSTYLETYPE_BITMAP_0, bitmapRect, clipRect);
	pt.x = 0;
	pt.y = 0;
	shape.ChangeStyle(1, 1, 0, &pt);
	shape.AddLineSegment(100, 0);
	shape.AddLineSegment(0, 100);
	shape.AddLineSegment(-100, 0);
	shape.AddLineSegment(0, -100);
	swfMovie.DefineObject(&shape, shape.m_Depth, true);
	swfMovie.ShowFrame();

	// Close .SWF file
	swfMovie.CloseSWFFile();
}

void CSWFLIBTestProjectDlg::CreateSWFMovie_Morphing()
{
	// Set movie params
	SIZE_F movieSize = {400, 400};
	int frameRate = 40;
	POINT_F pt;

	// Create empty .SWF file
	CSWFMovie swfMovie;
	swfMovie.OpenSWFFile(_T("SWF Sample Movies/Sample3.swf"), movieSize, frameRate);
	SWF_RGB bgColor = {255, 0, 0};
	swfMovie.SetBackgroundColor(bgColor);

	// Define morph shape
	RECT_F morphStartRect = {0, 0, 200, 200};
	RECT_F morphEndRect = {0, 0, 200, 200};
	CSWFMorphShape morphShape(5, 4, morphStartRect, morphEndRect);
	SWF_RGBA startLineColor = {0, 0, 0, 168};
	SWF_RGBA endLineColor = {255, 255, 255, 168};
	morphShape.AddLineStyle(1, startLineColor, 2, endLineColor);
	SWF_RGBA startFillColor = {0, 255, 0, 192};
	SWF_RGBA endFillColor = {0, 0, 255, 192};
	morphShape.AddSolidFillStyle(startFillColor, endFillColor);
	// Define starting morph shape
	pt.x = 20;
	pt.y = 20;
	morphShape.ChangeStyle(1, 1, 0, &pt, true);
	morphShape.AddLineSegment(75, 0, true);
	morphShape.AddLineSegment(75, 0, true);
	morphShape.AddLineSegment(0, 75, true);
	morphShape.AddLineSegment(0, 75, true);
	morphShape.AddLineSegment(-75, 0, true);
	morphShape.AddLineSegment(-75, 0, true);
	morphShape.AddLineSegment(0, -75, true);
	morphShape.AddLineSegment(0, -75, true);
	// Define ending morph shape
	pt.x = 20;
	pt.y = 95;
	morphShape.ChangeStyle(1, 1, 0, &pt, false);
	morphShape.AddLineSegment(50, -25, false);
	morphShape.AddLineSegment(25, -50, false);
	morphShape.AddLineSegment(25, 50, false);
	morphShape.AddLineSegment(50, 25, false);
	morphShape.AddLineSegment(-50, 25, false);
	morphShape.AddLineSegment(-25, 50, false);
	morphShape.AddLineSegment(-25, -50, false);
	morphShape.AddLineSegment(-50, -25, false);
	swfMovie.DefineObject(&morphShape, morphShape.m_Depth, true);

	// Custom animation
	int ratio = 0;
	for (int i=0; i<330; i++)
	{
		swfMovie.UpdateObject(&morphShape, morphShape.m_Depth, NULL, ratio);
		ratio += 200;
		swfMovie.ShowFrame();
	}

	// Close .SWF file
	swfMovie.CloseSWFFile();
}

void CSWFLIBTestProjectDlg::CreateSWFMovie_Sprite()
{
	// Set movie params
	SIZE_F movieSize = {400, 400};
	int frameRate = 40;
	POINT_F pt;

	// Create empty .SWF file
	CSWFMovie swfMovie;
	swfMovie.OpenSWFFile(_T("SWF Sample Movies/Sample4.swf"), movieSize, frameRate);
	SWF_RGB bgColor = {0, 255, 0};
	swfMovie.SetBackgroundColor(bgColor);

	// Define sprite
	RECT_F spriteRect = {0, 0, 50, 50};
	CSWFShape spriteShape(12, spriteRect, 7);
	SWF_RGBA lineColor = {255, 255, 0, 168};
	spriteShape.AddLineStyle(2, lineColor);
	SWF_RGBA fillColor = {255, 0, 0, 168};
	spriteShape.AddSolidFillStyle(fillColor);
	pt.x = 0;
	pt.y = 0;
	spriteShape.ChangeStyle(1, 1, 0, &pt);
	spriteShape.AddLineSegment(50, 0);
	spriteShape.AddLineSegment(0, 50);
	spriteShape.AddLineSegment(-50, 0);
	spriteShape.AddLineSegment(0, -50);
	swfMovie.DefineObject(&spriteShape, spriteShape.m_Depth, false);
	CSWFSprite sprite(13, (UCHAR*)"Sprite1", 8);
	sprite.AddObject(&spriteShape, spriteShape.m_Depth, NULL, -1, true);

	// Sprite animation
	float i;
	for (i=0; i<100; i++)
	{
		spriteShape.Translate(i, 0);
		sprite.UpdateObject(&spriteShape, spriteShape.m_Depth, NULL, -1);
		sprite.ShowFrame();
	}
	for (i=0; i<100; i++)
	{
		spriteShape.Translate(100, i);
		sprite.UpdateObject(&spriteShape, spriteShape.m_Depth, NULL, -1);
		sprite.ShowFrame();
	}
	for (i=0; i<100; i++)
	{
		spriteShape.Translate(100-i, 100-i);
		sprite.UpdateObject(&spriteShape, spriteShape.m_Depth, NULL, -1);
		sprite.ShowFrame();
	}
	swfMovie.DefineObject(&sprite, sprite.m_Depth, true);
	swfMovie.ShowFrame();

	// Close .SWF file
	swfMovie.CloseSWFFile();
}

void CSWFLIBTestProjectDlg::CreateSWFMovie_Action()
{
	// Set movie params
	SIZE_F movieSize = {400, 400};
	int frameRate = 12;

	// Create empty .SWF file
	CSWFMovie swfMovie;
	swfMovie.OpenSWFFile(_T("SWF Sample Movies/Sample5.swf"), movieSize, frameRate);
	SWF_RGB bgColor = {255, 255, 255};
	swfMovie.SetBackgroundColor(bgColor);

	// Define action
	CSWFAction action(false);
	action.Stop();
	swfMovie.TriggerAction(&action);
	swfMovie.ShowFrame();
	swfMovie.ShowFrame();
	swfMovie.ShowFrame();

	// Close .SWF file
	swfMovie.CloseSWFFile();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Elektromehanika d.o.o. Nis
Serbia Serbia
He has a master degree in Computer Science at Faculty of Electronics in Nis (Serbia), and works as a C++/C# application developer for Windows platforms since 2001. He likes traveling, reading and meeting new people and cultures.

Comments and Discussions