2024-05-17 00:33:37 -04:00
|
|
|
//
|
|
|
|
// Created by william on 16/05/24.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2024-05-17 11:40:02 -04:00
|
|
|
#include <stdlib.h>
|
2024-05-17 00:33:37 -04:00
|
|
|
#include "canvas.h"
|
|
|
|
|
2024-05-17 11:40:02 -04:00
|
|
|
Canvas canvas_init(int width, int height) {
|
|
|
|
assert(width > 0);
|
|
|
|
assert(height > 0);
|
|
|
|
|
|
|
|
Canvas canvas;
|
|
|
|
canvas.width = width;
|
|
|
|
canvas.height = height;
|
|
|
|
|
|
|
|
int size = width * height;
|
|
|
|
canvas.pixels = malloc(sizeof(Pixel) * size);
|
|
|
|
|
|
|
|
return canvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
void canvas_uninit(Canvas *canvas) {
|
|
|
|
assert(canvas != NULL);
|
|
|
|
assert(canvas->pixels != NULL);
|
|
|
|
|
|
|
|
free(canvas->pixels);
|
|
|
|
}
|
|
|
|
|
2024-05-17 13:16:21 -04:00
|
|
|
void canvas_draw(Canvas *canvas, Pixel pixel, int index) {
|
|
|
|
assert(index >= 0);
|
|
|
|
assert(index < canvas->width * canvas->height);
|
|
|
|
|
|
|
|
canvas->pixels[index] = pixel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void canvas_draw_pos(Canvas *canvas, Pixel pixel, int x, int y) {
|
2024-05-17 00:33:37 -04:00
|
|
|
assert(x >= 0);
|
2024-05-17 11:40:02 -04:00
|
|
|
assert(x < canvas->width);
|
2024-05-17 00:33:37 -04:00
|
|
|
assert(y >= 0);
|
2024-05-17 11:40:02 -04:00
|
|
|
assert(y < canvas->height);
|
2024-05-17 00:33:37 -04:00
|
|
|
|
2024-05-17 13:16:21 -04:00
|
|
|
int index = x + y * canvas->width;
|
|
|
|
canvas_draw(canvas, pixel, index);
|
2024-05-17 00:33:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void canvas_reset(Canvas *canvas) {
|
2024-05-17 11:40:02 -04:00
|
|
|
memset(canvas->pixels, 0, sizeof(Pixel) * (canvas->width * canvas->height));
|
2024-05-17 00:33:37 -04:00
|
|
|
}
|