Implementation of a 2D Game Engine Using Directx 8.1
Total Page:16
File Type:pdf, Size:1020Kb
Computer Science Daniel Linds¨ath and Martin Persson Implementation of a 2D Game Engine Using DirectX 8.1 Bachelor’s Project 2004:24 Implementation of a 2D Game Engine Using DirectX 8.1 Daniel Linds¨ath and Martin Persson c 2006 The author(s) and Karlstad University This report is submitted in partial fulfillment of the requirements for the Bachelor’s degree in Computer Science. All material in this report which is not our own work has been identified and no material is included for which a degree has previously been conferred. Daniel Linds¨ath Martin Persson Approved, 2004-06-03 Advisor: Hannes Persson Examiner: Donald F. Ross iii Abstract This paper describes our game engine written in C++, using the DirectX libraries for graphics, sound and input. Since the engine is written using DirectX, an introduction to this system is given. The report gives a description of the structure of the game and the game kernel. Following this is a description of the graphics engine and its core components. The main focus of the engine is on the physics and how it is used in the game to simulate reality. Input is discussed briefly, with examples to show how it relates to the physics engine. Implementation of audio in the game engine is not described, but a general description of how sound is used in games is given. A theory for the basics of how artificial intelligence can be used in the engine is presented. The system for the architecture of the levels is described as is its connection to the graphics engine. The last section of the report is an evaluation and suggestions for what to do in the future. A user manual for the level editor is included as an appendix. v Contents 1 Introduction 1 1.1 Problem ................................. 1 1.2 Purpose ................................. 2 1.3 Limitation ................................ 2 1.4 Goal ................................... 2 1.5 Disposition ............................... 3 2 DirectX and COM 4 2.1 The DirectX Kernel ........................... 4 2.2 HAL and HEL ............................. 6 2.3 The Components of DirectX ...................... 6 2.3.1 DirectDraw ........................... 6 2.3.2 DirectSound ........................... 8 2.3.3 DirectSound3D ......................... 8 2.3.4 DirectMusic ........................... 8 2.3.5 DirectInput ........................... 8 2.3.6 DirectPlay ............................ 9 2.3.7 Direct3D ............................. 9 2.3.8 DirectSetup ........................... 9 2.3.9 DirectX Graphics ........................ 9 2.3.10 DirectX Audio ......................... 10 2.3.11 DirectShow ........................... 10 2.4 COM ................................... 10 2.4.1 The COM Objects ....................... 11 2.4.2 GUID .............................. 11 2.5 DirectX and COM ........................... 12 vi 3 Game Engine Structure 13 4 The Kernel 16 5 Graphics Engine 18 5.1 Bitmaps ................................. 18 5.2 Bitmap Object Blitter ......................... 20 6 Physics 22 6.1 Newtonian Mechanics .......................... 22 6.1.1 Movements — Acceleration and Inertia ............ 22 6.1.2 Drag ............................... 23 6.1.3 Free-fall ............................. 24 6.2 Physics Within the Game ....................... 25 6.3 Collision Detect ............................. 27 6.3.1 Running Through Walls .................... 27 6.3.2 The Bresenham Algorithm ................... 29 7 Input 32 7.1 Forces .................................. 32 7.2 Impulses ................................. 33 8 Sound and Music 34 8.1 Sound in the Engine .......................... 34 9 The Basics of Artificial Intelligence 35 10 The Level System 36 11 Conclusion 38 11.1 Achievements .............................. 38 vii 11.2 Evaluation ................................ 39 11.3 Future Plans ............................... 40 References 42 A User Manual for the Map Editor 43 B winconsole.cpp 45 C motor.h 48 D bob.h 52 E game.h 55 E.1 Terrain .................................. 56 E.2 Character ................................ 56 E.3 Enemy .................................. 57 E.4 Player .................................. 58 E.5 Sublevel ................................. 58 E.6 Level ................................... 59 E.7 Game .................................. 59 F mapeditor.h 61 G Screenshot 64 viii List of Figures 2.1 “Classical” Programming vs. DirectX ................. 5 2.2 DirectX and Windows ......................... 7 3.1 Module Overview ............................ 13 3.2 Object Diagram of the Game Structure ................ 14 4.1 Game Kernel structure ......................... 16 5.1 Bitmap Example ............................ 18 5.2 BOB Inheritage ............................. 21 6.1 Velocity Vector ............................. 26 6.2 Collision Example ............................ 28 6.3 Another Collision Example ....................... 28 6.4 Line Approximations .......................... 30 10.1 The Different Layers .......................... 37 A.1 Empty Level Editor ........................... 43 G.1 A Screen Shot of an Example Game .................. 64 ix 1 Introduction According to the authors, the gaming industry of today focuses solely on 3D games, often with wondrous graphics as their main goal. This has had the effect that modern games seldom have any depth, story or “feel” to them. The authors think that this is a shame, and decided to create a 2D game engine in the hopes of returning some of the feel of the old games. 1.1 Problem A 2D game engine is a relatively complex thing to design. The main problem is to make the physics in the game correspond — in a good looking way — to the objects on the screen, using the existing graphics engine BOB [1]. The engine will, except for the the graphics API, be built from scratch. Problems that have to be solved is how to make the architecture of the levels1 be useful to the physics. A great part of the problem with the physics will be collision detection between the player and the map2 itself. The input handling using DirectInput will have to be integrated to the movement physics of the player. How to make the enemies in the game orientate themselves in the environment will also have to be solved. A level editor using the same map system as the game will also have to be implemented so that levels created using that editor can be loaded into the game. The main part of the work will be programming the engine itself, and parallel to the implementation write a documentation describing the engine. 1A level is an object containing a set of terrain, a number of enemies and events which one may interact with. 2The terrain parts of a level. Sometimes used interchangeably with level. 1 1.2 Purpose The purpose of this dissertation is to create a fully working game engine that can be used to create two dimensional platform games using DirectX. The engine will also be the base that Martin will use when he creates his first commercial computer game. 1.3 Limitation A fully functioning game engine is, of course, a massive project that can hardly be completed in the amount of time available for this paper. The authors have therefore decided to create the level system and the game physics and then make the rest of the details if there is enough time. 1.4 Goal When this dissertation is complete the following shall be done: • A well documented and working game engine API that allows programmers to create 2D games with a minimum amount of foundation coding, so that they may focus on the gaming experience. • A good implementation design that will make it easy to add new features later. • A working two dimensional physics engine. • A useful level editor that can be used to create the maps of the game. • A simple and general way to program the AI of the enemies in the game. • A virtual class for enemies. 2 1.5 Disposition Section 2 gives a shallow, technical introdction to DirectX and COM. These tools are then used in sections 5, 7 and 8. Section 3 shows how the modules of the game are connected and gives an introduction to sections 4 through 9. Section 4 gives a description of the game kernel which handles startup, exec- tution and shutdown of the game engine itself. Section 5 explains image formats, and relates back to the second section with DirectDraw. Sections 6 and 7 show how a player may alter the playing realm through input and physics. Section 6 also describes some of the issues related to physics in games. Sections 8 and 9 discusses sound and artificial intelligence, none of which have actually been implemented yet. Instead they show how an implementation could be used in the future and what to think about. Section 10 describes how the system for levels and maps works. This is the sys- tem that tells the graphics engine what to draw and where. The levels also contain all the information used by the physics engine, such as gravity and atmospheric density. Finally, Section 11 is a conclusion which summarizes the whole thesis. 3 2 DirectX and COM The purpose of this chapter is to give the reader a brief introduction to the basic concepts of the DirectX Application Programming Interface3 (API) and Microsoft Component Object Model (COM). The chapter comprises: • An introduction to DirectX • An introduction to COM • How DirectX and COM relate 2.1 The DirectX Kernel DirectX is an abstraction of many sub components that allows the programmer to utilize any hardware that is DirectX compatible. Rather than creating a module for each hardware manufacturer (as was common a few years ago), a programmer