From 787b25239eba37a6984090b6b0b5e84649a10962 Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Thu, 20 Apr 2023 00:31:41 +0100 Subject: [PATCH] Add first person view. --- firstperson.cpp | 39 +++++++++++++++++++++++++++++++++++++++ firstperson.h | 10 ++++++++++ view.cpp | 4 ++++ 3 files changed, 53 insertions(+) create mode 100644 firstperson.cpp create mode 100644 firstperson.h diff --git a/firstperson.cpp b/firstperson.cpp new file mode 100644 index 0000000..5af43a0 --- /dev/null +++ b/firstperson.cpp @@ -0,0 +1,39 @@ +#include "firstperson.h" +#include + +#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) +{ + renderTexture.clear(); + sf::CircleShape shape(50); + renderTexture.draw(shape); + 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; +} diff --git a/firstperson.h b/firstperson.h new file mode 100644 index 0000000..11434ab --- /dev/null +++ b/firstperson.h @@ -0,0 +1,10 @@ +#ifndef FIRSTPERSON_H +#define FIRSTPERSON_H + +#include + +int firstperson_init(unsigned int width, unsigned int height); +void firstperson_update(sf::RenderTarget* renderTarget); +void firstperson_setTexturePosition(float x, float y); + +#endif diff --git a/view.cpp b/view.cpp index 5ba2115..a4a7fcf 100644 --- a/view.cpp +++ b/view.cpp @@ -5,6 +5,7 @@ #include "maths.h" #include "camera.h" #include "minimap.h" +#include "firstperson.h" #define MINIMAP_SIZE 460 #define VIEW_SIZE MINIMAP_SIZE*2 @@ -22,8 +23,10 @@ int view_init() printf("view_init()\n"); if (!camera_init(&camera, sf::Vector2f(5.f/2.f, 5.f/2.f), 0.f, 100, 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; } @@ -50,6 +53,7 @@ int view_update() window.clear(); minimap_update(&window, &camera); + firstperson_update(&window); window.display(); return 1;