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.
This commit is contained in:
2026-03-06 17:37:16 +01:00
parent b58064a0d0
commit f546f7cddd
10 changed files with 142 additions and 16 deletions
+1 -1
View File
@@ -1 +1 @@
CMakeSFMLProject
wxwidgets
+1 -1
View File
@@ -3,7 +3,7 @@
<component name="CMakePythonSetting">
<option name="pythonIntegrationState" value="YES" />
</component>
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$/sfml">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$/wxwidgets">
<contentRoot DIR="$PROJECT_DIR$" />
</component>
</project>
+2 -1
View File
@@ -6,4 +6,5 @@ add_subdirectory(pointer)
add_subdirectory(classes)
add_subdirectory(containers)
add_subdirectory(misc)
add_subdirectory(sfml)
add_subdirectory(sfml)
add_subdirectory(wxwidgets)
+3 -1
View File
@@ -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)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT main)
file(COPY assets DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
+12 -1
View File
@@ -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));
}
}
+5 -1
View File
@@ -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;
};
+11
View File
@@ -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
+33 -10
View File
@@ -1,22 +1,37 @@
#include <SFML/Graphics.hpp>
#include <vector>
#include "Paddle.h"
#include "asset_ids.h"
int main()
{
sf::RenderWindow window( sf::VideoMode( { 640, 480 } ), "SFML works!" );
std::vector<sf::Drawable*> 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();
}
}
+13
View File
@@ -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})
+61
View File
@@ -0,0 +1,61 @@
#include <wx/wx.h>
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!");
}