/* * rpgwalk — демо: 8 уникальных RPG-персонажей ходят по травяному полю. * * Спрайты — third_party/16x16-RPG-characters (bard, CC), два W0-атласа * по 4 персонажа (12 кадров каждому не лезут в одну страницу на * восьмерых); палитра — из PNG через gfx_pal_fload (слоты 0..15 EGA, * 16+ цвета художника) + gfx_pal_sync для дабл-буфера. * * Лента персонажа: 12 кадров вертикально, dir*3 + кадр: * dir 0 = вниз (на нас), 1 = влево, 2 = вправо, 3 = вверх; * шаг ноги — sprite_anim(dir*3, dir*3+2, ANIM_PINGPONG) (маятник 0/1/2). * * Правила хождения (машина из двух фаз на sprite_moveto): * фаза 0: идём ДО КРАЯ экрана; у края — разворот на 180° и * фаза 1: идём до случайной точки (но не меньше ЧЕТВЕРТИ экрана); * там — поворот на ±90° (случайно) и снова фаза 0. * * ESC — выход. */ #include #include #include #include #include #include #define NCHR 15 #define MINX 0 #define MAXX (320 - 16) #define MINY 14 /* под заголовком */ #define MAXY (256 - 16) #define QX 80 /* четверть экрана */ #define QY 64 static atlas_t at1, at2; static sprite_t sp[NCHR]; static uint8_t dir[NCHR]; /* 0 вниз / 1 влево / 2 вправо / 3 вверх */ static uint8_t phase[NCHR]; /* 0 = до края, 1 = до случайной точки */ /* Направление → шаг ноги той же скоростью (у всех чуть разной). */ static void set_dir(uint8_t i, uint8_t d) { dir[i] = d; // sprite_anim(&sp[i], (uint8_t)(d * 3), (uint8_t)(d * 3 + 2), // (uint8_t)(5 + (i & 3)), ANIM_PINGPONG); sprite_anim(&sp[i], (uint8_t)(d * 3), (uint8_t)(d * 3 + 2), (uint8_t)20, ANIM_PINGPONG); } /* Идти до края экрана вдоль dir[i] (скорость: 1px, интервал 1-2). */ static void go_edge(uint8_t i) { sprite_t *s = &sp[i]; int tx = s->x, ty = s->y; switch (dir[i]) { case 0: ty = MAXY; break; case 1: tx = MINX; break; case 2: tx = MAXX; break; default: ty = MINY; break; } sprite_moveto(s, tx, ty, 1, (uint8_t)(1 + (i & 1))); phase[i] = 0; } /* Развернуться на 180° и идти до случайной точки (≥ четверти экрана). */ static void go_back_random(uint8_t i) { sprite_t *s = &sp[i]; int tx = s->x, ty = s->y, avail, dist; uint8_t d = dir[i]; // d = (uint8_t)(d ^ ((d < 2) ? 3 : 1)); /* 0↔3, 1↔2 — разворот */ d = 3 - d; /* 0↔3, 1↔2 — разворот */ set_dir(i, d); switch (d) { case 0: avail = MAXY - s->y; dist = QY; break; case 1: avail = s->x - MINX; dist = QX; break; case 2: avail = MAXX - s->x; dist = QX; break; default: avail = s->y - MINY; dist = QY; break; } if (avail > dist) dist += rand() % (avail - dist + 1); else dist = avail; /* некуда — до края */ switch (d) { case 0: ty += dist; break; case 1: tx -= dist; break; case 2: tx += dist; break; default: ty -= dist; break; } sprite_moveto(s, tx, ty, 1, (uint8_t)(1 + (i & 1))); phase[i] = 1; } /* Повернуть на ±90° (случайно) и идти до края. */ static void turn_90(uint8_t i) { uint8_t d = dir[i]; uint8_t vert = (d == 0 || d == 3); set_dir(i, vert ? (uint8_t)(1 + (rand() & 1)) /* → влево/вправо */ : (uint8_t)((rand() & 1) ? 0 : 3)); /* → вниз/вверх */ go_edge(i); } /* FPS-плашка слева-сверху: значение раз в секунду, рисуется на ОБЕИХ * страницах (fps_draw = 2 кадра), банк 0x50 — heal её учитывает. */ static void show_fps(uint8_t v) { char buf[4]; buf[0] = (char)('0' + (v / 100) % 10); buf[1] = (char)('0' + (v / 10) % 10); buf[2] = (char)('0' + v % 10); buf[3] = 0; setcolor(WHITE); setfillstyle(SOLID_FILL, BLACK); bar(0, 0, 27, 9); outtextxy(2, 1, buf); } static void draw_field(void) { int i; srand(4321); /* фон идентичен на обеих страницах */ setfillstyle(SOLID_FILL, GREEN); bar(0, 0, 319, 255); for (i = 0; i < 400; i++) /* травинки */ putpixel(rand() % 320, rand() % 244 + 12, (i & 3) ? LIGHTGREEN : BROWN); for (i = 0; i < 24; i++) { /* цветочки */ int x = rand() % 312 + 4, y = rand() % 232 + 16; putpixel(x, y, (i & 1) ? WHITE : YELLOW); putpixel(x - 1, y, LIGHTGREEN); putpixel(x + 1, y, LIGHTGREEN); } setcolor(WHITE); outtextxy(52, 3, "RPGWALK: 8 bards on patrol, ESC"); } int main(void) { uint8_t i, page, hidden; uint8_t frames = 0, last_sec, fps_val = 0, fps_draw = 0; datetime_t dt; if (atlas_load(&at1, "bard1.atl") != 0 && atlas_load(&at1, "a:\\bard1.atl") != 0) return 1; if (atlas_load(&at2, "bard2.atl") != 0 && atlas_load(&at2, "a:\\bard2.atl") != 0) return 1; initgraph(); gfx_sprite_clip(0); /* маршруты в пределах */ if (gfx_pal_fload(0, "bard.pal") < 0) gfx_pal_fload(0, "a:\\bard.pal"); /* EGA + цвета PNG */ gfx_pal_sync(); getdatetime(&dt); srand((unsigned)(dt.second * 77u + dt.minute)); for (i = 0; i < NCHR; i++) { /* Персонажи 0-3 из первого атласа, 4-7 из второго: спрайты * лежат подряд по атласам — один OUT смены страницы на кадр. */ atlas_sprite_init(&sp[i], (i < 4) ? &at1 : &at2, (uint8_t)(i & 3)); sp[i].x = 24 + (rand() % (MAXX - 48)); sp[i].y = MINY + 8 + (rand() % (MAXY - MINY - 16)); set_dir(i, (uint8_t)(rand() & 3)); go_edge(i); sprite_show(&sp[i]); } for (page = 0; page < 2; page++) { /* фон на обе страницы */ gfx_set_draw_page(page); draw_field(); sprite_update(sp, NCHR); } gfx_set_visible_page(0); getdatetime(&dt); srand((unsigned)(dt.second * 77u + dt.minute)); last_sec = dt.second; for (;;) { if (kbhit()) { int c = getch(); if (c == 27) break; /* 1/2/3 — FPS-делитель: логический кадр = n vsync'ов. * Скорость персонажей (пикселей на итерацию) не меняется, но * итераций/сек делится на n — визуальная проверка стабильности * тайминга (метр стоит ровно на 50/25/16). */ if (c >= '1' && c <= '9') gfx_set_fps_div((uint8_t)(c - '0')); } for (i = 0; i < NCHR; i++) if (!sprite_moving(&sp[i])) { if (phase[i] == 0) go_back_random(i); /* у края: разворот */ else turn_90(i); /* дошли: поворот ±90° */ } hidden = gfx_get_visible_page() ^ 1; gfx_set_draw_page(hidden); frames++; getdatetime(&dt); if (dt.second != last_sec) { last_sec = dt.second; fps_val = frames; frames = 0; fps_draw = 2; } if (fps_draw) { fps_draw--; show_fps(fps_val); } sprite_update(sp, NCHR); gfx_wait_vsync(); gfx_set_visible_page(hidden); } closegraph(); atlas_free(&at2); atlas_free(&at1); return 0; }