From 9b60c942b4983c237a2d5e4f8aad556d04dec012 Mon Sep 17 00:00:00 2001 From: Jean-Yves Lamoureux Date: Mon, 7 Dec 2009 11:48:01 +0000 Subject: [PATCH] * Added mouse events example --- XCode/libcacaXCode.xcodeproj/project.pbxproj | 2 + examples/Makefile.am | 5 +- examples/mouse.c | 92 ++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 examples/mouse.c diff --git a/XCode/libcacaXCode.xcodeproj/project.pbxproj b/XCode/libcacaXCode.xcodeproj/project.pbxproj index 8d216a4..884fdf5 100644 --- a/XCode/libcacaXCode.xcodeproj/project.pbxproj +++ b/XCode/libcacaXCode.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXFileReference section */ + E68A871110CD1F040047EB17 /* mouse.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = mouse.c; path = ../examples/mouse.c; sourceTree = SOURCE_ROOT; }; E6A7681710B560BB008B6DEC /* caca++.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = "caca++.cpp"; path = "../cxx/caca++.cpp"; sourceTree = SOURCE_ROOT; }; E6A7681810B560BB008B6DEC /* caca++.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "caca++.h"; path = "../cxx/caca++.h"; sourceTree = SOURCE_ROOT; }; E6A7681910B560BB008B6DEC /* cxxtest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = cxxtest.cpp; path = ../cxx/cxxtest.cpp; sourceTree = SOURCE_ROOT; }; @@ -273,6 +274,7 @@ E6DB665310AECDF500B6F924 /* truecolor.c */, E6DB665410AECDF500B6F924 /* unicode.c */, E6DB663910AECD1F00B6F924 /* trifiller.c */, + E68A871110CD1F040047EB17 /* mouse.c */, ); name = examples; sourceTree = ""; diff --git a/examples/Makefile.am b/examples/Makefile.am index f542dbc..84edd2d 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -2,7 +2,7 @@ AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/caca -I$(top_builddir)/caca -noinst_PROGRAMS = trifiller blit canvas colors conio $(conio_snake) demo demo0 dithering driver event export figfont font font2tga frames fullwidth gamma hsv input spritedit swallow text transform truecolor unicode import +noinst_PROGRAMS = trifiller blit canvas colors conio $(conio_snake) demo demo0 dithering driver event export figfont font font2tga frames fullwidth gamma hsv input spritedit swallow text transform truecolor unicode import mouse if USE_CXX conio_snake = conio-snake @@ -94,5 +94,8 @@ unicode_LDADD = ../caca/libcaca.la import_SOURCES = import.c import_LDADD = ../caca/libcaca.la +mouse_SOURCES = mouse.c +mouse_LDADD = ../caca/libcaca.la + echo-sources: ; echo $(SOURCES) diff --git a/examples/mouse.c b/examples/mouse.c new file mode 100644 index 0000000..726826c --- /dev/null +++ b/examples/mouse.c @@ -0,0 +1,92 @@ +/* + * mouse libcaca mouse events + * Copyright (c) 2006-2009 Jean-Yves Lamoureux + * All Rights Reserved + * + * $Id: mouse.c 3495 2009-05-21 20:55:21Z jylam $ + * + * This program 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. + */ +#include "config.h" + +#if !defined(__KERNEL__) +# include +# include +# include +#endif + +#include "caca.h" + + +int main(int argc, char *argv[]) +{ + int quit = 0; + int x = 0, y = 0, p = 0, b = 0; + caca_canvas_t *cv; + caca_display_t *dp; + + cv = caca_create_canvas(80, 24); + if (cv == NULL) + { + printf("Failed to create canvas\n"); + return 1; + } + + dp = caca_create_display(cv); + if (dp == NULL) + { + printf("Failed to create display\n"); + return 1; + } + + caca_set_display_time(dp, 40000); + caca_set_cursor(dp, 0); + + while (!quit) + { + caca_event_t ev; + while (caca_get_event(dp, CACA_EVENT_ANY, &ev, 0)) + { + if ((caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)) + { + quit = 1; + } + if (caca_get_event_type(&ev) & CACA_EVENT_MOUSE_MOTION) + { + x = caca_get_event_mouse_x(&ev); + y = caca_get_event_mouse_y(&ev); + } + if (caca_get_event_type(&ev) & CACA_EVENT_MOUSE_PRESS) + { + p = 1; + b = caca_get_event_mouse_button(&ev); + } + else if (caca_get_event_type(&ev) & CACA_EVENT_MOUSE_RELEASE) + { + p = 0; + b = caca_get_event_mouse_button(&ev); + } + } + caca_printf(cv, 0, 0, "%d,%d", x, y); + if (b) + { + caca_printf(cv, 0, 1, "Mouse button %d %s", b, + p == 1 ? "pressed" : "released"); + } + + caca_printf(cv, x - 2, y - 1, " |"); + caca_printf(cv, x - 2, y, "--|--"); + caca_printf(cv, x - 2, y + 1, " |"); + caca_refresh_display(dp); + caca_clear_canvas(cv); + } + + caca_free_display(dp); + caca_free_canvas(cv); + + return 0; +}