From d8a198bff96255d8fef118fba3abde12b08753ee Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Sat, 22 Aug 2015 06:50:40 -0700 Subject: [PATCH] That was weird... --- aStar.sln | 22 +++++ aStar/App.cpp | 168 ++++++++++++++++++++++++++++++++++ aStar/App.h | 2 +- aStar/Explorer.cpp | 11 +-- aStar/Explorer.h | 4 +- aStar/Map.cpp | 56 ++++++++++++ aStar/Map.h | 24 +++++ aStar/Pather.cpp | 18 ++-- aStar/Pather.h | 9 +- aStar/Tile.cpp | 37 ++++++++ aStar/Tile.h | 22 +++++ aStar/aStar.vcxproj | 176 ++++++++++++++++++++++++++++++++++++ aStar/aStar.vcxproj.filters | 54 +++++++++++ aStar/background.png | Bin 0 -> 6343 bytes aStar/main.cpp | 1 - 15 files changed, 577 insertions(+), 27 deletions(-) create mode 100644 aStar.sln create mode 100644 aStar/App.cpp create mode 100644 aStar/Map.cpp create mode 100644 aStar/Map.h create mode 100644 aStar/Tile.cpp create mode 100644 aStar/Tile.h create mode 100644 aStar/aStar.vcxproj create mode 100644 aStar/aStar.vcxproj.filters create mode 100644 aStar/background.png diff --git a/aStar.sln b/aStar.sln new file mode 100644 index 0000000..c525e8c --- /dev/null +++ b/aStar.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.23107.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "aStar", "aStar\aStar.vcxproj", "{9035B83C-F117-480E-9DEB-435AA0EBEA3F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9035B83C-F117-480E-9DEB-435AA0EBEA3F}.Debug|x64.ActiveCfg = Debug|x64 + {9035B83C-F117-480E-9DEB-435AA0EBEA3F}.Debug|x64.Build.0 = Debug|x64 + {9035B83C-F117-480E-9DEB-435AA0EBEA3F}.Release|x64.ActiveCfg = Release|x64 + {9035B83C-F117-480E-9DEB-435AA0EBEA3F}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/aStar/App.cpp b/aStar/App.cpp new file mode 100644 index 0000000..8e21ca9 --- /dev/null +++ b/aStar/App.cpp @@ -0,0 +1,168 @@ +#include "App.h" +#include +#include +#include "Map.h" + + +// ========== Constructors ============= +App::App() { + window = new sf::RenderWindow(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "SFML works!"); +} +App::~App() { } + +// ========== Mutes ============= +void App::Init() { + + // Set up the background texture + background_texture = new sf::Texture(); + background_texture->loadFromFile("background.png"); + + backgroundSprite.setTexture(*background_texture); + + _pixelArray = new sf::Uint8[WINDOW_WIDTH * WINDOW_HEIGHT * 4]; + pixel_array_texture.create(WINDOW_WIDTH, WINDOW_HEIGHT); + + explorer = new Explorer(&map); +} + +void App::Input() { + while (window->pollEvent(event)) { + if (event.type == sf::Event::Closed) + window->close(); + if (event.type == sf::Event::KeyPressed) { + if (event.key.code == sf::Keyboard::Space) { + + } + } + } +} + +void App::Update(double step_size) { + Input(); +} + +void App::Render() { + + + // HOUSEKEEPING + // Get the physics fps for the last render cycle + physics_fps = physics_frame_count * render_fps; + + // Frame time in seconds + frame_time = delta_time * 1000; + + // And the render fps + render_fps = 1000 / frame_time; + + + + // RENDERING + + window->clear(sf::Color::Blue); + window->draw(backgroundSprite); + + sf::Vector2i pos; + for (int i = 0; i < WINDOW_WIDTH * WINDOW_HEIGHT * 4; i++) { + _pixelArray[i] = 0; + } + + // Draw the tiles + for (int x = 0; x < Map::CELLS_WIDTH; x++) { + for (int y = 0; y < Map::CELLS_HEIGHT; y++) { + + pos.x = x; + pos.y = y; + sf::Color thing = map.getTile(pos)->getColor(); + + + for (int x2 = 1; x2 < 5; x2++) { + for (int y2 = 1; y2 < 5; y2++) { + + int pixel_x = (x * 5) + x2; + int pixel_y = (y * 5) + y2; + + _pixelArray[(pixel_y * WINDOW_WIDTH + pixel_x) * 4] = thing.r; // Red + _pixelArray[(pixel_y * WINDOW_WIDTH + pixel_x) * 4 + 1] = thing.g; // Green + _pixelArray[(pixel_y * WINDOW_WIDTH + pixel_x) * 4 + 2] = thing.b; // Blue + _pixelArray[(pixel_y * WINDOW_WIDTH + pixel_x) * 4 + 3] = thing.a; // Alpha + + } + } + } + } + + // Draw the player + + for (int x2 = 1; x2 < 5; x2++) { + for (int y2 = 1; y2 < 5; y2++) { + + int pixel_x = (explorer->getPosition().x * 5) + x2; + int pixel_y = (explorer->getPosition().y * 5) + y2; + + sf::Color color = explorer->getColor(); + + _pixelArray[(pixel_y * WINDOW_WIDTH + pixel_x) * 4] = color.r; // Red + _pixelArray[(pixel_y * WINDOW_WIDTH + pixel_x) * 4 + 1] = color.g; // Green + _pixelArray[(pixel_y * WINDOW_WIDTH + pixel_x) * 4 + 2] = color.b; // Blue + _pixelArray[(pixel_y * WINDOW_WIDTH + pixel_x) * 4 + 3] = color.a; // Alpha + + } + } + + pixel_array_texture.update(_pixelArray); + + pixel_array_sprite.setTexture(pixel_array_texture); + window->draw(pixel_array_sprite); + + window->display(); +} + + +void App::Run() { + Init(); + + while (window->isOpen()) { + // Time since app start + elapsed_time = time(); + + // Time between last frame start and this frame + // 2 seconds = 30 seconds - 28 seconds + delta_time = elapsed_time - current_time; + current_time = elapsed_time; + + // Make sure we aren't taking too big of steps when lagging + if (delta_time > 0.02f) + delta_time = 0.02f; + + // Add the delta time to the leftover time from the last frame + accumulator_time += delta_time; + + // While there is time left + while ((accumulator_time - step_size) >= step_size) { + // Take away the time we will be simulating + accumulator_time -= step_size; + + // Update the game for the timestep + Update(step_size); + + physics_frame_count++; + } + + Render(); + } +} + +float App::time() { + static __int64 start = 0; + static __int64 frequency = 0; + + if (start == 0) { + QueryPerformanceCounter((LARGE_INTEGER*)&start); + QueryPerformanceFrequency((LARGE_INTEGER*)&frequency); + return 0.0f; + } + + __int64 counter = 0; + QueryPerformanceCounter((LARGE_INTEGER*)&counter); + return (float)((counter - start) / double(frequency)); +} diff --git a/aStar/App.h b/aStar/App.h index a07be95..9c43953 100644 --- a/aStar/App.h +++ b/aStar/App.h @@ -22,7 +22,7 @@ private: sf::Uint8* _pixelArray; sf::Sprite pixel_array_sprite; sf::Texture pixel_array_texture; - + Explorer* explorer; // Art assets diff --git a/aStar/Explorer.cpp b/aStar/Explorer.cpp index 0e3bb6b..d26cd51 100644 --- a/aStar/Explorer.cpp +++ b/aStar/Explorer.cpp @@ -1,11 +1,10 @@ #include "Explorer.h" #include -Explorer::Explorer(Map* map_){ +Explorer::Explorer(Map* map_) { color = sf::Color::Blue; position = sf::Vector2i(10, 10); map = map_; - pather = new Pather(map_); } @@ -25,7 +24,7 @@ bool Explorer::move() { // While there are moves for us to take if (!movement_stack.empty()) { bool valid = false; // If the next move is valid, collision - int x = movement_stack.front(); + int x = movement_stack.top(); switch (x) { @@ -60,13 +59,13 @@ bool Explorer::move() { std::cout << "Path blocked" << std::endl; // Flush the moves list while(!movement_stack.empty()) { - movement_stack.pop_front(); + movement_stack.pop(); } return false; } // If everything went well, pop and return true - movement_stack.pop_front(); + movement_stack.pop(); return true; } else @@ -75,6 +74,6 @@ bool Explorer::move() { // A* bool Explorer::plan(sf::Vector2i destination_) { - movement_stack = pather->pathTo(position, destination_); + return true; } diff --git a/aStar/Explorer.h b/aStar/Explorer.h index f865e5d..3af0a1b 100644 --- a/aStar/Explorer.h +++ b/aStar/Explorer.h @@ -2,7 +2,6 @@ #include #include #include "Map.h" -#include "Pather.h" class Explorer { public: @@ -14,9 +13,8 @@ private: sf::Color color; sf::Vector2i position; Map* map; - Pather* pather; - std::deque movement_stack; + std::stack movement_stack; bool move(); bool plan(sf::Vector2i destination_); }; diff --git a/aStar/Map.cpp b/aStar/Map.cpp new file mode 100644 index 0000000..236535c --- /dev/null +++ b/aStar/Map.cpp @@ -0,0 +1,56 @@ +#include "Map.h" +#include + + +Map::Map() { + Init(); +} + + +Map::~Map() { +} + + + +Tile* Map::getTile(sf::Vector2i position_) { + if (position_.x > CELLS_WIDTH || position_.x < 0 + || position_.y > CELLS_HEIGHT || position_.y < 0) { + return nullptr; + } + + return tileArray[position_.x][position_.y]; +} + +bool Map::isTileSolid(sf::Vector2i position_) { + if (position_.x > CELLS_WIDTH || position_.x < 0 + || position_.y > CELLS_HEIGHT || position_.y < 0) { + return true; + } + else + return tileArray[position_.x][position_.y]->isSolid(); +} +Tile* Map::getTile(int x_, int y_) { + return tileArray[x_][y_]; +} + +void Map::setTile(sf::Vector2i position, Tile* data) { + delete tileArray[position.x][position.y]; + tileArray[position.x][position.y] = data; +} + +void Map::Init() { + int q; + + for (int x = 0; x < CELLS_WIDTH; x++) { + for (int y = 0; y < CELLS_HEIGHT; y++) { + q = rand() % 100; + if (q > 70) { + tileArray[x][y] = new Tile(true, 100.0, sf::Color::Cyan); + } + else { + tileArray[x][y] = new Tile(false, 0.0, sf::Color::Red); + } + + } + } +} diff --git a/aStar/Map.h b/aStar/Map.h new file mode 100644 index 0000000..4364cfc --- /dev/null +++ b/aStar/Map.h @@ -0,0 +1,24 @@ +#pragma once +#include "Tile.h" +#include + +class Map { +public: + static const int CELLS_HEIGHT = 153; + static const int CELLS_WIDTH = 319; + + Map(); + ~Map(); + + + Tile* getTile(sf::Vector2i position); + Tile* getTile(int x_, int y_); + bool isTileSolid(sf::Vector2i); + void setTile(sf::Vector2i position, Tile* data); + +private: + void Init(); + + Tile* tileArray[319][153]; +}; + diff --git a/aStar/Pather.cpp b/aStar/Pather.cpp index e0854e3..dab7527 100644 --- a/aStar/Pather.cpp +++ b/aStar/Pather.cpp @@ -100,10 +100,6 @@ Pather::Pather(Map* map_) { map = map_; } -Pather::Pather() { - -} - Pather::~Pather() { } @@ -111,7 +107,7 @@ sf::Vector2i Pather::getEndNodePosition() { return end_node->xy; } -std::deque Pather::pathTo(sf::Vector2i start, sf::Vector2i end) { +std::vector Pather::pathTo(sf::Vector2i start, sf::Vector2i end) { // Clear the visited map for erroneous data for (int i = 0; i < Map::CELLS_WIDTH; i++) { @@ -133,13 +129,13 @@ std::deque Pather::pathTo(sf::Vector2i start, sf::Vector2i end) { openList.emplace(start_node, 0); early_exit = false; - path_list = loop(); + //path_list = Loop(); return path_list; } -std::deque Pather::loop() { +std::vector Pather::loop() { while (!openList.empty() && !early_exit) { // Early exit jankyness, need to change this if (closedList.size() > 3000) { @@ -176,17 +172,17 @@ std::deque Pather::loop() { } } - std::deque return_path = returnPath(); + std::vector return_path = returnPath(); if (no_path || return_path.empty()) { + return std::vector(0, 0); std::cout << " no return path " << std::endl; - return return_path; } return return_path; } -std::deque Pather::returnPath() { - std::deque path; +std::vector Pather::returnPath() { + std::vector path; while (active_node != nullptr) { path.push_back(active_node->cameFrom); diff --git a/aStar/Pather.h b/aStar/Pather.h index af49b39..37913dc 100644 --- a/aStar/Pather.h +++ b/aStar/Pather.h @@ -29,7 +29,6 @@ private: class Pather { public: Pather(Map* map_); - Pather(); ~Pather(); Map* map; @@ -38,9 +37,9 @@ public: std::unordered_map closedList; int visitedMap[App::WINDOW_HEIGHT][App::WINDOW_WIDTH]; - std::deque pathTo(sf::Vector2i start, sf::Vector2i end); - std::deque loop(); - std::deque returnPath(); + std::vector pathTo(sf::Vector2i start, sf::Vector2i end); + std::vector loop(); + std::vector returnPath(); sf::Vector2i getEndNodePosition(); @@ -52,7 +51,7 @@ public: private: - std::deque path_list; + std::vector path_list; node* end_node; }; diff --git a/aStar/Tile.cpp b/aStar/Tile.cpp new file mode 100644 index 0000000..14813e9 --- /dev/null +++ b/aStar/Tile.cpp @@ -0,0 +1,37 @@ +#include "Tile.h" + + + +Tile::Tile(bool solid_, double movement_penalty_, sf::Color color_) { + solid = solid_; + movement_penalty = movement_penalty_; + color = color_; +} + + +Tile::Tile() { + solid = false; + movement_penalty = 0.0; + color = sf::Color::Red; +} + +Tile::~Tile() { +} + +bool Tile::isSolid() { + return solid; +} + +double Tile::getPenalty() { + return movement_penalty; +} + +sf::Color Tile::getColor() { + return color; +} + +void Tile::Rewrite(bool solid_, double movement_penalty_, sf::Color color_) { + solid = solid_; + movement_penalty = movement_penalty_; + color = color_; +} diff --git a/aStar/Tile.h b/aStar/Tile.h new file mode 100644 index 0000000..7b07e76 --- /dev/null +++ b/aStar/Tile.h @@ -0,0 +1,22 @@ +#pragma once +#include + +class Tile { +public: + Tile(bool solid_, double movement_penalty_, sf::Color color_); + Tile(); + ~Tile(); + + bool isSolid(); + double getPenalty(); + sf::Color getColor(); + void Rewrite(bool solid_, double movement_penalty_, sf::Color color_); + +private: + + + bool solid = false; + double movement_penalty = 0.0; + sf::Color color; +}; + diff --git a/aStar/aStar.vcxproj b/aStar/aStar.vcxproj new file mode 100644 index 0000000..779db3f --- /dev/null +++ b/aStar/aStar.vcxproj @@ -0,0 +1,176 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {9035B83C-F117-480E-9DEB-435AA0EBEA3F} + Win32Proj + aStar + 8.1 + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\include;$(IncludePath) + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\lib;$(LibraryPath) + + + false + + + false + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\include;$(IncludePath) + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\lib;$(LibraryPath) + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\include;%(AdditionalIncludeDirectories) + + + Console + true + sfml-graphics-d.lib;sfml-audio-d.lib;sfml-network-d.lib;sfml-system-d.lib;sfml-window-d.lib;%(AdditionalDependencies) + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\lib;%(AdditionalLibraryDirectories) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + sfml-graphics.lib;sfml-audio.lib;sfml-network.lib;sfml-system.lib;sfml-window.lib;%(AdditionalDependencies) + Z:\Cpp_Libs\SFML-Visual_Studio2015RCx64\lib;%(AdditionalLibraryDirectories) + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/aStar/aStar.vcxproj.filters b/aStar/aStar.vcxproj.filters new file mode 100644 index 0000000..0e28dba --- /dev/null +++ b/aStar/aStar.vcxproj.filters @@ -0,0 +1,54 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/aStar/background.png b/aStar/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f34dad70bd3d1c39b3fe762db7067420f6d50870 GIT binary patch literal 6343 zcmeAS@N?(olHy`uVBq!ia0y~yV6$OhVEV_w1QeMzMe75QVoUONcVYMsf(!O8p9~b? zEbxddW?8rBfipKXdsqYmjVK zPZ)@LBC!@kfq_{64VW@7%~ZX)>>&3}NZtaX6pm-GLR2v@Y^XS*hfrAj(nbI%(;%~0 z6r{k?O&g}J;iz&M++wep-p?CBGEf^&7)CL|6za%r^oA=u6=TKE0+fN;=+Ji&6p&z$ z@UbHn;gx5XDiuJ&P#cRN9s(=uc7KELv(fZ(%pj|vHbOlFRu^HDG##OEXNepKP#9_> z%tIiB2ObITh6W(e{xeHIJAg!?HjW12Xb_I3LQsZa7|qC|Wg$2rfQZrRWVAYgCx+1) zd9+ajOAMp!!qKKRxUT?85Ti}&(cZ~u2M^kV8to&G4l}?z)1$)-kfA4Vvte|kW^}M? sbi5Ec_&GY{Iy!0%9|xzxsP#1c?(I@r7R0W)4JvCrUHx3vIVCg!05pnX9RL6T literal 0 HcmV?d00001 diff --git a/aStar/main.cpp b/aStar/main.cpp index eaa9c5f..c30d317 100644 --- a/aStar/main.cpp +++ b/aStar/main.cpp @@ -1,7 +1,6 @@ #include #include #include "App.h" -#include "Pather.h" int main() {