nesemu/gui/canvas.c

50 lines
1.0 KiB
C
Raw Normal View History

2024-05-17 00:33:37 -04:00
//
// Created by william on 16/05/24.
//
#include <assert.h>
#include <string.h>
#include <stdlib.h>
2024-05-17 00:33:37 -04:00
#include "canvas.h"
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);
assert(x < canvas->width);
2024-05-17 00:33:37 -04:00
assert(y >= 0);
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) {
memset(canvas->pixels, 0, sizeof(Pixel) * (canvas->width * canvas->height));
2024-05-17 00:33:37 -04:00
}