'win api'에 해당되는 글 1건

  1. 2009.01.13 win api 간단한 충돌판정 프로그램

두 원이 있고 한가지 원을 키보드 화살표를 이동시켜

다른 한원에 부�치면 판정이 발생.

MyUtil.h는 아래 참고,

 

#include <stdio.h>
#include "stdafx.h"
#include "MyUtil.h"
#include<math.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
       LPTSTR lpCmdLine, int nCmdShow)
{
 static char szAppName[] = "Test SDK_버전 1";
 static char szTitle[]="Test SDK 윈도우즈 프로그램";

 MSG  msg;
 HWND  hWnd;
 WNDCLASSEX wc;

 wc.cbSize   = sizeof(WNDCLASSEX);
 wc.style   = CS_HREDRAW | CS_VREDRAW;
 wc.lpfnWndProc  = (WNDPROC)WndProc;
 wc.cbClsExtra  = 0;
 wc.cbWndExtra  = 0;
 wc.hInstance  = hInstance;
 wc.hIcon   = LoadIcon(NULL, IDI_APPLICATION);
 wc.hCursor   = LoadCursor(NULL,IDC_ARROW);
    //wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); 
 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
 wc.lpszMenuName  = NULL;
 wc.lpszClassName = szAppName;
 wc.hIconSm   = LoadIcon(NULL, IDI_APPLICATION);
 
 RegisterClassEx(&wc);

 hWnd=CreateWindow(szAppName, szTitle, WS_OVERLAPPEDWINDOW,
       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
       NULL, NULL, hInstance, NULL);

 if( !hWnd )
  return (FALSE);

 ShowWindow(hWnd, nCmdShow);
 UpdateWindow(hWnd);

 while(GetMessage(&msg, NULL, 0, 0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
 return (msg.wParam);
}
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
 char temp[80];
 char temp2[80];
 PAINTSTRUCT ps;
 HDC hdc;
  POINT p,s,d;
    RECT r; 
 
 //static int m_Alpha;
 static int x,y;
 static RECT m_Rect,m_Rect2,m_Bound;
     static COLORREF m_Color,m_Color2;
 
 
 //HPEN hPen,hOldPen;
 //  HBRUSH hBrush,hOldBrush;

  

 switch(uMsg)
 {
 case WM_CREATE : 
 // GetClientRect(hWnd,&r);
  //GetClientRect(hWnd,&m_Bound);
  //d=CenterPoint(r);
  
    //s=CenterPoint(m_Rect);
  //  d=CenterPoint(m_Rect2);


    // SetRect(&m_Rect,s.x-50,s.y-50,s.x+50,s.y+50);
 // SetRect(&m_Rect2,d.x-50,d.y-50,d.x+50,d.y+50);
  

        OffsetRect(&m_Rect,400,400);
  OffsetRect(&m_Rect2,200,200);
      
  
  m_Color=RGB(0,255,0);
  m_Color2=RGB(255,0,0);
  
  SetTimer(hWnd,1,10,NULL);
  SetTimer(hWnd,2,1000,NULL);
  break;

 case WM_PAINT :
   hdc = BeginPaint(hWnd, &ps);
             GetClientRect(hWnd,&r);
            
    d=CenterPoint(m_Rect2);
    s=CenterPoint(m_Rect);
   
    SetRect(&m_Rect,s.x-50,s.y-50,s.x+50,s.y+50);
    SetRect(&m_Rect2,d.x-100,d.y-100,d.x+100,d.y+100);

    // DrawObject(hdc,m_Rect2,m_Color2,0);
 
   
  // DrawObject(hdc,m_Rect,m_Color,1);
   
 
  
   
       
     Ellipse(hdc,m_Rect.left,m_Rect.top,m_Rect.right,m_Rect.bottom);   
     Ellipse(hdc,m_Rect2.left,m_Rect2.top,m_Rect2.right,m_Rect2.bottom);
  
  if(CheckBound2(s,d)==0)
  { 
   
    
   TextOut(hdc,100,100,"부�침",4);

  }
        
    sprintf(temp,"%d  %d",d.x,d.y);
    TextOut(hdc,200,200,temp,strlen(temp));
    sprintf(temp2,"%d  %d",s.x,s.y);
    TextOut(hdc,400,400,temp2,strlen(temp2));
   

    EndPaint(hWnd, &ps);
   break;

    case WM_KEYDOWN:
  switch(wParam)
  {
  case VK_LEFT:
   
   x=-1;
   y=0;
   break;
  
        case VK_RIGHT:
   x=1;
   y=0;
   break;
        case VK_UP:
   x=0;
   y=-1;
   break;
  case VK_DOWN:
   x=0;
   y=1;
   break; 
  } 
    case WM_TIMER:
   
     switch(wParam)
  {
  case 1:
      OffsetRect(&m_Rect,x,y);
      InvalidateRect(hWnd,NULL,TRUE);
  break;
  
     case 2:
            OffsetRect(&m_Rect2,100,y);
      InvalidateRect(hWnd,NULL,TRUE);
     break;
  }

   
   
   break;
  

 case WM_DESTROY :
  KillTimer(hWnd,1); 
  PostQuitMessage(0);
   break;
 }

 return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

#include "stdafx.h"
#include "MyUtil.h"
#include<math.h>
POINT CenterPoint(RECT& r)
{
 POINT p;

  p.x = (r.left + r.right) /2;
  p.y = (r.top + r.bottom) /2;

 return p;
}

int CheckBound(RECT& r,RECT& bound)
{
 if(r.left<=bound.left || r.right>=bound.right)
  return 1;
 if(r.top<=bound.top || r.bottom>=bound.bottom)
  return 1;
 else
  return 0;
}

int CheckBound2(POINT& r,POINT& bound)
{  
 double xyz=0;

 xyz=((bound.x-r.x)*(bound.x-r.x) + (bound.y-r.y)*(bound.y-r.y));
 if(sqrt(xyz)<150)
  return 0;
 
}
void DrawObject(HDC hdc, RECT& r,COLORREF color,int type)
{
     DrawObject(hdc, r, color,color,type);
}
void DrawObject(HDC hdc, RECT& r,COLORREF penColor,COLORREF brColor, int type)
{
 HPEN hPen,hOldPen;
    HBRUSH hBrush,hOldBrush;

         hPen=CreatePen(PS_SOLID,1,penColor);
   hOldPen=(HPEN)SelectObject(hdc,hPen);
   hBrush=CreateSolidBrush(brColor);
   hOldBrush=(HBRUSH)SelectObject(hdc,hBrush);

      switch(type)
   {
   case 0:
                
    Rectangle(hdc,r.left,r.top,r.right,r.bottom);
                break;
   case 1:
    
    Ellipse(hdc,r.left,r.top,r.right,r.bottom);
    break;
            }

           
   SelectObject(hdc,hOldPen);
   SelectObject(hdc,hOldBrush);
   DeleteObject(hPen);
   DeleteObject(hBrush);
}


Posted by dirlove
,