Hello there!
SO, I'm making an sprite editor program, and I want help on the most criticalk code: Accessing bits.

Our PSrite system (on casio FXes), sprites are coded directly by bits:

Example with a sprite of 8*8

11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111 //the 1 is to say "bit used"

and sprite of 9*9
1111111110000000// 0 are unused and must be
1111111110000000
1111111110000000
1111111110000000
1111111110000000
1111111110000000
1111111110000000
1111111110000000
1111111110000000// see the bits lost
// Here, I I want to get for example the 10th bit, I must get the first 1 of the second line

And I must be able to access to any bit of size of sprite, If you understand.

I have already found some parts of code, but nothing that important


Code:
int w,h; //size
int x,y; //position

w/8+1; //would give me the number of bytes used by line, I
(x*y)/w; // would give me the line to edit
suppose


I don't know how to end my topic, but I'll thank any help from you!
See ya!
Firstly,
Code:
typedef struct
{
  union
  {
    uint8_t data;
    struct
    {
      uint8_t b7 : 1;
      uint8_t b6 : 1;
      uint8_t b5 : 1;
      uint8_t b4 : 1;
      uint8_t b3 : 1;
      uint8_t b2 : 1;
      uint8_t b2 : 1;
      uint8_t b0 : 1;
    };
  };
} bitfield8_t;


Bits are from right to left in a byte, starting from 0. To use it, have

Code:
bitfield8_t bf;
bf.data = (byte); // your overflow


You can access bits directly by using bf.b(0 through 7).
A macro like this would work:


Code:
#define get_bit(input, bit) ((input & (1<<bit))>>bit)

Code:

unsigned char drawmap(unsigned char* spr, unsigned char w, unsigned char h)
{
   int x, y;
   unsigned char byt, pix;
   for(y = 0; y < h; y++)
   {
      for(x = 0; x < w; x++)
      {
         byt = spr[  (w*y + x)/w + x/8];
         pix = get_bit(byt, (x%8));
         if(pix)
         ML_rectangle(2*x, 2*y, 2*x+1, 2*y+1, 0, BLACK, BLACK);
      }
   }
}


With some tests, I think I almost got it...

EDIT :Test oncalc
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement