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
+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();
}
}