From 83cf6642717fb873d130d850812742b98accfd90 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Tue, 10 Jan 2023 14:33:33 +0100 Subject: [PATCH] browser: start on gui and sdl --- browser/main.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/browser/main.cpp b/browser/main.cpp index d7b16c1..bfae6a2 100644 --- a/browser/main.cpp +++ b/browser/main.cpp @@ -1,3 +1,83 @@ +#include "SDL_events.h" +#include "SDL_pixels.h" +#include "SDL_rect.h" +#include "SDL_render.h" +#include "SDL_video.h" +#include "utils/all.hpp" +#include #include +#include -int main() { fmt::print("browser: hello world!\n"); } \ No newline at end of file +class GUI { +public: + GUI(const GUI&) = delete; + auto operator=(const GUI&) -> GUI& = delete; + GUI(GUI&&) = delete; + auto operator=(GUI&&) -> GUI& = delete; + ~GUI() + { + SDL_DestroyWindow(window); + SDL_DestroyRenderer(renderer); + SDL_Quit(); + }; + friend auto std::make_unique(SDL_Window*&, SDL_Renderer*&) + -> std::unique_ptr; + static auto create() noexcept -> Result, void> + { + if (SDL_Init(SDL_INIT_VIDEO) != 0) + return {}; + SDL_Window* window = nullptr; + SDL_Renderer* renderer = nullptr; + if (SDL_CreateWindowAndRenderer(1280, 720, 0, &window, &renderer) != 0) + return {}; + return std::make_unique(window, renderer); + } + auto should_exit() noexcept -> bool + { + SDL_Event event; + bool should_exit = false; + while (SDL_PollEvent(&event) != 0) { + if (event.type == SDL_QUIT) + should_exit = true; + } + return should_exit; + } + auto set_background_color(uint8_t r, uint8_t g, uint8_t b) noexcept -> void + { + SDL_SetRenderDrawColor(renderer, r, g, b, SDL_ALPHA_OPAQUE); + SDL_RenderClear(renderer); + } + + auto create_rect(SDL_Rect rect, uint8_t r, uint8_t g, uint8_t b) noexcept + -> void + { + SDL_SetRenderDrawColor(renderer, r, g, b, SDL_ALPHA_OPAQUE); + SDL_RenderFillRect(renderer, &rect); + } + + auto update_gui() noexcept -> void { SDL_RenderPresent(this->renderer); } + +private: + GUI(SDL_Window* window, SDL_Renderer* renderer) + : window { window } + , renderer { renderer } + { } + SDL_Window* window; + SDL_Renderer* renderer; +}; + +auto main() -> int +{ + // test + fmt::print("browser: hello world!\n"); + auto gui = GUI::create().unwrap(); + while (true) { + bool should_exit = gui->should_exit(); + if (should_exit) + break; + gui->set_background_color(100, 180, 220); + SDL_Rect rect = { .x = 0, .y = 0, .w = 50, .h = 50 }; + gui->create_rect(rect, 255, 0, 0); + gui->update_gui(); + } +} \ No newline at end of file