////////////////////////////////////////////////////////////////////
// Toaster demo
// (c) 2007 Peter "ph0x" Moraliyski (ppm@freemail.hu)
// 2005.10.08 Nokia S60 (release)
// 2007.03.19 LPC2104 (port)
////////////////////////////////////////////////////////////////////

#include "common.h" 

#ifdef lpc	
	#include "string.h"
	#include "gfx.h"
#else
	#include "stdafx.h"
	#include "GameMidlet.cpp"
#endif

extern void _processinput();
extern Graphics g; //hack

void pause();
void gameKeypressed();
void myInit();
void gameHandler();
void menuKeypressed();
void menuHandler();
void gamePaint(Graphics g);
void menuPaint(Graphics g);
void initall();
void GameCanvas();
void tran(int n);
void spawn();

#ifdef lpc
	void load(int i, const void *s);
#else	
	void load(int i, String s);
#endif
	static Image img[12];
	//static Image meta[100][1];	

	const int F = 8;
	const int MOD_NORMAL = 0;
	const int MOD_BUFFERED = 1;
	
	int tick, proc, fpscn1, fpscn2, runmode;	
	static int SCROLLTXT, NUMLINE, LINESPPAGE;	
	static boolean debugp, paused,initbusy,paintbusy, title, results, online;	
	static int trans;
	static int newsub;
	static boolean inited;
	int subscene=0;

const int lut[] = {
	0x00,0xFE,0x01,0xFF,0xFF,0x00,0x00,0x01,0xFE,0xFF,0x01,0x02,0x01,0x00,0xFF,0xFE,
	0xFD,0xFE,0xFF,0xFF,0x00,0x00,0x01,0x02,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
	0x03,0x03,0x02,0x02,0x02,0x01,0x01,0x01,0x00,0x00,0x01,0xFF,0x00,0xFE,0xFE,0x00,
	0x00,0x02,0xFF,0xFE,0x00,0x02,0x02,0x01,0x00,0xFF,0xFE,0xFD,0xFD,0xFE,0xFE,0xFF,
	0x00,0x00,0x01,0x01,0x02,0x03,0x03,0x03,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x03,
	0x03,0x03,0x03,0x03,0x01,0x00,0x00,0xFF,0xFE,0x01,0xFE,0x02,0x00,0xFE,0xFF,0x01,
	0x02,0x02,0x01,0x00,0xFF,0xFE,0xFD,0xFD,0xFD,0xFE,0xFE,0xFF,0xFF,0x00,0x00,0x01,
	0x01,0x02,0x02,0x03,0x03,0x03,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x01,0x01,
	0xFF,0x00,0x00,0x02,0xFE,0x01,0x01,0xFF,0xFE,0x00,0x01,0x02,0x02,0x01,0x00,0x00,
	0xFF,0xFE,0xFD,0xFD,0xFD,0xFD,0xFE,0xFE,0xFF,0xFF,0x00,0x00,0x00,0x00,0x01,0x01,
	0x02,0x02,0x03,0x03,0x03,0x04,0x04,0x04,0x00,0x01,0xFE,0x00,0x00,0x01,0xFF,0x00,
	0x02,0x00,0xFE,0xFE,0x00,0x01,0x02,0x02,0x02,0x01,0x00,0xFF,0xFE,0xFE,0xFD,0xFD,
	0xFD,0xFD,0xFD,0xFD,0xFE,0xFE,0xFE,0xFF,0xFF,0x00,0x00,0x00,0x00,0x01,0x01,0x01,
	0x02,0x02,0x00,0x02,0xFF,0x01,0x01,0x00,0x00,0xFF,0x02,0x01,0xFF,0xFE,0xFF,0x00,
	0x01,0x02,0x03,0x02,0x01,0x01,0x00,0x00,0xFF,0xFE,0xFD,0xFD,0xFD,0xFD,0xFD,0xFD,
	0xFD,0xFD,0xFD,0xFD,0xFE,0xFE,0xFE,0xFF,0xFF,0xFF,0x00,0x00,0xFF,0x01,0x00,0x02,
	0x02,0x00,0x00,0xFE,0x01,0x02,0x00,0xFE,0xFE,0xFF,0x00,0x01,0x02,0x03,0x03,0x02,
	0x02,0x01,0x00,0x00,0xFF,0xFF,0xFE,0xFD,0xFD,0xFD,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,
	0xFC,0xFD,0xFD,0xFD,0xFD,0xFD,0xFF,0x00,0x00,0x01,0x02,0xFF,0x02,0xFE,0x00,0x02,
	0x01,0xFF,0xFE,0xFE,0xFF,0x00,0x01,0x02,0x03,0x03,0x03,0x02,0x02,0x01,0x01,0x00,
	0x00,0xFF,0xFF,0xFE,0xFE,0xFD,0xFD,0xFD,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,
	0xFF,0xFF,0x01,0x00,0x00,0xFE,0x02,0xFF,0xFF,0x01,0x02,0x00,0xFF,0xFE,0xFE,0xFF,
	0x00,0x00,0x01,0x02,0x03,0x03,0x03,0x03,0x02,0x02,0x01,0x01,0x00,0x00,0x00,0x00,
	0xFF,0xFF,0xFE,0xFE,0xFD,0xFD,0xFD,0xFC,0xFC,0xFC,0x00,0xFF,0x02,0x00,0x00,0xFF,
	0x01,0x00,0xFE,0x00,0x02,0x02,0x00,0xFF,0xFE,0xFE,0xFE,0xFF,0x00,0x01,0x02,0x02,
	0x03,0x03,0x03,0x03,0x03,0x03,0x02,0x02,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0xFF,
	0xFF,0xFF,0xFE,0xFE, 
};

	const static int sin3[] = {
          0,  1,  3,  4,  6,  7,  9,  10,
          12,  13,  15,  16,  18,  19,  21,  22,
          24,  25,  27,  28,  29,  31,  32,  33,
          35,  36,  37,  38,  40,  41,  42,  43,
          44,  45,  46,  47,  48,  49,  50,  51,
          52,  53,  54,  55,  55,  56,  57,  57,
          58,  59,  59,  60,  60,  61,  61,  61,
          62,  62,  62,  62,  63,  63,  63,  63,
          63,  63,  63,  63,  63,  63,  62,  62,
          62,  62,  61,  61,  60,  60,  59,  59,
          58,  58,  57,  56,  56,  55,  54,  53,
          53,  52,  51,  50,  49,  48,  47,  46,
          45,  44,  42,  41,  40,  39,  38,  36,
          35,  34,  33,  31,  30,  28,  27,  26,
          24,  23,  21,  20,  18,  17,  15,  14,
          12,  11,  9,  8,  6,  5,  3,  2,
          0, -2, -3, -5, -6, -8, -9, -11,
         -12, -14, -15, -17, -18, -20, -21, -23,
         -24, -26, -27, -28, -30, -31, -33, -34,
         -35, -37, -38, -39, -40, -41, -43, -44,
         -45, -46, -47, -48, -49, -50, -51, -52,
         -53, -54, -55, -55, -56, -57, -58, -58,
         -59, -59, -60, -61, -61, -61, -62, -62,
         -63, -63, -63, -63, -64, -64, -64, -64,
         -64, -64, -64, -64, -64, -64, -63, -63,
         -63, -63, -62, -62, -62, -61, -61, -60,
         -60, -59, -58, -58, -57, -56, -55, -55,
         -54, -53, -52, -51, -50, -49, -48, -47,
         -46, -45, -44, -43, -42, -40, -39, -38,
         -37, -35, -34, -33, -31, -30, -29, -27,
         -26, -24, -23, -21, -20, -18, -17, -15,
         -14, -12, -11, -9, -8, -6, -5, -3,
	}; 

	const int cos3[] = {
          63,  63,  63,  63,  63,  63,  62,  62,
          62,  62,  61,  61,  60,  60,  59,  59,
          58,  58,  57,  56,  56,  55,  54,  53,
          53,  52,  51,  50,  49,  48,  47,  46,
          45,  44,  42,  41,  40,  39,  38,  36,
          35,  34,  33,  31,  30,  28,  27,  26,
          24,  23,  21,  20,  18,  17,  15,  14,
          12,  11,  9,  8,  6,  5,  3,  2,
          0, -2, -3, -5, -6, -8, -9, -11,
         -12, -14, -15, -17, -18, -20, -21, -23,
         -24, -26, -27, -28, -30, -31, -33, -34,
         -35, -37, -38, -39, -40, -41, -43, -44,
         -45, -46, -47, -48, -49, -50, -51, -52,
         -53, -54, -55, -55, -56, -57, -58, -58,
         -59, -59, -60, -61, -61, -61, -62, -62,
         -63, -63, -63, -63, -64, -64, -64, -64,
         -64, -64, -64, -64, -64, -64, -63, -63,
         -63, -63, -62, -62, -62, -61, -61, -60,
         -60, -59, -58, -58, -57, -56, -55, -55,
         -54, -53, -52, -51, -50, -49, -48, -47,
         -46, -45, -44, -43, -42, -40, -39, -38,
         -37, -35, -34, -33, -31, -30, -29, -27,
         -26, -24, -23, -21, -20, -18, -17, -15,
         -14, -12, -11, -9, -8, -6, -5, -3,
          0,  1,  3,  4,  6,  7,  9,  10,
          12,  13,  15,  16,  18,  19,  21,  22,
          24,  25,  27,  28,  29,  31,  32,  33,
          35,  36,  37,  38,  40,  41,  42,  43,
          44,  45,  46,  47,  48,  49,  50,  51,
          52,  53,  54,  55,  55,  56,  57,  57,
          58,  59,  59,  60,  60,  61,  61,  61,
          62,  62,  62,  62,  63,  63,  63,  63,
	};

