2023-04-20 07:31:41 +08:00
|
|
|
#include "firstperson.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "maths.h"
|
|
|
|
|
|
|
|
static sf::RenderTexture renderTexture;
|
|
|
|
static sf::Vector2f renderTexturePosition;
|
|
|
|
|
|
|
|
static bool init = false;
|
|
|
|
static unsigned int width;
|
|
|
|
static unsigned int height;
|
|
|
|
|
2024-01-15 18:35:35 +08:00
|
|
|
static sf::Image image;
|
|
|
|
|
2023-04-20 07:31:41 +08:00
|
|
|
int firstperson_init(unsigned int _width, unsigned int _height)
|
|
|
|
{
|
|
|
|
printf("firstperson_init()\n");
|
|
|
|
if (!renderTexture.create(_width, _height)) return 0;
|
|
|
|
width = _width;
|
|
|
|
height = _height;
|
|
|
|
init = true;
|
2024-01-15 18:35:35 +08:00
|
|
|
|
|
|
|
if (!image.loadFromFile("xd.bmp")) return 0;
|
|
|
|
|
2023-04-20 07:31:41 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-04-20 09:21:21 +08:00
|
|
|
void firstperson_update(sf::RenderTarget* renderTarget, Camera* camera)
|
2023-04-20 07:31:41 +08:00
|
|
|
{
|
2023-04-20 09:21:21 +08:00
|
|
|
const float columnWidth = (float)width/(float)camera->resolution;
|
|
|
|
float columnHeight = 0;
|
2023-04-20 07:31:41 +08:00
|
|
|
renderTexture.clear();
|
|
|
|
|
2023-04-20 09:21:21 +08:00
|
|
|
for (unsigned int i = 0; i < camera->resolution; i++) {
|
2023-05-01 05:47:45 +08:00
|
|
|
// Get distance of ray intersection in the direction of the camera,
|
|
|
|
// instead of the actual ray distance to avoid fish eye effect.
|
|
|
|
float angleDiff = camera->rays[i].direction - camera->direction;
|
|
|
|
float distance = camera->rays[i].distance * cos(angleDiff);
|
|
|
|
|
|
|
|
if (distance > 0.f) columnHeight = 0.5f * (float)height/distance;
|
2023-04-20 09:21:21 +08:00
|
|
|
|
|
|
|
float centeredHeight = (float)height/2.f - columnHeight/2.f;
|
|
|
|
float distanceScale = distance/10.f;
|
|
|
|
distanceScale = (distanceScale > 1.f)? 1.f : distanceScale;
|
|
|
|
float brightness = 255.f*(1.f-distanceScale);
|
|
|
|
|
2023-05-01 07:10:01 +08:00
|
|
|
switch (camera->rays[i].tileData.side) {
|
|
|
|
case NORTH:
|
|
|
|
break;
|
|
|
|
case EAST:
|
|
|
|
case WEST:
|
|
|
|
brightness *= 0.9;
|
|
|
|
break;
|
|
|
|
case SOUTH:
|
|
|
|
brightness *= 0.8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sf::Color color(brightness, brightness, brightness);
|
|
|
|
switch (camera->rays[i].tileData.value) {
|
|
|
|
case 2:
|
|
|
|
color *= sf::Color::Red;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
color *= sf::Color::Green;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
color *= sf::Color::Blue;
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
2023-04-20 09:21:21 +08:00
|
|
|
sf::RectangleShape rectangle(sf::Vector2f(columnWidth, columnHeight));
|
|
|
|
rectangle.setPosition(sf::Vector2f(i*columnWidth, centeredHeight));
|
2023-05-01 07:10:01 +08:00
|
|
|
rectangle.setFillColor(color);
|
2023-04-20 09:21:21 +08:00
|
|
|
renderTexture.draw(rectangle);
|
|
|
|
}
|
|
|
|
|
2024-01-15 18:35:35 +08:00
|
|
|
sf::Texture imageTexture;
|
|
|
|
sf::Sprite imageSprite;
|
|
|
|
imageTexture.loadFromImage(image);
|
|
|
|
imageSprite.setTexture(imageTexture);
|
|
|
|
renderTexture.draw(imageSprite);
|
|
|
|
|
2023-04-20 09:21:21 +08:00
|
|
|
renderTexture.display();
|
2023-04-20 07:31:41 +08:00
|
|
|
sf::Sprite sprite(renderTexture.getTexture());
|
|
|
|
sprite.setPosition(renderTexturePosition);
|
2023-04-20 09:21:21 +08:00
|
|
|
|
2024-01-15 18:35:35 +08:00
|
|
|
renderTarget->draw(sprite);
|
2023-04-20 07:31:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void firstperson_setTexturePosition(float x, float y)
|
|
|
|
{
|
|
|
|
renderTexturePosition.x = x;
|
|
|
|
renderTexturePosition.y = y;
|
|
|
|
}
|