1 / 32

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

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

stasia
Télécharger la présentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  4. 타이머

  5. 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 timer case 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;

  6. Using 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;

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

  8. Demo: PROG6_1.CPP

  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: 윈도우가 부팅되고 난 후 지나간 시간!

  10. 자원(resources) • 프로그램에 추가하는 미디어(media)와 정보의 데이터베이스 • 윈도우 기반 응용 프로그램은 여러 개의 자원을 가질 수 있고, 이는 .EXE 파일의 끝에 추가되어 실행시간에 불려질 수 있다. • 자원들의 예 • 비트맵 • 대화 상자 • 아이콘 • 메뉴 • 사운드 • 문자열 • 자원 편집기 • .RC 확장자를 가진 자원 파일에 기술 • .RES 이진파일로 컴파일

  11. 메뉴생성 • 메뉴 정의가 포함된 윈도우즈 자원 스크립트 (.RC)를 생성한다. • 메뉴 항목을 정의하는 다양한 상수들을 포함하는, 프로그램에 포함시킬 헤더 파일을 생성한다. • 자원 스크립트를 프로젝트에 추가한다. • 자원 헤더 파일을 다른 .H 파일과 함께 C++ 파일에 포함시킨다. • 메뉴를 응용 프로그램에 적재하고 윈도우에 추가하는 코드를 작성한다. • 메뉴 항목이 선택되면 발생하는 WM_COMMAND 메뉴-선택 메시지를 처리하는 코드를 작성한다. • 응용 프로그램을 자원 스크립트 파일과 함께 컴파일하여 하나의 실행 파일을 생성한다.

  12. 메뉴 생성 과정

  13. 자원 스크립트 (.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

  14. 메뉴 ID … BEGIN MENUITEM “Open”, 1000 MENUITEM “Close”, 1001 … • ID는 어떤 값이든 될 수 있으나 0x7FFFF보다 작아야 • ID를 정하는 최선의 방법은 각 최상위 단계 메뉴에 100 이나 1000의 배수를 사용하고, 그 최상위 레벨 메뉴 안에 들어있는 메뉴 항목들마다 1씩 증가하는 것이다. 예를 들어, 메뉴 안에 네 개의 항목이 있으면 ID를 100, 101, 102, 103을 사용할 수 있다.

  15. 단축키(HotKey) 지정 • 앰퍼센드(&) 이용 MENUITEM “E&xit”, 1002

  16. 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 DISCARDABLE BEGIN POPUP “File” BEGIN MENUITEM “Open”, ID_MYMENU_FILE_OPEN MENUITEM “Close”, ID_MYMENU_FILE_CLOSE MENUITEM “Exit”, ID_MYMENU_FILE_EXIT …

  17. 응용 프로그램에 자원 추가하기 • .RC 파일을 불러오기 위해서 프로젝트에 추가 • .H 프로젝트에 추가 • 컴파일

  18. 메뉴를 불러오고 윈도우에 추가하기 - 1 • 실행 시간에 메뉴를 응용 프로그램에 불러오기 위해, 세 가지 선택이 있다 • 윈도우 클래스를 만들 때 메뉴를 정의하는 것 // set menu name to the ASCII name of your menu winclass.lpszMenuName = “MYMENU”; • 메인 윈도우를 생성할 때 LoadMenu() 함수를 사용하여 메뉴를 불러와 첨부하는 것 // create the window CreateWindow(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

  19. 메뉴를 불러오고 윈도우에 추가하기 - 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);

  20. WM_COMMAND 메시지 처리하기 // process menu messages case 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;

  21. DEMO: PROG6_2.CPP

  22. 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를 해석할 것인지를 나타낸다.(파일 이름인지 자원 식별자인지)

  23. PlaySound의 이용 • To play a .WAV asynchronously from disk: PlaySound(“SOUND.WAV”,NULL,SND_FILENAME | SND_ASYNC); • To play a .WAV from a file in loop mode: PlaySound(“SOUND.WAV”,NULL,SND_FILENAME | SND_ASYNC | 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,SND_RESOURCE | SND_ASYNC); • To play a .WAV from a resource in loop mode: PlaySound(MAKEINTRESOURCE(SOUND_WAV),hinstance, SND_RESOURCE | SND_ASYNC | SND_LOOP);

  24. PlaySound compile 할때 • WINMM.LIB 반드시 포함할 것

  25. DEMO: PROG6_3.CPP

  26. Get… 함수들 • GetCommandLine(): 명령행 정보 • GetCurrentProcess(): 실행중인 응용 프로그램 프로세스에 대한 정보 • GetCurrentTime(): 현재 시간 • GetExceptionCode(): 마지막 예외 코드(에러) • GetFileAttributes(): 파일의 속성 • GetSystemMetrics(): 다양한 윈도우즈 설정에 대한 정보

  27. 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

  28. WinX 게임 콘솔

  29. WinX 게임 콘솔의 main loop // perform all game-console-specific initialization Game_Init(); // enter main event loop while(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 resources Game_Shutdown(); // return to Windows return(msg.wParam);

  30. 함수들 - 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 ///////////////////////////////////////////////////////////

  31. 함수들 - 2 int Game_Shutdown(void *parms) { // shut down your game and release resources that you allocated in this function // your code goes here // return success return(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 success return(1); } // end Game_Main

  32. DEMO: WINX.CPP

More Related