const static short sincore[]={0, 71, 142, 214, 285, 356, 428, 499, 570, 640, 711, 781, 851, 921, 990, 1060,
	1129, 1197, 1265, 1333, 1400, 1467, 1534, 1600, 1665, 1731, 1795, 1859, 1922, 1985, 2047, 2109,
	2170, 2230, 2290, 2349, 2407, 2465, 2521, 2577, 2632, 2687, 2740, 2793, 2845, 2896, 2946, 2995,
	3043, 3091, 3137, 3183, 3227, 3271, 3313, 3355, 3395, 3435, 3473, 3510, 3547, 3582, 3616, 3649,
	3681, 3712, 3741, 3770, 3797, 3823, 3848, 3872, 3895, 3917, 3937, 3956, 3974, 3991, 4006, 4020,
	4033, 4045, 4056, 4065, 4073, 4080, 4086, 4090, 4093, 4095, 4096};


const int SINCORE2[]={
	0x0,
	0x324,0x648,0x96C,0xC8F,0xFB2,0x12D5,0x15F6,
	0x1917,0x1C37,0x1F56,0x2273,0x2590,0x28AA,0x2BC4,
	0x2EDB,0x31F1,0x3505,0x3817,0x3B26,0x3E33,0x413E,
	0x4447,0x474D,0x4A50,0x4D50,0x504D,0x5347,0x563E,
	0x5931,0x5C22,0x5F0E,0x61F7,0x64DC,0x67BD,0x6A9B,
	0x6D74,0x7049,0x7319,0x75E5,0x78AD,0x7B70,0x7E2E,
	0x80E7,0x839C,0x864B,0x88F5,0x8B9A,0x8E39,0x90D3,
	0x9368,0x95F6,0x987F,0x9B02,0x9D7F,0x9FF6,0xA267,
	0xA4D2,0xA736,0xA994,0xABEB,0xAE3B,0xB085,0xB2C8,
	0xB504,0xB73A,0xB968,0xBB8F,0xBDAE,0xBFC7,0xC1D8,
	0xC3E2,0xC5E4,0xC7DE,0xC9D1,0xCBBB,0xCD9F,0xCF7A,
	0xD14D,0xD318,0xD4DB,0xD695,0xD848,0xD9F2,0xDB94,
	0xDD2D,0xDEBE,0xE046,0xE1C5,0xE33C,0xE4AA,0xE60F,
	0xE76B,0xE8BF,0xEA09,0xEB4B,0xEC83,0xEDB2,0xEED8,
	0xEFF5,0xF109,0xF213,0xF314,0xF40B,0xF4FA,0xF5DE,
	0xF6BA,0xF78B,0xF853,0xF912,0xF9C7,0xFA73,0xFB14,
	0xFBAC,0xFC3B,0xFCBF,0xFD3A,0xFDAB,0xFE13,0xFE70,
	0xFEC4,0xFF0E,0xFF4E,0xFF84,0xFFB1,0xFFD3,0xFFEC,
	0xFFFB,0x10000,0xFFFB,0xFFEC,0xFFD3,0xFFB1,0xFF84,
	0xFF4E,0xFF0E,0xFEC4,0xFE70,0xFE13,0xFDAB,0xFD3A,
	0xFCBF,0xFC3B,0xFBAC,0xFB14,0xFA73,0xF9C7,0xF912,
	0xF853,0xF78B,0xF6BA,0xF5DE,0xF4FA,0xF40B,0xF314,
	0xF213,0xF109,0xEFF5,0xEED8,0xEDB2,0xEC83,0xEB4B,
	0xEA09,0xE8BF,0xE76B,0xE60F,0xE4AA,0xE33C,0xE1C5,
	0xE046,0xDEBE,0xDD2D,0xDB94,0xD9F2,0xD848,0xD695,
	0xD4DB,0xD318,0xD14D,0xCF7A,0xCD9F,0xCBBB,0xC9D1,
	0xC7DE,0xC5E4,0xC3E2,0xC1D8,0xBFC7,0xBDAE,0xBB8F,
	0xB968,0xB73A,0xB504,0xB2C8,0xB085,0xAE3B,0xABEB,
	0xA994,0xA736,0xA4D2,0xA267,0x9FF6,0x9D7F,0x9B02,
	0x987F,0x95F6,0x9368,0x90D3,0x8E39,0x8B9A,0x88F5,
	0x864B,0x839C,0x80E7,0x7E2E,0x7B70,0x78AD,0x75E5,
	0x7319,0x7049,0x6D74,0x6A9B,0x67BD,0x64DC,0x61F7,
	0x5F0E,0x5C22,0x5931,0x563E,0x5347,0x504D,0x4D50,
	0x4A50,0x474D,0x4447,0x413E,0x3E33,0x3B26,0x3817,
	0x3505,0x31F1,0x2EDB,0x2BC4,0x28AA,0x2590,0x2273,
	0x1F56,0x1C37,0x1917,0x15F6,0x12D5,0xFB2,0xC8F,
	0x96C,0x648,0x324,0x0,0xFFFFFCDC,0xFFFFF9B8,0xFFFFF694,
	0xFFFFF371,0xFFFFF04E,0xFFFFED2B,0xFFFFEA0A,0xFFFFE6E9,0xFFFFE3C9,0xFFFFE0AA,
	0xFFFFDD8D,0xFFFFDA70,0xFFFFD756,0xFFFFD43C,0xFFFFD125,0xFFFFCE0F,0xFFFFCAFB,
	0xFFFFC7E9,0xFFFFC4DA,0xFFFFC1CD,0xFFFFBEC2,0xFFFFBBB9,0xFFFFB8B3,0xFFFFB5B0,
	0xFFFFB2B0,0xFFFFAFB3,0xFFFFACB9,0xFFFFA9C2,0xFFFFA6CF,0xFFFFA3DE,0xFFFFA0F2,
	0xFFFF9E09,0xFFFF9B24,0xFFFF9843,0xFFFF9565,0xFFFF928C,0xFFFF8FB7,0xFFFF8CE7,
	0xFFFF8A1B,0xFFFF8753,0xFFFF8490,0xFFFF81D2,0xFFFF7F19,0xFFFF7C64,0xFFFF79B5,
	0xFFFF770B,0xFFFF7466,0xFFFF71C7,0xFFFF6F2D,0xFFFF6C98,0xFFFF6A0A,0xFFFF6781,
	0xFFFF64FE,0xFFFF6281,0xFFFF600A,0xFFFF5D99,0xFFFF5B2E,0xFFFF58CA,0xFFFF566C,
	0xFFFF5415,0xFFFF51C5,0xFFFF4F7B,0xFFFF4D38,0xFFFF4AFC,0xFFFF48C6,0xFFFF4698,
	0xFFFF4471,0xFFFF4252,0xFFFF4039,0xFFFF3E28,0xFFFF3C1E,0xFFFF3A1C,0xFFFF3822,
	0xFFFF362F,0xFFFF3445,0xFFFF3261,0xFFFF3086,0xFFFF2EB3,0xFFFF2CE8,0xFFFF2B25,
	0xFFFF296B,0xFFFF27B8,0xFFFF260E,0xFFFF246C,0xFFFF22D3,0xFFFF2142,0xFFFF1FBA,
	0xFFFF1E3B,0xFFFF1CC4,0xFFFF1B56,0xFFFF19F1,0xFFFF1895,0xFFFF1741,0xFFFF15F7,
	0xFFFF14B5,0xFFFF137D,0xFFFF124E,0xFFFF1128,0xFFFF100B,0xFFFF0EF7,0xFFFF0DED,
	0xFFFF0CEC,0xFFFF0BF5,0xFFFF0B06,0xFFFF0A22,0xFFFF0946,0xFFFF0875,0xFFFF07AD,
	0xFFFF06EE,0xFFFF0639,0xFFFF058D,0xFFFF04EC,0xFFFF0454,0xFFFF03C5,0xFFFF0341,
	0xFFFF02C6,0xFFFF0255,0xFFFF01ED,0xFFFF0190,0xFFFF013C,0xFFFF00F2,0xFFFF00B2,
	0xFFFF007C,0xFFFF004F,0xFFFF002D,0xFFFF0014,0xFFFF0005,0xFFFF0000,0xFFFF0005,
	0xFFFF0014,0xFFFF002D,0xFFFF004F,0xFFFF007C,0xFFFF00B2,0xFFFF00F2,0xFFFF013C,
	0xFFFF0190,0xFFFF01ED,0xFFFF0255,0xFFFF02C6,0xFFFF0341,0xFFFF03C5,0xFFFF0454,
	0xFFFF04EC,0xFFFF058D,0xFFFF0639,0xFFFF06EE,0xFFFF07AD,0xFFFF0875,0xFFFF0946,
	0xFFFF0A22,0xFFFF0B06,0xFFFF0BF5,0xFFFF0CEC,0xFFFF0DED,0xFFFF0EF7,0xFFFF100B,
	0xFFFF1128,0xFFFF124E,0xFFFF137D,0xFFFF14B5,0xFFFF15F7,0xFFFF1741,0xFFFF1895,
	0xFFFF19F1,0xFFFF1B56,0xFFFF1CC4,0xFFFF1E3B,0xFFFF1FBA,0xFFFF2142,0xFFFF22D3,
	0xFFFF246C,0xFFFF260E,0xFFFF27B8,0xFFFF296B,0xFFFF2B25,0xFFFF2CE8,0xFFFF2EB3,
	0xFFFF3086,0xFFFF3261,0xFFFF3445,0xFFFF362F,0xFFFF3822,0xFFFF3A1C,0xFFFF3C1E,
	0xFFFF3E28,0xFFFF4039,0xFFFF4252,0xFFFF4471,0xFFFF4698,0xFFFF48C6,0xFFFF4AFC,
	0xFFFF4D38,0xFFFF4F7B,0xFFFF51C5,0xFFFF5415,0xFFFF566C,0xFFFF58CA,0xFFFF5B2E,
	0xFFFF5D99,0xFFFF600A,0xFFFF6281,0xFFFF64FE,0xFFFF6781,0xFFFF6A0A,0xFFFF6C98,
	0xFFFF6F2D,0xFFFF71C7,0xFFFF7466,0xFFFF770B,0xFFFF79B5,0xFFFF7C64,0xFFFF7F19,
	0xFFFF81D2,0xFFFF8490,0xFFFF8753,0xFFFF8A1B,0xFFFF8CE7,0xFFFF8FB7,0xFFFF928C,
	0xFFFF9565,0xFFFF9843,0xFFFF9B24,0xFFFF9E09,0xFFFFA0F2,0xFFFFA3DE,0xFFFFA6CF,
	0xFFFFA9C2,0xFFFFACB9,0xFFFFAFB3,0xFFFFB2B0,0xFFFFB5B0,0xFFFFB8B3,0xFFFFBBB9,
	0xFFFFBEC2,0xFFFFC1CD,0xFFFFC4DA,0xFFFFC7E9,0xFFFFCAFB,0xFFFFCE0F,0xFFFFD125,
	0xFFFFD43C,0xFFFFD756,0xFFFFDA70,0xFFFFDD8D,0xFFFFE0AA,0xFFFFE3C9,0xFFFFE6E9,
	0xFFFFEA0A,0xFFFFED2B,0xFFFFF04E,0xFFFFF371,0xFFFFF694,0xFFFFF9B8,0xFFFFFCDC,
};

