1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#include<windows.h> //header file for window applications
#include<math.h>
#include<stdio.h>
/*
Program Name:- gdi_Box2BoxCollision
Compiler Used:- MS VC++ 2005 Express
Website:-Gfxguru.Net
Programmer:-krishx007
E-MAIL: krishx007@yahoo.com
*/
//function which processes all the windows messages
LRESULT CALLBACK WindowProc(HWND,UINT,WPARAM,LPARAM);
HDC hDC;
RECT ClientRect;
//Every Win32 application(not Console) starts with the execution of WinMain
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{
WNDCLASSEX wc; //every window is created from windows class
MSG msg; //message structure variable
HWND hWnd; // handle to window
// clear out the window class for use
ZeroMemory(&wc, sizeof(WNDCLASSEX));
// fill in the struct with the needed information
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW ;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(GetStockObject(BLACK_BRUSH));
wc.lpszClassName = L"MyClass1";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
//registering the class
RegisterClassEx(&wc);
//creating the window
hWnd=CreateWindowEx(NULL,L"MyClass1",L"krishx007@yahoo.com www.Gfxguru.Net",WS_SYSMENU,
10,10,800,600,NULL,NULL,hInstance,NULL);
//SetBkColor(hDC,RGB(0,0,0));
ShowWindow(hWnd,nShowCmd);
hDC=GetDC(hWnd);
GetClientRect(hWnd,&ClientRect);
POINT MousePos;
HBRUSH hRedBrush,hGreenBrush;
hRedBrush = CreateSolidBrush(RGB(255,0,0));
hGreenBrush = CreateSolidBrush(RGB(0,200,0));
int x1,y1,x2=150,y2=200;
int a1=300,b1=300,a2=500,b2=500;
//Message Loop which detects all the messages and sends to ProcessMessagefn
while(TRUE)
{
//if any message is sent by windows
while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message==WM_QUIT) break;
//-------------------------------------------
SetBkMode(hDC,TRANSPARENT);
GetCursorPos(&MousePos);
int mousex=MousePos.x;
int mousey=MousePos.y;
x1=mousex;
y1=mousey;
x2=x1+100;
y2=y1+150;
Rectangle(hDC,a1,b1,a2,b2);//Rectangle 1 guided by mouse
Rectangle(hDC,x1,y1,x2,y2);//Rectangle 2
TextOut(hDC, 0, 0,L"Bring the smaller rectangle guided by mouse pointer near static rectangle to check collision",93);
//This is the algo to check collision between two Rectangles having coordinates
//a1,b1,a2,b2 and x1,y1,x2,y2 respectively.
if(x2>a1 && y2>b1 && x1<a2 && y1<b2)//if Collision detected
{
TextOut(hDC, 0, 30,L"Collision detected", 20);
SelectObject(hDC,hRedBrush);// select the pen into context
SetTextColor(hDC,RGB(255,0,0));//setting TextColor
}
else//if Collision not detected
{
TextOut(hDC, 0, 30,L"Collision NOT detected",22);
SelectObject(hDC,hGreenBrush);
SetTextColor(hDC,RGB(0,0,255));//setting TextColor
}
InvalidateRect(hWnd,NULL,1);//Clear the previous drawing on Device Context
Sleep(100);//wait for 1000 milliseconds
}
return(msg.wParam);
}
LRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
//this message is sent by the window whenever the window is destroyed
case WM_DESTROY:
ReleaseDC(hWnd,hDC);//releasing the device context
PostQuitMessage(0);
break;
//default execution for all the other messages
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return(0);
}
|