source: libcaca/trunk/cxx/cxxtest.cpp @ 1310

Last change on this file since 1310 was 1310, checked in by Jean-Yves Lamoureux, 13 years ago
  • Fixed behaviour of the pig while resizing window
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1/*
2 *  cpptest       libcaca++ rendering test
3 *  Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
4 *                All Rights Reserved
5 *
6 *  $Id: cxxtest.cpp 1310 2006-11-09 09:27:35Z jylam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14#include "config.h"
15
16#include <iostream>
17
18#include <cucul++.h>
19#include <caca++.h>
20
21using namespace std;
22
23
24static char const pigstring[] =
25    "                                   \n"
26    "                             _     \n"
27    "    _._ _..._ .-',     _.._(`))    \n"
28    "   '-. `     '  /-._.-'    ',/     \n"
29    "      )         \\            '.    \n"
30    "     / _    _    |             \\   \n"
31    "    |  a    a    /              |  \n"
32    "    \\   .-.                     ;  \n"
33    "     '-('' ).-'       ,'       ;   \n"
34    "        '-;           |      .'    \n"
35    "           \\           \\    /      \n"
36    "           | 7  .__  _.-\\   \\      \n"
37    "           | |  |  ``/  /`  /      \n"
38    "      jgs /,_|  |   /,_/   /       \n"
39    "             /,_/      '`-'        \n";
40
41int main(int argc, char *argv[])
42{
43    Cucul *qq, *pig;
44    Caca  *kk;
45    Event ev;
46
47    int x = 0, y = 0, ix = 1, iy = 1;
48
49    try {
50        qq = new Cucul();
51    }
52    catch (int e) {
53        cerr << "Error while initializing cucul (" << e << ")" << endl;
54        return -1;
55    }
56
57    try {
58        kk = new Caca(qq);
59    }
60    catch(int e) {
61        cerr << "Error while attaching cucul to caca (" << e << ")" << endl;
62        return -1;
63    }
64
65    try {
66        // Import buffer into a canvas
67        Buffer *buf = new Buffer();
68        buf->loadMemory((void *)pigstring, strlen(pigstring));
69        pig = new Cucul(buf, "text");
70        delete buf;
71        // Change colour to magenta
72        pig->setColorANSI(CUCUL_LIGHTMAGENTA, CUCUL_TRANSPARENT);
73        for(int y = 0; y < pig->getHeight(); y++)
74            for(int x = 0; x < pig->getWidth(); x++)
75                pig->putChar(x, y, pig->getChar(x, y));
76    }
77    catch(int e) {
78        cerr << "Error while importing image (" << e << ")" << endl;
79        return -1;
80    }
81
82    kk->setDisplayTime(20000);
83
84    while(!kk->getEvent(ev.CACA_EVENT_KEY_PRESS, &ev, 0)) {
85
86
87        /* In case of resize ...*/
88        if((x + pig->getWidth())-1 >= qq->getWidth() || x < 0 )
89            x = 0;
90        if((y + pig->getHeight())-1 >= qq->getHeight() || y < 0 )
91            y = 0;
92
93
94
95
96        qq->Clear();
97
98        /* Draw pig */
99        qq->Blit(x, y, pig, NULL);
100
101        /* printf works */
102        qq->setColorANSI(CUCUL_LIGHTBLUE, CUCUL_BLACK);
103        qq->Printf(qq->getWidth() / 2 - 10, qq->getHeight() / 2,
104                   "Powered by libcaca %s", VERSION);
105
106        /* Blit */
107        kk->Display();
108
109        x += ix;
110        y += iy;
111
112        if(x + pig->getWidth() >= qq->getWidth() || x < 0 )
113            ix = -ix;
114        if(y + pig->getHeight() >= qq->getHeight() || y < 0 )
115            iy = -iy;
116
117    }
118
119    delete kk;
120    delete qq;
121
122    return 0;
123}
Note: See TracBrowser for help on using the repository browser.