const unsigned char height0[] =
{
//FIXME!!!
};

const unsigned char pal0[] =
{
	0x9B,0x8F,0x69,0xA8,0x9A,0x71,0xB6,0xA7,0x7B,0xD0,0xA6,0x7A,0xBF,0xB0,0x82,0xC9,
	0xB8,0x88,0xE2,0xBA,0x89,0xD8,0xC7,0x92,0xE3,0xC2,0x8F,0xE0,0xCE,0x97,0xE5,0xD3,
	0x9B,0xEC,0xD9,0xA0,0xF1,0xDD,0xA3,0xFA,0xE7,0xAA,0xFF,0xF6,0xB4,0x00,0x00,0x00,
};

	const int tubelut[] = {
		-16, -10, -8, -7, -5, -4, -4, -3, -2, -2, -1, -1, -1, -0, -0, -0, 0, 0, 0, 0, 1,
		 1, 1, 2, 2, 3, 4, 4, 5, 7, 8, 10,
	};

	int rand(int n) {return abs(rand())%n;}

	void quit() {}

	void genBg(Graphics graph, int length, int from, int until, int startcolor, int endcolor)
	{		
	    /*short sz1[3],sz2[3],sz3[3],sz4[3];
	    int i,j;
	    for (i=0;i<3;i++) {
	    	sz1[i]=(short) ((startcolor>>i*8) & 255);
	    	sz2[i]=(short) ((endcolor  >>i*8) & 255);
	    	sz4[i]=0;
	    }
	    for (i=0;i<until-from;i++) {
		    for (j=0;j<3;j++) {
				sz3[j]=(short) ((sz1[j]*(until-from-1-i)+sz2[j]*i)/(until-from));
				sz4[j]+=sz3[j]&15;
				if (sz4[j]>16) {sz3[j]+=16;sz4[j]-=16;}
				if (sz3[j]>255) sz3[j]=255;
			}
			graph.setColor(sz3[2],sz3[1],sz3[0]);
			graph.drawLine(0, i+from, length-1, i+from);
	    }*/
	}

// effects ///////////////////////////////////////////////////////////

class Effect {
	public:

	/*static int SW=GameCanvas.SW;
	static int SH=GameCanvas.SH;*/	
	/*const static Image *img= ::img;
	const static Image[][] meta=GameCanvas.meta;
	const static MetaImage[] mi=GameCanvas.mi;
	const static GameCanvas game= GameMidlet.mid.game;*/	
	const static int FP= 12; //GameCanvas.FP;
	
	int frame;
	int part;
	long startt;	
	int timeout;

	void paint(Graphics g) {printf("duh\n");}
	void run() {}
	void init() {
		frame= 0;
		part= 0;		
		timeout=200;	    
		startt=System::currentTimeMillis();			
	
		/*game.title= false;
		game.results= false;
		//hack
		SW=GameCanvas.SW;
		SH=GameCanvas.SH;
		FP=GameCanvas.FP;
		img=GameCanvas.img;
		meta=GameCanvas.meta;
		mi=GameCanvas.mi;
		game= GameMidlet.mid.game;*/
	
	}	
	
	static short sin2(int i) {
		i=(i+36000)%360;	//***ez lehet hogy nem kell majd ide
		if (i<90) return sincore[i];
		if (i<=180) return sincore[180-i];
		if (i<270) return (short) -sincore[i-180];
		return (short) -sincore[360-i];
	}

	static short cos2(int i) {
		return sin2(i+90);
	}
	
	Effect() {
	}
	
	Effect(int i) {
		timeout=i;
	}
							
};

class Water: public  Effect {	
	public:

	int f,x;
	
	Water() {
		
	}	

	Water(int n) {
		timeout=n;
	}	

	void init() {
		Effect::init();
		
		f= 0;
		x= 0;
	}

	void run() {
		f=(f+1)%10;		
		
		switch (part) {
		    case 0: 
			x-=1;
			if (x<-(img[3].getWidth()-SW)) {x=-(img[3].getWidth()-SW); part=1;}
		    break;
		    case 1: 
			x+=1;
			if (x>0) {x=0; part=0;}
		    break;
		}

		//if (timeout>0) timeout=0;
		if (10000+startt < System::currentTimeMillis()) 
		if (timeout>0) timeout=0;
		
	}	

	void paint(Graphics g) {		
		g.setColor256(0/*0x2e*/);
		g.fillRect(0,0,SW,SH);
		
		int h= 0;
		for (int i=0; i<SH/2; i++) {
			g.setClip(0, h+ SH -SH/2+i,SW,1);		
			int a=lut[SH/2*f+i];
			if (a>127) a-=256;
			g.drawImage(img[3], x, h+ i-(SH/2-i-1)+ SH -SH/2+(a), g.LEFT|g.TOP);			
		}
		g.setClip(0,0,SW,SH);
		g.drawImage(img[3], x, 0, g.LEFT|g.TOP);			
//		g.drawRegion(img[3], 0, 0, img[3].getWidth(), img[3].getHeight(), 1/*Sprite.TRANS_MIRROR_ROT180*/, x, 0, g.LEFT|g.TOP);
		//g.drawImage(img[2], 0, 0, g.LEFT|g.TOP);			
		//g.drawImage(img[5], SW, SH, g.RIGHT|g.BOTTOM);
	}
	
	
};

class Bobs: public Effect {	
	public:

	const static int MAXBOB=20;
	char bobx[MAXBOB];
	char boby[MAXBOB];		
	int c;		
	int frame2;

	Bobs() {
		
	}	

	Bobs(int n) {
		timeout=n;		
	}	

	void init() {
			Effect::init();
			
			c= 0;
			frame2= 0;
			
	    genBg(img[7].getGraphics(), SW, 0, SH, 0xfcddd2, 0xf8ad96);
	}

	void run() {		
				
		switch (part) {
			case 0:				
			case 3:
			case 6:
				if (frame%4==0)	if (c<MAXBOB-1) c++; else part++;				
			break;
			
			case 1:
			case 4:
			case 7:
				if (frame2%150==0) //300
				if (part<7) part++; //else timeout=0;
				
				if (35000-2000+startt < System::currentTimeMillis()) 
				if (timeout>0) timeout=0;
				
			break;
			
			case 2:
			case 5:
			case 8:
				if (frame%2==0)	if (c>1) c--; else part++;
			break;						
		}
		
		frame2++;
	}	

	void paint(Graphics g) {
		//hack
		g.setColor(0);
		g.fillRect(0,0,SW,SH);
		g.drawImage(img[2], 0, 0, g.LEFT|g.TOP);				
					
		int ff= frame*2;					
					
		for (int i=0; i<MAXBOB; i++) {
			int xx=0,yy=0,a=0;
			switch (part) {
				case 0:
				case 1:
				case 2:
					a=ff*2+i*10;
					xx=(sin2(a*4)+sin2(a*3))*10;
					yy=(cos2(a*3)-cos2(a*2))*10;		
				break;
				
				case 3:
				case 4:
				case 5:
					{
					a=ff*2+i*10;
					int b=ff+i*4;
					xx=(sin2(a*4+ff*4)-cos2(a*3+ff))*10;
					yy=(sin2(a*3)+cos2(a*2))*10;		
					}
				break;
				
				case 6:
				case 7:
				case 8:
					a=ff*2+i*((360<<16)/MAXBOB>>16);
					xx=(sin2(a*4+ff*4)-cos2(a*3))*10;
					yy=(sin2(a*3+ff*2)-cos2(a*2))*10;		
				break;
			}

			bobx[i]=15+SW/2+(xx>>FP);
			boby[i]=1+SH/3+(yy>>FP);
			//bobx[i]=bobx[i+1];
			//boby[i]=boby[i+1];
		}
		
		
		for (int i=0; i<c; i++) {
		    g.drawImage(img[6], bobx[i], boby[i], g.HCENTER|g.VCENTER);									    
		    
			//FIXBOBS
			/*if (part<=2) mi[2+ i%2].paint(g, bobx[i], boby[i], g.HCENTER|g.VCENTER, 0, frame2%6); else   
		    if (part<=5) mi[2+ 1+i%2].paint(g, bobx[i], boby[i], g.HCENTER|g.VCENTER, 0, frame2%6); else
				 mi[2+ i%3].paint(g, bobx[i], boby[i], g.HCENTER|g.VCENTER, 0, frame2%6);*/
		}   
		
///		g.drawImage(img[4], 0, SH, g.LEFT|g.BOTTOM);
		//g.drawImage(img[5], 0, 0, g.LEFT|g.TOP);			
		
	}
	
	
};

