Skip to content

Commit

Permalink
Render static object in game
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalkah committed Aug 11, 2024
1 parent 3801854 commit e730763
Show file tree
Hide file tree
Showing 17 changed files with 398 additions and 83 deletions.
7 changes: 5 additions & 2 deletions src/Camera/include/Camera/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <Camera/ray.h>
#include <Characters/character.h>
#include <GameObjects/game_object.h>
#include <GameObjects/static_object.h>
#include <Map/map.h>
#include <memory>

Expand All @@ -39,11 +40,13 @@ class Camera2D
std::shared_ptr<RayVector> GetRays() const;
std::shared_ptr<Ray> GetCrosshairRay() const;
Position2D GetPosition() const;
double GetFov() const { return config_.fov; }
double GetDeltaAngle() const { return config_.fov / config_.width; }
double GetFov() const;
double GetDeltaAngle() const;

void SetPosition(const Position2D& position);

Ray CalculateObjectRay(const std::shared_ptr<IGameObject>& object) const;

private:
void InitRays();
Camera2DConfig config_;
Expand Down
5 changes: 3 additions & 2 deletions src/Camera/include/Camera/raycaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@ namespace wolfenstein {
class RayCaster
{
public:
RayCaster(const int width, const double fov, const double depth);
RayCaster(const int num_ray, const double fov, const double depth);
~RayCaster();

void Update(const std::shared_ptr<Map>& map_ptr, const Position2D& position,
const std::shared_ptr<RayVector>& rays);

Ray Cast(const std::shared_ptr<Map>& map_ptr, const Position2D& position,
const double ray_theta);
double GetDeltaTheta() const;

private:
void PrepareRay(const Position2D& position, const double ray_angle,
Ray& ray, vector2d& ray_unit_step, vector2d& ray_length_1d,
vector2i& step, vector2i& map_check);

int width_;
int num_ray_;
double fov_;
double depth_;
double delta_theta_;
Expand Down
50 changes: 46 additions & 4 deletions src/Camera/src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@
*/

#include "Camera/camera.h"
#include "Math/vector.h"
#include <cmath>

namespace wolfenstein {

Camera2D::Camera2D(const Camera2DConfig& config)
: config_(config),
ray_cast_(
std::make_shared<RayCaster>(config.width, config.fov, config.depth)) {
ray_cast_(std::make_shared<RayCaster>(config.width / 2, config.fov,
config.depth)) {
InitRays();
}

void Camera2D::Update(const std::shared_ptr<Map>& map_ptr) {
ray_cast_->Update(map_ptr, position_, rays_);
crosshair_ray_ = std::make_shared<Ray>(rays_->at(config_.width / 2));
crosshair_ray_ = std::make_shared<Ray>(rays_->at(config_.width / 4));
}

std::shared_ptr<RayVector> Camera2D::GetRays() const {
Expand All @@ -37,13 +39,53 @@ Position2D Camera2D::GetPosition() const {
return position_;
}

double Camera2D::GetFov() const {
return config_.fov;
}
double Camera2D::GetDeltaAngle() const {
return ray_cast_->GetDeltaTheta();
}

void Camera2D::SetPosition(const Position2D& position) {
position_ = position;
}

Ray Camera2D::CalculateObjectRay(
const std::shared_ptr<IGameObject>& object) const {
Ray object_ray{};
auto static_object = std::dynamic_pointer_cast<StaticObject>(object);
const auto object_pose = static_object->GetPose();
const auto w = static_object->GetWidth();

// check if object is in the camera view
auto object_distance = Distance(object_pose, position_.pose);
if (object_distance > config_.depth) {
return object_ray;
}

const auto object_center_angle = std::atan2(
object_pose.y - position_.pose.y, object_pose.x - position_.pose.x);

auto object_left_edge_angle =
SubRadian(object_center_angle, ToRadians(90.0));
const auto left_edge_point =
object_pose + vector2d{w * std::cos(object_left_edge_angle),
w * std::sin(object_left_edge_angle)};
const auto left_edge_angle =
std::atan2(left_edge_point.y - position_.pose.y,
left_edge_point.x - position_.pose.x);
object_ray.theta =
left_edge_angle > 0 ? left_edge_angle : 2 * M_PI + left_edge_angle;
object_ray.is_hit = true;
object_ray.perpendicular_distance = object_distance;
object_ray.wall_id = static_object->GetTextureId();

return object_ray;
}

void Camera2D::InitRays() {
rays_ = std::make_shared<RayVector>();
for (int i = 0; i < config_.width; i++) {
for (int i = 0; i < config_.width / 2; i++) {
rays_->emplace_back(Ray());
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/Camera/src/raycaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace wolfenstein {

RayCaster::RayCaster(const int width, const double fov, const double depth)
: width_(width), fov_(fov), depth_(depth) {
delta_theta_ = fov_ / width_;
}
RayCaster::RayCaster(const int num_ray, const double fov, const double depth)
: num_ray_(num_ray),
fov_(fov),
depth_(depth),
delta_theta_(fov_ / num_ray) {}

RayCaster::~RayCaster() {}

Expand Down Expand Up @@ -61,6 +62,10 @@ Ray RayCaster::Cast(const std::shared_ptr<Map>& map_ptr,
return ray;
}

double RayCaster::GetDeltaTheta() const {
return delta_theta_;
}

void RayCaster::PrepareRay(const Position2D& position, const double ray_theta,
Ray& ray, vector2d& ray_unit_step,
vector2d& ray_length_1d, vector2i& step,
Expand Down
12 changes: 6 additions & 6 deletions src/Characters/src/player.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Characters/player.h"
#include "CollisionManager/collision_manager.h"
#include "Math/vector.h"
#include <SDL2/SDL.h>

namespace wolfenstein {
Expand Down Expand Up @@ -52,14 +53,14 @@ void Player::Move(double delta_time) {
}
if (keystate[SDL_SCANCODE_A]) {
delta_movement.first += speed_sin;
delta_movement.second += -speed_cos;
delta_movement.second -= speed_cos;
}
if (keystate[SDL_SCANCODE_S]) {
delta_movement.first += -speed_cos;
delta_movement.second += -speed_sin;
delta_movement.first -= speed_cos;
delta_movement.second -= speed_sin;
}
if (keystate[SDL_SCANCODE_D]) {
delta_movement.first += -speed_sin;
delta_movement.first -= speed_sin;
delta_movement.second += speed_cos;
}
if (!CollisionManager::GetInstance().CheckWallCollision(
Expand All @@ -76,9 +77,8 @@ void Player::Rotate(double delta_time) {
if (SDL_ShowCursor(SDL_QUERY) == SDL_DISABLE) {
int x, y;
SDL_GetMouseState(&x, &y);
position_.theta += (x - 400) * 0.001;
position_.theta = SumRadian(position_.theta, (x - 400) * 0.001);
SDL_WarpMouseInWindow(nullptr, 400, 300);
position_.theta = std::fmod(position_.theta, (M_PI * 2));
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/Core/src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void Game::Init() {
config_.fov, false};
renderer_ = std::make_shared<Renderer>("Wolfenstein", render_config);

Camera2DConfig camera_config = {config_.screen_width / 2, config_.fov,
Camera2DConfig camera_config = {config_.screen_width, config_.fov,
config_.view_distance};
camera_ = std::make_shared<Camera2D>(camera_config);

Expand All @@ -43,11 +43,17 @@ void Game::Init() {

player_->SetCameraPositionUpdator(
std::bind(camera_position_updator, std::placeholders::_1));
std::shared_ptr<StaticObject> candlebra =
std::make_shared<StaticObject>(vector2d(3.5, 1.5), 6, 0.1, 0.1);
const auto candlebra =
std::make_shared<StaticObject>(vector2d(3.5, 1.5), 6, 0.2, 0.2);
const auto candlebra1 =
std::make_shared<StaticObject>(vector2d(12.5, 9.5), 8, 0.2, 0.2);
const auto candlebra2 =
std::make_shared<StaticObject>(vector2d(3.5, 7.5), 6, 0.2, 0.2);

scene_->AddObject(player_);
scene_->AddObject(candlebra);
scene_->AddObject(candlebra1);
scene_->AddObject(candlebra2);

is_running_ = true;
time_manager_->InitClock();
Expand Down
1 change: 0 additions & 1 deletion src/GameObjects/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ link_directories(
add_library(
game_object
STATIC
src/game_object.cpp
src/static_object.cpp
src/dynamic_object.cpp
)
Expand Down
10 changes: 5 additions & 5 deletions src/GameObjects/include/GameObjects/static_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class StaticObject : public IGameObject
{
public:
explicit StaticObject(const vector2d& pose_, const int texture_id_,
const int width_, const int height_);
const double width_, const double height_);
~StaticObject();

void Update(double delta_time) override;
Expand All @@ -34,14 +34,14 @@ class StaticObject : public IGameObject
vector2d GetPose() const override;

int GetTextureId() const;
int GetWidth() const;
int GetHeight() const;
double GetWidth() const;
double GetHeight() const;

protected:
vector2d pose;
int texture_id;
int width;
int height;
double width;
double height;
};

} // namespace wolfenstein
Expand Down
6 changes: 0 additions & 6 deletions src/GameObjects/src/game_object.cpp

This file was deleted.

6 changes: 3 additions & 3 deletions src/GameObjects/src/static_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace wolfenstein {

StaticObject::StaticObject(const vector2d& pose_, const int texture_id_,
const int width_, const int height_)
const double width_, const double height_)
: pose(pose_), texture_id(texture_id_), width(width_), height(height_) {}

StaticObject::~StaticObject() {}
Expand Down Expand Up @@ -33,10 +33,10 @@ vector2d StaticObject::GetPose() const {
int StaticObject::GetTextureId() const {
return texture_id;
}
int StaticObject::GetWidth() const {
double StaticObject::GetWidth() const {
return width;
}
int StaticObject::GetHeight() const {
double StaticObject::GetHeight() const {
return height;
}

Expand Down
18 changes: 15 additions & 3 deletions src/Graphics/include/Graphics/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
#ifndef GRAPHICS_RENDERER_H_
#define GRAPHICS_RENDERER_H_

#include "GameObjects/dynamic_object.h"
#include "GameObjects/game_object.h"
#include "GameObjects/static_object.h"
#include "scene.h"
#include <Camera/camera.h>
#include <SDL2/SDL.h>
#include <memory>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

namespace wolfenstein {
Expand Down Expand Up @@ -74,6 +73,9 @@ class Renderer
void RenderScene(const std::shared_ptr<Scene>& scene_ptr,
const std::shared_ptr<Camera2D>& camera_ptr);

void RenderScene2D(const std::shared_ptr<Scene>& scene_ptr,
const std::shared_ptr<Camera2D>& camera_ptr);

private:
void RenderBackground();
void RenderWalls(const std::shared_ptr<Map>& map_ptr,
Expand All @@ -87,9 +89,19 @@ class Renderer
void RenderObjects(const std::vector<std::shared_ptr<IGameObject>>& objects,
const std::shared_ptr<Camera2D>& camera_ptr,
RenderQueue& render_queue);

int CalculateHorizontalSlice(const Ray& ray,
const std::shared_ptr<Camera2D> camera_ptr);
std::tuple<int, int, int> CalculateVerticalSlice(const double& distance);
void RenderTextures(RenderQueue& render_queue);

// Render 2D
void ClearScreen();
void SetDrawColor(SDL_Color color);
void DrawFilledRectangle(vector2i start, vector2i end);
void RenderMap(const std::shared_ptr<Map> map_ptr);
void RenderObjects(const std::vector<std::shared_ptr<IGameObject>>& objects,
const std::shared_ptr<Camera2D> camera_ptr);
void DrawLine(vector2i start, vector2i end);

SDL_Renderer* renderer_;
SDL_Window* window_;
Expand Down
Loading

0 comments on commit e730763

Please sign in to comment.