From 0eb4680c484b3006f5defc7532d2542b7ff7b783 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 10 Apr 2024 10:39:10 +0200 Subject: [PATCH 1/2] Demo static lib --- libadd/add.c | 1 + libadd/include/add.h | 6 ++++++ libadd/makefile | 34 ++++++++++++++++++++++++++++++++++ makefile | 11 ++++++++--- src/main.c | 2 ++ 5 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 libadd/add.c create mode 100644 libadd/include/add.h create mode 100644 libadd/makefile diff --git a/libadd/add.c b/libadd/add.c new file mode 100644 index 0000000..1f76448 --- /dev/null +++ b/libadd/add.c @@ -0,0 +1 @@ +int add(int a, int b) { return a + b; } diff --git a/libadd/include/add.h b/libadd/include/add.h new file mode 100644 index 0000000..465d504 --- /dev/null +++ b/libadd/include/add.h @@ -0,0 +1,6 @@ +#ifndef ADD_H +#define ADD_H + +int add(int a, int b); + +#endif // ADD_H \ No newline at end of file diff --git a/libadd/makefile b/libadd/makefile new file mode 100644 index 0000000..58c02a9 --- /dev/null +++ b/libadd/makefile @@ -0,0 +1,34 @@ +# Makefile for the add library +# Builds a static library from the add.c file + +# Library name +LIB = libadd.a + +# Compiler +CC = gcc + +# Compiler flags +CFLAGS = -Wall -Werror -Wextra -pedantic +CFLAGS += -std=c99 -O3 + +# Source file +SRC = add.c + +# Object file +OBJ = $(SRC:.c=.o) + +# Build the library +$(LIB): $(OBJ) + ar rcs $(LIB) $(OBJ) + +# Build the object file +$(OBJ): $(SRC) + $(CC) $(CFLAGS) -c $(SRC) + +format: + clang-format -i *.c include/*.h + +# Clean the object files +clean: + rm -f $(OBJ) + rm -f $(LIB) diff --git a/makefile b/makefile index 419b675..cee5329 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ -# Compiler and assembler +#dd Compiler and assembler CC = gcc AS = as LD = ld @@ -20,6 +20,7 @@ CFLAGS += -Wno-unused-local-typedefs CFLAGS += -Wno-unused-const-variable CFLAGS += -Wno-unused-macros CFLAGS += -O3 +CFLAGS += -Ilibadd/include CFLAGS += -g GITHASH = $(shell git rev-parse --short HEAD) @@ -36,6 +37,9 @@ OBJS := $(patsubst src/%.c,build/%.o,$(SRCS)) all: $(TARGET) +libadd/libadd.a: + $(MAKE) -C libadd + # For convenience run: $(TARGET) @./$(TARGET) @@ -45,7 +49,7 @@ mkbuilddir: @mkdir -p build # Link the object files into the target binary -$(TARGET): $(OBJS) +$(TARGET): $(OBJS) libadd/libadd.a @$(CC) $(CFLAGS) -o $@ $^ @echo -e "LD \t$^" @@ -67,6 +71,7 @@ clean: rm -r build rm -f build/*.o rm -f $(TARGET)* + $(MAKE) -C libadd clean --no-print-directory # Create a signed release tar: $(TARGET_TAR) @@ -81,4 +86,4 @@ $(TARGET_TAR): $(TARGET) strip $< tar --zstd -cvf $@ $< -.PHONY: all clean size mkbuilddir run tar sign \ No newline at end of file +.PHONY: all clean size mkbuilddir run tar sign diff --git a/src/main.c b/src/main.c index d0c89ee..459570f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,8 @@ +#include #include int main(int argc, const char **argv) { printf("Hello, World!\n"); + printf("add(1, 2) = %d\n", add(1, 2)); return 0; } From a2068517ec8c55fed7123c6fa2ba648d438bdeed Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Tue, 16 Apr 2024 18:10:39 +0200 Subject: [PATCH 2/2] Cube --- .gitignore | 4 +- makefile | 4 ++ src/main.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 111 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 42afabf..c1c1d76 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -/build \ No newline at end of file +/build +*.a +*.o \ No newline at end of file diff --git a/makefile b/makefile index cee5329..9e82b8b 100644 --- a/makefile +++ b/makefile @@ -21,6 +21,10 @@ CFLAGS += -Wno-unused-const-variable CFLAGS += -Wno-unused-macros CFLAGS += -O3 CFLAGS += -Ilibadd/include +CFLAGS += -lm +CFLAGS += -lglfw +CFLAGS += -lGL +CFLAGS += -lGLU CFLAGS += -g GITHASH = $(shell git rev-parse --short HEAD) diff --git a/src/main.c b/src/main.c index 459570f..4b4af4c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,107 @@ -#include +#define GLFW_INCLUDE_NONE +#include +#include +#include +#include #include -int main(int argc, const char **argv) { - printf("Hello, World!\n"); - printf("add(1, 2) = %d\n", add(1, 2)); - return 0; +#define WIDTH 800 +#define HEIGHT 600 + +// Function to draw the cube +void drawCube() { + glBegin(GL_QUADS); + + // Front face + glColor3f(1.0f, 0.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, 1.0f); + glVertex3f(1.0f, -1.0f, 1.0f); + glVertex3f(1.0f, 1.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, 1.0f); + + // Back face + glColor3f(0.0f, 1.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, -1.0f); + glVertex3f(-1.0f, 1.0f, -1.0f); + glVertex3f(1.0f, 1.0f, -1.0f); + glVertex3f(1.0f, -1.0f, -1.0f); + + // Top face + glColor3f(0.0f, 0.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, -1.0f); + glVertex3f(-1.0f, 1.0f, 1.0f); + glVertex3f(1.0f, 1.0f, 1.0f); + glVertex3f(1.0f, 1.0f, -1.0f); + + // Bottom face + glColor3f(1.0f, 1.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, -1.0f); + glVertex3f(1.0f, -1.0f, -1.0f); + glVertex3f(1.0f, -1.0f, 1.0f); + glVertex3f(-1.0f, -1.0f, 1.0f); + + // Right face + glColor3f(1.0f, 0.0f, 1.0f); + glVertex3f(1.0f, -1.0f, -1.0f); + glVertex3f(1.0f, 1.0f, -1.0f); + glVertex3f(1.0f, 1.0f, 1.0f); + glVertex3f(1.0f, -1.0f, 1.0f); + + // Left face + glColor3f(0.0f, 1.0f, 1.0f); + glVertex3f(-1.0f, -1.0f, -1.0f); + glVertex3f(-1.0f, -1.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, -1.0f); + + glEnd(); +} + +int main() { + // Initialize GLFW + if (!glfwInit()) { + fprintf(stderr, "Failed to initialize GLFW\n"); + return -1; + } + + // Create a windowed mode window and its OpenGL context + GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Rotating Cube", NULL, NULL); + if (!window) { + glfwTerminate(); + return -1; + } + + // Make the window's context current + glfwMakeContextCurrent(window); + + // Loop until the user closes the window + while (!glfwWindowShouldClose(window)) { + // Clear the framebuffer + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // Set up the view + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(45.0f, (float)WIDTH / (float)HEIGHT, 0.1f, 100.0f); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + gluLookAt(3, 3, 3, 0, 0, 0, 0, 1, 0); // Eye position, look at position, up direction + + // Rotate the cube + static double angle = 0; + glRotated(angle, 0, 1, 1); // Rotate around x, y, and z axes + angle += 0.5; + + // Draw the cube + drawCube(); + + // Swap front and back buffers + glfwSwapBuffers(window); + + // Poll for and process events + glfwPollEvents(); + } + + glfwTerminate(); + return 0; }