#ifndef RENDER_TARGET
#define RENDER_TARGET
#include "linmath.h"
#include <stdint.h>
#include <string.h>
typedef struct {
  int width;
  int height;
  uint8_t *pixels;
} render_target;

int bufferSize(int width, int height) {
  return width * height * 4 * sizeof(uint8_t);
}

void clearTarget(render_target *r) {
  memset(r->pixels, 0, 4 * r->height * r->width * sizeof(uint8_t));
  for (int i = 3; i < r->height * r->height * 4; i += 4) {
    r->pixels[i] = 255;
  }
}

void setColor(render_target *r, int x, int y, vec3 val) {
  int start = (r->width * y + x) * 4;
  r->pixels[start + 0] = (uint8_t)(val[0] * 255);
  r->pixels[start + 1] = (uint8_t)(val[1] * 255);
  r->pixels[start + 2] = (uint8_t)(val[2] * 255);
}
void getColor(render_target *r, int x, int y, int dst[3]) {
  int start = (r->width * y + x) * 4;
  dst[0] = r->pixels[start + 0];
  dst[1] = r->pixels[start + 1];
  dst[2] = r->pixels[start + 2];
}
void set(render_target *r, int x, int y, vec4 val) {
  int start = (r->width * y + x) * 4;
  r->pixels[start + 0] = (uint8_t)(val[0] * 255);
  r->pixels[start + 1] = (uint8_t)(val[1] * 255);
  r->pixels[start + 2] = (uint8_t)(val[2] * 255);
  r->pixels[start + 3] = (uint8_t)(val[3] * 255);
}
void get(render_target *r, int x, int y, int dst[4]) {
  int start = (r->width * y + x) * 4;
  dst[0] = r->pixels[start + 0];
  dst[1] = r->pixels[start + 1];
  dst[2] = r->pixels[start + 2];
  dst[3] = r->pixels[start + 3];
}
void setDepth(render_target *r, int x, int y, float val) {
  r->pixels[(r->width * y + x) * 4 + 3] = (uint8_t)(val * 255);
}
uint8_t getDepth(render_target *r, int x, int y) {
  return r->pixels[(r->width * y + x) * 4 + 3];
}
#endif
