(Final)NitroSpritesLib

Learn to use DS Game Maker by Example!

(Final)NitroSpritesLib

Postby Foxi4 » July 14th, 2011, 11:58 am

After alot of testing, writing and cursing under my nose, my little "baby" is finally "fairly ready".

Wanna load sprites from Nitro? Not sure how? How about using *just one function* to load them into VRAM? It's THAT simple.

Say "Hello" to your new little friends: SpriteLib.H and FAT_LoadSprite(ID,SpriteFilename,PaletteFilename);

EXAMPLE AND SOURCE:
NitroSprites Example.dsgm
(8.82 MiB) Downloaded 124 times

NitroSprites Example.nds
(2.36 MiB) Downloaded 90 times


LIBRARY:
SpriteLib.h
(562 Bytes) Downloaded 107 times

OUTDATED: Check later posts for SpriteLib2.h
Do post a commentary!

~Unicorn SoftWorks - Games are Magic
Last edited by Foxi4 on January 20th, 2012, 10:53 pm, edited 3 times in total.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1114
Joined: December 31st, 2010, 3:25 am

Re: NitroSpritesLib

Postby JanMulder » July 14th, 2011, 11:25 pm

We should really do something with all those Libs. I think we as community should keep making libraries and improve them. That would really be cool. If we don't use too much PAlib code we might be able to ditch PAlib partly.
here and yet not here
JanMulder
 
Posts: 726
Joined: December 30th, 2010, 3:08 pm

Re: NitroSpritesLib

Postby Foxi4 » July 14th, 2011, 11:48 pm

That's sort of why I kept this "as much pure C as humanly possible". I still need to add a "Free Allocated Sprite Memory" function there, for now the example frees it using a loop, but that's inefficient. (It frees the entire array of 128 possible sprites rather than just the ones currently displayed, since they're tied to their [Me]'s and you never really know if the NextInstance() will be a 1 or a friggin' 25). That's for the future though, for now I need to proof-check this several times for memory leaks.

I'll make more functions for the lib when I get the time, so that we don't have to use as many Actions to utilize the function. I still haven't figured out how DSGM assigns sprites (it uses some sort of a pointer but I can't find it anywhere in .h files), but I'll get there. For now, the loaded sprites are "pasted" over objects - that's life.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1114
Joined: December 31st, 2010, 3:25 am

Re: NitroSpritesLib

Postby JanMulder » July 15th, 2011, 12:05 am

Ok cool, but what would you think about making a whole new library with all libs that we as community have made and will make. We could just improve eachothers files and make it really strong by doing that.
here and yet not here
JanMulder
 
Posts: 726
Joined: December 30th, 2010, 3:08 pm

Re: NitroSpritesLib

Postby Foxi4 » July 15th, 2011, 12:08 am

That sounds great tbh, I'm in. All the code I'm posting here is free-to-use so I won't mind if anyone puts it in a "compiled" lib.

I'm currently working on a background loading lib using ever-so-slightly modified code from BassAceGold's example on PalibDev (props BassAceGold! He actually helped me fix some memory leakage issues) and I'll be posting it here as soon as it'll be capable of loading 4 backgrounds on each screen. That'll allow us to pretty much "scratch out" using in-game resources and leave that for pure code.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1114
Joined: December 31st, 2010, 3:25 am

Re: NitroSpritesLib

Postby JanMulder » July 15th, 2011, 12:15 am

You should be able to disable some backgroundbuffers you don't use though.
here and yet not here
JanMulder
 
Posts: 726
Joined: December 30th, 2010, 3:08 pm

Re: NitroSpritesLib

Postby Foxi4 » July 15th, 2011, 12:16 am

JanMulder wrote:You should be able to disable some backgroundbuffers you don't use though.


The background buffers "weigh" only 6 bytes or so when unused, memory is allocated to it only when it holds a background, so no space wasting happening there.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1114
Joined: December 31st, 2010, 3:25 am

Re: NitroSpritesLib

Postby JanMulder » July 15th, 2011, 12:31 am

Ok nice.
here and yet not here
JanMulder
 
Posts: 726
Joined: December 30th, 2010, 3:08 pm

Re: NitroSpritesLib

Postby Buck_7 » July 16th, 2011, 12:08 am

Foxi4 wrote:After alot of testing, writing and cursing under my nose, my little "baby" is finally "fairly ready".


Should be "My Little Pony"
Upcoming games I'm working on:
  • Minecraft2DS
  • Bow Man DS
  • Bird poo!
