From 03481db05f00aefc4b111f34553ce57aed72c9a8 Mon Sep 17 00:00:00 2001 From: Nakidai Date: Sun, 29 Oct 2023 20:03:23 +0300 Subject: [PATCH] Add files --- .gitignore | 2 ++ Makefile | 27 +++++++++++++++++++++++++++ include/screen.h | 18 ++++++++++++++++++ src/main.c | 8 ++++++++ src/screen.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 include/screen.h create mode 100644 src/main.c create mode 100644 src/screen.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0fd9bb7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +obj/ +game diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..65dc3a9 --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +OUT = game +CFLAGS = +LDFLAGS = +INCLUDE = -Iinclude +CC = cc +LD = ld +RM = rm -f +SRCDIR = src +OBJDIR = obj +SRC = main.c screen.c +OBJ = $(addprefix $(OBJDIR)/,$(SRC:.c=.o)) + +default: $(OUT) + +obj: + mkdir obj + +$(OBJDIR)/%.o: $(SRCDIR)/%.c + $(CC) -c -o $@ $< $(CFLAGS) $(INCLUDE) + +$(OUT): obj $(OBJ) + $(CC) -o $@ $(OBJ) $(LDFLAGS) + +clean: + $(RM) $(OUT) $(OBJDIR)/* + +.PHONY: default clean diff --git a/include/screen.h b/include/screen.h new file mode 100644 index 0000000..d4be528 --- /dev/null +++ b/include/screen.h @@ -0,0 +1,18 @@ +#ifndef __SCREEN_H__ +#define __SCREEN_H__ + +typedef struct screen_t +{ + int width; + int height; + char *screen; +} Screen; +typedef char Point; + +Screen *screenCreate(int width, int height, Point fill_value); +void screenFree(Screen *screen); + +Point *screenGetPoint(Screen *screen, int x, int y); +void screenShow(Screen *screen); + +#endif /* __SCREEN_H__ */ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..871aeb6 --- /dev/null +++ b/src/main.c @@ -0,0 +1,8 @@ +#include "screen.h" + +int main(int argc, char **argv) +{ + Screen *screen = screenCreate(5, 5, '#'); + screenShow(screen); + return 0; +} diff --git a/src/screen.c b/src/screen.c new file mode 100644 index 0000000..ebd5726 --- /dev/null +++ b/src/screen.c @@ -0,0 +1,45 @@ +#include "screen.h" +#include +#include +#include + +Screen *screenCreate(int width, int height, Point fill_value) +{ + Point *screen = malloc(width * height * sizeof(Point)); + memset(screen, fill_value, width * height * sizeof(Point)); + + Screen *out = malloc(sizeof(Screen)); + out->width = width; + out->height = height; + out->screen = screen; + return out; +} + +void screenFree(Screen *screen) +{ + free(screen->screen); + free(screen); +} + +Point *screenGetPoint(Screen *screen, int x, int y) +{ + return screen->screen + x + (y * screen->width); +} + +void screenShow(Screen *screen) +{ + int x, y, i; + int width = screen->width; + int height = screen->height; + Point point; + + for (y = 0; y < height; ++y) + { + for (x = 0; x < width; ++x) + { + point = *screenGetPoint(screen, x, y); + for (i = 0; i < 2; ++i) putchar(point); + } + putchar('\n'); + } +}