From f9eb3bcb0280d8d551c14555c563bf497d885d4c Mon Sep 17 00:00:00 2001 From: Arthur Moreno Date: Thu, 14 Nov 2024 01:01:50 +0100 Subject: [PATCH] port the game to linux --- CMakeLists.txt | 18 +++++++++++------- Makefile | 35 ++++++++++++++++++++++------------- src/Player.hpp | 10 +++++++--- src/Projectile.hpp | 10 +++++++--- src/SpriteSet.hpp | 10 +++++++--- src/main.cpp | 14 +++++++++----- 6 files changed, 63 insertions(+), 34 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 48f4e27..ca17927 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,15 +15,13 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) # Compiler Flags set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -g") +# Find SDL +find_package(SDL REQUIRED) + # Include Directories include_directories( ${PROJECT_SOURCE_DIR}/src - C:/msys64/ucrt64/include/SDL -) - -# Library Directories -link_directories( - C:/msys64/ucrt64/lib + ${SDL_INCLUDE_DIR} ) # Source Files (using .cpp files) @@ -33,4 +31,10 @@ file(GLOB SRC_FILES "${PROJECT_SOURCE_DIR}/src/*.cpp") add_executable(campo_de_batalha ${SRC_FILES}) # Link Libraries -target_link_libraries(campo_de_batalha mingw32 SDLmain SDL ws2_32) \ No newline at end of file +if(WIN32) + # Windows-specific libraries + target_link_libraries(campo_de_batalha ${SDL_LIBRARY} ${SDLMAIN_LIBRARY} mingw32 ws2_32) +else() + # Linux or other systems + target_link_libraries(campo_de_batalha ${SDL_LIBRARY} ${SDLMAIN_LIBRARY}) +endif() \ No newline at end of file diff --git a/Makefile b/Makefile index 7164875..362bf16 100644 --- a/Makefile +++ b/Makefile @@ -3,13 +3,16 @@ # ============================== # Project name -PROJECT := campo_de_batalha.exe +PROJECT := campo_de_batalha # Build directory BUILD_DIR := build -# CMake generator -CMAKE_GENERATOR := "MinGW Makefiles" +# CMake generator for Windows +CMAKE_GENERATOR_WIN := "MinGW Makefiles" + +# CMake generator for Linux (usually not needed) +CMAKE_GENERATOR_LINUX := # CMake executable CMAKE := cmake @@ -21,27 +24,33 @@ MAKE_CMD := make # RULES # ============================== -.PHONY: all build clean +.PHONY: all build build-linux clean format # Default target all: build -# Build target -build: - @echo "Starting build process..." - @mkdir $(BUILD_DIR) 2> NUL || echo "Build directory already exists." - @cd $(BUILD_DIR) && $(CMAKE) -G $(CMAKE_GENERATOR) .. && $(MAKE_CMD) --always-make +# Build target for Windows +build-win: + @echo "Starting build process on Windows..." + @if not exist $(BUILD_DIR) mkdir $(BUILD_DIR) + @cd $(BUILD_DIR) && $(CMAKE) -G $(CMAKE_GENERATOR_WIN) .. && $(MAKE_CMD) --always-make + +# Build target for Linux +build-linux: + @echo "Starting build process on Linux..." + @mkdir -p $(BUILD_DIR) + @cd $(BUILD_DIR) && $(CMAKE) $(CMAKE_GENERATOR_LINUX) .. && $(MAKE_CMD) # Clean target clean: @echo "Cleaning project..." - @rmdir /s /q $(BUILD_DIR) 2> NUL || echo "Build directory does not exist." - @if exist $(PROJECT) del /f /q $(PROJECT) + @$(RM) -r $(BUILD_DIR) + @$(RM) $(PROJECT) $(PROJECT).exe # Format target for Windows format: @echo "Formatting source files..." - @for /r %%f in (src\*.cpp src\*.hpp) do clang-format -i "%%f" + @for file in $(wildcard src/*.[ch]pp); do clang-format -i "$$file"; done # Phony Targets -.PHONY: all build clean format \ No newline at end of file +.PHONY: all build build-linux clean format \ No newline at end of file diff --git a/src/Player.hpp b/src/Player.hpp index 2e4ed32..900c901 100644 --- a/src/Player.hpp +++ b/src/Player.hpp @@ -2,10 +2,14 @@ #ifndef PLAYER_H #define PLAYER_H -#ifdef __APPLE__ -#include +#if defined(__APPLE__) + #include +#elif defined(_WIN32) || defined(_WIN64) + #include +#elif defined(__linux__) + #include #else -#include + #include #endif #include "Constants.hpp" diff --git a/src/Projectile.hpp b/src/Projectile.hpp index a92f819..ac31f6d 100644 --- a/src/Projectile.hpp +++ b/src/Projectile.hpp @@ -2,10 +2,14 @@ #ifndef PROJECTILE_H #define PROJECTILE_H -#ifdef __APPLE__ -#include +#if defined(__APPLE__) + #include +#elif defined(_WIN32) || defined(_WIN64) + #include +#elif defined(__linux__) + #include #else -#include + #include #endif #include "Constants.hpp" diff --git a/src/SpriteSet.hpp b/src/SpriteSet.hpp index c883936..4f664db 100644 --- a/src/SpriteSet.hpp +++ b/src/SpriteSet.hpp @@ -5,10 +5,14 @@ #include #include -#ifdef __APPLE__ -#include +#if defined(__APPLE__) + #include +#elif defined(_WIN32) || defined(_WIN64) + #include +#elif defined(__linux__) + #include #else -#include + #include #endif class SpriteSet { diff --git a/src/main.cpp b/src/main.cpp index ea2c423..e4d9fb0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,16 +1,20 @@ #include -#include -#include #include #include #define SDL_MAIN_HANDLED // Add this line -#ifdef __APPLE__ -#include +#if defined(__APPLE__) + #include +#elif defined(_WIN32) || defined(_WIN64) + #include + #include + #include +#elif defined(__linux__) + #include #else -#include + #include #endif #include "Constants.hpp"