瀏覽代碼

Start implementing a full <conio.h> replacement with a libcaca backend.

tags/v0.99.beta17
Sam Hocevar sam 15 年之前
父節點
當前提交
104c5a0c5b
共有 7 個檔案被更改,包括 1454 行新增119 行删除
  1. +119
    -117
      caca/Makefile.am
  2. +392
    -0
      caca/caca_conio.c
  3. +255
    -0
      caca/caca_conio.h
  4. +1
    -1
      configure.ac
  5. +1
    -0
      examples/.gitignore
  6. +4
    -1
      examples/Makefile.am
  7. +682
    -0
      examples/conio.c

+ 119
- 117
caca/Makefile.am 查看文件

@@ -1,117 +1,119 @@
# $Id$
EXTRA_DIST = caca_types.h.in caca.pc.in \
mono9.data monobold12.data \
libcaca.vcproj libcaca.def
DISTCLEANFILES = caca_types.h caca.pc
AM_CPPFLAGS = -I$(top_srcdir) -DPLUGINDIR=\"$(plugindir)\"
pkgconfig_DATA = caca.pc
pkgconfigdir = $(libdir)/pkgconfig
include_HEADERS = caca.h caca_types.h caca0.h
lib_LTLIBRARIES = libcaca.la
libcaca_la_SOURCES = \
caca.c \
caca.h \
caca_types.h \
caca_internals.h \
caca_stubs.h \
caca0.c \
caca0.h \
canvas.c \
dirty.c \
string.c \
legacy.c \
transform.c \
charset.c \
attr.c \
line.c \
box.c \
conic.c \
triangle.c \
frame.c \
dither.c \
font.c \
file.c \
figfont.c \
graphics.c \
event.c \
time.c \
$(codec_source) \
$(driver_source) \
$(NULL)
libcaca_la_DEPENDENCIES = \
mono9.data \
monobold12.data \
$(NULL)
libcaca_la_CPPFLAGS = $(AM_CPPFLAGS) @CACA_CFLAGS@ -D__LIBCACA__
libcaca_la_LDFLAGS = -no-undefined -version-number @LT_VERSION@
libcaca_la_LIBADD = @CACA_LIBS@ $(ZLIB_LIBS)
codec_source = \
codec/import.c \
codec/export.c \
codec/codec.h \
codec/text.c \
$(NULL)
driver_source = \
driver/conio.c \
driver/ncurses.c \
driver/null.c \
driver/raw.c \
driver/slang.c \
driver/vga.c \
driver/win32.c \
$(NULL)
install-exec-local:
$(mkinstalldirs) $(DESTDIR)$(libdir)
. libcaca.la; \
for x in $$library_names; do \
ln -sf $$x $(DESTDIR)$(libdir)/$$(echo $$x | sed 's/caca/cucul/g'); \
done
ln -sf libcaca.la $(DESTDIR)$(libdir)/libcucul.la
install-data-local:
$(mkinstalldirs) $(DESTDIR)$(pkgconfigdir)
ln -sf caca.pc $(DESTDIR)$(pkgconfigdir)/cucul.pc
$(mkinstalldirs) $(DESTDIR)$(includedir)
ln -sf caca.h $(DESTDIR)$(includedir)/cucul.h
uninstall-local:
. libcaca.la; \
for x in $$library_names; do \
rm -f $(DESTDIR)$(libdir)/$$(echo $$x | sed 's/caca/cucul/g'); \
done
rm -f $(DESTDIR)$(libdir)/libcucul.la
rmdir $(DESTDIR)$(libdir) 2>/dev/null || true
rm -f $(DESTDIR)$(pkgconfigdir)/cucul.pc
rmdir $(DESTDIR)$(pkgconfigdir) 2>/dev/null || true
rm -f $(DESTDIR)$(includedir)/cucul.h
if USE_PLUGINS
plugin_LTLIBRARIES = libx11_plugin.la libgl_plugin.la
plugindir = $(libdir)/caca
libx11_plugin_la_SOURCES = driver/x11.c
libx11_plugin_la_CPPFLAGS = $(AM_CPPFLAGS) $(X11_CFLAGS)
libx11_plugin_la_LDFLAGS = -no-undefined -module -no-version
libx11_plugin_la_LIBADD = libcaca.la $(X11_LIBS)
libgl_plugin_la_SOURCES = driver/gl.c
libgl_plugin_la_CPPFLAGS = $(AM_CPPFLAGS) $(GL_CFLAGS) #$(FTGL_CFLAGS)
libgl_plugin_la_LDFLAGS = -no-undefined -module -no-version
libgl_plugin_la_LIBADD = libcaca.la $(GL_LIBS) #$(FTGL_LIBS)
else
driver_source += driver/x11.c driver/gl.c
endif
if USE_COCOA
driver_source += driver/cocoa.m
endif
# $Id$

EXTRA_DIST = caca_types.h.in caca.pc.in \
mono9.data monobold12.data \
libcaca.vcproj libcaca.def
DISTCLEANFILES = caca_types.h caca.pc

AM_CPPFLAGS = -I$(top_srcdir) -DPLUGINDIR=\"$(plugindir)\"

pkgconfig_DATA = caca.pc
pkgconfigdir = $(libdir)/pkgconfig

include_HEADERS = caca.h caca_types.h caca_conio.h caca0.h

lib_LTLIBRARIES = libcaca.la

