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

Last change on this file since 2075 was 2075, checked in by Sam Hocevar, 12 years ago
  • Fix memory leaks that slipped in here and there. We still have 100,000 fewer than Firefox, lol.
  • Property svn:keywords set to Id
File size: 2.9 KB
RevLine 
[784]1/*
2 *  cpptest       libcaca++ rendering test
3 *  Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
4 *                All Rights Reserved
5 *
[1275]6 *  $Id: cxxtest.cpp 2075 2007-11-26 01:04:34Z sam $
[784]7 *
[1462]8 *  This program is free software. It comes without any warranty, to
[1452]9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
[784]12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
[781]15#include <iostream>
16
17#include <cucul++.h>
18#include <caca++.h>
19
20using namespace std;
21
22
[1282]23static char const pigstring[] =
24    "                                   \n"
25    "                             _     \n"
26    "    _._ _..._ .-',     _.._(`))    \n"
27    "   '-. `     '  /-._.-'    ',/     \n"
28    "      )         \\            '.    \n"
29    "     / _    _    |             \\   \n"
30    "    |  a    a    /              |  \n"
31    "    \\   .-.                     ;  \n"
32    "     '-('' ).-'       ,'       ;   \n"
33    "        '-;           |      .'    \n"
34    "           \\           \\    /      \n"
35    "           | 7  .__  _.-\\   \\      \n"
36    "           | |  |  ``/  /`  /      \n"
37    "      jgs /,_|  |   /,_/   /       \n"
38    "             /,_/      '`-'        \n";
[781]39
40int main(int argc, char *argv[])
41{
[2074]42    Cucul *cv, *pig;
43    Caca  *dp;
[796]44
[785]45    int x = 0, y = 0, ix = 1, iy = 1;
[781]46
47    try {
[2074]48        cv = new Cucul();
[892]49    }
[781]50    catch (int e) {
51        cerr << "Error while initializing cucul (" << e << ")" << endl;
52        return -1;
53    }
[892]54
[782]55    try {
[2074]56        dp = new Caca(cv);
[782]57    }
58    catch(int e) {
59        cerr << "Error while attaching cucul to caca (" << e << ")" << endl;
60        return -1;
61    }
[781]62
[1282]63    try {
64        // Import buffer into a canvas
[1388]65        pig = new Cucul();
[1282]66        pig->setColorANSI(CUCUL_LIGHTMAGENTA, CUCUL_TRANSPARENT);
[1388]67        pig->importMemory(pigstring, strlen(pigstring), "text");
[1282]68    }
69    catch(int e) {
70        cerr << "Error while importing image (" << e << ")" << endl;
71        return -1;
72    }
73
[2074]74    dp->setDisplayTime(20000);
[781]75
[2074]76    while(!dp->getEvent(Event::CACA_EVENT_KEY_PRESS, NULL, 0))
[2070]77    {
[892]78
[1310]79        /* In case of resize ...*/
[2074]80        if((x + pig->getWidth())-1 >= cv->getWidth() || x < 0 )
[1310]81            x = 0;
[2074]82        if((y + pig->getHeight())-1 >= cv->getHeight() || y < 0 )
[1310]83            y = 0;
84
[2074]85        cv->Clear();
[1282]86
[785]87        /* Draw pig */
[2074]88        cv->Blit(x, y, pig, NULL);
[892]89
[785]90        /* printf works */
[2074]91        cv->setColorANSI(CUCUL_LIGHTBLUE, CUCUL_BLACK);
92        cv->Printf(cv->getWidth() / 2 - 10, cv->getHeight() / 2,
93                   "Powered by libcaca %s", dp->getVersion());
[892]94
[785]95        /* Blit */
[2074]96        dp->Display();
[781]97
[1282]98        x += ix;
99        y += iy;
[781]100
[2074]101        if(x + pig->getWidth() >= cv->getWidth() || x < 0 )
[1282]102            ix = -ix;
[2074]103        if(y + pig->getHeight() >= cv->getHeight() || y < 0 )
[1282]104            iy = -iy;
[1310]105
[785]106    }
107
[2074]108    delete dp;
[2075]109    delete pig;
[2074]110    delete cv;
[781]111
112    return 0;
113}
Note: See TracBrowser for help on using the repository browser.