tetris.cpp 14.4 KB
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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
/***************************************************************************
 *   Copyright (C) 2005 by Jeff Ferr                                       *
 *   root@sat                                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "jframe.h"

#define X_BLOCKS	10
#define Y_BLOCKS	20

int items[] = {
	0,0,   -1,0,  0,-1, -1,-1,  // cube, normal
	0,0,   -1,0,  0,1,  -1,1,   // rotated 90 degrees
	0,0,   1,0,   0,1,   1,1,   // rotated 180 degrees
	0,0,   1,0,   0,-1,  1,-1,  // rotated 270 degrees

	0,0,   0,-1,  0,-2, 0,-3,   // straight line
	0,0,  -1,0,  -2,0, -3,0,
	0,0,   0,1,   0,2,  0,3, 
	0,0,   1,0,   2,0,  3,0,

	0,0,   1,0,   0,-1, -1,-1,  // stupid block
	0,0,   0,-1,  -1,0, -1, 1,
	0,0,  -1,0,   0,1,  1,1,
	0,0,   0,1,   1,0,  1,-1,

	0,0,   -1,0,  0,-1,  1,-1,  // stupid block 2
	0,0,   0,1,   -1,0,  -1,-1,
	0,0,   1,0,   0,1,   -1,1,
	0,0,   0,-1,  1,0,   1,1,

	0,0,   1,0,   -1,0,  0,-1,  // another one
	0,0,   0,1,   0,-1,  -1,0,
	0,0,   0,1,   -1,0,  1,0,
	0,0,   1,0,   0,-1,  0,1,

	0,0,   0,-1,  1,-1,  0,1,	// hook one
	0,0,   -1,0,  -1,-1, 1,0,
	0,0,   -1,1,  0,1,   0,-1,
	0,0,   -1,0,  1,0,   1,1,

	0,0,   0,1,  0,-1,  -1,-1,	// hook two
	0,0,   1,0, -1,0,   -1,1,
	0,0,   0,-1,  0,1,  1,1,
	0,0,   -1,0, 1,0,   1,-1
};

int checks[] = { 
	-1,1, 0,1, -1,1,  0,1,      // check cube, normal
	-1,2, 0,2, -1,2,  0,2,      // check, rotated 90 degrees
	0,2,  1,2,  0,2,  1,2,      // check, rotated 180 degrees
	0,1,  1,1,  0,1,  1,1,      // check, rotated 270 degrees

	0,1,  0,1,  0,1,  0,1,
	0,1, -1,1, -2,1, -3,1,
	0,4,  0,4,  0,4,  0,4,      // straight line
	0,1,  1,1,  2,1,  3,1,

	0,1,  -1,0, 1,1,  0,1,		// stupid block one
	0,1,  -1,2, 0,1, -1,2,
	0,2,  1,2,  -1,2, 0,2,
	0,2,  1,1,  0,2,  1,1,

	-1,1,  0,1, 1,0,  1,0,		// stupid block two
	-1,1,  0,2, 0,2,  -1,1,
	-1,2,  0,2, 1,1,  1,1,
	0,1,   1,2,  0,1,  1,2,

	-1,1,  0,1,  1,1, 1,1,		// block three
	-1,1,  0,2,  0,2, -1,1,
	-1,1,  0,2,  1,1, 1,1,
	0,2,   1,1,  0,2, 1,1,

	0,2,   1,0,  1,0, 0,2,		// hook one
	-1,1,  0,1,  1,1, 1,1,
	-1,2,  0,2,  0,2, -1,2,
	-1,1,  0,1,  1,2, 1,2,

	-1,0,  0,2,  0,2, -1,0,		// hook two
	-1,2,  0,1,  1,1,  1,1,
	0,2,   1,2,  1,2,  0,2,
	-1,1,  0,1,  1,1,  1,1
};

class Tetris : public jgui::Frame, public jthread::Thread{

	public:
		jgui::Graphics *goff;
		jgui::Image *ii;
		jgui::Font *largefont,
			 *smallfont;
		uint32_t blocks[7],
				 textcolor1,
				 textcolor2,
				 barcolor,
				 background;
		bool ingame,
			 showtitle;
		int screendata[X_BLOCKS][Y_BLOCKS];
		int	xblocks,
			yblocks,
			blocksize,
			width,
			height,
			maxcolors,
			barwidth,
			score,
			emptyline,
			objectx, 
			objecty,
			objectdx,
			objecttype,
			objectcolor,
			objectrotation,
			objectrotationd,
			objectptr,
			checkptr,
			itemcount,
			itemrotlen,
			itemlen,
			count,
			maxcount,
			curcount,
			fast,
			screendelay,
			screencount;
		jgui::jregion_t d;

	public:

		Tetris():
			jgui::Frame("Tetris", 0, 0, 1920, 1080)
		{
			smallfont = jgui::Font::CreateFont("./fonts/font.ttf", jgui::JFA_NONE, 20);
			largefont = jgui::Font::CreateFont("./fonts/font.ttf", jgui::JFA_NONE, 28);

			goff = NULL;
			ii = NULL;
			largefont = NULL;
			smallfont = NULL;
			textcolor1 = 0xff6080ff;
			textcolor2 = 0xffffa040;
			barcolor = 0xff80ff40;
			background = 0xff000000;
			ingame = false;
			showtitle = true;
			xblocks = X_BLOCKS;
			yblocks = Y_BLOCKS;
			blocksize = 32;
			width = xblocks*blocksize;
			height = yblocks*blocksize;
			maxcolors = 6;
			barwidth = 8;
			score = 0;
			emptyline = 0;
			objectx = 0;
			objecty = 0;
			objectdx = 0;
			objecttype = 0;
			objectcolor = 0;
			objectrotation = 0;
			objectrotationd = 0;
			objectptr = 0;
			checkptr = 0;
			itemcount = 7;
			itemrotlen = 8;
			itemlen = itemrotlen*4;
			count = 0;
			maxcount = 5;
			curcount = 0;
			fast = false;
			screendelay = 40;
			screencount = 40;

			blocks[0] = background;
			blocks[1] = 0xffff0000;
			blocks[2] = 0xff00ff00;
			blocks[3] = 0xff0000ff;
			blocks[4] = 0xffffff00;
			blocks[5] = 0xffff00ff;
			blocks[6] = 0xff00ffff;

			// resize(width+2*barwidth,height+30);

			d.x = GetX();
			d.y = GetY();
			d.width = GetWidth();
			d.height = GetHeight();

			SetBackgroundColor((background>>0x10)&0xff, (background>>0x08)&0xff, (background>>0x00)&0xff, (background>>0x18)&0xff);
			SetUndecorated(true);
			SetSize(width+2*barwidth, height+2*barwidth);
		}

		virtual ~Tetris()
		{
		}

		void init()
		{
			ingame=true;
			gameInit();

			Start();
		}

		void gameInit()
		{
			int i,j;

			for (i=0; i<xblocks; i++) {
				for (j=0; j<yblocks; j++) {
					screendata[i][j]=0;
				}
			}

			score=0;
			emptyline=-1;

			newObject();
			fast=false;
			curcount=maxcount;
		}


		void newObject()
		{
			int i,
				y;

			fast = false;

			objectx=xblocks/2-1;
			objectdx=0;
			objecty=0;
			objecttype=(int)(0.0+(itemcount * (rand()/(RAND_MAX+1.0))));
			if (objecttype>=itemcount)
				objecttype=itemcount-1;

			objectptr=(int)(objecttype*itemlen);
			checkptr=(int)(objecttype*itemlen);

			objectcolor=(int)(0.0+((maxcolors+1) * (rand()/(RAND_MAX+1.0))));
			if (objectcolor>maxcolors)
				objectcolor=maxcolors;
			objectrotation=0;
			count=maxcount;

			// check if game has ended
			for (i=0; i<4; i++)
			{
				y=items[objectptr+i*2+1];
				if (y>=0 && screendata[objectx+items[objectptr+i*2]][y]!=0)
				{
					ingame=false;
					showtitle=true;
				}
			}
		}

		virtual bool ProcessEvent(jgui::KeyEvent *event)
		{
			if (event->GetType() == jgui::JKT_PRESSED) {
				if (ingame) {
					if (event->GetSymbol() == jgui::JKS_CURSOR_LEFT) {
						objectdx = -1;
					} else if (event->GetSymbol() == jgui::JKS_CURSOR_RIGHT) {
						objectdx = +1;
					} else if (event->GetSymbol() == jgui::JKS_CURSOR_UP) {
						objectrotationd = +1;
					} else if (event->GetSymbol() == jgui::JKS_CURSOR_DOWN) {
						fast = true;
					} else if (event->GetSymbol() == jgui::JKS_ESCAPE) {
						ingame = false;
					}
				} else {
					if (event->GetSymbol() == jgui::JKS_S) {
						ingame=true;
						gameInit();
					}
				}
			} else if (event->GetType() == jgui::JKT_RELEASED) {
				fast = false;
			}

			return true;
		}

		virtual void Paint(jgui::Graphics *g)
		{
			if (goff == NULL && d.width>0 && d.height>0) {
				ii = jgui::Image::CreateImage(d.width, d.height);

				goff = ii->GetGraphics();
			}

			if (goff==NULL || ii==NULL) {
				return;
			}

			goff->SetColor(background);
			goff->FillRectangle(0, 0, d.width, d.height);

			if (ingame)
				playGame();
			else
				showIntro();
			
			showScore();

			g->DrawImage(ii, 0, 0);
		}

		void playGame()
		{
			bool bottomreached = false,
				 stillscrolling = false;

			if (emptyline<0) {
				bottomreached=drawObject();
			} else {
				scrollDown();
				stillscrolling=true;
			}
			drawBars();
			drawBlocks();
			if (stillscrolling || bottomreached) {
				checkFull();
			}
		}

		void showIntro()
		{
			std::string s;

			drawBars();
			drawBlocks();

			if (showtitle) {
				goff->SetFont(largefont);
				s = "Tetris";
				goff->SetColor(textcolor1);
				// goff->DrawString(s, barwidth+(width-largefont->GetStringWidth(s))/2-2, height/2-22);
				goff->SetColor(0xffffffff);
				// goff->DrawString(s,barwidth+(width-largefont->GetStringWidth(s)) / 2, height/2 - 20);

				goff->SetFont(smallfont);
				s = "(c)2001 by Brian Postma";
				goff->SetColor(textcolor2);
				// goff->DrawString(s,barwidth+(width-smallfont->GetStringWidth(s))/2 -1,height/2 + 9);
				goff->SetColor(0xffffffff);
				// goff->DrawString(s,barwidth+(width-smallfont->GetStringWidth(s))/2,height/2 + 10);

				s = "b.postma@hetnet.nl";
				goff->SetColor(textcolor2);
				// goff->DrawString(s,barwidth+(width-smallfont->GetStringWidth(s))/2 -1,height/2 + 29);
				goff->SetColor(0xffffffff);
				// goff->DrawString(s,barwidth+(width-smallfont->GetStringWidth(s))/2,height/2 + 30);
			} else {
				goff->SetFont(smallfont);
				s = "'S' to start game";
				goff->SetColor(textcolor1);
				// goff->DrawString(s,barwidth+(width-smallfont->GetStringWidth(s))/2 -1,height/2 - 31);
				goff->SetColor(0xffffffff);
				// goff->DrawString(s,barwidth+(width-smallfont->GetStringWidth(s))/2,height/2 - 30);

				s = "Use cursor left+right to move";
				goff->SetColor(textcolor2);
				// goff->DrawString(s,barwidth+(width-smallfont->GetStringWidth(s))/2 -1,height/2 - 11);
				goff->SetColor(0xffffffff);
				// goff->DrawString(s,barwidth+(width-smallfont->GetStringWidth(s))/2,height/2 - 10);

				s = "Use cursor up to rotate";
				goff->SetColor(textcolor2);
				// goff->DrawString(s,barwidth+(width-smallfont->GetStringWidth(s))/2 -1,height/2 + 9);
				goff->SetColor(0xffffffff);
				// goff->DrawString(s,barwidth+(width-smallfont->GetStringWidth(s))/2,height/2 + 10);

				s="Use cursor down to drop";
				goff->SetColor(textcolor2);
				// goff->DrawString(s,barwidth+(width-smallfont->GetStringWidth(s))/2 -1,height/2 + 29);
				goff->SetColor(0xffffffff);
				// goff->DrawString(s,barwidth+(width-smallfont->GetStringWidth(s))/2,height/2 + 30);
			}
			screencount--;
			if (screencount<=0) { 
				screencount=screendelay; showtitle=!showtitle; 
			}
		}

		void drawBars()
		{
			goff->SetColor(barcolor);
			goff->FillRectangle(0,0,barwidth,blocksize*yblocks);
			goff->FillRectangle(barwidth+blocksize*xblocks,0,barwidth,blocksize*yblocks);
			goff->FillRectangle(0,blocksize*yblocks,xblocks*blocksize+2*barwidth,barwidth);
		}

		bool drawObject()
		{
			int	i;
			bool	bottomreached=false;
			int		x,y,checkx,checky;

			// clear old
			for (i=0; i<4; i++) {
				x=objectx+items[objectptr+i*2+objectrotation*itemrotlen];
				y=objecty+items[objectptr+i*2+objectrotation*itemrotlen+1];
				checkx=objectx+checks[objectptr+i*2+objectrotation*itemrotlen];
				checky=objecty+checks[objectptr+i*2+objectrotation*itemrotlen+1];
				if (y>=0)
					screendata[x][y]=0;
				if (screendata[checkx][checky]!=0)
					bottomreached=true;

			}

			if (!bottomreached) {
				count--;
				if (count<=0 || fast) {
					objecty++;
					count=curcount;
				}
				checkRotation();
				objectdx=0;
				objectrotationd=0;
			}

			// draw new
			for (i=0; i<4; i++) {
				x=objectx+items[objectptr+i*2+objectrotation*itemrotlen];
				y=objecty+items[objectptr+i*2+objectrotation*itemrotlen+1];

				if (y>=0)
					screendata[x][y]=objectcolor;
				if (y>=(yblocks-1))
					bottomreached=true;
			}
			if (bottomreached) {
				score++;
				newObject();
			}
			return bottomreached;
		}

		void checkRotation()
		{
			int dummyx,
				dummyrot,
				x,
				y,
				i;
			bool cando = true;

			dummyrot=(objectrotation+objectrotationd)%4;
			dummyx=objectx+objectdx;

			// make sure the part doesn't rotate of the playscreen
			for (i=0; i<4; i++) {
				x=dummyx+items[objectptr+i*2+dummyrot*itemrotlen];
				if (x>=xblocks)
					dummyx-=(x-xblocks+1);
				else if (x<0)
					dummyx-=x;
			}

			for (i=0; (i<4 && cando); i++) {
				x=dummyx+items[objectptr+i*2+dummyrot*itemrotlen];
				y=objecty+items[objectptr+i*2+dummyrot*itemrotlen+1];
				if (y>=0)
					cando=cando&&(screendata[x][y]==0);
				if (y>=yblocks || x<0 || x>=xblocks)
					cando=false;
			}

			if (cando) {
				objectrotation = dummyrot;
				objectx = dummyx;
			}
		}

		void drawBlocks()
		{
			int x,
				y;

			for (x=0; x<xblocks; x++) {
				for (y=0; y<yblocks; y++) {
					goff->SetColor(blocks[screendata[x][y]]);
					goff->DrawRectangle(x*blocksize+barwidth,y*blocksize,blocksize-1,blocksize-1);
					goff->FillRectangle(x*blocksize+barwidth+3,y*blocksize+3,blocksize-6,blocksize-6);
				}
			}
		}

		void checkFull()
		{
			int	x,y;
			bool found=false;

			for (y=yblocks-1; ( y>=0 && !found); y--) {
				found=true;
				for (x=0; x<xblocks; x++) {
					if (screendata[x][y]==0)
						found=false;
				}
				if (found) {
					score+=10;

					// increase speed when you've got a lot of points
					if (score>800)
						curcount=1;
					else if (score>600)
						curcount=2;
					else if (score>400)
						curcount=3;
					else if (score>200)
						curcount=4;

					for (x=0; x<xblocks; x++)
					{
						screendata[x][y]=0;
					}
					emptyline=y;
				}
			}
		}

		void scrollDown()
		{
			int x,y;

			for (y=emptyline; y>0; y--) {
				for (x=0; x<xblocks; x++) {
					screendata[x][y]=screendata[x][y-1];
				}
			}
			for (x=0; x<xblocks; x++) {
				screendata[x][0]=0;
			}
			emptyline=-1;
		}


		void showScore()
		{
			std::string s;

			goff->SetFont(smallfont);
			goff->SetColor(0xffffffff);

			s="Score: " + score;

			goff->DrawString(s,width/2-40,(yblocks+1)*blocksize+10);
		}

		void Run()
		{
			uint64_t starttime;

			while(true) {
				starttime = (jcommon::Date::CurrentTimeMillis()+10LL);

				Repaint();

				usleep(50000);
				// usleep(1000*((long long)jcommon::Date::CurrentTimeMillis()-(long long)starttime));
			}
		}
};

int main()
{
	Tetris t;

	t.init();
	t.Show();

	return 0;
}