diff options
| author | Akshay Nair <phenax5@gmail.com> | 2026-06-02 11:51:17 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2026-06-02 11:51:17 +0530 |
| commit | 4ccc095ed2efeda97805bdfb9473f62ed46220b7 (patch) | |
| tree | d159a226caf7619d56252c58f354189926ef51c2 | |
| parent | 6cb596cafa71b8c8d3521deb0521c0969942fb3a (diff) | |
| download | sqlite-creative-coding-4ccc095ed2efeda97805bdfb9473f62ed46220b7.tar.gz sqlite-creative-coding-4ccc095ed2efeda97805bdfb9473f62ed46220b7.zip | |
Show pixels from db
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | justfile | 8 | ||||
| -rw-r--r-- | setup.sql | 45 | ||||
| -rw-r--r-- | src/main.c | 55 |
4 files changed, 107 insertions, 2 deletions
@@ -1,3 +1,4 @@ build/ compile_flags.txt *.db +*.ignore.* @@ -1,6 +1,6 @@ set export -CC := "gcc" +CC := "cc" CFLAGS := f"-std=c11 -O2 \ -Wall -Wextra -Wshadow -Wformat=2 -fsanitize=address,undefined \ {{shell('pkg-config --cflags --libs sqlite3')}}" @@ -19,3 +19,9 @@ run: build format: find src/ -iname '*.h' -o -iname '*.c' | xargs clang-format -i +setup-db: + rm fun.db + sqlite3 fun.db < setup.sql + +image: + just run | magick display -resize 500% - diff --git a/setup.sql b/setup.sql new file mode 100644 index 0000000..164e63d --- /dev/null +++ b/setup.sql @@ -0,0 +1,45 @@ +CREATE TABLE pixels ( + x INTEGER, + y INTEGER, + r INTEGER, + g INTEGER, + b INTEGER, + UNIQUE(x, y) +); + +INSERT INTO pixels (x, y, r, g, b) VALUES (1, 1, 0, 255, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (2, 1, 0, 255, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (3, 1, 0, 255, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (4, 1, 0, 255, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (5, 1, 0, 255, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (6, 1, 0, 255, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (1, 2, 0, 255, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (2, 2, 0, 255, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (3, 2, 0, 255, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (4, 2, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (5, 2, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (6, 2, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (1, 3, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (2, 3, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (3, 3, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (4, 3, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (5, 3, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (6, 3, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (1, 4, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (2, 4, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (3, 4, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (4, 4, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (5, 4, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (6, 4, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (1, 5, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (2, 5, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (3, 5, 255, 0, 0); +INSERT INTO pixels (x, y, r, g, b) VALUES (4, 5, 0, 0, 255); +INSERT INTO pixels (x, y, r, g, b) VALUES (5, 5, 0, 0, 255); +INSERT INTO pixels (x, y, r, g, b) VALUES (6, 5, 0, 0, 255); +INSERT INTO pixels (x, y, r, g, b) VALUES (1, 6, 0, 0, 255); +INSERT INTO pixels (x, y, r, g, b) VALUES (2, 6, 0, 0, 255); +INSERT INTO pixels (x, y, r, g, b) VALUES (3, 6, 0, 0, 255); +INSERT INTO pixels (x, y, r, g, b) VALUES (4, 6, 0, 0, 255); +INSERT INTO pixels (x, y, r, g, b) VALUES (5, 6, 0, 0, 255); +INSERT INTO pixels (x, y, r, g, b) VALUES (6, 6, 0, 0, 255); @@ -1,5 +1,28 @@ #include <sqlite3.h> #include <stdio.h> +#include <string.h> + +typedef struct { + int x; + int y; + int r; + int g; + int b; +} Pixel; + +void show_pixels(Pixel *pixels, int len) { + for (int i = 0; i < len; i++) { + Pixel p = pixels[i]; + printf("%d %d %d\n", p.r, p.g, p.b); + } +} + +void show_image(Pixel *pixels, int len) { + printf("P3\n"); + printf("%d %d\n", 6, 6); + printf("%d\n", 255); + show_pixels(pixels, len); +} int main(void) { sqlite3 *db; @@ -10,8 +33,38 @@ int main(void) { return 1; } - printf("Foobar\n"); + sqlite3_stmt *stmt; + sqlite3_prepare_v2(db, + "select x, y, r, g, b from pixels ORDER BY y ASC, x ASC", + -1, &stmt, NULL); + + Pixel pixels[200 * 200]; + int index = 0; + while (sqlite3_step(stmt) != SQLITE_DONE) { + int num_cols = sqlite3_column_count(stmt); + Pixel pixel; + for (int col = 0; col < num_cols; col++) { + const char *name = sqlite3_column_name(stmt, col); + if (strcmp(name, "x") == 0) + pixel.x = sqlite3_column_int(stmt, col); + else if (strcmp(name, "y") == 0) + pixel.y = sqlite3_column_int(stmt, col); + else if (strcmp(name, "r") == 0) + pixel.r = sqlite3_column_int(stmt, col); + else if (strcmp(name, "g") == 0) + pixel.g = sqlite3_column_int(stmt, col); + else if (strcmp(name, "b") == 0) + pixel.b = sqlite3_column_int(stmt, col); + } + pixels[index++] = pixel; + } + int frames = 1; + for (int i = 0; i < frames; i++) + show_image(pixels, index); + + sqlite3_finalize(stmt); sqlite3_close(db); + return 0; } |
