summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-11-20 16:20:00 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2020-11-20 16:20:00 -0500
commitc376f6f53f737d6220206baa323c0bc62d48a9f0 (patch)
tree364d5916797718b5339271f768c883e5fd799b67
opening a window
-rw-r--r--.gitignore5
-rw-r--r--Makefile6
-rw-r--r--main.c75
3 files changed, 86 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..bfa099f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+ted
+*~
+*.swp
+tags
+TAGS
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..ad7b1d1
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,6 @@
+ALL_CFLAGS=$(CFLAGS) -Wall -Wextra -Wshadow -Wconversion -Wpedantic -pedantic -std=gnu11 \
+ -Wno-unused-function -Wno-fixed-enum-extension -Wimplicit-fallthrough
+LIBS=-lSDL2 -lGL -ldl
+DEBUG_CFLAGS=$(ALL_CFLAGS) $(LIBS) -DDEBUG -O0 -g
+ted: *.[ch]
+ $(CC) main.c -o $@ $(DEBUG_CFLAGS)
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..9d9b99d
--- /dev/null
+++ b/main.c
@@ -0,0 +1,75 @@
+#ifdef __GNUC__
+#define no_warn_start _Pragma("GCC diagnostic push") \
+ _Pragma("GCC diagnostic ignored \"-Wpedantic\"") \
+ _Pragma("GCC diagnostic ignored \"-Wsign-conversion\"") \
+ _Pragma("GCC diagnostic ignored \"-Wsign-compare\"") \
+ _Pragma("GCC diagnostic ignored \"-Wconversion\"") \
+ _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"") \
+ _Pragma("GCC diagnostic ignored \"-Wunused-function\"")
+
+#define no_warn_end _Pragma("GCC diagnostic pop")
+#else
+#define no_warn_start
+#define no_warn_end
+#endif
+
+no_warn_start
+#include <SDL2/SDL.h>
+no_warn_end
+#include <GL/gl.h>
+#include <stdbool.h>
+
+static void die(char const *fmt, ...) {
+ char buf[256] = {0};
+
+ va_list args;
+ va_start(args, fmt);
+ vsnprintf(buf, sizeof buf - 1, fmt, args);
+ va_end(args);
+
+ // show a message box, and if that fails, print to stderr
+ if (SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", buf, NULL) < 0) {
+ fprintf(stderr, "%s\n", buf);
+ }
+
+ exit(EXIT_FAILURE);
+}
+
+
+int main(void) {
+ if (SDL_Init(SDL_INIT_VIDEO) < 0)
+ die("%s", SDL_GetError());
+
+ SDL_Window *window = SDL_CreateWindow("ted", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL);
+ if (!window)
+ die("%s", SDL_GetError());
+
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
+ SDL_GLContext *glctx = SDL_GL_CreateContext(window);
+ if (!glctx) {
+ die("%s", SDL_GetError());
+ }
+
+ SDL_GL_SetSwapInterval(1); // vsync
+
+
+ bool quit = false;
+ while (!quit) {
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ switch (event.type) {
+ case SDL_QUIT:
+ quit = true;
+ break;
+ }
+ }
+
+ glClearColor(0,0,0,1);
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ SDL_GL_SwapWindow(window);
+ }
+
+ return 0;
+}