Skip to content

Project Structure

Matthew Hopson edited this page Dec 26, 2019 · 4 revisions

This article is a quick overview what each folder (And some files) do, and how it all fits together.

As the project is in very early stages, it is uncertain where relations will be between different classes in the code, and so there are a a few classes with multiple responsibilities, which could later get split out (if needed, for now it should be ok).

Overview

Folder Explanation
cmake_modules This is just where FindSFML.cmake lives, which is for building the project (indirectly with cmake)
deps Dependencies, where some third-party libraries live such as enet(networking library) and glad (opengl loader) can be found
res Resources for the game, such as textures, sounds, etc
scripts .bat and .sh files for convenience, such as build.sh for running cmake and building the code
shaders OpenGL Shaders for the game
src Project source code, detailed explanation below
tests Catch2 Unit Tests

Source Code Structure

All code starts from src/main.cpp

By default, this will start a localhost server, and also will launch the client which will connect to it.

The "Game Loop"

Every game has main loops, this game has two: One for the server and one for the client.

Looking at these is probably a good start in understanding how this project works.

The Client Main Loop can be found in client_engine.cpp.

The Server Main Loop can be found in server_engine.cpp.

Both of these functions also act as the entry point for the client and server respectively.

Client

What the player sees and interacts with, connects to the server

The "entry point" can be found in client_engine.cpp, which is where the main loop for the client code is.

The majority of the client logic can be found in client.cpp, which is called from the main loop via the handleInput, update, and render functions. The game state is initialised using an init function rather than a constructor, because the order in which different things are initialised is very important (especially when it comes to networking and opengl code).

The network interactions are found also in the Client class in *client_commands.cpp. This code sends packets to the server (eg input, player position), and receives packets from the server (eg entity updates, chunk data).

The gl folder has abstractions around OpenGL objects such as vertex buffers, textures etc

The input is self explanitory

The world folder is for stuff relating to the gameplay, for now this is creating a mesh for chunks for use in rendering.

Common

For common functionality between the client and the server.

The network contains NetworkHost, which is a base class for both server and client. This uses the enet library for reliable UDP communications.

The net_command.cpp contains the protocol for networking. ClientCommand is for the server sending commands to client, and ServerCommand is for the client sending commands to the server.

TODO: Create a network protocol article

The world folder has all the world code, such as the chunk logic etc

Server

The game server.

The entry point and main loop for the server is found in server_engine.cpp.

It creates a server, and calls tick and sendPackets on it once every "server tick".

The main server logic is found in server.cpp. It is where all the players connected to the server are stored, as well as all the world data, packet handling etc.

World/ Terrain generation can be found in the world folder.