#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glut.h>
#include "lowlevel.h"

/* Variables local to this file */

int W; /* width of frame buffer */
int H; /* height of frame buffer */
int S; /* size of each pixel */

/*****************************************************************************/
/* Fake frame buffer - Use an array of 3 GLubytes per pixel as an imitiation
   frame buffer. We'll color the pixels of this array to draw a triangle */

/* pointer to faux frame buffer; a three-dimensional array */
GLubyte *fb;



void colorPixel(int x, int y, GLubyte r, GLubyte g, GLubyte b) {
  
  fb[y*W*3 + x*3 + 0] = r;
  fb[y*W*3 + x*3 + 1] = g;
  fb[y*W*3 + x*3 + 2] = b;

}



/* Allocate storage for fake frame buffer, and fill it with all black */
void initBigPixelArray(int w, int h, int s) {
  int x;
  int y;
  int k;

  W = w;
  H = h; 
  S = s;
  fb = (GLubyte*) malloc(W*H*3*sizeof(GLubyte));

  /* set initial color to grey */
  for (x=0; x<H; x++) {
    for (y=0; y<W; y++ ) {
      for (k=0; k<3; k++) {
      fb[y*W*3 + x*3 + k] =0x80;
      }
    }
  }
}

/* Draws fake framebuffer into OpenGL window  */
void drawBigPixelArray(void) {
  int x;
  int y;

  glBegin(GL_QUADS); 
  for (y=0; y<H; y++) {
    for (x=0; x<W; x++) {
      glColor3ub(fb[y*W*3 + x*3 + 0],fb[y*W*3 + x*3 + 1],fb[y*W*3 + x*3 + 2]);
      glVertex2i(x*S,y*S);
      glVertex2i(x*S,(y+1)*S);
      glVertex2i((x+1)*S,(y+1)*S);
      glVertex2i((x+1)*S,y*S);
    }
  }
  glEnd(); /* GL_QUADS */
  
}




