summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authororangerot <orangerot@orangerot.dev>2025-10-13 21:18:09 +0200
committerorangerot <orangerot@orangerot.dev>2025-10-13 21:18:09 +0200
commitaa2be58cd30d5b9f1eb274454a9cca7a9739d62d (patch)
tree3e02bab84cba382a1b99c8e05e02b7fd70b72b78
parent78b29d66ebb049cc46aef8244e702ed7729b85cd (diff)
feat: factor out game code
-rw-r--r--Makefile2
-rw-r--r--game.c25
-rw-r--r--game.h10
-rw-r--r--main.c15
4 files changed, 44 insertions, 8 deletions
diff --git a/Makefile b/Makefile
index 77f997b..95c7f8f 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
CFLAGS := -Wall -Wextra
LDFLAGS = -lglfw -lm -lGL -I./glad/include
-domino-dungeon: main.c glad/src/glad.c
+domino-dungeon: main.c game.c game.h glad/src/glad.c
$(CC) ${CFLAGS} ${LDFLAGS} -o $@ $^
diff --git a/game.c b/game.c
new file mode 100644
index 0000000..49f2feb
--- /dev/null
+++ b/game.c
@@ -0,0 +1,25 @@
+
+#include <stdio.h>
+#include <glad/glad.h>
+#include <GLFW/glfw3.h>
+#include <math.h>
+
+#include "game.h"
+
+void character_callback(GLFWwindow* window, unsigned int codepoint) {
+ printf("%c\n", codepoint);
+}
+
+void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
+ if (action != GLFW_PRESS) return;
+ switch (key) {
+ case GLFW_KEY_ENTER:
+ break;
+ case GLFW_KEY_BACKSPACE:
+ break;
+ }
+}
+
+void draw_image(decoded_image img) {
+ for (int i = 0; i < img.buf_size; i++) img.buf[i] = i;
+}
diff --git a/game.h b/game.h
new file mode 100644
index 0000000..b016ede
--- /dev/null
+++ b/game.h
@@ -0,0 +1,10 @@
+
+#include <stddef.h>
+#include <stdint.h>
+
+typedef struct decoded_image {
+ size_t width;
+ size_t height;
+ uint32_t *buf;
+ size_t buf_size;
+} decoded_image;
diff --git a/main.c b/main.c
index 9a65e6e..d092082 100644
--- a/main.c
+++ b/main.c
@@ -4,16 +4,16 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <math.h>
+#include <stdlib.h>
+
+#include "game.h"
unsigned int SCR_WIDTH = 800;
unsigned int SCR_HEIGHT = 600;
-typedef struct decoded_image {
- size_t width;
- size_t height;
- uint32_t *buf;
- ssize_t buf_size;
-} decoded_image;
+extern void character_callback(GLFWwindow* window, unsigned int codepoint);
+extern void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
+extern void draw_image(decoded_image img);
struct decoded_image canvas = {
.width = 256,
@@ -63,7 +63,6 @@ const uint32_t COLOR_RGBA = 0xFF21FF00;
int main(int argc, const char *argv[]) {
canvas.buf = malloc(canvas.buf_size * sizeof(int));
- for (int i = 0; i < canvas.buf_size; i++) canvas.buf[i] = i;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@@ -180,6 +179,8 @@ int main(int argc, const char *argv[]) {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
+ draw_image(canvas);
+
// bind textures on corresponding texture units
glActiveTexture(GL_TEXTURE);
glBindTexture(GL_TEXTURE_2D, texture);