From 5d088a00d44bd8b080413835b53fd9cd3d7d8750 Mon Sep 17 00:00:00 2001 From: Henri Vasserman Date: Sun, 11 Apr 2021 18:21:31 +0300 Subject: [PATCH] move code around --- xormod.c | 134 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 78 insertions(+), 56 deletions(-) diff --git a/xormod.c b/xormod.c index e0e8053..806b3d4 100644 --- a/xormod.c +++ b/xormod.c @@ -75,6 +75,74 @@ static void Picture_move(Picture *p, 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 w = 1280, h = 720; @@ -99,15 +167,9 @@ int main() { SDL_Texture *font = load_font(rend); - Picture *pictures[4]; - enum { P_TOPLEFT, P_TOPRGHT, P_BOTLEFT, P_BOTRGHT }; + Field f; + 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 }; int running = 1; while (running) { @@ -116,72 +178,32 @@ int main() { break; } if (keys[SDL_SCANCODE_LEFT]) { - off.x -= 16; + f.offset.x -= 16; } if (keys[SDL_SCANCODE_RIGHT]) { - off.x += 16; + f.offset.x += 16; } if (keys[SDL_SCANCODE_UP]) { - off.y -= 16; + f.offset.y -= 16; } if (keys[SDL_SCANCODE_DOWN]) { - off.y += 16; + f.offset.y += 16; } struct int2 mouse; if (SDL_GetMouseState(&mouse.x, &mouse.y) & SDL_BUTTON(SDL_BUTTON_LEFT)) { if (mouse_prev.x != -1 ) { - off.x -= mouse.x - mouse_prev.x; - off.y -= mouse.y - mouse_prev.y; + f.offset.x -= mouse.x - mouse_prev.x; + f.offset.y -= mouse.y - mouse_prev.y; } mouse_prev = mouse; } else if (mouse_prev.x != -1) { mouse_prev = (struct int2) { -1, -1 }; } - if (off.y < pictures[P_TOPLEFT]->y1) { - 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); - } + Field_scroll(&f, rend); - //SDL_SetRenderDrawColor(rend, 0, 0, 0, 255); - //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); - } + Field_draw(&f, rend); //char buf[72]; //SDL_SetRenderDrawColor(rend, 0, 0, 0, 255);