diff --git a/Makefile b/Makefile index 6588ab5..34043ad 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ -CFLAGS+=-Wall -Wextra -std=c99 -pedantic +CFLAGS+=-Wall -Wextra -std=c99 -pedantic -g LIBS= PREFIX?=/usr/local INSTALL?=install -main: main.c - $(CC) $(CFLAGS) -o main main.c $(LIBS) +main: painter.c main.c + $(CC) $(CFLAGS) -o main painter.c main.c $(LIBS) .PHONY: all clean diff --git a/main.c b/main.c index b6f7f3e..3af67ff 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,10 @@ #include +#include "painter.h" int main() { - printf("Hello, world!!\n"); + lp_painter *painter = lp_painter_create(512, 512); + lp_painter_fill_rect(painter, lp_rgb(255, 0, 0), 0, 0, 512, 512); + lp_painter_fill_rect(painter, lp_rgb(0, 0, 255), 0, 25, 103, 40); + lp_painter_save_ppm(painter, "./surface.ppm"); + lp_painter_free(painter); } diff --git a/painter.c b/painter.c new file mode 100644 index 0000000..870a0d1 --- /dev/null +++ b/painter.c @@ -0,0 +1,53 @@ +#include "painter.h" + +#include "stdio.h" + +lp_pixel lp_rgb(uint8_t r, uint8_t g, uint8_t b) { + return (lp_pixel){r, g, b}; +} + +lp_painter *lp_painter_create(size_t width, size_t height) { + lp_painter *painter = malloc(sizeof(lp_painter)); + lp_pixel *buf = malloc(width * height * sizeof(lp_pixel)); + + painter->buf = buf; + painter->width = width; + painter->height = height; + + return painter; +} + +void lp_painter_free(lp_painter *painter) { + if (painter) { + if (painter->buf) { + free(painter->buf); + } + free(painter); + } +} + +void lp_painter_fill_rect(lp_painter *painter, lp_pixel fill_color, size_t pos_x, size_t pos_y, size_t width, size_t height) { + for (size_t y = pos_y; y < pos_y + height; y++) { + for (size_t x = pos_x; x < pos_x + width; x++) { + size_t index = y * painter->width + x; + painter->buf[index] = fill_color; + } + } +} + +void lp_painter_save_ppm(lp_painter *painter, const char *filename) { + FILE *fp = fopen(filename, "w"); + if (fp == NULL) { + fprintf(stderr, "Failed to open file"); + return; + } + fprintf(fp, "P6\n%zu %zu 255\n", painter->width, painter->height); + for (size_t i = 0; i < painter->width * painter->height; i++) { + fwrite(&painter->buf[i].r, sizeof(uint8_t), 1, fp); + fwrite(&painter->buf[i].g, sizeof(uint8_t), 1, fp); + fwrite(&painter->buf[i].b, sizeof(uint8_t), 1, fp); + } + fwrite(painter->buf, sizeof(lp_pixel), painter->width * painter->height, fp); + fclose(fp); +} + diff --git a/painter.h b/painter.h new file mode 100644 index 0000000..bfb3651 --- /dev/null +++ b/painter.h @@ -0,0 +1,27 @@ +#ifndef _PAINTER_H +#define _PAINTER_H + +#include +#include + +typedef struct lp_pixel { + uint8_t r; + uint8_t g; + uint8_t b; +} lp_pixel; + +lp_pixel lp_rgb(uint8_t r, uint8_t g, uint8_t b); + +typedef struct lp_painter { + lp_pixel *buf; + size_t width; + size_t height; +} lp_painter; + +lp_painter *lp_painter_create(); +void lp_painter_free(lp_painter *painter); +void lp_painter_fill_rect(lp_painter *painter, lp_pixel fill_color, size_t pos_x, size_t pos_y, size_t width, size_t height); +void lp_painter_save_ppm(lp_painter *painter, const char *filename); + + +#endif \ No newline at end of file