Understanding the language, error messages, etc.
by r0ughneck » Thu Sep 25, 2014 7:51 pm
hi there!
i have a little problem with the horizontal (and vertical) flip of one of my sprites.^^
iknow, i know ... 12x12 is not the best size (cuz it produces 6 useless bytes) ... but it worked very well for my project.
... until now. :/
i used
drawBitmap() with a horizontal flip ... and it generates the following:
unfortunately ... i realy don't get all the crazy X and Y iterations (w,h,i,j,k,l) r0d0t used for the function in the library. >_<
did anyone of you have an idea how to fix this? :'D
-
r0ughneck
-
- Posts: 3
- Joined: Thu Sep 25, 2014 10:51 am
- Location: Germany
by rodot » Fri Sep 26, 2014 5:26 am
Yeah sorry for this cray piece of code >_<
Could you please post a Short Self Contained Compilable Example so I can try to find what's going on?
As your sprite's width is 6px, are they centered in 8 pixel sprites? If they are not centered it's normal you get an offset when you flip them.
-
rodot
- Site Admin
-
- Posts: 1290
- Joined: Mon Nov 19, 2012 11:54 pm
- Location: France
-
by r0ughneck » Fri Sep 26, 2014 7:14 pm
hey rodot,
sorry for the delay (had to work long today) ... and thanks for your time!
hm ... you're right ... i haven't centered the sprites. :/
like i said ... my sprites are 12x12pixels ... but they are stored in 16x12pixels / 3x12byte.
it's strange ... cuz the rotation works like a charm.^^
so ... did i have to center my 12x12pixel sprites in 16x16pixel sprites?
but let's have a look ... here is my simplified example :
- Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
typedef struct _area
{
const byte * sprite;
unsigned rotation :2;
unsigned flip :2;
}
AREA;
#define FLIP_V 1
#define FLIP_H 2
Gamebuino gb;
#define SPRITE_W 12
#define SPRITE_H 12
const byte houseDoor[] PROGMEM = { SPRITE_W, SPRITE_H, 0x80,0x10,0xFF,0xF0,0x80,0x10,0xBF,0xD0,0xBF,0xD0,0xBF,0xD0,0xBF,0xD0,0xBF,0xD0,0xBF,0xD0,0xBF,0xD0,0xB0,0xD0,0xE0,0x70 };
const byte houseRoofMiddle[] PROGMEM = { SPRITE_W, SPRITE_H, 0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xA9,0x50,0x50,0xA0,0xA9,0x50,0x00,0x00,0xFF,0xF0 };
const byte houseRoofSide[] PROGMEM = { SPRITE_W, SPRITE_H, 0x1F,0xF0,0x28,0x00,0x50,0x00,0xA8,0x00,0x90,0x00,0xA8,0x00,0x90,0x00,0xA2,0xA0,0x85,0x50,0xCA,0xA0,0xE0,0x00,0xFF,0xF0 };
const byte houseWall[] PROGMEM = { SPRITE_W, SPRITE_H, 0x80,0x10,0xFF,0xF0,0xEF,0x70,0xAF,0x50,0xAF,0x50,0xA0,0x50,0x9F,0x90,0x80,0x10,0x80,0x10,0x80,0x10,0xFF,0xF0,0x00,0x00 };
#define AREA_W 7
#define AREA_H 4
AREA area[] = {
{ houseRoofSide, 0, 0 }, { houseRoofMiddle, 0, 0 }, { houseRoofSide, 0, FLIP_V }, { NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL },
{ houseWall, 0, 0 }, { houseDoor, 0, 0 }, { houseWall, 0, 0 }, { NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL },
{ NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL },
{ NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL }, { NULL, NULL, NULL },
};
void setup( void )
{
gb.begin();
}
void loop( void )
{
if( gb.update() )
{
for( byte pos = 0 ; pos < AREA_H * AREA_W ; pos++ )
{
gb.display.drawBitmap( ( pos % AREA_W ) * SPRITE_W, ( pos / AREA_W ) * SPRITE_H, area[ pos ].sprite, area[ pos ].rotation, area[ pos ].flip );
}
}
}
-
r0ughneck
-
- Posts: 3
- Joined: Thu Sep 25, 2014 10:51 am
- Location: Germany
by rodot » Fri Sep 26, 2014 8:00 pm
The width have to be a multiple of 8 (I see you used a width of 12, which is not a multiple of 8, so it could create some weird behavior or draw random pixels), but the height can be any value. So you should center your 12x12 sprites in 16x12 sprites (or 16x16, but that's pure waste).
When a sprite is flipped or rotated, it around its center (not its corner), that's why you should center your sprites. Could you give it a try and tell us if it solves you problem?
-
rodot
- Site Admin
-
- Posts: 1290
- Joined: Mon Nov 19, 2012 11:54 pm
- Location: France
-
by r0ughneck » Fri Sep 26, 2014 11:02 pm
ok ... looks like a general bug! ;D
i tested it with a 16x16pixel sprite and the following code :
- Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
#define FLIP_H 2
#define FLIP_V 1
Gamebuino gb;
const byte test[] PROGMEM = {16,16,0x20,0x1E,0x50,0x12,0x50,0x1F,0xF8,0x11,0x88,0x1F,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xF8,0x1C,0x80,0x12,0x80,0x11,0x80,0x11,0xF8,0x1E,};
void setup( void )
{
gb.begin();
}
void loop( void )
{
if( gb.update() )
{
gb.display.drawBitmap( 0, 0, test, 0, FLIP_H );
}
}
and it produced this :
i fixed it myself in
Display.cpp ... just had to correct the
x and
y (or rather k and l) position with '- 1' :
- Code: Select all
if (flip & B00000001) { //horizontal flip
k = w - k - 1;
}
if (flip & B00000010) { //vertical flip
l = h - l - 1;
}
after that ... the sprite was flipped correctly :
and also my flipped 12x12 sprite works now!^^
-
r0ughneck
-
- Posts: 3
- Joined: Thu Sep 25, 2014 10:51 am
- Location: Germany
by rodot » Sat Sep 27, 2014 4:53 am
Nice, thanks for that bug fix, I'll do the change
-
rodot
- Site Admin
-
- Posts: 1290
- Joined: Mon Nov 19, 2012 11:54 pm
- Location: France
-
by Sushi » Tue Oct 07, 2014 9:54 pm
Are you possibly coding *cough cough*... pokemon?
-
Sushi
-
- Posts: 52
- Joined: Tue Oct 07, 2014 12:12 am
by desgroup » Fri Oct 10, 2014 12:10 pm
Naw, Zelda, [OldManVoice = 120/age] have played many of those game crossed the years...[/OldManVoice] and that looks like the original Link's awakening? On the Gameboy.
-
desgroup
-
- Posts: 25
- Joined: Sat Oct 04, 2014 6:58 pm
Return to Programming Questions
Who is online
Users browsing this forum: No registered users and 5 guests