From 93f57352a74404d6bb8b63ba315633de261a4fb5 Mon Sep 17 00:00:00 2001 From: uint Date: Thu, 18 Dec 2025 18:48:56 +0000 Subject: revamp style --- include/engine.h | 22 -------- include/quartz.h | 46 --------------- include/render.h | 11 ---- include/sdl.h | 9 --- src/engine.c | 110 ------------------------------------ src/quartz.c | 168 ------------------------------------------------------- src/render.c | 129 ------------------------------------------ src/sdl.c | 32 ----------- 8 files changed, 527 deletions(-) delete mode 100644 include/engine.h delete mode 100644 include/quartz.h delete mode 100644 include/render.h delete mode 100644 include/sdl.h delete mode 100644 src/engine.c delete mode 100644 src/quartz.c delete mode 100644 src/render.c delete mode 100644 src/sdl.c diff --git a/include/engine.h b/include/engine.h deleted file mode 100644 index 45ebac8..0000000 --- a/include/engine.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include - -#define MAX_TABS 32 /* TODO: move to config */ - -typedef struct { - char* url; - char* title; - bool loading; -} quartz_tab_t; - -int engine_get_current_index(void); -int engine_get_tab_count(void); -bool engine_init(void); -void engine_load_url(int id, const char* url); -void engine_shutdown(void); -void engine_tab_close(int id); -quartz_tab_t* engine_tab_current(void); -int engine_tab_new(const char* url); -void engine_tab_switch(int id); -void engine_update(void); diff --git a/include/quartz.h b/include/quartz.h deleted file mode 100644 index 7c1c92d..0000000 --- a/include/quartz.h +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -#include -#include - -#include - -/* types */ -typedef struct { - SDL_Window* win; - SDL_GLContext gl; - int win_w; - int win_h; - bool running; -} quartz_t; - -/* logging */ -#define ANSI_RESET "\x1b[0m" -#define ANSI_FG_WHITE "\x1b[37m" -#define ANSI_BG_CYAN "\x1b[46m" -#define ANSI_BG_MAGENTA "\x1b[45m" -#define ANSI_BG_RED "\x1b[41m" -#define ANSI_BG_GREEN "\x1b[42m" -#define ANSI_BG_YELLOW "\x1b[43m" - -#define LOG(fmt, ...) \ - fprintf(stdout, fmt "\n", ##__VA_ARGS__) - -#define LOG_INFO(fmt, ...) \ - fprintf(stdout, ANSI_FG_WHITE ANSI_BG_CYAN " INFO " ANSI_RESET " " fmt "\n", ##__VA_ARGS__) - -#define LOG_PASS(fmt, ...) \ - fprintf(stdout, ANSI_FG_WHITE ANSI_BG_GREEN " PASS " ANSI_RESET " " fmt " (%s:%d)\n", ##__VA_ARGS__, __FILE__, __LINE__) - -#define LOG_WARN(fmt, ...) \ - fprintf(stderr, ANSI_FG_WHITE ANSI_BG_YELLOW " WARN " ANSI_RESET " " fmt "\n", ##__VA_ARGS__) - -#define LOG_ERROR(fmt, ...) \ - fprintf(stderr, ANSI_FG_WHITE ANSI_BG_RED " ERROR " ANSI_RESET " " fmt " (%s:%d)\n", ##__VA_ARGS__, __FILE__, __LINE__) - -#ifdef DEBUG -#define LOG_DEBUG(fmt, ...) \ - fprintf(stdout, ANSI_FG_WHITE ANSI_BG_MAGENTA " DEBUG " ANSI_RESET " " fmt "\n", ##__VA_ARGS__) -#else -#define LOG_DEBUG(fmt, ...) ((void)0) -#endif diff --git a/include/render.h b/include/render.h deleted file mode 100644 index 7b8a555..0000000 --- a/include/render.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include - -#include - -void render_begin(int w, int h); -void render_end(SDL_Window* win); -bool render_init(void); -void render_rect(float x, float y, float w, float h, float r, float g, float b, float a); -void render_shutdown(void); diff --git a/include/sdl.h b/include/sdl.h deleted file mode 100644 index e73a903..0000000 --- a/include/sdl.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include - -#include - -SDL_Window* sdl_create_window(const char* title, int w, int h); -bool sdl_init(void); -void sdl_quit(void); diff --git a/src/engine.c b/src/engine.c deleted file mode 100644 index 5d4afe2..0000000 --- a/src/engine.c +++ /dev/null @@ -1,110 +0,0 @@ -#include -#include - -#include "engine.h" -#include "quartz.h" - -static quartz_tab_t tabs[MAX_TABS]; -static int tab_count = 0; -static int current_tab = -1; - -/* TODO: actually implement webkit */ - -int engine_get_current_index(void) -{ - /* fetch private */ - return current_tab; -} - -int engine_get_tab_count(void) -{ - /* fetch private */ - return tab_count; -} - -bool engine_init(void) -{ - LOG_PASS("engine: init"); - return true; -} - -void engine_load_url(int id, const char* url) -{ - if (id < 0 || id >= tab_count) - return; - - free(tabs[id].url); - tabs[id].url = SDL_strdup(url); - tabs[id].loading = true; - - LOG_INFO("engine: url=%s loaded to tab=%d", url, id); -} - -void engine_shutdown(void) -{ - LOG_INFO("engine: shutting down"); - for (int i = 0; i < tab_count; i++) { - free(tabs[i].url); - free(tabs[i].title); - } -} - -void engine_tab_close(int id) -{ - if (id < 0 || id >= tab_count) - return; - - LOG_INFO("engine: closed tab %d", id); - - free(tabs[id].url); - free(tabs[id].title); - - /* collapse */ - for (int i = id; i < tab_count-1; i++) - tabs[i] = tabs[i+1]; - - tab_count--; - if (current_tab >= tab_count) - current_tab = tab_count - 1; -} - -quartz_tab_t* engine_tab_current(void) -{ - for (int i = 0; i < tab_count; i++) - if (i == current_tab) - return &tabs[i]; - - return NULL; -} - -int engine_tab_new(const char* url) -{ - if (tab_count >= MAX_TABS) { - LOG_WARN("engine: maximum number of tabs reached"); - return -1; - } - - quartz_tab_t* t = &tabs[tab_count]; - t->url = SDL_strdup(url ? url : "about_blank"); - t->title = SDL_strdup("new tab"); /* TODO: customise */ - t->loading = true; - - current_tab = tab_count; - tab_count++; - - LOG_INFO("engine: created new tab: id=%d url=%s", current_tab, url); - return current_tab; -} - -void engine_tab_switch(int id) -{ - if (id < 0 || id >= tab_count) - return; - current_tab = id; - LOG_INFO("engine: switched to tab %d", id); -} - -void engine_update(void) -{ - /* nothing here */ -} diff --git a/src/quartz.c b/src/quartz.c deleted file mode 100644 index 8c7d92e..0000000 --- a/src/quartz.c +++ /dev/null @@ -1,168 +0,0 @@ -#include - -#include -#include -#include - -#include "engine.h" -#include "quartz.h" -#include "render.h" -#include "sdl.h" - -quartz_t quartz; - -void init(void); -void quit(void); -void render_tmp(void); -void run(void); - -void quit(void) -{ - engine_shutdown(); - render_shutdown(); - sdl_quit(); - LOG_INFO("quartz: quitting"); -} - -void render_tmp(void) -{ - render_begin(quartz.win_w, quartz.win_h); - - /* top bar */ - render_rect(0, 0, quartz.win_w, 32, 0.15f, 0.15f, 0.22f, 1.0f); - - /* number of tabs changes color */ - int n = engine_get_tab_count(); - int cur = engine_get_current_index(); - - float intensity = 0.2f + 0.05f * (float)n; - if (intensity > 1.0f) - intensity = 1.0f; - - /* tab indicator */ - render_rect(10, 8, 100, 16, intensity, (cur >= 0 ? 0.5f : 0.2f), 0.2f, 1.0f); - - render_end(quartz.win); -} - -void run(void) -{ - quartz.running = true; - LOG_INFO("quartz: starting"); - - SDL_Event ev; - - while (quartz.running) { - /* TODO: add event handler + this is just test until WebKit implemented */ - while (SDL_PollEvent(&ev)) { - if (ev.type == SDL_QUIT) - quartz.running = false; - - if (ev.type == SDL_WINDOWEVENT) { - if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { - quartz.win_w = ev.window.data1; - quartz.win_h = ev.window.data2; - } - } - - if (ev.type == SDL_KEYDOWN) { - SDL_Keycode key = ev.key.keysym.sym; - SDL_Keymod mods = ev.key.keysym.mod; - - /* C-t: new tab */ - if ((mods & KMOD_CTRL) && key == SDLK_t) { - int id = engine_tab_new("about:blank"); - if (id >= 0) { - LOG_INFO("quartz: new tab %d", id); - engine_tab_switch(id); - } - } - - /* C-w: close current tab */ - if ((mods & KMOD_CTRL) && key == SDLK_w) { - quartz_tab_t* cur = engine_tab_current(); - if (cur) { - LOG_INFO("quartz: closing current tab"); - engine_tab_close(engine_get_current_index()); - } - } - - /* C-t: switch tab */ - if ((mods & KMOD_CTRL) && key == SDLK_TAB) { - int n = engine_get_tab_count(); - int cur = engine_get_current_index(); - if (n > 0 && cur >= 0) { - int next = (cur + 1) % n; - engine_tab_switch(next); - } - } - } - } - - engine_update(); - render_tmp(); - SDL_Delay(10); - } -} - -void init(void) -{ - sdl_init(); - quartz.win = sdl_create_window("quartz", 800, 600); - if (!quartz.win) { - LOG_ERROR("sdl: failed to create quartz window"); - exit(EXIT_FAILURE); - } - else { - LOG_PASS("sdl: created SDL window"); - } - quartz.win_w = 800; - quartz.win_h = 600; - - /* init systems */ - quartz.gl = SDL_GL_CreateContext(quartz.win); - if (!quartz.gl) { - LOG_ERROR("opengl: failed to create GL context"); - exit(EXIT_FAILURE); - } - else { - LOG_PASS("opengl: created OpenGL ES context"); - } - - if (!render_init()) { - LOG_ERROR("renderer: failed to initialise renderer"); - exit(EXIT_FAILURE); - } - else { - LOG_PASS("renderer: initialised renderer "); - } - - if (!engine_init()) { - LOG_ERROR("engine: failed to init engine"); - exit(EXIT_FAILURE); - } - else { - LOG_ERROR("engine: initialised engine"); - } - - /* starting tab */ - if (engine_tab_new("about:blank") < 0) { - LOG_ERROR("engine: failed to create initial tab"); - exit(EXIT_FAILURE); - } - else { - LOG_PASS("engine: created initial tab"); - } -} - -int main(int argc, char** argv) -{ - (void) argc; - (void) argv; - - init(); - run(); - quit(); - - return EXIT_SUCCESS; -} diff --git a/src/render.c b/src/render.c deleted file mode 100644 index 2bc4430..0000000 --- a/src/render.c +++ /dev/null @@ -1,129 +0,0 @@ -#include -#include - -#include -#include - -#include "quartz.h" -#include "render.h" - -/* shader handles */ -static GLuint prog = 0; -static GLint u_res; -static GLint u_color; -static GLuint vbo = 0; - -/* helpers */ -static GLuint compile(GLenum type, const char* src); - -static GLuint compile(GLenum type, const char* src) -{ - GLuint s = glCreateShader(type); - glShaderSource(s, 1, &src, NULL); - glCompileShader(s); - - GLint ok; - glGetShaderiv(s, GL_COMPILE_STATUS, &ok); - if (!ok) { - char log[512]; - glGetShaderInfoLog(s, sizeof(log), NULL, log); - LOG_ERROR("renderer: shader error: %s", log); - glDeleteShader(s); - return 0; - } - - return s; -} - -void render_begin(int w, int h) -{ - glViewport(0, 0, w, h); - glClearColor(0.0, 0.0, 0.0, 1.0); - glClear(GL_COLOR_BUFFER_BIT); - - glUseProgram(prog); - glUniform2f(u_res, w, h); -} - -void render_end(SDL_Window* win) -{ - SDL_GL_SwapWindow(win); -} - -bool render_init(void) -{ - const char* vs = - "attribute vec2 a_pos;" - "uniform vec2 u_res;" - "void main() {" - "vec2 p = a_pos / u_res;" - "p.y = 1.0 - p.y;" - "p = p * 2.0 - 1.0;" - " gl_Position = vec4(p, 0.0, 1.0);" - "}"; - - const char* fs = - "precision mediump float;" - "uniform vec4 u_color;" - "void main()" - "{" - " gl_FragColor = u_color;" - "}"; - - GLuint v = compile(GL_VERTEX_SHADER, vs); - GLuint f = compile(GL_FRAGMENT_SHADER, fs); - if (!v || !f) - return false; - - prog = glCreateProgram(); - glAttachShader(prog, v); - glAttachShader(prog, f); - glBindAttribLocation(prog, 0, "a_pos"); - glLinkProgram(prog); - - glDeleteShader(v); - glDeleteShader(f); - - GLint ok; - glGetProgramiv(prog, GL_LINK_STATUS, &ok); - if (!ok) { - LOG_ERROR("renderer: program link failed"); - return false; - } - - u_res = glGetUniformLocation(prog, "u_res"); - u_color = glGetUniformLocation(prog, "u_color"); - - glGenBuffers(1, &vbo); - - return true; -} - -void render_rect(float x, float y, float w, float h, float r, float g, float b, float a) -{ - float verts[12] = { - x, y, - x+w, y, - x+w, y+h, - - x, y, - x+w, y+h, - x, y+h - }; - - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); - - glUniform4f(u_color, r, g, b, a); - - glDrawArrays(GL_TRIANGLES, 0, 6); -} - -void render_shutdown(void) -{ - glDeleteProgram(prog); - glDeleteBuffers(1, &vbo); -} diff --git a/src/sdl.c b/src/sdl.c deleted file mode 100644 index 55feaea..0000000 --- a/src/sdl.c +++ /dev/null @@ -1,32 +0,0 @@ -#include - -#include - -#include "quartz.h" -#include "sdl.h" - -SDL_Window* sdl_create_window(const char* title, int w, int h) -{ - return SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL); -} - -bool sdl_init(void) -{ - if (SDL_Init(SDL_INIT_VIDEO) != 0) { - LOG_ERROR("sdl: SDL_Init failed: %s", SDL_GetError()); - return false; - } - - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); - - return true; -} - -void sdl_quit(void) -{ - SDL_Quit(); -} -- cgit v1.2.3