-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
143 lines (124 loc) · 4.38 KB
/
main.cpp
File metadata and controls
143 lines (124 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* @file main.cpp
* @brief Simple SFML 3 Demo Application
*
* This application demonstrates basic SFML 3 features:
* - Window creation and event handling
* - Shape rendering (circle and rectangle)
* - Basic animation with delta time
* - Keyboard input handling
*
* @author SFML Demo Project
* @date 2025
*/
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <cmath>
/**
* @brief Application configuration constants
*/
namespace Config
{
constexpr unsigned int WINDOW_WIDTH = 800;
constexpr unsigned int WINDOW_HEIGHT = 600;
constexpr float CIRCLE_RADIUS = 50.0f;
constexpr float MOVEMENT_SPEED = 200.0f;
constexpr float ROTATION_SPEED = 90.0f;
}
/**
* @brief Main entry point of the application
* @return Exit code (0 for success)
*/
int main()
{
// Create the main window with SFML 3 API
sf::RenderWindow window(
sf::VideoMode({Config::WINDOW_WIDTH, Config::WINDOW_HEIGHT}),
"SFML 3 Static Demo",
sf::Style::Default
);
window.setFramerateLimit(60);
// Create a circle shape
sf::CircleShape circle(Config::CIRCLE_RADIUS);
circle.setFillColor(sf::Color::Cyan);
circle.setOutlineColor(sf::Color::White);
circle.setOutlineThickness(3.0f);
circle.setOrigin({Config::CIRCLE_RADIUS, Config::CIRCLE_RADIUS});
circle.setPosition({Config::WINDOW_WIDTH / 2.0f, Config::WINDOW_HEIGHT / 2.0f});
// Create a rectangle shape
sf::RectangleShape rectangle({100.0f, 60.0f});
rectangle.setFillColor(sf::Color::Magenta);
rectangle.setOutlineColor(sf::Color::Yellow);
rectangle.setOutlineThickness(2.0f);
rectangle.setOrigin({50.0f, 30.0f});
rectangle.setPosition({200.0f, 150.0f});
// Create instructions text (using default font is not available in SFML 3,
// so we'll just display shapes without text for simplicity)
// Clock for delta time calculation
sf::Clock clock;
// Main game loop
while (window.isOpen())
{
// Calculate delta time
float deltaTime = clock.restart().asSeconds();
// Process events using SFML 3 API
while (const auto event = window.pollEvent())
{
// Window closed event
if (event->is<sf::Event::Closed>())
{
window.close();
}
// Key pressed event
if (const auto* keyPressed = event->getIf<sf::Event::KeyPressed>())
{
if (keyPressed->code == sf::Keyboard::Key::Escape)
{
window.close();
}
}
}
// Handle continuous keyboard input for movement
sf::Vector2f movement{0.0f, 0.0f};
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up))
{
movement.y -= Config::MOVEMENT_SPEED * deltaTime;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down))
{
movement.y += Config::MOVEMENT_SPEED * deltaTime;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
{
movement.x -= Config::MOVEMENT_SPEED * deltaTime;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right))
{
movement.x += Config::MOVEMENT_SPEED * deltaTime;
}
// Apply movement to circle
circle.move(movement);
// Rotate the rectangle continuously
rectangle.rotate(sf::degrees(Config::ROTATION_SPEED * deltaTime));
// Keep circle within window bounds
sf::Vector2f circlePos = circle.getPosition();
circlePos.x = std::clamp(circlePos.x, Config::CIRCLE_RADIUS,
Config::WINDOW_WIDTH - Config::CIRCLE_RADIUS);
circlePos.y = std::clamp(circlePos.y, Config::CIRCLE_RADIUS,
Config::WINDOW_HEIGHT - Config::CIRCLE_RADIUS);
circle.setPosition(circlePos);
// Clear the window with a dark background
window.clear(sf::Color{30, 30, 40});
// Draw shapes
window.draw(rectangle);
window.draw(circle);
// Display the rendered frame
window.display();
}
return 0;
}