From: Jérôme Benoit Date: Wed, 15 Mar 2017 22:06:02 +0000 (+0100) Subject: TP 13 exo2: Add the code structure and some basic displaying routines for the tic... X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7515f89753dd7d16c4f2a9b11003f67d8148eb09;p=TD_C.git TP 13 exo2: Add the code structure and some basic displaying routines for the tic tac toe game Signed-off-by: Jérôme Benoit --- diff --git a/TP_13/exo1/lib/macros.h b/TP_13/exo1/lib/macros.h index 4b39cf4..3c0ad02 100644 --- a/TP_13/exo1/lib/macros.h +++ b/TP_13/exo1/lib/macros.h @@ -3,7 +3,7 @@ * * Filename: macros.h * - * Description: + * Description: Some useful macros * * Version: 1.0 * Created: 09/03/2017 15:28:46 diff --git a/TP_13/exo2/Makefile b/TP_13/exo2/Makefile new file mode 100644 index 0000000..8f48f4e --- /dev/null +++ b/TP_13/exo2/Makefile @@ -0,0 +1,145 @@ +# Sample Makefile to build simple project. +# +# This Makefile expect all source files (.c) to be at the same level, in the +# $(SRC_PATH) directory. +# +# This Makefile expect all embedded library source files (.c) to be at the same level, in the +# $(LIBRARY_PATH) directory. +# +# It will automatically generate dependencies, compile all files, and produce a +# binary using the provided name linked against the library if necessary. +# +# Set BINARY_NAME to the name of the binary file to build. +# Set LIBRARY_NAME to the name of the library file to build. +# The default path for the library code and object is lib. +# By default the linker will look for $(BINARY_NAME) library name. +# Set BUILD_TYPE to either debug or release. +# +# Automatic dependencies code from: +# http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr +BINARY_NAME=exo2 +SRC_PATH:=src +LIBRARY_NAME=libexo2 +LIBRARY_PATH:=lib +LDLIBS=-lncurses -ltinfo +BUILD_TYPE=debug +#BUILD_TYPE=release + +# ==================================== +# DO NOT CHANGE STUFF BEYOND THIS LINE +# ==================================== + +all: $(BINARY_NAME) $(BINARY_NAME).dynamic $(BINARY_NAME).static + +CC=gcc +LD=gcc +AR=ar + +WARN_FLAGS = -Wall -Wextra +STD_FLAG = -std=c11 + +ifeq ($(BUILD_TYPE),debug) +BUILDDIR := .build/debug +DEBUG_FLAG = -g +STRIP_FLAG = +OPTI_FLAG = -O0 +LTO_SUPPORT = yes +GOLD_SUPPORT = yes +else +BUILDDIR := .build/release +DEBUG_FLAG = +STRIP_FLAG = -s +OPTI_FLAG = -O3 +LTO_SUPPORT = yes +GOLD_SUPPORT = yes +endif + +ifeq ($(LTO_SUPPORT),yes) +CFLAGS_LTO = -flto -ffat-lto-objects +LDFLAGS_LTO = -fuse-linker-plugin -flto +endif + +ifeq ($(GOLD_SUPPORT),yes) +LDFLAGS_GOLD = -fuse-ld=gold +endif + +# Putting header files in the source directory is not the purpose of this INCLUDES variable +INCLUDES := $(INCLUDES) -I$(LIBRARY_PATH) +CFLAGS := $(CFLAGS) $(CFLAGS_LTO) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES) +LIBCFLAGS := -fPIC $(CFLAGS) +LDFLAGS := $(LDFLAGS) $(LDFLAGS_LTO) $(LDFLAGS_GOLD) $(STRIP_FLAG) +LIBLDFLAGS := -shared $(LDFLAGS) +STATICLIBLDFLAGS := -static $(LDFLAGS) +LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH) -l$(BINARY_NAME) + +OBJDIR := $(BUILDDIR)/objs +$(shell mkdir -p $(OBJDIR)) + +SRCS=$(wildcard $(SRC_PATH)/*.c) +LIBSRCS=$(wildcard $(LIBRARY_PATH)/*.c) +OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS))) +LIBOBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(LIBSRCS))) + +DEPDIR := $(BUILDDIR)/deps +$(shell mkdir -p $(DEPDIR)) +DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td +POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d + +$(LIBRARY_PATH)/$(LIBRARY_NAME).a: $(LIBOBJS) + @echo "[AR StO] $@" + @$(AR) rcs $@ $^ + +$(LIBRARY_PATH)/$(LIBRARY_NAME).so: $(LIBOBJS) + @echo "[LD ShO] $@" + @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ -o $@ + +#$(BINARY_NAME): $(OBJS) $(LIBOBJS) +# @echo "[LD ] $@" +# @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(BINARY_NAME): $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a + @echo "[LD ] $@" + @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(BINARY_NAME).static: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a + @echo "[LD ] $@" + @$(LD) $(CFLAGS) $(STATICLIBLDFLAGS) $^ $(LDLIBS) -o $@ + +$(BINARY_NAME).dynamic: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).so + @echo "[LD ] $@" + @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(OBJDIR)/%.o: $(SRC_PATH)/%.c $(DEPDIR)/%.d + @echo "[C ] $(notdir $*)" + @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@ + @$(POSTCOMPILE) + +include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))) + +$(OBJDIR)/%.o: $(LIBRARY_PATH)/%.c $(DEPDIR)/%.d + @echo "[C ] $(notdir $*)" + @$(CC) $(DEPFLAGS) $(LIBCFLAGS) -c $< -o $@ + @$(POSTCOMPILE) + +include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(LIBSRCS)))) + +$(DEPDIR)/%.d: ; + +.PRECIOUS: $(DEPDIR)/%.d + +#FIXME: add an install target + +clean: + @echo "[CLN]" + -@rm -r $(BUILDDIR) + -@rm $(BINARY_NAME) + -@rm $(BINARY_NAME).static + -@rm $(BINARY_NAME).dynamic + -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).a + -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).so + +disassemble: $(BINARY_NAME) + objdump -d $< | less + +symbols: $(BINARY_NAME) + objdump -t $< | sort | less diff --git a/TP_13/exo2/lib/display.c b/TP_13/exo2/lib/display.c new file mode 100644 index 0000000..22deb96 --- /dev/null +++ b/TP_13/exo2/lib/display.c @@ -0,0 +1,56 @@ +/* + * ===================================================================================== + * + * Filename: display.c + * + * Description: Routines to handle the display + * + * Version: 1.0 + * Created: 15/03/2017 20:06:11 + * Revision: none + * Compiler: gcc + * + * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org + * Organization: Piment Noir + * + * ===================================================================================== + */ + +#include + +/* in all print routine, y and x are the coordinates of the first character of the shape + * which can be a space ' ' */ + +void print_board(int y, int x) { + mvprintw(y, x, " | |"); + mvprintw(y+1, x, " | |"); + mvprintw(y+2, x, "----+----+----"); + mvprintw(y+3, x, " | |"); + mvprintw(y+4, x, " | |"); + mvprintw(y+5, x, "----+----+----"); + mvprintw(y+6, x, " | |"); + mvprintw(y+7, x, " | |"); +} + +/* there's only nine valid (y, x) 2-uplets for this two shapes + * that are : - base_y, base_x +1 + * - base_y, base_x + 6 + * - base_y, base_x + 11 + * - base_y + 3, base_x + 1 + * - base_y + 6, base_x + 1 + * - base_y + 3, base_x + 6 + * - base_y + 3, base_x + 11 + * - base_y + 6, base_x + 6 + * - base_y + 6, base_x + 11 + * The added y value can be {0, 3, 6} + * The added x value can be {1, 6, 11} */ + +void print_x(int y, int x) { + mvprintw(y, x, "\\/"); + mvprintw(y+1, x,"/\\"); +} + +void print_o(int y, int x) { + mvprintw(y, x, "/\\"); + mvprintw(y+1, x, "\\/"); +} diff --git a/TP_13/exo2/lib/display.h b/TP_13/exo2/lib/display.h new file mode 100644 index 0000000..f0b1136 --- /dev/null +++ b/TP_13/exo2/lib/display.h @@ -0,0 +1,26 @@ +/* + * ===================================================================================== + * + * Filename: display.h + * + * Description: Headers for display routines + * + * Version: 1.0 + * Created: 15/03/2017 20:07:12 + * Revision: none + * Compiler: gcc + * + * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org + * Organization: Piment Noir + * + * ===================================================================================== + */ + +#ifndef DISPLAY_H +#define DISPLAY_H + +void print_board(int y, int x); +void print_x(int y, int x); +void print_o(int y, int x); + +#endif /* DISPLAY_H */ diff --git a/TP_13/exo2/lib/macros.h b/TP_13/exo2/lib/macros.h new file mode 100644 index 0000000..3c0ad02 --- /dev/null +++ b/TP_13/exo2/lib/macros.h @@ -0,0 +1,32 @@ +/* + * ===================================================================================== + * + * Filename: macros.h + * + * Description: Some useful macros + * + * Version: 1.0 + * Created: 09/03/2017 15:28:46 + * Revision: none + * Compiler: gcc + * + * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org + * Organization: Piment Noir + * + * ===================================================================================== + */ + +#ifndef MACROS_H +#define MACROS_H + +#include + +/* definition to expand macro then apply to pragma message */ +#define VALUE_TO_STRING(x) #x +#define VALUE(x) VALUE_TO_STRING(x) +#define VAR_NAME_VALUE(var) #var "=" VALUE(var) + +/* FIXME: ensure we manipulate real array */ +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) + +#endif /* MACROS_H */ diff --git a/TP_13/exo2/lib/utils.c b/TP_13/exo2/lib/utils.c new file mode 100644 index 0000000..04a20e4 --- /dev/null +++ b/TP_13/exo2/lib/utils.c @@ -0,0 +1,13 @@ +#include "utils.h" + +void swap_int(int* v1, int* v2) { + int tmp = *v1; + *v1 = *v2; + *v2 = tmp; +} + +void swap_ptr(void* v1, void* v2) { + void* tmp = v1; + v1 = v2; + v2 = tmp; +} diff --git a/TP_13/exo2/lib/utils.h b/TP_13/exo2/lib/utils.h new file mode 100644 index 0000000..03f1226 --- /dev/null +++ b/TP_13/exo2/lib/utils.h @@ -0,0 +1,9 @@ +#ifndef UTILS_H +#define UTILS_H + +#include "macros.h" + +void swap_int(int* v1, int* v2); +void swap_ptr(void* v1, void* v2); + +#endif /* UTILS_H */ diff --git a/TP_13/exo2/src/main.c b/TP_13/exo2/src/main.c new file mode 100644 index 0000000..fcd3bcc --- /dev/null +++ b/TP_13/exo2/src/main.c @@ -0,0 +1,38 @@ +#include +#include +#include + +#include "display.h" + +int main() { + int row, col; + + initscr(); + getmaxyx(stdscr,row,col); + noecho(); + curs_set(0); + + /* center base coordinates for the board */ + const int base_y = row/2 - 4; + const int base_x = col/2 - 7; + + print_board(base_y, base_x); + + print_x(base_y, base_x + 1); + print_o(base_y, base_x + 6); + print_o(base_y, base_x + 11); + print_o(base_y + 3, base_x + 1); + print_o(base_y + 6, base_x + 1); + print_o(base_y + 3, base_x + 6); + print_x(base_y + 3, base_x + 11); + print_x(base_y + 6, base_x + 6); + print_x(base_y + 6, base_x + 11); + + refresh(); + + while (getch() != 'q'); + + endwin(); + + exit(EXIT_SUCCESS); +}