Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > by-pkgid > 397cf13019f8c526877f271962f26f4c > files > 812

libqt3-devel-3.1.1-13mdk.ppc.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/reggie/tmp/qt-3.1-reggie-23625/qt-x11-free-3.1.1/examples/life/life.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Conway's Game of Life</title>
<style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Conway's Game of Life</h1>

   
<p> 
<hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id: qt/life.h   3.1.1   edited Nov 8 10:35 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef LIFE_H
#define LIFE_H

#include &lt;<a href="qframe-h.html">qframe.h</a>&gt;


class LifeWidget : public <a href="qframe.html">QFrame</a>
{
    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
public:
    LifeWidget( int s = 10, QWidget *parent = 0, const char *name = 0 );

    void        setPoint( int i, int j );

    int         maxCol() { return maxi; }
    int         maxRow() { return maxj; }

public slots:
    void        nextGeneration();
    void        clear();

protected:
    virtual void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );
    virtual void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
    virtual void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
    virtual void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * );
    void         mouseHandle( const <a href="qpoint.html">QPoint</a> &amp;pos );

private:
    enum { MAXSIZE = 50, MINSIZE = 10, BORDER = 5 };

    bool        cells[2][MAXSIZE + 2][MAXSIZE + 2];
    int         current;
    int         maxi, maxj;

    int pos2index( int x )
    {
        return ( x - BORDER ) / SCALE + 1;
    }
    int index2pos( int i )
    {
        return  ( i - 1 ) * SCALE + BORDER;
    }

    int SCALE;
};


#endif // LIFE_H
</pre>

<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id: qt/life.cpp   3.1.1   edited Nov 8 10:35 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "life.h"

#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
#include &lt;<a href="qdrawutil-h.html">qdrawutil.h</a>&gt;
#include &lt;<a href="qcheckbox-h.html">qcheckbox.h</a>&gt;
#include &lt;<a href="qevent-h.html">qevent.h</a>&gt;
#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;

// The main game of life widget

<a name="f516"></a>LifeWidget::LifeWidget( int s, QWidget *parent, const char *name )
    : <a href="qframe.html">QFrame</a>( parent, name )
{
    SCALE = s;

    maxi = maxj = 50;
    <a href="qwidget.html#setMinimumSize">setMinimumSize</a>( MINSIZE * SCALE + 2 * BORDER,
                   MINSIZE * SCALE + 2 * BORDER );
    <a href="qwidget.html#setMaximumSize">setMaximumSize</a>( MAXSIZE * SCALE + 2 * BORDER,
                   MAXSIZE * SCALE + 2 * BORDER );
    <a href="qwidget.html#setSizeIncrement">setSizeIncrement</a>( SCALE, SCALE);

    clear();
    <a href="qwidget.html#resize">resize</a>( maxi * SCALE + 2 * BORDER , maxj * SCALE + 2 * BORDER );

}


void <a name="f517"></a>LifeWidget::clear()
{
    current = 0;
    for ( int t = 0; t &lt; 2; t++ )
        for ( int i = 0; i &lt; MAXSIZE + 2; i++ )
            for ( int j = 0; j &lt; MAXSIZE + 2; j++ )
                cells[t][i][j] = FALSE;

    <a href="qwidget.html#repaint">repaint</a>();
}


// We assume that the size will never be beyond the maximum size set
// this is not in general TRUE, but in practice it's good enough for
// this program

<a name="x1836"></a>void LifeWidget::<a href="qframe.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">QResizeEvent</a> * e )
{
<a name="x1842"></a>    maxi = (e-&gt;<a href="qresizeevent.html#size">size</a>().width()  - 2 * BORDER) / SCALE;
    maxj = (e-&gt;<a href="qresizeevent.html#size">size</a>().height() - 2 * BORDER) / SCALE;
}


void <a name="f518"></a>LifeWidget::setPoint( int i, int j )
{
    if ( i &lt; 1 || i &gt; maxi || j &lt; 1 || j &gt; maxi )
        return;
    cells[current][i][j] = TRUE;
    <a href="qwidget.html#repaint">repaint</a>( index2pos(i), index2pos(j), SCALE, SCALE, FALSE );
}


void <a name="f519"></a>LifeWidget::mouseHandle( const <a href="qpoint.html">QPoint</a> &amp;pos )
{
<a name="x1840"></a>    int i = pos2index( pos.<a href="qpoint.html#x">x</a>() );
<a name="x1841"></a>    int j = pos2index( pos.<a href="qpoint.html#y">y</a>() );
    setPoint( i, j );
}


<a name="x1843"></a>void LifeWidget::<a href="qwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
{
<a name="x1838"></a>    mouseHandle( e-&gt;<a href="qmouseevent.html#pos">pos</a>() );
}


<a name="x1844"></a>void LifeWidget::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
{
<a name="x1837"></a>    if ( e-&gt;<a href="qmouseevent.html#button">button</a>() == QMouseEvent::LeftButton )
        mouseHandle( e-&gt;<a href="qmouseevent.html#pos">pos</a>() );
}


void <a name="f520"></a>LifeWidget::nextGeneration()
{
    for ( int i = 1; i &lt;= MAXSIZE; i++ ) {
        for ( int j = 1; j &lt;= MAXSIZE; j++ ) {
            int t = cells[current][i - 1][j - 1]
            + cells[current][i - 1][j]
            + cells[current][i - 1][j + 1]
            + cells[current][i][j - 1]
            + cells[current][i][j + 1]
            + cells[current][i + 1][j - 1]
            + cells[current][i + 1][j]
            + cells[current][i + 1][j + 1];

            cells[!current][i][j] = ( t == 3 ||
                                      t == 2 &amp;&amp; cells[current][i][j] );
        }
    }
    current = !current;
    <a href="qwidget.html#repaint">repaint</a>( FALSE );           // repaint without erase
}


<a name="x1835"></a>void LifeWidget::<a href="qframe.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * e )
{
<a name="x1839"></a>    int starti = pos2index( e-&gt;<a href="qpaintevent.html#rect">rect</a>().left() );
    int stopi  = pos2index( e-&gt;<a href="qpaintevent.html#rect">rect</a>().right() );
    int startj = pos2index( e-&gt;<a href="qpaintevent.html#rect">rect</a>().top() );
    int stopj  = pos2index( e-&gt;<a href="qpaintevent.html#rect">rect</a>().bottom() );

    if (stopi &gt; maxi)
        stopi = maxi;
    if (stopj &gt; maxj)
        stopj = maxj;

    <a href="qpainter.html">QPainter</a> paint( this );
    for ( int i = starti; i &lt;= stopi; i++ ) {
        for ( int j = startj; j &lt;= stopj; j++ ) {
            if ( cells[current][i][j] )
                <a href="qpainter.html#qDrawShadePanel">qDrawShadePanel</a>( &amp;paint, index2pos( i ), index2pos( j ),
                                 SCALE - 1, SCALE - 1, colorGroup() );
            else if ( cells[!current][i][j] )
                <a href="qwidget.html#erase">erase</a>(index2pos( i ), index2pos( j ), SCALE - 1, SCALE - 1);
        }
    }
    <a href="qframe.html#drawFrame">drawFrame</a>( &amp;paint );
}
</pre>

<p> <hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id: qt/main.cpp   3.1.1   edited Nov 8 10:35 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "lifedlg.h"
#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;stdlib.h&gt;

void usage()
{
    <a href="qapplication.html#qWarning">qWarning</a>( "Usage: life [-scale scale]" );
}

int main( int argc, char **argv )
{
    <a href="qapplication.html">QApplication</a> a( argc, argv );

    int scale = 10;

    for ( int i = 1; i &lt; argc; i++ ){
        <a href="qstring.html">QString</a> arg = argv[i];
        if ( arg == "-scale" )
            scale = atoi( argv[++i] );
        else {
            usage();
            exit(1);
        }
    }

    if ( scale &lt; 2 )
        scale = 2;

    LifeDialog *life = new LifeDialog( scale );
    a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( life );
    life-&gt;<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Life");
    life-&gt;<a href="qwidget.html#show">show</a>();

    return a.<a href="qapplication.html#exec">exec</a>();
}
</pre>

<p>See also <a href="examples.html">Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2002 
<a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt version 3.1.1</div>
</table></div></address></body>
</html>