Allegro/STL Tutorial Ch.2 Study+more

Chapter 2

2. Preparing our game

2.1 What do we need?

A game consists of many components, like graphics, sound, input device management, data file management, etc. Allegro can deal with all of these. But it can't deal with the design; you must have a good design before you can use Allegro to create games. In this tutorial, we are going to make a helicopter game, where we control a helicopter on a mission to bomb beyond known reality every tank that dares to show its ugly warhead on the game screen.
게임은 그래픽, 사운드, 입력 관리, 데이터 파일관리등 많은 구성요소로 이루어져 있다. 알레그로는 이 모든것을 지원한다. 하지만 디자인(어떤 게임을 만들것인가 등)은 다루지 못한다. 따라서 당신은 알레그로를 사용하여 게임을 만들기 전에 좋은 디자인을 구상해야 한다. 이 튜토리얼에서 우리는 헬기를 컨트롤 해서 땡크에 폭탄을 떨구는 게임을 만들것이다.
Let's begin with the backdrop. For simplicity, we will have a static unmovable backdrop. However, to show how to use Allegro drawing functions, we will generate it from within our main program, using some cool shades.
우선 배경부터 건드려 보자. 간단하게 하기 위해서 우리는 움직이지 않는 배경을 쓸 것이다. 하지만 알레그로 드로잉 함수의 사용법을 보여주기 위해서 괜찮은 모양(shape)들을 사용해서 배경을 '생성' 해 보겠다.

First, we need the cool shades. That is, we need to have a game palette first. (In this tutorial, we will be working in 256-color modes.) We can create a palette using any appropriate sprite editor or paint program. The palette I will use is within the Allegro datafile below. Use the Allegro Grabber utility to see it.
우선, 괜찮은 모양들이 필요하다. 이 말은 즉, 우리는 게임 팔레트가 필요하다는 말이다. (이 튜토리얼에서 우리는 256 컬러 모드를 사용할것이다.) 우리는 팔레트를 어떠한 종류의 스프라이트 에디터나 페인팅 프로그램으로도 생성할수 있다. 내가 사용할 팔레트는 다음의 알레그로 데이터 파일에 포함되어 있다. 알레그로 Grabber 프로그램을 사용해서 아래의 데이터 파일을 볼 수 있다.

Download the tutorial datafile
refer 페이지에 있는 실제 링크에서 파일을 다운 받도록 해라. 링크 걸기도 귀찮다.

2.2 The Grabber

GRABBER.EXE resides in the TOOLS subdirectory of your Allegro directory. To make it easier to use, either put this directory into your PATH in AUTOEXEC.BAT, or copy GRABBER.EXE into somewhere that is already in the PATH, like your DJGPP BIN directory. This will make it easier for you to start the Grabber from your project directory.
GRABBER.exe 는 알레그로 디렉토리의 TOOLS 라는 서브 디렉토리에서 찾을 수 있다. 해당 디렉토리로 이동해서 찾는게 귀찮다면 autoexec.bat의 PATH에 경로를 등록하거나 GRABBER.EXE를 니가 간단히 접근할수 있는 디렉토리에 복사해 둬라. (나같으면 걍 바탕화면에 버려둘거 같긴 하다만...)
When you have downloaded the tutorial datafile and placed it into your project directory, start Grabber. From the File menu, select Load, and load TUTORIAL.DAT. (You could also have specified the datafile on the command line.) The datafile elements of our game is now listed. You may look through them now, and note the convenience of having everything in one datafile, but for more details about how the Grabber works, consult its help system. For now, though, we just need it to generate the header file tutorial.h. From the File menu, choose Save. Once saved, close the Grabber by choosing Quit.
다운받은 데이터 파일을 프로젝트 디렉토리에 복사했다면, Grabber를 실행하자. 파일 메뉴에서 Load를 선택하고 tutorial.dat 파일을 로드하자. (뭐, 커맨드 라인 옵션으로 선택할 수 도 있다.) 우리 게임에서 사용될것들이 보일것이다. 지금부터 우리가 할 일은 tutorial.h를 데이터 파일로부터 생성해 내는것이다. 파일 메뉴에서 Save를 선택하자. 저장한 다음에 Grabber를 종료하자.

2.3 The Datafile

