Game Programming Fundamentals
Tags: gamedev, programming, tutorial, productivity
Published: 2022-01-23 15:24:14

If you are looking for an answer to the question of **How to Develop a Game**, let's think together. First of all, we are not going to talk about the need to learn a software language or game engines to program games. In fact, the game industry is a field that includes many different industries such as mathematics, art, psychology and even cinema. In other words, it is never enough to master game engines and learn a software language. So what do we need to know? Let's see together.

## Game Mechanics

One of the biggest reasons why games reach huge audiences today is Game Mechanics. In order to better understand the game mechanics, we have to think of a system. There are various sub-parts that this system controls. To understand this better, you can examine the diagram below.

![Game Mechanics](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vg0m0uoaqr5htpe8cnm.png)

## System

As you can see in the diagram above, the system takes on the following tasks:
- Runs the load and save part to load the files.
- It then enables the intro animation to run using the audio and graphic parts.
- During the use of the game interface, user inputs ensure that the graphics and sound parts work in harmony. 
- Finally, it ensures that the graphics, sound, user input, artificial intelligence and network parts work in harmony in the game.

## Resource

The Resource describes the necessary files for the game located on the hard disk.
- These files contain configuration files that describe the configuration in which the game will run by default.
- If available, it contains the video file for the intro that will be played when the game first starts.
- Contains files where the user gets information about the game before the game.
- It contains the files containing the graphics, music and chapter information in the game.

## Game Model

- It is the state of the game graphics, music and text files loaded into memory from the resource in memory.
- Image files are stored in memory or in the texture memory of the video card.
- Section, score, etc. information is kept in variables and data structures.

## Upload and Save

This part is the particle that takes the graphics, music and other information from the source and puts it into the memory (game model) and undertakes the process of saving it back to the source in case of new score situations or when the game is recorded.

**Upload:** Configuration files, Chapter Files, Image Files, Music and Sound Files, 3D Model Files, Video Files, Help Files.

**Save:** Score files and other documents to be saved during the game

## Graphics

This is the most important and hardest part of the game in my opinion. Because it is impossible to have a game without visuals. This part is responsible for painting the screen.
- Playing the Video
- Displaying the Game Interface
- Viewing Score, Help and Configuration Information
- Displaying the Game's Own Graphics

## User Inputs

A game without interaction would be like a movie. User Interaction is definitely one of the sine qua non of game mechanics that connects users to the game. The system is in constant interaction with the user. Keyboard, mouse etc. this is where we're going to use the equipment.
- Using the Game Interface
- Reviewing Help Files
- Reviewing Score Files
- Configuring (System and Game Related)
- Controlling the user's character in the game

## Sound and Music

If you want to understand the importance of sound and music, please turn off the sound while playing games. You will notice that the value of the game drops when there is no sound. We can say that the value of the sound is as valuable as the graphics of the game.
- Music playing in the background
- Sound effects for collisions and movements

## Artificial intelligence (AI)

Let's consider the intelligence level of the enemies in the game. The more humanoid they look, the more intelligent the behavior and the more fun the game will be. It doesn't just have to be the enemy. In an open world game, citizens passing by look at their wristwatches, wipe the sweat on their foreheads in sunny weather, give way to you, even argue with you, attack you when you treat animals badly, etc. All of them can go into the subject of artificial intelligence.

## Network Listening and Sending

This part is the part that allows the game to be played by more than one person. In this part, the parties send information to the game server and the information received by listening to the server is updated in the game. 

**Establishing the Session:** The players who will participate in the game agree on which protocol and on which port they will communicate at first.

**Packet Delivery:** The moves made in the game are sent to the server in packets. Then the server distributes it to all parties after making the necessary change.

**Package Pickup:** Simultaneously, the parties are aware of the changes made by listening to the server and reflect this to the game.

# Hardware

Computer game lovers always want better graphics, music, physics and better artificial intelligence. For all this to happen, one of two conditions must be met.
- software optimization should be done, new algorithms should be found,
- appropriate equipment should be used and the processing speed should be increased.

Today, new algorithms and code optimization progress much slower than the speed at which hardware develops. Now, instead of new algorithms, the software, focuses on issues such as reusability, code openness, code security (encapsulation). The hardware is trying to increase the processor speed and memory capacity as much as possible for these new requests.

In this section, we can talk about many different hardware such as the following:
- graphics cards,
- processor,
- motherboard,
- sound cards,
- monitors, speaker and new input devices etc…

# Programming Language

Programming language is a computer language that offers the opportunity to develop commands and software in a certain standard form. Thanks to programming languages, it is possible to control what kind of output a computer can give in which situation. In short, computers and humans can communicate efficiently thanks to programming languages.

Since our topic is game development, we will focus on this subject. Depending on what you want to do and create, you can develop games with all programming languages. There will be those who insist you learn C++. This is absolutely not true. You can make great games with JavaScript, C#, Python or even C. A choice-based adventure game that only works on the console screen can be great. But if you want to make games that are more professional and reach more people, please keep reading.

## C++

Since C++ is a high-level language that will teach you the basics of object-oriented programming, it's a good idea to learn it. It is also the language used to create console and Windows games. Also, it uses OpenGL or a similar framework (If you continue reading the guide, I will give a detailed explanation about OpenGL). 

C++ is a fast compiling programming language. You also get a lot of say in memory management. It has extensive libraries useful for designing and powering complex graphics. There's a lot of literature for you to improve on, because it's been the programmer's language of choice for decades, and you'll find online communities ready and willing to answer your questions.