class Twister: public Effect {	
	public:

	int f3;
	int x;
	int frame2;	
	
	Twister() {
	}	

	Twister(int n) {
		timeout=n;
	}	

	void init() {
		Effect::init();
		
		f3=100;
		x=-SW/2;
		frame2=20;			
		 
    genBg(img[7].getGraphics(), SW, 0, SH, 0x024256, 0x4694ac);		
	}

	void run() {
		
		if (part!=0 && part!=3) frame2++; 		
		
		switch (part) {
			case 0:			
				if (x<SW/2) {
					x+=10; //5
					if (x > SW/2) x= SW/2;
				}
				else part=1;
			break;
						
			case 2:
				if (x<SW/2*3) x+=10; //5
				else {part=3;  f3=100;}
			break;

			case 3:
				if (x>SW/2) {
					x-=10; //5
					if (x < SW/2) x= SW/2;
				}
				else part=4;
			break;
			
			case 1:
			case 4:								
				if (frame2==270) part=2; //300
				//if (frame2==460) timeout=0; //600
				
				if (35000+startt < System::currentTimeMillis()) 
				if (timeout>0) timeout=0;
				
				
				if (f3>2) f3-=1;
			break;
		}
				
				
	}	

	void paint(Graphics g) {
		g.setColor(0);
		g.fillRect(0,0,SW,SH);
		//g.drawImage(img[18], 0, 0, g.LEFT|g.TOP);
		g.drawImage(img[1], 0, 0, g.LEFT|g.TOP);
		
		int f=frame2;	
		int f1=f*4;
		int f2=f*3;					
				
		int ff=(f+100);	
		int ff1=(f/2+100)*4;
		int ff2=(f+100)*3;					
		
		for (int i=0; i<SH; i++) {			
		if (part<3 || (part>=3 && i%2==0)) 		
		{ 
			int b=i/f3;			
			int a=sin3[(f1+b)%256]-sin3[(f2)%256];
			g.setClip(0,i,SW,1);			
			g.drawImage(img[8]/*[(a +65535)%64]*/, x, i-(a +65535)%32, g.HCENTER|g.TOP);
		} else {
			int b=i/f3;			
			int a=sin3[(ff1+b)%256]-sin3[(ff2)%256];
			g.setClip(0,i,SW,1);			
			g.drawImage(img[9]/*[(a +65535)%64]*/, x, i-(a +65535)%32, g.HCENTER|g.TOP);
		}
		}

		g.setClip(0,0,SW,SH);
		//g.drawImage(img[5], 0, 0, g.LEFT|g.TOP);
		
	}
	
	
};


	const static int MAXSKEW=8; //16	
	const static int MAXCOLOR=16;
	const static int MAXLEVELS=2;
	const static int FOVSTEP= ((512/6)<<16)/SW;		
	
	//static unsigned char *tmap;
	static unsigned char *hmap;
	static byte pal[MAXCOLOR];
	static byte skewtab[SW];
	//static byte htab[SW];
	//static int distab[];
	
	static int AIX[1];
	static int AIY[1];
	
	static byte colfreq[MAXCOLOR];
	static byte nulcol[MAXCOLOR];
	static byte coltab[SH][2];
	static unsigned char vmap[256][2];

	unsigned char CX;
	unsigned char CY;
	unsigned char CW;//125;
	unsigned char CH;//160;
		
	unsigned char step;
	byte skewstep; //4
	unsigned char colwidth; //4;
	unsigned char depth; //128;
	unsigned char gridsize;	
	unsigned char GRIDP; //raycasting starts from 1
	//int viewdist=35;
	unsigned char camh;	
	unsigned char camu; //256/2; 
	unsigned char camv; //256/2;
	unsigned char ang; //512/2;
	unsigned char margin;
	byte skew;
		
	boolean DEBUGON;
	boolean wiref;

	static unsigned char* pals[3];
	int morc;
	int mor;
	int KEYH, KEYV;
	int turn, turnc;


class Voxel: public Effect {	
	public:

	Voxel() {	
	}	

	Voxel(int n) {		
		timeout=n;				
	}		
	
	void init() {
		Effect::init();
		
//reset
	hmap= NULL;
	memset(pal, 0, sizeof(pal));
	memset(skewtab, 0, sizeof(skewtab));
	//memset(htab, 0, sizeof(htab));	
	AIX[1]=0;
	AIY[1]=0;	
	memset(colfreq, 0, sizeof(colfreq));	
	memset(nulcol, 0, sizeof(nulcol));	
	memset(coltab, 0, sizeof(coltab));	
	memset(vmap, 0, sizeof(vmap));	
	CX=0;
	CY=0;
	CW=0;
	CH=0;		
	step= 0;
	skewstep= 0;
	colwidth= 0;
	depth= 0;
	gridsize= 0;	
	GRIDP= 0;
	camh= 0;	
	camu= 0;
	camv= 0;
	ang= 0;
	margin= 0;
	skew= 0;		
	DEBUGON= false;
	wiref= false;
	memset(pals, 0, sizeof(pals));	
	morc= 0;
	mor= 0;
	KEYH= 0;
	KEYV= 0;
	turn= 0;
	turnc= 0;

//set		
		DEBUGON=false;
		wiref=true;

		CX=0;
		CY=0;
		CW=SW;//125;
		CH=SH;//160;
			
		step=4;
		skewstep=1; //4
		colwidth=10; //4;
		depth=100; //128;
		gridsize=8;	
		GRIDP=(depth-1)/gridsize; //raycasting starts from 1
		//int viewdist=35;
		camh=200; //200
		camu=0; //256/2; 
		camv=0; //256/2;
		ang=0; //512/2;
		margin=0;
		//tmap= (unsigned char*)ground0;
		hmap= (unsigned char*)height0;
		if (pals[0]!=null)
		palMorph(pals[0], pals[0], 1, 1);	
		
		//FIXVOX
		/*for (int i=0; i<3; i++) 
		memcpy(&pals[i], pal0, 768);*/
		pals[0]= (unsigned char*)pal0;
		pals[1]= (unsigned char*)pal0;
		pals[2]= (unsigned char*)pal0;
		palMorph(pals[0], pals[0], 1, 1);	
		//loadLevel(0);

		//setWarp(false);
		setSkew(0);
		//setShake(false);												
	}		

	void run() {
		if (turnc==0) {
			int old=KEYH;
			do KEYH=-1+rand(3); while (KEYH==old && old!=0);
			if (KEYH==0) 
			turnc=10+rand(5)*5; else 
			turnc=20+rand(10)*10;
		} else turnc--;
		
		if (frame==100) {mor=0; morc=32;} //200
		if (frame==200) {mor=1; morc=32;} //300		
		//if (frame==300) timeout=0;		 //400
		
		if (20000+startt < System::currentTimeMillis()) 
		if (timeout>0) timeout=0;
		
		
		if (morc>0) {
			palMorph(pals[mor], pals[mor+1], 32, 32-morc);
			morc--;
		}
		
		
		if (KEYH<0) {
			setSkew(skew-skewstep);
			if (skew<-MAXSKEW/2) {
				//camu=(camu-step+256)&255;
				ang=(ang-step+512)&511;
				//AIX[0]-=step<<16;
			}

		} else
		if (KEYH>0) {
			setSkew(skew+skewstep);
			if (skew>MAXSKEW/2) {
				//camu=(camu+step)&255;
				ang=(ang+step)&511;
				//AIX[0]+=step<<16;
			}

		} else {
			if (skew>0) {
				setSkew(skew-skewstep);
			} else
			if (skew<0) {
				setSkew(skew+skewstep);
			}

		}
				

		if (KEYV<0)	{

			//AIY[0]-=step<<16;
			AIX[0]-=sin2(-ang+512)*step;
			AIY[0]-=cos2(-ang+512)*step;
		} else
		if (KEYV>0)	{

			//AIY[0]+=step<<16;
			AIX[0]+=sin2(-ang+512)*step;
			AIY[0]+=cos2(-ang+512)*step;

		}

		
		AIX[0]-=sin2(-ang+512)*step;
		AIY[0]-=cos2(-ang+512)*step;

		if (AIX[0]>=0) camu=(AIX[0]>>16)%256; else camu=(-(-AIX[0]>>16))&255;
		if (AIY[0]>=0) camv=(AIY[0]>>16)%256; else camv=(-(-AIY[0]>>16))&255;			
		
	}	

	void paint(Graphics g) {
		/*if (wiref) {
			g.setColor(0);
			g.fillRect(0,0,SW,SH);

		} 
		else*/ 
		{		
			//g.setColor(155,155,155);
			//g.fillRect(0,0,SW,SH);

			g.setColor256(0xfa/*238,223,180*/);
			g.fillRect(0, 0, SW, SH);
		
			int x=-ang%64;
			for (int i=0; i<4; i++)
			g.drawImage(img[5], x+64*i, 0, g.LEFT|g.TOP);									
		}
		
		WF=0;
		for (int i=margin, step=FOVSTEP*margin; i<SW-margin; i+=colwidth, step+=FOVSTEP*colwidth) {					
			int a=(ang+(512*3)/4-(512/6)/2+(step>>16))&511; //mask obsolite?		
			castRay(g, a, i);																		
		}		
		
/*g.setClip(0,0,SW,SH);
g.setColor(0);
g.fillRect(0,0,SW,SH*1-frame*3); //SH*2
g.drawImage(img[15], (frame*8)%img[15].getWidth(), SH*1-frame*3, g.LEFT|g.TOP);
g.drawImage(img[15], (frame*8)%img[15].getWidth(), SH*1-frame*3, g.RIGHT|g.TOP);*/
g.setClip(0,0,SW,SH*2-frame*3); //SH*3
		
		if (wiref) drawWireFrame(g);
				
		g.setClip(0,0,SW,SH);
		//g.drawImage(img[14], 0, 0, g.LEFT|g.TOP);
		/*g.drawImage(img[21], 0, 0, g.LEFT|g.TOP);
		g.drawImage(img[22], SW, 0, g.RIGHT|g.TOP);
		g.drawImage(img[23], SW, SH, g.RIGHT|g.BOTTOM);
		g.drawImage(img[24], 0, SH, g.LEFT|g.BOTTOM);*/
		
	}
			
	int sin2(int i) {
		return SINCORE2[i&511];
	}
	int cos2(int i) {
		return sin2(i+512/4);
	}

	int unsign(byte n) {
		return n>=0?n:n+256;
	}
	
	void palMorph(const unsigned char *pal1, const unsigned char *pal2, int steps, int step) {				
				
		for (int i=0; i<MAXCOLOR; i++) {			
			int r1=unsign(pal1[i*3+0]);
			int g1=unsign(pal1[i*3+1]);
			int b1=unsign(pal1[i*3+2]);

			int r2=unsign(pal2[i*3+0]);
			int g2=unsign(pal2[i*3+1]);
			int b2=unsign(pal2[i*3+2]);
			
			int r=r1+((r2-r1<<16)/steps*step>>16);
			int g=g1+((g2-g1<<16)/steps*step>>16);
			int b=b1+((b2-b1<<16)/steps*step>>16);
			
			pal[i]=r<<16|g<<8|b;	
		};
				
	}
	
	void loadLevel(int n) {
		/*byte b[];

		byte[] tmap2=loadFile("ground"+n+".dat");				
		byte[] hmap2=loadFile("height"+n+".dat");		
		byte[] pal2=loadFile("ground"+n+".pal");
				
		palMorph(pals[0], pals[0], 1, 1);
				
		//hmap=new int[256][256];
		//tmap=new byte[256][256];
		
		for (int y=0; y<256; y++)
		for (int x=0; x<256; x++) {
			hmap[x][y]=unsign(hmap2[y*256+x]);
			tmap[x][y]=tmap2[y*256+x];
		}				
							
		//htab=new int[SW];
		*/
	}		

	void setSkew(int n) {				
		if (n<-MAXSKEW) n=-MAXSKEW; else
		if (n>MAXSKEW) n=MAXSKEW;
		
		if (skew==n) return;
		
		skew=n;		
		for (int i=0; i<SW/2; i++) {
			int y=((n<<16)/(SW/2)*i)>>16;
			skewtab[SW/2-i-1]=y;
			skewtab[SW/2+i]=-y;
		}
	}

	int tx[3];
	int ty[3];
	int foo1;
	int foo2;
	int WF;
	int drawn;

	void drawWireFrame(Graphics g) {		
		g.setColor(255,255,0);
		for (int i=0; i<WF; i++) 		
		{			
			if (i%GRIDP<GRIDP-1) g.drawLine(vmap[i][0], vmap[i][1], vmap[i+1][0], vmap[i+1][1]);			
			if (i<WF-GRIDP) g.drawLine(vmap[i][0], vmap[i][1], vmap[i+GRIDP][0], vmap[i+GRIDP][1]);
		}
	}
	
	void castRay(Graphics g, int a, int col) {
		int sh=SH+MAXSKEW; //ugly hack FIXVOX
		
		int ustep,vstep,U,V;
    	int miny,Y=0,H;
    	int ub,vb; 
    	int yr;    	

		miny=sh;  
		U=camu<<16;
		V=camv<<16;
		ustep=cos2(a);
		vstep=sin2(a); 

		byte oldc= 0;//hmap[camv*256+camu];
		int cc=0;		
		int max=0;
		int maxc=0;

		//System.arraycopy(nulcol,0, colfreq, 0, MAXCOLOR);	//FIXVOX

		/*
		for (int i=0; i<viewdist; i++) {	
			U+=ustep;
			V+=vstep;
		}
		*/
int miny2=0; 
		for (int i=1; i<depth; i++) {     
			ub=(U>>16)&255; 
			vb=(V>>16)&255; 

			H= 150+hmap[vb*256+ub]%20; 


			Y=sh/2-(((H-camh) << 5) / i); 

			if (Y<0) Y=0; 			
			if (Y<miny) {       												
				int c= 0; //tmap[vb*256+ub];																
				colfreq[c]+=miny-Y;
				if (c!=oldc) 
				{
					//colfreq[c]++;
					//g.fillRect(col, Y+skewtab[col], colwidth, miny-Y); //fixme?
					
					coltab[cc][0]=oldc;
					coltab[cc][1]=miny2;										
//if (col==margin+colwidth*0) System.out.println(coltab[cc][0]+" "+coltab[cc][1]+" "+miny);					
					oldc=c;
					cc++;					
					miny2=miny-Y;
				} else miny2+=miny-Y;					
				miny=Y; 								
			} 			
			U+=ustep; 
			V+=vstep; 
			
			if (wiref) 
			if (i%gridsize==0) {			
				int xx= SW/2+((col-SW/2)*depth)/i;
				int yy= miny+skewtab[col];
				//fake clip
				if (xx < 0) xx= 0;
				if (xx >= SW) xx= SW-1;
				if (yy < 0) yy= 0;
				if (yy >= SH) yy= SH-1;

				vmap[WF][0]= xx;
				vmap[WF][1]= yy;								
				WF++;
				
			}
		}
	/*
	vmap[WF][0]=col;
	vmap[WF][1]=miny+skewtab[col];				
	WF++;	
	*/			
	
///	if (wiref) return; //returns!
	
	coltab[cc][0]=oldc;
	coltab[cc][1]=miny2;										
	cc++;

	for (int i=0; i<MAXCOLOR; i++)
	if (colfreq[i]>maxc) {
		max=i;
		maxc=colfreq[i];
		//colfreq[i]=0;
	}
/*
System.out.print(col+". max "+max+" ["+colfreq[max]+"] miny "+miny+"      ");	
for (int i=0; i<MAXCOLOR; i++) System.out.print(colfreq[i]+",");
System.out.println();
*/

if (DEBUGON) g.setColor(255,0,255); else
	g.setColor(0, pal[max], 0); 
	g.fillRect(col, miny+skewtab[col], colwidth, sh-miny);
	
	int y=sh;
	for (int i=0; i<cc; i++) {
		y-=coltab[i][1];
		int c=coltab[i][0];
		if (c!=max) 
		{			
			g.setColor(0, pal[c], 0);			
			g.fillRect(col, y+skewtab[col], colwidth, coltab[i][1]);		
drawn++;			
		} 

	}

	int y2=y+skewtab[col];	
	int dy=foo1-y2;

	if (col>margin) {
		if (dy==0) {} else
		if (dy>0) {
			if (dy>colwidth) dy=colwidth;
			dy-=1;
			g.setClip(col-colwidth, foo1-colwidth, colwidth, colwidth);			
			g.setColor(0,foo2,0);
			//g.fillRect(col-colwidth, foo1-colwidth, colwidth, colwidth);
			///g.drawImage(img[8], col, foo1+colwidth*dy, g.RIGHT|g.BOTTOM);
			g.setClip(CX, CY, CW, CH);
								
			/*tx[0]=col;
			ty[0]=y2;
			tx[1]=col;
			ty[1]=foo1;
			tx[2]=col-colwidth;
			ty[2]=foo1;		
			dg.fillPolygon(tx, 0, ty, 0, 3, 0xFF<<24|foo2);*/
						
//			GameMidlet.dg.fillTriangle(col-1, y2, col, foo1, col-colwidth, foo1, 0xFF<<24|foo2);						
		} else {		
			if (dy<-(colwidth)) dy=-(colwidth);
			dy+=1;
			g.setClip(col, y2-colwidth, colwidth, colwidth);			
			g.setColor(0,foo2,0);
			//g.fillRect(col, y2-colwidth, colwidth, colwidth);
			///g.drawImage(img[9], col, y2+colwidth*-dy, g.LEFT|g.BOTTOM);
			g.setClip(CX, CY, CW, CH);
			
			/*tx[0]=col;
			ty[0]=y2;
			tx[1]=col;
			ty[1]=foo1;
			tx[2]=col+colwidth; //!
			ty[2]=y2; //!
			dg.fillPolygon(tx, 0, ty, 0, 3, 0xFF<<24|foo2);*/
						
//			GameMidlet.dg.fillTriangle(col-1, y2, col, foo1, col+colwidth, y2, 0xFF<<24|foo2);				
		}		
	}
//if (col==margin+colwidth) System.out.println(Math.abs(foo1-y2));		
	foo1=y2;
	foo2=pal[coltab[cc-1][0]];
	
	}
	
	
};

/*class Dummy: public Effect {	

	Dummy() {
		
	}	

	Dummy(int n) {
		timeout=n;
	}	

	void init() {
		
	}

	void run() {
		if (frame==16) quit();
	}	

	void paint(Graphics g) {
		
	}
};*/

class Tube: public Effect {	
	public:
	const static int r=16;
	int z;
	int a;	
	Image im;
	int xx;
	int yy;	
	int x,y;
	int anc;
	int gto;
	int dy;
	int sp;

	Tube() {
		
	}	

	Tube(int n) {
		timeout=n;
	}	
	
	void init() {
		Effect::init();
		
		z=SH+r;
		a= 0;	
		im=img[10];
		xx=SW/2;
		yy=0;	
		x= 0;
		y= 0;
		anc=g.HCENTER|g.BOTTOM;
		gto=SH/2;
		dy=-1;
		sp=Math::abs(gto-z)/2;
		
	}	

