diff --git a/Makefile.am b/Makefile.am index ece3190..8708553 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,6 @@ # $Id$ -SUBDIRS = kernel cucul caca src test tools cpp doc +SUBDIRS = kernel cucul caca src test tools cxx doc DIST_SUBDIRS = $(SUBDIRS) autotools debian msvc EXTRA_DIST = NOTES COPYING.GPL COPYING.LGPL bootstrap build-dos build-kernel build-win32 caca-config.in common.h libcaca.spec diff --git a/configure.ac b/configure.ac index 547204d..cbe421e 100644 --- a/configure.ac +++ b/configure.ac @@ -54,8 +54,8 @@ AC_ARG_ENABLE(vga, [ --enable-vga VGA support (default disabled)]) dnl language bindings -AC_ARG_ENABLE(cpp, - [ --enable-cpp C++ bindings (default disabled)]) +AC_ARG_ENABLE(cxx, + [ --enable-cxx C++ bindings (default disabled)]) dnl example programs features AC_ARG_ENABLE(imlib2, @@ -247,11 +247,11 @@ CFLAGS="${CFLAGS} -g -O2 -fno-strength-reduce -fomit-frame-pointer" CFLAGS="${CFLAGS} -Wall -Wpointer-arith -Wcast-align -Wcast-qual -Wstrict-prototypes -Waggregate-return -Wmissing-prototypes -Wnested-externs -Wsign-compare" # Build the C++ bindings? -ac_cv_my_have_cpp="no" -if test "${enable_cpp}" = "yes"; then - ac_cv_my_have_cpp="yes" +ac_cv_my_have_cxx="no" +if test "${enable_cxx}" = "yes"; then + ac_cv_my_have_cxx="yes" fi -AM_CONDITIONAL(USE_CPP, test "${ac_cv_my_have_cpp}" = "yes") +AM_CONDITIONAL(USE_CXX, test "${ac_cv_my_have_cxx}" = "yes") # Build cacaserver? ac_cv_my_have_fcntl="no" @@ -316,7 +316,7 @@ AC_CONFIG_FILES([ src/Makefile test/Makefile tools/Makefile - cpp/Makefile + cxx/Makefile doc/Makefile autotools/Makefile debian/Makefile diff --git a/cxx/Makefile.am b/cxx/Makefile.am new file mode 100644 index 0000000..f4bc097 --- /dev/null +++ b/cxx/Makefile.am @@ -0,0 +1,24 @@ +# $Id: Makefile.am 552 2006-04-13 16:10:16Z jylam $ + +AM_CPPFLAGS = -I$(top_srcdir)/cucul -I$(top_srcdir)/caca + +if USE_CXX +include_HEADERS = cucul++.h caca++.h +lib_LTLIBRARIES = libcucul++.la libcaca++.la +endif + +libcucul___la_SOURCES = cucul++.cpp cucul++.h +libcucul___la_LDFLAGS = -no-undefined -version-info @LT_VERSION@ +libcucul___la_LIBADD = ../cucul/libcucul.la + +libcaca___la_SOURCES = caca++.cpp caca++.h +libcaca___la_LDFLAGS = -no-undefined -version-info @LT_VERSION@ +libcaca___la_LIBADD = ../caca/libcaca.la + +if USE_CXX +noinst_PROGRAMS = cpptest +endif + +cpptest_SOURCES = cpptest.cpp +cpptest_LDADD = libcaca++.la libcucul++.la ../caca/libcaca.la ../cucul/libcucul.la @CACA_LIBS@ + diff --git a/cxx/caca++.cpp b/cxx/caca++.cpp new file mode 100644 index 0000000..bf3d168 --- /dev/null +++ b/cxx/caca++.cpp @@ -0,0 +1,97 @@ +/* + * libcaca++ C++ bindings for libcaca + * Copyright (c) 2006 Jean-Yves Lamoureux + * All Rights Reserved + * + * $Id$ + * + * This library is free software; 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 the main functions used by \e libcaca++ applications to + * initialise the library, get the screen properties, set the framerate and + * so on. + */ + + +#include "caca++.h" + + +Caca::Caca(Cucul *cv) +{ + dp = caca_create_display(cv->get_cucul_canvas_t()); + if(!dp) + throw -1; +} + +Caca::~Caca() +{ + caca_free_display(dp); +} + +void Caca::Attach(Cucul *cv) +{ + dp = caca_create_display(cv->get_cucul_canvas_t()); + if(!dp) + throw -1; +} + +void Caca::Detach() +{ + caca_free_display(dp); +} + +void Caca::setDelay(unsigned int d) +{ + caca_set_delay(dp, d); +} + +void Caca::Display() +{ + caca_refresh_display(dp); +} + +unsigned int Caca::getRendertime() +{ + return caca_get_rendertime(dp); +} + +unsigned int Caca::getWidth() +{ + return caca_get_display_width(dp); +} + +unsigned int Caca::getHeight() +{ + return caca_get_display_height(dp); +} + +int Caca::setTitle(char const *s) +{ + return caca_set_display_title(dp, s); +} + +int Caca::getEvent(unsigned int g, Event *n, int aa) +{ + return caca_get_event(dp, g, &n->e, aa); +} + +unsigned int Caca::getMouseX() +{ + return caca_get_mouse_x(dp); +} + +unsigned int Caca::getMouseY() +{ + return caca_get_mouse_x(dp); +} + +void Caca::setMouse(int v) +{ + caca_set_mouse(dp, v); +} + diff --git a/cxx/caca++.h b/cxx/caca++.h new file mode 100644 index 0000000..c089dbd --- /dev/null +++ b/cxx/caca++.h @@ -0,0 +1,76 @@ +/* + * libcaca++ C++ bindings for libcaca + * Copyright (c) 2006 Jean-Yves Lamoureux + * All Rights Reserved + * + * $Id$ + * + * This library is free software; 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. + */ + +/** \file caca++.h + * \version \$Id$ + * \author Jean-Yves Lamoureux + * \brief The \e libcaca++ public header. + * + * This header contains the public types and functions that applications + * using \e libcaca++ may use. + */ +#ifndef _CACA_PP_H +#define _CACA_PP_H + +#include +#include + +#include + +class Event +{ + friend class Caca; + public: + enum caca_event_type + { + CACA_EVENT_NONE = 0x0000, /**< No event. */ + + CACA_EVENT_KEY_PRESS = 0x0001, /**< A key was pressed. */ + CACA_EVENT_KEY_RELEASE = 0x0002, /**< A key was released. */ + CACA_EVENT_MOUSE_PRESS = 0x0004, /**< A mouse button was pressed. */ + CACA_EVENT_MOUSE_RELEASE = 0x0008, /**< A mouse button was released. */ + CACA_EVENT_MOUSE_MOTION = 0x0010, /**< The mouse was moved. */ + CACA_EVENT_RESIZE = 0x0020, /**< The window was resized. */ + + CACA_EVENT_ANY = 0xffff /**< Bitmask for any event. */ + } type; + + protected: + caca_event e; +}; + +class Caca +{ + public: + Caca(); + Caca(Cucul *cv); + ~Caca(); + + void Attach(Cucul *cv); + void Detach(); + void setDelay(unsigned int); + void Display(); + unsigned int getRendertime(); + unsigned int getWidth(); + unsigned int getHeight(); + int setTitle(char const *); + int getEvent(unsigned int, Event*, int); + unsigned int getMouseX(); + unsigned int getMouseY(); + void setMouse(int); + + private: + caca_display_t *dp; +}; + +#endif /* _CACA_PP_H */ diff --git a/cxx/caca++.pc.in b/cxx/caca++.pc.in new file mode 100644 index 0000000..d63e0a4 --- /dev/null +++ b/cxx/caca++.pc.in @@ -0,0 +1,12 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: caca++ +Description: Colour ASCII-Art library C++ bindings +Version: @VERSION@ +Requires: cucul++ = @VERSION@ +Conflicts: +Libs: -L${libdir} -lcaca -lcucul -lcucul++ +Cflags: -I${includedir} diff --git a/cxx/cpptest.cpp b/cxx/cpptest.cpp new file mode 100644 index 0000000..8292f4b --- /dev/null +++ b/cxx/cpptest.cpp @@ -0,0 +1,103 @@ +/* + * cpptest libcaca++ rendering test + * Copyright (c) 2006 Jean-Yves Lamoureux + * All Rights Reserved + * + * $Id$ + * + * This program is free software; 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. + */ + +#include "config.h" + +#include + +#include +#include + +using namespace std; + + +static char const *pig[]= { + " ", + " _ ", + " _._ _..._ .-', _.._(`)) ", + " '-. ` ' /-._.-' ',/ ", + " ) \\ '. ", + " / _ _ | \\ ", + " | a a / | ", + " \\ .-. ; " , + " '-('' ).-' ,' ; ", + " '-; | .' ", + " \\ \\ / ", + " | 7 .__ _.-\\ \\ ", + " | | | ``/ /` / ", + " jgs /,_| | /,_/ / ", + " /,_/ '`-' ", + " ", + NULL +}; + +int main(int argc, char *argv[]) +{ + Cucul *qq; + Caca *kk; + Event ev; + + int x = 0, y = 0, ix = 1, iy = 1; + + + + try { + qq = new Cucul(); + } + catch (int e) { + cerr << "Error while initializing cucul (" << e << ")" << endl; + return -1; + } + + try { + kk = new Caca(qq); + } + catch(int e) { + cerr << "Error while attaching cucul to caca (" << e << ")" << endl; + return -1; + } + + kk->setDelay(20000); + + while(!kk->getEvent(ev.CACA_EVENT_KEY_PRESS, &ev, 0)) { + + /* Draw pig */ + qq->setColor(CUCUL_COLOR_LIGHTMAGENTA, CUCUL_COLOR_BLACK); + + for(int i = 0; pig[i]; i++) + qq->putStr(x, y+i, (char*)pig[i]); + + /* printf works */ + qq->setColor(CUCUL_COLOR_LIGHTBLUE, CUCUL_COLOR_BLACK); + qq->Printf(30,15, "Powered by libcaca %s", VERSION); + + /* Blit */ + kk->Display(); + + x+=ix; + y+=iy; + + if(x>=(qq->getWidth()-35) || x<0 ) + ix=-ix; + if(y>=(qq->getHeight()-15) || y<0 ) + iy=-iy; + + + } + + + delete kk; + delete qq; + + return 0; +} diff --git a/cxx/cucul++.cpp b/cxx/cucul++.cpp new file mode 100644 index 0000000..3a2d098 --- /dev/null +++ b/cxx/cucul++.cpp @@ -0,0 +1,338 @@ +/* + * libcucul++ C++ bindings for libcucul + * Copyright (c) 2006 Jean-Yves Lamoureux + * All Rights Reserved + * + * $Id$ + * + * This library is free software; 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 the main functions used by \e libcucul++ applications + * to initialise a drawing context. + */ + +#include "config.h" + +#include // BUFSIZ +#include // va_* + +#include "cucul++.h" + +Cucul::Cucul() +{ + cv = cucul_create_canvas(0, 0); + if(!cv) + throw -1; +} + +Cucul::Cucul(int width, int height) +{ + cv = cucul_create_canvas(width, height); + if(!cv) throw -1; +} + +Cucul::Cucul(Buffer *b, char const *format) +{ + cv = cucul_import_canvas(b->get_buffer(), format); + if(!cv) throw -1; +} + +Cucul::~Cucul() +{ + if(cv) + cucul_free_canvas(cv); +} + +cucul_canvas_t *Cucul::get_cucul_canvas_t() +{ + return cv; +} + +void Cucul::setSize(unsigned int width, unsigned int height) +{ + cucul_set_canvas_size(cv, width, height); +} + +unsigned int Cucul::getWidth(void) +{ + return cucul_get_canvas_width(cv); +} + +unsigned int Cucul::getHeight(void) +{ + return cucul_get_canvas_height(cv); +} + +void Cucul::setColor(unsigned int f, unsigned int b) +{ + cucul_set_color(cv, f, b); +} + +char const * Cucul::getColorName(unsigned int color) +{ + return cucul_get_color_name(color); +} + +void Cucul::putChar(int x, int y, char ch) +{ + cucul_putchar(cv, x, y, ch); +} + +void Cucul::putStr(int x, int y, char *str) +{ + cucul_putstr(cv, x, y, str); +} + +void Cucul::Printf(int x, int y, char const * format,...) +{ + char tmp[BUFSIZ]; + char *buf = tmp; + va_list args; + + va_start(args, format); +#if defined(HAVE_VSNPRINTF) + vsnprintf(buf, getWidth() - x + 1, format, args); +#else + vsprintf(buf, format, args); +#endif + buf[getWidth() - x] = '\0'; + va_end(args); + + putStr(x, y, buf); +} + +void Cucul::Clear(void) +{ + cucul_clear_canvas(cv); +} + +void Cucul::Blit(int x, int y, Cucul* c1, Cucul* c2) +{ + cucul_blit(cv, x, y, c1->get_cucul_canvas_t(), c2->get_cucul_canvas_t()); +} + +void Cucul::Invert() +{ + cucul_invert(cv); +} + +void Cucul::Flip() +{ + cucul_flip(cv); +} + +void Cucul::Flop() +{ + cucul_flop(cv); +} + +void Cucul::Rotate() +{ + cucul_rotate(cv); +} + +void Cucul::drawLine(int x1, int y1, int x2, int y2, char const *ch) +{ + cucul_draw_line(cv, x1, y1, x2, y2, ch); +} + +void Cucul::drawPolyline(int const x[], int const y[], int f, char const *ch) +{ + cucul_draw_polyline(cv, x, y, f, ch); +} + +void Cucul::drawThinLine(int x1, int y1, int x2, int y2) +{ + cucul_draw_thin_line(cv, x1, y1, x2, y2); +} + +void Cucul::drawThinPolyline(int const x[], int const y[], int f) +{ + cucul_draw_thin_polyline(cv, x, y, f); +} + +void Cucul::drawCircle(int x, int y, int d, char const *ch) +{ + cucul_draw_circle(cv, x, y, d, ch); +} + +void Cucul::drawEllipse(int x, int y, int d1, int d2, char const *ch) +{ + cucul_draw_ellipse(cv, x, y, d1, d2, ch); +} + +void Cucul::drawThinEllipse(int x, int y, int d1, int d2) +{ + cucul_draw_thin_ellipse(cv, x, y, d1, d2); +} + +void Cucul::fillEllipse(int x, int y, int d1, int d2, char const *ch) +{ + cucul_fill_ellipse(cv, x, y, d1, d2, ch); +} + +void Cucul::drawBox(int x, int y, int w, int h, char const *ch) +{ + cucul_draw_box(cv, x, y, w, h, ch); +} + +void Cucul::drawThinBox(int x, int y, int w, int h) +{ + cucul_draw_thin_box(cv, x, y, w, h); +} + +void Cucul::fillBox(int x, int y, int w, int h, char const *ch) +{ + cucul_fill_box(cv, x, y, w, h, ch); +} + +void Cucul::drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, char const *ch) +{ + cucul_draw_triangle(cv, x1, y1, x2, y2, x3, y3, ch); +} + +void Cucul::drawThinTriangle(int x1, int y1, int x2, int y2, int x3, int y3) +{ + cucul_draw_thin_triangle(cv, x1, y1, x2, y2, x3, y3); +} + +void Cucul::fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, const char *ch) +{ + cucul_fill_triangle(cv, x1, y1, x2, y2, x3, y3, ch); +} + +int Cucul::Rand(int min, int max) +{ + return cucul_rand(min, max); +} + +Dither::Dither(unsigned int v1, unsigned int v2, unsigned int v3, unsigned int v4, unsigned int v5, unsigned int v6, unsigned int v7, unsigned int v8) +{ + dither = cucul_create_dither(v1, v2, v3, v4, v5, v6, v7, v8); +} +Dither::~Dither() +{ + cucul_free_dither(dither); +} + +void Dither::setPalette(unsigned int r[], unsigned int g[], unsigned int b[], unsigned int a[]) +{ + cucul_set_dither_palette(dither, r, g, b, a); +} + +void Dither::setBrightness(float f) +{ + cucul_set_dither_brightness(dither, f); +} + +void Dither::setGamma(float f) +{ + cucul_set_dither_gamma(dither, f); +} + +void Dither::setContrast(float f) +{ + cucul_set_dither_contrast(dither, f); +} + +void Dither::setInvert(int i) +{ + cucul_set_dither_invert(dither, i); +} + +void Dither::setAntialias(char const *cv) +{ + cucul_set_dither_antialias(dither, cv); +} + +char const *const * Dither::getAntialiasList() +{ + return cucul_get_dither_antialias_list(dither); +} + +void Dither::setColor(char const *cv) +{ + cucul_set_dither_color(dither, cv); +} + +char const *const * Dither::getColorList() +{ + return cucul_get_dither_color_list(dither); +} + +void Dither::setCharset(char const *cv) +{ + cucul_set_dither_charset(dither, cv); +} + +char const *const * Dither::getCharsetList() +{ + return cucul_get_dither_charset_list(dither); +} + +void Dither::setMode(char const *cv) +{ + cucul_set_dither_mode(dither, cv); +} + +char const *const * Dither::getModeList(void) +{ + return cucul_get_dither_mode_list(dither); +} + +void Dither::Bitmap(Cucul *cv, int x, int y, int w, int h, void *v) +{ + cucul_dither_bitmap(cv->get_cucul_canvas_t(), x, y, w, h, dither, v); +} + +Font::Font(void const *s, unsigned int v) +{ + font = cucul_load_font(s, v); + if(!font) throw -1; +} + +char const *const * Font::getList(void) +{ + return cucul_get_font_list(); +} + +unsigned int Font::getWidth() +{ + return cucul_get_font_width(font); +} + +unsigned int Font::getHeight() +{ + return cucul_get_font_height(font); +} + +void Font::renderCanvas(Cucul *cv, unsigned char *buf, unsigned int x, unsigned int y, unsigned int w) +{ + cucul_render_canvas(cv->get_cucul_canvas_t(), font, buf, x, y, w); +} + +Font::~Font() +{ + cucul_free_font(font); +} + +Buffer::Buffer(Cucul *cv, char const *buf) +{ + buffer = cucul_export_canvas(cv->get_cucul_canvas_t(), buf); + if(!buffer) throw -1; +} + +char const *const * Buffer::getExportList(void) +{ + return cucul_get_export_list(); +} + +cucul_buffer *Buffer::get_buffer(void) +{ + return buffer; +} diff --git a/cxx/cucul++.h b/cxx/cucul++.h new file mode 100644 index 0000000..9a95963 --- /dev/null +++ b/cxx/cucul++.h @@ -0,0 +1,139 @@ +/* + * libcucul++ C++ bindings for libcucul + * Copyright (c) 2006 Jean-Yves Lamoureux + * All Rights Reserved + * + * $Id$ + * + * This library is free software; 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. + */ + +/** \file cucul++.h + * \version \$Id$ + * \author Jean-Yves Lamoureux + * \brief The \e libcucul++ public header. + * + * This header contains the public types and functions that applications + * using \e libcucul++ may use. + */ + +#ifndef _CUCUL_PP_H +#define _CUCUL_PP_H + +#include + + +class Cucul; + +/* Ugly, I know */ +class Font +{ + public: + ~Font(); + Font(void const *, unsigned int); + char const *const * getList(void); + unsigned int getWidth(); + unsigned int getHeight(); + void renderCanvas(Cucul *, unsigned char *, unsigned int, unsigned int, unsigned int); + + private: + cucul_font *font; + +}; + +class Dither +{ + public: + Dither(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int); + ~Dither(); + + void setPalette(unsigned int r[], unsigned int g[], unsigned int b[], unsigned int a[]); + void setBrightness(float); + void setGamma(float); + void setContrast(float); + void setInvert(int); + void setAntialias(char const *); + char const *const * getAntialiasList(); + void setColor(char const *); + char const *const * getColorList(); + void setCharset(char const *); + char const *const * getCharsetList(); + void setMode(char const *); + char const *const * getModeList(); + void Bitmap(Cucul *, int, int, int, int, void *); + + private: + cucul_dither *dither; + +}; + +class Buffer +{ + friend class Cucul; + public: + Buffer(Cucul *cv, char const *); + ~Buffer(); + char const *const * getExportList(void); + + protected: + cucul_buffer *get_buffer(); + + private: + cucul_buffer *buffer; +}; + + +class Cucul +{ + friend class Caca; + friend class Dither; + friend class Font; + friend class Buffer; + public: + Cucul(); + Cucul(int width, int height); + Cucul(Buffer *, char const *); + ~Cucul(); + + void setSize(unsigned int w, unsigned int h); + unsigned int getWidth(void); + unsigned int getHeight(void); + void setColor(unsigned int f, unsigned int b); + char const * getColorName(unsigned int color); + void Printf(int x , int y , char const * format,...); + void putChar(int x, int y, char ch); + void putStr(int x, int y, char *str); + void Clear(void); + void Blit(int, int, Cucul* c1, Cucul* c2); + void Invert(); + void Flip(); + void Flop(); + void Rotate(); + void drawLine(int, int, int, int, char const *); + void drawPolyline(int const x[], int const y[], int, char const *); + void drawThinLine(int, int, int, int); + void drawThinPolyline(int const x[], int const y[], int); + void drawCircle(int, int, int, char const *); + void drawEllipse(int, int, int, int, char const *); + void drawThinEllipse(int, int, int, int); + void fillEllipse(int, int, int, int, char const *); + void drawBox(int, int, int, int, char const *); + void drawThinBox(int, int, int, int); + void fillBox(int, int, int, int, char const *); + void drawTriangle(int, int, int, int, int, int, char const *); + void drawThinTriangle(int, int, int, int, int, int); + void fillTriangle(int, int, int, int, int, int, char const *); + int Rand(int, int); + + protected: + cucul_canvas_t *get_cucul_canvas_t(); + + private: + cucul_canvas_t *cv; +}; + + +#endif /* _CUCUL_PP_H */ diff --git a/cxx/cucul++.pc.in b/cxx/cucul++.pc.in new file mode 100644 index 0000000..5ea9ebe --- /dev/null +++ b/cxx/cucul++.pc.in @@ -0,0 +1,12 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: cucul++ +Description: Canvas for ultrafast compositing of Unicode letters C++ binding +Version: @VERSION@ +Requires: +Conflicts: +Libs: -L${libdir} -lcucul -lcaca +Cflags: -I${includedir}