libcaca_la_SOURCES = \
caca.c \
caca.h \
caca_types.h \
caca_internals.h \
caca_stubs.h \
caca_conio.c \
caca_conio.h \
caca0.c \
caca0.h \
canvas.c \
dirty.c \
string.c \
legacy.c \
transform.c \
charset.c \
attr.c \
line.c \
box.c \
conic.c \
triangle.c \
frame.c \
dither.c \
font.c \
file.c \
figfont.c \
graphics.c \
event.c \
time.c \
$(codec_source) \
$(driver_source) \
$(NULL)
libcaca_la_DEPENDENCIES = \
mono9.data \
monobold12.data \
$(NULL)
libcaca_la_CPPFLAGS = $(AM_CPPFLAGS) @CACA_CFLAGS@ -D__LIBCACA__
libcaca_la_LDFLAGS = -no-undefined -version-number @LT_VERSION@
libcaca_la_LIBADD = @CACA_LIBS@ $(ZLIB_LIBS)

codec_source = \
codec/import.c \
codec/export.c \
codec/codec.h \
codec/text.c \
$(NULL)

driver_source = \
driver/conio.c \
driver/ncurses.c \
driver/null.c \
driver/raw.c \
driver/slang.c \
driver/vga.c \
driver/win32.c \
$(NULL)

install-exec-local:
$(mkinstalldirs) $(DESTDIR)$(libdir)
. libcaca.la; \
for x in $$library_names; do \
ln -sf $$x $(DESTDIR)$(libdir)/$$(echo $$x | sed 's/caca/cucul/g'); \
done
ln -sf libcaca.la $(DESTDIR)$(libdir)/libcucul.la

install-data-local:
$(mkinstalldirs) $(DESTDIR)$(pkgconfigdir)
ln -sf caca.pc $(DESTDIR)$(pkgconfigdir)/cucul.pc
$(mkinstalldirs) $(DESTDIR)$(includedir)
ln -sf caca.h $(DESTDIR)$(includedir)/cucul.h

uninstall-local:
. libcaca.la; \
for x in $$library_names; do \
rm -f $(DESTDIR)$(libdir)/$$(echo $$x | sed 's/caca/cucul/g'); \
done
rm -f $(DESTDIR)$(libdir)/libcucul.la
rmdir $(DESTDIR)$(libdir) 2>/dev/null || true
rm -f $(DESTDIR)$(pkgconfigdir)/cucul.pc
rmdir $(DESTDIR)$(pkgconfigdir) 2>/dev/null || true
rm -f $(DESTDIR)$(includedir)/cucul.h

if USE_PLUGINS
plugin_LTLIBRARIES = libx11_plugin.la libgl_plugin.la
plugindir = $(libdir)/caca

libx11_plugin_la_SOURCES = driver/x11.c
libx11_plugin_la_CPPFLAGS = $(AM_CPPFLAGS) $(X11_CFLAGS)
libx11_plugin_la_LDFLAGS = -no-undefined -module -no-version
libx11_plugin_la_LIBADD = libcaca.la $(X11_LIBS)

libgl_plugin_la_SOURCES = driver/gl.c
libgl_plugin_la_CPPFLAGS = $(AM_CPPFLAGS) $(GL_CFLAGS) #$(FTGL_CFLAGS)
libgl_plugin_la_LDFLAGS = -no-undefined -module -no-version
libgl_plugin_la_LIBADD = libcaca.la $(GL_LIBS) #$(FTGL_LIBS)
else
driver_source += driver/x11.c driver/gl.c
endif

if USE_COCOA
driver_source += driver/cocoa.m
endif


+ 392
- 0
caca/caca_conio.c 查看文件

@@ -0,0 +1,392 @@
/*
* libcaca Colour ASCII-Art library
* Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
* All Rights Reserved
*
* $Id$
*
* This library is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

/*
* This file contains a full conio.h reimplementation. More information
* on conio.h can be found on http://poli.cs.vsb.cz/c/help/conio.htm
*/

#include "config.h"

#if !defined(__KERNEL__)
# include <stdio.h>
# include <stdlib.h>
#endif

#include "caca.h"
#include "caca_internals.h"
#include "caca_conio.h"

static caca_canvas_t *cv;
static caca_display_t *dp;

static int unget_ch = -1;
static char pass_buffer[BUFSIZ];
static char cgets_buffer[BUFSIZ];

static void conio_init(void);
static void conio_refresh(void);
static void conio_fini(void);

/** \brief DOS conio.h cgets() equivalent */
char * caca_conio_cgets(char *str)
{
conio_init();

/* TODO: implement this function */
cgets_buffer[0] = '\0';

return cgets_buffer;
}

/** \brief DOS conio.h clreol() equivalent */
void caca_conio_clreol(void)
{
conio_init();

/* TODO: implement this function */
}

/** \brief DOS conio.h clrscr() equivalent */
void caca_conio_clrscr(void)
{
conio_init();

caca_clear_canvas(cv);

conio_refresh();
}

/** \brief DOS conio.h cprintf() equivalent */
int caca_conio_cprintf(const char *format, ...)
{
va_list args;

conio_init();

va_start(args, format);
caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args);
va_end(args);

conio_refresh();

/* FIXME: we should fix caca_vprintf so that it returns the number of
* characters that were printed. */
return 0;
}

/** \brief DOS conio.h cputs() equivalent */
int caca_conio_cputs(const char *str)
{
conio_init();

/* TODO: implement this function */

return 0;
}

/** \brief DOS conio.h cscanf() equivalent */
int caca_conio_cscanf(char *format, ...)
{
conio_init();

/* TODO: implement this function */

return 0;
}

/** \brief DOS conio.h delline() equivalent */
void caca_conio_delline(void)
{
conio_init();

/* TODO: implement this function */
}

