xormod/picture.c

54 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_4 __attribute__ ((vector_size (4 * sizeof(uint32_t))));
void Picture_render(Picture *p, SDL_Renderer *rend) {
int w = p->w, h = p->h;
uint32_4 c = {p->color, p->color, p->color, p->color};
const uint32_4 y0 = { 0, 0, 0, 0 };
const uint32_4 x0 = { 0, 1, 2, 3 };
uint32_4 y = y0 + p->y1;
for (int j = 0, r = 0; j < h; j++, y += 1) {
uint32_4 x = x0 + p->x1;
for (int i = 0; i < w; i += 4, r += 4, x += 4) {
uint32_4 z = (x ^ y) % 9;
uint32_4 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;
}