Creating and using screens |
Top Previous Next |
About screens A screen is the actual display where the sprites, maps and graphics will appear. By bypassing Windows and communicating directly with the video hardware, the DirectX 2D command set achieves fast frame rates, smooth animation and rapid response to user input. The screen contains two buffers that are accessible by the 2D command set, namely the FrontBuffer and the BackBuffer. The FrontBuffer is what you see on the monitor. It is the memory on the video card being displayed to the user. The BackBuffer can be thought of as a holding area where images are drawn ready to be displayed. By swapping the two buffers, known as flipping, the actual drawing can be hidden until ready to be shown. This allows for the illusion of animation and is how all games are written. Creating the screen Before any images, sprites or maps can be drawn the screen must first be created. This is done with one of two commands. CREATESCREEN creates a full screen exclusive 2D program and ATTACHSCREEN attaches a 2D screen to a regular window. For now we will concentrate on the full screen kind. Windowed mode will be covered later. CREATESCREEN is easy to use and you need only specify the width, height and desired bits per pixel (bpp). It is recommended for today's modern video cards to use 16 or 32 bpp as most cards are optimized for that color depth. For width and height use standard screen sizes such as 640x480, 800x600, 1024x768, etc. The larger the screen the more memory is required to show it. CREATESCREEN returns 0 if the screen was successfully opened and < 0 on error. Example of creating a screen: IF CREATESCREEN( 800, 600, 32) < 0 In the example, we tested to see if the screen was created and if not, displayed a message box and ended the program. For a real program you could try other screen resolutions if creating the screen fails before giving an error. Closing the screen When the program ends you must close the screen to return control of the buffer memory back to Windows. This is done with the CLOSESCREEN command. Before you close the screen make sure all sprites and maps are freed. You must also close the screen if you wish to change resolutions or bits per pixel.
Buffers and flipping As mentioned earlier there are two buffers that you will work with in any 2D DirectX program. The BackBuffer where you normally draw onto and the FrontBuffer that is currently being displayed to the user. To swap the front and back buffers use the FLIP command. The FLIP command has an optional argument to disable waiting for the monitor to refresh (VSYNC). FLIP returns the current FPS (frames per second) speed. For example: speed = FLIP( TRUE ) Will swap the front and back buffers and not wait for VSYNC. By not waiting for the monitor refresh, maximum frame rates can be achieved. There are times, however, where it is beneficial to wait for the monitor refresh. Not every program needs fast frame rates or appear to be real time. Flipping as fast as possible also puts the most strain on the system which means multitasking will be slower, etc. The default is to flip and wait for VSYNC so its not necessary to supply an argument if you don't need the faster frame rates, as in: speed = FLIP There is also a third buffer called the SpriteBuffer that is never directly shown on the screen or flipped but is used to link with a sprites image surface for drawing operations.
Filling the buffer with a solid color Use the FILLSCREEN command to fill a buffer with a color. This is usually the first statement in a game loop to erase any graphics left over from the previous flip. remember that flipping swaps the front and back buffers so the old front buffer becomes the new back buffer. If you don't fill the buffer you will have graphic remnants left over from the the previous flip. Here is a complete example that opens a screen, creates a basic loop that waits for a key to close, fills the BackBuffer (default) and handles flipping: IF CREATESCREEN( 800, 600, 32) < 0 The above code is a basic skeleton that forms the basis of all 2D programs. |