From f546f7cddd68328e533ad2107ff6ea12cff8b917 Mon Sep 17 00:00:00 2001 From: David Hackbarth Date: Fri, 6 Mar 2026 17:37:16 +0100 Subject: [PATCH] Integrate wxWidgets application and enhance SFML project - Added `wxwidgets` module with a basic application (menu and UI setup in `main.cpp`) and CMake configuration. - Updated `Paddle` class in SFML to support dynamic key bindings and movement logic via `Update` method. - Introduced `asset_ids.h` for centralized asset file definitions in SFML. - Enhanced `sfml/main.cpp` with animation logic and drawable management using `std::vector`. - Adjusted CMake configurations for SFML and added asset copying to runtime directory. --- cpp/.idea/.name | 2 +- cpp/.idea/misc.xml | 2 +- cpp/CMakeLists.txt | 3 +- cpp/sfml/CMakeLists.txt | 4 ++- cpp/sfml/src/Paddle.cpp | 13 +++++++- cpp/sfml/src/Paddle.h | 6 +++- cpp/sfml/src/asset_ids.h | 11 +++++++ cpp/sfml/src/main.cpp | 43 +++++++++++++++++++------ cpp/wxwidgets/CMakeLists.txt | 13 ++++++++ cpp/wxwidgets/src/main.cpp | 61 ++++++++++++++++++++++++++++++++++++ 10 files changed, 142 insertions(+), 16 deletions(-) create mode 100644 cpp/sfml/src/asset_ids.h create mode 100644 cpp/wxwidgets/CMakeLists.txt create mode 100644 cpp/wxwidgets/src/main.cpp diff --git a/cpp/.idea/.name b/cpp/.idea/.name index 3c2b3e6..32e40d2 100644 --- a/cpp/.idea/.name +++ b/cpp/.idea/.name @@ -1 +1 @@ -CMakeSFMLProject \ No newline at end of file +wxwidgets \ No newline at end of file diff --git a/cpp/.idea/misc.xml b/cpp/.idea/misc.xml index 103fa9f..4dff086 100644 --- a/cpp/.idea/misc.xml +++ b/cpp/.idea/misc.xml @@ -3,7 +3,7 @@ - + \ No newline at end of file diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 383200f..72aadb8 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -6,4 +6,5 @@ add_subdirectory(pointer) add_subdirectory(classes) add_subdirectory(containers) add_subdirectory(misc) -add_subdirectory(sfml) \ No newline at end of file +add_subdirectory(sfml) +add_subdirectory(wxwidgets) \ No newline at end of file diff --git a/cpp/sfml/CMakeLists.txt b/cpp/sfml/CMakeLists.txt index 54b0166..5ca236e 100644 --- a/cpp/sfml/CMakeLists.txt +++ b/cpp/sfml/CMakeLists.txt @@ -19,4 +19,6 @@ add_executable(main target_compile_features(main PRIVATE cxx_std_17) target_link_libraries(main PRIVATE SFML::Graphics) -set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT main) \ No newline at end of file +set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT main) + +file(COPY assets DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) \ No newline at end of file diff --git a/cpp/sfml/src/Paddle.cpp b/cpp/sfml/src/Paddle.cpp index 8a33fc0..94ab6df 100644 --- a/cpp/sfml/src/Paddle.cpp +++ b/cpp/sfml/src/Paddle.cpp @@ -4,7 +4,8 @@ #include "Paddle.h" -Paddle::Paddle() { +Paddle::Paddle(Keyboard::Key upKey, Keyboard::Key downKey) : _upKey(upKey), _downKey(downKey) +{ _shape = new sf::RectangleShape(sf::Vector2f(20.f, 100.f)); _shape->setFillColor(Color::White); } @@ -13,3 +14,13 @@ Paddle::~Paddle() { delete _shape; _shape = nullptr; } + +void Paddle::Update(float dt) { + if (Keyboard::isKeyPressed(_upKey)) { + _shape->setPosition(_shape->getPosition() + Vector2f(0, -100 * dt)); + } + + if (Keyboard::isKeyPressed(_downKey)) { + _shape->setPosition(_shape->getPosition() + Vector2f(0, 100 * dt)); + } +} diff --git a/cpp/sfml/src/Paddle.h b/cpp/sfml/src/Paddle.h index acaa73c..b725654 100644 --- a/cpp/sfml/src/Paddle.h +++ b/cpp/sfml/src/Paddle.h @@ -10,13 +10,17 @@ using namespace sf; class Paddle { public: - Paddle(); + Paddle(Keyboard::Key upKey, Keyboard::Key downKey); ~Paddle(); + void Update(float dt); + Shape& getShape() const { return *_shape; } private: Shape* _shape; + Keyboard::Key _upKey; + Keyboard::Key _downKey; }; diff --git a/cpp/sfml/src/asset_ids.h b/cpp/sfml/src/asset_ids.h new file mode 100644 index 0000000..6a8c4c5 --- /dev/null +++ b/cpp/sfml/src/asset_ids.h @@ -0,0 +1,11 @@ +// +// Created by David Hackbarth on 06.03.26. +// + +#ifndef CMAKESFMLPROJECT_ASSET_IDS_H +#define CMAKESFMLPROJECT_ASSET_IDS_H + +#define IDLE "assets/Characters/Double/character_pink_idle.png" +#define WALK_A "assets/Characters/Double/character_pink_walk_a.png" + +#endif //CMAKESFMLPROJECT_ASSET_IDS_H \ No newline at end of file diff --git a/cpp/sfml/src/main.cpp b/cpp/sfml/src/main.cpp index db6290c..9505c09 100644 --- a/cpp/sfml/src/main.cpp +++ b/cpp/sfml/src/main.cpp @@ -1,22 +1,37 @@ #include +#include #include "Paddle.h" +#include "asset_ids.h" int main() { sf::RenderWindow window( sf::VideoMode( { 640, 480 } ), "SFML works!" ); + std::vector drawables; + sf::CircleShape shape( 10.f ); shape.setFillColor( sf::Color::White ); + drawables.push_back(&shape); - Paddle player1; - Paddle player2; + Paddle player1(Keyboard::Key::W, Keyboard::Key::S); + Paddle player2(Keyboard::Key::Up, Keyboard::Key::Down); + drawables.push_back(&player1.getShape()); + drawables.push_back(&player2.getShape()); // texture/sprite - Texture texture("/Users/hacky/Projects/HWR/hwr2024_a/cpp/sfml/assets/Characters/Double/character_pink_idle.png"); - Sprite character(texture); + Texture idleTexture(IDLE); + Texture walkATexture(WALK_A); + Texture walkBTexture("assets/Characters/Double/character_pink_walk_b.png"); + Sprite character(idleTexture); + drawables.push_back(&character); + bool isA = true; float speed = 100.0f; + + // time Clock clock; + Clock animationClock; + while ( window.isOpen() ) { while ( const std::optional event = window.pollEvent() ) @@ -27,19 +42,27 @@ int main() float dt = clock.restart().asSeconds(); - if (Keyboard::isKeyPressed(Keyboard::Key::W)) { - player1.getShape().setPosition(player1.getShape().getPosition() + Vector2f(0, -speed * dt)); - } + // update + player1.Update(dt); + player2.Update(dt); - if (Keyboard::isKeyPressed(Keyboard::Key::S)) { - player1.getShape().setPosition(player1.getShape().getPosition() + Vector2f(0, speed * dt)); + // animation + if (animationClock.getElapsedTime().asMilliseconds() >= 300) { + isA = !isA; + animationClock.restart(); + character.setTexture(isA ? walkATexture : walkBTexture); } window.clear(); // window.draw( shape ); // window.draw(player1.getShape()); // window.draw(player2.getShape()); - window.draw(character); + // window.draw(character); + + for (sf::Drawable* drawable : drawables) { + window.draw(*drawable); + } + window.display(); } } \ No newline at end of file diff --git a/cpp/wxwidgets/CMakeLists.txt b/cpp/wxwidgets/CMakeLists.txt new file mode 100644 index 0000000..f625b78 --- /dev/null +++ b/cpp/wxwidgets/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 4.1) +project(wxwidgets LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 20) + +find_package(wxWidgets REQUIRED COMPONENTS net core base) +if (wxWidgets_USE_FILE) + include(${wxWidgets_USE_FILE}) +endif () + +add_executable(main + src/main.cpp) +target_link_libraries(main ${wxWidgets_LIBRARIES}) diff --git a/cpp/wxwidgets/src/main.cpp b/cpp/wxwidgets/src/main.cpp new file mode 100644 index 0000000..a145e08 --- /dev/null +++ b/cpp/wxwidgets/src/main.cpp @@ -0,0 +1,61 @@ +#include + +class App : public wxApp { +public: + bool OnInit() override; +}; + +wxIMPLEMENT_APP(App); + +class MainWindow : public wxFrame { +public: + MainWindow(); + +private: + void OnShowMenuItemClick(wxCommandEvent& event); +}; + +bool App::OnInit() { + MainWindow* mainWindow = new MainWindow(); + mainWindow->Show(); + return true; +} + +enum AppIds { + ID_LOWEST = 0, + + ID_SHOW +}; + +MainWindow::MainWindow() : wxFrame(nullptr, wxID_ANY, "Hello, world!") +{ + wxMenu* fileMenu = new wxMenu(); + fileMenu->Append(ID_SHOW, "Show"); + fileMenu->Append(wxID_ABOUT); + fileMenu->Append(wxID_EXIT); + + wxMenuBar* menuBar = new wxMenuBar(); + menuBar->Append(fileMenu, "&File"); + + SetMenuBar(menuBar); + + Bind(wxEVT_MENU, &MainWindow::OnShowMenuItemClick, this, ID_SHOW); + + wxBoxSizer* mainVertical = new wxBoxSizer(wxVERTICAL); + wxStaticText* label = new wxStaticText(this, wxID_ANY, "Hello, world!"); + mainVertical->Add(label); + mainVertical->Add(new wxStaticText(this, wxID_ANY, "Internal Instance")); + + wxBoxSizer* horizontal = new wxBoxSizer(wxHORIZONTAL); + horizontal->Add(new wxButton(this, ID_SHOW, "Show")); + horizontal->Add(new wxButton(this, ID_SHOW, "Show")); + horizontal->Add(new wxButton(this, ID_SHOW, "Show")); + horizontal->Add(new wxButton(this, ID_SHOW, "Show")); + mainVertical->Add(horizontal); + + SetSizer(mainVertical); +} + +void MainWindow::OnShowMenuItemClick(wxCommandEvent& event) { + wxMessageBox("Hello, world!"); +} \ No newline at end of file