#ifndef IMG_H
#define IMG_H

#include "aliases.h"
#include "mem_arena.h"
#include <stdbool.h>

#define BUF_TYPE(TYPE, NAME)                                                   \
  typedef struct NAME {                                                        \
    u64 width;                                                                 \
    u64 height;                                                                \
    TYPE *buf;                                                                 \
  } NAME

typedef struct colour Colour;
struct colour {
  u8 r;
  u8 g;
  u8 b;
  u8 a;
};

BUF_TYPE(void, Buffer);
BUF_TYPE(Colour, Image);
BUF_TYPE(f32, Depth);

#define init_buffer(ARENA, BUF)                                                \
  _init_buffer(ARENA, (Buffer *)BUF, sizeof(*((BUF)->buf)))
#define get_pixel(TYPE, BUF, X, Y)                                             \
  (*((TYPE *)(_get_pixel((Buffer *)BUF, X, Y, sizeof(*((BUF)->buf))))))
#define set_pixel(BUF, X, Y, VAL_PTR)                                          \
  _set_pixel((Buffer *)BUF, X, Y, VAL_PTR, sizeof(*((BUF)->buf)))
#define clear_buffer(BUF, VAL_PTR)                                             \
  _clear_buffer((Buffer *)BUF, VAL_PTR, sizeof(*((BUF)->buf)))

bool _init_buffer(Arena *arena, Buffer *buffer, u64 base_size);
u8 *_get_pixel(Buffer *buffer, u64 x, u64 y, u64 base_size);
void _set_pixel(Buffer *buffer, u64 x, u64 y, void *value, u64 base_size);
void _clear_buffer(Buffer *buffer, void *value, u64 base_size);
void draw_line(Image *img, u64 x0, u64 y0, u64 x1, u64 y1, Colour colour);
void save_image(const Image *img, const char *filename);

#endif // IMG_H
