Skip to content

Commit

Permalink
Update CMake structure, swap sample for sfml
Browse files Browse the repository at this point in the history
  • Loading branch information
FergusonAJ committed Oct 2, 2023
1 parent 1cb060d commit 7c14799
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 28 deletions.
25 changes: 15 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
add_subdirectory(tests)
18 changes: 0 additions & 18 deletions source/sample_main.cpp

This file was deleted.

58 changes: 58 additions & 0 deletions source/sfml_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;
}

0 comments on commit 7c14799

Please sign in to comment.