xormod/field.c

66 lines
1.8 KiB
C

#include "field.h"
#include <SDL.h>
#define SWAP(a, b) ({ \
__typeof(a) tmp = (a); \
(a) = (b); \
(b) = tmp; \
})
void Field_init(Field *f, SDL_Renderer *rend, int w, int h) {
f->tl = Picture_new(rend, 0, 0, w, h, 0xff7fffff);
f->tr = Picture_new(rend, w, 0, w, h, 0xffff7fff);
f->bl = Picture_new(rend, 0, h, w, h, 0xffffff7f);
f->br = Picture_new(rend, w, h, w, h, 0xff7fff7f);
f->offset = (struct int2){ 0, 0 };
}
void Field_scroll(Field *f, SDL_Renderer *rend) {
int w = f->pics[0]->w;
int h = f->pics[0]->h;
if (f->offset.y < f->tl->y1) {
SWAP(f->tl, f->bl);
SWAP(f->tr, f->br);
Picture_move(f->tl, 0, -2*h);
Picture_move(f->tr, 0, -2*h);
Picture_render(f->tl, rend);
Picture_render(f->tr, rend);
}
if (f->offset.x < f->tl->x1) {
SWAP(f->tl, f->tr);
SWAP(f->bl, f->br);
Picture_move(f->tl, -2*w, 0);
Picture_move(f->bl, -2*w, 0);
Picture_render(f->tl, rend);
Picture_render(f->bl, rend);
}
if (f->offset.y > f->br->y1) {
SWAP(f->tl, f->bl);
SWAP(f->tr, f->br);
Picture_move(f->bl, 0, 2*h);
Picture_move(f->br, 0, 2*h);
Picture_render(f->bl, rend);
Picture_render(f->br, rend);
}
if (f->offset.x > f->br->x1) {
SWAP(f->tl, f->tr);
SWAP(f->bl, f->br);
Picture_move(f->tr, 2*w, 0);
Picture_move(f->br, 2*w, 0);
Picture_render(f->tr, rend);
Picture_render(f->br, rend);
}
}
void Field_draw(Field *f, SDL_Renderer *rend) {
for (int i = 0; i < 4; i++) {
Picture *p = f->pics[i];
SDL_Rect dst = {
p->x1 - f->offset.x, p->y1 - f->offset.y, p->w, p->h,
};
SDL_RenderCopy(rend, p->texture, NULL, &dst);
}
}