feat: add draw functions

This commit is contained in:
2025-08-29 22:54:21 +02:00
parent 715aa7a261
commit 9a2be29b5f
2 changed files with 66 additions and 0 deletions

54
src/draw.c Normal file
View File

@@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "draw.h"
#include "math/vec4.h"
char* canvas;
void initCanvas(int width, int height)
{
int size = width*height;
canvas = malloc(size);
if (!canvas) exit(1);
memset(canvas, ' ', size);
}
void editCanvas(int width, int height)
{
int size = width * height;
canvas = realloc(canvas, size);
if (!canvas) exit(1);
}
static char drawPixel(float zBuffer)
{
if (zBuffer < 0.25f) return '.';
else if (zBuffer < 0.5f) return '*';
else if (zBuffer < 0.75f) return 'o';
else return '#';
}
void drawCanvas(Vec4f_t p, int width, int height)
{
int x = (int)p.x;
int y = (int)p.y;
if(x >= 0 && x < width && y >= 0 && y < height)
canvas[y*width + x] = drawPixel(p.z);
}
void renderCanvas(int width, int height)
{
for (int i = 0; i<height; i++) {
for (int j = 0; j<width; j++) {
putchar(canvas[i*width+j]);
}
putchar('\n');
}
}
void freeCanvas(void)
{
free(canvas);
}

12
src/draw.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef DRAW_H
#define DRAW_H
#include "math/vec4.h"
void initCanvas(int width, int height);
void editCanvas(int width, int height);
void drawCanvas(Vec4f_t p, int width, int height);
void renderCanvas(int width, int height);
void freeCanvas(void);
#endif // DRAW_H