/// LSU EE 4702-1 (Fall 2026), GPU Programming
//
 /// CPU-Only Rendering
//
//   Perform all rasterization steps using CPU code.

#include "frame_buffer.h"

void
frame_buffer_1(pFrame_Buffer& demo_frame_buffer)
{
  // Specifications of Frame Buffer (A window covering part of screen.)
  //
  const int wd = demo_frame_buffer.width_get();   // Width in pixels.
  const int ht = demo_frame_buffer.height_get();  // Height in pixels.
  //
  // Frame buffer is wd pixels wide and has a height of ht.
  //
  //     ←---- wd ------→
  //    *----------------*  
  //    |                |  ↑
  //    |   Frame        |  ht
  //    |   Buffer       |  |
  //    |                |  ↓
  //    *----------------*
  //
  // Pixels are numbered from 0 to wd * ht-1.
  //
  // Object demo_frame_buffer can be used like a C array.
  // To write pixel 123 with 456:
  demo_frame_buffer[ 123 ] = 456;
  //
  // The frame buffer format determines where pixel 0 is.
  // In this example, pixel 0 is in the lower left.
  // (Other formats put pixel 0 in the upper left.)
  //
  // Pixel wd-1 is in the lower right.
  // Pixel wd * ht - 1 is in the upper right.


  //     ←---- wd ------→
  //    *----------------*  
  //    |E              F|  ↑    E is first pixel of top row, F is last.
  //    |                |  ht
  //    |G               |  |
  //    |D               |  |    D is first pixel of penultimate row.
  //    |AB             C|  ↓    A is first pixel of last row, B is 2nd, etc.
  //    *----------------* 
  //
  demo_frame_buffer[ 0 ] = -1;             // Write pixel A, at (0,0)
  demo_frame_buffer[ 1 ] = -1;             // Write pixel B, at (1,0)
  demo_frame_buffer[ wd-1 ] = -1;          // Write pixel C, at (wd-1,0)
  demo_frame_buffer[ wd ] = -1;            // Write pixel D, at (0,1)
  demo_frame_buffer[ 2 * wd ] = -1;        // Write pixel G, at (0,2)
  demo_frame_buffer[ (ht-1) * wd ] = -1;   // Write pixel E, at (0,ht-1)
  demo_frame_buffer[ wd-1 + (ht-1) * wd ] = -1; // Write pxl F, at (wd-1,ht-1).
  //
  int x = 1, y = 1;
  demo_frame_buffer[ x + y * wd ] = -1;    // Write pixel at (x,y)
  //
  // Why use demo_frame_buffer[ x + y * wd ] to write (x,y) when
  // demo_frame_buffer[x,y] is easier?
  //
  //   Because in this course it's important to understand how the
  //   frame buffer is organized in memory.
  //
  //   Two possible memory layouts are demo_frame_buffer[ x + y * wd ]
  //   and demo_frame_buffer[ x * ht + y ]. There's no way to tell
  //   which one is being used if demo_frame_buffer[x,y] were
  //   supported.


  //                     RRGGBB
  uint32_t color_red = 0xff0000;

  ///
  /// Examples of Drawing to the Frame Buffer
  //
  // 
// Erase The Frame Buffer. // for ( int y=0; y<ht; y++ ) for ( int x=0; x<wd; x++ ) demo_frame_buffer[ y * wd + x ] = 0x101010; // Dark gray. { // Draw Some Pixels // const int x = 47, y = 24; // Write a red pixel at coordinate x=47, y=24. demo_frame_buffer[ y * wd + x ] = color_red; // Write a blue pixel to the left of the red pixel, at x=46, y=24. demo_frame_buffer[ y * wd + x - 1 ] = 0xff; // Write a white pixel above the red pixel. (Note: 0,0 is lower left.) demo_frame_buffer[ y * wd + x + wd ] = 0xffffff; } // Draw Diagonal Line // for ( int y=0; y<ht; y++ ) demo_frame_buffer[ y * wd + y ] = color_red; // 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 * wd + x ] = 0xff; // // wd is the width (in pixels) of the frame buffer. demo_frame_buffer[ 500 * wd + x ] = 0xff00; demo_frame_buffer[ x * wd + 100 ] = 0x8080ff; demo_frame_buffer[ x * wd + 500 ] = 0xff00; demo_frame_buffer[ 99 * wd + x ] = 0xff; demo_frame_buffer[ 501 * wd + x ] = 0xff00; demo_frame_buffer[ x * wd + 99 ] = 0x8080ff; demo_frame_buffer[ x * wd + 501 ] = 0xff00; } } int main(int argc, char **argv) { pFrame_Buffer demo_frame_buffer(argc,argv); demo_frame_buffer.show(frame_buffer_1); return 0; }