02.02.2013 Views

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 24: Embedding <strong>Flash</strong><br />

resource type. A new folder will appear in the Resource View panel labelled ‘FLASH’. Inside this<br />

folder will be the new resource. Give this a useful name by right-clicking and choosing ‘Properties’.<br />

Turning the resource in<strong>to</strong> a temporary swf file<br />

Before you can assign the cus<strong>to</strong>m resource <strong>to</strong> <strong>Flash</strong> you need <strong>to</strong> open it and s<strong>to</strong>re it as a temporary<br />

file. The function ‘CreateTmpSwf’ does this work for you. If you are interested in how this<br />

function works then you will need <strong>to</strong> get several books on the Windows platform <strong>to</strong> better<br />

understand the memory management and API calls.<br />

1 BOOL C<strong>Flash</strong>AXExeDlg::CreateTmpSwf()<br />

2 {<br />

3 HINSTANCE hInst = AfxGetResourceHandle();<br />

4 HRSRC hrsrc = ::FindResource(hInst,<br />

5 MAKEINTRESOURCE(IDR_FLASH), "FLASH");<br />

6 if (!hrsrc) {<br />

7 TRACE("BINARY resource not found");<br />

8 return FALSE;<br />

9 }<br />

10 HGLOBAL hg = LoadResource(hInst, hrsrc);<br />

11 if (!hg) {<br />

12 TRACE("Failed <strong>to</strong> load BINARY resource");<br />

13 return FALSE;<br />

14 }<br />

15 BYTE* pRes = (BYTE*) LockResource(hg);<br />

16 ASSERT(pRes);<br />

17<br />

18<br />

int iSize = ::SizeofResource(hInst, hrsrc);<br />

19 // Mark the resource pages as read/write so the mmioOpen<br />

20 // won’t fail<br />

21 DWORD dwOldProt;<br />

22 BOOL b = ::VirtualProtect(pRes,<br />

23 iSize,<br />

24 PAGE_READWRITE,<br />

25 &dwOldProt);<br />

26<br />

27<br />

ASSERT(b);<br />

28 GetTempPath(MAX_PATH, m_tmp);<br />

29<br />

30<br />

strcat(m_tmp, "\\tmp.swf");<br />

31 CFile file(m_tmp, CFile::modeWrite|CFile::modeCreate);<br />

32 file.Write(pRes, iSize);<br />

33 file.Close();<br />

34 return TRUE;<br />

35 }<br />

Listing 24.2<br />

383

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!