32
Chapter 6 Chapter 6 Windows 9x/XP/2000 프프프프프 프프프 (Wrapping)

Chapter 6 Windows 9x/XP/2000 프로그래밍 감싸기 (Wrapping)

  • Upload
    stasia

  • View
    42

  • Download
    8

Embed Size (px)

DESCRIPTION

Chapter 6 Windows 9x/XP/2000 프로그래밍 감싸기 (Wrapping). In This Chapter …. 윈도우즈 타이머 사용하기 메뉴 다루기 윈도우즈를 이용한 사운드 관리하기 윈도우즈로부터 정보 획득하기 WinX 게임 콘솔 이해하기. 타이밍이 중요하다. 알맞은 때에 특정한 작업을 수행하고 싶을 때 심장 박동소리를 1 초마다 나게 하거나 밀리세컨드마다 입력장치를 폴링한다거나 5 초마다 플레이어의 체력을 감소. 타이머. Timer 생성. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

Chapter 6Chapter 6

Windows 9x/XP/2000 프로그래밍 감싸기 (Wrapping)

Page 2: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

2

In This Chapter … In This Chapter …

• 윈도우즈 타이머 사용하기• 메뉴 다루기• 윈도우즈를 이용한 사운드 관리하기• 윈도우즈로부터 정보 획득하기• WinX 게임 콘솔 이해하기

Page 3: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

3

타이밍이 중요하다타이밍이 중요하다

• 알맞은 때에 특정한 작업을 수행하고 싶을 때 – 심장 박동소리를 1 초마다 나게 하거나 – 밀리세컨드마다 입력장치를 폴링한다거나 – 5 초마다 플레이어의 체력을 감소

Page 4: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

4

타이머 타이머

Page 5: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

5

Timer Timer 생성생성

UINT SetTimer(HWND hWnd, // handle of window UINT nIDEvent, // timer ID UINT uElapse, // time delay in microseconds TIMERPROC lpTimerFunc); // address of callback

Example) // the timer IDs (totally arbitrary)#define TIMER_ID1_SEC 1 // id of one-second timer#define TIMER_ID3_SEC 2 // id of three-second timercase WM_CREATE: { // create one-second timer SetTimer(hwnd, TIMER_ID1_SEC, 1000,NULL); // create three-second timer SetTimer(hwnd, TIMER_ID3_SEC, 3000,NULL); // exit event handler return(0); } break;

Page 6: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

6

Using WM_TIMER MessageUsing WM_TIMER Message