As you have now seen, all the data objects we need, like graphics and sounds, are contained in the datafile. The Grabber also created a header file (tutorial.h) that our main program can use to reference specific objects within the datafile whenever it needs to use them.
당신이 본대로, 데이터 파일에 우리가 필요로 하는 그래픽, 사운드 등이 모두 포함되어 있다. Grabber는 데이터 파일에 있는 object로부터 필요한 정보를 뽑아서 tutorial.h를 만들어 준다.
But how is it actually used? To see how, we'll write some more code. Start RHIDE again. Note that RHIDE will this time automatically load your project, since it's the only project existing in the directory. (This may cause troubles if you use the Save Options feature improperly, since options are saved in a project file, but that's another matter.)
근데, 어떻게 쓰지? 이런 당신을 위해서 내가 코드를 작성해 줬다. RHIDE를 시작한 다음에 프로젝트를 로드하자.(물론, RHIDE를 실행할때 마지막 프로젝트를 자동으로 로드하긴 한다.)

Now change your program to read:
코드를 다음과 같이 변경하자.

#include <allegro.h>
#include "tutorial.h"

DATAFILE* data;

int main()
{
  allegro_init();
  install_keyboard();

  data=load_datafile("tutorial.dat");

  set_gfx_mode(GFX_VGA,320,200,0,0);

  set_palette((RGB*)data[TUT_GAMEPAL].dat);

  textout_centre(screen,font,"Ready. Beep.",160,100,255);
  readkey();
  return 0;
}
Let's go through this program and see what it actually does. First, obviously, is the #include directives, that loads the definitions we need. tutorial.h, the one Grabber created, contains the TUT_GAMEPAL definition. Then we define a global variable, data, that is a pointer to DATAFILE entries. In the main program, we initialize Allegro with allegro_init(), install Allegro's keyboard subsystem with install_keyboard(), then load the datafile into memory, and assigns it to our global variable data. load_datafile() takes care of everything necessary to load it, including allocating memory, decompressing, and converting, and returns a pointer to the loaded and ready-to-use datafile.
If you are used to real-mode compilers, remember that DJGPP is a 32-bit protected-mode compiler, so we are working in a flat address space, with virtual memory and all, so there is no need to worry about exhausting memory by keeping the entire datafile loaded (unless your datafile is approaching something like 50 megabytes, of course).
자, 이제 이 프로그램이 어떻게 바꼈고, 뭔짓을 하는지 보자.우선 #include 지시자가 Grabber에서 생성한 tutorial.h-TUT_GAMEPAL 가 있는-를 포함하도록 추가되었다. DATAFILE을 가리키는 전역 변수도 하나 만들었다. main 함수 내에서는 알레그로 초기화에 사용되는 allegro_init(), 키보드 사용을 위한 install_keyboard() 함수를 호출하고 데이터 파일을 메모리에 로드하고 번역 변수에 할당했다. load_datafile() 함수하나만 부르면 데이터 파일을 메모리에 로드해서 decompress/convert/메모리 주소 반환해 주는등 바로 사용할 준비가 끝난다. 만약 당신이 리얼모드 컴파일러를 사용한다면, 물론 DJGPP는 32bit 보호모드 컴파일러지만, 우리는 선형 주소(가상 메모리를 포함해서)를 사용하게 된다.(...?) 따라서 메모리를 바닥내 버릴까봐 걱정할 필요는 없다. (데이터 파일이 50메가가 넘어가면 어찌될지는 모르겠지만.. 쩝..)

After we have entered VGA 320x200 256-color graphics mode (mode 13h) with set_gfx_mode(), we then sets the palette from the datafile. As you can see, data points to an array of DATAFILE structures, of which the dat field points to the actual data. Since dat is a void * pointer, we need to explicitly cast it to the appropriate data type, otherwise the compiler will complain that ANSI C++ forbids implicit casting. set_palette() expects an argument of type RGB *.
그래픽 모드를 VGA 320x200에 256 컬러를 사용하도록 설정한 다음에 데이터 파일에 있는 팔레트를 설정했다. 소스코드에서 보이다 시피, data 전역 변수는 DATAFILE 구조체 배열의 시작 주소를 가리킨다. data.dat 는 void * 기때문에 명시적 형 변환이 필요하다. 그렇지 않다면 컴파일러는 에러를 뱉어 낼 것이다. (ANSI C++는 묵시적 형변환을 지원하지 않는다.) 왜냐하면 set_palette()는 RGB* 타입의 매개변수를 요구하기 때문이다.

Then we output the usual text at the center of the screen and waits for a key, as before.
그리고, 이전 챕터의 마지막 예와 동일하게, 화면의 중앙에 글자를 출력하고 키 입력을 기다린다.

We can now proceed to the next chapter
훔훔, 이번 챕터는 캐 날림 번역으로 마무리, 하긴 챕터1도 발번역이긴 했지만 : )

refer: http://www.ping.uio.no/~ovehk/allegro/tut2.html

트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://handmade.egloos.com/tb/5068430 [도움말]

덧글

덧글 입력 영역