Allot of you like me wanted to know how to code a screen saver. Allot of you might have made simple screen savers that only worked as a exe. It way sound very hard to pull off a screen saver, and if you look at some of the tutorials for C++, it looks scary! But, there is a really simple way to start.
Since this is so simple, allot of this tutorial will be on the concepts of screen savers, how they are meant to work, how to pull this off, and then the code to compile with so it will take to the screen saver setup in the display settings.
Screen savers main design are to keep the screen in motion so a still image isn't burnt unto the screen. Mistakes might be to think that bright colours defeat the purpose of a screen saver. The answer to that will be no, the colours can be as intense as you can stand, as long as the screen is in motion, the screen saver is working.
Also, today most monitors are designed to prevent screen burns leaving screen savers to be more as what you use wallpaper (Either desktop or the actual stuff) for, decoration. So if you want a good screen saver, you want something that is liked or draws attention to itself. With this lil bit of knowledge out the way, lets move on to how screen savers work.
After the computer has been idle for the same amount of time set in the display properties, windows starts the screen saver application. When the screen saver is running, it waits for user input through either Mouse or the Keyboard. All the while, it also is rendering images unto the monitor to keep it in motion. Pretty simple heh? Now lets see how this works as code:
Okay! Lets start by putting the key concepts of a screen saver together. Will make are loop and give it user inputs:
'---|Code|----------------------------------------------------- SCREENRES 320, 200, 32,,1 'Set to 320x200 32bit colour Fullscreen 'Main LOOP DO GETMOUSE OMX, OMY,, MB 'Enable Mouse (Old Cords) GETMOUSE MX, MY,, MB 'Enable Mouse (New Cords) IF OMX <> MX THEN END ' Check for mouse movement X IF OMY <> MY THEN END ' Check for mouse movement Y IF MB > 0 THEN END ' Check for click w/o motion of mouse LOOP UNTIL INKEY$ <> "" 'End on keypress '--------------------------------------------------------------This code starts by setting the screen mode, and creating a loop. Inside the loop is a code to call the mouse for its cords at the start of the loop, then at the end of the loop. The IF..THENs see if there is a difference between the two calls. They also see if the mouse was clicked. Hey, the mouse can be clicked w/o moving it, unlikely, but better ready for it than not.
Now, we need something to save the screen. In this tut it will be something simple, I wont get into complicated graphic commands, or impressive 3D motion with OpenGL. But if later on after learning the concepts of screen savers, you are more than welcome to make a dazzling screen saver on your own! For now, will make a circle bounce around alternating color and smearing it as it goes. These are simple but cool to make!
Lets set up the stats for the circle:
'---|Code|----------------------------------------------------- SCREENRES 320, 200, 32,,1 'Set to 320x200 32bit colour Fullscreen X = 160: Y = 100: Radi = 10 'NEW, Circle cords and Radius.. R = 255: G = 255: B = 255 'NEW, R, G, B Values we'll alternate.. XV = 2: YV = 2 'NEW, Set X, Y velocities.. 'Main LOOP DO GETMOUSE OMX, OMY,, MB 'Enable Mouse (Old Cords) CIRCLE (X, Y), Radi, RGB(R, G, B) 'NEW Display Circle.. GETMOUSE MX, MY,, MB 'Enable Mouse (New Cords) IF OMX <> MX THEN END ' Check for mouse movement X IF OMY <> MY THEN END ' Check for mouse movement Y IF MB > 0 THEN END ' Check for click w/o motion of mouse LOOP UNTIL INKEY$ <> "" 'End on keypress '--------------------------------------------------------------The first new thing we added were the X, Y cords and the Radius setting. This used with Circle will start it in the center of the screen. Next we set the RGB values we want to alternate to white (255, 255, 255). The third new thing we added want be used yet, these will move the circle when added to X and Y. Inside the LOOP you will find the Circle statement with all the values we set above. When you run this code, it will display a circle in the center of the screen.
Now we need to set the barriers the circle will bounce off of. Then use IF.. THENs to enable there use with the circle.. We'll also add in the code to move it to make sure the barriers are right, and hide the mouse:
'---|Code|----------------------------------------------------- SCREENRES 320, 200, 32,,1 'Set to 320x200 32bit colour Fullscreen X = 160: Y = 100: Radi = 10 'Circle cords and Radius.. R = 255: G = 255: B = 255 'R, G, B Values we'll alternate.. XV = 2: YV = 2 'Set X, Y velocities.. MINX = 10: MAXX = 310 'NEW Set X's limits MINY = 10: MAXY = 180 'NEW Set Y's limits 'Main LOOP DO GETMOUSE OMX, OMY,, MB 'Enable Mouse (Old Cords) SETMOUSE ,,0 'NEW, Hide Mouse! CIRCLE (X, Y), Radi, RGB(R, G, B) 'Display Circle.. 'NEW 'Check if circle hit limits ' IF so, set random velocity to repel with.. IF X < MINX THEN XV = INT(RND * 5) + 1 IF X > MAXX THEN XV = -INT(RND * 5) + 1 IF Y < MINY THEN YV = INT(RND * 5) + 1 IF Y > MAXY THEN YV = -INT(RND * 5) + 1 'NEW 'Move Circle X += XV Y += YV SLEEP 100 'Delay the loop GETMOUSE MX, MY,, MB 'Enable Mouse (New Cords) IF OMX <> MX THEN END ' Check for mouse movement X IF OMY <> MY THEN END ' Check for mouse movement Y IF MB > 0 THEN END ' Check for click w/o motion of mouse LOOP UNTIL INKEY$ <> "" 'End on keypress '--------------------------------------------------------------Okay, we set are limits. If you notice they're off from the actual screen cords. This is to make up for the circles radius. Some times they can be calculated, other times you'll have to debug to get it right. But for the lower limits, add the radius. And the higher limits you subtract the radius.
The next new line hides the mouse so it won't mess up the rendering of the screen saver. Moving on, we come to the IF.. THENs for the barriers. When X and Y pass over the set limits it reverts them with a random velocity. This adds for a more interesting effect.
For the last few lines, X += XV and Y += YV moves the circle around, and SLEEP 100 allows us to see the circle instead of a high-speed blob. Take note that with this mouse kill routine, sleep needs to be between the GETMOUSE statements. Other wise it won't turn off to the mouse.
Now, if you let this run, it will just make the screen white and thus stop moving. This isn't what we want. In this last bit of code in this section, I'll add a random colour code....
'---|Code|----------------------------------------------------- SCREENRES 320, 200, 32,,1 'Set to 320x200 32bit colour Fullscreen X = 160: Y = 100: Radi = 10 'Circle cords and Radius.. R = 255: G = 255: B = 255 'R, G, B Values we'll alternate.. XV = 2: YV = 2 'Set X, Y velocities.. MINX = 10: MAXX = 310 ' Set X's limits MINY = 10: MAXY = 180 ' Set Y's limits 'Main LOOP DO GETMOUSE OMX, OMY,, MB 'Enable Mouse (Old Cords) SETMOUSE ,,0 ' Hide Mouse! 'Random colours R = INT(RND * 255) + 1 G = INT(RND * 255) + 1 B = INT(RND * 255) + 1 CIRCLE (X, Y), Radi, RGB(R, G, B) 'Display Circle.. 'Check if circle hit limits ' IF so, set random velocity to repel with.. IF X < MINX THEN XV = INT(RND * 5) + 1 IF X > MAXX THEN XV = -INT(RND * 5) + 1 IF Y < MINY THEN YV = INT(RND * 5) + 1 IF Y > MAXY THEN YV = -INT(RND * 5) + 1 'Move Circle X += XV Y += YV SLEEP 100 GETMOUSE MX, MY,, MB 'Enable Mouse (New Cords) IF OMX <> MX THEN END ' Check for mouse movement X IF OMY <> MY THEN END ' Check for mouse movement Y IF MB > 0 THEN END ' Check for click w/o motion of mouse LOOP UNTIL INKEY$ <> "" 'End on keypress '--------------------------------------------------------------
This is where I said a screen savers are really easy to make. This next little bit of code is all you need to turn the above simple screen saver into a real working .SCR application. Basically a .SCR is a .EXE. The only thing different about it needs to accept parameters that will be passed by the display properties. Look at this simple piece of code:
'-----[Code]-------------------------- IF LEFT$( COMMAND$, 2 ) = "/c" THEN END IF LEFT$( COMMAND$, 2 ) = "/p" THEN END '-------------------------------------Simple aye? To explain this, the first line that looks for "/c" in the command line, is to prevent the screen saver from running when you press settings. This is good to know because if you ever want settings, you can turn this into a code block for settings! The next line filters out the display from running the code when you select the screen saver. Its meant to find a preview to run in the little monitor on the GUI, but its best to leave this as is. Now, this code needs to be at the very top of the code, before any other statements are called. Here is what it should look like all together:
'---|Code|----------------------------------------------------- IF LEFT$( COMMAND$, 2 ) = "/c" THEN END IF LEFT$( COMMAND$, 2 ) = "/p" THEN END SCREENRES 320, 200, 32,,1 'Set to 320x200 32bit colour Fullscreen X = 160: Y = 100: Radi = 10 'Circle cords and Radius.. R = 255: G = 255: B = 255 'R, G, B Values we'll alternate.. XV = 2: YV = 2 'Set X, Y velocities.. MINX = 10: MAXX = 310 ' Set X's limits MINY = 10: MAXY = 180 ' Set Y's limits 'Main LOOP DO GETMOUSE OMX, OMY,, MB 'Enable Mouse (Old Cords) SETMOUSE ,,0 ' Hide Mouse! 'Random colours R = INT(RND * 255) + 1 G = INT(RND * 255) + 1 B = INT(RND * 255) + 1 CIRCLE (X, Y), Radi, RGB(R, G, B) 'Display Circle.. 'Check if circle hit limits ' IF so, set random velocity to repel with.. IF X < MINX THEN XV = INT(RND * 5) + 1 IF X > MAXX THEN XV = -INT(RND * 5) + 1 IF Y < MINY THEN YV = INT(RND * 5) + 1 IF Y > MAXY THEN YV = -INT(RND * 5) + 1 'Move Circle X += XV Y += YV SLEEP 100 GETMOUSE MX, MY,, MB 'Enable Mouse (New Cords) IF OMX <> MX THEN END ' Check for mouse movement X IF OMY <> MY THEN END ' Check for mouse movement Y IF MB > 0 THEN END ' Check for click w/o motion of mouse LOOP UNTIL INKEY$ <> "" 'End on keypress '--------------------------------------------------------------Now, all is left to do is compile. It's better to compile with -s gui to prevent the DOS-box from popping up before the screen saver displays. After you compile, you need change the .EXE to .SCR. To do this you may need to go to: Start> Setting> File Options : On the view setting, make sure the "Hide file extensions on known files" is not checked. This will allow you to change the file extensions in Windows Explorer. Once you've done that, all you need to do is move the .SCR to the windows directory. Then with the display properties, select your screen saver! Congrats! You've just made your first real screen saver!
dumbledore: For showing me how this was
done...
MystikShadows: Suggesting I write this...
VonGodric: The FB Code formatter
(Anyone else I forgot here)