Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

T-Rex animation

0.00/5 (No votes)
23 Apr 2014 1  
Self contained EXE animation

Introduction

This is a self contained EXE composited animation, written in procedural code with the low level SDK programming FLAT API, to get rid of any external dependencies.

Background

While I could have used the extended layered style, i made the choice of using the desktop composition feature, introduced in Windows Vista.

When desktop composition is enabled, individual windows no longer draw directly to the screen or primary display device as they did in previous versions of Windows.
Instead, their drawing is redirected to off-screen surfaces in video memory, which are then rendered into a desktop image and presented on the display.

In Windows 8, Desktop Window Manager (DWM) is always ON and cannot be disabled by end users and apps. As in Windows 7, DWM is used to compose the desktop.

In Windows Vista and Windows 7, desktop composition is enabled only with the AERO Glass Theme.

The GDIPLUS flat API is used to display each frame of the PNG image in loop mode, and the T-Rex roar is using a standard WAV audio remixed to match exactly the animation duration.
Everything is embedded as resource, to produce only one single final EXE file.

Using the code

The source code written in C++ is self explanatory, and designed to work in 64-bit mode (VS2010+).

Load_TREX is the main function in charge of loading the embedded resources

LONG_PTR Load_TREX () {
    LONG_PTR nRet = 0;
    HRSRC hResource = FindResource(gP.instance, MAKEINTRESOURCE(TREX), RT_RCDATA);
    if (hResource) {
       long imageSize = SizeofResource(gP.instance, hResource);
       if (imageSize) {
          LPVOID pResourceData = LockResource(LoadResource(gP.instance, hResource));
          if (pResourceData) {
             HGLOBAL MemBuffer = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD, imageSize);
             if (MemBuffer) {
                LPVOID pBuffer = GlobalLock(MemBuffer);
                if (pBuffer) {
                   CopyMemory(pBuffer, pResourceData, imageSize);
                   LPSTREAM pStream = NULL;
                   if (CreateStreamOnHGlobal(MemBuffer, FALSE, &pStream) == 0) {
                      LONG_PTR hImage = 0;
                      if (GdipCreateBitmapFromStream(pStream, hImage) == 0) {
                         nRet = hImage;
                      }
                      //IUnknown_Release(pStream);
                      ReleaseObject((PFUNKNOWN*) pStream);
                   }
                }
                GlobalUnlock(MemBuffer);
             }
             GlobalFree(MemBuffer);
          }
       }
    }
    return nRet;
}  

Points of Interest

The animation itself is performed from a child thread, rather than using a timer (but the timer code is also provided). The main advantage of using a thread is when dealing with multi-core, and when you have to use animation while the main process is running.

There is a much more complex demo based on the same DWM composited concept, here:
http://www.codeproject.com/Articles/705243/HUD-window-bit-DWM-composition

How to use the animation

Use drag and drop to move T-Rex everywhere on the desktop.
To close the animation, press the escape key or the ALT-F4 key combination.

And for those that are unable to compile the code, here is the link to download the EXE:

download T_Rex_EXE.zip

Here is another animation (written in PowerBASIC), but it is based on the same concept
download Cheetah.zip

If you like them i have a few more :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here