You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "graphics/opengl/Window.h"
  2. #include "html/HTMLParser.h"
  3. #include "html/TagNode.h"
  4. #include "html/TextNode.h"
  5. #include "networking/HTTPRequest.h"
  6. #include "networking/HTTPResponse.h"
  7. #include <ctime>
  8. #include <iostream>
  9. #include <memory>
  10. #include "StringUtils.h"
  11. #include "WebResource.h"
  12. #include "CommandLineParams.h"
  13. #include "URL.h"
  14. #include "Log.h"
  15. const std::unique_ptr<Window> window = std::make_unique<Window>();
  16. URL currentURL;
  17. bool setWindowContent(URL const& url);
  18. bool setWindowContent(URL const& url) {
  19. WebResource res = getWebResource(url);
  20. if (res.resourceType == ResourceType::INVALID) {
  21. logError() << "Invalid resource type: " << res.raw << std::endl;
  22. return false;
  23. }
  24. HTMLParser parser;
  25. const std::clock_t begin = clock();
  26. std::shared_ptr<Node> rootNode = parser.parse(res.raw);
  27. const std::clock_t end = clock();
  28. logDebug() << "Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
  29. window->setDOM(rootNode);
  30. return true;
  31. }
  32. void navTo(std::string url) {
  33. logDebug() << "navTo(" << url << ")" << std::endl;
  34. currentURL = currentURL.merge(URL(url));
  35. logDebug() << "go to: " << currentURL << std::endl;
  36. setWindowContent(currentURL);
  37. }
  38. int main(int argc, char *argv[]) {
  39. if (argc == 1) {
  40. std::cout << "./netrunner <url|file.html>" << std::endl;
  41. return 1;
  42. }
  43. std::cout << "/g/ntr - NetRunner build " << __DATE__ << std::endl;
  44. initCLParams(argc, argv);
  45. std::string url = getCLParamByIndex(1);
  46. if (url[0] == '/') {
  47. url = "file://" + url;
  48. }
  49. currentURL=URL(url);
  50. logDebug() << "loading [" << currentURL << "]" << std::endl;
  51. if (!setWindowContent(currentURL)) {
  52. return 1;
  53. }
  54. window->init();
  55. if (!window->window) {
  56. return 1;
  57. }
  58. while (!glfwWindowShouldClose(window->window)) {
  59. //const std::clock_t begin = clock();
  60. window->render();
  61. glfwWaitEvents(); // block until something changes
  62. //const std::clock_t end = clock();
  63. //std::cout << '\r' << std::fixed << (((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) * 1000) << std::scientific << " ms/f " << std::flush;
  64. }
  65. return 0;
  66. }