summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAbhinav Prasai <abhinav.prsai@gmail.com>2025-11-22 21:06:11 +0000
committerAbhinav Prasai <abhinav.prsai@gmail.com>2025-11-22 23:45:16 +0000
commit1b53cc099c2b218479e0aaa585e2251b6365cb08 (patch)
treeeb9003de3ed8748a2397b8bc33841c02b3600e43 /src
parentb0c0105d6373bc762ab535550ff186b0f0b2063d (diff)
create window + draw in context
.
Diffstat (limited to 'src')
-rw-r--r--src/quark.c98
-rw-r--r--src/sdl.c32
2 files changed, 130 insertions, 0 deletions
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();
+}