From 1591f70a8d98a8da3ad722f69e3e76e996fc59ae Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Sun, 3 Nov 2019 21:21:39 +0100 Subject: [PATCH] Cannot go through walls anymore Planar movement inside the maze Fixed orientation --- CMakeLists.txt | 4 +- resources/Map.tga | Bin 196652 -> 3116 bytes src/MovementManager.cpp | 17 ++++++++ src/MovementManager.h | 15 +++++++ src/TileTypes.h | 15 +++++++ src/main.cpp | 92 ++++++++++++++++++---------------------- 6 files changed, 92 insertions(+), 51 deletions(-) create mode 100644 src/MovementManager.cpp create mode 100644 src/MovementManager.h create mode 100644 src/TileTypes.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 818e15c..a367e2b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,9 @@ find_package(GLUT REQUIRED) add_executable(game - src/main.cpp) + src/main.cpp + src/MovementManager.h + src/MovementManager.cpp src/TileTypes.h) target_include_directories(game PRIVATE ${OPENGL_INCLUDE_DIR} diff --git a/resources/Map.tga b/resources/Map.tga index a37f997da321c6bc1e86cbaf3107e23f2ae4e1b1..3dbea173d5c47ed89ff5887035d7b72d0a1cd757 100644 GIT binary patch literal 3116 zcmcJLTMoh?5Jicr@Ut7xUDTwB(ZpBW|3(ypnM@eCbD@QP6quRQa~r}gq&0=#v!DLQ zk+09Nm8>IvguY-$=tp7A-_}2oGyYa7#N6`Nr_km_{+bk-d5zzf!kPnqYl@;d=JP3P z<}RN}0n8abPJzuyel3M-&hu>w#=MEIQ;_DZ{DkGrJNWXW_^jd literal 196652 zcmeH`ONt-`6a-sO;nEA}Ek+m+1%G(|i;OOGQ>!4r1YbqZVkpR$)e)8b@$vP?KR;i6 z{Qd8nZ~uMl{|sax0~yFb1~QO=3}he!8OT5eGLV4`WFP|>$Up`%kbw+jAOji5Kn609 zfed6I0~yG`|H;6Yell>;0Q@7oh#pf0zO?^F@MZ(;|IJ@N_V(Wf-fW=#zxnIO-u~Ob zn+>%8H-G)u+kYE)vw`;i=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Tr~jaC-7nOJ|$Up`%kbw+jAOji5Kn609fed6I0~yFb u1~QO=3}he!8OT5eGLV4`WMFRKw_kq$^vCC4KmYvGcYpry`NvP+fBXdvBk!XC diff --git a/src/MovementManager.cpp b/src/MovementManager.cpp new file mode 100644 index 0000000..752cb42 --- /dev/null +++ b/src/MovementManager.cpp @@ -0,0 +1,17 @@ +// +// Created by trotFunky on 03/11/2019. +// + +#include "MovementManager.h" + +Vec3i current_map_colour(const Vec3d& world_pos, const Texture& texture, const int& pixel_scale) +{ + Vec2i map_pos{static_cast(world_pos.x/pixel_scale), + static_cast(world_pos.z/pixel_scale)}; + + unsigned int data_index = (map_pos.x * texture.height + map_pos.y)*(texture.color_bits/8); + + return Vec3i{texture.image_data[data_index], + texture.image_data[data_index+1], + texture.image_data[data_index+2]}; +} diff --git a/src/MovementManager.h b/src/MovementManager.h new file mode 100644 index 0000000..2735c12 --- /dev/null +++ b/src/MovementManager.h @@ -0,0 +1,15 @@ +// +// Created by trotFunky on 03/11/2019. +// + +#ifndef LABYRINTHE_MOVEMENTMANAGER_H +#define LABYRINTHE_MOVEMENTMANAGER_H + +#include +#include + +/// Returns the colour of the pixel corresponding to the current world position, assuming the map starts at (0,0). +Vec3i current_map_colour(const Vec3d& world_pos, const Texture& texture, const int& pixel_scale); + + +#endif //LABYRINTHE_MOVEMENTMANAGER_H diff --git a/src/TileTypes.h b/src/TileTypes.h new file mode 100644 index 0000000..4aef4f4 --- /dev/null +++ b/src/TileTypes.h @@ -0,0 +1,15 @@ +// +// Created by trotFunky on 03/11/2019. +// + +#ifndef LABYRINTHE_TILETYPES_H +#define LABYRINTHE_TILETYPES_H + +#include "Math/Vectors.h" + +static const Vec3i path{255,255,255}; +static const Vec3i wall{0,0,0}; +static const Vec3i end{0,255,0}; +static const Vec3i objective{0,0,255}; + +#endif //LABYRINTHE_TILETYPES_H diff --git a/src/main.cpp b/src/main.cpp index 8545d02..2bda63a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,8 +4,14 @@ #include "Engine.h" #include "DataHandling/Texture.h" +#include "MovementManager.h" +#include "TileTypes.h" static Texture map; +static constexpr float horizontal_speed = 0.025; +static constexpr int units_per_pixel = 2; +static constexpr int map_side = 32; +static constexpr int corner_pos = (map_side*units_per_pixel); void manage_inputs() { @@ -15,31 +21,42 @@ void manage_inputs() exit(EXIT_SUCCESS); } + Vec3d horizontal_movement = OGLE::camera.get_gaze(); + horizontal_movement.y = 0; + horizontal_movement.normalize(); + + Vec3d previous_camera_position = OGLE::camera.get_eyepos(); + + // TODO : camera_translate function in MovementManager that handles both the correct translation and rollback. if (InputStatus::is_special_key_pressed(GLUT_KEY_RIGHT)) { - OGLE::camera.local_translate({0, 0, 0.1}); + OGLE::camera.local_translate({0, 0, horizontal_speed}); } if (InputStatus::is_special_key_pressed(GLUT_KEY_LEFT)) { - OGLE::camera.local_translate({0, 0, -0.1}); - } - if (InputStatus::is_key_pressed(' ')) - { - OGLE::camera.local_translate({0, 0.1, 0}); - } - if (InputStatus::is_special_key_pressed(GLUT_KEY_PAGE_DOWN)) - { - OGLE::camera.local_translate({0, -0.1, 0}); + OGLE::camera.local_translate({0, 0, -horizontal_speed}); } if (InputStatus::is_special_key_pressed(GLUT_KEY_UP)) { - OGLE::camera.local_translate({0.1, 0, 0}); + OGLE::camera.translate(horizontal_movement * horizontal_speed); } - if (InputStatus::is_special_key_pressed(GLUT_KEY_DOWN)) { - OGLE::camera.local_translate({-0.1, 0, 0}); + OGLE::camera.translate(horizontal_movement * -horizontal_speed); + } + + Vec3i map_colour = current_map_colour(OGLE::camera.get_eyepos(), map, units_per_pixel); + if (map_colour == wall) + { + // If we are going into a wall, revert + OGLE::camera.set_position(previous_camera_position); + } + else if (map_colour == end) + { + std::cout << "Success !" << std::endl; + glutDestroyWindow(glutGetWindow()); + exit(EXIT_SUCCESS); } Vec2i mouse_delta = InputStatus::get_mouse_delta(true); @@ -55,56 +72,29 @@ void display() { manage_inputs(); - glDisable(GL_TEXTURE_2D); - - glBegin(GL_QUADS); - - glColor3f(0.5,0,0); - glVertex3i(-1,-10,-1); - glVertex3i(-1,10,-1); - glVertex3i(1,10,-1); - glVertex3i(1,-10,-1); - - glColor3f(0.5,0,0); - glVertex3i(-1,-10,-1); - glVertex3i(-1,10,-1); - glVertex3i(-1,10,1); - glVertex3i(-1,-10,1); - - glColor3f(0.5,0,0); - glVertex3i(1,-10,1); - glVertex3i(1,10,1); - glVertex3i(1,10,-1); - glVertex3i(1,-10,-1); - - glColor3f(0.5,0,0); - glVertex3i(1,-10,1); - glVertex3i(1,10,1); - glVertex3i(-1,10,1); - glVertex3i(-1,-10,1); - - glEnd(); - - glEnable(GL_TEXTURE_2D); - glEnable(GL_ALPHA_TEST); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glBindTexture(GL_TEXTURE_2D,map.opengl_id[0]); + + // Deactivate interpolation + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glColor4f(1.0f, 1.0f, 1.0f,1.00); glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex3i(0,0,0); - glTexCoord2d(0,1); - glVertex3i(0,0,256); + glTexCoord2d(1,0); + glVertex3i(0,0,corner_pos); glTexCoord2d(1,1); - glVertex3i(256,0,256); + glVertex3i(corner_pos,0,corner_pos); - glTexCoord2d(1,0); - glVertex3i(256,0,0); + glTexCoord2d(0,1); + glVertex3i(corner_pos,0,0); glEnd(); glDisable(GL_ALPHA_TEST); @@ -117,6 +107,8 @@ int main(int argc, char** argv) map.load_rgb_tga("resources/map.tga"); + OGLE::camera.set_position({corner_pos/2,1,corner_pos/2}); + glutMainLoop(); return 0; } \ No newline at end of file