palib/examples/Sprites/Others/CreateGfx/source/main.c
2025-01-06 22:43:23 +00:00

69 lines
2.2 KiB
C

/* This simple demo will show you how to effectively use Sprite Gfxs
In a few word, when you load a sprite, it copies the sprite information (size, position, etc...)
and the sprite's image, the gfx... If you create 10 times the same sprite, you'll have 10 times
the same image in VRAM, which is kind of a shame... So let's see how to have a single copy of that image,
and thus save VRAM and loading time...
*/
// Includes, only one sprite
#include <PA9.h>
// PAGfx Include
#include "all_gfx.h"
// Main function
int main(void){
// PAlib init
PA_Init();
PA_LoadDefaultText(1, 0);
// Load the sprite palette, for both screens
PA_LoadSpritePal(0, // Screen
0, // Palette number
(void*)sprite0_Pal); // Palette name
PA_LoadSpritePal(1, // Screen
0, // Palette number
(void*)sprite0_Pal); // Palette name
// Now, let's copy the image of the sprite to VRAM :
// First screen
u16 gfx0 = PA_CreateGfx(0, // screen
(void*)vaisseau_Sprite, // Image
OBJ_SIZE_32X32, // Size
1); // 256 color sprite
// Same thing for the top screen :
u16 gfx1 = PA_CreateGfx(1, (void*)vaisseau_Sprite, OBJ_SIZE_32X32, 1);
// The gfx0 and gfx1 variables stock the number of the gfx, this will allow us to create sprites later on
PA_OutputSimpleText(1, 0, 10, "Press A to create a sprite on the bottom screen, B on the top screen");
u8 nsprites0 = 0; // Number of sprites on the bottom screen
u8 nsprites1 = 0; // Top screen
// Infinite loop
while(1)
{
// If A pressed
if (Pad.Newpress.A) {
PA_CreateSpriteFromGfx(0, // screen
nsprites0, // Next sprite to load...
gfx0, // image to use in memory, no image copying !
OBJ_SIZE_32X32, 1, 0, PA_Rand()%256, PA_Rand()%192); // The rest is like normal sprites
++nsprites0; // Next time, load the next sprite number
}
// Same thing with B
if (Pad.Newpress.B) {
PA_CreateSpriteFromGfx(1, // screen
nsprites1, // Next sprite to load...
gfx1, // image to use in memory, no image copying !
OBJ_SIZE_32X32, 1, 0, PA_Rand()%256, PA_Rand()%192); // The rest is like normal sprites
++nsprites1; // Next time, load the next sprite number
}
PA_WaitForVBL();
}
return 0;
}