Draw window

This commit is contained in:
orosmatthew 2023-09-27 15:01:46 -04:00
parent 2e05418c68
commit e968f11164
4 changed files with 77 additions and 34 deletions

View File

@ -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)
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)

44
src/fetch.cpp Normal file
View File

@ -0,0 +1,44 @@
#include "fetch.hpp"
#include <iostream>
#include <string>
#include <curl/curl.h>
CURL* g_curl;
size_t curl_write_func(void* ptr, size_t size, size_t num, std::string* str)
{
str->append(static_cast<char*>(ptr), size * num);
return size * num;
}
std::optional<std::string> 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();
}

10
src/fetch.hpp Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include <string>
#include <optional>
void init_curl();
std::optional<std::string> fetch_url(const std::string& url);
void destroy_curl();

View File

@ -1,45 +1,29 @@
#include <curl/curl.h>
#include "fetch.hpp"
#include <iostream>
#include <optional>
CURL* g_curl;
size_t curl_write_func(void* ptr, size_t size, size_t num, std::string* str)
{
str->append(static_cast<char*>(ptr), size * num);
return size * num;
}
std::optional<std::string> 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 <raylib-cpp.hpp>
#include <raylib.h>
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<std::string> 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;
}