kyuseo의 게임 프로그래밍

InversectRect 함수의 리턴값 오류 본문

C++ 개발

InversectRect 함수의 리턴값 오류

kyuseo 2008. 1. 3. 20:01

개요..

 

Windows 2000 / XP에서 정상 작동되는 프로그램이 Windows 98 에서 정상 작동 되지 않는 현상을 발견하여, 시행착오와 테스트를 통하여 윈도우 API 함수인 IntersectRect 함수의 결함을 발견하였습니다.

 

IntersectRect 함수는 BOOL을 반환하도록 설계가 되어있지만, 이상한 원인으로 Window 98 에서는 리턴값이 TRUE(1), FALSE(0)이 아닌 0, Not 0 을 리턴하는 사실을 발견하였습니다.

 

따라서 그 함수의 리턴값을 사용할 경우 0 과 Not 0으로 비교해야 올바른 결과를 얻을 수 있습니다.

 

 

테스트 코드

 

void IntersectRectTest()

{

    // 겹친 사각형 테스트

    // rtn 이 TRUE(1)이 나오면 정상, Window XP /2000 / 98 모두 정상 작동됨

    CRect rcTemp;

    CRect rcScreen( 0, 0, 1024, 768 );

    CRect rcWindow( 5, 40, 80, 59 );

 

    int rtn = rcTemp.IntersectRect( &rcScreen, &rcWindow );

    cout << "IntersectRect1 = " << rtn << endl;

 

    // 안겹친 사각형 테스트     

    // rtn 이 FALSE(0)이 나오면 정상, Window XP /2000 에서는 정상 작동됨, Window 98 에서는 0이 아닌값이 리턴됨

 

    rcScreen.SetRect( 0, 0, 1024, 768 );

    rcWindow.SetRect( 5000, 4000, 8000, 5900 );

 

    rtn = rcTemp.IntersectRect( &rcScreen, &rcWindow );

 

    cout << "IntersectRect2 = " << rtn << endl;

 

    Sleep(1000);

}

 

 

실행결과

    Window 2000 / XP 에서의 결과 (올바른 결과가 나온다.)

 

    >> IntersectRect1 = 1

    >> IntersectRect2 = 0

 

 

    Window 98 에서의 결과 (잘못된 결과가 나온다.)

 

    >> IntersectRect1 = 769

    >> IntersectRect2 = 0

 

 

참고 IntersectRect 함수의 MSDN 설명서

 

IntersectRect

The IntersectRect function calculates the intersection of two source rectangles and places the coordinates of the intersection rectangle into the destination rectangle. If the source rectangles do not intersect, an empty rectangle (in which all coordinates are set to zero) is placed into the destination rectangle.

 

BOOL IntersectRect(

LPRECT lprcDst, // intersection buffer

CONST RECT *lprcSrc1, // first rectangle

CONST RECT *lprcSrc2 // second rectangle

);

 

Parameters

lprcDst

[out] Pointer to the RECT structure that is to receive the intersection of the rectangles pointed to by the lprcSrc1 and lprcSrc2 parameters. This parameter cannot be NULL.

lprcSrc1

[in] Pointer to the RECT structure that contains the first source rectangle.

lprcSrc2

[in] Pointer to the RECT structure that contains the second source rectangle.

 

Return Values

If the rectangles intersect, the return value is nonzero.

If the rectangles do not intersect, the return value is zero.