xormod/picture.c

52 lines
1.4 KiB
C

#include "picture.h"
Picture *Picture_new(SDL_Renderer *rend,
int x, int y, int w, int h, uint32_t color) {
Picture *p = calloc(1, sizeof(Picture));
p->x1 = x;
p->x2 = x + w;
p->y1 = y;
p->y2 = y + h;
p->w = w;
p->h = h;
p->color = color;
p->s = (w + 7) / 8 * 8;
p->pixels = malloc(p->s * h * sizeof(uint32_t));
p->surface = SDL_CreateRGBSurfaceWithFormatFrom(
p->pixels, w, h, 32, p->s * sizeof(uint32_t),
SDL_PIXELFORMAT_RGBA32);
Picture_render(p, rend);
return p;
}
typedef uint32_t uint32_8 __attribute__ ((vector_size (8 * sizeof(uint32_t))));
void Picture_render(Picture *p, SDL_Renderer *rend) {
int w = p->w, h = p->h;
uint32_8 c = p->color - (uint32_8){};
uint32_8 y = p->y1 - (uint32_8){};
for (int j = 0, r = 0; j < h; j++, y += 1) {
uint32_8 x = p->x1 + (uint32_8){0, 1, 2, 3, 4, 5, 6, 7};
for (int i = 0; i < w; i += 8,r += 8, x += 8) {
uint32_8 z = (x ^ y) % 9;
uint32_8 f = (z==0) & c;
memcpy(&p->pixels[r], (uint32_t*)&f, sizeof(f));
}
}
if (p->texture) SDL_DestroyTexture(p->texture);
p->texture = SDL_CreateTextureFromSurface(rend, p->surface);
SDL_SetTextureBlendMode(p->texture, SDL_BLENDMODE_NONE);
}
void Picture_move(Picture *p, int x, int y) {
p->x1 += x;
p->x2 += x;
p->y1 += y;
p->y2 += y;
}