	void run() {		
		if (sp>0) {
			sp--;
			z+=dy*2;
		} else {
			gto=SH/2-SH/3+rand(SH/3*2);
			if (gto>z) dy=1; else dy=-1;
			sp=Math::abs(gto-z)/2;
		}
		
		if (frame==8) part++; //50
		//if (frame==210) timeout=0; //230
		
		if (10000-2000+startt < System::currentTimeMillis()) 
		if (timeout>0) timeout=0;

		switch (part) {
			case 0:
			break;
			case 1:
				a+=5;
				x=xx;
				y=yy+(sin2(a)*(im.getHeight()+20)>>FP);
				
				if (a>180) {
					im=img[11];	
					x=xx=0;					
					y=yy=50;
					anc=g.RIGHT|g.TOP;					
					part++;
					a=0;
				}
			break;
			
			case 2:								
				a+=5;				
				if (a>180) {				
					im=img[4];	
					x=xx=SW;					
					y=yy=SH-50;
					anc=g.LEFT|g.BOTTOM;					
				}
				if (a>360) part++;
				
				x=xx+(sin2(a)*(im.getWidth()+20)>>FP);
				y=yy;
								
			break;
		}
	}	

	void paint(Graphics g) {		
						
		g.drawImage(img[1], 0, 0,  g.LEFT|g.TOP);						
								
		for (int i=0; i<r*2; i++) {			
			g.setClip(0, z+i, SW, 1);
			g.drawImage(img[1], 0, -(-i -r -tubelut[i]+r*3),  g.LEFT|g.TOP);
		}
		g.setClip(0,0,SW,SH);
		g.drawImage(im, x, y, anc);
				
		g.setClip(0,0,SW,SH);
		//g.drawImage(img[5], 0, 0, Graphics.LEFT|Graphics.TOP);
		
	}
	
	
};

class Splash: public Effect {	
	public:

	const static int r=16;
	int z;

	Splash() {
		
	}	

	Splash(int n) {
		timeout=n;
	}	
	
	void init() {
		Effect::init();
		
		z=0;
	}	

	void run() {
		if (z<SH+r) z+=2; else 
		if (z==SH+r) {																											
			timeout=0; //GameMidlet.trans(GameMidlet.subscene+1);
			z++; //hack
		}
		
	}	

	void paint(Graphics g) {
						
		g.setColor256(0x3);
		g.fillRect(0,0,SW,SH);
		
		g.setClip(0, 0, SW, z);				
		g.drawImage(img[7], 0, 0, 0);

		for (int i=0; i<r*2; i++) {			
			g.setClip(0, z+i -r*2, SW, 1);
			g.drawImage(img[7], 0, i+tubelut[i] -r*3, g.LEFT|g.TOP);
		}
		
		g.setClip(0,0,SW,SH);
		//g.drawImage(img[5], 0, 0, Graphics.LEFT|Graphics.TOP);
		
	}
	
};


// effects end /////////////////////////////////////////////////////////////

	
//#if n7650 || n7210 || n3510i || n6600
//# 	//static DirectGraphics dg;
//#endif

//#if n7650 || n7210
//# 	Sound snd;
//#endif

	static boolean gameOver = false;	
	int SCORE, VIBRA;	
	int KEY;
	int CNT;	
///	boolean keypressed;
	static int vol=50;

//#if n6600	|| k700i
//# //~	static Player plr[]=new Player[2];
//# //~	static VolumeControl vc[]=new VolumeControl[2];
//#endif	

