/// LSU EE 4702-1 (Fall 2023), GPU Programming
//
 /// CPU-Only Rendering
 ///
 /// THIS VERSION includes edits from the 23 August 2023 class.

//
//   Perform all rasterization steps using CPU code.

#include "frame_buffer.h"

void
set_pixel( pFrame_Buffer& fb, int x1, int y1, int x2, int y2, uint32_t color)
{
  const int wid = fb.width_get();
  const int hgt = fb.height_get();

  auto f = [&](int x) { return y1 + ( x - x1 ) / ( x2 - x1 ) * ( y2-y1); };

  for ( int x = x1;  x <= x2;  x++ )
    fb[ f(x)*wid + x ] = color;
}

void
frame_buffer_1(pFrame_Buffer& demo_frame_buffer)
{
  // Specifications of User's Monitor
  //
  const int win_width = demo_frame_buffer.width_get();
  const int win_height = demo_frame_buffer.height_get();

  //                     RRGGBB
  uint32_t color_red = 0xff0000;

  demo_frame_buffer[0] = 0xff00; // Green
  demo_frame_buffer[1] = 0xff00; // Green
  demo_frame_buffer[2] = 0xff; // Blue



  // Clear Frame Buffer.
  //
  for ( int y=0; y<win_height; y++ )
    for ( int x=0; x<win_width; x++ )
      demo_frame_buffer[ y * win_width + x ] = 0x111111; // Dark Gray

  for ( int i=0; i<win_width; i++ )
    {
      demo_frame_buffer[i] = 0xff00;
      demo_frame_buffer[i + win_width ] = color_red;
      demo_frame_buffer[i + 2*win_width] = 0x580da6; // 0xff00ff;
    }

  // Draw Diagonal Line
  //
  for ( int y=0; y<win_height; y++ )
    demo_frame_buffer[ y * win_width + y ] = color_red;


  set_pixel(demo_frame_buffer, 100,100,  500, 100, 0xff );


  // Draw a Square
  //
  for ( int x=100; x<500; x++ )
    {
      // Write frame buffer at location x=x, and y=100 (0 is bottom).
      //
      demo_frame_buffer[ 100 * win_width + x ] = 0xff;
      //
      // win_width is the width (in pixels) of the frame buffer.

      demo_frame_buffer[ 500 * win_width + x ] = 0xff00;
      demo_frame_buffer[ x * win_width + 100 ] = 0x8080ff;
      demo_frame_buffer[ x * win_width + 500 ] = 0xff00;

      demo_frame_buffer[ 99 * win_width + x ] = 0xff;
      demo_frame_buffer[ 501 * win_width + x ] = 0xff00;
      demo_frame_buffer[ x * win_width + 99 ] = 0x8080ff;
      demo_frame_buffer[ x * win_width + 501 ] = 0xff00;
    }

}




int
main(int argc, char **argv)
{
  pFrame_Buffer demo_frame_buffer(argc,argv);
  demo_frame_buffer.show(frame_buffer_1);
  return 0;
}