KrCollisionMap* KrSpriteResource::GetCollisionMap( KrImage* state/*, int window*/ ) { GLASSERT( state && state->ToSprite() ); KrSprite* sprite = state->ToSprite(); GLASSERT( sprite->SpriteResource() == this ); if ( state->XScale( /*window*/ ) != 1 || state->YScale( /*window*/ ) != 1 ) { if ( !IsScaleCached( state->CompositeXForm( /*window*/ ).xScale, state->CompositeXForm( /*window*/ ).yScale ) ) { // For sprites, only cached scales can be used! /*#ifdef DEBUG GLOUTPUT( "ERROR: Collision only allowed with cahed sprites!\n" ); GLOUTPUT( "SpriteResource '%s' for Action '%s' Frame %d scale %f, %f\n", ResourceName().c_str(), sprite->GetAction()->Name().c_str(), sprite->Frame(), state->CompositeXForm( window ).xScale.ToDouble(), state->CompositeXForm( window ).xScale.ToDouble() ); #endif*/ GLASSERT( 0 ); return 0; } } KrAction* action = sprite->GetAction(); return action->GetCollisionMap( state->CompositeXForm( /*window*/ ).xScale, state->CompositeXForm( /*window*/ ).yScale, sprite->Frame() ); }
KrCanvasResource* KrSpriteResource::CreateCanvasResource( U32 id, int frame, int* hotx, int* hoty ) { KrAction* action = 0; actionIdMap->Find( id, &action ); if ( action ) return action->CreateCanvasResource( frame, hotx, hoty ); return 0; }
KrCanvasResource* KrSpriteResource::CreateCanvasResource( const gedString& actionName, int frame, int* hotx, int* hoty ) { KrAction* action = 0; actionMap->Find( actionName, &action ); if ( action ) return action->CreateCanvasResource( frame, hotx, hoty ); return 0; }
KrAction* SharedStateData::CreateAnimationAction() { KrAction* action = 0; // If there is a current action, write the frame actions // and encode them. if ( currentAction ) { int nFrames = currentAction->children.Count(); int i = 0; if ( nFrames > 0 ) { action = new KrAction( "no_action_name" ); GlSListIterator< EdWidget* > it( currentAction->children ); for( i = 0, it.Begin(); !it.Done(); ++i, it.Next() ) { EdWidgetFrame* frame = it.Current()->ToFrame(); GLASSERT( frame ); KrDom::Frame frameData = frame->GetFrameData(); KrPaintInfo info( canvasResource->Pixels(), canvasResource->Width(), canvasResource->Height() ); action->AddFrame(); GLASSERT( action->NumFrames() == i+1 ); action->GetFrame( i )->Create( &info, frameData.x, frameData.y, frameData.width, frameData.height, frameData.hotspotX, frameData.hotspotY, frameData.deltaX, frameData.deltaY ); } } } return action; }
void KrSpriteResource::RemoveAction( const gedString& actionName ) //maks { Uint32 id; for(int i = 0; i < actionArr.Count(); i++) { KrAction *action = actionArr.Item(i); if(action->Name() == actionName) { id = action->Id(); actionArr.Remove(i); delete action; actionMap->Remove(actionName); actionIdMap->Remove(id); break; } } }
void KrSpriteResource::Draw( KrPaintInfo* paintInfo, const gedString& actionname, int frame, const KrMatrix2& xForm, const KrColorTransform& cForm, const KrRect& clip, int quality, int openGLZ ) { KrAction* act = 0; actionMap->Find( actionname, &act ); if ( act ) { act->Draw( paintInfo, frame, xForm, cForm, clip, openGLZ ); } /*#ifdef DEBUG else { GLOUTPUT( "WARING KrSpriteResource::Draw failed to find action %s\n", actionname.c_str() ); } #endif*/ }
KrFontResource::KrFontResource( const std::string& name, KrPaintInfo* info, int startingGlyph, int addSpaceGlyph, int type, int length ) : KrSpriteResource( name ) { startIndex = startingGlyph; fontType = type; space = addSpaceGlyph; KrAction* action = new KrAction( "NONE" ); if ( fontType == FIXED ) { int width = info->width / length; int height = info->height; for( int i = 0; i < length; i++ ) { action->AddFrame(); KrRle* rle = action->GetFrame( i ); rle->Create( info, i * width, 0, width, height, i * width, 0, // the hotspots are absolute coordinates! width, height ); } } else { KrPainter painter( info ); int height = info->height - 1; int x = 0; int transparent = 0; while ( x < info->width ) { x += painter.CalcNotTransparentRun( x, info->width - 1, 0 ); if ( x < info->width ) transparent = painter.CalcTransparentRun( x, info->width - 1, 0 ); else transparent = 0; if ( x < info->width && transparent > 0 ) { action->AddFrame(); KrRle* rle = action->GetFrame( action->NumFrames() - 1 ); rle->Create( info, x, 1, transparent, height, x, 1, // the hotspots are absolute coordinates! transparent, height ); } x += transparent; } } AddAction( action ); CalcSpaceWidth(); }
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 ); } }
bool KrEncoder::EncodeSprite( SDL_Surface* surface, const AllInfo& allInfo, KrConsole* console ) { // gedString spriteName, actionName; // TiXmlElement* action; // TiXmlElement* frame; // // spriteName = "no_sprite_name"; // if ( sprite->Attribute( "name" ) ) // { // spriteName = *(sprite->Attribute( "name" )); // } // Create or locate the sprite. if ( !vault.GetSpriteResource( allInfo.name ) ) { vault.AddResource( new KrSpriteResource( allInfo.name ) ); } KrSpriteResource* spriteResource = vault.GetSpriteResource( allInfo.name ); GLASSERT( spriteResource ); GlDynArray< int > rotation; if ( allInfo.rotation.Count() ) rotation = allInfo.rotation; else rotation.PushBack( 0 ); for( int i=0; i<(int) rotation.Count(); ++i ) { // Get or create the action. // If using rotation, append to the name. gedString action = allInfo.action; if ( rotation.Count() > 1 ) { char buf[16]; sprintf( buf, ".ROT%03d", rotation[i] ); action += buf; } if ( !spriteResource->GetAction( action ) ) { spriteResource->AddAction( new KrAction( action ) ); } KrAction* actionRes = spriteResource->GetAction( action ); GLASSERT( actionRes ); int index = actionRes->NumFrames(); actionRes->AddFrame(); KrPaintInfo info( surface ); KrRle* rle = actionRes->GetFrame( index ); if ( allInfo.isoTargetWidth > 0 ) { CreateIsoTile( &info, console, allInfo.x, allInfo.y, allInfo.width, allInfo.height, rle, allInfo.isoTargetWidth, rotation[i] ); } else { rle->Create( &info, allInfo.x, allInfo.y, allInfo.width, allInfo.height, allInfo.hotx, allInfo.hoty, allInfo.deltax, allInfo.deltay ); } PrintSprite( console, allInfo.name, action, index, actionRes->GetFrame( index ) ); } return true; }