Skip to content

Instantly share code, notes, and snippets.

@daramkun
Created January 25, 2015 06:40
Show Gist options
  • Select an option

  • Save daramkun/458dd47b4ebdc8a16052 to your computer and use it in GitHub Desktop.

Select an option

Save daramkun/458dd47b4ebdc8a16052 to your computer and use it in GitHub Desktop.
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <cstdio>
#include <cmath>
float x = 0, y = 0;
int targetX = 0, targetY = 0;
float angle;
HBITMAP myBitmap;
LRESULT CALLBACK WndProc ( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch ( uMsg )
{
case WM_CREATE:
myBitmap = ( HBITMAP ) LoadImage ( GetModuleHandle ( nullptr ), L"sample.bmp", IMAGE_BITMAP, 320, 320, LR_LOADFROMFILE );
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
// 창의 기본 DC를 가져온다
HDC hDC = BeginPaint ( hWnd, &ps );
// 기본 DC의 호환 DC (Compatible DC)를 생성한다
// 호환 DC는 기본 DC와 시스템적으로 동일한 구성 (색의 가지수 등)을 가지게 된다
HDC hCompDC = CreateCompatibleDC ( hDC );
// 기본 DC의 호환 비트맵 (Compatible Bitmap)을 생성한다.
// 동일한 색상 깊이를 가지게 되므로 색이 깨지지 않는다.
HBITMAP hCompBitmap = CreateCompatibleBitmap ( hDC, 800, 600 );
// 호환 DC에 호환 비트맵을 적용
HBITMAP oldBitmap = ( HBITMAP ) SelectObject ( hCompDC, hCompBitmap );
HDC bitmapDC = CreateCompatibleDC ( hCompDC );
HBITMAP oldBitmapDCBitmap = ( HBITMAP ) SelectObject ( bitmapDC, myBitmap );
BitBlt ( hCompDC, x, y, 320, 320, bitmapDC, 0, 0, SRCCOPY );
SelectObject ( bitmapDC, oldBitmapDCBitmap );
DeleteDC ( bitmapDC );
// 호환 DC를 기본 DC에 BitBlt로 고속으로 복사한다
BitBlt ( hDC, 0, 0, 800, 600, hCompDC, 0, 0, SRCCOPY );
// 사용이 끝난 호환 DC 및 비트맵을 제거한다.
SelectObject ( hCompDC, oldBitmap );
DeleteObject ( hCompBitmap );
DeleteDC ( hCompDC );
// 창의 기본 DC를 해제한다
EndPaint ( hWnd, &ps );
}
break;
case WM_LBUTTONDOWN:
targetX = LOWORD ( lParam );
targetY = HIWORD ( lParam );
printf ( "targetX = %d, targetY = %d\n", targetX, targetY );
angle = atan2f (targetY - y, targetX - x);
break;
case WM_CLOSE:
DeleteObject ( myBitmap );
PostQuitMessage ( 0 );
break;
default:
return DefWindowProc ( hWnd, uMsg, wParam, lParam );
}
return 0;
}
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow )
{
WNDCLASS wndClass = { CS_VREDRAW | CS_HREDRAW, WndProc, 0, 0, hInstance, 0, 0, ( HBRUSH ) GetStockObject ( WHITE_BRUSH ), nullptr, L"asdf" };
RegisterClass ( &wndClass );
RECT rect = { 0, 0, 800, 600 };
AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW, FALSE );
int width = rect.right - rect.left, height = rect.bottom - rect.top;
int _x = GetSystemMetrics ( SM_CXSCREEN ) / 2 - width / 2, _y = GetSystemMetrics ( SM_CYSCREEN ) / 2 - height / 2;
HWND hWnd = CreateWindow ( L"asdf", L"Test", WS_OVERLAPPEDWINDOW, _x, _y, width, height, nullptr, nullptr, hInstance, nullptr );
ShowWindow ( hWnd, SW_SHOW );
UpdateWindow ( hWnd );
AllocConsole ();
freopen ( "CONOUT$", "w", stdout );
MSG msg;
while ( true )
{
if ( PeekMessage ( &msg, nullptr, 0, 0, PM_NOREMOVE ) )
{
if ( !GetMessage ( &msg, nullptr, 0, 0 ) )
break;
TranslateMessage ( &msg );
DispatchMessage ( &msg );
}
else
{
if ( targetX != x )
{
float tempX = x;
x += cos ( angle );
if ( ( tempX < targetX && x > targetX ) || ( tempX > targetX && x < targetX ) )
x = targetX;
}
if ( targetY != y )
{
float tempY = y;
y += sin ( angle );
if ( ( tempY < targetY && y > targetY ) || ( tempY > targetY && y < targetY ) )
y = targetY;
}
printf ( "x: %f, y: %f\n", x, y );
InvalidateRect ( hWnd, nullptr, FALSE );
Sleep ( 1 );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment