How to Program a 2D Engine
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
May be interested
- Complete how to install Cheat Engine and use Cheat Engine when playing gameswhat is cheat engine? cheat engine is a game support tool, help cheat game, change game parameters or increase certain stats in the game. here's how to install cheat engine and use cheat engine when playing games.
- What is Apple's Neural Engine? How does it work?your iphone, ipad, mac, and apple tv use a dedicated npu (neural processing unit) called the apple neural engine (ane) that is much faster and more energy efficient than a cpu or gpu.
- The trick to changing the search engine Safari browser on Mac OS X is quick and simpleyou are tired of thinking about how to change the search engine in safari. assuming you want to use the google search engine but on your safari, the default search engine is yahoo.
- What is search engine? Learn about search enginessearch engine is software accessed on the internet to search information databases according to user queries. this tool provides a list of results that best match what the user is trying to find.
- Some basic concepts to know about Search Engine Optimization - SEO (part 1)the concept of seo - search engine optimization, or often referred to as search engine optimization, is probably not strange to many of us. but knowing the nature as well as taking advantage of seo is not everyone knows and performs properly. in the following article, we will introduce you the concepts of v &
- Successfully tested the Mach 5 enginethe mach 5 is about 50% faster than the world's fastest jet engine - sr-71 blackbird.
- Is search engine submission necessary?should you also submit your web pages, or have this become as obsolete as cordless phones and fax machines? let tipsmake find the answer through the following article!
- Apple developed its own search engine for the iPhoneyahoo! is looking forward to knocking google out to become the default search engine on safari, but it is not easy because apple could develop its own search engine.
- How to change search engine on Safariwith safari browser, you can change the default search engine here from google to another search engine, such as bing ai engine.
- How to add a search engine to qBittorrentmany modern bittorrent clients have a search function. in many cases this functionality is semi-hidden, as in qbittorrent. you have to take a few extra steps to reach its full potential.