2019-05-27 13:50:49 +02:00
|
|
|
#include <iostream>
|
2024-01-21 22:03:04 +00:00
|
|
|
#include <SFML/Graphics.hpp>
|
2019-05-27 13:50:49 +02:00
|
|
|
|
2024-01-26 23:00:26 +00:00
|
|
|
#ifdef IMGUI
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <imgui-SFML.h>
|
|
|
|
#endif
|
|
|
|
|
2019-05-27 13:50:49 +02:00
|
|
|
#include "World.h"
|
|
|
|
|
2023-12-30 14:50:35 +01:00
|
|
|
// TODO: Find a way to go to edges instead of just equally split (?)
|
|
|
|
|
2019-05-27 13:50:49 +02:00
|
|
|
int main()
|
|
|
|
{
|
2024-01-28 22:08:35 +00:00
|
|
|
World world(32,32);
|
|
|
|
world.setBlock(BlockType::AIR,1,1,30,30);
|
|
|
|
world.setBlock(BlockType::WALL,4,4,2,2);
|
|
|
|
world.player.move(2,2);
|
|
|
|
std::cout << world << std::endl;
|
2019-06-01 06:42:29 +02:00
|
|
|
|
2024-01-28 22:08:35 +00:00
|
|
|
sf::RenderWindow window(sf::VideoMode(1000,1000),"Da raycasting");
|
2024-01-26 23:00:26 +00:00
|
|
|
#ifdef IMGUI
|
|
|
|
if (!ImGui::SFML::Init(window)) {
|
|
|
|
std::cout << "Failed to init Dear ImGui SFML" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sf::Clock fpsDataClock;
|
|
|
|
float frameTimings[100];
|
|
|
|
unsigned int frameCount = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// window.setFramerateLimit(60);
|
2019-06-01 06:42:29 +02:00
|
|
|
|
2024-01-28 22:08:35 +00:00
|
|
|
sf::Event event{};
|
|
|
|
sf::Clock frameTime;
|
|
|
|
while (window.isOpen())
|
|
|
|
{
|
|
|
|
while (window.pollEvent(event))
|
|
|
|
{
|
2024-01-24 23:09:59 +00:00
|
|
|
if (event.type == sf::Event::Closed) {
|
|
|
|
window.close();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (event.type == sf::Event::Resized) {
|
|
|
|
// Keep the view area fit to the window.
|
|
|
|
sf::FloatRect newView(0, 0,
|
|
|
|
static_cast<float>(event.size.width),
|
|
|
|
static_cast<float>(event.size.height));
|
|
|
|
window.setView(sf::View(newView));
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-26 23:00:26 +00:00
|
|
|
#ifdef IMGUI
|
|
|
|
ImGui::SFML::ProcessEvent(window, event);
|
|
|
|
// Check if Dear ImGui should process the key events.
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
if (io.WantCaptureMouse || io.WantCaptureKeyboard)
|
|
|
|
continue;
|
|
|
|
#endif
|
2024-01-28 22:08:35 +00:00
|
|
|
if (event.type == sf::Event::KeyPressed) {
|
|
|
|
switch (event.key.code) {
|
|
|
|
case sf::Keyboard::Key::Escape:
|
|
|
|
window.close();
|
|
|
|
break;
|
|
|
|
case sf::Keyboard::Key::Left:
|
|
|
|
world.player.currentRotationSpeed = -world.player.rotationSpeed;
|
|
|
|
break;
|
|
|
|
case sf::Keyboard::Key::Right:
|
|
|
|
world.player.currentRotationSpeed = world.player.rotationSpeed;
|
|
|
|
break;
|
2024-01-21 22:11:46 +00:00
|
|
|
case sf::Keyboard::Key::Up:
|
|
|
|
world.player.updateSpeed(1, 0);
|
|
|
|
break;
|
|
|
|
case sf::Keyboard::Key::Down:
|
|
|
|
world.player.updateSpeed(-1, 0);
|
|
|
|
break;
|
2024-01-28 22:08:35 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (event.type == sf::Event::KeyReleased) {
|
|
|
|
switch (event.key.code) {
|
|
|
|
case sf::Keyboard::Key::Left:
|
|
|
|
case sf::Keyboard::Key::Right:
|
|
|
|
world.player.currentRotationSpeed = 0;
|
|
|
|
break;
|
2024-01-21 22:11:46 +00:00
|
|
|
case sf::Keyboard::Key::Up:
|
|
|
|
case sf::Keyboard::Key::Down:
|
|
|
|
world.player.updateSpeed(0, 0);
|
|
|
|
break;
|
2024-01-28 22:08:35 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.clear();
|
2024-01-26 23:00:26 +00:00
|
|
|
const sf::Time& deltaT = frameTime.restart();
|
|
|
|
|
|
|
|
#ifdef IMGUI
|
|
|
|
ImGui::SFML::Update(window, deltaT);
|
|
|
|
#endif
|
2024-01-21 22:03:04 +00:00
|
|
|
|
2024-01-26 23:00:26 +00:00
|
|
|
world.step(deltaT.asSeconds());
|
2024-01-28 22:08:35 +00:00
|
|
|
world.render(window);
|
2024-01-26 23:00:26 +00:00
|
|
|
|
|
|
|
#ifdef IMGUI
|
|
|
|
// ImGui::ShowDemoWindow();
|
|
|
|
|
|
|
|
ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDecoration
|
|
|
|
| ImGuiWindowFlags_AlwaysAutoResize
|
|
|
|
| ImGuiWindowFlags_NoSavedSettings
|
|
|
|
| ImGuiWindowFlags_NoFocusOnAppearing
|
|
|
|
| ImGuiWindowFlags_NoNav;
|
|
|
|
ImVec2 window_area = ImGui::GetMainViewport()->WorkSize;
|
|
|
|
ImGui::SetNextWindowPos({window_area.x - 20, 20}, ImGuiCond_Always, {1,0});
|
|
|
|
ImGui::SetNextWindowBgAlpha(0.2f);
|
|
|
|
ImGui::Begin("FPS", nullptr ,window_flags);
|
|
|
|
if (fpsDataClock.getElapsedTime().asMilliseconds() > 20) {
|
|
|
|
frameTimings[frameCount%100] = static_cast<float>(deltaT.asMicroseconds());
|
|
|
|
frameCount++;
|
|
|
|
fpsDataClock.restart();
|
|
|
|
}
|
|
|
|
ImGui::Text("FPS : %d", (int)(1.0/deltaT.asSeconds()));
|
|
|
|
ImGui::PlotLines("µs", frameTimings, frameCount >= 100 ? 100 : frameCount);
|
|
|
|
ImGui::End();
|
|
|
|
|
|
|
|
ImGui::SFML::Render(window);
|
|
|
|
#endif
|
|
|
|
|
2024-01-28 22:08:35 +00:00
|
|
|
window.display();
|
|
|
|
}
|
2019-06-01 06:42:29 +02:00
|
|
|
|
2024-01-26 23:00:26 +00:00
|
|
|
#ifdef IMGUI
|
|
|
|
ImGui::SFML::Shutdown();
|
|
|
|
#endif
|
2024-01-28 22:08:35 +00:00
|
|
|
return 0;
|
2023-12-30 14:50:35 +01:00
|
|
|
}
|