/** \brief DOS conio.h getch() equivalent */
int caca_conio_getch(void)
{
caca_event_t ev;
int ch;

conio_init();

if(unget_ch >= 0)
{
int tmp = unget_ch;
unget_ch = -1;
return tmp;
}

caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1);
ch = caca_get_event_key_ch(&ev);

switch(ch)
{
case CACA_KEY_LEFT: ch = 75; break;
case CACA_KEY_RIGHT: ch = 77; break;
default: break;
}

return ch;
}

/** \brief DOS conio.h getche() equivalent */
int caca_conio_getche(void)
{
conio_init();

/* TODO: implement this function */

return 0;
}

/** \brief DOS conio.h getpass() equivalent */
char * caca_conio_getpass(const char *prompt)
{
conio_init();

/* TODO: implement this function */
pass_buffer[0] = '\0';

return pass_buffer;
}

/** \brief DOS conio.h gettext() equivalent */
int caca_conio_gettext(int left, int top, int right, int bottom, void *destin)
{
conio_init();

/* TODO: implement this function */

return 0;
}

/** \brief DOS conio.h gettextinfo() equivalent */
void caca_conio_gettextinfo(struct caca_conio_text_info *r)
{
conio_init();

/* TODO: implement this function */
}

/** \brief DOS conio.h gotoxy() equivalent */
void caca_conio_gotoxy(int x, int y)
{
conio_init();

caca_gotoxy(cv, x - 1, y - 1);

conio_refresh();
}

/** \brief DOS conio.h highvideo() equivalent */
void caca_conio_highvideo(void)
{
conio_init();

/* TODO: implement this function */
}

/** \brief DOS conio.h insline() equivalent */
void caca_conio_insline(void)
{
conio_init();

/* TODO: implement this function */
}

/** \brief DOS conio.h kbhit() equivalent */
int caca_conio_kbhit(void)
{
conio_init();

/* TODO: implement this function */

return 0;
}

/** \brief DOS conio.h lowvideo() equivalent */
void caca_conio_lowvideo(void)
{
conio_init();

/* TODO: implement this function */
}

/** \brief DOS conio.h movetext() equivalent */
int caca_conio_movetext(int left, int top, int right, int bottom,
int destleft, int desttop)
{
conio_init();

/* TODO: implement this function */

return 0;
}

/** \brief DOS conio.h normvideo() equivalent */
void caca_conio_normvideo(void)
{
conio_init();

/* TODO: implement this function */
}

/** \brief DOS stdio.h printf() equivalent */
int caca_conio_printf(const char *format, ...)
{
va_list args;

conio_init();

va_start(args, format);
caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args);
va_end(args);

conio_refresh();

/* FIXME: we should fix caca_vprintf so that it returns the number of
* characters that were printed. */
return 0;
}

/** \brief DOS conio.h putch() equivalent */
int caca_conio_putch(int ch)
{
conio_init();

/* TODO: implement this function */

return 0;
}

/** \brief DOS conio.h puttext() equivalent */
int caca_conio_puttext(int left, int top, int right, int bottom, void *destin)
{
conio_init();

/* TODO: implement this function */

return 0;
}

/** \brief DOS conio.h _setcursortype() equivalent */
void caca_conio__setcursortype(int cur_t)
{
conio_init();

/* TODO: implement this function */
}

/** \brief DOS conio.h textattr() equivalent */
void caca_conio_textattr(int newattr)
{
conio_init();

/* TODO: implement this function */
}

/** \brief DOS conio.h textbackground() equivalent */
void caca_conio_textbackground(int newcolor)
{
conio_init();

caca_set_color_ansi(cv, caca_attr_to_ansi_fg(caca_get_attr(cv, -1, -1)),
newcolor);
}

/** \brief DOS conio.h textcolor() equivalent */
void caca_conio_textcolor(int newcolor)
{
conio_init();

caca_set_color_ansi(cv, newcolor,
caca_attr_to_ansi_bg(caca_get_attr(cv, -1, -1)));
}

/** \brief DOS conio.h textmode() equivalent */
void caca_conio_textmode(int newmode)
{
conio_init();

/* TODO: implement this function */
}

/** \brief DOS conio.h ungetch() equivalent */
int caca_conio_ungetch(int ch)
{
conio_init();

if(unget_ch >= 0)
return EOF;

unget_ch = ch;

return ch;
}

/** \brief DOS conio.h wherex() equivalent */
int caca_conio_wherex(void)
{
conio_init();

return caca_wherex(cv) + 1;
}

/** \brief DOS conio.h wherey() equivalent */
int caca_conio_wherey(void)
{
conio_init();

return caca_wherey(cv) + 1;
}

/** \brief DOS conio.h window() equivalent */
void caca_conio_window(int left, int top, int right, int bottom)
{
conio_init();

/* TODO: implement this function */
}

/* XXX: the following functions are local. */

static void conio_init(void)
{
if(!cv)
cv = caca_create_canvas(80, 25);
if(!dp)
{
dp = caca_create_display(cv);
caca_set_cursor(dp, 1);
#if defined HAVE_ATEXIT
atexit(conio_fini);
#endif
}
}

static void conio_refresh(void)
{
caca_refresh_display(dp);
}

static void conio_fini(void)
{
caca_free_display(dp);
dp = NULL;
caca_free_canvas(cv);
cv = NULL;
}


+ 255
- 0
caca/caca_conio.h 查看文件

@@ -0,0 +1,255 @@
/*
* libcaca Colour ASCII-Art library
* Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
* All Rights Reserved
*
* $Id$
*
* This library is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

/*
* This header contains a conio.h reimplementation.
*/

#ifndef __CACA_CONIO_H__
#define __CACA_CONIO_H__

