forked from nakidai/csnake
1
0
Fork 0

Add files

master
Nakidai 2023-10-29 20:03:23 +03:00
commit 03481db05f
Signed by untrusted user who does not match committer: nakidai
GPG Key ID: 914675D395210A97
5 changed files with 100 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
obj/
game

27
Makefile Normal file
View File

@ -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

18
include/screen.h Normal file
View File

@ -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__ */

8
src/main.c Normal file
View File

@ -0,0 +1,8 @@
#include "screen.h"
int main(int argc, char **argv)
{
Screen *screen = screenCreate(5, 5, '#');
screenShow(screen);
return 0;
}

45
src/screen.c Normal file
View File

@ -0,0 +1,45 @@
#include "screen.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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');
}
}