move code around

main
Henri Vasserman 2021-04-11 18:21:31 +03:00
parent 57a2eff6c1
commit 5d088a00d4
1 changed files with 78 additions and 56 deletions

134
xormod.c
View File

@ -75,6 +75,74 @@ static void Picture_move(Picture *p, int x, int y) {
struct int2 { int x; int y; }; struct int2 { int x; int y; };
typedef struct {
union {
struct {
Picture *tl;
Picture *tr;
Picture *bl;
Picture *br;
};
Picture *pics[4];
};
struct int2 offset;
} Field;
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);
}
}
int main() { int main() {
int w = 1280, h = 720; int w = 1280, h = 720;
@ -99,15 +167,9 @@ int main() {
SDL_Texture *font = load_font(rend); SDL_Texture *font = load_font(rend);
Picture *pictures[4]; Field f;
enum { P_TOPLEFT, P_TOPRGHT, P_BOTLEFT, P_BOTRGHT }; Field_init(&f, rend, w, h);
pictures[P_TOPLEFT] = Picture_new(rend, 0, 0, w, h, 0xff7fffff);
pictures[P_TOPRGHT] = Picture_new(rend, w, 0, w, h, 0xffff7fff);
pictures[P_BOTLEFT] = Picture_new(rend, 0, h, w, h, 0xffffff7f);
pictures[P_BOTRGHT] = Picture_new(rend, w, h, w, h, 0xff7fff7f);
struct int2 off = { 0, 0 };
struct int2 mouse_prev = { -1, -1 }; struct int2 mouse_prev = { -1, -1 };
int running = 1; int running = 1;
while (running) { while (running) {
@ -116,72 +178,32 @@ int main() {
break; break;
} }
if (keys[SDL_SCANCODE_LEFT]) { if (keys[SDL_SCANCODE_LEFT]) {
off.x -= 16; f.offset.x -= 16;
} }
if (keys[SDL_SCANCODE_RIGHT]) { if (keys[SDL_SCANCODE_RIGHT]) {
off.x += 16; f.offset.x += 16;
} }
if (keys[SDL_SCANCODE_UP]) { if (keys[SDL_SCANCODE_UP]) {
off.y -= 16; f.offset.y -= 16;
} }
if (keys[SDL_SCANCODE_DOWN]) { if (keys[SDL_SCANCODE_DOWN]) {
off.y += 16; f.offset.y += 16;
} }
struct int2 mouse; struct int2 mouse;
if (SDL_GetMouseState(&mouse.x, &mouse.y) & SDL_BUTTON(SDL_BUTTON_LEFT)) { if (SDL_GetMouseState(&mouse.x, &mouse.y) & SDL_BUTTON(SDL_BUTTON_LEFT)) {
if (mouse_prev.x != -1 ) { if (mouse_prev.x != -1 ) {
off.x -= mouse.x - mouse_prev.x; f.offset.x -= mouse.x - mouse_prev.x;
off.y -= mouse.y - mouse_prev.y; f.offset.y -= mouse.y - mouse_prev.y;
} }
mouse_prev = mouse; mouse_prev = mouse;
} else if (mouse_prev.x != -1) { } else if (mouse_prev.x != -1) {
mouse_prev = (struct int2) { -1, -1 }; mouse_prev = (struct int2) { -1, -1 };
} }
if (off.y < pictures[P_TOPLEFT]->y1) { Field_scroll(&f, rend);
SWAP(pictures[P_TOPLEFT], pictures[P_BOTLEFT]);
SWAP(pictures[P_TOPRGHT], pictures[P_BOTRGHT]);
Picture_move(pictures[P_TOPLEFT], 0, -2*h);
Picture_move(pictures[P_TOPRGHT], 0, -2*h);
Picture_render(pictures[P_TOPLEFT], rend);
Picture_render(pictures[P_TOPRGHT], rend);
}
if (off.x < pictures[P_TOPLEFT]->x1) {
SWAP(pictures[P_TOPLEFT], pictures[P_TOPRGHT]);
SWAP(pictures[P_BOTLEFT], pictures[P_BOTRGHT]);
Picture_move(pictures[P_TOPLEFT], -2*w, 0);
Picture_move(pictures[P_BOTLEFT], -2*w, 0);
Picture_render(pictures[P_TOPLEFT], rend);
Picture_render(pictures[P_BOTLEFT], rend);
}
if (off.y > pictures[P_BOTRGHT]->y1) {
SWAP(pictures[P_TOPLEFT], pictures[P_BOTLEFT]);
SWAP(pictures[P_TOPRGHT], pictures[P_BOTRGHT]);
Picture_move(pictures[P_BOTLEFT], 0, 2*h);
Picture_move(pictures[P_BOTRGHT], 0, 2*h);
Picture_render(pictures[P_BOTLEFT], rend);
Picture_render(pictures[P_BOTRGHT], rend);
}
if (off.x > pictures[P_BOTRGHT]->x1) {
SWAP(pictures[P_TOPLEFT], pictures[P_TOPRGHT]);
SWAP(pictures[P_BOTLEFT], pictures[P_BOTRGHT]);
Picture_move(pictures[P_TOPRGHT], 2*w, 0);
Picture_move(pictures[P_BOTRGHT], 2*w, 0);
Picture_render(pictures[P_TOPRGHT], rend);
Picture_render(pictures[P_BOTRGHT], rend);
}
//SDL_SetRenderDrawColor(rend, 0, 0, 0, 255); Field_draw(&f, rend);
//SDL_RenderClear(rend);
for (int i = 0; i < 4; i++) {
Picture *p = pictures[i];
SDL_Rect dst = {
p->x1 - off.x, p->y1 - off.y, w, h,
};
SDL_RenderCopy(rend, p->texture, NULL, &dst);
}
//char buf[72]; //char buf[72];
//SDL_SetRenderDrawColor(rend, 0, 0, 0, 255); //SDL_SetRenderDrawColor(rend, 0, 0, 0, 255);