예제 #1
0
void ColorSample::setColor(int _r, int _g, int _b) 
{
	KrRGBA color;

	color.Set(_r, _g, _b, 255);
	setColor(color);
}
예제 #2
0
void BemGame::DrawMiniMap()
{
	int k, m, n;

	KrResource* res = engine->Vault()->GetResource( KYRATAG_CANVAS, "minimap" );
	KrCanvasResource* cres = res->ToCanvasResource();
	GLASSERT( cres );

	KrRGBA* target = cres->Pixels();
	int factorX = cres->Width()  / MAPX;
	int factorY = cres->Height() / MAPY;

	memset( target, 0, sizeof( KrRGBA ) * cres->Height() * cres->Width() );
	
	// Draw the floor tiles on the map.

	for( m=0; m<MAPX; m++ )
	{
		for( n=0; n<MAPY; n++ )
		{
			if ( GetMap( m, MAPY - 1 - n ) == FLOOR ) // The map is stored upside down.
			{
				KrRGBA color;
				color.Set( 0, 0, 255, 255 );

				int x = factorX * m;
				int y = factorY * n;
				
				DrawMiniMapBox( cres, color, 
								x+1, y+1, 
								factorX-1, factorY-1 );
			}
		}
	}

	// Draw a box for each actor:
	for( k=0; k<numActors; k++ )
	{
		// Draw a box for each actor.
		KrRGBA color;
		if ( actor[k].sprite->SpriteResource()->ResourceName() == "DRONE" )
			color.Set( 255, 0, 0, 180 );
		else
			color.Set( 0, 255, 0, 180 );

		int x = factorX * actor[k].mapX;
		int y = factorY * ( MAPY - actor[k].mapY - 1);	// The map is stored upside down.

		DrawMiniMapBox( cres, color,
						x+1, y+1, 
						factorX-1, factorY-1 );

	}
	// Remember we have to refresh a canvas after we draw!
	cres->Refresh();	
}
예제 #3
0
KrRGBA HSV_to_RGB(double H, double S, double V, bool bRound = true) 
{ 
	//HSV values = From 0 to 1
	//RGB results = From 0 to 255
	//http://www.easyrgb.com/math.php?MATH=M21#text21

	if(bRound)
	{
		H = round(H);
		S = round(S);
		V = round(V);
	}
	
	H /= 255.0;
	S /= 255.0;
	V /= 255.0;  

	KrRGBA RGB;  
	
	
	if ( S == 0 )                       
	{
		RGB.Set(V * 255, V * 255, V * 255, 255);
	}
	else
	{
		double var_h, var_i, var_1, var_2, var_3, var_r, var_g, var_b;

		if(H == 1.0) H = 0.0;

		var_h = H * 6;
		var_i = int( var_h );             //Or ... var_i = floor( var_h )
		var_1 = V * ( 1 - S );
		var_2 = V * ( 1 - S * ( var_h - var_i ) );
		var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );
		
		if      ( var_i == 0 ) { var_r = V     ; var_g = var_3 ; var_b = var_1; }
		else if ( var_i == 1 ) { var_r = var_2 ; var_g = V     ; var_b = var_1; }
		else if ( var_i == 2 ) { var_r = var_1 ; var_g = V     ; var_b = var_3; }
		else if ( var_i == 3 ) { var_r = var_1 ; var_g = var_2 ; var_b = V;     }
		else if ( var_i == 4 ) { var_r = var_3 ; var_g = var_1 ; var_b = V;     }
		else                   { var_r = V     ; var_g = var_1 ; var_b = var_2; }


		if(bRound)
			RGB.Set(round(var_r * 255), round(var_g * 255), round(var_b * 255), 255);
		else
			RGB.Set(var_r * 255, var_g * 255, var_b * 255, 255);
	}
	
	return RGB; 
} 
예제 #4
0
void TestBlitter::DrawFrame()
{
	numTest = 0;

	KrRGBA newColor;
	KrRGBA baseColor;
	KrColorTransform cform;
	
	newColor.Set( 160, 110, 40, 80 );
	baseColor.Set( 20, 80, 200 );
	cform.Set( 40, 80, 50, 90, 30, 40, 200 );

	SetUp( newColor, baseColor, cform );

	char buf[ 256 ];
	sprintf( buf, "                      color             surface           resulting" );
	textBox->SetTextChar( buf, 0 );

	int line = 1;
	for( int i=0; i<numTest; i++ )
	{
		if ( i%3 == 0 )
		{
// 			++line;
			sprintf( buf, "                                                 Optimal %3d %3d %3d",
					 test[i].r.ToIntRoundUp(), 
					 test[i].g.ToIntRoundUp(), 
					 test[i].b.ToIntRoundUp() );
			textBox->SetTextChar( buf, line );
			++line;
		}

		bool ok =    (( test[i].r.ToIntRoundUp() - test[i].resultColor.c.red ) < 10 )
		          && (( test[i].g.ToIntRoundUp() - test[i].resultColor.c.green ) < 10 )
				  && (( test[i].b.ToIntRoundUp() - test[i].resultColor.c.blue ) < 10 );
		          
		sprintf( buf, "%20s %3d %3d %3d %3d   %3d %3d %3d %3d   %3d %3d %3d  %s",
				 test[i].name.c_str(),
				 test[i].newColor.c.red, test[i].newColor.c.green, test[i].newColor.c.blue, test[i].newColor.c.alpha,
				 test[i].surfaceColor.c.red, test[i].surfaceColor.c.green, test[i].surfaceColor.c.blue, test[i].surfaceColor.c.alpha,
				 test[i].resultColor.c.red, test[i].resultColor.c.green, test[i].resultColor.c.blue,
				 ok ? "[ok]" : "[error]" );

		textBox->SetTextChar( buf, line );
		++line;
	}
	
	engine->Draw();
}
예제 #5
0
SharedStateData::SharedStateData( SDL_Surface* screen )
{
	currentSprite = 0;
	currentFrame = 0;
	currentAction = 0;
	currentTile = 0;
	currentObject = SPRITE;

	// Use the sprite engine for the editor. It's a way to
	// test everything.
	engine = new KrEngine( screen );
	GLASSERT( engine );

	// Make the backgrounds reasonably sized so we don't spend
	// all our time sorting.
	backgroundRes = new KrCanvasResource( "backgroundRes", 100, 100, false );
	for ( int i=0; i<100; i++ )
	{
		for( int j=0; j<100; j++ )
		{
			KrRGBA* pixel = backgroundRes->Pixels() + i + j*backgroundRes->Width();
			if ( ( ( i / 10 ) & 0x01 ) == ( ( j / 10 ) & 0x01 ) ) 
				pixel->c.red = pixel->c.green = pixel->c.blue = 30;
			else
				pixel->c.red = pixel->c.green = pixel->c.blue = 60;
		}
	}
	engine->Vault()->AddResource( backgroundRes );

//	engine->Vault()->LoadDatFileFromMemory( fontDat, fontDatSize );
// 	engine->Vault()->LoadDatFile( "consolefont.dat" );
	KrFontResource* fontRes = KrEncoder::CreateFixedFontResource( "CONSOLE", CONSOLEFONT_DAT, CONSOLEFONT_SIZE );
	engine->Vault()->AddResource( fontRes );
	fontResource = engine->Vault()->GetFontResource( "CONSOLE" );
	GLASSERT( fontResource );

	consoleHolder = new KrImNode();
	consoleHolder->SetZDepth( CONSOLE_DEPTH );
	engine->Tree()->AddNode( 0, consoleHolder );

	int consoleHeight = fontResource->FontHeight() * 6;
	int consoleWidth  = screen->w * 9 / 12;
	consoleHolder->SetPos( 0, screen->h - consoleHeight );

	KrScheme scheme( fontResource );
	console = new KrConsole( consoleWidth, consoleHeight, 0, scheme );
	engine->Tree()->AddNode( consoleHolder, console );

	KrRGBA blue;
	blue.Set( 0, 0, 80 );
	console->SetBackgroundColor( blue );

	int a, b, c;
	engine->Version( &a, &b, &c );
	console->Print( "Welcome to the Sprite Editor! [version %d.%d.%d]\nThis area is the input console.\n",
					a, b, c  );

	// A text box for output, with a background.
	// And toss it background for good measure.
	KrRGBA infoColor[2];
	infoColor[0].Set( 0, 120, 0 );
	infoColor[1].Set( 0, 70,  0 );
	KrBoxResource* infoResource = new KrBoxResource( "", 
													 screen->w - consoleWidth, 
													 consoleHeight,
													 infoColor, 2,
													 KrBoxResource::FILL );
	engine->Vault()->AddResource( infoResource );

	infoBoxBack = new KrBox( infoResource );
	engine->Tree()->AddNode( 0, infoBoxBack );
	infoBoxBack->SetZDepth( INFO_BACKGROUND_DEPTH );
	infoBoxBack->SetPos( consoleWidth, screen->h - consoleHeight );

	infoBox = new KrTextBox(	fontResource,
								screen->w - consoleWidth, 
								consoleHeight, 0 );

	engine->Tree()->AddNode( infoBoxBack, infoBox );
	infoBox->SetZDepth( INFO_DEPTH );
	//infoBox->SetPos( consoleWidth, screen->h - consoleHeight );

	// Create a new node for the view state. Set
	// it to a depth of one, so that the image
	// (loaded later) won't cover up the widgets.
	widgetNode = new KrImNode();
	widgetNode->SetZDepth( WIDGET_DEPTH );
	engine->Tree()->AddNode( 0, widgetNode );
	
	widget = new EdWidget( engine->Tree(), widgetNode, 0 );

	imnode = new KrImNode();
	imnode->SetZDepth( MAIN_IMNODE_DEPTH );
	engine->Tree()->AddNode( 0, imnode );

// 	spriteData = new TiXmlDocument;
	defFileName = "";
	animAction = 0;
	canvasResource = 0;
	nTrans = 0;
}
예제 #6
0
int main( int argc, char *argv[] )
{
	const SDL_version* sdlVersion = SDL_Linked_Version();
	if ( sdlVersion->minor < 2 )
	{
		printf( "SDL version must be at least 1.2.0\n" );
		GLASSERT( 0 );
		exit( 254 );
	}

	if ( argc < 2 )
	{
		printf( "Usage: krmapmaker map.xml\n" );
		exit( 253 );
	}

	TiXmlDocument doc( argv[1] );
	doc.LoadFile();

	XmlUtil xmlUtil( &doc );
	if ( !xmlUtil.IsValidMap() )
	{
		printf( "Not a valid map file.\n" );
		exit( 252 );
	}

	/* Initialize the SDL library */
	if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_NOPARACHUTE) < 0 ) {
		#ifdef DEBUG
			GLOUTPUT( "Couldn't initialize SDL: %s\n",SDL_GetError());
		#endif
		exit(255);
	}

	SDL_WM_SetCaption( "Kyra MapMaker", 0 );

	int screenX = 800;
	int screenY = 600;

	SDL_Surface* screen = SDL_SetVideoMode( screenX, screenY, 32, SDL_SWSURFACE );

	if ( screen )
	{
		KrResourceVault fontVault;
		//fontVault.LoadDatFileFromMemory( fontDat, sizeof( fontDat ) );
		KrFontResource* consoleFontRes =  KrEncoder::CreateFixedFontResource( "CONSOLE", CONSOLEFONT_DAT, CONSOLEFONT_SIZE );

		Layout layout( screen, consoleFontRes );
		if ( layout.Engine()->Vault()->LoadDatFile( xmlUtil.DatFileName().c_str() ) != true )
		{
			printf( "Couldn't load dat file.\n" );
			exit( 250 );
		}

		KrSquareWorldMap* worldMap = new KrSquareWorldMap( 100, 100, 100 );	// fixme hardcoded!
		layout.Engine()->Tree()->AddNode( layout.Map(), worldMap );
		worldMap->SetPos( 0, screenY-1, Layout::MAIN_VIEW );

		xmlUtil.Init( layout.Engine()->Vault(), layout.Engine(), layout.Map(), worldMap );
//		xmlUtil.InsertLayers( layout.LayerBox() );

		UILogic logic( &layout, &xmlUtil );
		layout.SetLayer( xmlUtil.GetLayer( "null" ), 0 );		

		KrRGBA white;
		white.Set( 200, 200, 200 );
		KrBox* whereBox = new KrBox( layout.Engine()->ScreenBounds( Layout::MAIN_VIEW ).Width(),
									 layout.Engine()->ScreenBounds( Layout::MAIN_VIEW ).Height(),
									 white, 
									 KrBoxResource::OUTLINE );
		layout.Engine()->Tree()->AddNode( 0, whereBox );
		whereBox->SetVisible( false );
		whereBox->SetVisible( true, Layout::MINIMAP_VIEW );

		layout.Engine()->Draw();

		SDL_Event event;

		while ( SDL_WaitEvent( &event ) )
		{
			if ( event.type == SDL_QUIT )
				break;

			KrEventManager::Instance()->HandleEvent( event, layout.Engine() );

			bool mapChanged = false;

			if (	( event.type == SDL_MOUSEMOTION || event.type == SDL_MOUSEBUTTONDOWN )
				 && layout.Engine()->GetWindowFromPoint( event.motion.x, event.motion.y ) == Layout::MAIN_VIEW )
			{
				KrVector2T< GlFixed > map;
				worldMap->ScreenToMap( event.motion.x, event.motion.y, &map, Layout::MAIN_VIEW );
				layout.DisplayMapCoords( map.x.ToFloat(), map.y.ToFloat() );

				if(			event.type == SDL_MOUSEBUTTONDOWN 
					   ||  ( event.type == SDL_MOUSEMOTION && event.motion.state == 1 ) )
				{
					if ( logic.CurrentImage() )
					{
						worldMap->SetLoc( map.x.ToInt(), map.y.ToInt(), 
										  logic.CurrentImage()->Clone()->ToImage() );
						mapChanged = true;
					}
				}
			}
			else if ( event.type == SDL_KEYDOWN )
			{
				if ( event.key.keysym.sym == SDLK_UP )
				{
					worldMap->SetPos( worldMap->X(), worldMap->Y() + worldMap->TileHeight(), Layout::MAIN_VIEW );
					mapChanged = true;
				}
				else if ( event.key.keysym.sym == SDLK_DOWN )
				{
					worldMap->SetPos( worldMap->X(), worldMap->Y() - worldMap->TileHeight(), Layout::MAIN_VIEW );
					mapChanged = true;
				}				
				else if ( event.key.keysym.sym == SDLK_RIGHT )
				{
					worldMap->SetPos( worldMap->X() - worldMap->TileWidth(), worldMap->Y(), Layout::MAIN_VIEW );
					mapChanged = true;
				}
				else if ( event.key.keysym.sym == SDLK_LEFT )
				{
					worldMap->SetPos( worldMap->X() + worldMap->TileWidth(), 
									  worldMap->Y(), 
									  Layout::MAIN_VIEW );
					mapChanged = true;
				}
				else if ( event.key.keysym.sym >= SDLK_0 && event.key.keysym.sym <= SDLK_7 )
				{
					layout.SetRotation( event.key.keysym.sym - SDLK_0 );
					logic.SetRotation( event.key.keysym.sym - SDLK_0 );
				}
			}

			if ( mapChanged )
			{
				layout.Engine()->Tree()->Walk();
				KrRect bounds = worldMap->CompositeBounds( Layout::MAIN_VIEW );

//				layout.Map()->SetPos(	-bounds.xmin, // - layout.Engine()->ScreenBounds( Layout::MAIN_VIEW ).xmin,
//										-bounds.ymin, // - layout.Engine()->ScreenBounds( Layout::MAIN_VIEW ).ymin,
//										Layout::MINIMAP_VIEW );

				GlFixed scale = GlMin(  GlFixed( layout.Engine()->ScreenBounds( Layout::MINIMAP_VIEW ).Width() ) / GlFixed( bounds.Width() ),
										GlFixed( layout.Engine()->ScreenBounds( Layout::MINIMAP_VIEW ).Height() ) / GlFixed( bounds.Height() ) );
				
				layout.Map()->SetPos(	0,
										( GlFixed( Layout::UI_WIDTH ) / scale ).ToInt(),
										Layout::MINIMAP_VIEW );

				layout.Engine()->Tree()->Root()->SetScale( scale, scale, Layout::MINIMAP_VIEW );
				GLOUTPUT( "Minimap scale set to %f\n", scale.ToFloat() );

				whereBox->SetPos( -worldMap->X(), bounds.Height() - 1 - worldMap->Y() );		
			}

			layout.Engine()->Draw();
		}	
	}
	SDL_Quit();

	return 0;
}
예제 #7
0
BemGame::BemGame( SDL_Surface* screen, bool useWindows )
{
	random.SetSeed( 0 );

	KrRGBA green;
	green.Set( 0, 200, 0 );
	
	if ( useWindows )
	{
		KrRect rects[5];
		
		const int lW = 280;
		const int lH = 180;
		const int r = 200;
		const int pad = 10;
		
		rects[0].Set( 0,   0, screen->w - 1 - pad - r, r + pad - 1 );
		rects[1].Set( 0, r + pad, screen->w-1, screen->h - 1 - pad - lH );
		rects[2].Set( lW + pad, screen->h - pad - lH, screen->w-1, screen->h - 1 );

		rects[3].Set( 0, screen->h - lH, lW-1, screen->h - 1 );
		rects[4].Set( screen->w - r, 0, screen->w-1, r-1 );

		engine = new KrEngine( screen, 5, rects, &green );
	}
	else
	{
		engine = new KrEngine( screen );
	}
	GLASSERT( engine );

	// The vault contains all the resources used by this
	// demo.
	if (!engine->Vault()->LoadDatFile( "bem.dat" ) )
	{
		GLOUTPUT(( "Error loading dat file: 'bem.dat'!\n" ));
		exit( 100 );
	}

	KrRGBA black;
	black.Set( 0, 0, 0 );
	if ( useWindows )
	{
		engine->FillBackgroundWindow( 0, 0 );			// main
		engine->FillBackgroundWindow( 1, 0 );			// main
		engine->FillBackgroundWindow( 2, 0 );			// main
		engine->FillBackgroundWindow( 3, &black );		// zoomed out
		engine->FillBackgroundWindow( 4, &black );		// moving zoom
	}
	else
	{
		// We have a space image so the engine should not draw
		// the background. Performance enhancement.
		engine->FillBackground( 0 );
	}

	// Set up some depth categories:
	backgroundTree = new KrImNode;
	underTree	   = new KrImNode;
	floorTree      = new KrImNode;
	standingTree   = new KrImNode;
	overTree       = new KrImNode;

	// Depth sort by order of addition. All the z-depths are
	// left at 0. In other words, when added at the same Z-depth (0
	// in this case) the object added most recently is on top.
	engine->Tree()->AddNode( 0, backgroundTree );
	engine->Tree()->AddNode( 0, underTree );
	engine->Tree()->AddNode( 0, floorTree );
	engine->Tree()->AddNode( 0, standingTree );
	engine->Tree()->AddNode( 0, overTree );

	// Store the floor size. Due to the way the sprites are defined,
	// the size in terms of positioning the tiles is slightly different
	// than the size in terms of the tile bitmap.
	KrSpriteResource* resource = engine->Vault()->GetSpriteResource( BEM_ROOM | BEM_FLOOR );
	GLASSERT( resource );
	tileWidth  = resource->GetActionByIndex( 0 )->Frame( 0 ).Width() +2;
	tileHeight = resource->GetActionByIndex( 0 )->Frame( 0 ).Height();

	isoMath = new GlIsoMath( tileWidth, tileHeight );
	isoMath->SetScreenCenterToTile( screen->w,
									screen->h,
									MAPX / 2, MAPY / 2, 0 );

	AddSpaceTiles();
	AddFloor();
	AddRoomObjects();
	AddActors( useWindows );

	// Add the mini-map
	KrCanvasResource* canvasResource =
					new KrCanvasResource(	"minimap",
											140, 140,
											true  );
	GLASSERT( canvasResource );


	// An example of a user-defined resource getting added to a vault.
	engine->Vault()->AddResource( canvasResource );

	canvas = new KrCanvas( canvasResource );
	canvas->SetPos( screen->w - canvasResource->Width(),
					screen->h - canvasResource->Height() );
	engine->Tree()->AddNode( overTree, canvas );

	subtick = 0;
	tick = 0;
	teleFrame = 0;
	teleSprite = 0;
	DrawMiniMap();

	if ( useWindows )
	{
		// Set the 3 "band" windows to be positioned
		// to the first.
 		engine->Tree()->Root()->SetPos( 0, 0, 0 );
		engine->Tree()->Root()->SetPos( -engine->ScreenBounds(1).min.x, -engine->ScreenBounds(1).min.y, 1 );
 		engine->Tree()->Root()->SetPos( -engine->ScreenBounds(2).min.x, -engine->ScreenBounds(2).min.y, 2 );

		SetupLeftWindow();
		SetupRightWindow();
	}
}
예제 #8
0
TileTest::TileTest( SDL_Surface* _screen )
{
	drawn = false;
	screen = _screen;
	engine = new KrEngine( screen );
	//GLOUTPUT( "TileTest::TileTest\n" );
	engine->Validate();	// check validity

	int i, j;

	// Classes to hold other objects:
	engine->Vault()->LoadDatFile( "standardtest.dat" );

	KrTileResource* noAlphaRes = engine->Vault()->GetTileResource( "NOALPHA" );
	KrTileResource* alphaRes = engine->Vault()->GetTileResource( "ALPHA" );
	GLASSERT( noAlphaRes && alphaRes );

	// Transforms:
	KrColorTransform alphaCForm;
	alphaCForm.SetAlpha( 255 * 70 / 100 );

	KrColorTransform redCForm;
	redCForm.SetRed( 255 * 50 / 100, 127 );

	KrColorTransform blueCForm;
	blueCForm.SetBlue( 255 * 50 / 100, 127 );

	KrColorTransform greenAlphaCForm;
	greenAlphaCForm.SetGreen( 255 * 50 / 100, 127 );
	greenAlphaCForm.SetAlpha( 255 * 70 / 100 );

	KrColorTransform blueAlphaCForm;
	blueAlphaCForm.SetBlue( 255 * 50 / 100, 127 );
	blueAlphaCForm.SetAlpha( 255 * 70 / 100 );

	// Containers:
	KrImNode* c0 = new KrImNode;	// background
	KrImNode* c1 = new KrImNode;	// tiles
 	KrImNode* c2 = new KrImNode;	// canvas

	engine->Tree()->AddNode( 0, c0 );
	engine->Tree()->AddNode( c0, c1 );
	engine->Tree()->AddNode( c0, c2 );

	c1->SetPos(   0,   0 );
	c2->SetPos(	420,   0 );
	c1->SetZDepth( 1 );
	c2->SetZDepth( 1 );

	// ---------- Background ----------- //
	KrSpriteResource* backSpriteRes = engine->Vault()->GetSpriteResource( "BACKGROUND" );
	GLASSERT( backSpriteRes );
	KrAction* action = backSpriteRes->GetActionByIndex( 0 );
	GLASSERT( action );
	const KrRle& rle = action->Frame( 0 );

	for ( i=0; i <= (screen->w) / rle.Width(); i++ )
	{
		for ( j=0; j <= (screen->h) / rle.Height(); j++ )
		{
			KrSprite* sprite = new KrSprite( backSpriteRes );
			sprite->SetPos( i*rle.Width(), j*rle.Height() );
			GLASSERT( sprite );
			engine->Tree()->AddNode( c0, sprite );
		}
	}

	// ---------- The "no alpha" tile.
	// no transform:
	for ( i=0; i<8; i++ )
	{
		noAlpha[i][0] = new KrTile( noAlphaRes );
		noAlpha[i][0]->SetPos( i*noAlpha[i][0]->Size(), 0 );
		noAlpha[i][0]->SetRotation( i );
		GLASSERT( noAlpha );
		engine->Tree()->AddNode( c1, noAlpha[i][0] );
	}

	// alpha:
	for ( i=0; i<8; i++ )
	{
		noAlpha[i][1] = new KrTile( noAlphaRes );
		noAlpha[i][1]->SetColor( alphaCForm );
		noAlpha[i][1]->SetPos( i*noAlpha[i][1]->Size(), 1*noAlpha[i][1]->Size() );
		noAlpha[i][1]->SetRotation( i );
		GLASSERT( noAlpha[i][1] );
		engine->Tree()->AddNode( c1, noAlpha[i][1] );
	}

	// red:
	for ( i=0; i<8; i++ )
	{
		noAlpha[i][2] = new KrTile( noAlphaRes );
		noAlpha[i][2]->SetColor( redCForm );
		noAlpha[i][2]->SetPos( i*noAlpha[i][2]->Size(), 2*noAlpha[i][2]->Size() );
		noAlpha[i][2]->SetRotation( i );
		GLASSERT( noAlpha[i][2] );
		engine->Tree()->AddNode( c1, noAlpha[i][2] );
	}

	// combination:
	for ( i=0; i<8; i++ )
	{
		noAlpha[i][3] = new KrTile( noAlphaRes );
		noAlpha[i][3]->SetColor( blueAlphaCForm );
		noAlpha[i][3]->SetPos( i*noAlpha[i][3]->Size(), 3*noAlpha[i][3]->Size() );
		noAlpha[i][3]->SetRotation( i );
		GLASSERT( noAlpha );
		engine->Tree()->AddNode( c1, noAlpha[i][3] );
	}

	// ---------- The "alpha" tile.
	// no transform:
	for ( i=0; i<8; i++ )
	{
// 		i=6;
		alpha[i][0] = new KrTile( alphaRes );
		alpha[i][0]->SetPos( i*alpha[i][0]->Size(), 4*alpha[i][0]->Size() );
		alpha[i][0]->SetRotation( i );
		GLASSERT( alpha[i][0] );
		engine->Tree()->AddNode( c1, alpha[i][0] );
	}

	// alpha:
	for ( i=0; i<8; i++ )
	{
		alpha[i][1] = new KrTile( alphaRes );
		alpha[i][1]->SetColor( alphaCForm );
		alpha[i][1]->SetPos( i*alpha[i][1]->Size(), 5*alpha[i][1]->Size() );
		alpha[i][1]->SetRotation( i );
		GLASSERT( alpha[i][1] );
		engine->Tree()->AddNode( c1, alpha[i][1] );
	}

	// red:
	for ( i=0; i<8; i++ )
	{
		alpha[i][2] = new KrTile( alphaRes );
		alpha[i][2]->SetColor( redCForm );
		alpha[i][2]->SetPos( i*alpha[i][2]->Size(), 6*alpha[i][2]->Size() );
		alpha[i][2]->SetRotation( i );
		GLASSERT( alpha[i][2] );
		engine->Tree()->AddNode( c1, alpha[i][2] );
	}

	// combination:
	for ( i=0; i<8; i++ )
	{
		alpha[i][3] = new KrTile( alphaRes );
		alpha[i][3]->SetColor( blueAlphaCForm );
		alpha[i][3]->SetPos( i*alpha[i][3]->Size(), 7*alpha[i][3]->Size() );
		alpha[i][3]->SetRotation( i );
		GLASSERT( alpha[i][3] );
		engine->Tree()->AddNode( c1, alpha[i][3] );
	}

	// ----------- A canvas ----------------- //
	KrCanvasResource* canvasResource = new KrCanvasResource( "mycanvas",
															 50, 50,
															 true );
	engine->Vault()->AddResource( canvasResource );
	KrRGBA* pixels = canvasResource->Pixels();
	KrRGBA color;
	for( i=0; i<canvasResource->Width(); i++ )
	{
		for( j=0; j<canvasResource->Height(); j++ )
		{
			color.c.red = i*4 + 50;
			color.c.green = j*4 + 50;
			color.c.blue = 0;
			color.c.alpha = 255 - i;
			pixels[ j*canvasResource->Width() + i ] = color;
		}
		// Put in a diagonal line:
		color.Set( 0, 0, 255 );
		pixels[ i*canvasResource->Width() + i ] = color;
	}

	const int NUMCANVASDIV2 = NUMCANVAS / 2;
	for ( i=0; i<NUMCANVASDIV2; i++ )
	{
		GlFixed sx;
		GlFixed sy;

		// The left canvas:
		canvas[i*2] = new KrCanvas( canvasResource );

		engine->Tree()->AddNode( c2, canvas[i*2] );
		canvas[i*2]->SetPos( 0, i * canvas[i*2]->Height() * 3 / 2 );
		sx.v = GlFixed_1 * (i+1) / 3;
		sy.v = GlFixed_1 * (i+1) / 3;

		canvas[i*2]->SetScale( sx, sy );

		// The right canvas:
		canvas[i*2+1] = new KrCanvas( canvasResource );
		engine->Tree()->AddNode( c2, canvas[i*2+1] );

		canvas[i*2+1]->SetPos( 100, i * canvas[i*2+1]->Height() * 3 / 2 );
		sx.v = GlFixed_1 * (NUMCANVASDIV2 + 1 - i) / 3;
		sy.v = GlFixed_1 * (i+1) / 3;

		canvas[i*2+1]->SetScale( sx, sy );
	}
}
예제 #9
0
파일: scale.cpp 프로젝트: Zitrax/brainblast
void ScalingTest::DrawFrame()
{
	if ( state == BOTH )		
	{	
		scaleX.v += GlFixed_1 / 64;
		scaleY.v += GlFixed_1 / 64;
		if ( scaleX > GlFixed( 1.4 ) ) 
		{
			scaleX.v = GlFixed_1 / 2;
			scaleY.v = GlFixed_1 / 2;
			state = HORIZONTAL;
		}
	}
	else if ( state == HORIZONTAL )
	{
		scaleX.v += GlFixed_1 / 64;
		if ( scaleX > GlFixed( 1.4 ) ) 
		{
			scaleX.v = GlFixed_1 / 2;
			scaleY.v = GlFixed_1 / 2;
			state = VERTICAL;
		}
	}
	else
	{
		scaleY.v += GlFixed_1 / 64;
		if ( scaleY > GlFixed( 1.4 ) ) 
		{
			scaleX.v = GlFixed_1 / 8;
			scaleY.v = GlFixed_1 / 8;
			state = BOTH;
		}
	}

	ship->SetScale( scaleX, scaleY );

	travellingShip->SetPos( travellingShip->X() - 2, travellingShip->Y() );
	if ( travellingShip->X() < 0 ) 
	{
		travellingShip->SetPos( engine->ScreenBounds().Width(), travellingShip->Y() );
	}

	GlFixed tscale;
	tscale = 1.0 + sin( double( Frame() ) / 25.0 ) / 2.0;
	travellingShip->SetScale( tscale, tscale );

	for( int i=0; i<3; ++i )
	{
		canvas[i]->SetScale( scaleX, scaleY );
	}

	char buffer[ 64 ];
	sprintf( buffer, "scale: %.3f X %.3f", scaleX.ToDouble(), scaleY.ToDouble() );
	textBox->SetTextChar( buffer, 0 );
	textBox->SetTextChar( "Sprite", 1 );
	textBox->SetTextChar( "Alpha Blended Canvas: fast -- bi-linear -- adaptive", 2 );

	KrRect bounds;
	KrMatrix2 xForm;
	xForm.Set( SHIPX, SHIPY, scaleX, scaleY );
 	ship->SpriteResource()->GetAction( "BODY" )->CalculateBounds( 0, xForm, &bounds );

	// Add the background alignment boxes.
	KrRGBA color;;
	color.Set( 255, 0, 0, 128 );

	KrBoxResource* boxRes = new KrBoxResource(	"databox",
												bounds.Width(), bounds.Height(),
												&color, 1,
												KrBoxResource::OUTLINE );
	KrBox* box = new KrBox( boxRes );
	box->SetPos( bounds.min.x, bounds.min.y );
	engine->Tree()->AddNode( 0, box );

	engine->Draw();

	engine->Tree()->DeleteNode( box );

	delete boxRes;
}
예제 #10
0
void KrEncoder::CreateIsoTile(	KrPaintInfo* info, 
								KrConsole* console,
								int x, int y,
								int width, int height,
								KrRle* rle,
								int isoWidth,
								int rotation )
{
	if ( isoWidth % 4 )
	{
		console->Print( "ERROR: Isometric tile created with non-multiplo of 4 width (%d).\n", isoWidth );
		return;
	}

	int isoHeight = isoWidth / 2;
	KrPainter painter( info );
	KrRGBA rgba;

	// Create a memory buffer to hold the tile:
	KrRGBA* isoMemory = new KrRGBA[ isoWidth * isoHeight ];
	memset( isoMemory, 0, isoWidth * isoHeight * sizeof( KrRGBA ) );

	for( int iy = 0; iy < isoHeight; ++iy )
	{
		int rowwidth = 0;
		if ( iy < isoHeight / 2 )
			rowwidth = 2 + 4 * iy;
		else
			rowwidth = 2 + 4 * ( isoHeight - iy - 1 );

		const int QUALITY = 4;		// 2, 4
		const int QUALITYAREA = 16;	// 4, 16
		const int QUALITYBIAS = 7;	// 1, 7
		const GlFixed increment = GlFixed( 1  ) / GlFixed( QUALITY );

		for( int ix =  isoWidth / 2 - rowwidth / 2;
		         ix <  isoWidth / 2 + rowwidth / 2;
				 ++ix )
		{
			int red = 0, green = 0, blue = 0, alpha = 0;

			for ( int i=0; i<QUALITY; ++i )
			{
				for ( int j=0; j<QUALITY; ++j )
				{
					GlFixed fsx, fsy;
					int sx, sy;

					IsoToSource( ix + increment*i,   
								 iy - isoHeight / 2 + increment*j,   
								 isoWidth, width, height, &fsx, &fsy,
								 rotation, increment );
					
					//sx = fsx.ToInt();
					//sy = fsy.ToInt();

					sx = GlClamp( fsx.ToIntRound(), 0, width - 1 );
					sy = GlClamp( fsy.ToIntRound(), 0, height - 1 );					

					painter.BreakPixel( sx + x, sy + y, &rgba );
					
					red   += rgba.c.red;
					green += rgba.c.green;
					blue  += rgba.c.blue;
					alpha += rgba.c.alpha;
				}
			}

			// Whew! all that for one pixel.
			// Use rounding on the colors, to gamma-correct
			rgba.Set(	( red + QUALITYBIAS ) / QUALITYAREA, 
						( green + QUALITYBIAS ) / QUALITYAREA, 
						( blue + QUALITYBIAS ) / QUALITYAREA, 
						( alpha + QUALITYBIAS ) / QUALITYAREA );
			GLASSERT( iy >= 0 && iy < isoHeight );
			GLASSERT( ix >= 0 && ix < isoWidth );
			isoMemory[ iy * isoWidth + ix ] = rgba;
		}
	}
	KrPaintInfo isoInfo( isoMemory, isoWidth, isoHeight );
	rle->Create( &isoInfo, 0, 0, 
				 isoWidth, isoHeight, 
				 ( isoWidth - 1 ) / 2, ( isoHeight - 1 ) / 2, 
				 isoWidth, isoHeight );
	delete [] isoMemory;
}
예제 #11
0
void KrProgress::Update()
{
    if (!m_progress || !Engine())
        return;

    if (!m_value) {
        m_progress->SetVisible(false);
        return;
    }

    int yPos;
    double ScaleY, ScaleX;

    if (m_vertical) {

        ScaleX = 1.0;
        ScaleY = (double)m_value/m_maxValue;
        yPos = (int)((m_height-2) * (1-ScaleY)) + 1;

    } else {

        ScaleX = (double)m_value/m_maxValue;
        ScaleY = 1.0;
        yPos = 1;

    }

    if (m_multicolor) {

        float Red1   = m_FillCol.Redf();
        float Red2   = m_ChangeCol.Redf();
        float Green1 = m_FillCol.Greenf();
        float Green2 = m_ChangeCol.Greenf();
        float Blue1  = m_FillCol.Bluef();
        float Blue2  = m_ChangeCol.Bluef();

        float percentage = (float)m_value/(float)m_maxValue;

        float Red, Green, Blue;

        if (Red1 < Red2)
            Red = (Red2-Red1)*percentage + Red1;
        else
            Red = (Red1-Red2)*percentage + Red2;

        if (Green1 < Green2)
            Green = (Green2-Green1)*percentage + Green1;
        else
            Green = (Green1-Green2)*percentage + Green2;

        if (Blue1 < Blue2)
            Blue = (Blue2-Blue1)*percentage + Blue1;
        else
            Blue = (Blue1-Blue2)*percentage + Blue2;

        KrRGBA temp;
        temp.Set (Red*255, Green*255, Blue*255);

        if (m_progress) {
            if (Engine()) {
                Engine()->Tree()->DeleteNode(m_progress);
            } else {
                delete m_progress;
            }
        }

        delete m_progressres;

        m_progressres = new KrBoxResource( "KrProgress Bar", m_width-2, m_height-2, &temp,  1, KrBoxResource::FILL );
        m_progress = new KrBox(m_progressres);

        if (m_progress && Engine()) {
            Engine()->Tree()->AddNode (this, m_progress);
        }
    }

    m_progress->SetPos (1, yPos);
    m_progress->SetScale (GlFixed(ScaleX), GlFixed(ScaleY));
    m_progress->SetVisible (true);
}