Skip to content

Commit

Permalink
Merge pull request #27 from FergusonAJ/cmake_with_sfml
Browse files Browse the repository at this point in the history
Add CMake with SFML support
  • Loading branch information
FergusonAJ authored Oct 3, 2023
2 parents 7fa68ce + 0bf1a32 commit a2b921c
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# vscode cmake build
build

# clion cmake build
cmake-build-debug
cmake-build-release

# clion settings
.idea/

# vscode settings
.vscode/
.github/

# Vim swap files
*.swp
*.swo
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "third_party/Catch2"]
path = third_party/Catch2
url = https://github.com/catchorg/Catch2.git
[submodule "third_party/SFML"]
path = third_party/SFML
url = https://github.com/SFML/SFML.git
45 changes: 45 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
cmake_minimum_required(VERSION 3.12)

project(CSE_491)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wcast-align -Winfinite-recursion -Wnon-virtual-dtor -Wnull-dereference -Woverloaded-virtual -pedantic")

set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
set(CMAKE_CXX_FLAGS_MINSIZEREL "-DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")

# all the possible executables [each team can have their own configuration]
# copy over simple or same and change the accordingly
set(EXECUTABLES simple sfml_example)


# setting the output path to executable
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/executable)


# moving assets to build
file(COPY ./assets DESTINATION ${CMAKE_CURRENT_BINARY_DIR})


# Configure SFML
set(SFML_SRC_DIR third_party/SFML)
set(SFML_BUILD_DIR sfml_build)
set(MAIN_SOURCE_DIR ./)
add_subdirectory(${SFML_SRC_DIR} ${SFML_BUILD_DIR})


foreach(EXECUTABLE ${EXECUTABLES})
set(SOURCES source/${EXECUTABLE}_main.cpp)
add_executable(${EXECUTABLE} ${SOURCES})
target_include_directories(${EXECUTABLE}
PRIVATE ${MAIN_SOURCE_DIR}
)
target_link_libraries(${EXECUTABLE}
PRIVATE sfml-window sfml-audio sfml-graphics
)
endforeach()


add_subdirectory(tests)
102 changes: 102 additions & 0 deletions docs/HowToCompile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# How to Compile

First you'll need to clone the repo _with submodules_.

If you haven't yet cloned the repo, run:
```
git clone https://github.com/MSU-CSE491/cse_491_fall_2023.git --recursive
```

If you _have_ already cloned the repo, but you need to download the submodules, run the following inside the repo's directory:
```
git submodule init
git submodule update
```

Now jump to the section for your operating system.

## Compiling under Linux

If you're on a Linux machine, you first need to download some dependencies for SFML.
Specifically, you need:
- freetype
- x11
- xrandr
- udev
- opengl
- flac
- ogg
- vorbis
- vorbisenc
- vorbisfile
- openal
- pthread

If you're on Ubuntu or other similar Debian distro, you can run the following:
```
sudo apt install libX-dev
```
For library X.

Once you have all dependencies downloaded, navigate to the root of the repo and run the following to build:
```
mkdir build
cd build
cmake ..
cmake --build .
```
All compiled executables should be in the `/build/executables/` directory.

## Compiling under MacOS

Mac shouldn't require any additional dependencies, so simply run the following, starting at the root of the repo:
```
mkdir build
cd build
cmake ..
cmake --build .
```
All compiled executables should be in the `/build/executables/` directory.

## Compiling under Windows with MinGW

After you have the repo and submodules downloaded, we need to ensure you have MinGW and CMake.
In a command line window, try running `g++` and `cmake`, if either give a "command not found" message, then we need to install them.

MinGW gives us g++, and can be downloaded here: https://winlibs.com/

CMake can be downloaded from here: https://cmake.org/download/

***Note:*** You'll likely need to add both MinGW's and CMake's `bin` folder to your path (I think CMake has a `bin` directory, otherwise add whatever directory contains the executable).
To add a directory to your path, follow these instructions: https://www.architectryan.com/2018/03/17/add-to-the-path-on-windows-10/
Note that you may run into issues if the directories you're adding have spaces in their paths (e.g., "C:\Program Files\..." was giving me issues). If you run into this, I'd recommend creating a new directory on your `C:\` drive, like `C:\bin\` and then add CMake and MinGW as subdirectories there.

Once you have CMake and MinGW working in your terminal (note you'll have to restart cmd/VSCode/whatever to get the path changes to take effect), run the following from the root of your repo:
```
mkdir build
cd build
cmake -G "MinGW Makefiles" ..
cmake --build .
```

All compiled executables should be in the `/build/executables/` directory.

Note that if you tried to build using CMake before, it likely tried to use MSVC as a compiler. If so, just wipe the build folder and start fresh.

## How to compile in debug mode

To compile in debug mode, simply add `-DCMAKE_BUILD_TYPE=Debug` to the first cmake command.

This would normally look like:
```
cmake -DCMAKE_BUILD_TYPE=Debug ..
```
Unless you're on Windows, which would be:

```
cmake -DCMAKE_BUILD_TYPE=Debug -G "MinGW Makefiles" ..
```

You can then build like normal.

Note that you can also replace `Debug` with `Release` to compile with optimizations.
3 changes: 3 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Below are a set of guides for how to develop your own modules and joining them together into an executable (potentially with additional analysis and data collection.)

- [How to compile](HowToCompile.md) - Walks through how to compile via CMake.

- [How to Build an Agent](HowToBuildAnAgent.md) - A guide to designing your own type of autonomous agent.

- [How to Build a World](HowToBuildAWorld.md) - A guide to designing you own world with custom interaction modes among agents and other environmental properties.
Expand All @@ -11,3 +13,4 @@ Below are a set of guides for how to develop your own modules and joining them t
- [How to Assemble a `main()` function](HowToAssembleAMain.md) - A guide to selecting worlds, agents, and interfaces and turning them into a custom executable (including how to collect data about the resulting system.)

- [How to build, run, and create unit tests](HowToTest.md) - Walks through how to build and run existing unit tests, as well as create new unit test files.

1 change: 1 addition & 0 deletions source/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ FILES_generated = *.o $(EXECUTABLES)

clean:
rm -rf $(FILES_backup) $(FILES_generated)

58 changes: 58 additions & 0 deletions source/sfml_example_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <iostream>

#include "SFML/Graphics.hpp"

int main(){

// Create the window
sf::RenderWindow window(sf::VideoMode({800, 600}), "SFML works!");
window.setFramerateLimit(60);
// Create our tile and its properties
const int tile_size = 20;
sf::RectangleShape rect(sf::Vector2f(tile_size, tile_size));
rect.setFillColor(sf::Color(200, 50, 0));
sf::Vector2f rect_pos(window.getSize().x / 2, window.getSize().y / 2);
sf::Vector2f rect_vel(200,200);

sf::Clock clock; // Create a clock to help calculate delta time
while(window.isOpen()){
auto delta_time = clock.restart().asSeconds();

// Handle input
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed) break;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::Q)){
break;
}

// Update rectangle, including collision
rect_pos += rect_vel * delta_time;
if(rect_pos.x + rect.getSize().x > window.getSize().x){
rect_pos.x = window.getSize().x - rect.getSize().x;
rect_vel.x *= -1;
}
else if(rect_pos.x < 0){
rect_pos.x = 0;
rect_vel.x *= -1;
}
if(rect_pos.y + rect.getSize().y > window.getSize().y){
rect_pos.y = window.getSize().y - rect.getSize().y;
rect_vel.y *= -1;
}
else if(rect_pos.y < 0){
rect_pos.y = 0;
rect_vel.y *= -1;
}

// Render!
window.clear(sf::Color::Black);
rect.setPosition(rect_pos);
window.draw(rect);
window.display();
}
return 0;
}
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.10)
project(tests VERSION 0.1)
#project(tests VERSION 0.1)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
Expand Down
1 change: 1 addition & 0 deletions third_party/SFML
Submodule SFML added at 9d1d9c

0 comments on commit a2b921c

Please sign in to comment.