const int spiraltab[][2]= {
{15, 14},
{14, 13},
{13, 13},
{13, 12},
{12, 11},
{11, 11},
{10, 10},
{10, 9},
{9, 8},
{8, 8},
{8, 7},
{7, 6},
{6, 6},
{6, 5},
{5, 5},
{4, 4},
{4, 4},
{3, 4},
{3, 3},
{3, 3},
{2, 2},
{2, 2},
{2, 2},
{1, 2},
{1, 1},
{1, 1},
{1, 1},
{0, 1},
{0, 0},
{0, 0},
{0, 0},
{0, 0},
{0, 0},
{0, 0},
{0, 0},
{0, 0},
{1, 0},
{1, 1},
{1, 1},
{1, 1},
{2, 1},
{2, 2},
{2, 2},
{2, 2},
{3, 3},
{3, 3},
{4, 3},
{4, 4},
{4, 4},
{5, 5},
{5, 6},
{6, 6},
{6, 7},
{7, 8},
{8, 8},
{8, 9},
{9, 10},
{10, 10},
{11, 11},
{11, 12},
{12, 13},
{13, 13},
{13, 14},
{14, 15},
};
	
		
	typedef void (Effect::*tvoid)(void);
	typedef void (Effect::*tgraph)(Graphics g);
	typedef int Effect::*tint;
	typedef struct {
		tvoid init;
		tvoid run;		
		tgraph paint;
		Effect *members;
	} tclass;

	Twister twister;
	Water water;
	Bobs bobs;	
	Voxel voxel;	
	Tube tube;	
	Splash splash;

	static tclass effect[] = {									
			{(tvoid)&Splash::init, (tvoid)&Splash::run, (tgraph)&Splash::paint, &splash},
			{(tvoid)&Tube::init, (tvoid)&Tube::run, (tgraph)&Tube::paint, &tube},
			{(tvoid)&Bobs::init, (tvoid)&Bobs::run, (tgraph)&Bobs::paint, &bobs},
			{(tvoid)&Voxel::init, (tvoid)&Voxel::run, (tgraph)&Voxel::paint, &voxel},			
			{(tvoid)&Twister::init, (tvoid)&Twister::run, (tgraph)&Twister::paint, &twister},			
			{(tvoid)&Water::init, (tvoid)&Water::run, (tgraph)&Water::paint, &water},									
	};

	//effect[subscene].members->timeout= 0;
	//(effect[subscene].members->*effect[subscene].init)();

	void hideNotify() {		
		for (int i=0;i<MY_KEY_SZAM;i++) keypressed[i]=0;
		if (title || results); else pause();
	}

	void showNotify() {
	    //update= true;
	}
	
	void myRestart(int sc) {
	    
	}
	
	void pauseHandler() {
	    
	}
	
	void pauseKeypressed() {
	    gameKeypressed();
	}
		
	void handler() {
		/*int i,type;
		if (scene==SCENE_SPLASH) {							//splash screenek
			if (subscene==1) {loadImage(1);subscene++;frame=1;}
			if (subscene==4) {loadImage(2);subscene++;frame=1;}
			if ((frame&31)==0) subscene++;
			if (subscene==6) game_initmenu();		//scene:=0
			return;
		}*/
	    
//		SLEEP=50;
		//a 2 db "for (i=0;i<MY_KEY_SZAM;i++)" koze rakjuk a billentyuzet-tol fuggo szamitasokat
		//ele/utan rakjuk a szamitasigenyes dolgokat amik nem kellenek bele, igy kicsit optimalisabb lesz a billentyukezeles
		for (int i=0;i<MY_KEY_SZAM;i++) keypressed[i]+=(keypressed[i]&1)<<2;		//elmenti az epp aktualis billentyu statuszt, es azt vizsgaljuk, hogy ne tudjon feluton csak ugy megvaltozni :))
																				//de az ujonnan jott esemenyeket azert eltarolja !!
		key_was_pressed=3;		//key_was_pressed=15;	//ai-nal hasznaltam ugy hogy az epp soron kovetkezo AI erre a ket bitre generalta a billentyuzet parancsokat
		key_is__pressed=1;		//key_is__pressed=7;	//igy ugyanazt a kezelot lehetett ra alkalmazni mint a playerre csak mas ertekkel hasonlitotta a billentyuzet tomb ertekeit
		/*if (scene!=SCENE_MENU && scene!=SCENE_MENU && keypressed[MY_KEY_LSOFT]>key_was_pressed) {	//pause elohivasa
			scene_saved=scene;		//elmentjuk honnan jottunk
			scene=SCENE_MENU;
			subscene=submenu_pause;
			menu_aktpos=1;
			keypressed[MY_KEY_LSOFT]-=5;	//csak egyszer ertelmezzuk a leutest
		}*/
//***TODO billentyukezelest itt kene implementalni
		if (paused) {pauseHandler(); pauseKeypressed();} else
		switch (scene) {		//kulonbozo scenekben mit csinaljunk
			case SCENE_GAME: gameKeypressed(); gameHandler(); break;			
			case SCENE_MENU: menuKeypressed(); menuHandler(); break;									
			
			/*case SCENE_GAMEOVER:
			case SCENE_STORY:
			case SCENE_CREDITS:
			case SCENE_INTRO:
			case SCENE_FIREWORKS:
			case SCENE_SPLASH: 
			case SCENE_TUBE: miscKeypressed(); miscHandler(); break;*/
		}
		
		//debug
		//if (keypressed[MY_KEY_POUND] > key_was_pressed) quit();
		//if (keypressed[MY_KEY_NUM2] > key_was_pressed) if (scene==SCENE_GAME && !paused) gameOver(true);
		//if (keypressed[MY_KEY_STAR] > key_was_pressed) if (scene==SCENE_MENU) debugp= !debugp;
//-------------------------------------------
		for (int i=0;i<MY_KEY_SZAM;i++) if (keypressed[i]>3) keypressed[i]&=2; else keypressed[i]&=3;	//toroljuk a felhasznalt billentyu leutes esemenyeket,
//***TODO billentyutol fuggetlen framenkent torteno dolgok.. bar ezeket lehet az elozo todo-ba is tenni, de a billentyukezelesnek jot tesz, ha minnel kevesebb dolog van a ket "for (i=0;i<MY_KEY_SZAM;i++)..." kozott
		//pl fizika
	}

	void paint_offscreen( Graphics g )
	{
		//UBERHACK
		/*if (scene==SCENE_MENU)
		if (frame==0) return;*/
	    
	    switch (scene) {
		case SCENE_GAME: gamePaint(g); break;
		case SCENE_MENU: menuPaint(g); break;				
		
		/*case SCENE_GAMEOVER:
		case SCENE_STORY:
		case SCENE_CREDITS:
		case SCENE_INTRO:
		case SCENE_FIREWORKS:
		case SCENE_SPLASH: 
		case SCENE_TUBE: miscPaint(g); break;*/
	    }
	    
	    /*if (paused) {		
		g.drawImage(img[42], SW, SH, g.RIGHT|g.BOTTOM);
		g.drawImage(img[41], 0, SH, g.LEFT|g.BOTTOM);				
	    }*/
	
	    /*//debug
	    if (debugp) {
	    g.setColor(0);
	    g.fillRect(0, +20*0, 80, 20);
	    g.setColor(0xffffff);
	    g.drawString(""+fps, 2, 2, g.LEFT|g.TOP);

	    }*/
	    
	}
	
	void paint( Graphics g )
	{
	    if (initbusy) return; //hack
	    paintbusy= true;
	    
	    if (OFFSCREEN) g.drawImage(Ioffscreen,0,0,g.LEFT|g.TOP);
	    else paint_offscreen(g);
		    
	    paintbusy= false;	    	    
	}

	long accTime;	
	int fps,fps1,fpscnt1,fps2,fpscnt2; //temp
	
	void redraw() {
		//if (OFFSCREEN) if (g2!=null) paint_offscreen(g2);	//offscreen eseten ez szamolja a rajzolast					        			
		repaint();
		serviceRepaints(); //while (busy) GameMidlet.mid.trd.yield();				    
	}
	
	void run( )
	{				

		myInit();				

		//if (rms[RMS_INSTANT]==1) startGame(1); else myRestart(SCENE_SPLASH); 
		myRestart(SCENE_MENU); 
		//myRestart(SCENE_FIREWORKS); 
		//startGame(1);		
		//scene=SCENE_FOO;

//System.gc(); System.out.println("gamestart:"+Runtime.getRuntime().freeMemory());
		
	    //img[7]=Image.createImage("amuq.png");
	    
		/*
		effect = new Effect[] {			
			new Splash(),			
			new Voxel(), 
			new Water(),
			new Twister(),
			new Bobs(),
			new Results(),
			//new Dummy(),
			
		};
		
		MAXTEST= effect.length;
		test= new int[10]; //hack MAXTEST
		*/

		//init();
		//for (int i=0; i<effect.length; i++) effect[i].init();	    					

		initall(); 
		(effect[subscene].members->*effect[subscene].init)();
	  //redraw();		

		while ( !gameOver ) {                        
			_processinput();

			long startTime = System::currentTimeMillis(); 
		    handler();	//ez csinal mindent			    
		    {			
			redraw();
			long timeTaken = System::currentTimeMillis() - startTime;
		        if (timeTaken > 0)
			//if ((frame&31) == 0) 
			fps= (1000<<F)/timeTaken>>F;
		        //if (timeTaken < SLEEP) while (System.currentTimeMillis()-startTime < SLEEP) ;//GameMidlet.mid.trd.yield();//
		        if (timeTaken < SLEEP) sleep(SLEEP - timeTaken);
		    } 
		    {
			if (!paused) frame++;
			
			fpscnt1++; //debug
			fpscnt2++; //debug
		    }		
		    proc= (proc+1)&1;
		    tick++;		    		    
	    }	
		
		quit();	    
	    
	}
		
	void pause() {
	    paused= true;
	}
	
	void menuKeypressed() {
	    
	}
	
	void menuHandler() {
	    
	}
	
	void menuPaint(Graphics g) {
	    
	}		
	     	
    //static MetaImage mi[100];	
	//static MetaImage myfnt;
	
	const static int bflyframes[][4]= {
	    {0,  2, 24, 15},
	    {0,  52, 28, 13},
	    {0,  100, 29, 13},
	    {0,  146, 28, 13},
	    {0,  190, 24, 15},
	    {0,  235, 28, 17},
	};
	
	static int bflyframes2[][4]= {
	    {10, 23, 12, 23},
	    {9, 66, 14, 27},
	    {8, 113, 14, 27},
	    {9, 160, 14, 27},
	    {10, 211, 12, 23},
	    {10, 258, 20, 24},
	};
	
	static int bflyframes3[][4]= {
	    {42, 5, 16, 21},
	    {44, 50, 14, 22},
	    {45, 96, 12, 23},
	    {44, 144, 13, 22},
	    {42, 193, 16, 21},
	    {41, 242, 18, 19},
	};
	
	/*const int fontframes[][4]= { //fixme	   
	    {0, 8*0, 8, 8},
	    {0, 8*1, 8, 8},
	    {0, 8*2, 8, 8},
	    {0, 8*3, 8, 8},
	    {0, 8*4, 8, 8},
	    {0, 8*5, 8, 8},
	    {0, 8*6, 8, 8},
	    {0, 8*7, 8, 8},
	    {0, 8*8, 8, 8},
	    {0, 8*9, 8, 8},
	    {0, 8*10, 8, 8},
	    {0, 8*11, 8, 8},
	    {0, 8*12, 8, 8},
	    {0, 8*13, 8, 8},
	    {0, 8*14, 8, 8},
	    {0, 8*15, 8, 8},
	    {0, 8*16, 8, 8},
	    {0, 8*17, 8, 8},
	    {0, 8*18, 8, 8},
	    {0, 8*19, 8, 8},
	    {0, 8*20, 8, 8},
	    {0, 8*21, 8, 8},
	    {0, 8*22, 8, 8},
	    {0, 8*23, 8, 8},
	    {0, 8*24, 8, 8},
	    {0, 8*25, 8, 8},	   
	    {0, 8*26, 8, 8},
	    {0, 8*27, 8, 8},
	    {0, 8*28, 8, 8},
	    {0, 8*29, 8, 8},
	    {0, 8*30, 8, 8},
	    {0, 8*31, 8, 8},
	    {0, 8*32, 8, 8},
	    {0, 8*33, 8, 8},
	    {0, 8*34, 8, 8},
	    {0, 8*35, 8, 8},
	    {0, 8*36, 8, 8},
	    {0, 8*37, 8, 8},
	    {0, 8*38, 8, 8},
	    {0, 8*39, 8, 8},
	    {0, 8*40, 8, 8},
	    {0, 8*41, 8, 8},	    	    
	    {0, 8*42, 8, 8},
	    {0, 8*43, 8, 8},
	    {0, 8*44, 8, 8},
	    {0, 8*45, 8, 8},
	    {0, 8*46, 8, 8},
	    {0, 8*47, 8, 8},
	    {0, 8*48, 8, 8},
	    {0, 8*49, 8, 8},
	    {0, 8*50, 8, 8},
	    {0, 8*51, 8, 8},
	    {0, 8*52, 8, 8},
	    {0, 8*53, 8, 8},
	    {0, 8*54, 8, 8},
	};*/	
	
		
	void myInit() {        		
				
		GameMidlet();
		GameCanvas();
			 			
		    /*img[7].createImage(SW,SH);
		    load(1, "splash.png");
		    load(45, "logo.png");*/
		    
		    //mi[20].create("font8x8.png");
		    //myfnt= mi[21].create(mi[20], fontframes);				    		
		
		scene=SCENE_GAME;
	}	

	void gameKeypressed() {	 
		  /*if (keypressed[MY_KEY_LSOFT] > key_was_pressed || keypressed[MY_KEY_STAR] > key_was_pressed) {
				if (paused) paused= false; else
				if (title) effect[subscene].members->timeout= 0; 
				//else if (results) {leech(); effect[subscene].init();}
		  }
		    
	    
	  	if (keypressed[MY_KEY_RSOFT] > key_was_pressed || keypressed[MY_KEY_POUND] > key_was_pressed) 
			if (title || paused || results) quit(); else pause();
	    
	    if (keypressed[MY_KEY_UP] > key_was_pressed) if (SCROLLTXT-LINESPPAGE<0) SCROLLTXT=0; else SCROLLTXT-=LINESPPAGE;
	    if (keypressed[MY_KEY_DOWN] > key_was_pressed) if (SCROLLTXT+LINESPPAGE < NUMLINE) SCROLLTXT+=LINESPPAGE;
	    */
	    
	    if (keypressed[MY_KEY_LEFT]  > key_was_pressed) {	    	
	    	tran(subscene-1);
	    	effect[subscene].members->timeout=-1; //hack
	    }
	    
	    if (keypressed[MY_KEY_RIGHT]  > key_was_pressed) {	    	
	    	tran(subscene+1);
	    	effect[subscene].members->timeout=-1; //hack
	    }
	    
	    if (keypressed[MY_KEY_FIRE]  > key_was_pressed) {	    	
	    	spawn();
	    	//effect[subscene].members->timeout=-1; //hack
	    }
	    
	}
	
	
// demo /////////////////////////////////////////////////////////////////////////////////////////////////	
	

	void tran(int n) {
		if (!inited) return;

		if (n < 0) n= SIZEOF(effect)-1; //return;
		if (n >= SIZEOF(effect)/* && trans==0*/) n= 0; //return;

		newsub=n;			
		trans=2;

	}		

#ifdef lpc
	void load(int i, const void *s) {
			img[i].createImage(s);
	} 
#else
	void load(int i, String s) {
			//if (img[i]!=noimg) return;
			
			String s2= "res\\"+s;
			img[i].createImage(s2);
			//img[i]=noimg; 
	  
			//System.out.println("E load: "+s);		
		} 
