raycasting/view.cpp

68 lines
1.1 KiB
C++
Raw Normal View History

2023-03-31 06:49:36 +08:00
#include "view.h"
#include <SFML/Graphics.hpp>
#include <stdio.h>
#include "level.h"
2023-04-01 07:16:14 +08:00
#include "camera.h"
2023-03-31 06:49:36 +08:00
2023-04-01 05:56:26 +08:00
static int handleKeyCode(sf::Keyboard::Key key);
2023-03-31 06:49:36 +08:00
static sf::Uint32 style = sf::Style::Titlebar;
static sf::RenderWindow window(sf::VideoMode(500, 500), "Raycasting", style);
2023-04-02 07:41:32 +08:00
static sf::Clock timer;
2023-03-31 06:49:36 +08:00
2023-04-01 07:16:14 +08:00
static Camera camera = { sf::Vector2f(300.f, 250.f), 0.f, 1.f, 70.f };
2023-03-31 06:49:36 +08:00
int view_init()
{
printf("view_init()\n");
level_init(&window);
2023-03-31 06:49:36 +08:00
return 1;
}
int view_update()
{
if (!window.isOpen()) return 0;
sf::Event event;
while (window.pollEvent(event)) {
2023-04-01 05:56:26 +08:00
switch (event.type) {
case sf::Event::Closed:
return 0;
case sf::Event::KeyPressed:
if (handleKeyCode(event.key.code)) continue;
return 0;
default:
continue;
2023-03-31 06:49:36 +08:00
}
}
window.clear();
2023-04-02 07:41:32 +08:00
sf::Time t = timer.restart();
if (!level_update()) return 0;
2023-04-02 07:41:32 +08:00
camera_update(&camera, &window, t.asSeconds());
2023-03-31 06:49:36 +08:00
window.display();
return 1;
}
void view_end()
{
printf("view_end()\n");
level_end();
2023-04-01 05:56:26 +08:00
window.close();
}
static int handleKeyCode(sf::Keyboard::Key key)
{
switch (key) {
case sf::Keyboard::Escape:
case sf::Keyboard::Q:
return 0;
default:
return 1;
}
}