feat: Add global Linux build support and configurations

This commit is contained in:
Huseyn Ismayilov 2024-11-27 20:52:57 +04:00
parent 7f465331c4
commit 2e2f4bf3d8
10 changed files with 298 additions and 19 deletions

View File

@ -21,3 +21,20 @@ add_subdirectory(${THIRDPARTY_DIR}/stb)
# Add engine and editor
add_subdirectory(engine)
add_subdirectory(editor)
# Create .desktop file for linux
if(UNIX)
set(DESKTOP_FILE "${EDITOR_RESOURCES_DIR}/ferx.desktop")
set(ICON_FILE "${ENGINE_RESOURCES_DIR}/icons/icon.png")
include(GNUInstallDirs)
install(FILES ${DESKTOP_FILE}
DESTINATION ${CMAKE_INSTALL_DATADIR}/applications
)
install(FILES ${ICON_FILE}
DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/256x256/apps
RENAME ferx.png
)
endif(UNIX)

View File

@ -5,7 +5,8 @@ project(editor)
set(NAME ferx)
set(EDITOR_SOURCE_DIR src)
set(EDITOR_INCLUDE_DIR include)
set(EDITOR_RESOURCES_DIR resources)
set(EDITOR_RESOURCES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/resources)
set(EDITOR_RESOURCES_DIR ${EDITOR_RESOURCES_DIR} PARENT_SCOPE)
file(GLOB_RECURSE EDITOR_SOURCES ${EDITOR_SOURCE_DIR}/*.cpp)
@ -16,7 +17,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "Release")
file(COPY ${EDITOR_RESOURCES_DIR} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
target_compile_definitions(${NAME} PUBLIC EDITOR_RESOURCES_PATH="${CMAKE_CURRENT_BINARY_DIR}/resources/")
else()
target_compile_definitions(${NAME} PUBLIC EDITOR_RESOURCES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/resources/")
target_compile_definitions(${NAME} PUBLIC EDITOR_RESOURCES_PATH="${EDITOR_RESOURCES_DIR}/")
endif()
target_sources(${NAME} PRIVATE ${EDITOR_SOURCES})

11
editor/resources/ferx.desktop Executable file
View File

@ -0,0 +1,11 @@
[Desktop Entry]
Name=Ferx Engine
Exec=ferx
Version=1.0
Type=Application
Categories=Development;
Terminal=false
Icon=ferx
Comment=Basic C++ game engine for learning
StartupWMClass=ferx
StartupNotify=true

View File

@ -21,7 +21,8 @@ file(GLOB COMPONENTS_SOURCES "${COMPONENTS_INCLUDES}/*.cpp")
set(UI_INCLUDES ui)
file(GLOB UI_SOURCES "${UI_INCLUDES}/*.cpp")
set(ENGINE_RESOURCES_DIR resources)
set(ENGINE_RESOURCES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/resources)
set(ENGINE_RESOURCES_DIR ${ENGINE_RESOURCES_DIR} PARENT_SCOPE)
# Build engine as library
add_library(${PROJECT_NAME})
@ -31,7 +32,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "Release")
file(COPY ${ENGINE_RESOURCES_DIR} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
target_compile_definitions(${PROJECT_NAME} PUBLIC ENGINE_RESOURCES_PATH="${CMAKE_CURRENT_BINARY_DIR}/resources/")
else()
target_compile_definitions(${PROJECT_NAME} PUBLIC ENGINE_RESOURCES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/resources/")
target_compile_definitions(${PROJECT_NAME} PUBLIC ENGINE_RESOURCES_PATH="${ENGINE_RESOURCES_DIR}/")
endif()
# Link sources, include directories, and third party libraries

View File

@ -28,6 +28,12 @@ void Window::Init()
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#if defined(_WIN32)
// Add Windows-specific configuration if needed
#elif defined(__linux__)
glfwWindowHintString(GLFW_X11_CLASS_NAME, "ferx");
glfwWindowHintString(GLFW_X11_INSTANCE_NAME, "Ferx");
#endif
m_Window = glfwCreateWindow(m_Data.Size.Width, m_Data.Size.Height, m_Data.Title.c_str(), nullptr, nullptr);
if (!m_Window) {
@ -36,6 +42,8 @@ void Window::Init()
return;
}
SetWindowIcon();
glfwMakeContextCurrent(m_Window);
}
@ -65,6 +73,27 @@ WindowSize Window::GetSize()
return m_Data.Size;
}
void Window::SetWindowIcon() const
{
int width, height, channels;
const char* iconPath = ENGINE_RESOURCES_PATH"icons/icon.png";
unsigned char* pixels = stbi_load(iconPath, &width, &height, &channels, 4); // Force RGBA
if (!pixels) {
std::cerr << "Failed to load icon: " << iconPath << std::endl;
return;
}
GLFWimage images[1];
images[0].width = width;
images[0].height = height;
images[0].pixels = pixels;
glfwSetWindowIcon(m_Window, 1, images);
stbi_image_free(pixels);
}
void Window::Shutdown() const
{
glfwDestroyWindow(m_Window);

View File

@ -39,9 +39,10 @@ public:
const std::string& GetTitle() const;
WindowSize GetSize();
void SetWindowIcon() const;
void Shutdown() const;
private:
GLFWwindow* m_Window{};
WindowData m_Data;
};
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

@ -1,24 +1,25 @@
#!/bin/bash
# Set variables
RELEASE_BUILD_DIR="build-release"
DEBUG_BUILD_DIR="build-debug"
EDITOR_DIR="editor"
NAME="ferx"
EXECUTABLE_PATH="editor/ferx"
# Run commands
case $1 in
build-release)
cmake -S .. -B ../"$RELEASE_BUILD_DIR" -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake -S .. -B ../"$RELEASE_BUILD_DIR" -G Ninja -DCMAKE_BUILD_TYPE=Release -DGLFW_BUILD_WAYLAND=OFF -DGLFW_BUILD_X11=ON
cmake --build ../"$RELEASE_BUILD_DIR" -j8
;;
build-debug)
cmake -S .. -B ../"$DEBUG_BUILD_DIR" -G Ninja -DCMAKE_BUILD_TYPE=Debug
cmake -S .. -B ../"$DEBUG_BUILD_DIR" -G Ninja -DCMAKE_BUILD_TYPE=Debug -DGLFW_BUILD_WAYLAND=OFF -DGLFW_BUILD_X11=ON
cmake --build ../"$DEBUG_BUILD_DIR" -j8
;;
run-release)
cd "../$RELEASE_BUILD_DIR/$EDITOR_DIR" && ./"$NAME"
"../$RELEASE_BUILD_DIR/$EXECUTABLE_PATH"
;;
run-debug)
cd "../$DEBUG_BUILD_DIR/$EDITOR_DIR" && ./"$NAME"
"../$DEBUG_BUILD_DIR/$EXECUTABLE_PATH"
;;
clean-release)
cmake --build ../"$RELEASE_BUILD_DIR" --target clean
@ -36,8 +37,23 @@ case $1 in
bash $0 build-debug
bash $0 run-debug
;;
--help)
echo "Usage: $0 <command>"
echo
echo "Commands:"
echo " all Build and run the project in Debug mode"
echo " build-release Build the project in Release mode"
echo " build-debug Build the project in Debug mode"
echo " run-release Run the Release build"
echo " run-debug Run the Debug build"
echo " clean-release Clean Release build files"
echo " clean-debug Clean Debug build files"
echo " clean-release-all Remove all Release build files"
echo " clean-debug-all Remove all Debug build files"
;;
*)
echo "Usage: $0 {all|build-release|build-debug|run-release|run-debug|clean-release|clean-debug|clean-release-all|clean-debug-all}"
echo "Unknown command: $1"
echo "Use --help for a list of available commands"
exit 1
;;
esac

135
scripts/InstallLinux.sh Executable file
View File

@ -0,0 +1,135 @@
#!/bin/bash
# Set variables
LINUX_BUILD_DIR="/usr/local/lib/ferx"
EXECUTABLE_PATH="$LINUX_BUILD_DIR/editor/ferx"
# Detect the Linux distribution
detect_distro() {
if [ -f /etc/os-release ]; then
. /etc/os-release
if [[ "$ID_LIKE" == *"ubuntu"* ]] || [[ "$ID_LIKE" == *"debian"* ]]; then
echo "ubuntu"
elif [[ "$ID_LIKE" == *"rhel"* ]] || [[ "$ID_LIKE" == *"centos"* ]] || [[ "$ID_LIKE" == *"fedora"* ]]; then
echo "fedora"
elif [[ "$ID_LIKE" == *"arch"* ]]; then
echo "arch"
else
echo "$ID"
fi
else
echo "unknown"
fi
}
install_ubuntu() {
echo "Installing dependencies for Ubuntu/Debian..."
sudo apt update && sudo apt install -y libxkbcommon-dev libxinerama-dev libwayland-dev libxrandr-dev libxcursor-dev libxi-dev mesa-common-dev
}
install_fedora() {
echo "Installing dependencies for Fedora/RHEL/CentOS..."
sudo dnf install -y libxkbcommon-devel libXinerama-devel wayland-devel libXrandr-devel libXcursor-devel libXi-devel mesa-libGL-devel
}
install_arch() {
echo "Installing dependencies for Arch Linux..."
sudo pacman -Syu libxkbcommon libxinerama wayland libxrandr libxcursor libxi mesa
}
# Install Linux Dependencies
install_dependencies() {
distro=$(detect_distro)
case "$distro" in
ubuntu)
install_ubuntu
;;
fedora)
install_fedora
;;
arch)
install_arch
;;
*)
echo "Unsupported distribution: $distro"
exit 1
;;
esac
}
# Build project in global directory
build(){
echo "Building project..."
sudo cmake -S .. -B "$LINUX_BUILD_DIR" -G Ninja -DCMAKE_BUILD_TYPE=Release -DGLFW_BUILD_WAYLAND=OFF -DGLFW_BUILD_X11=ON
sudo cmake --build "$LINUX_BUILD_DIR" -j8
echo "Creating symbolic link for executable..."
sudo ln -sf "$EXECUTABLE_PATH" /usr/local/bin/ferx
}
# Install .desktop file
install_desktop_file() {
if [ ! -d "$LINUX_BUILD_DIR" ]; then
echo "Build directory not found! Please build the project first."
exit 1
fi
echo "Installing configurations..."
sudo cmake --install "$LINUX_BUILD_DIR" --prefix /usr/
}
case $1 in
install-dependencies)
install_dependencies
;;
install-ubuntu)
install_ubuntu
;;
install-fedora)
install_fedora
;;
install-arch)
install_arch
;;
--help)
echo "Usage: $0 <command>"
echo
echo "This script helps with the installation and setup of the Ferx project."
echo "It supports installing dependencies, building the project, and creating symlinks for easy access."
echo
echo "Commands:"
echo " install-dependencies - Automatically installs dependencies based on the detected Linux distribution"
echo " install-ubuntu - Installs dependencies using apt for Ubuntu/Debian"
echo " install-fedora - Installs dependencies using dnf for Fedora/RHEL"
echo " install-arch - Installs dependencies using pacman for Arch Linux"
echo " build - Builds the project and installs it into /usr/local/lib/ferx"
echo " install-desktop-file - Installs the .desktop file and creates a symbolic link to the executable"
echo
echo "If no command is specified, the script will automatically follow all steps:"
echo " 1. Install dependencies based on your Linux distribution"
echo " 2. Build the project and install it into /usr/local/lib/ferx"
echo " 3. Install the .desktop file and create a symbolic link for easy access"
echo
echo "Examples:"
echo " $0 install-dependencies - Automatically installs dependencies based on your distribution."
echo " $0 build - Builds the project in /usr/local/lib/ferx."
echo " $0 install-desktop-file - Installs the .desktop file and creates a symlink."
echo " $0 - Executes all steps: installs dependencies, builds the project, and installs the desktop file."
echo
echo "To run the full process without specifying a command, just run the script without arguments."
echo "It will automatically handle installing dependencies, building the project, and installing the desktop file."
echo
;;
*)
install_dependencies
build
install_desktop_file
echo "Installation complete!"
;;
esac

View File

@ -1,25 +1,93 @@
DEBUG_BUILD_DIR = build-debug
RELEASE_BUILD_DIR = build-release
EDITOR_DIR = editor
NAME = ferx
EXECUTABLE_PATH = editor/ferx
LINUX_BUILD_DIR = /usr/local/lib/ferx
LINUX_INSTALL_FILE_PATH = InstallLinux.sh
RM += -r
all: build-debug run-debug
all: debug
# Help
help:
@echo "Usage: make <target>"
@echo
@echo "Targets:"
@echo " help Show this help message"
@echo " install-dependencies Install project dependencies based on the detected Linux distribution"
@echo " install-ubuntu Install dependencies for Ubuntu/Debian-based systems"
@echo " install-fedora Install dependencies for Fedora/RHEL-based systems"
@echo " install-arch Install dependencies for Arch Linux"
@echo " linux Install dependencies, build, and set up the project for Linux"
@echo " debug Build and run the project in Debug mode"
@echo " release Build and run the project in Release mode"
@echo " build-debug Build the project in Debug mode"
@echo " build-release Build the project in Release mode"
@echo " run-debug Run the Debug build"
@echo " run-release Run the Release build"
@echo " clean-debug Clean Debug build files"
@echo " clean-release Clean Release build files"
@echo " clean-debug-all Remove all Debug build files"
@echo " clean-release-all Remove all Release build files"
@echo
@echo "If no target is specified, the script will default to building the Debug version."
@echo "You can also specify a target to run the corresponding command. For example:"
@echo " make help - Displays this help message."
@echo " make linux - Installs dependencies, builds, and sets up the project for Linux."
@echo " make debug - Builds the project in Debug mode and runs it."
@echo " make release - Builds the project in Release mode and runs it."
@echo " make clean-debug-all - Removes all Debug build files."
@echo " make clean-release-all - Removes all Release build files."
# Install for Linux
linux: install-dependencies build-linux install-desktop-file run-linux
install-dependencies:
@echo "Detecting platform..."
@sh ./${LINUX_INSTALL_FILE_PATH} install-dependencies
install-ubuntu:
@echo "Installing dependencies for Ubuntu..."
@sh ./${LINUX_INSTALL_FILE_PATH} install-ubuntu
install-fedora:
@echo "Installing dependencies for Fedora..."
@sh ./${LINUX_INSTALL_FILE_PATH} install-fedora
install-arch:
@echo "Installing dependencies for Arch Linux..."
@sh ./${LINUX_INSTALL_FILE_PATH} install-arch
build-linux:
sudo cmake -S .. -B $(LINUX_BUILD_DIR) -G Ninja -DCMAKE_BUILD_TYPE=Release -DGLFW_BUILD_WAYLAND=OFF -DGLFW_BUILD_X11=ON
sudo cmake --build $(LINUX_BUILD_DIR) -j8
sudo ln -sf ${LINUX_BUILD_DIR}/${EXECUTABLE_PATH} /usr/local/bin/ferx
run-linux:
ferx
install-desktop-file:
sudo cmake --install ${LINUX_BUILD_DIR} --prefix /usr/
# Build project
debug: build-debug run-debug
release: build-release run-release
build-release:
cmake -S .. -B ../$(RELEASE_BUILD_DIR) -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake -S .. -B ../$(RELEASE_BUILD_DIR) -G Ninja -DCMAKE_BUILD_TYPE=Release -DGLFW_BUILD_WAYLAND=OFF -DGLFW_BUILD_X11=ON
cmake --build ../$(RELEASE_BUILD_DIR) -j8
build-debug:
cmake -S .. -B ../$(DEBUG_BUILD_DIR) -G Ninja -DCMAKE_BUILD_TYPE=Debug
cmake -S .. -B ../$(DEBUG_BUILD_DIR) -G Ninja -DCMAKE_BUILD_TYPE=Debug -DGLFW_BUILD_WAYLAND=OFF -DGLFW_BUILD_X11=ON
cmake --build ../$(DEBUG_BUILD_DIR) -j8
run-release:
cd ../$(RELEASE_BUILD_DIR)/${EDITOR_DIR} && ./${NAME}
../$(RELEASE_BUILD_DIR)/${EXECUTABLE_PATH}
run-debug:
cd ../$(DEBUG_BUILD_DIR)/${EDITOR_DIR} && ./${NAME}
../$(DEBUG_BUILD_DIR)/${EXECUTABLE_PATH}
clean-release:
cmake --build ../$(RELEASE_BUILD_DIR) --target clean