본문 바로가기
프로그래밍/API

API-애니메이션

by 리뷰하는 (게임)프로그래머_리프TV 2010. 5. 10.



애니메이션의 기본은 여러장, 또는 한장이지만 부분별로 쪼개어 번갈아 가면서 계속 보여줌으로써 애니메이션을 연출 할 수 있다.

긴 설명이 필요 할거 같지는 않고,

다음의 이미지를 번갈아서 출력한다고 생각해 보자.


이미지의 크기는 x축 48*12, y출 48이다.

따로 애니메이션을 플레이 해주는 함수가 존재하는 것은 아니고,

BitBlt 함수를 사용해 간단하게 화면에 뿌려 줄 수 있다.

예제를 보면 쉽게 이해 할 수 있다.

#include <windows.h>
#include "resource.h"

int frame;
HBITMAP hBitDice;

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
HINSTANCE g_hInst;
LPCTSTR lpszClass = TEXT("애니메이션");

int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
					 LPSTR lpszCmdParam, int nCmdShow )
{
	HWND hWnd;
	MSG Message;
	WNDCLASS WndClass;
	g_hInst = hInstance;

	WndClass.cbClsExtra = 0;
	WndClass.cbWndExtra = 0;
	WndClass.hbrBackground = (HBRUSH)GetStockObject( COLOR_WINDOW+1 );
	WndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
	WndClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	WndClass.hInstance = hInstance;
	WndClass.lpfnWndProc = WndProc;
	WndClass.lpszClassName = lpszClass;
	WndClass.lpszMenuName = NULL;
	WndClass.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass( &WndClass );

	hWnd = CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
		NULL, (HMENU)NULL, hInstance, NULL );
	ShowWindow( hWnd, nCmdShow );

	while( GetMessage( &Message, NULL, 0, 0 ) )
	{
		TranslateMessage( &Message );
		DispatchMessage( &Message );
	}
	return (int)Message.wParam;
}

LRESULT CALLBACK WndProc( HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam )
{
	HDC hdc, MemDC;
	HBITMAP hPreBit;
	PAINTSTRUCT ps;

	switch( iMessage )
	{
	case WM_CREATE:
		hBitDice = LoadBitmap( g_hInst, MAKEINTRESOURCE( IDB_DICE ) );
		frame = 0;
		SetTimer( hWnd, 1, 200, NULL );
		return 0;
	case WM_TIMER:
		++frame;
		frame %= 12;
		InvalidateRect( hWnd, NULL, FALSE );
		return 0;
	case WM_DESTROY:
		KillTimer( hWnd, 1 );
		PostQuitMessage(0);
		return 0;
	case WM_PAINT:
		hdc = BeginPaint( hWnd, &ps );
		MemDC = CreateCompatibleDC( hdc );

		hPreBit = (HBITMAP) SelectObject( MemDC, hBitDice );

		BitBlt( hdc, 100, 100, 48, 48, MemDC, 48*frame, 0, SRCCOPY ); 
		
		SelectObject( MemDC, hPreBit );

		DeleteObject( MemDC );

		DeleteObject( hPreBit );

		EndPaint( hWnd, &ps );
		return 0;
	}
	return( DefWindowProc( hWnd, iMessage, wParam, lParam));
}​

예제에서 보면

타이머를 사용해 frame를 수치를 조정하면서, 이미지를 48*fream, 0 으로 보여주는 것을 확인 할 수 있다.

즉 이미지 전체를 띄우는 것이 아니라.

이미지의 부분 부분을 잘라내어 특정 시간을 두고 조금씩 그 위치를 변경하는 것이다.

// 실행 화면

1부터 12까지 꾸준하게 번갈아 가면서 출력된다.

'프로그래밍 > API' 카테고리의 다른 글

API-ini파일  (1) 2010.05.10
API-알파값(비트맵 이미지 투명화)  (0) 2010.05.10
API-PlaySound  (0) 2010.05.10
API-더블 버퍼링  (0) 2010.05.10
API-ChildWnd  (0) 2010.04.24