Draw window
This commit is contained in:
parent
2e05418c68
commit
e968f11164
@ -12,6 +12,11 @@ if (WIN32)
|
|||||||
endif ()
|
endif ()
|
||||||
add_subdirectory(external/curl-8.3.0)
|
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
44
src/fetch.cpp
Normal 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
10
src/fetch.hpp
Normal 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();
|
48
src/main.cpp
48
src/main.cpp
@ -1,45 +1,29 @@
|
|||||||
#include <curl/curl.h>
|
#include "fetch.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <raylib-cpp.hpp>
|
||||||
CURL* g_curl;
|
#include <raylib.h>
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
init_curl();
|
||||||
g_curl = curl_easy_init();
|
|
||||||
if (g_curl == nullptr) {
|
|
||||||
std::cerr << "Failed to initialize CURL" << std::endl;
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<std::string> data = fetch_url("https://example.com");
|
std::optional<std::string> data = fetch_url("https://example.com");
|
||||||
if (data.has_value()) {
|
if (data.has_value()) {
|
||||||
std::cout << data.value();
|
std::cout << data.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
curl_easy_cleanup(g_curl);
|
SetConfigFlags(ConfigFlags::FLAG_WINDOW_RESIZABLE);
|
||||||
curl_global_cleanup();
|
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;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user