2024-10-05 21:19:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-04-12 19:15:41 +00:00
|
|
|
#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;
|
2024-10-05 21:19:26 +00:00
|
|
|
p->s = (w * sizeof(uint32_t) + 7) / 8 * 8;
|
2021-04-12 19:15:41 +00:00
|
|
|
|
2024-10-05 21:19:26 +00:00
|
|
|
p->pixels = malloc(p->s * h);
|
|
|
|
p->surface = SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_RGBA32, p->pixels, p->s);
|
|
|
|
if (p->surface == NULL) {
|
|
|
|
fprintf(stderr, "Can't create surface: %s\n", SDL_GetError());
|
|
|
|
exit(-1);
|
|
|
|
}
|
2021-04-12 19:15:41 +00:00
|
|
|
Picture_render(p, rend);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2021-12-01 23:01:48 +00:00
|
|
|
typedef uint32_t uint32_4 __attribute__ ((vector_size (4 * sizeof(uint32_t))));
|
2021-04-12 19:15:41 +00:00
|
|
|
|
|
|
|
void Picture_render(Picture *p, SDL_Renderer *rend) {
|
|
|
|
int w = p->w, h = p->h;
|
2021-12-01 23:01:48 +00:00
|
|
|
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;
|
2021-04-12 19:15:41 +00:00
|
|
|
for (int j = 0, r = 0; j < h; j++, y += 1) {
|
2021-12-01 23:01:48 +00:00
|
|
|
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;
|
2021-04-12 19:15:41 +00:00
|
|
|
memcpy(&p->pixels[r], (uint32_t*)&f, sizeof(f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p->texture) SDL_DestroyTexture(p->texture);
|
|
|
|
p->texture = SDL_CreateTextureFromSurface(rend, p->surface);
|
2024-10-05 21:19:26 +00:00
|
|
|
if (p->texture == NULL) {
|
|
|
|
fprintf(stderr, "Can't create texture: %s\n", SDL_GetError());
|
|
|
|
exit(-1);
|
|
|
|
}
|
2021-04-12 19:15:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|