What to Consider When Developing a Video Game
To make a video game of your own, you'll need a fun idea for a game, which is a big hurdle to overcome to start making a game. Let's assume you have an idea for a game and want to bring it to life. To do this, you'll need to write code to make everything work, art and sound design to make everything appealing, and you'll need to do anything you can to ensure your game runs smoothly and the experience is enjoyable for your user.
The last piece of the puzzle of making the game run smoothly is the priority for many game developers, and choosing the correct language to code your game typically ties back to this issue. C++ is "renowned for its speed and flexibility, and [its] ability to communicate directly with hardware." Python and Lua can be easier to pick up when learning a new programming language, and both come with support from many open-source game engines. These three languages have been mainly used for 2D games. They can also be used with 3D games, but it is recommended to use Python for 3D over the other two since JavaScript has limited support and Lua has no support.
C#(Sharp)
C# has support from many game engines, which include Unity and MonoGame. C# is similar to C++, and it is reported to be "less complicated and easier to set up with Visual Studio and VS Code as your Integrated Development Environment." :
Memory Management
In C++, the programmer manually manages memory, while C# has an automatic garbage collector. C# is more convenient for this task, but C++ gives the programmer more control over how the memory is used, which can lead to optimizing code to run more quickly for smoother performance. Depending on the size of the project, this amount of control over memory may not be necessary if the project is small.
Platform Dependency
C# is specific to Windows, while C++ can be compiled to run on any platform, given that you use the appropriate compiler. This flexibility is great for C++, and Microsoft is working to make C# global, but we don't know when that will be or what constraints it will come with.
Pointers
Pointers are a way of referencing an address in memory, and I will go into more detail later in the article. In C++, pointers can be used anywhere in the program, while C# can only use pointers in unsafe mode. "Unsafe code can create issues with stability and security due to its inherent complex syntax and potential for memory-related errors, such as stack overflow, accessing and overwriting system memory." The differences between Java and C++ are similar to C# and let's explore why We're going to break down these three things with a few coding examples.
Pointers
A pointer is a variable that stores the memory address as its value. Since a pointer stores an address, we can make a call-by-reference. Pointers can "create and manipulate dynamic data structures" Use the unary operator & on the variable address you want to store on the previously created pointer. To access the value stored at an address, use the unary operator * on the pointer.
#include <bits/stdc++.h>
using namespace std;
void pointers() {
int var = 20;
// Declare pointer variable
// Note that the data type of ptr and var must be the same
int* ptr;
// Declare pointer of a pointer variable
int** ptr2;
// Assign the address of a variable to a pointer
ptr = &var;
// Assign the address of a pointer to another pointer
ptr2 = &ptr;
// ptr holds the address of var
cout << "Value at ptr = " << ptr << endl;
// var holds the value of 20
cout << "Value at var = " << var << endl;
// * dereferences ptr to give the value of 20
// located at the address assigned to ptr
cout << "Value at *ptr = " << *ptr << endl;
// ptr2 holds the address of ptr
// Even pointers have addresses of their own
cout << "Value at ptr2 = " << ptr2 << endl;
// Dereferencing ptr2 once reveals that
// ptr2 references the same address as ptr
cout << "Value at *ptr2 = " << *ptr2 << endl;
// Dereferencing ptr2 twice reveals 20,
// the value you receive when dereferencing ptr once
cout << "Value at **ptr2 = " << **ptr2 << endl;
}
int main() {
pointers();
/*
* Value at ptr = 0x6caebffc54
* Value at var = 20
* Value at *ptr = 20
* Value at ptr2 = 0x6caebffc48
* Value at *ptr2 = 0x6caebffc54
* Value at **ptr2 = 20
*/
return 0;
}
Since we store the address to other data types, we simulate calling by reference. We can modify any data type within a function and reuse that updated data later in our code.
Dynamic Memory Allocation
If we expect a certain maximum-sized input, we could prepare our program to have enough memory set aside to match the worst-case scenario. This could pose a problem as a project continues to scale larger and larger. What if we could pick how much memory we need only once we know how much is needed? This would prevent unneeded memory usage and allow our code to run more efficiently. This is the key principle behind dynamic memory allocation: only set aside enough memory space to accomplish the task at hand and then free up the space immediately after.
Check out the following code
The use of the delete keyword is essential when coding in C++. Since programmers need to clear out their memory manually, developing good habits to clear out memory once it's finished being used will lead to less frustration and fewer bugs, and it avoids the dreaded Memory Leak.
Memory Leak
In C++, there is no automatic garbage collection, which means that any memory that a programmer dynamically allocates throughout the lifetime of a program needs to be freed manually after its usage by the programmer once it is no longer needed. If a programmer forgets to free this memory after its usage, it will occupy the space while the program lives and will be unavailable to other processes. Often, a function may need to be called several times, and if each call allocates more memory without removing it, a lot of unused memory will take up space. This accumulation of unwanted memory usage is referred to as a memory leak. These can drastically slow down a program, but they are the avoidable price for faster running code.
[2]
[4]
[6]
[8] https://www.geeksforgeeks.org/memory-leak-in-c-and-how-to-avoid-it/
SOCIAL SHARE CARD GENERATOR