Add rectangle fill
This commit is contained in:
parent
b95c9acca1
commit
aaedea1dec
4 changed files with 89 additions and 4 deletions
6
Makefile
6
Makefile
|
@ -1,10 +1,10 @@
|
||||||
CFLAGS+=-Wall -Wextra -std=c99 -pedantic
|
CFLAGS+=-Wall -Wextra -std=c99 -pedantic -g
|
||||||
LIBS=
|
LIBS=
|
||||||
PREFIX?=/usr/local
|
PREFIX?=/usr/local
|
||||||
INSTALL?=install
|
INSTALL?=install
|
||||||
|
|
||||||
main: main.c
|
main: painter.c main.c
|
||||||
$(CC) $(CFLAGS) -o main main.c $(LIBS)
|
$(CC) $(CFLAGS) -o main painter.c main.c $(LIBS)
|
||||||
|
|
||||||
|
|
||||||
.PHONY: all clean
|
.PHONY: all clean
|
||||||
|
|
7
main.c
7
main.c
|
@ -1,5 +1,10 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "painter.h"
|
||||||
|
|
||||||
int main() {
|
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);
|
||||||
}
|
}
|
||||||
|
|
53
painter.c
Normal file
53
painter.c
Normal file
|
@ -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);
|
||||||
|
}
|
||||||
|
|
27
painter.h
Normal file
27
painter.h
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#ifndef _PAINTER_H
|
||||||
|
#define _PAINTER_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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
|
Loading…
Reference in a new issue