From f7742a68ae57ffa9e7b965c6cc816f7977b9e467 Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Thu, 7 Sep 2017 14:31:23 -0400 Subject: [PATCH] AddPseudoFieldObject --- asm/field_map_obj.s | 79 --------------------------------------------- include/malloc.h | 4 +++ src/field_map_obj.c | 27 ++++++++++++++++ 3 files changed, 31 insertions(+), 79 deletions(-) diff --git a/asm/field_map_obj.s b/asm/field_map_obj.s index 571223ec33..7be604b277 100644 --- a/asm/field_map_obj.s +++ b/asm/field_map_obj.s @@ -5,85 +5,6 @@ .text - thumb_func_start AddPseudoFieldObject -@ void AddPseudoFieldObject(u8 graphicsId, void ( *callback)(), u16 x, u16 y, u8 subpriority) -AddPseudoFieldObject: @ 808DD68 - push {r4-r7,lr} - mov r7, r9 - mov r6, r8 - push {r6,r7} - sub sp, 0x4 - adds r4, r0, 0 - adds r5, r1, 0 - ldr r0, [sp, 0x20] - lsls r4, 16 - lsrs r4, 16 - lsls r2, 16 - lsrs r2, 16 - mov r8, r2 - lsls r3, 16 - lsrs r7, r3, 16 - lsls r0, 24 - lsrs r0, 24 - mov r9, r0 - movs r0, 0x18 - bl Alloc - adds r6, r0, 0 - adds r0, r4, 0 - adds r1, r5, 0 - adds r2, r6, 0 - mov r3, sp - bl MakeObjectTemplateFromFieldObjectGraphicsInfo - ldrh r1, [r6, 0x2] - ldr r0, =0x0000ffff - cmp r1, r0 - beq _0808DDAE - adds r0, r1, 0 - bl sub_808E894 -_0808DDAE: - mov r0, r8 - lsls r1, r0, 16 - asrs r1, 16 - lsls r2, r7, 16 - asrs r2, 16 - adds r0, r6, 0 - mov r3, r9 - bl CreateSprite - lsls r0, 24 - lsrs r5, r0, 24 - adds r0, r6, 0 - bl Free - cmp r5, 0x40 - beq _0808DDF2 - ldr r1, [sp] - cmp r1, 0 - beq _0808DDF2 - lsls r4, r5, 4 - adds r4, r5 - lsls r4, 2 - ldr r0, =gSprites - adds r4, r0 - adds r0, r4, 0 - bl SetSubspriteTables - adds r4, 0x42 - ldrb r1, [r4] - movs r0, 0x3F - ands r0, r1 - movs r1, 0x80 - orrs r0, r1 - strb r0, [r4] -_0808DDF2: - adds r0, r5, 0 - add sp, 0x4 - pop {r3,r4} - mov r8, r3 - mov r9, r4 - pop {r4-r7} - pop {r1} - bx r1 - .pool - thumb_func_end AddPseudoFieldObject - thumb_func_start sprite_new sprite_new: @ 808DE0C push {r4-r7,lr} diff --git a/include/malloc.h b/include/malloc.h index bd870ede30..b3216b05cd 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -1,6 +1,10 @@ #ifndef GUARD_MALLOC_H #define GUARD_MALLOC_H +#define malloc Alloc +#define calloc AllocZeroed +#define free Free + void *Alloc(u32 size); void *AllocZeroed(u32 size); void Free(void *pointer); diff --git a/src/field_map_obj.c b/src/field_map_obj.c index 8197217734..c434ee7db5 100644 --- a/src/field_map_obj.c +++ b/src/field_map_obj.c @@ -1,6 +1,7 @@ // Includes #include "global.h" +#include "malloc.h" #include "sprite.h" #include "rom4.h" #include "field_player_avatar.h" @@ -30,6 +31,7 @@ void sub_8096518(struct MapObject *, struct Sprite *); /*static*/ void MakeObjectTemplateFromFieldObjectTemplate(struct MapObjectTemplate *, struct SpriteTemplate *, const struct SubspriteTable **); /*static*/ void GetFieldObjectMovingCameraOffset(s16 *, s16 *); /*static*/ struct MapObjectTemplate *GetFieldObjectTemplateByLocalIdAndMap(u8, u8, u8); +/*static*/ void sub_808E894(u16); // ROM data @@ -618,3 +620,28 @@ void MakeObjectTemplateFromFieldObjectTemplate(struct MapObjectTemplate *mapObje { MakeObjectTemplateFromFieldObjectGraphicsInfoWithCallbackIndex(mapObjectTemplate->graphicsId, mapObjectTemplate->movementType, spriteTemplate, subspriteTables); } + +u8 AddPseudoFieldObject(u16 graphicsId, void (*callback)(struct Sprite *), s16 x, s16 y, u8 subpriority) +{ + struct SpriteTemplate *spriteTemplate; + const struct SubspriteTable *subspriteTables; + struct Sprite *sprite; + u8 spriteIdx; + + spriteTemplate = malloc(sizeof(struct SpriteTemplate)); + MakeObjectTemplateFromFieldObjectGraphicsInfo(graphicsId, callback, spriteTemplate, &subspriteTables); + if (spriteTemplate->paletteTag != 0xffff) + { + sub_808E894(spriteTemplate->paletteTag); + } + spriteIdx = CreateSprite(spriteTemplate, x, y, subpriority); + free(spriteTemplate); + + if (spriteIdx != MAX_SPRITES && subspriteTables != NULL) + { + sprite = &gSprites[spriteIdx]; + SetSubspriteTables(sprite, subspriteTables); + sprite->subspriteMode = 2; + } + return spriteIdx; +}