1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>
Qt Toolkit - life/main.cpp example file
</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: white; color: black; }
--></style>
</head><body bgcolor="#ffffff">
<table width="100%">
<tr><td><a href="index.html">
<img width="100" height="100" src="qtlogo.png"
alt="Home" border="0"><img width="100"
height="100" src="face.png" alt="Home" border="0">
</a><td valign=top><div align=right><img src="dochead.png" width="472" height="27"><br>
<a href="classes.html"><b>Classes</b></a>
-<a href="annotated.html">Annotated</a>
- <a href="hierarchy.html">Tree</a>
-<a href="functions.html">Functions</a>
-<a href="index.html">Home</a>
-<a href="topicals.html"><b>Structure</b></a>
</div>
</table>
<h1 align=center>Conway's Game of Life</h1><br clear="all">
???
<hr>
Header file: <pre>/****************************************************************************
** $Id: qt/examples/life/life.h 2.3.2 edited 2001-01-26 $
**
** 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 <<a href="qframe-h.html">qframe.h</a>>
class LifeWidget : public QFrame
{
Q_OBJECT
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 QPoint &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>
<hr>
Implementation: <pre>/****************************************************************************
** $Id: qt/examples/life/life.cpp 2.3.2 edited 2001-01-26 $
**
** 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 <<a href="qpainter-h.html">qpainter.h</a>>
#include <<a href="qdrawutil-h.html">qdrawutil.h</a>>
#include <<a href="qcheckbox-h.html">qcheckbox.h</a>>
#include <<a href="qevent-h.html">qevent.h</a>>
#include <<a href="qapplication-h.html">qapplication.h</a>>
// The main game of life widget
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#c0b5fb">setMinimumSize</a>( MINSIZE * SCALE + 2 * BORDER,
MINSIZE * SCALE + 2 * BORDER );
<a href="qwidget.html#c78dce">setMaximumSize</a>( MAXSIZE * SCALE + 2 * BORDER,
MAXSIZE * SCALE + 2 * BORDER );
<a href="qwidget.html#baa891">setSizeIncrement</a>( SCALE, SCALE);
<a href=#181>clear</a>();
<a href="qwidget.html#ff9d07">resize</a>( maxi * SCALE + 2 * BORDER , maxj * SCALE + 2 * BORDER );
}
void <a name="181"></a>LifeWidget::clear()
{
current = 0;
for ( int t = 0; t < 2; t++ )
for ( int i = 0; i < MAXSIZE + 2; i++ )
for ( int j = 0; j < MAXSIZE + 2; j++ )
cells[t][i][j] = FALSE;
<a href="qwidget.html#d5cfc3">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
void <a name="182"></a>LifeWidget::resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * e )
{
maxi = (e->size().width() - 2 * BORDER) / SCALE;
maxj = (e->size().height() - 2 * BORDER) / SCALE;
}
void <a name="183"></a>LifeWidget::setPoint( int i, int j )
{
if ( i < 1 || i > maxi || j < 1 || j > maxi )
return;
cells[current][i][j] = TRUE;
<a href="qwidget.html#d5cfc3">repaint</a>( index2pos(i), index2pos(j), SCALE, SCALE, FALSE );
}
void <a name="184"></a>LifeWidget::mouseHandle( const QPoint &pos )
{
int i = pos2index( pos.x() );
int j = pos2index( pos.y() );
<a href=#183>setPoint</a>( i, j );
}
void <a name="185"></a>LifeWidget::mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> *e )
{
<a href=#184>mouseHandle</a>( e->pos() );
}
void <a name="186"></a>LifeWidget::mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> *e )
{
if ( e->button() == QMouseEvent::LeftButton )
<a href=#184>mouseHandle</a>( e->pos() );
}
void <a name="187"></a>LifeWidget::nextGeneration()
{
for ( int i = 1; i <= MAXSIZE; i++ ) {
for ( int j = 1; j <= 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 && cells[current][i][j] );
}
}
current = !current;
<a href="qwidget.html#d5cfc3">repaint</a>( FALSE ); // repaint without erase
}
void <a name="188"></a>LifeWidget::paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * e )
{
int starti = pos2index( e-><a href="qpaintevent.html#2d6e18">rect</a>().left() );
int stopi = pos2index( e-><a href="qpaintevent.html#2d6e18">rect</a>().right() );
int startj = pos2index( e-><a href="qpaintevent.html#2d6e18">rect</a>().top() );
int stopj = pos2index( e-><a href="qpaintevent.html#2d6e18">rect</a>().bottom() );
if (stopi > maxi)
stopi = maxi;
if (stopj > maxj)
stopj = maxj;
<a href="qpainter.html">QPainter</a> paint( this );
for ( int i = starti; i <= stopi; i++ ) {
for ( int j = startj; j <= stopj; j++ ) {
if ( cells[current][i][j] )
<a href="qpainter.html#b03ffb">qDrawShadePanel</a>( &paint, index2pos( i ), index2pos( j ),
SCALE - 1, SCALE - 1, <a href="qwidget.html#d92bf4">colorGroup</a>() );
else if ( cells[!current][i][j] )
paint.<a href="qpainter.html#bd5570">eraseRect</a>( index2pos( i ), index2pos( j ),
SCALE - 1, SCALE - 1);
}
}
<a href="qframe.html#0dc534">drawFrame</a>( &paint );
}
</pre>
<hr>
Main:
<pre>/****************************************************************************
** $Id: qt/examples/life/main.cpp 2.3.2 edited 2001-06-12 $
**
** 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 <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>
#include <stdlib.h>
void usage()
{
<a name="qWarning"></a><a href="qapplication.html#290ef4">qWarning</a>( "Usage: life [-scale scale]" );
}
int main( int argc, char **argv )
{
<a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv );
int scale = 10;
for ( int i = 1; i < argc; i++ ){
<a name="QString"></a><a href="qstring.html">QString</a> arg = argv[i];
if ( arg == "-scale" )
scale = atoi( argv[++i] );
else {
usage();
exit(1);
}
}
if ( scale < 2 )
scale = 2;
LifeDialog *life = new LifeDialog( scale );
a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( life );
life-><a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - Life");
life-><a name="show"></a><a href="qwidget.html#200ee5">show</a>();
return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();
}
</pre>
<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright 2001 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.2</div>
</table></div></address></body></html>
|