case WM_TIMER: { // timer event is present; determine which timer fired switch(wparam) { case TIMER_ID1_SEC: { // more code for first timed item } break; case TIMER_ID3_SEC: { // more code for second timed item } break; // test for other IDs; other code you require default:break; } // end switch} break;

Page 7: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

7

Killing TimerKilling Timer

• 타이머는 자원을 소비한다 . 따라서 그냥 놔두고 딴짓을 하지 않도록 해야 한다 . 즉 , 타이머를 다 사용한 후에 죽이거나 또는 WM_DESTROY 메시지에서 죽이도록 한다 .

KillTimer(hwnd,TIMER_ID1_SEC);

KillTimer(hwnd,TIMER_ID3_SEC);

Page 8: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

8

Demo: PROG6_1.CPPDemo: PROG6_1.CPP

Page 9: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

9

윈도우즈에서 시간 재기윈도우즈에서 시간 재기

• 현재의 정확한 시간 ( 밀리세컨드 ) 를 물어보고 , 시작 시간과 비교

• 100ms 를 기다리는 예제

// next line gets the current value

DWORD frame_start_time = GetTickCount();

// your code goes here

// now make sure that you have waited 100 milliseconds

while(GetTickCount() - frame_start_time < 100);

• GetTickCount: 윈도우가 부팅되고 난 후 지나간 시간 !

Page 10: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

10

자원자원 (resources) (resources)

• 프로그램에 추가하는 미디어 (media) 와 정보의 데이터베이스 • 윈도우 기반 응용 프로그램은 여러 개의 자원을 가질 수 있고 ,

이는 .EXE 파일의 끝에 추가되어 실행시간에 불려질 수 있다 . • 자원들의 예

– 비트맵– 대화 상자– 아이콘– 메뉴– 사운드– 문자열

• 자원 편집기 • .RC 확장자를 가진 자원 파일에 기술 • .RES 이진파일로 컴파일

Page 11: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

11

메뉴생성메뉴생성

• 메뉴 정의가 포함된 윈도우즈 자원 스크립트 (.RC) 를 생성한다 .

• 메뉴 항목을 정의하는 다양한 상수들을 포함하는 , 프로그램에 포함시킬 헤더 파일을 생성한다 .

• 자원 스크립트를 프로젝트에 추가한다 .• 자원 헤더 파일을 다른 .H 파일과 함께 C++ 파일에

포함시킨다 .• 메뉴를 응용 프로그램에 적재하고 윈도우에 추가하는

코드를 작성한다 .• 메뉴 항목이 선택되면 발생하는 WM_COMMAND

메뉴 - 선택 메시지를 처리하는 코드를 작성한다 .• 응용 프로그램을 자원 스크립트 파일과 함께

컴파일하여 하나의 실행 파일을 생성한다 .

Page 12: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

12

메뉴 생성 과정메뉴 생성 과정

Page 13: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

13

자원 스크립트 자원 스크립트 (.RC)(.RC)

MYMENU MENU DISCARDABLE

BEGIN

POPUP “File”

BEGIN

MENUITEM “Open”, 1000

MENUITEM “Close”, 1001

MENUITEM “Exit”, 1002

END

POPUP “Help”

BEGIN

MENUITEM “About”, 2000

END

END

Page 14: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

14

메뉴 메뉴 IDID

BEGIN

MENUITEM “Open”, 1000

MENUITEM “Close”, 1001

…• ID 는 어떤 값이든 될 수 있으나 0x7FFFF 보다 작아야• ID 를 정하는 최선의 방법은 각 최상위 단계 메뉴에 100

이나 1000 의 배수를 사용하고 , 그 최상위 레벨 메뉴 안에 들어있는 메뉴 항목들마다 1 씩 증가하는 것이다 . 예를 들어 , 메뉴 안에 네 개의 항목이 있으면 ID 를 100, 101, 102, 103 을 사용할 수 있다 .

Page 15: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

15

단축키단축키 (HotKey) (HotKey) 지정지정

• 앰퍼센드 (&) 이용

MENUITEM “E&xit”, 1002

Page 16: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

16

Header File Header File 이용이용

• .H 파일// MYMENU.H, header with menu ids// Notice the clean naming I used for the ids; you// can very quickly figure out the menu, submenu, and item#define ID_MYMENU_FILE_OPEN 1000#define ID_MYMENU_FILE_CLOSE 1001#define ID_MYMENU_FILE_EXIT 1002#define ID_MYMENU_HELP_ABOUT 2000

• .RC 파일#include “MYMENU.H”MYMENU MENU DISCARDABLEBEGIN POPUP “File” BEGIN MENUITEM “Open”, ID_MYMENU_FILE_OPEN MENUITEM “Close”, ID_MYMENU_FILE_CLOSE MENUITEM “Exit”, ID_MYMENU_FILE_EXIT …

Page 17: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

17

응용 프로그램에 자원 추가하기응용 프로그램에 자원 추가하기

• .RC 파일을 불러오기 위해서 프로젝트에 추가• .H 프로젝트에 추가 • 컴파일

Page 18: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

18

메뉴를 불러오고 윈도우에 추가하기 메뉴를 불러오고 윈도우에 추가하기 - 1- 1

• 실행 시간에 메뉴를 응용 프로그램에 불러오기 위해 , 세 가지 선택이 있다 1. 윈도우 클래스를 만들 때 메뉴를 정의하는 것

// set menu name to the ASCII name of your menuwinclass.lpszMenuName = “MYMENU”;

2. 메인 윈도우를 생성할 때 LoadMenu() 함수를 사용하여 메뉴를 불러와 첨부하는 것

// create the windowCreateWindow(WINDOW_CLASS_NAME, // class “Basic Menus”, // title WS_OVERLAPPEDWINDOW | WS_VISIBLE, // flags 0,0, // x,y WINDOW_WIDTH, // width WINDOW_HEIGHT, // height NULL, // handle to parent LoadMenu(hinstance, “MYMENU”), // handle to menu hinstance, // instance NULL); // creation parameters

Page 19: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

19

메뉴를 불러오고 윈도우에 추가하기 메뉴를 불러오고 윈도우에 추가하기 - 2- 2

3. 초기 윈도우 생성이 끝난 후에 붙이는 것

// load the menu from resource and get handle to it

HMENU hmymenu = LoadMenu(hinstance, “MYMENU”);

// attach the menu to the window and save old

SetMenu(hwnd, hmymenu);

Page 20: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

20

WM_COMMAND WM_COMMAND 메시지 처리하기 메시지 처리하기

// process menu messagescase WM_COMMAND: { // what menu item? switch(wparam) { case ID_MYMENU_FILE_OPEN: { } break; case ID_MYMENU_FILE_CLOSE: { } break; case ID_MYMENU_FILE_EXIT: { } break; case ID_MYMENU_HELP_ABOUT: { } break; default: break; } // end switch } break;

Page 21: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

21

DEMO: PROG6_2.CPPDEMO: PROG6_2.CPP

Page 22: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

22

PlaySound() PlaySound() 함수함수

BOOL PlaySound(

LPCSTR pszSound, // string identifying sound

HMODULE hmod, // executable module to play from

DWORD fdwSound); // sound flags

• pzSound: 디스크의 파일 이름이나 응용 프로그램에 포함된 .WAV 파일의 이름을 나타내는 문자열의 포인터 .

• hmod: 문맥 - 민감 (context-sensitve) 함 . 플래그 설정에 좌우됨 .

• fdsSound: 플래그 인수 . 사운드를 재생하는 속성에 따라서 어떻게 pzSound 를 해석할 것인지를 나타낸다 .( 파일 이름인지 자원 식별자인지 )

Page 23: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

23

PlaySoundPlaySound 의 이용의 이용

• To play a .WAV asynchronously from disk:PlaySound(“SOUND.WAV”,NULL,SND_FILENAME | SND_ASY

NC);

• To play a .WAV from a file in loop mode:PlaySound(“SOUND.WAV”,NULL,SND_FILENAME | SND_ASY

NC | SND_LOOP);

• To play a .WAV asynchronously from a resource (where SOUND_WAV is the integer ID of the .WAV resource):PlaySound(MAKEINTRESOURCE(SOUND_WAV),hinstance,SN

D_RESOURCE | SND_ASYNC);

• To play a .WAV from a resource in loop mode:PlaySound(MAKEINTRESOURCE(SOUND_WAV),hinstance, S

ND_RESOURCE | SND_ASYNC | SND_LOOP);

Page 24: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

24

PlaySound compile PlaySound compile 할때 할때

• WINMM.LIB 반드시 포함할 것

Page 25: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

25

DEMO: PROG6_3.CPPDEMO: PROG6_3.CPP

Page 26: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

26

Get… Get… 함수들함수들

• GetCommandLine(): 명령행 정보• GetCurrentProcess(): 실행중인 응용 프로그램

프로세스에 대한 정보• GetCurrentTime(): 현재 시간• GetExceptionCode(): 마지막 예외 코드 ( 에러 )• GetFileAttributes(): 파일의 속성• GetSystemMetrics(): 다양한 윈도우즈 설정에 대한

정보

Page 27: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

27

GetSystemMetrics() GetSystemMetrics()

int GetSystemMetrics( int nIndex); // ID of system metric to retrieve

• Table 6-2 Useful Information IDs for GetSystemMetrics()

• SM_CMOUSEBUTTONS Number of mouse buttons, or zero if no mouse is installed

• SM_CXBORDER SM_CYBORDER Width and height of window border

• SM_CXSCREEN SM_CYSCREEN Width and height of screen

• SM_CXMIN Minimum width of window• SM_CYMIN Minimum height of window• SM_SLOWMACHINE TRUE if the computer has

a slow processor; FALSE otherwise

Page 28: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

28

WinX WinX 게임 콘솔게임 콘솔

Page 29: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

29

WinX WinX 게임 콘솔의 게임 콘솔의 main loopmain loop

// perform all game-console-specific initializationGame_Init();// enter main event loopwhile(1) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { // test whether this is a quit if (msg.message == WM_QUIT) break; // translate any accelerator keys TranslateMessage(&msg); // send the message to the window proc DispatchMessage(&msg); } // end if // main game processing goes here Game_Main(); } // end while// shut down game and release all resourcesGame_Shutdown();// return to Windowsreturn(msg.wParam);

Page 30: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

30

함수들 함수들 - 1- 1

int Game_Init(void *parms)

{

// do all initialization for your game in this function

// your code goes here

// return success

return(1);

} // end Game_Init

///////////////////////////////////////////////////////////

Page 31: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

31

함수들 함수들 - 2- 2

int Game_Shutdown(void *parms){// shut down your game and release resources that you allocated in this func

tion// your code goes here// return successreturn(1);} // end Game_Shutdown///////////////////////////////////////////////////////////int Game_Main(void *parms){// this is the workhorse of your game; it will be called continuously in// real-time, like main() in C; all the calls for your game go here!// your code goes here// return successreturn(1);} // end Game_Main

Page 32: Chapter 6 Windows 9x/XP/2000  프로그래밍  감싸기 (Wrapping)

32

DEMO: WINX.CPPDEMO: WINX.CPP