*
* Filename: macros.h
*
- * Description:
+ * Description: Some useful macros
*
* Version: 1.0
* Created: 09/03/2017 15:28:46
--- /dev/null
+# 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
--- /dev/null
+/*
+ * =====================================================================================
+ *
+ * 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 <ncurses.h>
+
+/* 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, "\\/");
+}
--- /dev/null
+/*
+ * =====================================================================================
+ *
+ * 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 */
--- /dev/null
+/*
+ * =====================================================================================
+ *
+ * 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 <stdlib.h>
+
+/* 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 */
--- /dev/null
+#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;
+}
--- /dev/null
+#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 */
--- /dev/null
+#include <stdlib.h>
+#include <stdio.h>
+#include <ncurses.h>
+
+#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);
+}