#endif	
	
	void initall() {
		if (inited) return;
				
	    //meta[23]=new Image[16];
	    //for (int i=0; i<16; i++) meta[23][i]=Image.createImage("strip_"+i+".png"); //todo load method for meta	    	
				
		//FIXBOBS
		/*
		mi[1].create("ball.png");   
		mi[2].create(mi[1], bflyframes);    
		mi[3].create(mi[1], bflyframes2);    
		mi[4].create(mi[1], bflyframes3);*/
		
	    load(1, _back1);
	    load(2, _back2);
	    load(3, _greets);
	    load(4, _music);
	    load(5, _back);
	    load(6, _ball);
	    load(7, _amuq);
			load(8, _spiral);
			load(9, _spiral2);
			load(10, _title);
			load(11, _code);
			
	    //load(8, _strip);
	    //load(9, _stripf);
		
		/*load(12, "back.png");
		load(13, "back4.png");
		//img[14]=Image.createImage("overlay3.png");
		load(15, "background4.png");
		load(16, "overlay4.png");
		//img[17]=Image.createImage("spiral2.png");
		load(18, "background5.png");
		load(21, "overlay3tl.png");
		load(22, "overlay3tr.png");
		load(23, "overlay3br.png");
		load(24, "overlay3bl.png");
		load(25, "code.png");
		load(26, "music.png");
		load(27, "title.png");
		load(28, "pause.png");
		load(29, "strip.png");
		load(30, "stripf.png");
				
		load(32, "bar00.png");
		load(33, "bar01.png");
		load(34, "bar02.png");
		
		load(35, "barb00.png");
		load(36, "barb01.png");
		load(37, "barb02.png");
		//load(38, "exit.png");
		load(39, "internet.png");
		load(40, "arrow.png");
		load(41, "play.png");
		load(42, "quit.png");		
		load(43, "arrow00.png");
		load(44, "arrow01.png");
		//45 reserved for logo
*/		
			//FIXSPIRAL
			/*for (int i2=0; i2<2; i2++) {
				meta[i2]=new Image[64];
				
				for (int i=0; i<64; i++) {
					Image im=Image.createImage(105-(spiraltab[i][0]+spiraltab[i][1]), 1);
					im.getGraphics().drawImage(img[19+i2],-spiraltab[i][0], -i, g.LEFT|g.TOP);
					im=Image.createImage(im);
					meta[i2][i]=im;
				}
			}*/
	    
		
//#if n6600 || k700i			
//# //~		try {		
//# //~			for (int i=0; i<plr.length; i++) {									
//# //~				//InputStream is = Runtime.getRuntime().getClass().getResourceAsStream("ukko"+i+".mid");
//# //~				//plr[i]=Manager.createPlayer(is, "audio/midi");
//# //~				InputStream is = Runtime.getRuntime().getClass().getResourceAsStream("tune"+i+".amr");
//# //~				plr[i]=Manager.createPlayer(is, "audio/AMR");
//# //~				plr[i].realize();					
//# //~				plr[i].prefetch();
//# //~			
//# //~				vc[i]=((VolumeControl)plr[i].getControl("VolumeControl"));
//# //~				vc[i].setLevel(vol);								
//# //~			}		    	
//# //~						
//# //~	    }  catch(Exception e) {};		
//# //~		//plr[0].addPlayerListener(this); 
//# //~		//plr[1].setLoopCount(-1);					    			    		    			
//# //~
//#endif
		
		//effect[subscene].init(); //for (int i=0; i<effect.length; i++) effect[i].init();	    	
		inited=true;		
	}		

	/* The Constructor */
	void GameMidlet() {
		//System.gc(); System.out.println("ENTRY "+Runtime.getRuntime().freeMemory()+" / "+Runtime.getRuntime().totalMemory());

		//for (int i=0; i<SIZEOF(img); i++) img[i]=noimg; //fixmelpc noimg not inited

		//for (int i=0; i<SIZEOF(meta); i++) meta[i][0]=noimg;		
	
	}
		
	static void stopsound() {
//#if n6600 || k700i					
//# //~		try {
//# //~			//for (int i=0; i<snd.length; i++) snd[i].stop();				
//# //~			for (int i=0; i<plr.length; i++) {
//# //~				plr[i].stop();							
//# //~				//plr[i].close();
//# //~			}
//# //~	    }  catch(Exception e) {};
//#endif	    
	}		

	static void playsound(int n) {
//#if n6600 || k700i								
//# //~			if (plr[n].getState()==Player.STARTED) return;
//# //~			
//# //~			stopsound();
//# //~
//# //~			try {		
//# //~		    	plr[n].start();
//# //~		    }  catch(Exception e) {};
//#endif		
	}		

	/*static void quit() {
	    //System.out.println("quit");
		stopsound();
		gameOver= true;
	}*/

	/* Begin the application, show its frame */
	void startApp() {

	}


	/* Application is being terminated, kill threads */
	void pauseApp() {

	}

	static byte* loadFile(String s) {
		/*byte buf[]=null;

		try {
	    	if (s==null) return null;
	        InputStream res = Runtime.getRuntime().getClass().getResourceAsStream(s);
	        if(res == null) return null;
	        int flen=(int)res.skip(1000000);
			res.close();
	        if(flen<1) return null;
	        res = Runtime.getRuntime().getClass().getResourceAsStream(s);
	        buf = new byte[flen];

	        int bytes=0;
	        int offset=0;
	        while ( true ) {
	        	bytes=res.read(buf,offset,buf.length-offset);
	        	offset+=bytes;
	        	if ( bytes==-1 || offset>=buf.length ) break;
	        }
	        res.close();
		}  catch(Exception e) {System.out.println("E loadfile: "+s);};

	    return buf;*/

	}

	void setClip2(Graphics g, int x, int y, int w, int h) {
//if (h==54) System.out.println("duh");		
		g.setClip(x,y,w,h);
		g.clipRect(x,y,w,h);
	}


	void vibra(int hz, int del) {
	}

		
	int bwx[]={0,SW,SW,0};
	int bwy[]={0,0,SH,SH};
	int fadcnt, fadcol, fadalp, fadadd;

	int voltimeout;
		
	void GameCanvas() {
//#if k700i
//# //~		setFullScreenMode(true);
//#endif
	    
	}


	void fadein() {
		fadcol=0xffffff;
		fadalp=0;
		fadadd=4;
		fadcnt=SH+6*6;			
	}

	void fadeout() {
		fadcol=0xffffff;
		fadalp=0xff;
		fadadd=-4;
		fadcnt=SH+6*6;			
	}
	
	void spawn() {
		if (!inited) return;
		
			//effect[subscene]=(Effect)effect[subscene].getClass().newInstance(); //FIXMAIN?
			(effect[subscene].members->*effect[subscene].init)();

	}		    	
			  		
	void gameHandler() {
		(effect[subscene].members->*effect[subscene].run)();
		
		if (effect[subscene].members->timeout == 0) {
			tran(subscene+1);					 			
			effect[subscene].members->timeout=-1; //hack
		}
				
		if (trans>0) {					
			
			if (trans==2) {
				fadein();
				trans=1;				
			} else
			if (trans==1 && fadcnt==0) {								
				//if (effect[subscene]->frame>0) test[subscene]/= effect[subscene]->frame;
				//System.out.println(test[subscene]);
				    
				subscene=newsub;
				spawn();
				fadeout();				 
				trans=0;
			};
		} 
		
		
		if (fadcnt>=0) {			
			fadcnt-=Math::abs(fadadd);			
			if (fadcnt<0) fadcnt=0;
			fadalp+=fadadd;
			if (fadalp<0) fadalp=0;
			if (fadalp>255) fadalp=255;
		}
		
		if (subscene==1) playsound(0);
		if (subscene==7) playsound(1);				
		
		frame++;						
		effect[subscene].members->frame++; //=frame; //hack
		//test[subscene]+= fps;
//System.out.println(fps);		
		CNT++;
	}
		
	void gamePaint(Graphics g) {
		g.setClip(0,0,SW,SH);	
		
		(effect[subscene].members->*effect[subscene].paint)(g);
		
		if (fadalp>0) {
			g.setClip(0,0,SW,SH);

			g.setColor256(0x3); //g.setColor(0xffffff);
			if (fadadd<0) { //out			    			    
			    g.fillRect(0,-6*6,SW,fadcnt);
			    for (int i=1; i<=6; i++) g.fillRect(0, -6*6+ fadcnt+6*6-6*i,SW,i);			    			
			} else { //in
			    g.fillRect(0,-6*6+ fadcnt+6*6,SW,SH);
			    for (int i=1; i<=6; i++) g.fillRect(0,-6*6+ fadcnt+6*i,SW,i);			    			
			}
			
		}
				
		if (voltimeout>0) {
			g.setClip(0,0,SW,SH);
			g.setColor(128,128,128);
			g.fillRect(10,SH-10,SW-10*2,5);
			g.setColor(0,255,0);
			int n=((SW-10*2)*vol<<8)/100>>8;
			g.fillRect(10,SH-10,n,5);
			g.setColor(50,50,50);
			g.drawRect(10,SH-10,SW-10*2,5);
			voltimeout--;
		}		
		
		//if (!title && !results && !paused) g.drawImage(img[40], SW-2, SH-2, g.RIGHT|g.BOTTOM);
		/*String s="("+subscene+") "+"f"+fps+" e"+effect[subscene].frame+" g"+frame;
		int x=10;
		int y=SH-20;
		g.setClip(0,0,SW,SH);				

		g.setColor(0);
		g.drawString(s, x-1, y, g.LEFT|g.TOP);
		g.drawString(s, x+1, y, g.LEFT|g.TOP);
		g.drawString(s, x, y-1, g.LEFT|g.TOP);
		g.drawString(s, x, y+1, g.LEFT|g.TOP);

		g.setColor(255,255,255);
		g.drawString(s, x, y, g.LEFT|g.TOP);
		fpscnt++; */
		
	}
	
	void drawTri(Graphics g, int x, int y, int attr) {
	} 

	void paint2(Graphics g) {

		switch (scene) {
			case 0:
				gamePaint(g);
			break;

		}

	}

	
//} // GameCanvas
