-
Notifications
You must be signed in to change notification settings - Fork 0
Project Specification Guide
Understand the standard directory structure and organization of the MyEngine project to help you create a standardized project.
You can refer to the following two project structures for better management:
YourProject
├── CMakeLists.txt
└── main.cpp
Contains only the main.cpp file for writing the main logic of the project. Suitable for simple small projects or rapid prototyping.
YourProject
├── CMakeLists.txt
├── main.cpp
├── assets
│ ├── fonts
│ ├── images
│ ├── audios
│ └── ...
├── src
│ ├── Window.cpp/h
│ ├── Object.cpp/h
│ └── ...
└── README.md
Contains the main.cpp file, as well as the src directory for storing project source files and the assets directory for storing project resource files (such as fonts, images, audios, etc.).
If considering cross-platform development, it is strongly recommended to use CMake as the project management tool. If only considering single-platform development, tools like Makefile or Visual Studio can be considered.
The following will briefly introduce the necessary scripts in CMake to help you quickly understand and create a project.
cmake_minimum_required(VERSION 3.24)
project(MyApp) # Define your project name
# project(MyApp VERSION 1.0.0 LANGUAGES CXX) # Or you can define the project version and programming language in detail
set(CMAKE_CXX_STANDARD 20) # Specify the C++ standard, C++20 or above is required here
set(CMAKE_INCLUDE_CURRENT_DIR ON) # Include the current directory to facilitate the use of relative paths in CMakeLists.txt
# Define project-related compilation definitions, which will be set by default when running the project
add_compile_definitions(APP_NAME="MyApp")
add_compile_definitions(APP_ID="com.MyApp.app")
add_compile_definitions(APP_VERSION="v1.0.0")
# Compilation options, which can be set according to the compiler type
if (MSVC)
add_compile_options(/EHsc /W4 /D_DEBUG /MDd)
else()
add_compile_options(-Wall -g -Wextra -Wpedantic)
endif()set(SDL3_LIB "/path/to/SDL3")
set(SDL3_IMAGE_LIB "/path/to/SDL3_image")
set(SDL3_TTF_LIB "/path/to/SDL3_ttf")
set(SDL3_MIXER_LIB "/path/to/SDL3_mixer")
set(MYENGINE_LIB "/path/to/MyEngine")# Add these paths to the CMake search path so that find_package can find the corresponding libraries
list(APPEND CMAKE_PREFIX_PATH ${SDL3_LIB})
list(APPEND CMAKE_PREFIX_PATH ${SDL3_IMAGE_LIB})
list(APPEND CMAKE_PREFIX_PATH ${SDL3_TTF_LIB})
list(APPEND CMAKE_PREFIX_PATH ${SDL3_MIXER_LIB})
list(APPEND CMAKE_PREFIX_PATH ${MYENGINE_LIB})
# Find and load libraries such as SDL3, SDL3_image, SDL3_ttf, SDL3_mixer, MyEngine, etc.
find_package(SDL3 REQUIRED)
find_package(SDL3_image REQUIRED)
find_package(SDL3_ttf REQUIRED)
find_package(SDL3_mixer REQUIRED)
find_package(MyEngine REQUIRED)# Add an executable file, specify its name as ${PROJECT_NAME}, and include the project source files
add_executable(${PROJECT_NAME}
src/main.cpp
src/core/MyWindow.cpp
src/ui/MyWidget.cpp
.....
)target_link_libraries(${PROJECT_NAME} PRIVATE
SDL3::SDL3
SDL3_image::SDL3_image
SDL3_ttf::SDL3_ttf
SDL3_mixer::SDL3_mixer
MyEngine::MyEngine
)# Set the output directory of the executable file to the bin directory under the build directory
set_target_properties(${PROJECT_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)# Copy project resource files to the output directory of the executable file
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/assets
${CMAKE_BINARY_DIR}/bin/assets
)By default, you can directly include the header file MyEngine.h which contains all the necessary header files for the entire project, without needing to include other header files.
#include <MyEngine/MyEngine>If you only need to include a specific header file, such as the Random.h header file, you can directly include that header file.
#include <MyEngine/Utils/Random.h>Each time you use a class or function in the MyEngine namespace, you need to add the MyEngine:: prefix. For example:
MyEngine::Engine engine;
auto win = new MyEngine::Window(&engine, "Hello world!");
win->show();
engine.exec();You can directly use the MyEngine namespace without adding the MyEngine:: prefix.
using namespace MyEngine;It is recommended to use the FileSystem class to manage resource files in the system.
// Use absolute path
Texture img(FileSystem::getAbsolutePath("./assets/image.png"), renderer);
// Use relative path (relative to the executable file, there may be issues with file not found)
Texture img("assets/image.png", renderer);// Load resources after window creation
auto window = new Window(&engine, "My App");
Texture background(FileSystem::getAbsolutePath("./assets/bg.png"), window->renderer());
Texture sprite(FileSystem::getAbsolutePath("./assets/sprite.png"), window->renderer());- Class names: Use camel case naming, such as
MyWindow,MyWidget, etc. - Function names: Use camel case naming, such as
myFunction,myMethod, etc. - Variable names: Use camel case naming or underscore naming, such as
myVariable,my_variable, etc. - Constant names: Use all uppercase letters, such as
MY_CONSTANT,PI, etc.
For private member variables, it is recommended to add an underscore _ prefix to their names, such as _var, _your_function, to distinguish them from public member variables.
This project contains some example templates that you can copy and supplement your code into your project to quickly start development.
More example templates will be added for use in the future.