mirror of
https://github.com/Floriansylvain/whateversfml.git
synced 2026-08-19 11:43:22 +02:00
feat: new base
This commit is contained in:
+79
-20
@@ -1,9 +1,16 @@
|
|||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Graphics/CircleShape.hpp>
|
||||||
|
#include <SFML/Graphics/Color.hpp>
|
||||||
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
#include <SFML/Graphics/RectangleShape.hpp>
|
#include <SFML/Graphics/RectangleShape.hpp>
|
||||||
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
|
#include <SFML/Graphics/Vertex.hpp>
|
||||||
#include <SFML/System/Clock.hpp>
|
#include <SFML/System/Clock.hpp>
|
||||||
#include <SFML/System/String.hpp>
|
#include <SFML/System/String.hpp>
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
#include <SFML/Window/Cursor.hpp>
|
#include <SFML/Window/Cursor.hpp>
|
||||||
|
#include <SFML/Window/Event.hpp>
|
||||||
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
#include <SFML/Window/Mouse.hpp>
|
#include <SFML/Window/Mouse.hpp>
|
||||||
#include <SFML/Window/Window.hpp>
|
#include <SFML/Window/Window.hpp>
|
||||||
|
|
||||||
@@ -11,34 +18,66 @@ constexpr int DEBUG_REFRESH_RATE_IN_MS = 250;
|
|||||||
constexpr bool VSYNC = true;
|
constexpr bool VSYNC = true;
|
||||||
constexpr int WIDTH = 1920;
|
constexpr int WIDTH = 1920;
|
||||||
constexpr int HEIGHT = 1080;
|
constexpr int HEIGHT = 1080;
|
||||||
|
constexpr float ASPECT = 16.f / 9.f;
|
||||||
|
|
||||||
void updateShapePosition(sf::RectangleShape &shape, sf::RenderWindow &window) {
|
constexpr float SPEED = 3.f;
|
||||||
|
|
||||||
|
void updateShapePosition(sf::CircleShape &shape, sf::RenderWindow &window,
|
||||||
|
sf::View &worldView) {
|
||||||
if (!window.hasFocus())
|
if (!window.hasFocus())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto shapeSize = shape.getSize();
|
auto shapeRadius = shape.getRadius();
|
||||||
auto mousePos = sf::Mouse::getPosition(window);
|
auto mousePos = sf::Mouse::getPosition(window);
|
||||||
|
auto mousePosWorld = window.mapPixelToCoords(mousePos, worldView);
|
||||||
|
|
||||||
shape.setPosition(
|
shape.setPosition(sf::Vector2f(mousePosWorld.x - shapeRadius,
|
||||||
sf::Vector2f(mousePos.x - shapeSize.x / 2, window.getSize().y - 50));
|
mousePosWorld.y - shapeRadius));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string getDebugText(double elapsedMilliseconds) {
|
std::string getDebugText(double elapsedMilliseconds, sf::View &worldView) {
|
||||||
if (elapsedMilliseconds <= 0)
|
if (elapsedMilliseconds <= 0)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
|
auto viewPos = worldView.getCenter();
|
||||||
|
|
||||||
return "V-SYNC: " + std::string(VSYNC ? "On" : "Off") +
|
return "V-SYNC: " + std::string(VSYNC ? "On" : "Off") +
|
||||||
"\nFrametime: " + std::to_string(elapsedMilliseconds) +
|
"\nFrametime: " + std::to_string(elapsedMilliseconds) +
|
||||||
"\nFPS: " + std::to_string((int)(1000 / elapsedMilliseconds));
|
"\nFPS: " + std::to_string((int)(1000 / elapsedMilliseconds)) +
|
||||||
|
"\n\nView x, y: " + std::to_string((int)viewPos.x) + ", " +
|
||||||
|
std::to_string((int)viewPos.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawGrid(sf::RenderWindow &window) {
|
||||||
|
sf::Color gray(64, 64, 64);
|
||||||
|
sf::VertexArray lines(sf::PrimitiveType::Lines);
|
||||||
|
|
||||||
|
for (float x = 0; x <= WIDTH; x += 20.f) {
|
||||||
|
lines.append(sf::Vertex{{x, 0.f}, gray});
|
||||||
|
lines.append(sf::Vertex{{x, HEIGHT}, gray});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (float y = 0; y <= HEIGHT; y += 20.f) {
|
||||||
|
lines.append(sf::Vertex{{0.f, y}, gray});
|
||||||
|
lines.append(sf::Vertex{{WIDTH, y}, gray});
|
||||||
|
}
|
||||||
|
|
||||||
|
window.draw(lines);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "Whatever SFML");
|
sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "Whatever SFML");
|
||||||
window.setVerticalSyncEnabled(VSYNC);
|
window.setVerticalSyncEnabled(VSYNC);
|
||||||
|
|
||||||
sf::RectangleShape shape(sf::Vector2f(200, 25));
|
sf::RectangleShape worldLimit({WIDTH - 6, HEIGHT - 6});
|
||||||
|
worldLimit.setFillColor(sf::Color::Transparent);
|
||||||
|
worldLimit.setOutlineColor(sf::Color::Red);
|
||||||
|
worldLimit.setOutlineThickness(3.f);
|
||||||
|
worldLimit.setPosition({3.f, 3.f});
|
||||||
|
|
||||||
|
sf::CircleShape shape(15.f);
|
||||||
shape.setFillColor(sf::Color(100, 250, 50));
|
shape.setFillColor(sf::Color(100, 250, 50));
|
||||||
shape.setPosition(sf::Vector2f(0, HEIGHT - 50));
|
shape.setPosition({0, HEIGHT});
|
||||||
|
|
||||||
sf::Clock clock;
|
sf::Clock clock;
|
||||||
|
|
||||||
@@ -46,12 +85,12 @@ int main() {
|
|||||||
if (!font.openFromFile("assets/Consolas.ttf"))
|
if (!font.openFromFile("assets/Consolas.ttf"))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
sf::Text text(font);
|
sf::Text debugUI(font);
|
||||||
text.setString(getDebugText(0));
|
debugUI.setString("");
|
||||||
|
|
||||||
sf::View view;
|
sf::View worldView;
|
||||||
view.setSize(sf::Vector2f(WIDTH, HEIGHT));
|
worldView.setSize({WIDTH, HEIGHT});
|
||||||
view.setCenter(sf::Vector2f((float)WIDTH / 2, (float)HEIGHT / 2));
|
worldView.setCenter({(float)WIDTH / 2, (float)HEIGHT / 2});
|
||||||
|
|
||||||
double lastTextUpdateElapsedMs = 0;
|
double lastTextUpdateElapsedMs = 0;
|
||||||
|
|
||||||
@@ -61,17 +100,37 @@ int main() {
|
|||||||
window.close();
|
window.close();
|
||||||
return 0;
|
return 0;
|
||||||
} else if (event->is<sf::Event::Resized>()) {
|
} else if (event->is<sf::Event::Resized>()) {
|
||||||
auto size = window.getSize();
|
auto windowSize = window.getSize();
|
||||||
view.setSize(sf::Vector2f(size.x, size.y));
|
float windowRatio =
|
||||||
view.setCenter(sf::Vector2f((float)size.x / 2, (float)size.y / 2));
|
static_cast<float>(windowSize.x) / static_cast<float>(windowSize.y);
|
||||||
|
float viewWidth = 1.0f;
|
||||||
|
float viewHeight = 1.0f;
|
||||||
|
float posX = 0.0f;
|
||||||
|
float posY = 0.0f;
|
||||||
|
|
||||||
|
if (windowRatio > ASPECT) {
|
||||||
|
viewWidth = ASPECT / windowRatio;
|
||||||
|
posX = (1.0f - viewWidth) / 2.0f;
|
||||||
|
} else {
|
||||||
|
viewHeight = windowRatio / ASPECT;
|
||||||
|
posY = (1.0f - viewHeight) / 2.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
worldView.setViewport(
|
||||||
|
sf::FloatRect({posX, posY}, {viewWidth, viewHeight}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateShapePosition(shape, window, worldView);
|
||||||
|
|
||||||
window.clear();
|
window.clear();
|
||||||
updateShapePosition(shape, window);
|
|
||||||
window.setView(view);
|
window.setView(worldView);
|
||||||
window.draw(shape);
|
window.draw(shape);
|
||||||
window.draw(text);
|
drawGrid(window);
|
||||||
|
// window.draw(worldLimit);
|
||||||
|
window.draw(debugUI);
|
||||||
|
|
||||||
window.display();
|
window.display();
|
||||||
|
|
||||||
double elapsedMilliseconds =
|
double elapsedMilliseconds =
|
||||||
@@ -80,7 +139,7 @@ int main() {
|
|||||||
lastTextUpdateElapsedMs += elapsedMilliseconds;
|
lastTextUpdateElapsedMs += elapsedMilliseconds;
|
||||||
|
|
||||||
if (lastTextUpdateElapsedMs > DEBUG_REFRESH_RATE_IN_MS) {
|
if (lastTextUpdateElapsedMs > DEBUG_REFRESH_RATE_IN_MS) {
|
||||||
text.setString(getDebugText(elapsedMilliseconds));
|
debugUI.setString(getDebugText(elapsedMilliseconds, worldView));
|
||||||
lastTextUpdateElapsedMs = 0;
|
lastTextUpdateElapsedMs = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user