#include <caca.h>

#if !defined(__KERNEL__)
# include <stdio.h>
#endif

#undef __extern
#if defined(_DOXYGEN_SKIP_ME)
#elif defined(_WIN32) && defined(__LIBCACA__)
# define __extern extern __declspec(dllexport)
#else
# define __extern extern
#endif

#ifdef __cplusplus
extern "C"
{
#endif

/* conio.h defines and global variables */
#define CACA_CONIO_BLINK 128
#define CACA_CONIO_BLACK 0
#define CACA_CONIO_BLUE 1
#define CACA_CONIO_GREEN 2
#define CACA_CONIO_CYAN 3
#define CACA_CONIO_RED 4
#define CACA_CONIO_MAGENTA 5
#define CACA_CONIO_BROWN 6
#define CACA_CONIO_LIGHTGRAY 7
#define CACA_CONIO_DARKGRAY 8
#define CACA_CONIO_LIGHTBLUE 9
#define CACA_CONIO_LIGHTGREEN 10
#define CACA_CONIO_LIGHTCYAN 11
#define CACA_CONIO_LIGHTRED 12
#define CACA_CONIO_LIGHTMAGENTA 13
#define CACA_CONIO_YELLOW 14
#define CACA_CONIO_WHITE 15
__extern int caca_conio_directvideo;
#define CACA_CONIO__NOCURSOR 0
#define CACA_CONIO__SOLIDCURSOR 1
#define CACA_CONIO__NORMALCURSOR 2
struct caca_conio_text_info
{
unsigned char winleft; /* left window coordinate */
unsigned char wintop; /* top window coordinate */
unsigned char winright; /* right window coordinate */
unsigned char winbottom; /* bottom window coordinate */
unsigned char attribute; /* text attribute */
unsigned char normattr; /* normal attribute */
unsigned char currmode; /* current video mode:
BW40, BW80, C40, C80, or C4350 */
unsigned char screenheight; /* text screen's height */
unsigned char screenwidth; /* text screen's width */
unsigned char curx; /* x-coordinate in current window */
unsigned char cury; /* y-coordinate in current window */
};
#define CACA_CONIO_LASTMODE -1
#define CACA_CONIO_BW40 0
#define CACA_CONIO_C40 1
#define CACA_CONIO_BW80 2
#define CACA_CONIO_C80 3
#define CACA_CONIO_MONO 7
#define CACA_CONIO_C4350 64
__extern int caca_conio__wscroll;

#if !defined _DOXYGEN_SKIP_ME && !defined __LIBCACA__
# undef BLINK
# define BLINK CACA_CONIO_BLINK
# undef BLACK
# define BLACK CACA_CONIO_BLACK
# undef BLUE
# define BLUE CACA_CONIO_BLUE
# undef GREEN
# define GREEN CACA_CONIO_GREEN
# undef CYAN
# define CYAN CACA_CONIO_CYAN
# undef RED
# define RED CACA_CONIO_RED
# undef MAGENTA
# define MAGENTA CACA_CONIO_MAGENTA
# undef BROWN
# define BROWN CACA_CONIO_BROWN
# undef LIGHTGRAY
# define LIGHTGRAY CACA_CONIO_LIGHTGRAY
# undef DARKGRAY
# define DARKGRAY CACA_CONIO_DARKGRAY
# undef LIGHTBLUE
# define LIGHTBLUE CACA_CONIO_LIGHTBLUE
# undef LIGHTGREEN
# define LIGHTGREEN CACA_CONIO_LIGHTGREEN
# undef LIGHTCYAN
# define LIGHTCYAN CACA_CONIO_LIGHTCYAN
# undef LIGHTRED
# define LIGHTRED CACA_CONIO_LIGHTRED
# undef LIGHTMAGENTA
# define LIGHTMAGENTA CACA_CONIO_LIGHTMAGENTA
# undef YELLOW
# define YELLOW CACA_CONIO_YELLOW
# undef WHITE
# define WHITE CACA_CONIO_WHITE
# undef directvideo
# define directvideo caca_conio_directvideo
# undef _NOCURSOR
# define _NOCURSOR CACA_CONIO__NOCURSOR
# undef _SOLIDCURSOR
# define _SOLIDCURSOR CACA_CONIO__SOLIDCURSOR
# undef _NORMALCURSOR
# define _NORMALCURSOR CACA_CONIO__NORMALCURSOR
# undef text_info
# define text_info caca_conio_text_info
# undef LASTMODE
# define LASTMODE CACA_CONIO_LASTMODE
# undef BW40
# define BW40 CACA_CONIO_BW40
# undef C40
# define C40 CACA_CONIO_C40
# undef BW80
# define BW80 CACA_CONIO_BW80
# undef C80
# define C80 CACA_CONIO_C80
# undef MONO
# define MONO CACA_CONIO_MONO
# undef C4350
# define C4350 CACA_CONIO_C4350
# undef _wscroll
# define _wscroll caca_conio__wscroll
#endif

/* conio.h functions */
__extern char * caca_conio_cgets(char *str);
__extern void caca_conio_clreol(void);
__extern void caca_conio_clrscr(void);
__extern int caca_conio_cprintf(const char *format, ...);
__extern int caca_conio_cputs(const char *str);
__extern int caca_conio_cscanf(char *format, ...);
__extern void caca_conio_delline(void);
__extern int caca_conio_getch(void);
__extern int caca_conio_getche(void);
__extern char * caca_conio_getpass(const char *prompt);
__extern int caca_conio_gettext(int left, int top, int right, int bottom,
void *destin);
__extern void caca_conio_gettextinfo(struct caca_conio_text_info *r);
__extern void caca_conio_gotoxy(int x, int y);
__extern void caca_conio_highvideo(void);
__extern void caca_conio_insline(void);
__extern int caca_conio_kbhit(void);
__extern void caca_conio_lowvideo(void);
__extern int caca_conio_movetext(int left, int top, int right, int bottom,
int destleft, int desttop);
__extern void caca_conio_normvideo(void);
__extern int caca_conio_printf(const char *format, ...);
__extern int caca_conio_putch(int ch);
__extern int caca_conio_puttext(int left, int top, int right, int bottom,
void *destin);
__extern void caca_conio__setcursortype(int cur_t);
__extern void caca_conio_textattr(int newattr);
__extern void caca_conio_textbackground(int newcolor);
__extern void caca_conio_textcolor(int newcolor);
__extern void caca_conio_textmode(int newmode);
__extern int caca_conio_ungetch(int ch);
__extern int caca_conio_wherex(void);
__extern int caca_conio_wherey(void);
__extern void caca_conio_window(int left, int top, int right, int bottom);

#if !defined _DOXYGEN_SKIP_ME && !defined __LIBCACA__
# undef cgets
# define cgets caca_conio_cgets
# undef clreol
# define clreol caca_conio_clreol
# undef clrscr
# define clrscr caca_conio_clrscr
# undef cprintf
# define cprintf caca_conio_cprintf
# undef cputs
# define cputs caca_conio_cputs
# undef cscanf
# define cscanf caca_conio_cscanf
# undef delline
# define delline caca_conio_delline
# undef getch
# define getch caca_conio_getch
# undef getche
# define getche caca_conio_getche
# undef getpass
# define getpass caca_conio_getpass
# undef gettext
# define gettext caca_conio_gettext
# undef gettextinfo
# define gettextinfo caca_conio_gettextinfo
# undef gotoxy
# define gotoxy caca_conio_gotoxy
# undef highvideo
# define highvideo caca_conio_highvideo
# undef insline
# define insline caca_conio_insline
# undef kbhit
# define kbhit caca_conio_kbhit
# undef lowvideo
# define lowvideo caca_conio_lowvideo
# undef movetext
# define movetext caca_conio_movetext
# undef normvideo
# define normvideo caca_conio_normvideo
# undef printf
# define printf caca_conio_printf
# undef putch
# define putch caca_conio_putch
# undef puttext
# define puttext caca_conio_puttext
# undef _setcursortype
# define _setcursortype caca_conio__setcursortype
# undef textattr
# define textattr caca_conio_textattr
# undef textbackground
# define textbackground caca_conio_textbackground
# undef textcolor
# define textcolor caca_conio_textcolor
# undef textmode
# define textmode caca_conio_textmode
# undef ungetch
# define ungetch caca_conio_ungetch
# undef wherex
# define wherex caca_conio_wherex
# undef wherey
# define wherey caca_conio_wherey
# undef window
# define window caca_conio_window
#endif

#ifdef __cplusplus
}
#endif

