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
- 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.
- C is a good and fast choice.
- C++ is much better in design, but suffers on performance a bit.
- Java can be good. It's OS independent, speed and structured.
- Assembly is a hard one, but controlling it is the best bet.
- 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.
- 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.
- Wikipedia is your best friend. Search its portals and you will find amazing knowledge on every aspect.
- Buying the books on programming games might come in handy.
- 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
- 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.
- Gather your graphics. Draw your required graphics, like the screen, players etc.
- Divide your whole engine into different parallel parts. As you may know, parallel programming is faster. So divide it. Some portions include:-
- Rendering pipeline
- Gravity
- AI
- Next screen
- Controlling the above elements (the most important!)
- 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.
- 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
- Code the rastering. Rastering is the displaying of every pixel on the screen.
- 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.
- Use the "AND" format to draw the Sprite of the image. In BitBlt of Windows, this is the raster code: SRCAND.
- Use the above format to draw texture on the sprite.
- Code the physics. You'll need to understand and provide for the dynamics of your environment.
- Add lighting. Strictly speaking, lighting is not required in 2D engines, but you can add it if you'd like.
- Handle shadows. You can use the shadow volume technique for shadows. However, it's not needed since you don't need lighting.
- 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
- 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.
- 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".
- 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.
- 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]
- 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.
- 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
You should read it
- How to Draw a Wheelchair Shower Ramp in Siemens NX
- Cheat Engine - Download Cheat Engine here
- Corel Draw: What is Corel DRAW - Introduction
- How to draw a frame in Word
- How to draw anime cats in detail step by step
- How to Draw a Panda Using Microsoft Paint
- SVG element in HTML5
- How to draw anime hair covering one eye
- How to Draw Basic Shapes Using Open Office Draw
- Corel Draw: Draw a burning candle
- The world's largest jet engine GE9X made the first flight
- How to Paint an Engine Bay
Maybe you are interested
How to use Auto Clicker Assist to automatically click the mouse
Useful tips to improve your Smart TV experience
How to change the default font in Google Docs - Use the font of choice
This is the culprit that prevents users from updating Windows 11 24H2
What is Facetime? How to use Facetime on iPhone/iPad
How to switch users on the Linux command line