summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2026-03-14 22:37:09 +0200
committerSyndamia <kamen@syndamia.com>2026-03-14 22:37:55 +0200
commit643aa74994fb8bca341958433c1028f495931d3e (patch)
treea919ef65d1609d09efec1846a745aed45cf17f7b
downloadppm_graphics-643aa74994fb8bca341958433c1028f495931d3e.tar
ppm_graphics-643aa74994fb8bca341958433c1028f495931d3e.tar.gz
ppm_graphics-643aa74994fb8bca341958433c1028f495931d3e.zip
feat!: Moving checker pattern
-rw-r--r--graphics.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/graphics.c b/graphics.c
new file mode 100644
index 0000000..df10685
--- /dev/null
+++ b/graphics.c
@@ -0,0 +1,94 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+inline int
+OK(int x) {
+ return x == 0;
+}
+
+////RGBImage////
+
+typedef uint8_t byte;
+typedef uint16_t byte2;
+typedef uint32_t byte4;
+
+typedef struct _RGB {
+ byte r;
+ byte g;
+ byte b;
+} RGB;
+
+void
+ARGB_set(RGB* rgb, byte4 color) {
+ rgb->r = (0xFF0000 & color) >> 16;
+ rgb->g = (0x00FF00 & color) >> 8;
+ rgb->b = (0x0000FF & color);
+}
+
+typedef struct _RGBImage {
+ int width;
+ int height;
+ RGB* img;
+} RGBImage;
+
+RGBImage
+RGBImage_new(int width, int height) {
+ return (RGBImage){
+ .width = width,
+ .height = height,
+ .img = malloc(sizeof(RGB) * width * height)
+ };
+}
+
+void
+RGBImage_free(RGBImage img) {
+ free(img.img);
+}
+
+void
+RGBImage_delete(RGBImage* img) {
+ RGBImage_free(*img);
+ img->width = img->height = 0;
+ img->img = NULL;
+}
+
+RGB*
+RGBImage_at(RGBImage img, int row, int col) {
+ return img.img + (row % img.height) * img.width + col % img.width;
+}
+
+int
+ppm6_write(FILE* f, RGBImage img) {
+ fprintf(f, "P6\n%d %d\n255\n", img.width, img.height);
+ fwrite(img.img, sizeof(*img.img), img.width * img.height, f);
+ return fflush(f);
+}
+
+////main////
+
+int
+main() {
+ RGBImage frame = RGBImage_new(512, 512);
+
+ for (int frameIndex = 0; frameIndex < 120; ++frameIndex) {
+
+ for (int r = 0; r < frame.width; ++r)
+ for (int c = 0; c < frame.height; ++c) {
+ RGB* pixel = RGBImage_at(frame, r, c);
+
+ if (((r + frameIndex) / 32 + (c + frameIndex) / 32) % 2 == 0) {
+ ARGB_set(pixel, 0x000000);
+ }
+ else {
+ ARGB_set(pixel, 0xFF00FF);
+ }
+ }
+
+ ppm6_write(stdout, frame);
+ }
+
+ RGBImage_delete(&frame);
+
+ return 0;
+}