Compare commits
11
Commits
ccfe9fd786
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5a51a375a4 | ||
|
|
22cd090f92 | ||
|
|
67c41106ff | ||
|
|
4a9d85ba1c | ||
|
|
d186b948a7 | ||
|
|
900504971b | ||
|
|
787b25239e | ||
|
|
2c06859473 | ||
|
|
33f673741b | ||
|
|
bce640219f | ||
|
|
0acfb6e0a9 |
@@ -1,3 +1,4 @@
|
||||
# ignore binaries
|
||||
obj
|
||||
main
|
||||
tags
|
||||
|
||||
+34
-61
@@ -1,74 +1,30 @@
|
||||
#include "camera.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#include "maths.h"
|
||||
#include "level.h"
|
||||
|
||||
#define ROTATION_SPEED PI
|
||||
#define TRANSLATIONAL_SPEED 100.f
|
||||
#define TRANSLATIONAL_SPEED 1.f
|
||||
|
||||
static void draw(Camera* camera, sf::RenderWindow* window);
|
||||
static void drawRays(Camera* camera, sf::RenderWindow* window);
|
||||
static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, float length, sf::Color color);
|
||||
static void move(Camera* camera, float t);
|
||||
static void castRays(Camera* camera);
|
||||
|
||||
void camera_update(Camera* camera, sf::RenderWindow* window, float t)
|
||||
int camera_init(Camera* camera, sf::Vector2f pos, float direction, unsigned int resolution, float fov)
|
||||
{
|
||||
if (!camera || !window) return;
|
||||
camera->pos = pos;
|
||||
camera->direction = direction;
|
||||
camera->resolution = resolution;
|
||||
camera->fov = fov;
|
||||
|
||||
draw(camera, window);
|
||||
camera->rays = (CameraRay*)malloc(sizeof(CameraRay)*resolution);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void camera_update(Camera* camera, float t)
|
||||
{
|
||||
if (!camera) return;
|
||||
move(camera, t);
|
||||
}
|
||||
|
||||
static void draw(Camera* camera, sf::RenderWindow* window)
|
||||
{
|
||||
const float circleRadius = 5.f;
|
||||
sf::CircleShape circle(circleRadius);
|
||||
circle.setPosition(camera->pos);
|
||||
circle.setOrigin(circleRadius, circleRadius);
|
||||
circle.setFillColor(sf::Color::Green);
|
||||
|
||||
drawRays(camera, window);
|
||||
drawLine(window, camera->pos, camera->direction, 100, sf::Color::Red);
|
||||
window->draw(circle);
|
||||
}
|
||||
|
||||
static void drawRays(Camera* camera, sf::RenderWindow* window)
|
||||
{
|
||||
float rayDirection = 0;
|
||||
float rayDirectionStep = camera->fov / (float)camera->resolution;
|
||||
bool isOddResolution = (camera->resolution % 2);
|
||||
float rayDirectionOffset = isOddResolution? 0 : rayDirectionStep / 2.f;
|
||||
|
||||
for (unsigned int i = 0; i < camera->resolution; i++) {
|
||||
if (isOddResolution && i == 0)
|
||||
rayDirection = camera->direction;
|
||||
else if (i % 2)
|
||||
rayDirection = camera->direction - rayDirectionOffset;
|
||||
else
|
||||
rayDirection = camera->direction + rayDirectionOffset;
|
||||
|
||||
float distance = level_rayCastDistance(camera->pos, rayDirection);
|
||||
|
||||
drawLine(window, camera->pos, rayDirection, distance, sf::Color(150, 150, 100));
|
||||
|
||||
if ((i + isOddResolution) % 2) rayDirectionOffset += rayDirectionStep;
|
||||
}
|
||||
}
|
||||
|
||||
static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, float length, sf::Color color)
|
||||
{
|
||||
if (!window) return;
|
||||
|
||||
sf::Vector2f endOffset(length * cos(angle), length * sin(angle));
|
||||
sf::Vertex start(pos);
|
||||
sf::Vertex end(pos + endOffset);
|
||||
|
||||
start.color = color;
|
||||
end.color = color;
|
||||
|
||||
sf::Vertex line[] = { start, end };
|
||||
|
||||
window->draw(line, 2, sf::Lines);
|
||||
castRays(camera);
|
||||
}
|
||||
|
||||
static void move(Camera* camera, float t)
|
||||
@@ -85,8 +41,25 @@ static void move(Camera* camera, float t)
|
||||
rotation -=1;
|
||||
|
||||
float magnitude = forward * t * TRANSLATIONAL_SPEED;
|
||||
sf::Vector2f offset(magnitude * cos(camera->direction), magnitude *sin(camera->direction));
|
||||
sf::Vector2f offset(magnitude * cos(camera->direction), magnitude * sin(camera->direction));
|
||||
|
||||
camera->pos += offset;
|
||||
camera->direction += rotation * t * ROTATION_SPEED;
|
||||
}
|
||||
|
||||
void camera_destroy(Camera *camera)
|
||||
{
|
||||
free(camera->rays);
|
||||
}
|
||||
|
||||
static void castRays(Camera* camera)
|
||||
{
|
||||
float rayDirectionStep = camera->fov / (float)camera->resolution;
|
||||
float rayDirection = camera->direction - camera->fov/2.f + rayDirectionStep/2.f;
|
||||
for (unsigned int i = 0; i < camera->resolution; i++) {
|
||||
camera->rays[i].direction = rayDirection;
|
||||
camera->rays[i].distance = level_rayCast(camera->pos, rayDirection, &camera->rays[i].tileData);
|
||||
|
||||
rayDirection += rayDirectionStep;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,14 @@
|
||||
#define CAMERA_H
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "level.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float direction;
|
||||
float distance;
|
||||
TileData tileData;
|
||||
} CameraRay;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
@@ -9,8 +17,12 @@ typedef struct
|
||||
float direction;
|
||||
unsigned int resolution;
|
||||
float fov;
|
||||
|
||||
CameraRay* rays;
|
||||
} Camera;
|
||||
|
||||
void camera_update(Camera* camera, sf::RenderWindow* window, float t);
|
||||
int camera_init(Camera* camera, sf::Vector2f pos, float direction, unsigned int resolution, float fov);
|
||||
void camera_update(Camera* camera, float t);
|
||||
void camera_destroy(Camera* camera);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
#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;
|
||||
|
||||
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;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void firstperson_update(sf::RenderTarget* renderTarget, Camera* camera)
|
||||
{
|
||||
const float columnWidth = (float)width/(float)camera->resolution;
|
||||
float columnHeight = 0;
|
||||
renderTexture.clear();
|
||||
|
||||
for (unsigned int i = 0; i < camera->resolution; i++) {
|
||||
// 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;
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
sf::RectangleShape rectangle(sf::Vector2f(columnWidth, columnHeight));
|
||||
rectangle.setPosition(sf::Vector2f(i*columnWidth, centeredHeight));
|
||||
rectangle.setFillColor(color);
|
||||
renderTexture.draw(rectangle);
|
||||
}
|
||||
|
||||
renderTexture.display();
|
||||
sf::Sprite sprite(renderTexture.getTexture());
|
||||
sprite.setPosition(renderTexturePosition);
|
||||
renderTarget->draw(sprite);
|
||||
|
||||
}
|
||||
|
||||
void firstperson_setTexturePosition(float x, float y)
|
||||
{
|
||||
renderTexturePosition.x = x;
|
||||
renderTexturePosition.y = y;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef FIRSTPERSON_H
|
||||
#define FIRSTPERSON_H
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "camera.h"
|
||||
|
||||
int firstperson_init(unsigned int width, unsigned int height);
|
||||
void firstperson_update(sf::RenderTarget* renderTarget, Camera* camera);
|
||||
void firstperson_setTexturePosition(float x, float y);
|
||||
|
||||
#endif
|
||||
@@ -1,117 +1,63 @@
|
||||
#include "level.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#include "maths.h"
|
||||
|
||||
#define WIDTH 5
|
||||
#define HEIGHT 5
|
||||
#define WIDTH 10
|
||||
#define HEIGHT 10
|
||||
|
||||
static void drawGrid();
|
||||
static void drawGridLine(unsigned int step, bool isHorizontal);
|
||||
static sf::Vertex getGridLineVertex(unsigned int n, unsigned int maxDimension, bool isStart, bool isHorizontal);
|
||||
|
||||
static float castRay(sf::Vector2f point, float direction);
|
||||
static float castRay(sf::Vector2f point, float direction, TileData* tileData);
|
||||
static void getGridIndex(sf::Vector2f point, int* x, int* y);
|
||||
|
||||
static sf::RenderWindow* window = nullptr;
|
||||
|
||||
static unsigned int level[WIDTH * HEIGHT] = {
|
||||
1, 1, 1, 1, 1,
|
||||
0, 0, 0, 0, 0,
|
||||
0, 0, 1, 0, 1,
|
||||
0, 0, 0, 0, 0,
|
||||
0, 0, 1, 0, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 0, 0, 0, 0, 0, 1, 1,
|
||||
1, 1, 1, 0, 0, 0, 0, 2, 0, 1,
|
||||
1, 0, 0, 1, 0, 0, 0, 0, 0, 1,
|
||||
1, 0, 0, 1, 0, 0, 0, 3, 0, 1,
|
||||
1, 0, 0, 1, 0, 0, 0, 0, 0, 1,
|
||||
1, 0, 0, 1, 0, 0, 0, 4, 0, 1,
|
||||
1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
||||
1, 0, 0, 1, 0, 0, 0, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
};
|
||||
|
||||
int level_init(sf::RenderWindow* renderWindow)
|
||||
int level_init()
|
||||
{
|
||||
printf("level_init()\n");
|
||||
window = renderWindow;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int level_update()
|
||||
void level_update(sf::RenderTarget* renderTarget, unsigned int drawSize)
|
||||
{
|
||||
if (!window) return 0;
|
||||
drawGrid();
|
||||
return 1;
|
||||
if (!renderTarget) return;
|
||||
}
|
||||
|
||||
void level_end()
|
||||
{
|
||||
printf("level_end()\n");
|
||||
return;
|
||||
}
|
||||
|
||||
float level_rayCastDistance(sf::Vector2f point, float direction)
|
||||
float level_rayCast(sf::Vector2f point, float direction, TileData* tileData)
|
||||
{
|
||||
return castRay(point, direction);
|
||||
if (!tileData) return -1.f;
|
||||
return castRay(point, direction, tileData);
|
||||
}
|
||||
|
||||
static void drawGrid()
|
||||
void level_getDimensions(unsigned int* width, unsigned int* height)
|
||||
{
|
||||
const sf::Vector2u windowSize = window->getSize();
|
||||
const unsigned int stepX = windowSize.x/WIDTH;
|
||||
const unsigned int stepY = windowSize.y/HEIGHT;
|
||||
|
||||
for (unsigned int x = 0; x < WIDTH; x++) {
|
||||
for (unsigned int y = 0; y < HEIGHT; y++) {
|
||||
if (!level[y * HEIGHT + x]) continue;
|
||||
|
||||
sf::RectangleShape rectangle(sf::Vector2f(stepY, stepY));
|
||||
rectangle.setPosition(x * stepX, y * stepY);
|
||||
|
||||
window->draw(rectangle);
|
||||
}
|
||||
}
|
||||
|
||||
drawGridLine(stepX, true);
|
||||
drawGridLine(stepY, false);
|
||||
*width = WIDTH;
|
||||
*height = HEIGHT;
|
||||
}
|
||||
|
||||
static void drawGridLine(unsigned int step, bool isHorizontal)
|
||||
unsigned int level_getGridValue(unsigned int x, unsigned int y)
|
||||
{
|
||||
unsigned int lines = isHorizontal? WIDTH : HEIGHT;
|
||||
if (x < 0 || WIDTH <= x) return 0;
|
||||
if (y < 0 || HEIGHT <= y) return 0;
|
||||
|
||||
for (unsigned int n = 0; n < lines; n++) {
|
||||
if (n == 0) continue;
|
||||
unsigned int offset = n * step;
|
||||
unsigned int maxDimension = lines * step;
|
||||
sf::Vertex line[] =
|
||||
{
|
||||
getGridLineVertex(offset, maxDimension, true, isHorizontal),
|
||||
getGridLineVertex(offset, maxDimension, false, isHorizontal)
|
||||
};
|
||||
|
||||
window->draw(line, 2, sf::Lines);
|
||||
}
|
||||
return level[y * HEIGHT + x];
|
||||
}
|
||||
|
||||
static sf::Vertex getGridLineVertex(unsigned int offset, unsigned int maxDimension, bool isStart, bool isHorizontal)
|
||||
static float castRay(sf::Vector2f point, float direction, TileData* tileData)
|
||||
{
|
||||
sf::Vertex start;
|
||||
sf::Vertex end;
|
||||
|
||||
if (isHorizontal) {
|
||||
start = sf::Vertex(sf::Vector2f(offset, 0));
|
||||
end = sf::Vertex(sf::Vector2f(offset, maxDimension));
|
||||
}
|
||||
else {
|
||||
start = sf::Vertex(sf::Vector2f(0, offset));
|
||||
end = sf::Vertex(sf::Vector2f(maxDimension, offset));
|
||||
}
|
||||
|
||||
start.color = sf::Color(100, 100, 100);
|
||||
end.color = sf::Color(100, 100, 100);
|
||||
return isStart? start : end;
|
||||
}
|
||||
|
||||
static float castRay(sf::Vector2f point, float direction)
|
||||
{
|
||||
const sf::Vector2u windowSize = window->getSize();
|
||||
const unsigned int tileWidth = windowSize.x/WIDTH;
|
||||
const unsigned int tileHeight = windowSize.y/HEIGHT;
|
||||
|
||||
int indexX, indexY;
|
||||
getGridIndex(point, &indexX, &indexY);
|
||||
|
||||
@@ -135,13 +81,13 @@ static float castRay(sf::Vector2f point, float direction)
|
||||
bool goingDown = direction < PI;
|
||||
int signDown = goingDown? 1 : -1;
|
||||
|
||||
float horizontalDy = (float)((indexY + goingDown) * tileHeight) - point.y;
|
||||
float horizontalDy = (float)(indexY + goingDown) - point.y;
|
||||
float horizontalDx = horizontalDy/tan(direction);
|
||||
|
||||
float horizontalStepX = (float)(signDown * (tileWidth/tan(direction)));
|
||||
float horizontalStepY = (float)(signDown * (int)tileHeight);
|
||||
float horizontalStepX = ((float)signDown * (1.f/tan(direction)));
|
||||
float horizontalStepY = (float)signDown;
|
||||
float horizontalProjectedX = point.x + horizontalDx;
|
||||
float horizontalProjectedY = (indexY + goingDown) * tileHeight;
|
||||
float horizontalProjectedY = indexY + goingDown;
|
||||
|
||||
float horizontalDistCoeff = sin(direction);
|
||||
float horizontalRayDist = std::abs(horizontalDy/horizontalDistCoeff);
|
||||
@@ -150,18 +96,19 @@ static float castRay(sf::Vector2f point, float direction)
|
||||
bool goingRight = direction < PI;
|
||||
int signRight = goingRight? 1 : -1;
|
||||
|
||||
float verticalDx = (float)((indexX + goingRight) * tileWidth) - point.x;
|
||||
float verticalDx = (float)(indexX + goingRight) - point.x;
|
||||
float verticalDy = -verticalDx/tan(direction); // y axis needs to be flipped
|
||||
|
||||
float verticalStepY = -(float)(signRight * (tileHeight/tan(direction))); // y axis also flipped here
|
||||
float verticalStepX = (float)(signRight * (int)tileHeight);
|
||||
float verticalStepY = -((float)signRight * (1.f/tan(direction))); // y axis also flipped here
|
||||
float verticalStepX = (float)signRight;
|
||||
float verticalProjectedY = point.y + verticalDy;
|
||||
float verticalProjectedX = (indexX + goingRight) * tileWidth;
|
||||
float verticalProjectedX = indexX + goingRight;
|
||||
|
||||
float verticalDistCoeff = sin(direction);
|
||||
float verticalRayDist = std::abs(verticalDx/verticalDistCoeff);
|
||||
|
||||
while (true) {
|
||||
unsigned int tries = WIDTH * HEIGHT;
|
||||
while (tries--) {
|
||||
int indexX0, indexY0; // store grid indices for horizontal intersections
|
||||
int indexX1, indexY1; // store grid indices for vertical intersections
|
||||
getGridIndex(sf::Vector2f(horizontalProjectedX, horizontalProjectedY), &indexX0, &indexY0);
|
||||
@@ -179,7 +126,12 @@ static float castRay(sf::Vector2f point, float direction)
|
||||
if (!(inLevel0 || inLevel1)) break;
|
||||
|
||||
if (horizontalRayDist < verticalRayDist) {
|
||||
if (level[indexY0 * WIDTH + indexX0]) return horizontalRayDist;
|
||||
unsigned int gridValue = level[indexY0 * WIDTH + indexX0];
|
||||
if (gridValue) {
|
||||
tileData->value = gridValue;
|
||||
tileData->side = goingDown? NORTH : SOUTH;
|
||||
return horizontalRayDist;
|
||||
}
|
||||
|
||||
horizontalProjectedX += horizontalStepX;
|
||||
horizontalProjectedY += horizontalStepY;
|
||||
@@ -187,7 +139,12 @@ static float castRay(sf::Vector2f point, float direction)
|
||||
horizontalRayDist += std::abs(horizontalStepY/horizontalDistCoeff);
|
||||
}
|
||||
else {
|
||||
if (level[indexY1 * WIDTH + indexX1]) return verticalRayDist;
|
||||
unsigned int gridValue = level[indexY1 * WIDTH + indexX1];
|
||||
if (gridValue) {
|
||||
tileData->value = gridValue;
|
||||
tileData->side = goingRight? WEST : EAST;
|
||||
return verticalRayDist;
|
||||
}
|
||||
|
||||
verticalProjectedX += verticalStepX;
|
||||
verticalProjectedY += verticalStepY;
|
||||
@@ -201,10 +158,8 @@ static float castRay(sf::Vector2f point, float direction)
|
||||
|
||||
static void getGridIndex(sf::Vector2f point, int* x, int* y)
|
||||
{
|
||||
const sf::Vector2u windowSize = window->getSize();
|
||||
|
||||
*x = point.x / (int)(windowSize.x / WIDTH);
|
||||
*y = point.y / (int)(windowSize.y / HEIGHT);
|
||||
*x = point.x;
|
||||
*y = point.y;
|
||||
|
||||
if (*x < 0 || WIDTH <= *x) *x = -1;
|
||||
if (*y < 0 || HEIGHT <= *y) *y = -1;
|
||||
|
||||
@@ -3,9 +3,24 @@
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
int level_init(sf::RenderWindow* renderWindow);
|
||||
int level_update();
|
||||
enum level_tileSide
|
||||
{
|
||||
NORTH,
|
||||
EAST,
|
||||
SOUTH,
|
||||
WEST
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned int value;
|
||||
level_tileSide side;
|
||||
} TileData;
|
||||
|
||||
int level_init();
|
||||
void level_end();
|
||||
float level_rayCastDistance(sf::Vector2f point, float direction);
|
||||
float level_rayCast(sf::Vector2f point, float direction, TileData* tileData);
|
||||
void level_getDimensions(unsigned int* width, unsigned int* height);
|
||||
unsigned int level_getGridValue(unsigned int x, unsigned int y);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
view_init();
|
||||
if (!view_init()) return 1;
|
||||
while (view_update());
|
||||
view_end();
|
||||
|
||||
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
#include "minimap.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#include "maths.h"
|
||||
#include "level.h"
|
||||
// Camera drawing
|
||||
static void drawCamera(sf::RenderTarget* renderTarget, Camera* camera);
|
||||
static void drawRays(sf::RenderTarget* renderTarget, Camera* camera);
|
||||
static void drawLine(sf::RenderTarget* renderTarget, sf::Vector2f pos, float angle, float length, sf::Color color);
|
||||
// Grid drawing
|
||||
static void drawGrid(sf::RenderTarget* renderTarget, unsigned int tileSize);
|
||||
static void drawGridLine(sf::RenderTarget* renderTarget, float step, bool isHorizontal);
|
||||
static sf::Vertex getGridLineVertex(float n, float maxDimension, bool isStart, bool isHorizontal);
|
||||
|
||||
static sf::RenderTexture renderTexture;
|
||||
static sf::Vector2f renderTexturePosition;
|
||||
|
||||
static bool init = false;
|
||||
static unsigned int minimapSize;
|
||||
static unsigned int gridWidth, gridHeight;
|
||||
static float drawScale;
|
||||
|
||||
int minimap_init(unsigned int size)
|
||||
{
|
||||
printf("minimap_init()\n");
|
||||
if (!renderTexture.create(size, size)) return 0;
|
||||
|
||||
minimapSize = size;
|
||||
level_getDimensions(&gridWidth, &gridHeight);
|
||||
drawScale = (float)size/(float)gridWidth;
|
||||
|
||||
init = true;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void minimap_update(sf::RenderTarget* renderTarget, Camera* camera)
|
||||
{
|
||||
if (!init) return;
|
||||
if (!renderTarget || !camera) return;
|
||||
|
||||
renderTexture.clear();
|
||||
drawGrid(&renderTexture, minimapSize/gridWidth);
|
||||
drawCamera(&renderTexture, camera);
|
||||
renderTexture.display();
|
||||
|
||||
sf::Sprite sprite(renderTexture.getTexture());
|
||||
renderTarget->draw(sprite);
|
||||
}
|
||||
|
||||
void minimap_setTexturePosition(float x, float y)
|
||||
{
|
||||
renderTexturePosition.x = x;
|
||||
renderTexturePosition.y = y;
|
||||
}
|
||||
|
||||
static void drawCamera(sf::RenderTarget* renderTarget, Camera* camera)
|
||||
{
|
||||
const sf::Vector2f scaledPos = camera->pos * drawScale;
|
||||
const float circleRadius = 0.02f * minimapSize;
|
||||
sf::CircleShape circle(circleRadius);
|
||||
circle.setPosition(scaledPos);
|
||||
circle.setOrigin(circleRadius, circleRadius);
|
||||
circle.setFillColor(sf::Color::Green);
|
||||
|
||||
drawRays(renderTarget, camera);
|
||||
drawLine(renderTarget, scaledPos, camera->direction, minimapSize / 5.f, sf::Color::Red);
|
||||
renderTarget->draw(circle);
|
||||
}
|
||||
|
||||
static void drawRays(sf::RenderTarget* renderTarget, Camera* camera)
|
||||
{
|
||||
const sf::Vector2f scaledPos = camera->pos * drawScale;
|
||||
|
||||
for (unsigned int i = 0; i < camera->resolution; i++) {
|
||||
CameraRay* ray = &camera->rays[i];
|
||||
drawLine(renderTarget, scaledPos, ray->direction, ray->distance * drawScale, sf::Color(150, 150, 100));
|
||||
}
|
||||
}
|
||||
|
||||
static void drawLine(sf::RenderTarget* renderTarget, sf::Vector2f pos, float angle, float length, sf::Color color)
|
||||
{
|
||||
if (!renderTarget) return;
|
||||
|
||||
sf::Vector2f endOffset(length * cos(angle), length * sin(angle));
|
||||
sf::Vertex start(pos);
|
||||
sf::Vertex end(pos + endOffset);
|
||||
|
||||
start.color = color;
|
||||
end.color = color;
|
||||
|
||||
sf::Vertex line[] = { start, end };
|
||||
|
||||
renderTarget->draw(line, 2, sf::Lines);
|
||||
}
|
||||
|
||||
static void drawGrid(sf::RenderTarget* renderTarget, unsigned int tileSize)
|
||||
{
|
||||
for (unsigned int x = 0; x < gridWidth; x++) {
|
||||
for (unsigned int y = 0; y < gridHeight; y++) {
|
||||
unsigned int value = level_getGridValue(x, y);
|
||||
if (!value) continue;
|
||||
|
||||
sf::Color color(255, 255, 255);
|
||||
switch (value) {
|
||||
case 2:
|
||||
color = sf::Color::Red;
|
||||
break;
|
||||
case 3:
|
||||
color = sf::Color::Green;
|
||||
break;
|
||||
case 4:
|
||||
color = sf::Color::Blue;
|
||||
break;
|
||||
};
|
||||
|
||||
sf::RectangleShape rectangle(sf::Vector2f(tileSize, tileSize));
|
||||
rectangle.setPosition((float)x * tileSize, (float)y * tileSize);
|
||||
rectangle.setFillColor(color);
|
||||
renderTarget->draw(rectangle);
|
||||
}
|
||||
}
|
||||
|
||||
drawGridLine(renderTarget, tileSize, true);
|
||||
drawGridLine(renderTarget, tileSize, false);
|
||||
}
|
||||
|
||||
static void drawGridLine(sf::RenderTarget* renderTarget, float step, bool isHorizontal)
|
||||
{
|
||||
unsigned int lines = isHorizontal? gridWidth : gridHeight;
|
||||
|
||||
for (unsigned int n = 0; n < lines; n++) {
|
||||
if (n == 0) continue;
|
||||
float offset = (float)n * step;
|
||||
float maxDimension = (float)lines * step;
|
||||
sf::Vertex line[] =
|
||||
{
|
||||
getGridLineVertex(offset, maxDimension, true, isHorizontal),
|
||||
getGridLineVertex(offset, maxDimension, false, isHorizontal)
|
||||
};
|
||||
|
||||
renderTarget->draw(line, 2, sf::Lines);
|
||||
}
|
||||
}
|
||||
|
||||
static sf::Vertex getGridLineVertex(float offset, float maxDimension, bool isStart, bool isHorizontal)
|
||||
{
|
||||
sf::Vertex start;
|
||||
sf::Vertex end;
|
||||
|
||||
if (isHorizontal) {
|
||||
start = sf::Vertex(sf::Vector2f(offset, 0));
|
||||
end = sf::Vertex(sf::Vector2f(offset, maxDimension));
|
||||
}
|
||||
else {
|
||||
start = sf::Vertex(sf::Vector2f(0, offset));
|
||||
end = sf::Vertex(sf::Vector2f(maxDimension, offset));
|
||||
}
|
||||
|
||||
sf::Color color(100, 100, 100);
|
||||
start.color = color;
|
||||
end.color = color;
|
||||
return isStart? start : end;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef MINIMAP_H
|
||||
#define MINIMAP_H
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "camera.h"
|
||||
|
||||
int minimap_init(unsigned int size);
|
||||
void minimap_update(sf::RenderTarget* renderTarget, Camera* camera);
|
||||
void minimap_setTexturePosition(float x, float y);
|
||||
|
||||
#endif
|
||||
@@ -2,22 +2,32 @@
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "level.h"
|
||||
#include "camera.h"
|
||||
#include "maths.h"
|
||||
#include "camera.h"
|
||||
#include "minimap.h"
|
||||
#include "firstperson.h"
|
||||
|
||||
#define MINIMAP_SIZE 512
|
||||
#define VIEW_SIZE MINIMAP_SIZE*2
|
||||
|
||||
static int handleKeyCode(sf::Keyboard::Key key);
|
||||
|
||||
static sf::Uint32 style = sf::Style::Titlebar;
|
||||
static sf::RenderWindow window(sf::VideoMode(500, 500), "Raycasting", style);
|
||||
static sf::Clock timer;
|
||||
static Camera camera;
|
||||
|
||||
static Camera camera = { sf::Vector2f(300.f, 250.f), 0.f, 100, 2.0f*PI };
|
||||
static sf::Uint32 style = sf::Style::Titlebar;
|
||||
static sf::RenderWindow window(sf::VideoMode(MINIMAP_SIZE + VIEW_SIZE, MINIMAP_SIZE), "Raycasting", style);
|
||||
static sf::Clock timer;
|
||||
|
||||
int view_init()
|
||||
{
|
||||
printf("view_init()\n");
|
||||
level_init(&window);
|
||||
if (!camera_init(&camera, sf::Vector2f(10.f/2.f, 10.f/2.f), 0.f, VIEW_SIZE, 0.5f*PI)) return 0;
|
||||
if (!minimap_init(MINIMAP_SIZE)) return 0;
|
||||
if (!firstperson_init(VIEW_SIZE, MINIMAP_SIZE)) return 0;
|
||||
|
||||
minimap_setTexturePosition(0.f, 0.f);
|
||||
firstperson_setTexturePosition(MINIMAP_SIZE, 0.f);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -38,11 +48,14 @@ int view_update()
|
||||
}
|
||||
}
|
||||
|
||||
window.clear();
|
||||
|
||||
sf::Time t = timer.restart();
|
||||
if (!level_update()) return 0;
|
||||
camera_update(&camera, &window, t.asSeconds());
|
||||
|
||||
camera_update(&camera, t.asSeconds());
|
||||
|
||||
window.clear();
|
||||
minimap_update(&window, &camera);
|
||||
firstperson_update(&window, &camera);
|
||||
|
||||
window.display();
|
||||
|
||||
return 1;
|
||||
@@ -50,10 +63,9 @@ int view_update()
|
||||
|
||||
void view_end()
|
||||
{
|
||||
printf("view_end()\n");
|
||||
level_end();
|
||||
|
||||
camera_destroy(&camera);
|
||||
window.close();
|
||||
printf("view_end()\n");
|
||||
}
|
||||
|
||||
static int handleKeyCode(sf::Keyboard::Key key)
|
||||
|
||||
Reference in New Issue
Block a user