Sign in to follow this  
Hermes

SimpleAnimation

Recommended Posts

This sample code works very bad when trying to exit the program pressing the VK_ESCAPE or the X close button of the window or even worse it gets stuck in memory after i close the window.My question is why!!! The SwapBackBuffer uses BitBlt maybe a timer would solve the mistery of this very slowwwwww process......
// Monster.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include  "main.h"
#include "monster1.h"

/*//////////////////////////////globals\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/

CMonster gMonster;



int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 





 HWND        hwnd;										
	// Create the variable to hold the window messages
    MSG         msg;	
	
	// Create the structure that holds the attributes of the window.
	// I just crammed them in like this to save space because we know what they do.
    WNDCLASSEX  wndclass = {sizeof (wndclass), CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hInstance,
						  LoadIcon (NULL, IDI_WINLOGO), LoadCursor (NULL, IDC_ARROW), (HBRUSH) GetStockObject (WHITE_BRUSH),
						  NULL, "Window Class", LoadIcon (NULL, IDI_WINLOGO)};


	// Register the window class with the operating system
	RegisterClassEx (&wndclass);	
	
	// Now, we actually create the window
    hwnd = CreateWindow ("Window Class",					// window class name 
						 "Bitmap Background",	  			// window's Title    
						 WS_SYSMENU,						// window style	- This style won't allow the window to resize
						 CW_USEDEFAULT,						// initial x position
						 CW_USEDEFAULT,						// initial y position
						 SCREEN_WIDTH,								// Here we pass in our desired width (800)	 
						 SCREEN_HEIGHT,						    // Here we pass in our desired height (600)	 
						 NULL,								// This is the parent window handle.  
						 NULL,								// This is the window menu handle
						 hInstance,						    // This is the programs instance handle.
						 NULL);								// We don't want to pass any extra data in, so NULL

	// This shows our window. 
    ShowWindow (hwnd, nCmdShow);							

	// This pretty much paints our window to the screen.
    UpdateWindow (hwnd);									

	// Here is our main loop. 
	while (1)					
    {	
		// *We use PeekMessage() instead of GetMessage() to see if there is a message
		// from windows, if not, then we want to animate when nothing is going on.*

		// Check if there is a window message and remove it from the queue
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			// If the message was to quit, break out of the while loop
			if(msg.message == WM_QUIT||msg.wParam==VK_ESCAPE) break;

			// This Translates messages so windows understands them.
			TranslateMessage (&msg);					
		
			// This sends the messages to the WndProc().
			DispatchMessage (&msg);							
		}
		else
		{

			gMonster.SwapBackBuffer(false);
		}	
    }

	// Unregister the window class with the operating system
	UnregisterClass("Window Class",hInstance);
    
	// Quit the program
	return msg.wParam ;										
}

// Here is the WndProc that handles all the messages															
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	// Check which message was passed in
    switch (iMsg)											
    {
		// This message is sent when the window is created (CreateWindow())
		case WM_CREATE:

			// Create the double buffer and load the bitmaps	
			Init(hwnd);										
			break;	
	
		case WM_RBUTTONDOWN:
		     break;

		// This message is sent when the window is destroyed
		case WM_DESTROY:
			PostQuitMessage(0);
            break; 
	}													

	// Process/handle the default messages
	return DefWindowProc (hwnd, iMsg, wParam, lParam);		
															
}





void Init(HWND hWnd)
{


gMonster.CreateBuffer(hWnd);
gMonster.LoadTheBackground("background.bmp");
gMonster.DrawBackgroundImage();
//gMonster.DisplayBitmap(0,0);

//gMonster.SwapBackBuffer(false);




}






Can BitBlt be too slow and eat my CPU ??????since i don't have a timer yet and it probably blows my message quoue up

Please take a look .......

[sad]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this