Add project files.
This commit is contained in:
parent
18b81590d3
commit
045f3a8439
38
CMakeLists.txt
Normal file
38
CMakeLists.txt
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
# CMakeList.txt : Top-level CMake project file, do global configuration
|
||||||
|
# and include sub-projects here.
|
||||||
|
#
|
||||||
|
cmake_minimum_required (VERSION 3.8)
|
||||||
|
|
||||||
|
project ("MapViewer")
|
||||||
|
|
||||||
|
find_package(SDL2 CONFIG REQUIRED)
|
||||||
|
|
||||||
|
# Include sub-projects.
|
||||||
|
add_subdirectory ("lib/osmparser")
|
||||||
|
|
||||||
|
file(GLOB_RECURSE SOURCE_FILES
|
||||||
|
"src/*.cpp"
|
||||||
|
)
|
||||||
|
|
||||||
|
file(GLOB_RECURSE INCLUDE_FILES
|
||||||
|
"include/*.hpp"
|
||||||
|
)
|
||||||
|
|
||||||
|
add_executable(mapviewer
|
||||||
|
${INCLUDE_FILES} ${SOURCE_FILES}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(mapviewer PRIVATE
|
||||||
|
osmp
|
||||||
|
SDL2::SDL2
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(mapviewer PRIVATE
|
||||||
|
osmp
|
||||||
|
SDL2::SDL2 SDL2::SDL2main
|
||||||
|
)
|
||||||
|
|
||||||
|
add_custom_command(TARGET mapviewer POST_BUILD
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/res/map.osm $<TARGET_FILE_DIR:mapviewer>
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/res/bigmap.osm $<TARGET_FILE_DIR:mapviewer>
|
||||||
|
)
|
16
CMakeSettings.json
Normal file
16
CMakeSettings.json
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "x64-Debug",
|
||||||
|
"generator": "Ninja",
|
||||||
|
"configurationType": "Debug",
|
||||||
|
"inheritEnvironments": [ "msvc_x64_x64" ],
|
||||||
|
"buildRoot": "${projectDir}\\out\\build\\${name}",
|
||||||
|
"installRoot": "${projectDir}\\out\\install\\${name}",
|
||||||
|
"cmakeCommandArgs": "",
|
||||||
|
"buildCommandArgs": "",
|
||||||
|
"ctestCommandArgs": "",
|
||||||
|
"cmakeToolchain": "C:/Users/Robert/source/repos/vcpkg/scripts/buildsystems/vcpkg.cmake"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
28
lib/osmparser/CMakeLists.txt
Normal file
28
lib/osmparser/CMakeLists.txt
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
|
project(osmp)
|
||||||
|
|
||||||
|
find_package(tinyxml2 CONFIG REQUIRED)
|
||||||
|
|
||||||
|
file(GLOB_RECURSE CPP_FILES
|
||||||
|
"src/*.cpp"
|
||||||
|
)
|
||||||
|
|
||||||
|
file(GLOB_RECURSE HPP_FILES
|
||||||
|
"include/*.hpp"
|
||||||
|
)
|
||||||
|
|
||||||
|
get_target_property(TINYXML2_INCLUDE_DIR tinyxml2::tinyxml2 INTERFACE_INCLUDE_DIRECTORIES)
|
||||||
|
|
||||||
|
add_library(osmp STATIC
|
||||||
|
${CPP_FILES} ${HPP_FILES}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(osmp PUBLIC
|
||||||
|
include
|
||||||
|
${TINYXML2_INCLUDE_DIR}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(osmp PRIVATE
|
||||||
|
tinyxml2::tinyxml2
|
||||||
|
)
|
34
lib/osmparser/include/osmnode.hpp
Normal file
34
lib/osmparser/include/osmnode.hpp
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "util.hpp"
|
||||||
|
|
||||||
|
namespace osmp
|
||||||
|
{
|
||||||
|
class Object;
|
||||||
|
|
||||||
|
class Node
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Node(const tinyxml2::XMLElement* xml, Object* parent);
|
||||||
|
|
||||||
|
const std::vector<Tag>& GetTags() const;
|
||||||
|
size_t GetTagsSize() const;
|
||||||
|
const Tag& GetTag(size_t index) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Object* parent;
|
||||||
|
|
||||||
|
std::vector<Tag> tags;
|
||||||
|
|
||||||
|
public:
|
||||||
|
unsigned int id;
|
||||||
|
float lat, lon;
|
||||||
|
std::string user;
|
||||||
|
unsigned int uid;
|
||||||
|
bool visible;
|
||||||
|
std::string version;
|
||||||
|
unsigned int changeset;
|
||||||
|
std::string timestamp;
|
||||||
|
};
|
||||||
|
}
|
37
lib/osmparser/include/osmobject.hpp
Normal file
37
lib/osmparser/include/osmobject.hpp
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <util.hpp>
|
||||||
|
|
||||||
|
namespace osmp
|
||||||
|
{
|
||||||
|
class Node;
|
||||||
|
class Way;
|
||||||
|
|
||||||
|
class Object
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Object(const std::string& file);
|
||||||
|
~Object();
|
||||||
|
|
||||||
|
std::vector<Node*> GetNodes() const;
|
||||||
|
size_t GetNodesSize() const;
|
||||||
|
const Node* GetNode(unsigned int id) const;
|
||||||
|
|
||||||
|
std::vector<Way*> GetWays() const;
|
||||||
|
size_t GetWaysSize() const;
|
||||||
|
const Way* GetWay(unsigned int id) const;
|
||||||
|
|
||||||
|
public:
|
||||||
|
const std::string version;
|
||||||
|
const std::string generator;
|
||||||
|
|
||||||
|
Bounds bounds;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::map<unsigned int, Node*> nodes;
|
||||||
|
std::map<unsigned int, Way*> ways;
|
||||||
|
};
|
||||||
|
}
|
5
lib/osmparser/include/osmp.hpp
Normal file
5
lib/osmparser/include/osmp.hpp
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <osmobject.hpp>
|
||||||
|
#include <osmnode.hpp>
|
||||||
|
#include <osmway.hpp>
|
39
lib/osmparser/include/osmway.hpp
Normal file
39
lib/osmparser/include/osmway.hpp
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <util.hpp>
|
||||||
|
|
||||||
|
namespace osmp
|
||||||
|
{
|
||||||
|
class Object;
|
||||||
|
class Node;
|
||||||
|
|
||||||
|
class Way
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Way(const tinyxml2::XMLElement* way_elem, Object* parent);
|
||||||
|
|
||||||
|
const std::vector<Tag>& GetTags() const;
|
||||||
|
size_t GetTagsSize() const;
|
||||||
|
const Tag& GetTag(size_t index) const;
|
||||||
|
|
||||||
|
const std::vector<const Node*>& GetNodes() const;
|
||||||
|
size_t GetNodesSize() const;
|
||||||
|
const Node& GetNode(size_t index) const;
|
||||||
|
|
||||||
|
public:
|
||||||
|
unsigned int id;
|
||||||
|
std::string user;
|
||||||
|
unsigned int uid;
|
||||||
|
bool visible;
|
||||||
|
std::string version;
|
||||||
|
unsigned int changeset;
|
||||||
|
std::string timestamp;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Object* parent;
|
||||||
|
|
||||||
|
std::vector<const Node*> nodes;
|
||||||
|
std::vector<Tag> tags;
|
||||||
|
};
|
||||||
|
}
|
27
lib/osmparser/include/util.hpp
Normal file
27
lib/osmparser/include/util.hpp
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace tinyxml2
|
||||||
|
{
|
||||||
|
class XMLElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace osmp
|
||||||
|
{
|
||||||
|
typedef struct sBounds
|
||||||
|
{
|
||||||
|
float minlat, minlon, maxlat, maxlon;
|
||||||
|
} Bounds;
|
||||||
|
|
||||||
|
typedef struct sTag
|
||||||
|
{
|
||||||
|
std::string k; // TODO: Should/could be an enum
|
||||||
|
std::string v;
|
||||||
|
} Tag;
|
||||||
|
|
||||||
|
std::string GetSafeAttributeString(const tinyxml2::XMLElement* elem, const std::string& name);
|
||||||
|
float GetSafeAttributeFloat(const tinyxml2::XMLElement* elem, const std::string& name);
|
||||||
|
unsigned int GetSafeAttributeUint(const tinyxml2::XMLElement* elem, const std::string& name);
|
||||||
|
bool GetSafeAttributeBool(const tinyxml2::XMLElement* elem, const std::string& name);
|
||||||
|
}
|
49
lib/osmparser/src/osmnode.cpp
Normal file
49
lib/osmparser/src/osmnode.cpp
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
#include <osmnode.hpp>
|
||||||
|
|
||||||
|
#include <tinyxml2.h>
|
||||||
|
|
||||||
|
namespace xml = tinyxml2;
|
||||||
|
|
||||||
|
namespace osmp
|
||||||
|
{
|
||||||
|
Node::Node(const tinyxml2::XMLElement* node_elem, Object* parent) :
|
||||||
|
parent(parent)
|
||||||
|
{
|
||||||
|
// Get Attribute
|
||||||
|
id = GetSafeAttributeUint(node_elem, "id");
|
||||||
|
lat = GetSafeAttributeFloat(node_elem, "lat");
|
||||||
|
lon = GetSafeAttributeFloat(node_elem, "lon");
|
||||||
|
user = GetSafeAttributeString(node_elem, "user");
|
||||||
|
uid = GetSafeAttributeUint(node_elem, "uid");
|
||||||
|
visible = GetSafeAttributeBool(node_elem, "visible");
|
||||||
|
version = GetSafeAttributeString(node_elem, "version");
|
||||||
|
changeset = GetSafeAttributeUint(node_elem, "changeset");
|
||||||
|
timestamp = GetSafeAttributeString(node_elem, "timestamp");
|
||||||
|
|
||||||
|
const xml::XMLElement* tag_element = node_elem->FirstChildElement("tag");
|
||||||
|
while (tag_element != nullptr)
|
||||||
|
{
|
||||||
|
tags.push_back({
|
||||||
|
GetSafeAttributeString(tag_element, "k"),
|
||||||
|
GetSafeAttributeString(tag_element, "v")
|
||||||
|
});
|
||||||
|
|
||||||
|
tag_element = tag_element->NextSiblingElement("tag");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<Tag>& Node::GetTags() const
|
||||||
|
{
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Node::GetTagsSize() const
|
||||||
|
{
|
||||||
|
return tags.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
const Tag& Node::GetTag(size_t index) const
|
||||||
|
{
|
||||||
|
return tags[index];
|
||||||
|
}
|
||||||
|
}
|
118
lib/osmparser/src/osmobject.cpp
Normal file
118
lib/osmparser/src/osmobject.cpp
Normal file
|
@ -0,0 +1,118 @@
|
||||||
|
#include <osmobject.hpp>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <tinyxml2.h>
|
||||||
|
|
||||||
|
#include <osmnode.hpp>
|
||||||
|
#include <osmway.hpp>
|
||||||
|
|
||||||
|
namespace xml = tinyxml2;
|
||||||
|
|
||||||
|
namespace osmp
|
||||||
|
{
|
||||||
|
Object::Object(const std::string& file) :
|
||||||
|
bounds({ 0.0f, 0.0f, 0.0f, 0.0f })
|
||||||
|
{
|
||||||
|
xml::XMLDocument doc;
|
||||||
|
xml::XMLError result = doc.LoadFile(file.c_str());
|
||||||
|
if (result != xml::XML_SUCCESS)
|
||||||
|
{
|
||||||
|
std::cerr << "Error: " << result << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
xml::XMLElement* root = doc.FirstChildElement();
|
||||||
|
|
||||||
|
// Get bounds
|
||||||
|
xml::XMLElement* bounds_elem = root->FirstChildElement("bounds");
|
||||||
|
bounds = {
|
||||||
|
GetSafeAttributeFloat(bounds_elem, "minlat"),
|
||||||
|
GetSafeAttributeFloat(bounds_elem, "minlon"),
|
||||||
|
GetSafeAttributeFloat(bounds_elem, "maxlat"),
|
||||||
|
GetSafeAttributeFloat(bounds_elem, "maxlon")
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get nodes
|
||||||
|
xml::XMLElement* node_elem = root->FirstChildElement("node");
|
||||||
|
while (node_elem != nullptr)
|
||||||
|
{
|
||||||
|
Node* new_node = new Node(node_elem, this);
|
||||||
|
nodes.insert(std::make_pair(new_node->id, new_node));
|
||||||
|
|
||||||
|
node_elem = node_elem->NextSiblingElement("node");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get ways
|
||||||
|
xml::XMLElement* way_elem = root->FirstChildElement("way");
|
||||||
|
while (way_elem != nullptr)
|
||||||
|
{
|
||||||
|
Way* new_way = new Way(way_elem, this);
|
||||||
|
ways.insert(std::make_pair(new_way->id, new_way));
|
||||||
|
|
||||||
|
way_elem = way_elem->NextSiblingElement("way");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Object::~Object()
|
||||||
|
{
|
||||||
|
for (std::map<unsigned int, Way*>::iterator it = ways.begin(); it != ways.end(); ++it)
|
||||||
|
{
|
||||||
|
delete it->second;
|
||||||
|
}
|
||||||
|
ways.clear();
|
||||||
|
|
||||||
|
for (std::map<unsigned int, Node*>::iterator it = nodes.begin(); it != nodes.end(); ++it)
|
||||||
|
{
|
||||||
|
delete it->second;
|
||||||
|
}
|
||||||
|
nodes.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Node*> Object::GetNodes() const
|
||||||
|
{
|
||||||
|
std::vector<Node*> vecNodes;
|
||||||
|
for (std::map<unsigned int, Node*>::const_iterator it = nodes.begin(); it != nodes.end(); it++)
|
||||||
|
vecNodes.push_back(it->second);
|
||||||
|
|
||||||
|
return vecNodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Object::GetNodesSize() const
|
||||||
|
{
|
||||||
|
return nodes.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
const Node* Object::GetNode(unsigned int id) const
|
||||||
|
{
|
||||||
|
std::map<unsigned int, Node*>::const_iterator node = nodes.find(id);
|
||||||
|
if (node != nodes.end())
|
||||||
|
return node->second;
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Way*> Object::GetWays() const
|
||||||
|
{
|
||||||
|
std::vector<Way*> vecWays;
|
||||||
|
for (std::map<unsigned int, Way*>::const_iterator it = ways.begin(); it != ways.end(); it++)
|
||||||
|
vecWays.push_back(it->second);
|
||||||
|
|
||||||
|
return vecWays;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Object::GetWaysSize() const
|
||||||
|
{
|
||||||
|
return ways.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
const Way* Object::GetWay(unsigned int id) const
|
||||||
|
{
|
||||||
|
std::map<unsigned int, Way*>::const_iterator way = ways.find(id);
|
||||||
|
if (way != ways.end())
|
||||||
|
return way->second;
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
73
lib/osmparser/src/osmway.cpp
Normal file
73
lib/osmparser/src/osmway.cpp
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
#include <osmway.hpp>
|
||||||
|
|
||||||
|
#include <tinyxml2.h>
|
||||||
|
#include <osmobject.hpp>
|
||||||
|
|
||||||
|
namespace xml = tinyxml2;
|
||||||
|
|
||||||
|
namespace osmp
|
||||||
|
{
|
||||||
|
Way::Way(const tinyxml2::XMLElement* way_elem, Object* parent) :
|
||||||
|
parent(parent)
|
||||||
|
{
|
||||||
|
// Attributes
|
||||||
|
id = GetSafeAttributeUint(way_elem, "id");
|
||||||
|
user = GetSafeAttributeString(way_elem, "user");
|
||||||
|
uid = GetSafeAttributeUint(way_elem, "uid");
|
||||||
|
visible = GetSafeAttributeBool(way_elem, "visible");
|
||||||
|
version = GetSafeAttributeString(way_elem, "version");
|
||||||
|
changeset = GetSafeAttributeUint(way_elem, "changeset");
|
||||||
|
timestamp = GetSafeAttributeString(way_elem, "timestamp");
|
||||||
|
|
||||||
|
const xml::XMLElement* nd_elem = way_elem->FirstChildElement("nd");
|
||||||
|
while (nd_elem != nullptr)
|
||||||
|
{
|
||||||
|
nodes.push_back(
|
||||||
|
parent->GetNode(GetSafeAttributeUint(nd_elem, "ref"))
|
||||||
|
);
|
||||||
|
|
||||||
|
nd_elem = nd_elem->NextSiblingElement("nd");
|
||||||
|
}
|
||||||
|
|
||||||
|
const xml::XMLElement* tag_elem = way_elem->FirstChildElement("tag");
|
||||||
|
while (tag_elem != nullptr)
|
||||||
|
{
|
||||||
|
tags.push_back({
|
||||||
|
GetSafeAttributeString(tag_elem, "k"),
|
||||||
|
GetSafeAttributeString(tag_elem, "v")
|
||||||
|
});
|
||||||
|
|
||||||
|
tag_elem = tag_elem->NextSiblingElement("tag");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<Tag>& Way::GetTags() const
|
||||||
|
{
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Way::GetTagsSize() const
|
||||||
|
{
|
||||||
|
return tags.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
const Tag& Way::GetTag(size_t index) const
|
||||||
|
{
|
||||||
|
return tags[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<const Node*>& Way::GetNodes() const
|
||||||
|
{
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Way::GetNodesSize() const
|
||||||
|
{
|
||||||
|
return nodes.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
const Node& Way::GetNode(size_t index) const
|
||||||
|
{
|
||||||
|
return *(nodes[index]);
|
||||||
|
}
|
||||||
|
}
|
59
lib/osmparser/src/util.cpp
Normal file
59
lib/osmparser/src/util.cpp
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
#include <util.hpp>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <tinyxml2.h>
|
||||||
|
|
||||||
|
namespace xml = tinyxml2;
|
||||||
|
|
||||||
|
namespace osmp
|
||||||
|
{
|
||||||
|
#define FAILED(err) (err != xml::XML_SUCCESS)
|
||||||
|
|
||||||
|
std::string GetSafeAttributeString(const tinyxml2::XMLElement* elem, const std::string& name)
|
||||||
|
{
|
||||||
|
const char* buffer;
|
||||||
|
|
||||||
|
xml::XMLError result = elem->QueryStringAttribute(name.c_str(), &buffer);
|
||||||
|
if (FAILED(result))
|
||||||
|
{
|
||||||
|
std::cerr << "Failed to fetch string attribute \"" << name << "\"" << std::endl;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string returnStr(buffer);
|
||||||
|
return returnStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
float GetSafeAttributeFloat(const tinyxml2::XMLElement* elem, const std::string& name)
|
||||||
|
{
|
||||||
|
float returnVal = 0.0f;
|
||||||
|
|
||||||
|
xml::XMLError result = elem->QueryFloatAttribute(name.c_str(), &returnVal);
|
||||||
|
if (FAILED(result))
|
||||||
|
std::cerr << "Failed to fetch float attribute \"" << name << "\"" << std::endl;
|
||||||
|
|
||||||
|
return returnVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int GetSafeAttributeUint(const tinyxml2::XMLElement* elem, const std::string& name)
|
||||||
|
{
|
||||||
|
unsigned int returnVal = 0;
|
||||||
|
|
||||||
|
xml::XMLError result = elem->QueryUnsignedAttribute(name.c_str(), &returnVal);
|
||||||
|
if (FAILED(result))
|
||||||
|
std::cerr << "Failed to fetch uint attribute \"" << name << "\"" << std::endl;
|
||||||
|
|
||||||
|
return returnVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GetSafeAttributeBool(const tinyxml2::XMLElement* elem, const std::string& name)
|
||||||
|
{
|
||||||
|
bool returnVal = false;
|
||||||
|
|
||||||
|
xml::XMLError result = elem->QueryBoolAttribute(name.c_str(), &returnVal);
|
||||||
|
if (FAILED(result))
|
||||||
|
std::cerr << "Failed to fetch bool attribute \"" << name << "\"" << std::endl;
|
||||||
|
|
||||||
|
return returnVal;
|
||||||
|
}
|
||||||
|
}
|
748451
res/bigmap.osm
Normal file
748451
res/bigmap.osm
Normal file
File diff suppressed because it is too large
Load diff
553930
res/map.osm
Normal file
553930
res/map.osm
Normal file
File diff suppressed because it is too large
Load diff
115
src/main.cpp
Normal file
115
src/main.cpp
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <osmp.hpp>
|
||||||
|
#include <SDL.h>
|
||||||
|
|
||||||
|
// Map values from one interval [A, B] to another [a, b]
|
||||||
|
inline float Map(float A, float B, float a, float b, float x);
|
||||||
|
|
||||||
|
// A structure to hold a sequence of 2D points
|
||||||
|
typedef struct sRenderableWay
|
||||||
|
{
|
||||||
|
size_t length;
|
||||||
|
SDL_FPoint* points;
|
||||||
|
} RenderableWay;
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
// Load map data and calculate window size
|
||||||
|
osmp::Object* obj = new osmp::Object("bigmap.osm");
|
||||||
|
osmp::Bounds bounds = obj->bounds;
|
||||||
|
float aspectRatio = (float)(bounds.maxlon - bounds.minlon) / (float)(bounds.maxlat - bounds.minlat);
|
||||||
|
int windowWidth = 800;
|
||||||
|
int windowHeight = windowWidth / aspectRatio;
|
||||||
|
|
||||||
|
// Fetch all the ways
|
||||||
|
std::vector<osmp::Way*> ways = obj->GetWays();
|
||||||
|
|
||||||
|
// Turn them into renderable ways by mapping the global coordinates to screen coordinates (do this smarter in the future pls)
|
||||||
|
std::vector<RenderableWay> rWays;
|
||||||
|
for (osmp::Way* way : ways)
|
||||||
|
{
|
||||||
|
std::vector<const osmp::Node*> nodes = way->GetNodes();
|
||||||
|
|
||||||
|
RenderableWay rWay;
|
||||||
|
rWay.length = nodes.size();
|
||||||
|
rWay.points = new SDL_FPoint[rWay.length];
|
||||||
|
|
||||||
|
for (int i = 0; i < rWay.length; i++)
|
||||||
|
{
|
||||||
|
rWay.points[i].x = Map(bounds.minlon, bounds.maxlon, 0, windowWidth, nodes[i]->lon);
|
||||||
|
rWay.points[i].y = 1000 - Map(bounds.minlat, bounds.maxlat, 0, windowHeight, nodes[i]->lat);
|
||||||
|
}
|
||||||
|
|
||||||
|
rWays.push_back(rWay);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release map data
|
||||||
|
delete obj;
|
||||||
|
|
||||||
|
// Initiaize graphics API
|
||||||
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
|
|
||||||
|
// Create Window + Renderer
|
||||||
|
SDL_Window* window = SDL_CreateWindow("MapViewer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowWidth, windowHeight, SDL_WINDOW_SHOWN);
|
||||||
|
if (window == nullptr)
|
||||||
|
{
|
||||||
|
std::cerr << "Failed to create Window" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
|
||||||
|
if (renderer == nullptr)
|
||||||
|
{
|
||||||
|
std::cerr << "Failed to create renderer" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Window loop
|
||||||
|
bool isOpen = true;
|
||||||
|
SDL_Event e;
|
||||||
|
while (isOpen)
|
||||||
|
{
|
||||||
|
while (SDL_PollEvent(&e))
|
||||||
|
{
|
||||||
|
if (e.type == SDL_WINDOWEVENT)
|
||||||
|
{
|
||||||
|
switch (e.window.event)
|
||||||
|
{
|
||||||
|
case SDL_WINDOWEVENT_CLOSE:
|
||||||
|
isOpen = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
|
// Render the ways
|
||||||
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||||
|
for (RenderableWay rWay : rWays)
|
||||||
|
{
|
||||||
|
SDL_RenderDrawLinesF(renderer, rWay.points, rWay.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup time
|
||||||
|
SDL_DestroyRenderer(renderer);
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
|
||||||
|
SDL_Quit();
|
||||||
|
|
||||||
|
for (RenderableWay& rWay : rWays)
|
||||||
|
delete[] rWay.points;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline float Map(float A, float B, float a, float b, float x)
|
||||||
|
{
|
||||||
|
return (x - A) * (b - a) / (B - A) + a;
|
||||||
|
}
|
Loading…
Reference in a new issue