Commit 8449e235 authored by Srb František's avatar Srb František
Browse files

First commit

parents

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30717.126
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CppGame", "CppGame\CppGame.vcxproj", "{B7D94891-601F-4EDC-91FD-421B62B6A385}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B7D94891-601F-4EDC-91FD-421B62B6A385}.Debug|x64.ActiveCfg = Debug|x64
{B7D94891-601F-4EDC-91FD-421B62B6A385}.Debug|x64.Build.0 = Debug|x64
{B7D94891-601F-4EDC-91FD-421B62B6A385}.Debug|x86.ActiveCfg = Debug|Win32
{B7D94891-601F-4EDC-91FD-421B62B6A385}.Debug|x86.Build.0 = Debug|Win32
{B7D94891-601F-4EDC-91FD-421B62B6A385}.Release|x64.ActiveCfg = Release|x64
{B7D94891-601F-4EDC-91FD-421B62B6A385}.Release|x64.Build.0 = Release|x64
{B7D94891-601F-4EDC-91FD-421B62B6A385}.Release|x86.ActiveCfg = Release|Win32
{B7D94891-601F-4EDC-91FD-421B62B6A385}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {24323C20-42EE-41A0-9047-2325ADC6C5DC}
EndGlobalSection
EndGlobal
#include "Characters.h"
Enemy::Enemy(float fov, float vision_range, std::pair<float, float> position,
float mov_speed, float rot_speed, int rays,
tuple_color color, std::vector<std::tuple<float, float, int>> path)
: path(path), going_reversed(false), path_index(0), progress(0.0f),
waiting(true), Player(fov, vision_range, position, mov_speed, rot_speed, rays,color)
{
// recalculate point stops to lines (to show them on DEBUG_MODE)
std::vector<Line> line_path;
auto [last_x, last_y, _] = path[0];
for (auto&& [x, y, _] : path) {
if (x != last_x || y != last_y) {
line_path.push_back(Line(last_x, last_y, x, y, color, color));
}
last_x = x;
last_y = y;
}
this->line_path = line_path;
}
void Enemy::update(double delta_time)
{
//TODO
//probably will redo this piece of code
static constexpr float epsilon = 0.01f;
auto [stop_x, stop_y, wait_time] = path[path_index];
auto [x, y] = position;
if (path.size() == 1) {
rotation += delta_time;
return;
}
if (waiting) {
progress += delta_time;
if (progress >= wait_time / 10.0f) {
//start next path
progress = 0;
waiting = false;
if (going_reversed) {
if (path_index == 0) {
going_reversed = false;
++path_index;
}
else {
--path_index;
}
}
else {
if (path_index == path.size()-1) {
going_reversed = true;
--path_index;
}
else {
++path_index;
}
}
auto [next_stop_x, next_stop_y, _] = path[path_index];
rotation = atan2(next_stop_y - y, next_stop_x - x);
}
}
//if walking
else {
x += cos(rotation) * mov_speed * (float)delta_time;
y += sin(rotation) * mov_speed * (float)delta_time;
position = std::make_pair(x, y);
float dx = x - stop_x;
float dy = y - stop_y;
if (dx < epsilon && dy < epsilon && dx > -epsilon && dy > -epsilon) {
position = std::make_pair(stop_x, stop_y); //allign
waiting = true;
progress = 0;
}
}
}
Player::Player(float fov, float vision_range, std::pair<float, float> position, float mov_speed, float rot_speed, int rays,
tuple_color color)
:
rotation(0.0f),
fov(fov),
vision_range(vision_range),
position(position),
mov_speed(mov_speed),
rot_speed(rot_speed),
rays(rays),
color(color)
{}
#pragma once
#include <utility>
#include <vector>
#include "Line.h"
#include <optional>
class Player {
public:
float rotation;
float fov;
float vision_range;
std::pair<float, float> position;
float mov_speed;
float rot_speed;
tuple_color color;
int rays;
Player(float fov, float vision_range, std::pair<float, float> position,
float mov_speed, float rot_speed, int rays, tuple_color color);
};
class Enemy : public Player {
public:
std::vector<std::tuple<float,float,int>> path;
bool going_reversed;
size_t path_index;
float progress;
bool waiting;
std::optional<std::vector<Line>> line_path;
Enemy(float fov, float vision_range, std::pair<float, float> position,
float mov_speed, float rot_speed, int rays, tuple_color color,
std::vector<std::tuple<float, float, int>> path);
void update(double delta_time);
};
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{b7d94891-601f-4edc-91fd-421b62b6a385}</ProjectGuid>
<RootNamespace>CppGame</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<FunctionLevelLinking>true</FunctionLevelLinking>
<BasicRuntimeChecks>Default</BasicRuntimeChecks>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>opengl32.lib;glfw3.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(SolutionDir)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalOptions>/NODEFAULTLIB:msvcrtd.lib %(AdditionalOptions)</AdditionalOptions>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>opengl32.lib;glfw3.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(SolutionDir)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalOptions>/NODEFAULTLIB:msvcrtd.lib %(AdditionalOptions)</AdditionalOptions>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Characters.cpp" />
<ClCompile Include="Display.cpp" />
<ClCompile Include="glad.c" />
<ClCompile Include="Line.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Scene.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Display.h" />
<ClInclude Include="Line.h" />
<ClInclude Include="Characters.h" />
<ClInclude Include="Scene.h" />
</ItemGroup>
<ItemGroup>
<Text Include="map0.txt" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Zdrojové soubory">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Hlavičkové soubory">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Soubory zdrojů">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Zdrojové soubory</Filter>
</ClCompile>
<ClCompile Include="glad.c">
<Filter>Zdrojové soubory</Filter>
</ClCompile>
<ClCompile Include="Line.cpp">
<Filter>Zdrojové soubory</Filter>
</ClCompile>
<ClCompile Include="Display.cpp">
<Filter>Zdrojové soubory</Filter>
</ClCompile>
<ClCompile Include="Scene.cpp">
<Filter>Zdrojové soubory</Filter>
</ClCompile>
<ClCompile Include="Characters.cpp">
<Filter>Zdrojové soubory</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Line.h">
<Filter>Hlavičkové soubory</Filter>
</ClInclude>
<ClInclude Include="Display.h">
<Filter>Hlavičkové soubory</Filter>
</ClInclude>
<ClInclude Include="Scene.h">
<Filter>Hlavičkové soubory</Filter>
</ClInclude>
<ClInclude Include="Characters.h">
<Filter>Hlavičkové soubory</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="map0.txt">
<Filter>Zdrojové soubory</Filter>
</Text>
</ItemGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
\ No newline at end of file
#include "Display.h"
Display::Display(unsigned int VAO,unsigned int shaderProgram)
{
this->VAO = VAO;
this->shaderProgram = shaderProgram;
}
Display::~Display()
{
}
void Display::DrawObject(float* line,unsigned int num_of_vertices) const
{
//Main opengl drawing
glBufferData(GL_ARRAY_BUFFER, num_of_vertices*sizeof(line[0])*5, line, GL_DYNAMIC_DRAW);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_LINES, 0, num_of_vertices);
}
void Display::CleanWindow()
{
//clean whole screen and set every pixel to black
glClearColor(0, 0,0, 1);
glClear(GL_COLOR_BUFFER_BIT);
}
#pragma once
#include <glad/glad.h>
#include <glfw3.h>
#include <tuple>
class Display
{
public:
Display(unsigned int VAO, unsigned int shaderProgram);
~Display();
void DrawObject(float* line, unsigned int num_of_vertices) const;
void CleanWindow();
private:
unsigned int VAO;
unsigned int shaderProgram;
};
#include "Line.h"
Line::Line(float x1, float y1, float x2, float y2) : Line(x1,y1,x2,y2,tuple_color(1,1,1), tuple_color(1,1,1))
{}
//ctor
Line::Line(float x1, float y1, float x2, float y2,tuple_color color1, tuple_color color2)
: x1(x1), x2(x2), y1(y1), y2(y2)
{
auto&& [r1, g1, b1] = color1;
this->r1 = r1;
this->g1 = g1;
this->b1 = b1;
auto&& [r2, g2, b2] = color2;
this->r2 = r2;
this->g2 = g2;
this->b2 = b2;
}
//[0,width|height] -> [-1.0f,1.0f]
Line Line::normalize(int x1, int y1, int x2, int y2, int width, int height,
tuple_color color1, tuple_color color2)
{
return Line(2*x1/(float)width-1, 2 * y1 / (float)height - 1,
2 * x2 / (float)width - 1, 2 * y2/ (float)height - 1,
color1, color2
);
}
Line::~Line()
{
}
//returns shortened ray to the closest wall
Line Line::ray_cast(Line ray,const std::vector<Line>& walls)
{
float x1 = ray.x1;
float x2 = ray.x2;
float y1 = ray.y1;
float y2 = ray.y2;
float min_t = 1;
tuple_color color_of_closest_wall=tuple_color(ray.r2, ray.g2, ray.b2);
for (auto&& wall : walls) {
float x3 = wall.x1;
float y3 = wall.y1;
float x4 = wall.x2;
float y4 = wall.y2;
//taken from wiki article line-line intersection
// (x1,y1) + t[x2-x1,y2-y1]
float t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) /
((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
float u = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) /
((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
if (t >= 0 && t <= 1 && u >= 0 && u <= 1 && t < min_t) {
min_t = t;
color_of_closest_wall = tuple_color(
(1-u) * wall.r1 + u*wall.r2,
(1 - u) * wall.g1 + u * wall.g2,
(1 - u) * wall.b1 + u * wall.b2);
}
}
return(Line(x1,y1,x1+min_t*(x2-x1),y1+min_t*(y2-y1),
tuple_color(ray.r1,ray.g1,ray.b1), color_of_closest_wall));
}
#pragma once
#include <vector>
#include <string>
#include <fstream>
#include <tuple>
using tuple_color = std::tuple<float, float, float>;
class Line
{
public:
float x1,y1,r1,g1,b1,x2,y2,r2,g2,b2;
Line(float x1, float y1, float x2, float y2);
Line(float x1,float y1,float x2, float y2,tuple_color color1, tuple_color color2);
static Line normalize(int x1, int y1, int x2, int y2,int width,int height,tuple_color color1, tuple_color color2);
float length() {
float dx = x1 - x2;
float dy = y1 - y2;
return sqrt(dx * dx + dy * dy);
}
~Line();
static Line ray_cast(Line ray, const std::vector<Line>& walls);
};
#include "Scene.h"
#include <iostream>
#include <math.h>
static constexpr std::tuple<float, float, float> white = std::make_tuple(1.0f, 1.0f, 1.0f);
//calculate distance between 2 points
float dist(pos a, pos b) {
auto [x1, y1] = a;
auto [x2, y2] = b;
float dx = x1 - x2;
float dy = y1 - y2;
return sqrt(dx * dx + dy * dy);
}
// parse string to 2 ints
std::pair<int, int> parse_coords(std::string&& str, char sym)
{
size_t pos = 0;
std::vector<std::string> tokens;
while ((pos = str.find(sym)) != std::string::npos) {
tokens.push_back(str.substr(0, pos));
str.erase(0, pos + 1);
}
if (str != "") tokens.push_back(str);
return std::make_pair(std::stoi(tokens[0]), std::stoi(tokens[1]));
}
// parse string to 3 ints and divide them to float by 255.0f
tuple_color parse_color(std::string&& str, char sym)
{
size_t pos = 0;
std::vector<std::string> tokens;
while ((pos = str.find(sym)) != std::string::npos) {
tokens.push_back(str.substr(0, pos));
str.erase(0, pos + 1);
}
if (str != "") tokens.push_back(str);
return tuple_color(
std::stoi(tokens[0]) / 255.0f,
std::stoi(tokens[1]) / 255.0f,
std::stoi(tokens[2]) / 255.0f
);
}
void Scene::load_scene_from_file(std::string file)
{
walls.clear();
enemies.clear();
std::fstream MapFile(file);
std::string line;
// map resolution
getline(MapFile, line);
auto [width, height] = parse_coords(std::move(line), ' ');
// player start position
getline(MapFile, line);
auto [start_x, start_y] = parse_coords(std::move(line), ' ');
// goal position
getline(MapFile, line);
auto [goal_x, goal_y] = parse_coords(std::move(line), ' ');
tuple_color color_of_object;
int last_x = -1, last_y = -1;
//load enemies
std::vector<std::tuple<float, float, int>> path;
while (getline(MapFile, line)) {
if (line[0] == '-') {
// starting reading Objects
if (path.size() != 0) {
auto&& [x, y, _] = path[0];
enemies.push_back(Enemy(
PI * 2 / 3.0f, 0.25f, pos(x, y), 0.3f, 1.5f, 120, color_of_object, path
));