feat: factor out game code
This commit is contained in:
parent
78b29d66eb
commit
aa2be58cd3
2
Makefile
2
Makefile
|
@ -1,6 +1,6 @@
|
||||||
CFLAGS := -Wall -Wextra
|
CFLAGS := -Wall -Wextra
|
||||||
LDFLAGS = -lglfw -lm -lGL -I./glad/include
|
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 $@ $^
|
$(CC) ${CFLAGS} ${LDFLAGS} -o $@ $^
|
||||||
|
|
||||||
|
|
25
game.c
Normal file
25
game.c
Normal file
|
@ -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;
|
||||||
|
}
|
10
game.h
Normal file
10
game.h
Normal file
|
@ -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;
|
15
main.c
15
main.c
|
@ -4,16 +4,16 @@
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "game.h"
|
||||||
|
|
||||||
unsigned int SCR_WIDTH = 800;
|
unsigned int SCR_WIDTH = 800;
|
||||||
unsigned int SCR_HEIGHT = 600;
|
unsigned int SCR_HEIGHT = 600;
|
||||||
|
|
||||||
typedef struct decoded_image {
|
extern void character_callback(GLFWwindow* window, unsigned int codepoint);
|
||||||
size_t width;
|
extern void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
||||||
size_t height;
|
extern void draw_image(decoded_image img);
|
||||||
uint32_t *buf;
|
|
||||||
ssize_t buf_size;
|
|
||||||
} decoded_image;
|
|
||||||
|
|
||||||
struct decoded_image canvas = {
|
struct decoded_image canvas = {
|
||||||
.width = 256,
|
.width = 256,
|
||||||
|
@ -63,7 +63,6 @@ const uint32_t COLOR_RGBA = 0xFF21FF00;
|
||||||
int main(int argc, const char *argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
|
|
||||||
canvas.buf = malloc(canvas.buf_size * sizeof(int));
|
canvas.buf = malloc(canvas.buf_size * sizeof(int));
|
||||||
for (int i = 0; i < canvas.buf_size; i++) canvas.buf[i] = i;
|
|
||||||
|
|
||||||
glfwInit();
|
glfwInit();
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
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);
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
draw_image(canvas);
|
||||||
|
|
||||||
// bind textures on corresponding texture units
|
// bind textures on corresponding texture units
|
||||||
glActiveTexture(GL_TEXTURE);
|
glActiveTexture(GL_TEXTURE);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
|
|
Loading…
Reference in a new issue