summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/.gitignore4
-rw-r--r--examples/Makefile7
-rw-r--r--examples/logging.c32
-rw-r--r--examples/ppm.c29
4 files changed, 55 insertions, 17 deletions
diff --git a/examples/.gitignore b/examples/.gitignore
index dcb0ae9..b4d84dd 100644
--- a/examples/.gitignore
+++ b/examples/.gitignore
@@ -1,3 +1,4 @@
+# Build artifacts
stringv
stringb
foreach
@@ -6,4 +7,7 @@ slice
arena
files
logging
+ppm
+# Generated artifacts
+*.ppm \ No newline at end of file
diff --git a/examples/Makefile b/examples/Makefile
index cd83e92..50d531b 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -5,7 +5,7 @@ CFLAGS = -Wall -Wextra -std=c99 -fsanitize=address -g -O0
LDFLAGS =
# Example targets
-EXAMPLES = foreach stringv stringb array slice arena files logging
+EXAMPLES = foreach stringv stringb array slice arena files logging ppm
# Default target
all: $(EXAMPLES)
@@ -35,6 +35,9 @@ files: files.c
logging: logging.c
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
+ppm: ppm.c
+ $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
+
# Run all examples
run: all
@echo "\n=== Running stringv ===\n"
@@ -53,6 +56,8 @@ run: all
@./files
@echo "\n=== Running logging ===\n"
@./logging
+ @echo "\n=== Running ppm ===\n"
+ @./ppm
# Clean build artifacts
clean:
diff --git a/examples/logging.c b/examples/logging.c
index 1d2744c..8e87d59 100644
--- a/examples/logging.c
+++ b/examples/logging.c
@@ -2,24 +2,24 @@
#include "../nonstd.h"
int main(void) {
- // Default level is LOG_INFO
- LOG_INFO_MSG("This is an info message: %d", 42);
- LOG_DEBUG_MSG("This debug message will NOT be shown by default");
+ // Default level is LOG_INFO
+ LOG_INFO_MSG("This is an info message: %d", 42);
+ LOG_DEBUG_MSG("This debug message will NOT be shown by default");
- // Change level to LOG_DEBUG
- set_log_level(LOG_DEBUG);
- LOG_DEBUG_MSG("Now debug messages are shown: %s", "hello");
+ // Change level to LOG_DEBUG
+ set_log_level(LOG_DEBUG);
+ LOG_DEBUG_MSG("Now debug messages are shown: %s", "hello");
- // Warnings and Errors
- LOG_WARN_MSG("This is a warning!");
- LOG_ERROR_MSG("This is an error!");
+ // Warnings and Errors
+ LOG_WARN_MSG("This is a warning!");
+ LOG_ERROR_MSG("This is an error!");
- // Environment variable override test
- // You can set LOG_LEVEL=1 (WARN) etc.
- LogLevel env_level = get_log_level_from_env();
- if (env_level != LOG_DEBUG) {
- printf("Environment overrides level to: %d\n", env_level);
- }
+ // Environment variable override test
+ // You can set LOG_LEVEL=1 (WARN) etc.
+ LogLevel env_level = get_log_level_from_env();
+ if (env_level != LOG_DEBUG) {
+ printf("Environment overrides level to: %d\n", env_level);
+ }
- return 0;
+ return 0;
}
diff --git a/examples/ppm.c b/examples/ppm.c
new file mode 100644
index 0000000..95fafcf
--- /dev/null
+++ b/examples/ppm.c
@@ -0,0 +1,29 @@
+#define NONSTD_IMPLEMENTATION
+#include "../nonstd.h"
+
+int main() {
+ u32 width = 400;
+ u32 height = 400;
+ Canvas img = ppm_init(width, height);
+
+ // Background
+ ppm_fill(&img, COLOR_HEX(0x1a1a1a));
+
+ // Draw some shapes
+ ppm_draw_rect(&img, 50, 50, 100, 100, CLR_RED);
+ ppm_draw_circle(&img, 250, 100, 40, CLR_BLUE);
+ ppm_draw_triangle(&img, 50, 350, 150, 350, 100, 250, CLR_YELLOW);
+ ppm_draw_line(&img, 200, 200, 350, 350, CLR_GREEN);
+
+ // Random colors and macros
+ ppm_draw_rect(&img, 200, 250, 50, 80, COLOR_RGB(255, 165, 0));
+
+ if (ppm_save(&img, "example.ppm")) {
+ printf("Image saved to example.ppm\n");
+ } else {
+ printf("Failed to save image\n");
+ }
+
+ ppm_free(&img);
+ return 0;
+}