2023-03-31 06:49:36 +08:00
|
|
|
#include "view.h"
|
2023-03-31 08:08:02 +08:00
|
|
|
#include <SFML/Graphics.hpp>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2023-04-05 03:02:49 +08:00
|
|
|
#include "maths.h"
|
2023-04-16 09:17:11 +08:00
|
|
|
#include "camera.h"
|
|
|
|
#include "minimap.h"
|
2023-04-20 07:31:41 +08:00
|
|
|
#include "firstperson.h"
|
2023-04-16 09:17:11 +08:00
|
|
|
|
2023-04-20 09:21:21 +08:00
|
|
|
#define MINIMAP_SIZE 720
|
2023-04-20 07:28:17 +08:00
|
|
|
#define VIEW_SIZE MINIMAP_SIZE*2
|
2023-03-31 06:49:36 +08:00
|
|
|
|
2023-04-01 05:56:26 +08:00
|
|
|
static int handleKeyCode(sf::Keyboard::Key key);
|
|
|
|
|
2023-04-20 07:28:17 +08:00
|
|
|
static Camera camera;
|
2023-04-16 09:17:11 +08:00
|
|
|
|
2023-03-31 06:49:36 +08:00
|
|
|
static sf::Uint32 style = sf::Style::Titlebar;
|
2023-04-20 07:28:17 +08:00
|
|
|
static sf::RenderWindow window(sf::VideoMode(MINIMAP_SIZE + VIEW_SIZE, MINIMAP_SIZE), "Raycasting", style);
|
2023-04-02 07:41:32 +08:00
|
|
|
static sf::Clock timer;
|
2023-03-31 06:49:36 +08:00
|
|
|
|
|
|
|
int view_init()
|
|
|
|
{
|
2023-03-31 08:08:02 +08:00
|
|
|
printf("view_init()\n");
|
2023-04-20 09:21:21 +08:00
|
|
|
if (!camera_init(&camera, sf::Vector2f(10.f/2.f, 10.f/2.f), 0.f, 128, 0.5f*PI)) return 0;
|
2023-04-20 07:28:17 +08:00
|
|
|
if (!minimap_init(MINIMAP_SIZE)) return 0;
|
2023-04-20 07:31:41 +08:00
|
|
|
if (!firstperson_init(VIEW_SIZE, MINIMAP_SIZE)) return 0;
|
2023-04-20 07:28:17 +08:00
|
|
|
|
|
|
|
minimap_setTexturePosition(0.f, 0.f);
|
2023-04-20 07:31:41 +08:00
|
|
|
firstperson_setTexturePosition(MINIMAP_SIZE, 0.f);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-02 07:41:32 +08:00
|
|
|
sf::Time t = timer.restart();
|
2023-04-16 09:17:11 +08:00
|
|
|
|
|
|
|
camera_update(&camera, t.asSeconds());
|
|
|
|
|
|
|
|
window.clear();
|
|
|
|
minimap_update(&window, &camera);
|
2023-04-20 09:21:21 +08:00
|
|
|
firstperson_update(&window, &camera);
|
2023-03-31 06:49:36 +08:00
|
|
|
window.display();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-03-31 08:08:02 +08:00
|
|
|
void view_end()
|
|
|
|
{
|
2023-04-20 07:28:17 +08:00
|
|
|
camera_destroy(&camera);
|
2023-04-01 05:56:26 +08:00
|
|
|
window.close();
|
2023-04-20 07:28:17 +08:00
|
|
|
printf("view_end()\n");
|
2023-04-01 05:56:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int handleKeyCode(sf::Keyboard::Key key)
|
|
|
|
{
|
|
|
|
switch (key) {
|
|
|
|
case sf::Keyboard::Escape:
|
|
|
|
case sf::Keyboard::Q:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
2023-03-31 08:08:02 +08:00
|
|
|
}
|