lronaldo / cpctelera

Astonishingly fast Amstrad CPC game engine for C developers
http://lronaldo.github.io/cpctelera/
GNU Lesser General Public License v3.0
231 stars 54 forks source link

Img2CPC add option for 2D array output #52

Open Arnaud6128 opened 7 years ago

Arnaud6128 commented 7 years ago

The objective is drawing easily a part of a large sprite.

If i take example of bitmap letters, i have to generate tileset in order to select directly the letter i want to draw.

u8* const letter_tileset[27] = { 
    letter_00, letter_01, ...
};

const u8 letter_00[8 * 8] = { ... }
const u8 letter_01[8 * 8] = { ... }

But the Tilesets uses 27 * 2 bytes.

I was thinking about generating 2d sprite array.

const u8 letters[27][8*8] = { {.....}, {....}, ... };

In this way we can directly access the sprite part we need and it doesn't take more memory to realize this.

AugustoRuiz commented 7 years ago

El 25 jul. 2017 19:54, "Arnaud" notifications@github.com escribió:

The objective is drawing easily a part of a large sprite.

If a take example of bitmap letters, i have to generate tileset in order to select directly the letter i want to draw.

u8* const letter_tileset[27] = { letter_00, letter_01, ... };

const u8 letter_00[8 8] = { ... } const u8 letter_01[8 8] = { ... }

But the Tilesets uses 27 * 2 bytes.

I was thinking about generating 2d sprite array.

const u8 letters[27][8*8] = { {.....}, {....}, ... };

In this way we can directly access the sprite part we need and it doesn't take more memory to realize this.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/lronaldo/cpctelera/issues/52, or mute the thread https://github.com/notifications/unsubscribe-auth/ANE5qB4av2KqRVm7mnD6vp3k8gSvJeDSks5sRiu1gaJpZM4Oi5ha .

lronaldo commented 7 years ago

@Arnaud6128 Actually, if you do not ask for tileset generation, you will get exactly what you want, except for the C declaration of the array. All the data about the tiles/sprites will be in the same order in memory, be it in individual arrays or in a 2D array. You can add a new symbol to your code and use your array as a 2D array this way:

/// Create a label at the start of the 2D array. 
/// We need a dummy function to insert assembly code (otherwise SDCC complaints)
/// We declare the function __naked to prevent it from generating any additional code
void dummyf() __naked {
__asm
      _letters2D::
__endasm;
}
const u8 letter_00[8 * 8] = { ... } 
const u8 letter_01[8 * 8] = { ... } 

Then you can use your 2D array letters2D just by declaring it in the header file or wherever you need it:

extern const u8 letters2D[27][8*8];

Then the compiler will asume a 2D array that starts at the place or your label.

However, remember that you are trading memory space for computational time. When accessing members of your 2D array, calculations will be performed (base_location_of_the_array + size_of_dimension2 * X + Y). This will be calculated either by the compiler (when you write down letters2D[X][Y] or by yourself and your routines when you needed it.

Also remember that you can directly refer to the generated symbols (level_XX) and save the calculation time, but loose access generality (not possible to make loops and other things to refer to all letters).

Depending on your needs, you may prefer one approach or the other.

Meanwhile, I assume that @AugustoRuiz will be able to easily add the option to Img2CPC.