nesemu/gui/canvas.c

50 lines
1.0 KiB
C

//
// Created by william on 16/05/24.
//
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#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);
}
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) {
assert(x >= 0);
assert(x < canvas->width);
assert(y >= 0);
assert(y < canvas->height);
int index = x + y * canvas->width;
canvas_draw(canvas, pixel, index);
}
void canvas_reset(Canvas *canvas) {
memset(canvas->pixels, 0, sizeof(Pixel) * (canvas->width * canvas->height));
}