[App 개발] Xcode 2.2 에서 카본 프로그램 만들자 (6) Keyboard
본문
1. 키보드 입력 이벤트를 처리하는 루틴에서 이번에는 입력된 키보드 값을 버추얼 키 값에서 우리에게 친숙한 ASCII 값으로 바꾸었습니다. 이 값을 이용하여 화면에 글씨를 출력하게 됩니다. 컨트롤 캐릭터 중에서 13번 은 ^M 이지요. 엔터 (Carriage Return) 입니다. 27 번은 ESC 입니다. 이 예제에서 엔터를 누르면 다음 줄로 이동하게 되고, ESC 를 누르면 화면을 지우게 됩니다. 소스를 살펴 보시고요.
2. 글꼴을 그린 PNG 파일을 읽어서 그것을 36개의 CGImage 로 만들어 두었습니다. 키보드 입력에 따라서 그 값에 해당하는 CGImage 를 화면에 출력하는 것이지요. 글꼴 PNG 파일은 하나이지만, 그것을 36개로 쪼개어 두고 원본 CGImage 는 제거합니다. CGImage 객체의 데이터들은 모두 비됴 메모리로 들어가게 되어 매우 빠른 속도로 화면이 갱신됩니다.
3. font.png 파일을 프로젝트의 Resource 폴더에 넣었두고 같이 컴파일을 하면 이 파일이 한 패키지 안으로 들어가게 됩니다. 이렇게 했을 때 어떻게 이 파일을 읽어들일 것인가? 바로 ConvertToBundleFilename() 함수로 그 방법을 구현해 놓았습니다. 보시면서 "왜 캐릭터 스트링으로 만들었다가 또 다시 코어 파운데이션 스트링으로 만드는 삽질을 했을까?" 라고 제발 저한테 태클 걸지 마세요. ㅡㅡ; 이것은 다 초보자를 위하여 시범을 보인 것입니다. ㅠㅠ
소스가 좀 길고, 폰트 파일까지 같이 있기때문에 이번에는 프로젝트도 같이 포함해 놓았습니다. 프로젝트만 달랑 받아서 낼름 컴파일한 다음 "어? 글씨가 삐뚤 빼둘이야... 바붕" 하고 곰방 지워버리는 사람은 미운 사람... ㅎㅎㅎ
--- Typer.h ---
#include
void InstallTyperEventHandler( WindowRef window ) ;
OSStatus TyperEventHandler( EventHandlerCallRef myHandler,
EventRef event,
void *userData ) ;
void LoadFont( void ) ;
void ClearFont( void ) ;
void NextLine( void ) ;
void ClearScreen( WindowRef window ) ;
void DrawFont( WindowRef window, char code ) ;
--- Typer.c ---
#include "Typer.h"
#include
int cx = 0, cy = 0 ;
int mx = 16, my= 29 ;
void InstallTyperEventHandler( WindowRef window )
{
EventTypeSpec typerSpec[] = {
{ kEventClassKeyboard, kEventRawKeyDown },
} ;
OSStatus err ;
err = InstallEventHandler( GetWindowEventTarget( window ),
NewEventHandlerUPP( TyperEventHandler ),
GetEventTypeCount( typerSpec ),
typerSpec,
(void *)window,
NULL ) ;
}
OSStatus TyperEventHandler( EventHandlerCallRef myHandler,
EventRef event,
void *userData )
{
OSStatus err = eventNotHandledErr ;
char charCode ;
switch( GetEventClass( event ) ) {
case kEventClassKeyboard :
{
switch( GetEventKind( event ) )
{
case kEventRawKeyDown :
GetEventParameter( event,
kEventParamKeyMacCharCodes,
typeChar,
NULL,
sizeof( char ),
NULL,
&charCode ) ;
DrawFont( (WindowRef)userData, charCode ) ;
break ;
}
break ;
}
default:
break ;
}
return err ;
}
CGImageRef fonts[ 36 ] ;
void ConvertToBundleFilename( char *source, char *target )
{
CFStringRef filenameCFString, filePathCFString ;
CFURLRef fileLocation;
filenameCFString = CFStringCreateWithFileSystemRepresentation( NULL, source ) ;
fileLocation = CFBundleCopyResourceURL( CFBundleGetMainBundle(),
filenameCFString,
NULL,
NULL ) ;
CFRelease( filenameCFString ) ;
if ( fileLocation == NULL ) return ;
filePathCFString = CFURLCopyFileSystemPath( fileLocation, kCFURLPOSIXPathStyle ) ;
CFRelease( fileLocation ) ;
CFStringGetFileSystemRepresentation( filePathCFString,
target,
PATH_MAX ) ; // PATH_MAX is defined in
CFRelease( filePathCFString ) ;
}
void LoadFont( void )
{
CGDataProviderRef provider ;
CFStringRef path ;
CFURLRef url ;
CGImageRef myImage ;
char target[ PATH_MAX ] ;
CGRect rect ;
int i ;
ConvertToBundleFilename( "font.png", target ) ;
path = CFStringCreateWithCString( NULL,
target,
kCFStringEncodingUTF8 ) ;
url = CFURLCreateWithFileSystemPath( NULL,
path,
kCFURLPOSIXPathStyle, NULL ) ;
CFRelease( path ) ;
provider = CGDataProviderCreateWithURL( url ) ;
CFRelease( url ) ;
myImage = CGImageCreateWithPNGDataProvider( provider,
NULL,
true,
kCGRenderingIntentDefault ) ;
CGDataProviderRelease( provider ) ;
&
최신글이 없습니다.
최신글이 없습니다.
댓글목록 1
악동시니님의 댓글
집에감 해봐야지..흐흐흐..