#endif /* __CACA_CONIO_H__ */

+ 1
- 1
configure.ac 查看文件

@@ -103,7 +103,7 @@ AC_ARG_ENABLE(zzuf,

AC_CHECK_HEADERS(stdio.h stdarg.h signal.h sys/ioctl.h sys/time.h endian.h unistd.h arpa/inet.h netinet/in.h winsock2.h errno.h locale.h getopt.h dlfcn.h termios.h)
AC_CHECK_FUNCS(signal ioctl snprintf vsnprintf getenv putenv strcasecmp htons)
AC_CHECK_FUNCS(usleep gettimeofday)
AC_CHECK_FUNCS(usleep gettimeofday atexit)

ac_cv_have_getopt_long="no"
AC_CHECK_FUNCS(getopt_long,


+ 1
- 0
examples/.gitignore 查看文件

@@ -1,6 +1,7 @@
blit
canvas
colors
conio
demo
demo0
dithering


+ 4
- 1
examples/Makefile.am 查看文件

@@ -2,7 +2,7 @@

AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/caca -I$(top_builddir)/caca

noinst_PROGRAMS = blit canvas colors demo demo0 dithering driver event export figfont font font2tga frames fullwidth gamma hsv input spritedit swallow text transform truecolor unicode import
noinst_PROGRAMS = blit canvas colors conio demo demo0 dithering driver event export figfont font font2tga frames fullwidth gamma hsv input spritedit swallow text transform truecolor unicode import

blit_SOURCES = blit.c
blit_LDADD = ../caca/libcaca.la
@@ -13,6 +13,9 @@ canvas_LDADD = ../caca/libcaca.la
colors_SOURCES = colors.c
colors_LDADD = ../caca/libcaca.la

conio_SOURCES = conio.c
conio_LDADD = ../caca/libcaca.la

demo_SOURCES = demo.c
demo_LDADD = ../caca/libcaca.la
demo_LDFLAGS = @MATH_LIBS@


+ 682
- 0
examples/conio.c 查看文件

@@ -0,0 +1,682 @@
/*4il.c (4 in line) - compiled under Borland C++ 5.0.
*Version : Beta
*Author : Cheok Yan Cheng
*Date started coding : 11/10/2000
*Date finished coding : 13/10/2000
*/

/*If you had encounted any problem, bugs or comment on this source code
*you are welcomed to contact me by my e-mail : yccheok@yahoo.com
*/

/*Some terms in this source code that you encounted may looked strange to you.
*However, I have no time to include the explaination in detail.
*If you really want to know, please contact me through e-mail.
*Sorry for the inconvenience
*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "caca_conio.h"

#undef max

#define X_BOARD 8
#define Y_BOARD 8
#define BOARD_SIZE X_BOARD*Y_BOARD

#define BOOLEAN int
#define TRUE 1
#define FALSE 0

#define WIN 1
#define LOSE -1
#define DRAW 0
#define OK 2

#define COMPUTER 0
#define HUMAN 1
#define EMPTY -1
#define BASE -2

#define WIN_MESSAGE "I Win! Press Any Key To Continue...\n"
#define LOSE_MESSAGE "You Win! Press Any Key To Continue...\n"
#define DRAW_MESSAGE "Draw! Press Any Key To Continue...\n"

BOOLEAN computerMove(void);
BOOLEAN doubleHaed(int, int [Y_BOARD][X_BOARD]);
BOOLEAN humanMove(int);
BOOLEAN isFull(void);
BOOLEAN isWon(int, int [Y_BOARD][X_BOARD]);
BOOLEAN twoSnake(int, int, int [Y_BOARD][X_BOARD]);
int bestmove(void);
int max(int [X_BOARD]);
int status(void);
int xDoubleHead(int, int [Y_BOARD][X_BOARD]);
int xTwoSnake(int, int [Y_BOARD][X_BOARD]);
int xWon(int, int [Y_BOARD][X_BOARD]);
int y_Base(int, int [Y_BOARD][X_BOARD]);
void duplicate(int [Y_BOARD][X_BOARD], int [Y_BOARD][X_BOARD]);
void drawBoard(void);
void drawPiece(void);
void genNumWin(int [X_BOARD]);
void getHumanMove(void);
void init(void);
void makeMove(int, int, int [Y_BOARD][X_BOARD]);
void sorting(int n[X_BOARD]);

int move[X_BOARD] = {3, 4, 2, 5, 1, 6, 0, 7};
int col[X_BOARD] = {-1,-1,-1,-1,-1,-1,-1,-1};
int square[Y_BOARD][X_BOARD] =
{ {-1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1},
{-2, -2, -2, -2, -2, -2, -2, -2},
};

BOOLEAN computerMove(void)
{
int x_best;
if (isFull())
return FALSE;
x_best = bestmove();
gotoxy(52,2);
printf("x:%d, y:%d\n" ,x_best+1 ,Y_BOARD-y_Base(x_best,square));
makeMove(COMPUTER, x_best, square);
return TRUE;
}

BOOLEAN doubleHead(int who, int xsquare[Y_BOARD][X_BOARD])
{
int x,y;
for(y=0; y<Y_BOARD; y++)
{
for(x=0; x<(X_BOARD-4); x++)
{
if( xsquare[y][x] == BASE &&
xsquare[y][x+1] == who &&
xsquare[y][x+2] == who &&
xsquare[y][x+3] == who &&
xsquare[y][x+4] == BASE )
return TRUE;
}
}
return FALSE;
}

BOOLEAN humanMove(int x)
{
gotoxy(1,20);
if ((x < 0) || x >= X_BOARD)
return FALSE;
if (y_Base(x, square) == -1) /*indicate no base at that x-coulomb*/
return FALSE;
if (isFull()) /*the board is full*/
return FALSE;
gotoxy(52,5);
printf("x:%d, y:%d\n" ,x+1 ,Y_BOARD-y_Base(x,square));
makeMove(HUMAN,x,square);
return TRUE;
}

BOOLEAN isFull()
{
int x;
for(x=0; x<X_BOARD; x++)
{
if(square[0][x] == EMPTY || square[0][x] == BASE)
return FALSE;
}
return TRUE;
}

BOOLEAN isWon(int who, int xsquare[Y_BOARD][X_BOARD])
{
int x,y;
for(x=0; x<X_BOARD; x++)
{
for(y=0; y<Y_BOARD; y++)
{
/*horizontal position*/
if( xsquare[y][x] == who &&
xsquare[y][x+1] == who &&
xsquare[y][x+2] == who &&
xsquare[y][x+3] == who &&
(x+3)<X_BOARD
)
return TRUE;

/*vertical position*/
if( xsquare[y][x] == who &&
xsquare[y+1][x] == who &&
xsquare[y+2][x] == who &&
xsquare[y+3][x] == who &&
(y+3)<Y_BOARD
)
return TRUE;

/*downstair diagonal position*/
if( xsquare[y][x] == who &&
xsquare[y+1][x+1] == who &&
xsquare[y+2][x+2] == who &&
xsquare[y+3][x+3] == who &&
(x+3)<X_BOARD &&
(y+3)<Y_BOARD
)
return TRUE;

/*upstair diagonal position*/
if( xsquare[y][x] == who &&
xsquare[y-1][x+1] == who &&
xsquare[y-2][x+2] == who &&
xsquare[y-3][x+3] == who &&
(x+3)<X_BOARD &&
(y-3)>=0
)
return TRUE;
}
}
return FALSE;
}

BOOLEAN twoSnake(int who, int x, int xsquare[Y_BOARD][X_BOARD])
{
int xxsquare[Y_BOARD][X_BOARD];
int n[Y_BOARD] = {0,0,0,0,0,0,0,0};
int i;

for(i=0; i<Y_BOARD; i++)
{
if(xsquare[i][x] == BASE || xsquare[i][x] == EMPTY)
{
duplicate(xxsquare, xsquare);
xxsquare[i][x] = who;

if(isWon(who, xxsquare))
n[i] = TRUE;
}
}

for(i=0; i<(Y_BOARD-1); i++)
{
if( n[i] == TRUE && n[i+1] == TRUE )
return TRUE;
}
return FALSE;
}

int bestmove()
{
int xsquare[Y_BOARD][X_BOARD], n[X_BOARD], i, snake;

gotoxy(1,19);
textcolor(4);
if(xWon(COMPUTER, square) != -1)
{
cprintf("Computer Previous Depth : +1\n");
return xWon(COMPUTER, square);
}
if(xWon(HUMAN, square) != -1)
{
cprintf("Computer Previous Depth : -1\n");
return xWon(HUMAN, square);
}

for(i=0; i<X_BOARD; i++)
{
if(y_Base(move[i], square) != -1 && col[move[i]] == COMPUTER)
{
duplicate(xsquare, square);
makeMove(COMPUTER, move[i], xsquare);
if(xWon(HUMAN, xsquare) == -1)
{
cprintf("Computer Previous Depth : +2\n");
return move[i];
}
}
}

if(xDoubleHead(COMPUTER, square) != -1)
{
duplicate(xsquare, square);
makeMove(COMPUTER, xDoubleHead(COMPUTER, xsquare), xsquare);
if(xWon(HUMAN, xsquare) == -1)
{
cprintf("Computer Previous Depth : +3\n");
return xDoubleHead(COMPUTER, square);
}
}

if(xDoubleHead(HUMAN, square) != -1)
{
duplicate(xsquare, square);
makeMove(COMPUTER, xDoubleHead(HUMAN, xsquare), xsquare);
if(xWon(HUMAN, xsquare) == -1)
{
cprintf("Computer Previous Depth : -3\n");
return xDoubleHead(HUMAN, square);
}
}

snake =xTwoSnake(COMPUTER, square);
if( snake != -1)
{
duplicate(xsquare, square);
makeMove(COMPUTER, snake, xsquare);
if(xWon(HUMAN, xsquare) == -1)
{
cprintf("Computer Previous Depth : +4\n");
return snake;
}
}
if(xTwoSnake(HUMAN, square) != -1)
{
duplicate(xsquare, square);
makeMove(COMPUTER, xTwoSnake(HUMAN, xsquare), xsquare);
if(xWon(HUMAN, xsquare) == -1)
{
cprintf("Computer Previous Depth : -4\n");
return xTwoSnake(HUMAN, square);
}
}

genNumWin(n);
sorting(n);

for(i=0; i<X_BOARD; i++)
{
if( y_Base (n[i], square) != -1)
{
duplicate(xsquare, square);
makeMove(COMPUTER, n[i], xsquare);
if(xWon(HUMAN, xsquare) == -1)
{
cprintf("Computer Previous Depth : +5\n");
return n[i];
}
}
}

for(i=0; i<X_BOARD; i++)
{
if( y_Base (move[i], square) != -1)
{
cprintf("Computer Previous Depth : +0\n");
return move[i];
}
}
return -1;
}

int max(int n[X_BOARD])
{
int i, big;
big = 0;
for(i=0; i<X_BOARD; i++)
{
if(n[i]>big)
big = n[i];
}
return big;
}

int status()
{
if (isWon(COMPUTER, square))
return WIN;
else
if (isWon(HUMAN, square))
return LOSE;
else
if (isFull())
return DRAW;
else
return OK;
}

int xDoubleHead(int who, int xsquare[Y_BOARD][X_BOARD])
{
int x;
int xxsquare[Y_BOARD][X_BOARD];
for(x=0; x<X_BOARD; x++)
{
if(y_Base(x,xsquare) != -1)
{
duplicate(xxsquare, xsquare);
makeMove(who, x, xxsquare);
if(doubleHead(who, xxsquare))
return x;
}
}
return -1;
}

int xTwoSnake(int who, int xsquare[Y_BOARD][X_BOARD])
{
int x, dx;
int xxsquare[Y_BOARD][X_BOARD];
for(x=0; x<X_BOARD; x++)
{
if(y_Base(move[x],xsquare) != -1)
{
duplicate(xxsquare, xsquare);
makeMove(who, move[x], xxsquare);
for(dx=0; dx<X_BOARD; dx++)
{
if( twoSnake(who, move[dx], xxsquare) && col[move[dx]] != who)
{
if(who == COMPUTER)
col[move[dx]] = who;
return move[x];
}
}
}
}
return -1;
}

int xWon(int who, int xsquare[Y_BOARD][X_BOARD])
{
int x;
int xxsquare[Y_BOARD][X_BOARD];
for(x=0; x<X_BOARD; x++)
{
if(y_Base(x,xsquare) != -1)
{
duplicate(xxsquare, xsquare);
makeMove(who, x, xxsquare);
if(isWon(who, xxsquare))
return x;
}
}
return -1;
}

int y_Base(int x, int xsquare[Y_BOARD][X_BOARD])
{
int y;
for(y=0; y<Y_BOARD; y++)
{
if(xsquare[y][x] == BASE)
return y;
}
return -1; /*indicate no base at that x*/
}

void duplicate(int xSquare[Y_BOARD][X_BOARD], int oSquare[Y_BOARD][X_BOARD])
{
int x,y;
for(x=0; x<X_BOARD; x++)
{
for(y=0; y<Y_BOARD; y++)
{
xSquare[y][x] = oSquare[y][x];
}
}
}

void drawBoard()
{
textcolor(0);
textbackground(7);
clrscr();
gotoxy(1,1);
printf("+-----+-----+-----+-----+-----+-----+-----+-----+\n");
printf("| | | | | | | | |\n");
printf("+-----+-----+-----+-----+-----+-----+-----+-----+\n");
printf("| | | | | | | | |\n");
printf("+-----+-----+-----+-----+-----+-----+-----+-----+\n");
printf("| | | | | | | | |\n");
printf("+-----+-----+-----+-----+-----+-----+-----+-----+\n");
printf("| | | | | | | | |\n");
printf("+-----+-----+-----+-----+-----+-----+-----+-----+\n");
printf("| | | | | | | | |\n");
printf("+-----+-----+-----+-----+-----+-----+-----+-----+\n");
printf("| | | | | | | | |\n");
printf("+-----+-----+-----+-----+-----+-----+-----+-----+\n");
printf("| | | | | | | | |\n");
printf("+-----+-----+-----+-----+-----+-----+-----+-----+\n");
printf("| | | | | | | | |\n");
printf("+-----+-----+-----+-----+-----+-----+-----+-----+\n");
printf("ARROW:move\tSPACE:select\tESC:exit\n");
textcolor(1);
gotoxy(44,20);
cprintf("%-1s","4 In Line\n");
gotoxy(44,21);
cprintf("%-1s","ver Beta by Cheok Yan Cheng\n");
gotoxy(44,22);
cprintf("E-mail : yccheok@yahoo.com\n");
gotoxy(44,23);
cprintf("Web Site: www.geocities.com/yccheok\n");
gotoxy(44,24);
cprintf("Source code included!\n");
gotoxy(1,22);
printf("Human's Piece is O\n");
gotoxy(1,23);
printf("Computer's Piece is X\n");
gotoxy(52,1);
printf("Computer Move :\n");
gotoxy(52,4);
printf("Human Move :\n");
}

void drawPiece()
{
int x,y;
for(x=0; x<X_BOARD; x++)
{
for(y=0; y<Y_BOARD; y++)
{
if(square[y][x] == HUMAN)
{
gotoxy(x*6+4, y*2+2);
textcolor(1);
cprintf("O\n");
}
else
if(square[y][x] == COMPUTER)
{
gotoxy(x*6+4, y*2+2);
textcolor(4);
cprintf("X\n");
}
}
}
}

void genNumWin(int n[X_BOARD])
{
int i, j, k, mov;
int xsquare[Y_BOARD][X_BOARD];
int xxsquare[Y_BOARD][X_BOARD];

for(i=0; i<X_BOARD; i++)
{
n[i]=0;
if(y_Base(i, square) != -1) /*has base exsit?*/
{
duplicate(xsquare, square);
makeMove(COMPUTER, i, xsquare);

for(j=0; j<X_BOARD; j++)
{
for(k=0; k<Y_BOARD; k++)
{
if(xsquare[k][j] == EMPTY || xsquare[k][j] == BASE)
{
duplicate(xxsquare, xsquare);
xxsquare[k][j] = COMPUTER;
if(isWon(COMPUTER, xxsquare))
n[i]++;
}
}
}
}
}
}

void getHumanMove()
{
int x=3 ;
int ch;
while(TRUE)
{
gotoxy(x*6 +4, 2);
ch = (int)getch();
switch(ch)
{
case 75:/*LEFT*/
if(x>0)
x--;
break;
case 77:/*RIGHT*/
if(x<(X_BOARD-1))
x++;
break;
case 27:/*ESC*/
textcolor(7);
textbackground(0);
clrscr();
printf("Thank You For Playing 4 in line by Cheok Yan Cheng!\n");
exit(0);
break;
case 32:/*SPACE*/
if(humanMove(x))
{
drawPiece();
return;
}
else
{
gotoxy(1,20);
textcolor(4);
cprintf("OOPs! Wrong Move! \n");
}
}
}
}

void init()
{
int x,y;
for(x=0; x<X_BOARD; x++)
{
for(y=0; y<(Y_BOARD-1); y++)
{
square[y][x] = EMPTY;
}
square[7][x] = BASE;
col[x] = -1;
}
}

void makeMove(int who, int x, int xsquare[Y_BOARD][X_BOARD])
{
int y;
y = y_Base(x, xsquare);
xsquare[y][x] = who;
if(y>0)
xsquare[y-1][x] = BASE;
}

void sorting(int n[])
{
int i, index, alpha;
int tmp[X_BOARD];
int store[X_BOARD];

for(index=0; index<X_BOARD; index++)
{
alpha = max(n);
for(i=0; i<X_BOARD; i++)
{
if(n[move[i]] == alpha)
{
store[index] = move[i];
n[move[i]] = -1;
break;
}
}
}
for(i=0; i<X_BOARD; i++)
n[i] = store[i];
}

void main()
{
BOOLEAN myturn;
myturn = TRUE;
drawBoard();
srand(time(NULL));
rand();
do
{
switch (status())
{
case WIN:
case LOSE:
case DRAW:
init();
drawBoard();
if (myturn)
{
makeMove(COMPUTER,(2+rand()%4),square);
}
myturn = !myturn;
drawPiece();
}
textcolor(4);
gotoxy(1,20);
cprintf("Your Turn, Please.\n");
getHumanMove();
gotoxy(1,20);
textcolor(4);
switch (status())
{
case WIN:
cprintf(WIN_MESSAGE);
getch();
break;
case LOSE:
cprintf(LOSE_MESSAGE);
getch();
break;
case DRAW:
cprintf(DRAW_MESSAGE);
getch();
break;
default:/*OK*/
if(computerMove())
{
gotoxy(1,20);
drawPiece();
gotoxy(1,20);
switch (status())
{
case WIN:
cprintf(WIN_MESSAGE);
getch();
break;
case LOSE:
cprintf(LOSE_MESSAGE);
getch();
break;
case DRAW:
cprintf(DRAW_MESSAGE);
getch();
break;
}
}
}
}
while(TRUE);
}


Loading…
取消
儲存