Skip to content

Add engine function for rendering a specific part of a sprite #40

Description

@ArcadeMakerSources

Sometimes we want want to draw a specific area of an image - for example, imagine that we have a portal wall in the game, which when moving into it it's launching you to another place in the room, and you want to have the effect of moving only the area of the object that has alredy collided with the portal, so you render half of it in the portal and half in the other exit (hope you get it 😅).
Anyway, we want to implement drawPartialSprite(spriteID, imageIndex, x, y partX, partY, partWidth, partHeight, [angle], [alpha]). the function declaration should be added to ArcadeMaker.Core/IGame.Funcs.cs:

/// <summary>Draws a specific part of a sprite</summary>
[EngineFunc(8)]
[Param("spriteID", ParamType.Number, "The ID of the sprite to draw. Can be accessed by referencing 'Sprites.spr_name'.")]
// rest of the parameters...
Exp.Void DrawPartialSprite(Exp.Instance? _, IValue?[] args);

And the implementation needs to happen in Engines/MonoGame/Core/ArcadeMakerMonoGame.cs.

ArcadeMaker uses texture atlas to render sprites, and the monogame backend contains a class called TextureRegion (Engines/MonoGame/Core/Graphics/TextureRegion.cs) that represent a region of a sprite image in the texture atlas. This class contains some Draw(...) methods, but currently none of them lets u specify a specific area within the texture to only draw it (the MonoGame's SpriteBatch.Draw(...) does). We need to add a method that accepts that specification to this TextureRegion class and then pass the arguments from the engine function to there, so it will look something like this:

public Exp.Void DrawPartialSprite(Exp.Instance? _, IValue?[] args)
{
   var sprite = Sprites.GetById(args[0].ThrowIfNull().Number);
   int imageIndex = args[1].ThrowIfNull().Number;
   // rest of the args...
   MainTextureAtlas.GetRegion(sprite, imageIndex).Draw(new Vector2(x, y), new Rectangle(partX, partY, partWidth, partHeight), angle, alpha); // this Draw(...) method needs to be added to TextureRegion, it can easily be implemented by using the "sourceRect" parameter of the MonoGame's Draw(...)
   return Exp.Void.Return;

Finally, a futile implementation should be declared at ArcadeMaker.IDE/Debugging/FutileGame.cs. just return null and it's enough.

Please feel free to ask any question you have :)
Thanks a lot!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions