summaryrefslogtreecommitdiff
path: root/noncore/games/tetrix
authorkergoth <kergoth>2002-01-25 22:14:26 (UTC)
committer kergoth <kergoth>2002-01-25 22:14:26 (UTC)
commit15318cad33835e4e2dc620d033e43cd930676cdd (patch) (side-by-side diff)
treec2fa0399a2c47fda8e2cd0092c73a809d17f68eb /noncore/games/tetrix
downloadopie-15318cad33835e4e2dc620d033e43cd930676cdd.zip
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2
Initial revision
Diffstat (limited to 'noncore/games/tetrix') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/tetrix/.cvsignore3
-rw-r--r--noncore/games/tetrix/Makefile.in157
-rw-r--r--noncore/games/tetrix/gtetrix.cpp514
-rw-r--r--noncore/games/tetrix/gtetrix.h104
-rw-r--r--noncore/games/tetrix/main.cpp33
-rw-r--r--noncore/games/tetrix/qpe-tetrix.control10
-rw-r--r--noncore/games/tetrix/qtetrix.cpp170
-rw-r--r--noncore/games/tetrix/qtetrix.h78
-rw-r--r--noncore/games/tetrix/qtetrixb.cpp251
-rw-r--r--noncore/games/tetrix/qtetrixb.h80
-rw-r--r--noncore/games/tetrix/tetrix.pro17
-rw-r--r--noncore/games/tetrix/tpiece.cpp201
-rw-r--r--noncore/games/tetrix/tpiece.h62
13 files changed, 1680 insertions, 0 deletions
diff --git a/noncore/games/tetrix/.cvsignore b/noncore/games/tetrix/.cvsignore
new file mode 100644
index 0000000..edfa921
--- a/dev/null
+++ b/noncore/games/tetrix/.cvsignore
@@ -0,0 +1,3 @@
+moc_*
+*.moc
+Makefile
diff --git a/noncore/games/tetrix/Makefile.in b/noncore/games/tetrix/Makefile.in
new file mode 100644
index 0000000..3a74fdc
--- a/dev/null
+++ b/noncore/games/tetrix/Makefile.in
@@ -0,0 +1,157 @@
+#############################################################################
+
+####### Compiler, tools and options
+
+CXX = $(SYSCONF_CXX) $(QT_CXX_MT)
+CXXFLAGS= $(SYSCONF_CXXFLAGS_QT) $(SYSCONF_CXXFLAGS)
+CC = $(SYSCONF_CC) $(QT_C_MT)
+CFLAGS = $(SYSCONF_CFLAGS)
+INCPATH = -I$(QPEDIR)/include
+LFLAGS = $(SYSCONF_LFLAGS_QT) $(SYSCONF_RPATH_QT) $(SYSCONF_LFLAGS) $(QT_LFLAGS_MT)
+LIBS = $(SUBLIBS) -lqpe $(SYSCONF_LIBS_QT) $(SYSCONF_LIBS) $(SYSCONF_LIBS_QTAPP)
+MOC = $(SYSCONF_MOC)
+UIC = $(SYSCONF_UIC)
+
+####### Target
+
+DESTDIR = ../bin/
+VER_MAJ = 1
+VER_MIN = 0
+VER_PATCH = 0
+TARGET = tetrix
+TARGET1 = lib$(TARGET).so.$(VER_MAJ)
+
+####### Files
+
+HEADERS = minefield.h \
+ gtetrix.h \
+ qtetrix.h \
+ qtetrixb.h \
+ tpiece.h
+SOURCES = main.cpp \
+ gtetrix.cpp \
+ qtetrix.cpp \
+ qtetrixb.cpp \
+ tpiece.cpp
+OBJECTS = main.o \
+ gtetrix.o \
+ qtetrix.o \
+ qtetrixb.o \
+ tpiece.o
+INTERFACES =
+UICDECLS =
+UICIMPLS =
+SRCMOC = moc_qtetrix.cpp \
+ moc_qtetrixb.cpp
+OBJMOC = moc_qtetrix.o \
+ moc_qtetrixb.o
+
+
+####### Implicit rules
+
+.SUFFIXES: .cpp .cxx .cc .C .c
+
+.cpp.o:
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
+
+.cxx.o:
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
+
+.cc.o:
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
+
+.C.o:
+ $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
+
+.c.o:
+ $(CC) -c $(CFLAGS) $(INCPATH) -o $@ $<
+
+####### Build rules
+
+
+all: $(DESTDIR)$(TARGET)
+
+$(DESTDIR)$(TARGET): $(UICDECLS) $(OBJECTS) $(OBJMOC) $(SUBLIBS)
+ $(SYSCONF_LINK) $(LFLAGS) -o $(DESTDIR)$(TARGET) $(OBJECTS) $(OBJMOC) $(LIBS)
+
+moc: $(SRCMOC)
+
+tmake:
+ tmake tetrix.pro
+
+clean:
+ -rm -f $(OBJECTS) $(OBJMOC) $(SRCMOC) $(UICIMPLS) $(UICDECLS)
+ -rm -f *~ core
+ -rm -f allmoc.cpp
+
+####### Extension Modules
+
+listpromodules:
+ @echo
+
+listallmodules:
+ @echo
+
+listaddonpromodules:
+ @echo
+
+listaddonentmodules:
+ @echo
+
+
+REQUIRES=
+
+####### Sub-libraries
+
+
+###### Combined headers
+
+
+
+####### Compile
+
+main.o: main.cpp \
+ qtetrix.h \
+ qtetrixb.h \
+ gtetrix.h \
+ tpiece.h \
+ $(QPEDIR)/include/qpe/qpeapplication.h
+
+gtetrix.o: gtetrix.cpp \
+ gtetrix.h \
+ tpiece.h
+
+qtetrix.o: qtetrix.cpp \
+ qtetrix.h \
+ qtetrixb.h \
+ gtetrix.h \
+ tpiece.h \
+ $(QPEDIR)/include/qpe/resource.h
+
+qtetrixb.o: qtetrixb.cpp \
+ qtetrixb.h \
+ gtetrix.h \
+ tpiece.h \
+ qtetrix.h
+
+tpiece.o: tpiece.cpp \
+ tpiece.h
+
+moc_qtetrix.o: moc_qtetrix.cpp \
+ qtetrix.h \
+ qtetrixb.h \
+ gtetrix.h \
+ tpiece.h
+
+moc_qtetrixb.o: moc_qtetrixb.cpp \
+ qtetrixb.h \
+ gtetrix.h \
+ tpiece.h
+
+moc_qtetrix.cpp: qtetrix.h
+ $(MOC) qtetrix.h -o moc_qtetrix.cpp
+
+moc_qtetrixb.cpp: qtetrixb.h
+ $(MOC) qtetrixb.h -o moc_qtetrixb.cpp
+
+
diff --git a/noncore/games/tetrix/gtetrix.cpp b/noncore/games/tetrix/gtetrix.cpp
new file mode 100644
index 0000000..d1f38b1
--- a/dev/null
+++ b/noncore/games/tetrix/gtetrix.cpp
@@ -0,0 +1,514 @@
+/**********************************************************************
+** Copyright (C) 2000 Trolltech AS. All rights reserved.
+**
+** This file is part of Qtopia Environment.
+**
+** This file may be distributed and/or modified under the terms of the
+** GNU General Public License version 2 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** See http://www.trolltech.com/gpl/ for GPL licensing information.
+**
+** Contact info@trolltech.com if any conditions of this licensing are
+** not clear to you.
+**
+**********************************************************************/
+
+
+#include "gtetrix.h"
+
+#include <string.h>
+
+GenericTetrix::GenericTetrix(int boardWidth,int boardHeight)
+{
+ int i,j;
+
+ width = boardWidth;
+ height = boardHeight;
+ boardPtr = new int[height*width]; // Note the order, this makes it easier
+ // to remove full lines.
+ for(i = 0 ; i < height ; i++)
+ for(j = 0 ; j < width ; j++)
+ board(j,i) = 0;
+ currentLine = -1; // -1 if no falling piece.
+ currentPos = 0;
+ showNext = 0; // FALSE
+ nLinesRemoved = 0;
+ nPiecesDropped = 0;
+ score = 0;
+ level = 1;
+ gameID = 0;
+ nClearLines = height;
+}
+
+GenericTetrix::~GenericTetrix()
+{
+ delete[] boardPtr;
+}
+
+
+void GenericTetrix::clearBoard(int fillRandomLines)
+{
+ int i,j;
+
+ if (fillRandomLines >= height)
+ fillRandomLines = height - 1;
+
+ erasePiece();
+ for(i = height - nClearLines - 1 ; i >= fillRandomLines ; i--)
+ for(j = 0 ; j < width ; j++)
+ if (board(j,i) != 0) {
+ draw(j,i,0);
+ board(j,i) = 0;
+ }
+ if (fillRandomLines != 0)
+ for (i = 0 ; i < fillRandomLines ; i++) {
+ fillRandom(i);
+ }
+ nClearLines = height - fillRandomLines;
+}
+
+void GenericTetrix::showBoard()
+{
+ int i,j;
+
+ showPiece();
+ for(i = height - nClearLines - 1 ; i >= 0 ; i--)
+ for(j = 0 ; j < width ; j++)
+ if (board(j,i) != 0)
+ draw(j,i,board(j,i));
+}
+
+void GenericTetrix::hideBoard()
+{
+ int i,j;
+
+ erasePiece();
+ for(i = height - nClearLines - 1 ; i >= 0 ; i--)
+ for(j = 0 ; j < width ; j++)
+ if (board(j,i) != 0)
+ draw(j,i,0);
+}
+
+void GenericTetrix::startGame(int gameType,int fillRandomLines)
+{
+ gameID = gameType;
+ clearBoard(fillRandomLines);
+ nLinesRemoved = 0;
+ updateRemoved(nLinesRemoved);
+ nClearLines = height;
+ nPiecesDropped = 0;
+ score = 0;
+ updateScore(score);
+ level = 1;
+ updateLevel(level);
+ newPiece();
+}
+
+void GenericTetrix::revealNextPiece(int revealIt)
+{
+ if (showNext == revealIt)
+ return;
+ showNext = revealIt;
+ if (!showNext)
+ eraseNextPiece();
+ else
+ showNextPiece();
+}
+
+void GenericTetrix::updateBoard(int x1,int y1,int x2, int y2,
+ int dontUpdateBlanks)
+{
+ int i,j;
+ int tmp;
+
+ if (x1 > x2) {
+ tmp = x2;
+ x2 = x1;
+ x1 = tmp;
+ }
+ if (y1 > y2) {
+ tmp = y2;
+ y2 = y1;
+ y1 = tmp;
+ }
+ if (x1 < 0)
+ x1 = 0;
+ if (x2 >= width)
+ x2 = width - 1;
+ if (y1 < 0)
+ y1 = 0;
+ if (y2 >= height)
+ y2 = height - 1;
+ for(i = y1 ; i <= y2 ; i++)
+ for(j = x1 ; j <= x2 ; j++)
+ if (!dontUpdateBlanks || board(j,height - i - 1) != 0)
+ draw(j,height - i - 1,board(j,height - i - 1));
+ showPiece(); // Remember to update piece correctly!!!!
+}
+
+
+void GenericTetrix::fillRandom(int line)
+{
+ int i,j;
+ int holes;
+
+ for(i = 0 ; i < width ; i++)
+ board(i,line) = TetrixPiece::randomValue(7);
+ holes = 0;
+ for(i = 0 ; i < width ; i++)
+ if (board(i,line) == 0) // Count holes in the line.
+ holes++;
+ if (holes == 0) // Full line, make a random hole:
+ board(TetrixPiece::randomValue(width),line) = 0;
+ if (holes == width) // Empty line, make a random square:
+ board(TetrixPiece::randomValue(width),line) =
+ TetrixPiece::randomValue(6) + 1;
+ for(j = 0 ; j < width ; j++)
+ draw(j,i,board(j,i));
+}
+
+void GenericTetrix::moveLeft(int steps)
+{
+ while(steps) {
+ if (!canMoveTo(currentPos - 1,currentLine))
+ return;
+ moveTo(currentPos - 1,currentLine);
+ steps--;
+ }
+}
+
+void GenericTetrix::moveRight(int steps)
+{
+ while(steps) {
+ if (!canMoveTo(currentPos + 1,currentLine))
+ return;
+ moveTo(currentPos + 1,currentLine);
+ steps--;
+ }
+}
+
+void GenericTetrix::rotateLeft()
+{
+ TetrixPiece tmp(currentPiece);
+
+ tmp.rotateLeft();
+ if (!canPosition(tmp))
+ return;
+ position(tmp);
+ currentPiece = tmp;
+}
+
+void GenericTetrix::rotateRight()
+{
+ TetrixPiece tmp(currentPiece);
+
+ tmp.rotateRight();
+ if (!canPosition(tmp))
+ return;
+ position(tmp);
+ currentPiece = tmp;
+}
+
+void GenericTetrix::dropDown()
+{
+ if (currentLine == -1)
+ return;
+
+ int dropHeight = 0;
+ int newLine = currentLine;
+ while(newLine) {
+ if (!canMoveTo(currentPos,newLine - 1))
+ break;
+ newLine--;
+ dropHeight++;
+ }
+ if (dropHeight != 0)
+ moveTo(currentPos,newLine);
+ internalPieceDropped(dropHeight);
+}
+
+void GenericTetrix::oneLineDown()
+{
+ if (currentLine == -1)
+ return;
+ if (canMoveTo(currentPos,currentLine - 1)) {
+ moveTo(currentPos,currentLine - 1);
+ } else {
+ internalPieceDropped(0);
+ }
+}
+
+void GenericTetrix::newPiece()
+{
+ currentPiece = nextPiece;
+ if (showNext)
+ eraseNextPiece();
+ nextPiece.setRandomType();
+ if (showNext)
+ showNextPiece();
+ currentLine = height - 1 + currentPiece.getMinY();
+ currentPos = width/2 + 1;
+ if (!canMoveTo(currentPos,currentLine)) {
+ currentLine = -1;
+ gameOver();
+ } else {
+ showPiece();
+ }
+}
+
+void GenericTetrix::removePiece()
+{
+ erasePiece();
+ currentLine = -1;
+}
+
+void GenericTetrix::drawNextSquare(int,int,int)
+{
+
+}
+
+void GenericTetrix::pieceDropped(int)
+{
+ newPiece();
+}
+
+void GenericTetrix::updateRemoved(int)
+{
+}
+
+void GenericTetrix::updateScore(int)
+{
+}
+
+void GenericTetrix::updateLevel(int)
+{
+}
+
+void GenericTetrix::removeFullLines()
+{
+ int i,j,k;
+ int nFullLines;
+
+ for(i = 0 ; i < height - nClearLines ; i++) {
+ for(j = 0 ; j < width ; j++)
+ if (board(j,i) == 0)
+ break;
+ if (j == width) {
+ nFullLines = 1;
+ for(k = i + 1 ; k < height - nClearLines ; k++) {
+ for(j = 0 ; j < width ; j++)
+ if (board(j,k) == 0)
+ break;
+ if (j == width) {
+ nFullLines++;
+ } else {
+ for(j = 0 ; j < width ; j++) {
+ if (board(j,k - nFullLines) != board(j,k)) {
+ board(j,k - nFullLines) = board(j,k);
+ draw( j,k - nFullLines,
+ board(j,k - nFullLines));
+ }
+ }
+ }
+ }
+ nClearLines = nClearLines + nFullLines;
+ nLinesRemoved = nLinesRemoved + nFullLines;
+ updateRemoved(nLinesRemoved);
+ score = score + 10*nFullLines; // updateScore must be
+ // called by caller!
+ for (i = height - nClearLines ;
+ i < height - nClearLines + nFullLines ;
+ i++)
+ for(j = 0 ; j < width ; j++)
+ if (board(j,i) != 0) {
+ draw(j,i,0);
+ board(j,i) = 0;
+ }
+ }
+ }
+}
+
+void GenericTetrix::showPiece()
+{
+ int x,y;
+
+ if (currentLine == -1)
+ return;
+
+ for(int i = 0 ; i < 4 ; i++) {
+ currentPiece.getCoord(i,x,y);
+ draw(currentPos + x,currentLine - y,currentPiece.getType());
+ }
+}
+
+void GenericTetrix::erasePiece()
+{
+ int x,y;
+
+ if (currentLine == -1)
+ return;
+
+ for(int i = 0 ; i < 4 ; i++) {
+ currentPiece.getCoord(i,x,y);
+ draw(currentPos + x,currentLine - y,0);
+ }
+}
+
+void GenericTetrix::internalPieceDropped(int dropHeight)
+{
+ gluePiece();
+ nPiecesDropped++;
+ if (nPiecesDropped % 25 == 0) {
+ level++;
+ updateLevel(level);
+ }
+ score = score + 7 + dropHeight;
+ removeFullLines();
+ updateScore(score);
+ pieceDropped(dropHeight);
+}
+
+void GenericTetrix::gluePiece()
+{
+ int x,y;
+ int min;
+
+ if (currentLine == -1)
+ return;
+
+ for(int i = 0 ; i < 4 ; i++) {
+ currentPiece.getCoord(i,x,y);
+ board(currentPos + x,currentLine - y) = currentPiece.getType();
+ }
+ min = currentPiece.getMinY();
+ if (currentLine - min >= height - nClearLines)
+ nClearLines = height - currentLine + min - 1;
+}
+
+void GenericTetrix::showNextPiece(int erase)
+{
+ int x,y;
+ int minX = nextPiece.getMinX();
+ int minY = nextPiece.getMinY();
+ int maxX = nextPiece.getMaxX();
+ int maxY = nextPiece.getMaxY();
+
+ int xOffset = (3 - (maxX - minX))/2;
+ int yOffset = (3 - (maxY - minY))/2;
+
+ for(int i = 0 ; i < 4 ; i++) {
+ nextPiece.getCoord(i,x,y);
+ if (erase)
+ drawNextSquare(x + xOffset - minX,
+ y + yOffset - minY,0);
+ else
+ drawNextSquare(x + xOffset - minX,
+ y + yOffset - minY,nextPiece.getType());
+ }
+}
+
+int GenericTetrix::canPosition(TetrixPiece &piece)
+{
+ if (currentLine == -1)
+ return 0;
+
+ int x,y;
+
+ for(int i = 0 ; i < 4 ; i++) {
+ piece.getCoord(i,x,y);
+ x = currentPos + x;
+ y = currentLine - y; // Board and pieces have inverted y-coord. systems.
+ if (x < 0 || x >= width || y < 0 || y >= height)
+ return 0; // Outside board, cannot put piece here.
+ if (board(x,y) != 0)
+ return 0; // Over a non-zero square, cannot put piece here.
+ }
+ return 1; // Inside board and no non-zero squares underneath.
+
+}
+
+int GenericTetrix::canMoveTo(int xPosition,int line)
+{
+ if (currentLine == -1)
+ return 0;
+
+ int x,y;
+
+ for(int i = 0 ; i < 4 ; i++) {
+ currentPiece.getCoord(i,x,y);
+ x = xPosition + x;
+ y = line - y; // Board and pieces have inverted y-coord. systems.
+ if (x < 0 || x >= width || y < 0 || y >= height)
+ return 0; // Outside board, cannot put piece here.
+ if (board(x,y) != 0)
+ return 0; // Over a non-zero square, cannot put piece here.
+ }
+ return 1; // Inside board and no non-zero squares underneath.
+}
+
+void GenericTetrix::moveTo(int xPosition,int line)
+{
+ if (currentLine == -1)
+ return;
+ optimizedMove(xPosition,line,currentPiece);
+ currentPos = xPosition;
+ currentLine = line;
+}
+
+void GenericTetrix::position(TetrixPiece &piece)
+{
+ if (currentLine == -1)
+ return;
+
+ optimizedMove(currentPos,currentLine,piece);
+}
+
+void GenericTetrix::optimizedMove(int newPos, int newLine,
+ TetrixPiece &newPiece)
+{
+ int updates [8][3];
+ int nUpdates;
+ int value;
+ int x,y;
+ int i,j;
+
+ for(i = 0 ; i < 4 ; i++) { // Put the erasing coords into updates
+ currentPiece.getCoord(i,x,y);
+ updates[i][0] = currentPos + x;
+ updates[i][1] = currentLine - y;
+ updates[i][2] = 0;
+ }
+ nUpdates = 4;
+ for(i = 0 ; i < 4 ; i++) { // Any drawing coord same as an erasing one?
+ newPiece.getCoord(i,x,y);
+ x = newPos + x;
+ y = newLine - y;
+ for (j = 0 ; j < 4 ; j++)
+ if (updates[j][0] == x && updates[j][1] == y) { // Same coord,
+ // don't have to erase
+ if (currentPiece.getType() == newPiece.getType())
+ updates[j][2] = -1; // Correct on screen, no update!
+ else
+ updates[j][2] = newPiece.getType();
+ break;
+ }
+ if (j == 4) { // This coord does not overlap an erasing one
+ updates[nUpdates][0] = x;
+ updates[nUpdates][1] = y;
+ updates[nUpdates][2] = newPiece.getType();
+ nUpdates++;
+ }
+ }
+ for (i = 0 ; i < nUpdates ; i++) { // Do the updating
+ x = updates[i][0];
+ y = updates[i][1];
+ value = updates[i][2];
+ if (value != -1) // Only update if new value != current
+ draw(x,y,value);
+ }
+}
diff --git a/noncore/games/tetrix/gtetrix.h b/noncore/games/tetrix/gtetrix.h
new file mode 100644
index 0000000..520dd89
--- a/dev/null
+++ b/noncore/games/tetrix/gtetrix.h
@@ -0,0 +1,104 @@
+/**********************************************************************
+** Copyright (C) 2000 Trolltech AS. All rights reserved.
+**
+** This file is part of Qtopia Environment.
+**
+** This file may be distributed and/or modified under the terms of the
+** GNU General Public License version 2 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** See http://www.trolltech.com/gpl/ for GPL licensing information.
+**
+** Contact info@trolltech.com if any conditions of this licensing are
+** not clear to you.
+**
+**********************************************************************/
+
+
+#ifndef GTETRIX_H
+#define GTETRIX_H
+
+#include "tpiece.h"
+
+
+class GenericTetrix
+{
+public:
+ GenericTetrix(int boardWidth = 10,int boardHeight = 22);
+ virtual ~GenericTetrix();
+
+ void clearBoard(int fillRandomLines = 0);
+ void revealNextPiece(int revealIt);
+ void updateBoard(int x1,int y1,int x2,int y2,int dontUpdateBlanks = 0);
+ void updateNext(){if (showNext) showNextPiece();}
+ void hideBoard();
+ void showBoard();
+ void fillRandom(int line);
+
+ void moveLeft(int steps = 1);
+ void moveRight(int steps = 1);
+ void rotateLeft();
+ void rotateRight();
+ void dropDown();
+ void oneLineDown();
+ void newPiece();
+ void removePiece();
+
+ int noOfClearLines() {return nClearLines;}
+ int getLinesRemoved() {return nLinesRemoved;}
+ int getPiecesDropped() {return nPiecesDropped;}
+ int getScore() {return score;}
+ int getLevel() {return level;}
+ int boardHeight() {return height;}
+ int boardWidth() {return width;}
+
+ virtual void drawSquare(int x,int y,int value) = 0;
+ virtual void gameOver() = 0;
+
+ virtual void startGame(int gameType = 0,int fillRandomLines = 0);
+ virtual void drawNextSquare(int x,int y,int value);
+ virtual void pieceDropped(int dropHeight);
+ virtual void updateRemoved(int noOfLines);
+ virtual void updateScore(int newScore);
+ virtual void updateLevel(int newLevel);
+
+private:
+ void draw(int x, int y, int value){drawSquare(x,height - y,value);}
+ void removeFullLines();
+ void removeLine(int line);
+ void showPiece();
+ void erasePiece();
+ void internalPieceDropped(int dropHeight);
+ void gluePiece();
+ void showNextPiece(int erase = 0);
+ void eraseNextPiece(){showNextPiece(1);};
+ int canPosition(TetrixPiece &piece); // Returns a boolean value.
+ int canMoveTo(int xPosition, int line); // Returns a boolean value.
+ void moveTo(int xPosition,int line);
+ void position(TetrixPiece &piece);
+ void optimizedMove(int newPos, int newLine,TetrixPiece &newPiece);
+
+ int &board(int x,int y){return boardPtr[width*y + x];}
+
+ TetrixPiece currentPiece;
+ TetrixPiece nextPiece;
+ int currentLine;
+ int currentPos;
+ int showNext; // Boolean variable.
+ int nLinesRemoved;
+ int nPiecesDropped;
+ int score;
+ int level;
+ int gameID;
+ int nClearLines;
+ int width;
+ int height;
+ int *boardPtr;
+};
+
+
+#endif
diff --git a/noncore/games/tetrix/main.cpp b/noncore/games/tetrix/main.cpp
new file mode 100644
index 0000000..e36d52d
--- a/dev/null
+++ b/noncore/games/tetrix/main.cpp
@@ -0,0 +1,33 @@
+/**********************************************************************
+** Copyright (C) 2000 Trolltech AS. All rights reserved.
+**
+** This file is part of Qtopia Environment.
+**
+** This file may be distributed and/or modified under the terms of the
+** GNU General Public License version 2 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** See http://www.trolltech.com/gpl/ for GPL licensing information.
+**
+** Contact info@trolltech.com if any conditions of this licensing are
+** not clear to you.
+**
+**********************************************************************/
+
+#include "qtetrix.h"
+
+#include <qpe/qpeapplication.h>
+
+int main( int argc, char **argv )
+{
+ QPEApplication a(argc,argv);
+
+ QTetrix *tetrix = new QTetrix;
+ a.showMainWidget(tetrix);
+
+ return a.exec();
+}
diff --git a/noncore/games/tetrix/qpe-tetrix.control b/noncore/games/tetrix/qpe-tetrix.control
new file mode 100644
index 0000000..46dfdf5
--- a/dev/null
+++ b/noncore/games/tetrix/qpe-tetrix.control
@@ -0,0 +1,10 @@
+Files: bin/tetrix apps/Games/tetrix.desktop
+Priority: optional
+Section: qpe/games
+Maintainer: Warwick Allison <warwick@trolltech.com>
+Architecture: arm
+Arch: iPAQ
+Version: $QPE_VERSION-3
+Depends: qpe-base ($QPE_VERSION)
+Description: Game: control falling blocks
+ A game for the Qtopia environment.
diff --git a/noncore/games/tetrix/qtetrix.cpp b/noncore/games/tetrix/qtetrix.cpp
new file mode 100644
index 0000000..f649894
--- a/dev/null
+++ b/noncore/games/tetrix/qtetrix.cpp
@@ -0,0 +1,170 @@
+/**********************************************************************
+** Copyright (C) 2000 Trolltech AS. All rights reserved.
+**
+** This file is part of Qtopia Environment.
+**
+** This file may be distributed and/or modified under the terms of the
+** GNU General Public License version 2 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** See http://www.trolltech.com/gpl/ for GPL licensing information.
+**
+** Contact info@trolltech.com if any conditions of this licensing are
+** not clear to you.
+**
+**********************************************************************/
+
+
+#include "qtetrix.h"
+
+#include <qpe/resource.h>
+
+#include <qapplication.h>
+#include <qlabel.h>
+#include <qdatetime.h>
+#include <qlayout.h>
+
+
+
+void drawTetrixButton( QPainter *p, int x, int y, int w, int h,
+ const QColor *color )
+{
+ QColor fc;
+ if ( color ) {
+ QPointArray a;
+ a.setPoints( 3, x,y+h-1, x,y, x+w-1,y );
+ p->setPen( color->light() );
+ p->drawPolyline( a );
+ a.setPoints( 3, x+1,y+h-1, x+w-1,y+h-1, x+w-1,y+1 );
+ p->setPen( color->dark() );
+ p->drawPolyline( a );
+ x++;
+ y++;
+ w -= 2;
+ h -= 2;
+ fc = *color;
+ }
+ else
+ fc = p->backgroundColor();
+ p->fillRect( x, y, w, h, fc );
+}
+
+
+ShowNextPiece::ShowNextPiece( QWidget *parent, const char *name )
+ : QFrame( parent, name )
+{
+ setFrameStyle( QFrame::Panel | QFrame::Sunken );
+ xOffset = -1; // -1 until first resizeEvent.
+}
+
+void ShowNextPiece::resizeEvent( QResizeEvent *e )
+{
+ QSize sz = e->size();
+ blockWidth = (sz.width() - 3)/5;
+ blockHeight = (sz.height() - 3)/6;
+ xOffset = (sz.width() - 3)/5;
+ yOffset = (sz.height() - 3)/6;
+}
+
+
+void ShowNextPiece::paintEvent( QPaintEvent * )
+{
+ QPainter p( this );
+ drawFrame( &p );
+ p.end(); // explicit end() so any slots can paint too
+ emit update();
+}
+
+
+void ShowNextPiece::drawNextSquare(int x, int y,QColor *color)
+{
+ if (xOffset == -1) // Before first resizeEvent?
+ return;
+
+ QPainter paint;
+ paint.begin(this);
+ drawTetrixButton( &paint, xOffset+x*blockWidth, yOffset+y*blockHeight,
+ blockWidth, blockHeight, color );
+ paint.end();
+}
+
+
+QTetrix::QTetrix( QWidget *parent, const char *name, WFlags f )
+ : QMainWindow( parent, name, f )
+{
+ setIcon( Resource::loadPixmap( "tetrix_icon" ) );
+ setCaption( tr("Tetrix" ) );
+
+ QTime t = QTime::currentTime();
+ TetrixPiece::setRandomSeed( (((double)t.hour())+t.minute()+t.second())/
+ (24+60+60) );
+
+ QWidget *gameArea = new QWidget( this );
+ setCentralWidget( gameArea );
+
+ QGridLayout *gl = new QGridLayout( gameArea, 5, 3, 8 );
+
+ QLabel *l;
+ l = new QLabel( tr("Next"), gameArea );
+ gl->addWidget( l, 0, 0 );
+ showNext = new ShowNextPiece(gameArea);
+ showNext->setBackgroundColor(QColor(0,0,0));
+ gl->addWidget( showNext, 0, 1 );
+
+ l = new QLabel( tr("Score"), gameArea );
+ gl->addWidget( l, 1, 0 );
+ showScore = new QLabel(gameArea);
+ gl->addWidget( showScore, 1, 1 );
+ l = new QLabel( tr("Level"), gameArea );
+ gl->addWidget( l, 2, 0 );
+ showLevel = new QLabel(gameArea);
+ gl->addWidget( showLevel, 2, 1 );
+ l = new QLabel( tr("Removed"), gameArea );
+ gl->addWidget( l, 3, 0 );
+ showLines = new QLabel(gameArea);
+ gl->addWidget( showLines, 3, 1 );
+
+ board = new QTetrixBoard(gameArea);
+ board->setBackgroundColor(QColor(0,0,0));
+ board->setFixedWidth( 124 );
+ gl->addMultiCellWidget( board, 0, 4, 2, 2 );
+ gl->addColSpacing( 2, 100 );
+ gl->addColSpacing( 1, 35 );
+ gl->addRowSpacing( 0, 35 );
+
+ QPushButton *pb = new QPushButton( tr("Start"), gameArea );
+ pb->setFocusPolicy( NoFocus );
+ connect( pb, SIGNAL( clicked() ), board, SLOT( start() ) );
+ gl->addMultiCellWidget( pb, 4, 4, 0, 1 );
+
+ connect( board, SIGNAL(gameOverSignal()), SLOT(gameOver()) );
+ connect( board, SIGNAL(drawNextSquareSignal(int,int,QColor*)), showNext,
+ SLOT(drawNextSquare(int,int,QColor*)) );
+ connect( showNext, SIGNAL(update()), board, SLOT(updateNext()) );
+ connect( board, SIGNAL(updateScoreSignal(int)), showScore,
+ SLOT(setNum(int)) );
+ connect( board, SIGNAL(updateLevelSignal(int)), showLevel,
+ SLOT(setNum(int)));
+ connect( board, SIGNAL(updateRemovedSignal(int)), showLines,
+ SLOT(setNum(int)));
+
+ showScore->setNum( 0 );
+ showLevel->setNum( 0 );
+ showLines->setNum( 0 );
+ board->revealNextPiece(TRUE);
+ board->setFocusPolicy( StrongFocus );
+}
+
+void QTetrix::gameOver()
+{
+}
+
+
+void QTetrix::quit()
+{
+ close();
+}
diff --git a/noncore/games/tetrix/qtetrix.h b/noncore/games/tetrix/qtetrix.h
new file mode 100644
index 0000000..b6e058a
--- a/dev/null
+++ b/noncore/games/tetrix/qtetrix.h
@@ -0,0 +1,78 @@
+/**********************************************************************
+** Copyright (C) 2000 Trolltech AS. All rights reserved.
+**
+** This file is part of Qtopia Environment.
+**
+** This file may be distributed and/or modified under the terms of the
+** GNU General Public License version 2 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** See http://www.trolltech.com/gpl/ for GPL licensing information.
+**
+** Contact info@trolltech.com if any conditions of this licensing are
+** not clear to you.
+**
+**********************************************************************/
+
+
+#ifndef QTETRIX_H
+#define QTETRIX_H
+
+#include "qtetrixb.h"
+#include <qframe.h>
+#include <qlcdnumber.h>
+#include <qpushbutton.h>
+#include <qpainter.h>
+#include <qmainwindow.h>
+
+class QLabel;
+
+class ShowNextPiece : public QFrame
+{
+ Q_OBJECT
+ friend class QTetrix;
+public:
+ ShowNextPiece( QWidget *parent=0, const char *name=0 );
+public slots:
+ void drawNextSquare( int x, int y,QColor *color );
+signals:
+ void update();
+private:
+ void paintEvent( QPaintEvent * );
+ void resizeEvent( QResizeEvent * );
+
+ int blockWidth,blockHeight;
+ int xOffset,yOffset;
+};
+
+
+class QTetrix : public QMainWindow
+{
+ Q_OBJECT
+public:
+ QTetrix( QWidget *parent=0, const char *name=0, WFlags f=0 );
+ void startGame() { board->startGame(); }
+
+public slots:
+ void gameOver();
+ void quit();
+private:
+ void keyPressEvent( QKeyEvent *e ) { board->keyPressEvent(e); }
+
+ QTetrixBoard *board;
+ ShowNextPiece *showNext;
+ QLabel *showScore;
+ QLabel *showLevel;
+ QLabel *showLines;
+};
+
+
+void drawTetrixButton( QPainter *, int x, int y, int w, int h,
+ const QColor *color );
+
+
+#endif
diff --git a/noncore/games/tetrix/qtetrixb.cpp b/noncore/games/tetrix/qtetrixb.cpp
new file mode 100644
index 0000000..521f171
--- a/dev/null
+++ b/noncore/games/tetrix/qtetrixb.cpp
@@ -0,0 +1,251 @@
+/**********************************************************************
+** Copyright (C) 2000 Trolltech AS. All rights reserved.
+**
+** This file is part of Qtopia Environment.
+**
+** This file may be distributed and/or modified under the terms of the
+** GNU General Public License version 2 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** See http://www.trolltech.com/gpl/ for GPL licensing information.
+**
+** Contact info@trolltech.com if any conditions of this licensing are
+** not clear to you.
+**
+**********************************************************************/
+
+
+#include "qtetrixb.h"
+#include "qtetrix.h"
+#include <qtimer.h>
+#include <qkeycode.h>
+#include <qpainter.h>
+
+const int waitAfterLineTime = 500;
+
+QTetrixBoard::QTetrixBoard( QWidget *p, const char *name )
+ : QFrame( p, name )
+{
+ setFrameStyle( QFrame::Panel | QFrame::Sunken );
+ paint = 0;
+ timer = new QTimer(this);
+ connect( timer, SIGNAL(timeout()), SLOT(timeout()) );
+
+ colors[0].setRgb(200,100,100);
+ colors[1].setRgb(100,200,100);
+ colors[2].setRgb(100,100,200);
+ colors[3].setRgb(200,200,100);
+ colors[4].setRgb(200,100,200);
+ colors[5].setRgb(100,200,200);
+ colors[6].setRgb(218,170, 0);
+
+ xOffset = -1; // -1 until a resizeEvent is received.
+ blockWidth = 20;
+ yOffset = 30;
+ blockHeight = 20;
+ noGame = TRUE;
+ isPaused = FALSE;
+ waitingAfterLine = FALSE;
+ updateTimeoutTime(); // Sets timeoutTime
+}
+
+void QTetrixBoard::startGame(int gameType,int fillRandomLines)
+{
+ if ( isPaused )
+ return; // ignore if game is paused
+ noGame = FALSE;
+ GenericTetrix::startGame( gameType, fillRandomLines );
+ // Note that the timer is started by updateLevel!
+}
+
+
+void QTetrixBoard::pause()
+{
+ if ( noGame ) // game not active
+ return;
+ isPaused = !isPaused;
+ if ( isPaused ) {
+ timer->stop();
+ hideBoard();
+ }
+ else
+ timer->start(timeoutTime);
+ update();
+}
+
+
+void QTetrixBoard::drawSquare(int x,int y,int value)
+{
+ if (xOffset == -1) // Before first resizeEvent?
+ return;
+
+ const int X = xOffset + x*blockWidth;
+ const int Y = yOffset + (y - 1)*blockHeight;
+
+ bool localPainter = paint == 0;
+ QPainter *p;
+ if ( localPainter )
+ p = new QPainter( this );
+ else
+ p = paint;
+ drawTetrixButton( p, X, Y, blockWidth, blockHeight,
+ value == 0 ? 0 : &colors[value-1] );
+ /*
+ if ( value != 0 ) {
+ QColor tc, bc;
+ tc = colors[value-1].light();
+ bc = colors[value-1].dark();
+ p->drawShadePanel( X, Y, blockWidth, blockHeight,
+ tc, bc, 1, colors[value-1], TRUE );
+ }
+ else
+ p->fillRect( X, Y, blockWidth, blockHeight, backgroundColor() );
+ */
+ if ( localPainter )
+ delete p;
+}
+
+void QTetrixBoard::drawNextSquare( int x, int y, int value )
+{
+ if ( value == 0 )
+ emit drawNextSquareSignal (x, y, 0 );
+ else
+ emit drawNextSquareSignal( x, y, &colors[value-1] );
+}
+
+void QTetrixBoard::updateRemoved( int noOfLines )
+{
+ if ( noOfLines > 0 ) {
+ timer->stop();
+ timer->start( waitAfterLineTime );
+ waitingAfterLine = TRUE;
+ }
+ emit updateRemovedSignal( noOfLines );
+}
+
+void QTetrixBoard::updateScore( int newScore )
+{
+ emit updateScoreSignal( newScore );
+}
+
+void QTetrixBoard::updateLevel( int newLevel )
+{
+ timer->stop();
+ updateTimeoutTime();
+ timer->start( timeoutTime );
+ emit updateLevelSignal( newLevel );
+}
+
+void QTetrixBoard::pieceDropped(int)
+{
+ if ( waitingAfterLine ) // give player a break if a line has been removed
+ return;
+ newPiece();
+}
+
+void QTetrixBoard::gameOver()
+{
+ timer->stop();
+ noGame = TRUE;
+ emit gameOverSignal();
+}
+
+void QTetrixBoard::timeout()
+{
+ if ( waitingAfterLine ) {
+ timer->stop();
+ waitingAfterLine = FALSE;
+ newPiece();
+ timer->start( timeoutTime );
+ } else {
+ oneLineDown();
+ }
+}
+
+void QTetrixBoard::drawContents( QPainter *p )
+{
+ const char *text = "Press \"Pause\"";
+ QRect r = contentsRect();
+ paint = p; // set widget painter
+ if ( isPaused ) {
+ p->drawText( r, AlignCenter | AlignVCenter, text );
+ return;
+ }
+ int x1,y1,x2,y2;
+ x1 = (r.left() - xOffset) / blockWidth;
+ if (x1 < 0)
+ x1 = 0;
+ if (x1 >= boardWidth())
+ x1 = boardWidth() - 1;
+
+ x2 = (r.right() - xOffset) / blockWidth;
+ if (x2 < 0)
+ x2 = 0;
+ if (x2 >= boardWidth())
+ x2 = boardWidth() - 1;
+
+ y1 = (r.top() - yOffset) / blockHeight;
+ if (y1 < 0)
+ y1 = 0;
+ if (y1 >= boardHeight())
+ y1 = boardHeight() - 1;
+
+ y2 = (r.bottom() - yOffset) / blockHeight;
+ if (y2 < 0)
+ y2 = 0;
+ if (y2 >= boardHeight())
+ y2 = boardHeight() - 1;
+
+ updateBoard( x1, y1, x2, y2, TRUE );
+ paint = 0; // reset widget painter
+ return;
+}
+
+void QTetrixBoard::resizeEvent(QResizeEvent *e)
+{
+ QSize sz = e->size();
+ blockWidth = (sz.width() - 2)/10;
+ blockHeight = (sz.height() - 2)/22;
+ xOffset = 1;
+ //yOffset = 1;
+ yOffset = (sz.height() - 2) - (blockHeight *22);
+}
+
+void QTetrixBoard::keyPressEvent( QKeyEvent *e )
+{
+ if ( noGame || isPaused || waitingAfterLine )
+ return;
+ switch( e->key() ) {
+ case Key_Left :
+ moveLeft();
+ break;
+ case Key_Right :
+ moveRight();
+ break;
+ case Key_Down :
+// rotateRight();
+ dropDown();
+ break;
+ case Key_Up :
+ rotateLeft();
+ break;
+ case Key_Space :
+ dropDown();
+ break;
+ case Key_D :
+ oneLineDown();
+ break;
+ default:
+ return;
+ }
+ e->accept();
+}
+
+void QTetrixBoard::updateTimeoutTime()
+{
+ timeoutTime = 1000/(1 + getLevel());
+}
diff --git a/noncore/games/tetrix/qtetrixb.h b/noncore/games/tetrix/qtetrixb.h
new file mode 100644
index 0000000..4134a84
--- a/dev/null
+++ b/noncore/games/tetrix/qtetrixb.h
@@ -0,0 +1,80 @@
+/**********************************************************************
+** Copyright (C) 2000 Trolltech AS. All rights reserved.
+**
+** This file is part of Qtopia Environment.
+**
+** This file may be distributed and/or modified under the terms of the
+** GNU General Public License version 2 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** See http://www.trolltech.com/gpl/ for GPL licensing information.
+**
+** Contact info@trolltech.com if any conditions of this licensing are
+** not clear to you.
+**
+**********************************************************************/
+
+
+#ifndef QTETRIXB_H
+#define QTETRIXB_H
+
+#include "gtetrix.h"
+#include <qframe.h>
+
+class QTimer;
+
+class QTetrixBoard : public QFrame, public GenericTetrix
+{
+ Q_OBJECT
+public:
+ QTetrixBoard( QWidget *parent=0, const char *name=0 );
+
+ void gameOver();
+ void startGame(int gameType = 0,int fillRandomLines = 0);
+
+public slots:
+ void timeout();
+ void updateNext() { GenericTetrix::updateNext(); }
+ void key(QKeyEvent *e) { keyPressEvent(e); }
+ void start() { startGame(); }
+ void pause();
+
+signals:
+ void gameOverSignal();
+ void drawNextSquareSignal(int x,int y,QColor *color1);
+ void updateRemovedSignal(int noOfLines);
+ void updateScoreSignal(int score);
+ void updateLevelSignal(int level);
+
+public: // until we have keyboard focus, should be protected
+ void keyPressEvent( QKeyEvent * );
+
+private:
+ void drawContents( QPainter * );
+ void resizeEvent( QResizeEvent * );
+ void drawSquare(int x,int y,int value);
+ void drawNextSquare(int x,int y,int value);
+ void updateRemoved(int noOfLines);
+ void updateScore(int newScore);
+ void updateLevel(int newLlevel);
+ void pieceDropped(int dropHeight);
+ void updateTimeoutTime();
+
+ QTimer *timer;
+
+ int xOffset,yOffset;
+ int blockWidth,blockHeight;
+ int timeoutTime;
+ bool noGame;
+ bool isPaused;
+ bool waitingAfterLine;
+
+ QColor colors[7];
+ QPainter *paint;
+};
+
+#endif
diff --git a/noncore/games/tetrix/tetrix.pro b/noncore/games/tetrix/tetrix.pro
new file mode 100644
index 0000000..1160585
--- a/dev/null
+++ b/noncore/games/tetrix/tetrix.pro
@@ -0,0 +1,17 @@
+TEMPLATE = app
+CONFIG = qt warn_on release
+DESTDIR = ../bin
+HEADERS = minefield.h \
+ gtetrix.h \
+ qtetrix.h \
+ qtetrixb.h \
+ tpiece.h
+SOURCES = main.cpp \
+ gtetrix.cpp \
+ qtetrix.cpp \
+ qtetrixb.cpp \
+ tpiece.cpp
+INCLUDEPATH += $(QPEDIR)/include
+DEPENDPATH += $(QPEDIR)/include
+LIBS += -lqpe
+INTERFACES =
diff --git a/noncore/games/tetrix/tpiece.cpp b/noncore/games/tetrix/tpiece.cpp
new file mode 100644
index 0000000..fe8b766
--- a/dev/null
+++ b/noncore/games/tetrix/tpiece.cpp
@@ -0,0 +1,201 @@
+/**********************************************************************
+** Copyright (C) 2000 Trolltech AS. All rights reserved.
+**
+** This file is part of Qtopia Environment.
+**
+** This file may be distributed and/or modified under the terms of the
+** GNU General Public License version 2 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** See http://www.trolltech.com/gpl/ for GPL licensing information.
+**
+** Contact info@trolltech.com if any conditions of this licensing are
+** not clear to you.
+**
+**********************************************************************/
+
+
+#include "tpiece.h"
+#include "qstring.h"
+#include <stdlib.h>
+#include <time.h>
+
+void TetrixPiece::rotateLeft()
+{
+ if ( pieceType == 5 ) // don't rotate square piece type
+ return;
+ int tmp;
+ for (int i = 0 ; i < 4 ; i++) {
+ tmp = getXCoord(i);
+ setXCoord(i,getYCoord(i));
+ setYCoord(i,-tmp);
+ }
+}
+
+void TetrixPiece::rotateRight()
+{
+ if ( pieceType == 5 ) // don't rotate square piece type
+ return;
+ int tmp;
+ for (int i = 0 ; i < 4 ; i++) {
+ tmp = getXCoord(i);
+ setXCoord(i,-getYCoord(i));
+ setYCoord(i,tmp);
+ }
+}
+
+int TetrixPiece::getMinX()
+{
+ int tmp = coordinates[0][0];
+ for(int i = 1 ; i < 4 ; i++)
+ if (tmp > coordinates[i][0])
+ tmp = coordinates[i][0];
+ return tmp;
+}
+
+int TetrixPiece::getMaxX()
+{
+ int tmp = coordinates[0][0];
+ for(int i = 1 ; i < 4 ; i++)
+ if (tmp < coordinates[i][0])
+ tmp = coordinates[i][0];
+ return tmp;
+
+}
+
+int TetrixPiece::getMinY()
+{
+ int tmp = coordinates[0][1];
+ for(int i = 1 ; i < 4 ; i++)
+ if (tmp > coordinates[i][1])
+ tmp = coordinates[i][1];
+ return tmp;
+}
+
+int TetrixPiece::getMaxY()
+{
+ int tmp = coordinates[0][1];
+ for(int i = 1 ; i < 4 ; i++)
+ if (tmp < coordinates[i][1])
+ tmp = coordinates[i][1];
+ return tmp;
+}
+
+void TetrixPiece::initialize(int type)
+{
+ static int pieceTypes[7][4][2] = {{{ 0,-1},
+ { 0, 0},
+ {-1, 0},
+ {-1, 1}},
+
+ {{ 0,-1},
+ { 0, 0},
+ { 1, 0},
+ { 1, 1}},
+
+ {{ 0,-1},
+ { 0, 0},
+ { 0, 1},
+ { 0, 2}},
+
+ {{-1, 0},
+ { 0, 0},
+ { 1, 0},
+ { 0, 1}},
+
+ {{ 0, 0},
+ { 1, 0},
+ { 0, 1},
+ { 1, 1}},
+
+ {{-1,-1},
+ { 0,-1},
+ { 0, 0},
+ { 0, 1}},
+
+ {{ 1,-1},
+ { 0,-1},
+ { 0, 0},
+ { 0, 1}}};
+ if (type < 1 || type > 7)
+ type = 1;
+ pieceType = type;
+ for(int i = 0 ; i < 4 ; i++) {
+ coordinates[i][0] = pieceTypes[type - 1][i][0];
+ coordinates[i][1] = pieceTypes[type - 1][i][1];
+ }
+}
+
+
+/*
+ * Sigh, oh beautiful nostalgia! This random algorithm has
+ * been taken from the book "Adventures with your pocket calculator"
+ * and I used it in my first implemented and machine-
+ * run program of any size to speak of. Imagine how hungry I
+ * was after having programmed BASIC on paper for
+ * half a year?!!?!?!?!?!? The first program I typed in was a
+ * slot machine game and was made in BASIC on a SHARP
+ * PC-1211 with 1,47 KB RAM (one point four seven kilobytes) and
+ * a one-line LCD-display (I think it had 32 characters) in the
+ * year of our lord 1981. The man I had bought the machine from worked
+ * as a COBOL programmer and was amazed and impressed
+ * when I demonstrated the program 2 days after I had
+ * bought the machine, quote: "Gees, I have been looking so long
+ * for a "random" command in that BASIC, what is it called?"
+ * Oh, how I still get a thrill out of the thought of the
+ * explanation I then gave him...
+ */
+
+/*
+ * Sukk, aa vakre nostalgi! Denne random algoritmen er
+ * tatt fra boka "Adventures with your pocket calculator"
+ * og den brukte jeg i mitt foerste implementerte og maskin-
+ * kjoerte program av nevneverdig stoerrelse. Tror du jeg var
+ * noe sulten etter aa ha programmert BASIC paa papir i et
+ * halvt aar?!!?!?!?!?!? Programmet jeg tasta inn foerst var et
+ * "enarmet banditt" spill og ble laget i BASIC paa en SHARP
+ * PC-1211 med 1,47 KB RAM (en komma foertisju kilobyte) og
+ * et en-linjers LCD-display (tror det hadde 32 karakterer) i det
+ * herrens aar 1981. Mannen jeg kjoepte maskinen av jobbet til
+ * daglig med COBOL programmering og var forbloeffet og imponert
+ * da jeg demonstrerte programmet 2 dager etter at jeg hadde
+ * kjoept maskinen, sitat: "Joess, jeg som har leita saa lenge
+ * etter en random kommando i den BASICen, hva var det den
+ * het?" Aa, jeg frydes ennaa ved tanken paa forklaringen jeg
+ * deretter ga ham...
+ */
+
+double TetrixPiece::randomSeed = 0.33333;
+
+void TetrixPiece::setRandomSeed(double seed)
+{
+#ifdef __MIPSEL__
+ srand( clock() );
+#else
+ QCString buffer;
+ if (seed < 0)
+ seed = - seed;
+ if (seed >= 1)
+ seed = seed - (double) ((int) seed);
+ buffer.sprintf("%1.5f",(float) seed);
+ for (int i = 0 ; i < 5 ; i++)
+ if ((buffer[i + 2] - '0') % 2 == 0)
+ buffer[i + 2]++;
+ randomSeed = atof(buffer);
+#endif
+}
+
+int TetrixPiece::randomValue(int maxPlusOne)
+{
+#ifdef __MIPSEL__
+ return rand() % maxPlusOne;
+#else
+ randomSeed = randomSeed*147;
+ randomSeed = randomSeed - (double) ((int) randomSeed);
+ return (int) (randomSeed*maxPlusOne);
+#endif
+}
diff --git a/noncore/games/tetrix/tpiece.h b/noncore/games/tetrix/tpiece.h
new file mode 100644
index 0000000..9c1c629
--- a/dev/null
+++ b/noncore/games/tetrix/tpiece.h
@@ -0,0 +1,62 @@
+/**********************************************************************
+** Copyright (C) 2000 Trolltech AS. All rights reserved.
+**
+** This file is part of Qtopia Environment.
+**
+** This file may be distributed and/or modified under the terms of the
+** GNU General Public License version 2 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** See http://www.trolltech.com/gpl/ for GPL licensing information.
+**
+** Contact info@trolltech.com if any conditions of this licensing are
+** not clear to you.
+**
+**********************************************************************/
+
+
+#ifndef TPIECE_H
+#define TPIECE_H
+
+class TetrixPiece
+{
+public:
+ TetrixPiece() {setRandomType();}
+ TetrixPiece(int type) {initialize(type % 7 + 1);}
+
+ void setRandomType() {initialize(randomValue(7) + 1);}
+
+ void rotateLeft();
+ void rotateRight();
+
+ int getType() {return pieceType;}
+ int getXCoord(int index) {return coordinates[index][0];}
+ int getYCoord(int index) {return coordinates[index][1];}
+ void getCoord(int index,int &x,int&y){x = coordinates[index][0];
+ y = coordinates[index][1];}
+ int getMinX();
+ int getMaxX();
+ int getMinY();
+ int getMaxY();
+
+ static void setRandomSeed(double seed);
+ static int randomValue(int maxPlusOne);
+
+private:
+ void setXCoord(int index,int value) {coordinates[index][0] = value;}
+ void setYCoord(int index,int value) {coordinates[index][1] = value;}
+ void setCoords(int index,int x,int y){coordinates[index][0] = x;
+ coordinates[index][1] = y;}
+ void initialize(int type);
+
+ int pieceType;
+ int coordinates[4][2];
+
+ static double randomSeed;
+};
+
+#endif