It would be a lie if we said that C++ is easy to learn. It is difficult to learn compared to other programming languages. But it can be useful not only because C++ games are easy to distribute on various platforms, but also because if you know C++ you can quickly learn C# and other object-oriented languages. Both C++ and C# are only two of the most actively used programming languages today.

To sum it up, learning C++ is a good choice if you want to create games from scratch.

## C♯

The benefit of C# lies in the XNA framework. This is a set of tools and workspaces by Microsoft that are particularly suitable for games on Xbox or Windows platforms. 

If you compare C++ and C#, you might consider this example. C++ is like a manual transmission car. C# is like an automatic transmission car. Let's consider the Unity game engine. If you use the Unity game engine, you have to code the scripts in C#. However, the core of this game engine is C++ code.

The platform you target, the game you want to make, the game engine you will use, etc. factors will affect the language you choose. But yes learning C# would be a great idea.

## JAVA

Game programmers often use Java because Java supports multithreading and sockets. Multithreading uses less memory and makes the most of available CPU without blocking the user while intensive processes are running in the background. Sockets help in the creation of multiplayer games. Also, Java runs in a virtual machine so the game is easier to deploy.

Also, if you want to develop applications and games for Android platforms, Java would be a great option.

## JavaScript

Making games for browsers is very different from making games for consoles. If you need 3D graphics or complex graphics and a content management system, you will also need SVG or WebGL.

Flash animation using the programming language ActionScript was common for browser games in the past, but Flash is finished.

A WebGL-enabled Java engine such as [PlayCanvas](https://playcanvas.com/), developed at MIT, allows users to simultaneously work in the game with an online browser and publish to multiple platforms.

Moreover, there are great game engines that you can use JavaScript. To give an example of these, we can definitely say [PhaserJS](https://phaser.io/).

# APIs

What is API? It stands for Application Programming Interface. They are libraries that help us perform specific tasks with a number of functions they provide.

The APIs we will use in our games will be libraries that allow us to take advantage of them by providing an interface to the graphics and sound hardware. For example OpenGL, GLUT, GLU DirectX (Direct3D, DirectDraw, DirectShow, DirectInput, DirectPlayer, DirectSound, DirectMusic), Java2D, Java3D, JavaMusic, JavaNET, JavaMedia, FMOD, etc…

I think it will be very useful to learn the OpenGL library. So let's talk a little bit about OpenGL.

## OpenGL

OpenGL resides on graphics hardware and provides an interface to that hardware. This interface contains 250 different instruction sets. 200 of them are in the OpenGL core and the other 50 are in the OpenGL Utility Library. You can develop 3D interactive applications thanks to the OpenGL graphics API.

OpenGL is a platform independent API. It has been designed as an interface that performs a command flow that is run consecutively. In order to ensure this quality, it does not include commands such as windowing tasks and obtaining user input, but instead benefits from the windowing and input services provided by the platform it uses to OpenGL. Likewise, it does not contain commands for creating high-level 3D objects. But with the commands it provides, it can create an automobile, plane or molecule surface. To create the desired models of this type with OpenGL, we make use of the points, lines and polygons in the small toolkit we call the geometric primitive.

Of course, the creation of a better graphics system can be built on top of OpenGL's core structures. OpenGL Utility Library (GLU) has many high-level modeling features, Quadratic surfaces, NURBS curve and surface rendering. 

## What are the Operations Performed During Rendering?

In the following 4 stages, you can briefly see what processes are happening during the rendering process.
- Creating shapes and geometric objects with mathematical expressions (OpenGL has tools to create primitive objects such as point, line, polygon, image, bitmap)
- Arrangement of objects in three-dimensional plane and adjustment of perspective
- Calculating the color values of the objects, applying them to the screen by taking into account the light and texture values
- The conversion of the definitions and color values of all object values to pixel values, briefly the realization of the rasterization event.

## A Simple OpenGL Code Example

Doing so many options and complex operations with the OpenGL graphics system can make OpenGL complex. But basically, the structure of a program made with OpenGL is very simple. What you're doing is actually defining certain states and putting a control mechanism about which objects OpenGL should render and how it should render.

You can actually think of OpenGL as a Real Time Rendering Software. Before introducing this programming construct, let's try to grasp some terms.

```c
main(){
       initializeAwindowPlease( );
             glClearColor(0.0, 0.0, 0.0, 0.0);
             glClear(GL_COLOR_BUFFER_BIT)
             glColor3f(1.0, 1.0, 1.0);
             glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0)
             glBegin(GL_POLYGON)
                    glVertex3f(0.25, 0.25, 0.0)
                    glVertex3f(0.75, 0.25, 0.0)
                    glVertex3f(0.75, 0.75, 0.0)
                    glVertex3f(0.25, 0.75, 0.0)
             glEnd( );
             glFlush( )
             UpdateTheWindowAndCheckForEvents( );
```

### Summary

First of all, thank you very much to those who have come this far. Our conclusion from this article should be:

Game development is not just about learning programming languages and game engines.

You have to master a lot of technology. Among the technologies we did not mention are programs such as Adobe Premier, Adobe After Effects, 3D Studio Max, Lightwave, Maya and Poser. Learning and mastering each of these is only possible with experience.

That's why we talked about hardware and OpenGL. Love your computer and keyboard rather than focusing on one thing. The best and only rule of thumb to master game development is this:

Be curious, read a lot and try without fear.