From e968f111644aae28d935385618b91ea76490df56 Mon Sep 17 00:00:00 2001 From: orosmatthew Date: Wed, 27 Sep 2023 15:01:46 -0400 Subject: [PATCH] Draw window --- CMakeLists.txt | 9 +++++++-- src/fetch.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/fetch.hpp | 10 ++++++++++ src/main.cpp | 48 ++++++++++++++++-------------------------------- 4 files changed, 77 insertions(+), 34 deletions(-) create mode 100644 src/fetch.cpp create mode 100644 src/fetch.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4282ca5..3d8721b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,11 @@ if (WIN32) endif () add_subdirectory(external/curl-8.3.0) -add_executable(browser src/main.cpp) +set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) +add_subdirectory(external/raylib-4.5.0) -target_link_libraries(browser libcurl) \ No newline at end of file +add_subdirectory(external/raylib-cpp-4.5.1) + +add_executable(browser src/main.cpp src/fetch.cpp) + +target_link_libraries(browser libcurl raylib raylib_cpp) \ No newline at end of file diff --git a/src/fetch.cpp b/src/fetch.cpp new file mode 100644 index 0000000..b940ea0 --- /dev/null +++ b/src/fetch.cpp @@ -0,0 +1,44 @@ +#include "fetch.hpp" + +#include +#include + +#include + +CURL* g_curl; + +size_t curl_write_func(void* ptr, size_t size, size_t num, std::string* str) +{ + str->append(static_cast(ptr), size * num); + return size * num; +} + +std::optional fetch_url(const std::string& url) +{ + std::string curl_write_data; + curl_easy_setopt(g_curl, CURLOPT_URL, url.c_str()); + curl_easy_setopt(g_curl, CURLOPT_CA_CACHE_TIMEOUT, 604800L); + curl_easy_setopt(g_curl, CURLOPT_WRITEFUNCTION, curl_write_func); + curl_easy_setopt(g_curl, CURLOPT_WRITEDATA, &curl_write_data); + CURLcode res = curl_easy_perform(g_curl); + if (res != CURLE_OK) { + std::cerr << curl_easy_strerror(res) << std::endl; + return {}; + } + return curl_write_data; +} + +void init_curl() +{ + curl_global_init(CURL_GLOBAL_DEFAULT); + g_curl = curl_easy_init(); + if (g_curl == nullptr) { + std::cerr << "Failed to initialize CURL" << std::endl; + } +} + +void destroy_curl() +{ + curl_easy_cleanup(g_curl); + curl_global_cleanup(); +} diff --git a/src/fetch.hpp b/src/fetch.hpp new file mode 100644 index 0000000..483e370 --- /dev/null +++ b/src/fetch.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include +#include + +void init_curl(); + +std::optional fetch_url(const std::string& url); + +void destroy_curl(); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ec2e212..5ecb047 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,45 +1,29 @@ -#include +#include "fetch.hpp" #include #include - -CURL* g_curl; - -size_t curl_write_func(void* ptr, size_t size, size_t num, std::string* str) -{ - str->append(static_cast(ptr), size * num); - return size * num; -} - -std::optional fetch_url(const std::string& url) -{ - std::string curl_write_data; - curl_easy_setopt(g_curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(g_curl, CURLOPT_CA_CACHE_TIMEOUT, 604800L); - curl_easy_setopt(g_curl, CURLOPT_WRITEFUNCTION, curl_write_func); - curl_easy_setopt(g_curl, CURLOPT_WRITEDATA, &curl_write_data); - CURLcode res = curl_easy_perform(g_curl); - if (res != CURLE_OK) { - std::cerr << curl_easy_strerror(res) << std::endl; - return {}; - } - return curl_write_data; -} +#include +#include int main() { - curl_global_init(CURL_GLOBAL_DEFAULT); - g_curl = curl_easy_init(); - if (g_curl == nullptr) { - std::cerr << "Failed to initialize CURL" << std::endl; - return EXIT_FAILURE; - } + init_curl(); std::optional data = fetch_url("https://example.com"); if (data.has_value()) { std::cout << data.value(); } - curl_easy_cleanup(g_curl); - curl_global_cleanup(); + SetConfigFlags(ConfigFlags::FLAG_WINDOW_RESIZABLE); + raylib::Window window(800, 600, "Browser"); + SetTargetFPS(60); + + while (!window.ShouldClose()) { + BeginDrawing(); + window.ClearBackground(raylib::Color::White()); + DrawText(data.value().data(), 0, 0, 24, raylib::Color::Black()); + EndDrawing(); + } + + destroy_curl(); return EXIT_SUCCESS; } \ No newline at end of file