Add misc and sfml modules with initial implementations, including new classes, assets, and CMake integration.
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by David Hackbarth on 27.02.26.
|
||||
//
|
||||
|
||||
#include "Ball.h"
|
||||
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// Created by David Hackbarth on 27.02.26.
|
||||
//
|
||||
|
||||
#ifndef CMAKESFMLPROJECT_BALL_H
|
||||
#define CMAKESFMLPROJECT_BALL_H
|
||||
|
||||
|
||||
class Ball {
|
||||
};
|
||||
|
||||
|
||||
#endif //CMAKESFMLPROJECT_BALL_H
|
||||
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by David Hackbarth on 27.02.26.
|
||||
//
|
||||
|
||||
#include "Paddle.h"
|
||||
|
||||
Paddle::Paddle(sf::Keyboard::Key upKey, sf::Keyboard::Key downKey) : _shape(sf::Vector2f(20, 100)), _upKey(upKey),
|
||||
_downKey(downKey) {
|
||||
_shape.setFillColor(sf::Color::White);
|
||||
}
|
||||
|
||||
void Paddle::Update(float dt) {
|
||||
if (sf::Keyboard::isKeyPressed(_upKey)) {
|
||||
_shape.move(sf::Vector2f(0, -100 * dt));
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(_downKey)) {
|
||||
_shape.move(sf::Vector2f(0, 100 * dt));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by David Hackbarth on 27.02.26.
|
||||
//
|
||||
|
||||
#ifndef CMAKESFMLPROJECT_PADDLE_H
|
||||
#define CMAKESFMLPROJECT_PADDLE_H
|
||||
#include "SFML/Graphics/RectangleShape.hpp"
|
||||
#include "SFML/Window/Keyboard.hpp"
|
||||
|
||||
class Paddle {
|
||||
public:
|
||||
Paddle(sf::Keyboard::Key upKey, sf::Keyboard::Key downKey);
|
||||
|
||||
sf::RectangleShape& getShape() { return _shape; }
|
||||
|
||||
void Update(float dt);
|
||||
|
||||
private:
|
||||
sf::RectangleShape _shape;
|
||||
sf::Keyboard::Key _upKey;
|
||||
sf::Keyboard::Key _downKey;
|
||||
};
|
||||
|
||||
|
||||
#endif //CMAKESFMLPROJECT_PADDLE_H
|
||||
@@ -0,0 +1,70 @@
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <vector>
|
||||
#include "elements/Paddle.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
sf::RenderWindow window( sf::VideoMode( { 640, 480 } ), "SFML works!" );
|
||||
|
||||
std::vector<sf::Drawable*> drawables;
|
||||
|
||||
sf::CircleShape circle( 20.f );
|
||||
circle.setPosition( { 300.f, 220.f } );
|
||||
circle.setFillColor( sf::Color::White );
|
||||
drawables.push_back( &circle );
|
||||
|
||||
// paddles
|
||||
Paddle player1(sf::Keyboard::Key::W, sf::Keyboard::Key::S);
|
||||
player1.getShape().setPosition( { 10.f, 190.f } );
|
||||
drawables.push_back( &player1.getShape() );
|
||||
|
||||
Paddle player2(sf::Keyboard::Key::Up, sf::Keyboard::Key::Down);
|
||||
player2.getShape().setPosition( { 610.f, 190.f } );
|
||||
drawables.push_back( &player2.getShape() );
|
||||
|
||||
// sprite
|
||||
sf::Texture idleTexture("/Users/hacky/Projects/HWR/hwr2024_b/cpp/sfml/assets/Characters/Default/character_green_idle.png");
|
||||
sf::Texture walk1Texture("/Users/hacky/Projects/HWR/hwr2024_b/cpp/sfml/assets/Characters/Default/character_green_walk_a.png");
|
||||
sf::Texture walk2Texture("/Users/hacky/Projects/HWR/hwr2024_b/cpp/sfml/assets/Characters/Default/character_green_walk_b.png");
|
||||
sf::Sprite character(idleTexture);
|
||||
character.setPosition( { 300.f, 220.f } );
|
||||
drawables.push_back( &character );
|
||||
bool isA = true;
|
||||
|
||||
// time
|
||||
sf::Clock clock;
|
||||
sf::Clock animationClock;
|
||||
|
||||
while ( window.isOpen() )
|
||||
{
|
||||
while ( const std::optional event = window.pollEvent() )
|
||||
{
|
||||
if ( event->is<sf::Event::Closed>() )
|
||||
window.close();
|
||||
}
|
||||
|
||||
// time
|
||||
const float dt = clock.restart().asSeconds();
|
||||
|
||||
// update
|
||||
circle.setPosition(circle.getPosition() + sf::Vector2(100.f, 100.f) * dt);
|
||||
player1.Update(dt);
|
||||
player2.Update(dt);
|
||||
|
||||
if (animationClock.getElapsedTime().asMilliseconds() >= 300) {
|
||||
character.setTexture(isA ? walk1Texture : walk2Texture);
|
||||
isA = !isA;
|
||||
animationClock.restart();
|
||||
}
|
||||
|
||||
// draw
|
||||
window.clear();
|
||||
// window.draw( circle );
|
||||
|
||||
for (sf::Drawable* drawable : drawables) {
|
||||
window.draw(*drawable);
|
||||
}
|
||||
|
||||
window.display();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user