Home » Community » Forums » DirectX and XNA » Screensaver Template
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 Screensaver Template
Post New Topic  Post Reply 
Does anyone know of any good blank template projects or wizards for a DirectX screensaver? I have the Moire sample from the 8.1 SDK, which I'm sure I could pull from for a project of my own. I just thought I'd ask before I started on that as a blank project would save some time. Thanks for any info.

 User Rating: 1019   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

You can go ahead and try this... it comes from Windows 98 Programming from the Ground up...
(I am pretty much just typing it out of the book, so I haven't tried it...)
//Code for a minimal screensaver
//
#include <windows.h>
#include <scrnsave.h>

char str[80] = "Windows 98 Screensaver";

int delay = 200;

LRESULT WINAPI ScreenSaverProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static HDC hdc;
	static unsigned int timer;
	static RECT srcdim;
	static SIZE size;
	static int X = 0, Y = 0;
	static HBRUSH hBlkBrush;
	static TEXTMETRIC tm;

	switch(message)
	{
	case WM_CREATE:
		timer = SetTimer(hwnd, 1, delay, NULL);
		hBlkBrush = (HBRUSH) GetStockObject(BLACK_BRUSH);
		break;

	case WM_ERASEBKGND:

		hdc = GetDC(hwnd);

		//get coordinates of the screen
		GetClientRect(hwnd, &scrdim);

		//erase the screen
		SelectObject(hdc, hBlkBrush);
		PatBlt(hdc, 0, 0, scrdim.right, scrdim.bottom, PATCOPY);

		//get and save height and length of string
		GetTextMetrics(hdc, &tm);
		GetTextExtentPoint32(hdc, str, strlen(str), &size);

		ReleaseDC(hwnd, hdc);
		break;
		
	case WM_TIMER:
		hdc = GetDC(hwnd);

		//erase previous output
		SelectObject(hdc, hBlkBrush);
		PatBlt(hdc, X, Y, X + size.cx, Y + size.cy, PATCOPY);

		//move the string to a new location
		X += 10; Y += 10;
		if(X > scrdim.right) X = 0
		if(Y > scrdim.bottom) Y = 0

		//output string
		SetBkColor(hdc, RGB(0,0,0));
		SetTextColor(hdc, RGB(0,255,255));
		TextOut(hdc, X, Y, str, strlen(str));

		ReleaseDC(hwnd, hdc);
		break;

	case WM_DESTROY:
		KillTimer(hwnd, timer);
		break;

	default:
		return DefScreenSaverProc(hwnd, message, wParam, lParam);
	}

	return 0;
}


//placeholder dialog box function
BOOL WINAPI ScreenSaverCofigureDialog(HWND, hdwnd, UNIT message, WPARAM wParam, LPARAM lParam)
{
	return 0;
}

//no class to register
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
	return 1;
}


//it requires the following resource file:
/*
#include <windows.h>
#include <scnsave.h>

ID_APP ICON SCRICON.ICO

STRINGTABLE
{
	IDS_DESCRIPTION "My Screen Saver #1"
}

*/
 



Like I said, I haven't tried compiling it, so I have no idea if it works...

 User Rating: 1622   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

A screensaver is a normal windows application that's invoked with some specific command-line parameters. I can't remember them off the top of my head, but things like "yourscreensaver /p window-handle" when the user is previewing it, and you're supposed to render into the window with the given handle. You get the idea.

A google for "screensaver tutorial" would give you some good results. As soon as you know the command-line parameters needed, just add code to handle them in your own application-framework and you've got a screensaver.

Please read the DirectX FAQ, it answers a lot of common technical questions | NeXe | Journal #259850

 User Rating: 1823   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

Well, I gave it a shot, but its giving me some link errors.

saver.obj : error LNK2019: unresolved external symbol _DefScreenSaverProc@16 referenced in function _ScreenSaverProc@16

I think I'll just work backwards from the Moire project.

 User Rating: 1019   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by Wampus

I think I'll just work backwards from the Moire project.

Can you possibly post the code from the Moire project?



 User Rating: 1622   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Yes, Moe's screensaver works, after a few corrections:

#include <windows.h>
#include <scrnsave.h>

char str[80] = "Windows Screensaver";

int delay = 200;

#pragma comment ( lib, "scrnsave.lib" )
#pragma comment ( lib, "comctl32.lib" )


LRESULT WINAPI ScreenSaverProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static HDC hdc;
	static unsigned int timer;
	static RECT scrdim;
	static SIZE size;
	static int X = 0, Y = 0;
	static HBRUSH hBlkBrush;
	static TEXTMETRIC tm;

	switch(message)
	{
	case WM_CREATE:
		timer = (UINT) SetTimer(hwnd, 1, delay, NULL);
		hBlkBrush = (HBRUSH) GetStockObject(BLACK_BRUSH);
		break;

	case WM_ERASEBKGND:

		hdc = GetDC(hwnd);

		//get coordinates of the screen
		GetClientRect(hwnd, &scrdim);

		//erase the screen
		SelectObject(hdc, hBlkBrush);
		PatBlt(hdc, 0, 0, scrdim.right, scrdim.bottom, PATCOPY);

		//get and save height and length of string
		GetTextMetrics(hdc, &tm);
		GetTextExtentPoint32(hdc, str, (int) strlen(str), &size);

		ReleaseDC(hwnd, hdc);
		break;
		
	case WM_TIMER:
		hdc = GetDC(hwnd);

		//erase previous output
		SelectObject(hdc, hBlkBrush);
		PatBlt(hdc, X, Y, X + size.cx, Y + size.cy, PATCOPY);

		//move the string to a new location
		X += 10; Y += 10;
		if(X > scrdim.right) X = 0;
		if(Y > scrdim.bottom) Y = 0;

		//output string
		SetBkColor(hdc, RGB(0,0,0));
		SetTextColor(hdc, RGB(0,255,255));
		TextOut(hdc, X, Y, str, (int) strlen(str));

		ReleaseDC(hwnd, hdc);
		break;

	case WM_DESTROY:
		KillTimer(hwnd, timer);
		break;

	default:
		return DefScreenSaverProc(hwnd, message, wParam, lParam);
	}

	return 0;
}


//placeholder dialog box function
BOOL WINAPI ScreenSaverConfigureDialog(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	return 0;
}

//no class to register
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
	return 1;
}



... and resource.rc ...

#include <windows.h>
#include <scrnsave.h>

ID_APP ICON SCRICON.ICO

STRINGTABLE
{
	IDS_DESCRIPTION "My Screen Saver #1"
}



... and don't forget to put scricon.ico into the project's directory.

 User Rating: 1066   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Hmm. How to use Direct3D along with screensavers?
The program fails if I try to create Direct3D device. I mean the device creates ok, but the program quits.

 User Rating: 1066   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by ReaVeR-Andrey
Hmm. How to use Direct3D along with screensavers?
The program fails if I try to create Direct3D device. I mean the device creates ok, but the program quits.

Interesting. Do you mean it quits when set as a screensaver?

Please read the DirectX FAQ, it answers a lot of common technical questions | NeXe | Journal #259850

 User Rating: 1823   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

Quote:
Original post by ReaVeR-Andrey
Yes, Moe's screensaver works, after a few corrections:

*** Source Snippet Removed ***
... and resource.rc ...
*** Source Snippet Removed ***
... and don't forget to put scricon.ico into the project's directory.

Excellent! I think I might have to try playing around with it some time... (when I have time).



 User Rating: 1622   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: