From 7c1479906da6c283695136b2a7a8dfdb2db40c19 Mon Sep 17 00:00:00 2001 From: Austin Ferguson Date: Sun, 1 Oct 2023 21:26:29 -0400 Subject: [PATCH] Update CMake structure, swap sample for sfml --- CMakeLists.txt | 25 ++++++++++-------- source/sample_main.cpp | 18 ------------- source/sfml_main.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 28 deletions(-) delete mode 100644 source/sample_main.cpp create mode 100644 source/sfml_main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b6ea4a6..2200df9a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ 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 sample) +set(EXECUTABLES simple sfml) # setting the output path to executable @@ -23,18 +23,23 @@ set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/executable) 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}) - if(${EXECUTABLE} STREQUAL "simple") - set(SOURCES source/${EXECUTABLE}_main.cpp) - elseif(${EXECUTABLE} STREQUAL "sample") - set(SOURCES source/${EXECUTABLE}_main.cpp) - # add the executable here to complile and run - endif() +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) \ No newline at end of file +add_subdirectory(tests) diff --git a/source/sample_main.cpp b/source/sample_main.cpp deleted file mode 100644 index 7cc02999..00000000 --- a/source/sample_main.cpp +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file - * @author Aman Dhruva Thamminana - * @version 0.1 - * @date 2023-09-27 - * - * @copyright Copyright (c) 2023 - * - */ - - -#include - -int main() -{ - std::cout << "Howdy World!" << std::endl; - return 0; -} \ No newline at end of file diff --git a/source/sfml_main.cpp b/source/sfml_main.cpp new file mode 100644 index 00000000..fe1cd1d6 --- /dev/null +++ b/source/sfml_main.cpp @@ -0,0 +1,58 @@ +#include + +#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; +}