Advertisement
Guest User

Untitled

a guest
Apr 6th, 2018
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1.  
  2. //DXYN - Draws a sprite at coordinate (VX, VY) that has a width of 8 pixels and a height of N pixels.
  3. //Each row of 8 pixels is read as bit-coded starting from memory location I; I value doesn’t change after the execution of this instruction.
  4. // As described above, VF is set to 1 if any screen pixels are flipped from set to unset when the sprite is drawn, and to 0 if that doesn’t happen
  5. void ChipEight::DrawSpriteatVXandVYandN(uint16_t opcode){
  6.     //Get the x, y coordinates
  7.     int rX = this->registers[(opcode & 0x0F00) >> 8];
  8.     int rY = this->registers[(opcode & 0x00F0) >> 4];
  9.     int height = opcode & 0x000F;
  10.  
  11.     //We don't know if we've flipped any bits yet, so set to 0 for now.
  12.     this->registers[0xF] = 0;
  13.  
  14.     //Loop for how many rows we want the sprite to be.
  15.     for (int row = 0; row < height; row++)
  16.     {
  17.         //The pixel is just one ROW of the SPRITE which is N rows
  18.         uint8_t pixel = this->memory[iRegister + row]; //Get each row of the sprite by incrementing I
  19.  
  20.         for (int col = 0; col < 8; col++) //Each sprite is 8 units wide
  21.         {
  22.             //Get the bit we want to check if it's set:
  23.             int bit = pixel & (0b10000000 >> col);
  24.  
  25.             //Check if the bit is set
  26.             if(bit != 0)
  27.             {
  28.                 //Check for wrap on X
  29.                 if (rX + col > DISPLAY_WIDTH)
  30.                 {
  31.                     continue;
  32.                 }
  33.  
  34.                 //We want to draw, but we must check if the bit is already set here:
  35.                 if(this->display[rX + col][rY + row] == 1)
  36.                 {
  37.                     //It is set, so we need to set the 0xF register because we will erase.
  38.                     this->registers[0xF] = 1;
  39.                 }
  40.  
  41.                 //XOR the position with 1, which will flip the bit.
  42.                 this->display[rX + col][rY + row] ^= 1;
  43.            }
  44.         }  
  45.     }
  46.     IncrementProgramCounter();
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement