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 a37f997..3dbea17 100644 Binary files a/resources/Map.tga and b/resources/Map.tga differ 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