How to Program a 2D Engine

Though there are many engines on the web. However, creating one will make you realize the true power of programming. This is a good start for moving up to 3D graphics. Master a language. This is the most basic step of what you select. You...
Part 1 of 4:

Gathering Your Tools

  1. How to Program a 2D Engine Picture 1How to Program a 2D Engine Picture 1
    Master a language. This is the most basic step of what you select. You can use C/C++ along with some functions rewritten with assembly. This will make your engine faster and reveal the innermost of the libraries you use.
    1. C is a good and fast choice.
    2. C++ is much better in design, but suffers on performance a bit.
    3. Java can be good. It's OS independent, speed and structured.
    4. Assembly is a hard one, but controlling it is the best bet.
  2. How to Program a 2D Engine Picture 2How to Program a 2D Engine Picture 2
    Select an API (optional). You could start with an API like OpenGL or DirectX, but it depends on your adventure. If you want a reliable engine and do not want to spend time, use an API. But if you have plenty of time and want to learn the innermost of the engine, then the recommendation is simple: go solo.
  3. How to Program a 2D Engine Picture 3How to Program a 2D Engine Picture 3
    Learn the basic definitions. You have to search the web for reliable content on things like raster graphics, vector graphics, lightning and shadows, collisions, physics (for a fighting simulation, of course), and much much more.
    1. Wikipedia is your best friend. Search its portals and you will find amazing knowledge on every aspect.
    2. Buying the books on programming games might come in handy.
  4. How to Program a 2D Engine Picture 4How to Program a 2D Engine Picture 4
    Prepare your mind. You will be frustrated by the difficulty of the job, but do not turn back. Always be motivated. Learn. Create. Don't look back.
Part 2 of 4:

Preparing the Design

  1. How to Program a 2D Engine Picture 5How to Program a 2D Engine Picture 5
    Think what you want to make. Prepare an idea of what you will make and what you won't. Clearing your mind at this step will help in the long run.
  2. How to Program a 2D Engine Picture 6How to Program a 2D Engine Picture 6
    Gather your graphics. Draw your required graphics, like the screen, players etc.
  3. How to Program a 2D Engine Picture 7How to Program a 2D Engine Picture 7
    Divide your whole engine into different parallel parts. As you may know, parallel programming is faster. So divide it. Some portions include:-
    1. Rendering pipeline
    2. Gravity
    3. AI
    4. Next screen
    5. Controlling the above elements (the most important!)
  4. How to Program a 2D Engine Picture 8How to Program a 2D Engine Picture 8
    Create one-shot algorithms for the elements. Just think what will happen, how and why and who will make that happen. It will help in coding.
    1. Donald Knuth said that pre-optimizations are root of the evil. Throw away the faster and the slower parts, and focus on the engine. But remember to sort them out after the engine is completed.
Part 3 of 4:

Coding the Basic Items

  1. How to Program a 2D Engine Picture 9How to Program a 2D Engine Picture 9
    Code the rastering. Rastering is the displaying of every pixel on the screen.
    1. Use the "ignore the color" format to erase the previous image. In BitBlt of Windows, this has raster code BLACKNESS and WHITENESS. Note that, you have to draw the background of the screen if you have it.
    2. Use the "AND" format to draw the Sprite of the image. In BitBlt of Windows, this is the raster code: SRCAND.
    3. Use the above format to draw texture on the sprite.
  2. How to Program a 2D Engine Picture 10How to Program a 2D Engine Picture 10
    Code the physics. You'll need to understand and provide for the dynamics of your environment.
  3. How to Program a 2D Engine Picture 11How to Program a 2D Engine Picture 11
    Add lighting. Strictly speaking, lighting is not required in 2D engines, but you can add it if you'd like.
  4. How to Program a 2D Engine Picture 12How to Program a 2D Engine Picture 12
    Handle shadows. You can use the shadow volume technique for shadows. However, it's not needed since you don't need lighting.
  5. How to Program a 2D Engine Picture 13How to Program a 2D Engine Picture 13
    Code the AI. Depending on the type of engine and game, you can think of yourself that "What would I do in this situation" and create its algorithm. For example, in a fighting simulation, if the AI is at 4th floor, and there are 2 opponents at the first floor and a rocket launcher spawned at the 3rd floor, then instead of going straight to the first floor, the AI should head to the 3rd floor. If you are using OOP, then in your AI class, this will be a decide function.
Part 4 of 4:

Optimizing

  1. How to Program a 2D Engine Picture 14How to Program a 2D Engine Picture 14
    Use double screen buffering. This involves drawing everything to an bitmap in the memory, and then drawing the bitmap to the screen.This will be a bit slow and memory inefficient, but it will remove flicker and make the engine more smooth.
  2. How to Program a 2D Engine Picture 15How to Program a 2D Engine Picture 15
    Consider the sequence of steps in algorithms. For example, in a simple animation, a simple algorithm will state that "erase:decide:draw". However, if the decide is more complex, there will be a significant difference in the erase and draw, producing flicker. You must use "decide:erase:draw".
  3. How to Program a 2D Engine Picture 16How to Program a 2D Engine Picture 16
    Use custom allocators. When the engine starts, occupy a useful amount of RAM and divide it in your engine using programming. The OS is busy and will show latency in fulfilling your demand, so have RAM in advance.
  4. How to Program a 2D Engine Picture 17How to Program a 2D Engine Picture 17
    Use your cache wisely. Use some of the most important elements often so that they are stored in L1 cache. Doing this would speed things. Cache of L2 uses 7ns, whereas reading 1MB from RAM takes 250 us.[1]
  5. How to Program a 2D Engine Picture 18How to Program a 2D Engine Picture 18
    Make sure you use parallel processing. Use SIMD special instructions to work in parallel. Moreover, take full advantages of so many cores of processor by creating parallel units. The CreateThread function in Windows and Fork in Linux are beneficial.
  6. How to Program a 2D Engine Picture 19How to Program a 2D Engine Picture 19
    Make sure that your parallel world gets to GPU as well. The latest integrated GPUs have 10 execution cores with 1200 MHz. Too slow to compete with GTX 8800, but is an overkill for your engine.
4 ★ | 1 Vote