first commit

This commit is contained in:
sam 2024-08-11 16:18:06 +12:00
commit f9d48018a6
16 changed files with 203 additions and 0 deletions

Binary file not shown.

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
compile_commands.json
**/*.o
test/log.txt

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "src/thirdparty/log.c"]
path = src/thirdparty/log.c
url = https://github.com/rxi/log.c

12
Makefile Normal file
View file

@ -0,0 +1,12 @@
all:
$(MAKE) -C src
$(MAKE) -C test clean
$(MAKE) -C test simple.o
./test/simple.o
bear: clean
bear -- $(MAKE)
clean:
$(MAKE) -C src clean
$(MAKE) -C test clean

Binary file not shown.

22
src/Makefile Normal file
View file

@ -0,0 +1,22 @@
LIBRARY=libgearlib.a
CFLAGS=-O3 \
-I. \
-Ithirdparty \
-DUSE_GLFW \
-DLOG_USE_COLOR \
$(shell pkg-config --cflags glfw3)
CFILES=$(shell find -L * -type f -name '*.c')
OBJ=$(CFILES:.c=.o)
$(LIBRARY): $(OBJ) Makefile
$(AR) rcs $(LIBRARY) $(OBJ)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -rf $(OBJ) $(LIBRARY)

47
src/gearlib.h Normal file
View file

@ -0,0 +1,47 @@
#ifndef __GEARLIB_H__
#define __GEARLIB_H__
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <assert.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef uintptr_t uptr;
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef intptr_t iptr;
typedef float_t f32;
typedef double_t f64;
#define GRAPI extern
#define GR_DEFAULT_MAJOR 4
#define GR_DEFAULT_MINOR 6
GRAPI i32 gr_state_init();
#define gr_state_assert() assert(gr_state != NULL && "State not initialized")
#define gr_state_set(prop, val) gr_state_assert(); gr_state->prop = val
#define gr_state_get(prop) ({ gr_state_assert(); gr_state->prop; })
GRAPI i32 gr_window_init(i32 major, i32 minor);
GRAPI i32 gr_window_create(i32 width, i32 height, char* title);
GRAPI i32 gr_log_add_file(char* filename);
typedef struct {
i32 major;
i32 minor;
bool window_initialized;
void* window;
} gr_state_t;
GRAPI gr_state_t* gr_state;
#endif

BIN
src/libgearlib.a Normal file

Binary file not shown.

14
src/log.c Normal file
View file

@ -0,0 +1,14 @@
#include <gearlib.h>
#include <log.c/src/log.h>
i32 gr_log_add_file(char* filename) {
FILE* fd = fopen(filename, "w");
if(fd == NULL) {
log_error("Couldn't open file \"%s\"", filename);
return 1;
}
log_add_fp(fd, LOG_INFO);
log_info("Added file \"%s\" as logging file", filename);
return 0;
}

16
src/state.c Normal file
View file

@ -0,0 +1,16 @@
#include "log.c/src/log.h"
#include <gearlib.h>
#include <stdlib.h>
gr_state_t* gr_state = NULL;
i32 gr_state_init() {
if(gr_state != NULL) {
log_fatal("Init must only be called once");
return 1;
}
gr_state = calloc(1, sizeof(gr_state_t));
log_info("Created state");
return 0;
}

1
src/thirdparty/log.c vendored Submodule

@ -0,0 +1 @@
Subproject commit f9ea34994bd58ed342d2245cd4110bb5c6790153

49
src/window.c Normal file
View file

@ -0,0 +1,49 @@
#include <gearlib.h>
#include <GLFW/glfw3.h>
#include <log.c/src/log.h>
i32 gr_window_init(i32 major, i32 minor) {
if(gr_state->window_initialized == true) {
log_fatal("Init already called");
return 1;
}
gr_state_set(major, major);
gr_state_set(minor, minor);
gr_state_set(window_initialized, true);
log_info("Initialized for OpenGL %d.%d", major, minor);
return 0;
}
i32 gr_window_create(i32 width, i32 height, char* title) {
if(!gr_state_get(window_initialized)) {
log_info("Window was not initialized before calling create, defaulting to OpenGL %d.%d",
GR_DEFAULT_MAJOR, GR_DEFAULT_MINOR);
gr_window_init(GR_DEFAULT_MAJOR, GR_DEFAULT_MINOR);
}
#ifdef USE_GLFW
if(glfwInit() != GLFW_TRUE) {
log_fatal("GLFW failed to initialize");
} else {
log_info("GLFW initialized successfully");
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, gr_state_get(major));
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, gr_state_get(minor));
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
gr_state_set(window, glfwCreateWindow(width, height, title, NULL, NULL));
if(gr_state_get(window) == NULL) {
log_fatal("Failed to create window");
return 1;
} else {
log_info("Window created successfully");
}
#endif
return 0;
}

15
test/Makefile Normal file
View file

@ -0,0 +1,15 @@
CFLAGS=-O3 -I../src
LDFLAGS=-L../src -lgearlib $(shell pkg-config --libs glfw3)
CFILES=$(shell find -L * -type f -name '*.c')
OBJ=$(CFILES:.c=.o)
%.o: %.c
$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
all: $(OBJ)
clean:
rm -rf $(OBJ) $(BINARY)

7
test/simple-init.c Normal file
View file

@ -0,0 +1,7 @@
#include <gearlib.h>
int main() {
gr_state_init();
gr_window_init(4, 1);
gr_window_create(800, 600, "hi");
}

8
test/simple-log.c Normal file
View file

@ -0,0 +1,8 @@
#include <gearlib.h>
int main() {
gr_state_init();
gr_log_add_file("log.txt");
gr_window_create(800, 600, "hi");
}

6
test/simple.c Normal file
View file

@ -0,0 +1,6 @@
#include <gearlib.h>
int main() {
gr_state_init();
gr_window_create(800, 600, "hi");
}