------------------------------------------------------------------------
COOL LOOK 3D portal port for DS! http://drunkencoders.com/2013/01/dstal-advance/
needMusic?needHelp?Ask.
User avatar
Buck_7
 
Posts: 964
Joined: February 19th, 2011, 1:44 am

Re: NitroSpritesLib

Postby Buck_7 » July 16th, 2011, 12:27 am

I want to load backgrounds as well...
How do I do that?
Upcoming games I'm working on:
  • Minecraft2DS
  • Bow Man DS
  • Bird poo!
------------------------------------------------------------------------
COOL LOOK 3D portal port for DS! http://drunkencoders.com/2013/01/dstal-advance/
needMusic?needHelp?Ask.
User avatar
Buck_7
 
Posts: 964
Joined: February 19th, 2011, 1:44 am

Re: NitroSpritesLib

Postby Foxi4 » July 16th, 2011, 1:54 am

You wait. The lib is cooking as we speak. It will be posted once there's a nice example prepared for it.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1114
Joined: December 31st, 2010, 3:25 am

Re: NitroSpritesLib

Postby YoshiInAVoid » January 8th, 2012, 6:41 pm

Thanks foxi4, this is really good work! I added some more features to your libary and improved a few things:

Code: Select all
//NitroSpritesLib by Foxi4 - Improved and added new features by YoshiInAVoid

unsigned char* SpriteTiles[128] _GFX_ALIGN;
unsigned short SpritePalette[256] _GFX_ALIGN;
FILE* File;
u32 Size;

u8 SpritesCreated = 0;

void PrepareSprite(u8 SpriteNum, char* TileName, char* PaletteName) {
   File = fopen(PaletteName, "rb");
   fread(SpritePalette, 1, sizeof(SpritePalette), File);
   fclose(File);
   File = fopen(TileName, "rb");
   fseek(File, 0 , SEEK_END);
   Size = ftell(File);
   rewind(File);
   SpriteTiles[SpriteNum] = malloc(Size);
   SpritesCreated += 1;
   fclose(File);
}

void CreateSprite(u8 Screen, u8 SpriteNum, u8 PaletteNum, u8 X, u8 Y, u8 Shape, u8 Size, char* TileName, char* PaletteName) {
   PrepareSprite(SpriteNum, TileName, PaletteName);
   PA_LoadSpritePal(Screen, SpriteNum, SpritePalette);
   int Gfx = PA_CreateGfx(Screen, SpriteTiles[SpriteNum], Shape, Size, 1);
   PA_CreateSpriteFromGfx(Screen, SpriteNum, Gfx, Shape, Size, 1, PaletteNum, X, Y);
   free(SpriteTiles[SpriteNum]);
   free(File);
}

void DeleteSprite(u8 Screen, u8 SpriteNum) {
   PA_DeleteSprite(Screen, SpriteNum);
   PA_DeleteGfx(Screen, SpriteNum);
}

void DeleteAllSprites(u8 Screen) {
   u8 FreeLoop = 0;
   for (FreeLoop = 0; FreeLoop <= SpritesCreated; FreeLoop++) {
      DeleteSprite(Screen, FreeLoop);
   }
}


In the CreateSprite thing, it looks like alot of parameters, but if you do "OBJ_SIZE_64X64" it automatically fills in Size and Shape. ColourMode is always 1, meaning 256.

Example:

CreateSprite(1, 0, 0, 96, 64, OBJ_SIZE_64X64, "Untitled_Sprite.bin", "Untitled_Pal.bin");
CreateSprite(1, 1, 0, 96, 64, OBJ_SIZE_64X64, "Untitled_Sprite.bin", "Untitled_Pal.bin");
CreateSprite(1, 2, 0, 96, 64, OBJ_SIZE_64X64, "Untitled_Sprite.bin", "Untitled_Pal.bin");
CreateSprite(1, 3, 0, 96, 64, OBJ_SIZE_64X64, "Untitled_Sprite.bin", "Untitled_Pal.bin");
CreateSprite(1, 4, 0, 96, 64, OBJ_SIZE_64X64, "Untitled_Sprite.bin", "Untitled_Pal.bin");

Will create 5 unicorn sprites numbered 0 - 4 from files "Untitled_Sprite.bin" and "Untitled_Pal.bin" which come with your example. All on the top screen positioned at (96, 64) and all loaded off palette 0. All of them 64X64.


Also, I will use this in Zombase; you have earned yourself another mention in the credits, well done!
YoshiInAVoid
 
Posts: 1960
Joined: December 30th, 2010, 11:53 am
Location: England

