diff options
| author | Abhinav Prasai <abhinav.prsai@gmail.com> | 2025-11-22 21:06:11 +0000 |
|---|---|---|
| committer | Abhinav Prasai <abhinav.prsai@gmail.com> | 2025-11-22 23:45:16 +0000 |
| commit | 1b53cc099c2b218479e0aaa585e2251b6365cb08 (patch) | |
| tree | eb9003de3ed8748a2397b8bc33841c02b3600e43 | |
| parent | b0c0105d6373bc762ab535550ff186b0f0b2063d (diff) | |
create window + draw in context
.
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | Makefile | 28 | ||||
| -rw-r--r-- | include/quark.h | 46 | ||||
| -rw-r--r-- | include/sdl.h | 9 | ||||
| -rw-r--r-- | src/quark.c | 98 | ||||
| -rw-r--r-- | src/sdl.c | 32 |
6 files changed, 216 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a4ba574 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +build/ +quark +compile_flags.txt diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9a92cd0 --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +CC = cc + +DEPS = -lSDL2 -lGLESv2 +INCLUDE = -Iinclude +CFLAGS = -std=c99 -O2 -Wall -Wextra $(INCLUDE) +LDFLAGS = $(DEPS) + +SRC = src/quark.c src/sdl.c +OBJ = build/quark.o build/sdl.o + +all: quark + +quark: $(OBJ) + $(CC) -o quark $(OBJ) $(LDFLAGS) + +build/%.o: src/%.c + mkdir -p build + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -f quark $(OBJ) + +run: + ./quark + +compile_flags: + rm -f compile_flags.txt + for f in ${CFLAGS}; do echo $$f >> compile_flags.txt; done diff --git a/include/quark.h b/include/quark.h new file mode 100644 index 0000000..233314e --- /dev/null +++ b/include/quark.h @@ -0,0 +1,46 @@ +#pragma once + +#include <stdbool.h> +#include <stdio.h> + +#include <SDL2/SDL.h> + +/* types */ +typedef struct { + SDL_Window* win; + SDL_GLContext gl; + int win_w; + int win_h; + bool running; +} quark_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/sdl.h b/include/sdl.h new file mode 100644 index 0000000..e73a903 --- /dev/null +++ b/include/sdl.h @@ -0,0 +1,9 @@ +#pragma once + +#include <stdbool.h> + +#include <SDL2/SDL.h> + +SDL_Window* sdl_create_window(const char* title, int w, int h); +bool sdl_init(void); +void sdl_quit(void); diff --git a/src/quark.c b/src/quark.c new file mode 100644 index 0000000..ed98a83 --- /dev/null +++ b/src/quark.c @@ -0,0 +1,98 @@ +#include <stdbool.h> + +#include <SDL2/SDL.h> +#include <SDL2/SDL_opengles2.h> +#include <stdlib.h> + +#include "quark.h" +#include "sdl.h" + +quark_t quark; + +void quit(void); +void render_tmp(void); +void run(void); +void setup(void); + +void quit(void) +{ + LOG_INFO("quitting quark..."); + sdl_quit(); +} + +void render_tmp(void) +{ + glViewport(0, 0, quark.win_w, quark.win_h); + + glClearColor(0.0, 0.0, 0.0, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + glEnable(GL_SCISSOR_TEST); + int bar_h = 32; + glScissor(0, quark.win_h - bar_h, quark.win_w, bar_h); + glClearColor(0.25, 0.25, 0.30, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + glDisable(GL_SCISSOR_TEST); + + SDL_GL_SwapWindow(quark.win); +} + +void run(void) +{ + quark.running = true; + LOG_INFO("starting quark..."); + + SDL_Event ev; + + while (quark.running) { + while (SDL_PollEvent(&ev)) { + if (ev.type == SDL_QUIT) + quark.running = false; + + if (ev.type == SDL_WINDOWEVENT_RESIZED) { + quark.win_w = ev.window.data1; + quark.win_h = ev.window.data2; + } + } + + render_tmp(); + SDL_Delay(10); + } +} + +void setup(void) +{ + sdl_init(); + quark.win = sdl_create_window("quark", 800, 600); + if (!quark.win) { + LOG_ERROR("failed to create quark window"); + exit(EXIT_FAILURE); + } + else { + LOG_PASS("created SDL window"); + } + quark.win_w = 800; + quark.win_h = 600; + + /* create OpenGL context */ + quark.gl = SDL_GL_CreateContext(quark.win); + if (!quark.gl) { + LOG_ERROR("failed to create GL context"); + exit(EXIT_FAILURE); + } + else { + LOG_PASS("created OpenGL ES context"); + } +} + +int main(int argc, char** argv) +{ + (void) argc; + (void) argv; + + setup(); + run(); + quit(); + + return EXIT_SUCCESS; +} diff --git a/src/sdl.c b/src/sdl.c new file mode 100644 index 0000000..4614969 --- /dev/null +++ b/src/sdl.c @@ -0,0 +1,32 @@ +#include <stdbool.h> + +#include <SDL2/SDL.h> + +#include "quark.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_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(); +} |
