How to pass a 2d tiles image to a function in C++

I’d like to have a function that accept a 2d Tiles image of this kind:

const uint8_t Dragon [][802] ={
//[0] cell:0x0 
{
40,40,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
...
etc etc

In the body I’d need to call Display::drawBitmap(0,0, tiledImage[x]) with the passed tiled image. I’d like to avoid passing the sizes of the 2d array and keep it C only.
Is it possibile?

2 Likes

First thing’s first: C++, not C. :P


Secondly I’m confused by what you’re asking.

Display::drawBitmap(0,0, Dragon[frameIndex]) would draw the specified ‘frame’ at point (0,0) without issue.
The image sizes are stored as part of the image format.

You don’t need to pass the array size unless you want to explicitly check the frameIndex is valid.

Maybe I should post some more code:

//This is the function  I'd like to implement (the parameters type is probably wrong)
void DrawTiles(const uint16_t* palette, const uint8_t *image, int8_t textColor)
{
 //other stuff here
 //Draw image
 g.display.drawBitmap(xo+x,yo+y,image[index_from_somewhere]);
}

int main ()
{
 DrawTiles(City_pal,City, 7); //<< how pass City?
}

and

gfx.h
//Total colors 16
const uint16_t City_pal[] = {
0,6474,31018,1066,43654,25289,50712,65437,59594,64771,63333,24009,21819,33715,62388,65141,
};

//Sprite sheet:4x4
const uint8_t City [][802] ={
//[0] cell:0x0
{
40,40,
85,85,85,85,85,85,85,85,85,86,102,102,118,101,85,85,85,85,102,102,
85,85,85,85,85,85,85,85,85,86,102,102,118,101,85,85,85,85,102,102,
85,85,85,85,85,85,85,85,85,85,86,102,101,85,85,85,85,85,85,102,
102,102,102,102,102,221,102,102,102,102,101,85,85,102,102,102,102,102,101,85,
102,102,102,102,102,221,102,102,102,102,96,0,6,102,102,102,102,102,102,0,
17,17,17,17,17,17,17,17,17,17,17,0,0,0,0,0,0,0,0,0,
221,221,221,221,221,221,221,221,221,221,221,0,5,221,221,0,13,221,213,0,
221,221,221,221,221,221,221,221,221,221,221,0,5,221,221,0,13,221,213,0,
221,221,221,221,221,221,221,221,221,221,221,0,208,0,29,0,13,16,0,208,
221,221,221,221,221,221,221,221,221,221,221,221,221,221,80,0,0,85,221,221,
221,221,221,221,221,221,221,221,221,221,221,221,221,221,80,0,0,85,221,221,
102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,0,5,221,102,102,
85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,0,0,85,85,85,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,0,5,85,85,85,
85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,0,5,85,85,85,
17,17,17,17,17,17,17,17,17,81,17,17,17,17,17,0,1,17,17,17,
17,17,17,17,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
17,17,17,17,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
17,17,17,17,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,0,13,221,221,221,
85,85,85,85,85,85,85,85,85,213,85,85,85,85,85,0,5,85,85,85,
17,17,17,17,17,17,17,17,17,81,17,17,17,17,17,0,1,17,17,17,
17,17,17,17,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
17,17,17,17,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
17,17,17,17,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
17,17,17,17,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
17,17,17,17,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
49,17,17,17,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
49,17,17,17,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
49,17,17,17,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
17,19,17,17,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
49,17,17,49,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
49,17,17,49,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
49,19,17,17,17,17,17,17,17,209,17,17,17,17,17,0,1,17,17,17,
19,51,17,49,17,17,17,17,17,81,17,17,17,17,17,0,1,17,17,17,
19,51,17,49,17,17,17,17,17,81,17,17,17,17,17,0,1,17,17,17,
17,17,17,61,221,221,221,221,221,221,221,221,221,221,221,0,13,221,221,221,
19,51,17,0,0,0,0,0,0,80,0,0,0,0,0,0,0,0,0,0,
19,51,17,0,0,0,0,0,0,80,0,0,0,0,0,0,0,0,0,0,
},
//[1] cell:1x0
{
40,40,
103,102,85,85,85,85,85,85,85,85,85,85,85,81,148,68,68,68,68,73,
103,102,85,85,85,85,85,85,85,85,85,85,85,81,148,68,68,68,68,73,
102,85,85,85,85,85,85,85,85,85,85,85,85,85,228,68,68,68,68,78,
85,102,102,102,102,102,102,102,102,102,102,102,102,102,148,68,68,68,68,73,
0,102,102,102,102,102,102,102,102,102,102,102,102,103,148,68,68,68,68,73,
1,17,17,17,17,17,17,17,17,17,17,17,17,17,228,68,68,68,68,78,
13,221,221,221,221,221,221,221,221,221,221,221,221,209,148,68,68,68,68,73,
13,221,221,221,221,221,221,221,221,221,221,221,221,209,148,68,68,68,68,73,
13,221,221,221,221,221,221,221,221,221,221,221,213,81,228,68,68,68,68,78,
221,221,221,221,221,221,221,221,221,221,221,213,93,81,148,68,68,68,68,73,
221,221,221,221,221,221,221,221,221,221,221,213,93,209,148,68,68,68,68,73,
102,102,102,102,102,102,102,102,102,102,102,109,214,101,228,68,68,68,68,78,
85,85,85,85,85,85,85,85,85,85,85,85,85,85,68,68,68,68,68,68,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,148,68,68,68,68,73,
85,85,85,85,85,85,85,85,85,85,85,85,80,0,228,68,68,68,68,78,
85,85,85,85,85,85,85,85,85,85,85,85,80,0,228,68,68,68,68,78,
17,21,81,17,17,17,17,17,17,17,17,21,80,0,148,68,68,68,66,36,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,228,68,68,68,66,36,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,148,68,68,68,68,73,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,148,68,68,68,68,73,
221,221,221,221,221,221,221,221,221,221,221,221,208,0,228,68,68,34,34,36,
85,85,213,85,85,85,85,85,85,85,85,85,208,0,68,68,68,34,34,36,
17,21,81,17,17,17,17,17,17,17,17,21,80,0,148,68,68,34,34,36,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,228,68,68,68,68,78,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,228,68,68,68,68,78,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,148,68,68,68,68,73,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,228,68,68,68,68,78,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,228,68,68,68,68,78,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,148,68,68,68,68,73,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,68,68,68,68,68,78,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,68,68,68,68,68,78,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,68,68,68,68,68,73,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,228,68,68,68,68,78,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,228,68,68,68,68,78,
17,21,209,17,17,17,17,17,17,17,17,21,208,0,66,34,36,68,68,73,
17,21,81,17,17,17,17,17,17,17,17,21,80,0,66,34,36,68,68,78,
17,21,81,17,17,17,17,17,17,17,17,21,80,0,66,34,36,68,68,78,
221,221,221,221,221,221,221,221,221,221,221,221,208,0,148,68,68,68,68,73,
0,5,80,0,0,0,0,0,0,0,0,5,80,0,226,34,34,34,36,68,
0,0,80,0,0,0,0,0,0,0,0,5,80,0,226,34,34,34,36,68,
},
//[2] cell:2x0
{
40,40,
17,21,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,
17,21,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,
17,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,
102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,
119,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,
17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,
21,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,
17,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,
17,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,221,
17,221,221,221,221,221,221,213,93,221,221,221,221,221,221,221,221,221,221,221,
17,221,221,221,221,221,221,213,93,221,221,221,221,221,221,221,221,221,221,221,
85,102,102,102,102,102,102,93,214,102,102,102,102,102,102,102,102,102,102,102,
5,85,85,85,85,85,85,5,85,85,85,85,85,85,85,85,85,85,85,85,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,17,17,17,17,17,17,17,17,17,17,17,18,0,0,0,0,0,0,0,
0,17,17,17,17,17,17,17,17,17,17,17,18,0,0,0,0,0,0,0,
0,1,17,17,17,17,34,34,34,34,34,34,36,1,17,17,17,16,0,17,
0,1,17,17,17,17,34,34,34,34,34,34,36,1,17,17,17,16,0,17,
0,17,17,17,17,17,34,34,34,34,34,34,36,0,0,0,0,80,0,85,
0,17,17,17,17,17,34,34,34,34,34,34,36,0,0,0,0,80,0,85,
0,0,0,0,17,17,34,34,34,34,34,34,36,1,17,17,17,16,0,17,
0,0,0,0,17,17,34,34,34,34,34,34,36,1,17,17,17,16,0,17,
0,0,0,0,17,17,34,34,34,34,34,34,36,1,17,17,17,16,0,17,
0,17,17,17,17,17,34,34,34,34,34,34,36,1,17,17,17,16,0,17,
0,17,17,17,17,17,34,34,34,34,34,34,36,1,17,17,17,16,0,17,

etc etc
void DrawTiles(const uint16_t * palette, const uint8_t * const * images, int8_t textColor)
{
 // Other stuff here
 // Draw image
 Display::drawBitmap(xo + x, yo + y, images[indexFromSomewhere]);
}

(Note that I renamed image to images.)

The type of City is const uint8_t City[NumberOfFrames][BytesPerFrame].

Arrays ‘decay’ (technical term) to pointers.
So the type of City decays to const uint8_t * const * const City,
which is to say: a const pointer to a const pointer to a const uint8_t.

So images is a const uint8_t * const * (a pointer to a const pointer to a const uint8_t).
Using the indexing operator fetches an item from it at a specific index (corresponding to the first ‘rank’ or ‘dimension’ (technical terms) of the array),
so the result of images[indexFromSomewhere] is of type const uint8_t * const (i.e. an image - a pointer to image data).

Just remember that the number of levels of indirection (i.e. the number of * in the type) should match the number of ranks on the array.


If you do genuinely need the width and height of the array (e.g. for bounds checking) then that’s another matter.

1 Like

First of all thanks a lot @Pharap !
Then I’ll need some weeks to digest this “monstrosity” :sweat_smile: ,
and as last but not last. How should I call DrawTiles?
DrawTiles(City_pal,City, 7) still not working:

error: cannot convert 'const uint8_t (*)[802] {aka const unsigned char (*)[802]}' to 'const uint8_t* const* {aka const unsigned char* const*}' for argument '2' to 'void DrawTiles(const uint16_t*, const uint8_t* const*, int8_t)'|

missing some cast?

If in doubt, play around with cdecl.org.
It won’t recognise uint8_t & co, but it will handle the fundamental types (char, int etc):
https://cdecl.org/?q=const+char++const++images%3B

I just double checked and aparently the decay rule isn’t applied recursively,
so a 2D array will only decay once.

(I didn’t even know that I didn’t know that. Weird edge case rules never stop crawling out of the woodwork.)

In which case, time to do it the template way.
I’m often reluctant to post template code because I know templates scare people
(probably more than pointers).

Just pretend you know how it works:

template< std::size_t numberOfFrames, std::size_t imageSize >
void DrawTiles(const uint16_t * palette, const uint8_t (&images)[numberOfFrames][imageSize], int8_t textColor)
{
 // Other stuff here
 // Draw image
 
 // Added extra bounds checking
 // Yay safety!
 if(indexFromSomewhere < numberOfFrames)
 {
  Display::drawBitmap(xo + x, yo + y, images[indexFromSomewhere]);
 }
}

Bookmarked!

Oh my god!

That’s scary me more and more…

Sorry again, but how it works? Should I call it passing array size? `DrawTiles(City_pal,City[16][802], 7)`??? I missing something obviusly

It works! It works! (I just mixed some method firm)
Kudo @Pharap
going offline now.

pok15.bin (124.1 KB)

2 Likes

Keep up the hard work! Maybe there’s a Game Publisher badge waiting for you!

(… I know, I know … sry ;-))

1 Like

grrrr :angry:

1 Like

OMG! That is a nice WIP game.

Looking very nice!

Playing right now

Maybe we should have a “Pharap taught me some C++ stuff” badge to encourage more of these kinds of useful questions. :P

Seriously though, if anything’s been proven today,
it’s that there are so many crazy C++ rules that nobody ever stops learning C++.

1 Like

Exactly, another WIP!! I have something wrong I suppose.
So it works on hardware?

Embitz build. Does it means I have half memory free?

Why not “Ask to Expert” ?

Just solved the first puzzle YAY!!!

Actually, I am working on a new gamedisk release and this is so kickass it will definitely be on it.

Would you allow me to add some music?

EDIT: all the pretty graphics by you? Because if yes, you are truly gifted in many ways

You have about 90-100k still to go (leaving space for loader)

1 Like

Sure! Code will come, I just wrote something yesterday after downloading new lib and discover mode15. Love it.
What about ask friends here post their preferite 16 color images 160x160 (not NSFW) and open a poll?
I’d like to have a persistence mode (EEPROM saving on power off?), high score and undo maybe.

I was trying to keep it C only for learning purpose… but template seems necessary.

The graphics looks super sharp on the display. Its just beautiful.

“Ask an Expert” or “Ask the Expert”? :P

Seriously though, we could just have a couple of tiers of question asking badges:
“Asked a question”, “Asked a notable question”, “Asked a famous question”

It started as a joke, now I’m not even sure if I’m joking anymore.
I think I’ll just leave that idea haning and come back to it if we need badge suggestions in the future.


Semi-related: earlier today I discovered a discourse plugin that allows threads in designated categories to be marked as ‘questions’ and then the owner of the thread can opt to select a specific post as an ‘answer’ (kind of like Stackoverflow).

It’s an interesting idea.


Strictly using C means no PokittoLib.

Using C++ that looks like C isn’t using C.
It doesn’t work like that.

1 Like

Not for sure. I’ve 0% capabilities on that field. Maybe some music attitude but graphics…

@adekto is good at graphics.

I’m sometimes good at graphics.
(I can do ‘art’ - in single quotes for legal reasons :P.)


Sidenote:
I feel like we’ve ended up going off on a tangent,
but migrating it would be awkard because the game is on the post accepting the answer.

1 Like