Re: NitroSpritesLib

Postby Foxi4 » January 13th, 2012, 6:47 pm

That does indeed look interesting, I just may use those snippets if I ever move my lazy ass and make a revision. As of today, I'm more interested in NightFox than in PALib, and NightFox Lib does all this "crap" I had to come up with "on the spot" and "out of the box", so yeah...

I was also thinking of just sprintf'ing the Palette and Spritename from one argument as to lower the ammount of Arguments in the function, but then I thought - what if the user has a pre-made palette for all sprites? And then I thought, "even so, wouldn't he want to load the sprite separatelly?". And then I thought "If he's at this level already then he probably knows all this anyways, so why am I even bothered?" lol.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1114
Joined: December 31st, 2010, 3:25 am

Re: NitroSpritesLib

Postby YoshiInAVoid » January 13th, 2012, 10:33 pm

I have updated this code further, when I get back to my ZOMBASE PC, I'll post the latest code.
YoshiInAVoid
 
Posts: 1960
Joined: December 30th, 2010, 11:53 am
Location: England

Re: NitroSpritesLib

Postby Foxi4 » January 14th, 2012, 12:16 am

Eager to see it. ^.^
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1114
Joined: December 31st, 2010, 3:25 am

Re: NitroSpritesLib

Postby Foxi4 » January 14th, 2012, 3:48 am

Another thing I'd like to add:

I don't like it how CreateSprite free's the Sprite information - you will be unable to create a new sprite from the same resource and will be limited to GFX data which shares frames, thus the sprites will not be individual. It's best to keep it and Free it only when you are sure you will not use it again, otherwise you have to go through the preparation steps again when there's no need to do so.

Moreover, CreateSprite automatically prepares a new resource, similarily to my example which only showed how to actually use the lib. It is entirely optional and actually "wrong" and wasteful. You can Prepare the sprite once and spawn countless sprites out of the same resource, each with different GFX'es corresponding to them or one GFX corresponding to them depending on whether you'll use Create or Create from GFX. It's best to use both separately, actually.

Otherwise you end up having 1 Sprite resource (that your function Free's, but still) and 1 Sprite GFX for the very same Sprite. The way I see it, there should be 1 origin resource from which GFX'es are created depending on whether or not they are actually needed... but I'll dwell into that IF I ever muster up enough will to update the lib.

Another thing I don't like about NitroSprites which needs changing is that my and your function alike load a palette for each sprite, which isn't always the right thing to do - sometimes numerous sprites use one palette. This should be made a different function entirely.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1114
Joined: December 31st, 2010, 3:25 am

Re: NitroSpritesLib

Postby memefefe » January 14th, 2012, 6:53 am

Hey I was just wondering if there's a way to load backgrounds from nitrofs? Like something pretty easy? I'm working on an rpg in my spare time and try to make it as simple and space saving as possible. I'd love any help (⌒▽⌒)
Who doesnt wanna watch a weirdo tumble?
http://kawaiiweirdo.tumblr.com
memefefe
 
Posts: 952
Joined: August 19th, 2011, 3:35 am
Location: The infinite expanses of my mind

Re: NitroSpritesLib

Postby YoshiInAVoid » January 14th, 2012, 11:15 am

NitroBackgroundsLib, look in the tutorials section.

@Foxi4, yeah, I moved the free thing into a function LoadedAllSprites() where it frees everything after you have loaded all your sprites. I also changed a few more things as well. It's no where near as good as if you had made it though, and I hope that you do eventually find the time to update this since it is very useful.
YoshiInAVoid
 
Posts: 1960
Joined: December 30th, 2010, 11:53 am
Location: England

Re: NitroSpritesLib

Postby YoshiInAVoid » January 15th, 2012, 4:29 pm

It's messing up while using an animated sprite...
YoshiInAVoid
 
Posts: 1960
Joined: December 30th, 2010, 11:53 am
Location: England

Re: NitroSpritesLib

Postby Foxi4 » January 15th, 2012, 4:33 pm

-Snippity Snip!-
Last edited by Foxi4 on January 15th, 2012, 10:35 pm, edited 1 time in total.
Current Projects:

FalloutDS: Postponed until proper libraries are created, 10%
Foxi_Lib: A completely revised version of NitroSprites[FINAL], NitroBackgrounds and NitroText[FINAL], 40%
Foxi4
 
Posts: 1114
Joined: December 31st, 2010, 3:25 am

Next

Return to Tutorials & Examples

Who is online

Users browsing this forum: No registered users and 0 guests

cron