#include "kknd.h" /* ========================================================================= * sprites.c - Procedural 2D sprite/terrain generation. * Recreates the chunky post-apocalyptic KKND look without using any * original game assets. All art is drawn at runtime into SDL textures. * ========================================================================= */ #define SURF_FMT SDL_PIXELFORMAT_RGBA32 static SDL_Surface *new_surf(int w, int h) { return SDL_CreateRGBSurfaceWithFormat(0, w, h, 32, SURF_FMT); } static void px(SDL_Surface *s, int x, int y, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { if (x < 0 || y < 0 || x >= s->w || y >= s->h) return; Uint32 *p = (Uint32 *)((Uint8 *)s->pixels + y * s->pitch); Uint32 pix = (a << 24) | (b << 16) | (g << 8) | r; p[x] = pix; } static void fill_rect(SDL_Surface *s, int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { for (int j = y; j < y + h; j++) for (int i = x; i < x + w; i++) px(s, i, j, r, g, b, a); } static void disc(SDL_Surface *s, int cx, int cy, int rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { for (int j = -rad; j <= rad; j++) for (int i = -rad; i <= rad; i++) if (i*i + j*j <= rad*rad) px(s, cx + i, cy + j, r, g, b, a); } static void ring(SDL_Surface *s, int cx, int cy, int rad, int t, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { for (int j = -rad; j <= rad; j++) for (int i = -rad; i <= rad; i++) { int d = i*i + j*j; if (d <= rad*rad && d >= (rad - t)*(rad - t)) px(s, cx + i, cy + j, r, g, b, a); } } static void line(SDL_Surface *s, int x0, int y0, int x1, int y1, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { int dx = abs(x1 - x0), dy = -abs(y1 - y0); int sx = x0 < x1 ? 1 : -1, sy = y0 < y1 ? 1 : -1; int err = dx + dy; for (;;) { px(s, x0, y0, r, g, b, a); if (x0 == x1 && y0 == y1) break; int e2 = 2 * err; if (e2 >= dy) { err += dy; x0 += sx; } if (e2 <= dx) { err += dx; y0 += sy; } } } static SDL_Color darken(SDL_Color c, float f) { return (SDL_Color){ (Uint8)(c.r*f), (Uint8)(c.g*f), (Uint8)(c.b*f), c.a }; } static SDL_Color lighten(SDL_Color c, float f) { return (SDL_Color){ (Uint8)(c.r + (255-c.r)*f), (Uint8)(c.g + (255-c.g)*f), (Uint8)(c.b + (255-c.b)*f), c.a }; } /* ---------------- unit sprites ---------------- */ /* Near-black outline first (KKND2 silhouette), then bright faction fill. */ SDL_Texture *sprites_make_unit(SDL_Renderer *r, UnitType t, Faction f) { int S = 30; SDL_Surface *s = new_surf(S, S); SDL_Color base = faction_color(f); SDL_Color dark = darken(base, 0.55f); SDL_Color lite = lighten(base, 0.45f); /* Tyre / mid-tone parity with snapshot draw_unit (mr/mg/mb). */ const Uint8 tr = 70, tg = 65, tb = 58; const Uint8 ok = 18, og = 16, ob = 14; /* near-black outline */ (void)dark; switch (t) { case UT_INFANTRY: disc(s, 15, 15, 9, ok, og, ob, 255); disc(s, 15, 15, 7, base.r, base.g, base.b, 255); disc(s, 15, 11, 4, lite.r, lite.g, lite.b, 255); line(s, 15, 15, 15, 1, ok, og, ob, 255); /* rifle */ break; case UT_BUGGY: fill_rect(s, 3, 7, 24, 16, ok, og, ob, 255); fill_rect(s, 5, 9, 20, 12, base.r, base.g, base.b, 255); fill_rect(s, 7, 11, 16, 8, lite.r, lite.g, lite.b, 255); /* mid body */ disc(s, 8, 7, 4, tr, tg, tb, 255); disc(s, 22, 7, 4, tr, tg, tb, 255); disc(s, 8, 23, 4, tr, tg, tb, 255); disc(s, 22, 23, 4, tr, tg, tb, 255); line(s, 15, 15, 29, 15, ok, og, ob, 255); break; case UT_TANK: fill_rect(s, 2, 8, 26, 14, ok, og, ob, 255); fill_rect(s, 4, 10, 22, 10, base.r, base.g, base.b, 255); disc(s, 15, 15, 7, lite.r, lite.g, lite.b, 255); line(s, 15, 15, 30, 15, ok, og, ob, 255); disc(s, 8, 23, 4, tr, tg, tb, 255); disc(s, 22, 23, 4, tr, tg, tb, 255); break; case UT_ARTILLERY: fill_rect(s, 2, 10, 26, 12, ok, og, ob, 255); fill_rect(s, 4, 12, 22, 8, base.r, base.g, base.b, 255); disc(s, 15, 15, 6, lite.r, lite.g, lite.b, 255); line(s, 15, 15, 3, 3, ok, og, ob, 255); line(s, 15, 15, 27, 27, ok, og, ob, 255); break; case UT_HARVESTER: fill_rect(s, 1, 6, 28, 18, ok, og, ob, 255); fill_rect(s, 3, 8, 24, 14, base.r, base.g, base.b, 255); fill_rect(s, 5, 10, 20, 10, 200, 170, 90, 255); /* scrap bed */ disc(s, 8, 23, 4, tr, tg, tb, 255); disc(s, 22, 23, 4, tr, tg, tb, 255); line(s, 5, 7, 5, 2, lite.r, lite.g, lite.b, 255); break; case UT_JEEP: fill_rect(s, 4, 9, 22, 12, ok, og, ob, 255); fill_rect(s, 6, 11, 18, 8, base.r, base.g, base.b, 255); disc(s, 9, 22, 4, tr, tg, tb, 255); disc(s, 21, 22, 4, tr, tg, tb, 255); line(s, 15, 11, 15, 3, lite.r, lite.g, lite.b, 255); /* roll bar */ line(s, 15, 15, 28, 15, ok, og, ob, 255); break; case UT_MUTANT: disc(s, 15, 16, 10, ok, og, ob, 255); disc(s, 15, 14, 8, base.r, base.g, base.b, 255); disc(s, 15, 10, 4, lite.r, lite.g, lite.b, 255); line(s, 15, 16, 24, 9, ok, og, ob, 255); /* arm/club */ break; case UT_BEAST: fill_rect(s, 5, 9, 20, 12, ok, og, ob, 255); fill_rect(s, 7, 11, 16, 8, base.r, base.g, base.b, 255); disc(s, 9, 21, 3, tr, tg, tb, 255); disc(s, 14, 22, 3, tr, tg, tb, 255); disc(s, 19, 21, 3, tr, tg, tb, 255); disc(s, 23, 22, 3, tr, tg, tb, 255); disc(s, 23, 12, 5, lite.r, lite.g, lite.b, 255); break; case UT_RAVAGER: fill_rect(s, 0, 7, 30, 16, ok, og, ob, 255); fill_rect(s, 2, 9, 26, 12, base.r, base.g, base.b, 255); disc(s, 15, 15, 8, lite.r, lite.g, lite.b, 255); line(s, 15, 15, 29, 15, ok, og, ob, 255); /* big barrel */ disc(s, 7, 23, 4, tr, tg, tb, 255); disc(s, 23, 23, 4, tr, tg, tb, 255); break; case UT_FLAME: fill_rect(s, 5, 11, 20, 15, ok, og, ob, 255); fill_rect(s, 7, 13, 16, 11, base.r, base.g, base.b, 255); disc(s, 15, 10, 5, lite.r, lite.g, lite.b, 255); line(s, 15, 15, 28, 20, 255, 150, 50, 255); /* flame nozzle */ break; case UT_MISSILE: fill_rect(s, 4, 11, 22, 14, ok, og, ob, 255); fill_rect(s, 6, 13, 18, 10, base.r, base.g, base.b, 255); disc(s, 9, 23, 4, tr, tg, tb, 255); disc(s, 21, 23, 4, tr, tg, tb, 255); line(s, 15, 15, 28, 6, 255, 80, 60, 255); /* launcher */ break; case UT_MEDIC: fill_rect(s, 5, 10, 20, 15, ok, og, ob, 255); fill_rect(s, 7, 12, 16, 11, 245, 245, 245, 255); /* white ambulance */ disc(s, 10, 24, 4, tr, tg, tb, 255); disc(s, 20, 24, 4, tr, tg, tb, 255); disc(s, 15, 9, 5, 240, 50, 50, 255); /* red cross */ fill_rect(s, 14, 5, 3, 8, 255, 255, 255, 255); fill_rect(s, 11, 8, 8, 3, 255, 255, 255, 255); break; default: disc(s, 15, 15, 10, ok, og, ob, 255); disc(s, 15, 15, 8, base.r, base.g, base.b, 255); break; } SDL_Texture *tex = SDL_CreateTextureFromSurface(r, s); SDL_FreeSurface(s); return tex; } /* ---------------- building sprites ---------------- */ /* Bright concrete pad + BT_* interiors (parity with snapshot draw_bld). */ SDL_Texture *sprites_make_bld(SDL_Renderer *r, BuildingType t, Faction f) { int w = bld_def(t)->w, h = bld_def(t)->h; int W = w * TILE, H = h * TILE; SDL_Surface *s = new_surf(W, H); SDL_Color base = faction_color(f); SDL_Color dark = darken(base, 0.5f); SDL_Color lite = lighten(base, 0.35f); const Uint8 ok = 22, og = 20, ob = 16; const Uint8 wall = 205, wall2 = 175; /* Bright concrete pad on arid sand */ fill_rect(s, 0, 0, W, H, ok, og, ob, 255); fill_rect(s, 2, 2, W - 4, H - 4, 210, 185, 130, 255); fill_rect(s, 4, 4, W - 8, H - 8, wall2, (Uint8)(wall2 - 18), (Uint8)(wall2 - 45), 255); fill_rect(s, 5, 5, W - 10, H - 10, wall, (Uint8)(wall - 22), (Uint8)(wall - 55), 255); fill_rect(s, 5, 5, W - 10, 6, (Uint8)(wall + 30), (Uint8)(wall + 10), (Uint8)(wall - 20), 255); switch (t) { case BT_BASE: fill_rect(s, 5, 5, W - 10, H - 10, ok, og, ob, 255); fill_rect(s, 7, 7, W - 14, H - 14, base.r, base.g, base.b, 255); disc(s, W/2, H/2, W/4, lite.r, lite.g, lite.b, 255); disc(s, W/2, H/2, W/6, 255, 250, 180, 255); line(s, W/2, H/2, W/2, 4, 220, 210, 80, 255); fill_rect(s, W/2 - 2, 4, 4, H/3, 200, 180, 60, 255); /* antenna mast */ break; case BT_WARFACTORY: fill_rect(s, 5, 5, W - 10, H - 10, ok, og, ob, 255); fill_rect(s, 7, 7, W - 14, H - 14, base.r, base.g, base.b, 255); fill_rect(s, 10, 10, W - 20, 12, lite.r, lite.g, lite.b, 255); fill_rect(s, 10, H - 24, W - 20, 12, lite.r, lite.g, lite.b, 255); fill_rect(s, W/2 - 4, 8, 8, H - 16, 35, 35, 40, 255); /* bay door */ break; case BT_REFINERY: disc(s, W/2, H/2, W/4 + 2, ok, og, ob, 255); disc(s, W/2, H/2, W/4, 200, 165, 85, 255); ring(s, W/2, H/2, W/4 - 3, 3, 90, 70, 45, 255); fill_rect(s, 8, H - 14, W - 16, 8, 140, 110, 55, 255); /* base slab */ break; case BT_POWER: disc(s, W/2, H/2, W/3 + 1, ok, og, ob, 255); disc(s, W/2, H/2, W/3, 100, 160, 210, 255); line(s, W/2, 4, W/2, H - 4, 230, 245, 255, 255); line(s, 4, H/2, W - 4, H/2, 230, 245, 255, 255); disc(s, W/2, H/2, 4, 250, 252, 255, 255); break; case BT_TURRET: fill_rect(s, 4, H - 10, W - 8, 8, 55, 50, 42, 255); disc(s, W/2, H/2, W/3 + 1, ok, og, ob, 255); disc(s, W/2, H/2, W/3, base.r, base.g, base.b, 255); disc(s, W/2, H/2, W/5, lite.r, lite.g, lite.b, 255); line(s, W/2, H/2, W/2 + 14, H/2 - 14, 30, 28, 24, 255); disc(s, W/2 + 12, H/2 - 12, 2, 255, 200, 80, 255); /* barrel tip */ break; case BT_WALL: fill_rect(s, 1, 1, W - 2, H - 2, ok, og, ob, 255); fill_rect(s, 2, 2, W - 4, H - 4, 175, 155, 120, 255); fill_rect(s, 3, 3, W - 6, 5, 210, 190, 150, 255); line(s, 2, 2, W - 2, H - 2, 60, 52, 42, 255); break; case BT_REPAIR: fill_rect(s, 7, 7, W - 14, H - 14, ok, og, ob, 255); fill_rect(s, 9, 9, W - 18, H - 18, base.r, base.g, base.b, 255); ring(s, W/2, H/2, W/4, 3, 120, 220, 120, 255); line(s, W/2 - 12, H/2, W/2 + 12, H/2, 120, 220, 120, 255); line(s, W/2, H/2 - 12, W/2, H/2 + 12, 120, 220, 120, 255); break; case BT_RESEARCH: fill_rect(s, 7, 7, W - 14, H - 14, ok, og, ob, 255); fill_rect(s, 9, 9, W - 18, H - 18, base.r, base.g, base.b, 255); disc(s, W/2, H/2, W/5, 210, 180, 80, 255); line(s, W/2, 6, W/2, H - 6, 230, 230, 160, 255); line(s, 6, H/2, W - 6, H/2, 230, 230, 160, 255); break; default: (void)dark; break; } SDL_Texture *tex = SDL_CreateTextureFromSurface(r, s); SDL_FreeSurface(s); return tex; } /* ---------------- terrain tiles ---------------- */ SDL_Texture *sprites_make_terrain(SDL_Renderer *r, TerrainType t) { SDL_Surface *s = new_surf(TILE, TILE); Uint8 tr=0,tg=0,tb=0; /* Classic KKND2 wasteland: yellow-tan sand (R>>B, G slightly under R). */ switch (t) { case T_WASTELAND: tr=228; tg=188; tb=98; break; case T_RUBBLE: tr=168; tg=138; tb=88; break; case T_ROAD: tr=175; tg=148; tb=98; break; case T_GRASS: tr=242; tg=198; tb=92; break; /* sunlit desert */ case T_WATER: tr=72; tg=128; tb=178; break; case T_ROCK: tr=155; tg=142; tb=128; break; case T_SCRAP: tr=240; tg=190; tb=78; break; default: tr=228; tg=188; tb=98; break; } fill_rect(s, 0, 0, TILE, TILE, tr, tg, tb, 255); /* speckle for texture */ for (int i = 0; i < 40; i++) { int x = (i*73) % TILE, y = (i*131) % TILE; int v = ((i*53) % 24) - 12; px(s, x, y, (Uint8)kknd_clampi(tr+v,0,255), (Uint8)kknd_clampi(tg+v,0,255), (Uint8)kknd_clampi(tb+v,0,255), 255); } if (t == T_SCRAP) { for (int i = 0; i < 10; i++) { int x = (i*97) % TILE, y = (i*61) % TILE; disc(s, x, y, 2, 180, 150, 60, 255); } } if (t == T_WATER) { for (int i = 0; i < TILE; i += 4) line(s, 0, i, TILE, i, 60, 100, 150, 255); } if (t == T_ROCK) { for (int i = 0; i < 12; i++) { int x = (i*89) % TILE, y = (i*149) % TILE; disc(s, x, y, 3, 130, 124, 118, 255); } } SDL_Texture *tex = SDL_CreateTextureFromSurface(r, s); SDL_FreeSurface(s); return tex; } /* ---------------- bullets ---------------- */ static SDL_Texture *make_bullet(SDL_Renderer *r, int kind) { int S = 10; SDL_Surface *s = new_surf(S, S); switch (kind) { case 0: disc(s, 5, 5, 3, 255, 230, 120, 255); break; /* MG tracer */ case 1: disc(s, 5, 5, 4, 255, 160, 60, 255); break; /* shell */ case 2: disc(s, 5, 5, 3, 120, 255, 160, 255); break; /* laser */ case 3: disc(s, 5, 5, 4, 255, 90, 90, 255); break; /* missile */ default: disc(s, 5, 5, 3, 255, 255, 255, 255); } SDL_Texture *tex = SDL_CreateTextureFromSurface(r, s); SDL_FreeSurface(s); return tex; } int sprites_init(Sprites *sp, SDL_Renderer *r) { memset(sp, 0, sizeof(Sprites)); for (int t = 0; t < T_COUNT; t++) sp->terrain[t] = sprites_make_terrain(r, (TerrainType)t); for (int t = 1; t < UT_COUNT; t++) { sp->unit[t][FACTION_SURVIVOR] = sprites_make_unit(r, (UnitType)t, FACTION_SURVIVOR); sp->unit[t][FACTION_EVOLVED] = sprites_make_unit(r, (UnitType)t, FACTION_EVOLVED); sp->unit[t][FACTION_SERIES9] = sprites_make_unit(r, (UnitType)t, FACTION_SERIES9); } for (int t = 1; t < BT_COUNT; t++) { sp->bld[t][FACTION_SURVIVOR] = sprites_make_bld(r, (BuildingType)t, FACTION_SURVIVOR); sp->bld[t][FACTION_EVOLVED] = sprites_make_bld(r, (BuildingType)t, FACTION_EVOLVED); sp->bld[t][FACTION_SERIES9] = sprites_make_bld(r, (BuildingType)t, FACTION_SERIES9); } for (int k = 0; k < 4; k++) sp->bullet[k] = make_bullet(r, k); sp->font_small = TTF_OpenFont("/usr/share/fonts/TTF/DejaVuSans.ttf", 14); if (!sp->font_small) sp->font_small = TTF_OpenFont("/usr/share/fonts/gnu-free/FreeSans.ttf", 14); sp->font_med = sp->font_small ? TTF_OpenFont("/usr/share/fonts/TTF/DejaVuSans.ttf", 20) : NULL; sp->font_big = sp->font_small ? TTF_OpenFont("/usr/share/fonts/TTF/DejaVuSans.ttf", 34) : NULL; if (!sp->font_small) { sp->font_small = TTF_OpenFont("/usr/share/fonts/TTF/DejaVuSans-Bold.ttf", 14); } return 1; } void sprites_free(Sprites *sp) { for (int t = 0; t < T_COUNT; t++) if (sp->terrain[t]) SDL_DestroyTexture(sp->terrain[t]); for (int t = 1; t < UT_COUNT; t++) for (int f = 0; f < MAX_FACTIONS; f++) if (sp->unit[t][f]) SDL_DestroyTexture(sp->unit[t][f]); for (int t = 1; t < BT_COUNT; t++) for (int f = 0; f < MAX_FACTIONS; f++) if (sp->bld[t][f]) SDL_DestroyTexture(sp->bld[t][f]); for (int k = 0; k < 4; k++) if (sp->bullet[k]) SDL_DestroyTexture(sp->bullet[k]); if (sp->font_small) TTF_CloseFont(sp->font_small); if (sp->font_med) TTF_CloseFont(sp->font_med); if (sp->font_big) TTF_CloseFont(sp->font_big); }