(c) 1996-2003 Lucian Wischik, www.wischik.com/scr
Question: How to design the best "screensaver framework?"
Answer: After seven years experience in designing savers and frameworks, my final conclusion is -- don't. Don't make a framework. Instead, make a complete, self-contained, minimal saver that doesn't impose any structure on the programmer and doesn't require to be "learnt". Keep it simple. Accordingly, I present five very basic screen savers. They cover most of the important areas:
These examples complement my technical article, How To Write a 32bit Screen Saver. That article goes into exhaustive detail about exactly how a saver must behave. To understand the internals of these examples, read the article.
Here's what else I've learnt -- the following guidelines, along with the example code, is the distilled "best practice" from years of programming savers and millions of downloads. These guidelines will reduce the effort to design, debug and distribute your savers, and will reduce the number of support-emails you have to answer:
Copy the "scrprev.exe" utility into your Windows directory. Test the saver with arguments
/c | to run the settings dialog |
/p scrprev | to test in preview mode |
/s | to run full-screen |
/sm | to test multi-monitor behaviour |
Note: const tstring DebugFile=_T("OutputDebugString") at the start of the file. This will log interesting events, and will also run the saver in "friendly" mode (i.e. not topmost, doesn't quit as soon as it looses focus or the mouse moves). Before deployment you MUST change this.
Make sure you change the two STRINGTABLE resources: 1 is the name of the saver, and 2 is its url for Add/Remove Programs. (you can remove 2 if there isn't a web page).
When you have finished, rename the saver to "mysaver_setup.exe" and deploy it. The extension ".exe" will make the auto-self-installer happen when the user double-clicks on it.
To create a new saver, I copy the directory from an old one, rename all the files, open them all up in Notepad and alter the names using search/replace. Don't forget the STRINGTABLE resources in the .rc file, and the VERSIONINFO, and the manifest.
There are project files for Visual C++6, Visual C++ .NET and Borland C++Builder5. I'm sure the code will work fine under other compilers. If you want, you can delete the project files for compilers you're not using. The extensions are:
.sln .vcprog | Visual Studio .NET workspace / project |
.dsw .dsp | Visual C++6 workspace / project |
.bpg .bpf .bpr | Borland C++Builder group / project |
Each example has pretty much the same structure. All the saver code is contained in a single .cpp file that looks like this:
// Comments at the top about how it works and how A bunch of header declarations // --------------------------------------------- The code that's unique to each saver. This is // --------------------------------------------- Then comes the generic saver code that's |
MinScr -- this saver doesn't do anything special, just blobs on the screen using double-buffering. It's a good starting point.
Images -- this saver uses images. It draws a sprite (BMP with transparency) on top of a background (JPG). What's special is that it stores the sprite and background compressed in a ZIP file, and this zip is in turn stored as a resource in the .SCR file. At runtime, when the images are needed, they're decompressed directly from resource into memory without any intermediate files.
PlayOgg -- this saver plays music from an OGG file that's embedded as a resource. I used to think music in a screen saver was silly (because savers are run when you're not at the computer!) But I was wrong: for some of my musical savers, I've had lots of happy parents write to me that their young children are transfixed by the combination of music and images. OGG is a file format that's like MP3 only better.
AudioIn -- this saver captures the currently-playing sound (be it from CD or Windows Media Player or whatever) and displays a voiceprint of it.
ThreeDee -- uses OpenGL for hardware-accelerated 3D graphics. Also optionally changes the screen mode, so as to run faster. There are lots of troublesome fiddly issues to do with 3d graphics and with mode changing: read the comments at the start of threedee.cpp.
By Lucian Wischik, University of Bologna. June 2003.