Advertisement
Guest User

Untitled

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