I followed this tutorial to create a Game Engine in C++ and SFML .
I was apprehensive, since creating a Game Engine sounds like a vaguely daunting task.
I am getting the hang of the setup for SFML, and decided that I don’t need to keep practicing it every time so I exported the template of the properties for this project once I had set it up so that I can reuse them in the future.
[The property changes are fairly simple.
1. Go into the Properties of the project.
4. And under Linker -> Input -> Additional Dependencies, add the graphics, window, and system .lib file names spearated by semi-colons(;).
Press ‘Apply’ and ‘OK’]
5. Also, ensure that the .dll files are held in the Debug folder of your Project.
Once this was all complete for my Game Engine project, I began to follow the tutorial.
There’s no point in me repeating what you can find here, since I did pretty much exactly that. There were points (such as creating three separate .cpp files for Input, Update, and Draw…?) where I was unsure whether the tutorial was going about it in the right way, but as only my third project in SFML, I didn’t feel that this was the time to be going rogue.
I will, however, summarise the Game Engine.
The Game Engine consists of 6 .cpp (C++) files and 2 .h (Header) files (not including targetver.h, stdafx.h, and stdafx.cpp, which are all there as default).
These files are: Main.cpp, Engine.cpp, and Player.cpp; Input.cpp, Update.cpp and Draw.cpp; and Engine.h and Player.h.
Main.cpp simply calls the constructor for Engine, calls the start method for Engine, and returns 0 when complete. This is the entire functionality of the program, on the highest level (the game engine is made and started, when it is finished, the program finishes).
Engine.cpp and Engine.h work together to provide the overall functionality at a slightly lower level. Engine.cpp sets up the window size and background texture when constructed, and its ‘start’ method initialises a clock and contains a while loop calling input, update (and passing it the current time), and draw methods as long a the window is open. Engine.h just contains all of the variables and methods of Engine, and is included by Engine.cpp to be able to use these.
Player.cpp and Player.h (called ‘Bob’ in the tutorial) provide player setup and functionality. The contructor brings in the texture for the player, and initialises the speed, and x-y position of the player. There are then 6 methods: getSprite(), moveLeft(), moveRight(), stopLeft(), stopRight(), and update(). The only one of any real interest is update(), which takes in a time parameter, to be multiplied by the declared speed to calculate the distance traveled and add it to the x position (right), or subtract it from the x position (left). Again, Player.h simply creates all of the required variables and methods.
Input.cpp just takes user keyboard input and calls the appropriate method- wouldn’t this be better in the Player.cpp and Engine.cpp files?
Update.cpp just calls the update method in player, passing it the time from Engine.cpp. This call could be in Engine.cpp right?
Draw.cpp clears the window, then adds in the background and player sprites and then calls display();.
And that’s it! All of these parts will work together to run a simple game, and more functionality could always be added to these components to create a more complex game using this Game Engine.
Please comment below if you can explain to me why there is a need for separate Update.cpp, Draw.cpp and Input.cpp files.