| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#include "draw/draw_buffer.h" |
| 50 |
|
| 51 |
#include <assert.h> |
| 52 |
|
| 53 |
#include "algorithms.h" |
| 54 |
#include "log.h" |
| 55 |
#include "los.h" |
| 56 |
|
| 57 |
|
| 58 |
void DrawBufferInit(DrawBuffer *b, struct vec2i size, GraphicsDevice *g) |
| 59 |
{ |
| 60 |
b->OrigSize = size; |
| 61 |
CArrayInitFillZero(&b->tiles, sizeof(Tile *), size.x * size.y); |
| 62 |
b->g = g; |
| 63 |
CArrayInit(&b->displaylist, sizeof(const Thing *)); |
| 64 |
CArrayReserve(&b->displaylist, 32); |
| 65 |
} |
| 66 |
void DrawBufferTerminate(DrawBuffer *b) |
| 67 |
{ |
| 68 |
CArrayTerminate(&b->tiles); |
| 69 |
CArrayTerminate(&b->displaylist); |
| 70 |
} |
| 71 |
|
| 72 |
void DrawBufferSetFromMap( |
| 73 |
DrawBuffer *buffer, const Map *map, const struct vec2 origin, |
| 74 |
const int width) |
| 75 |
{ |
| 76 |
buffer->Size = svec2i(width, buffer->OrigSize.y); |
| 77 |
|
| 78 |
buffer->xTop = (int)origin.x - TILE_WIDTH * width / 2; |
| 79 |
buffer->yTop = (int)origin.y - TILE_HEIGHT * buffer->OrigSize.y / 2; |
| 80 |
|
| 81 |
buffer->xStart = buffer->xTop / TILE_WIDTH; |
| 82 |
buffer->yStart = buffer->yTop / TILE_HEIGHT; |
| 83 |
if (buffer->xTop < 0) |
| 84 |
{ |
| 85 |
buffer->xStart--; |
| 86 |
} |
| 87 |
if (buffer->yTop < 0) |
| 88 |
{ |
| 89 |
buffer->yStart--; |
| 90 |
} |
| 91 |
|
| 92 |
buffer->dx = buffer->xStart * TILE_WIDTH - buffer->xTop; |
| 93 |
buffer->dy = buffer->yStart * TILE_HEIGHT - buffer->yTop; |
| 94 |
|
| 95 |
Tile **bufTile = CArrayGet(&buffer->tiles, 0); |
| 96 |
struct vec2i pos; |
| 97 |
for (pos.y = buffer->yStart; |
| 98 |
pos.y < buffer->yStart + buffer->Size.y; |
| 99 |
pos.y++) |
| 100 |
{ |
| 101 |
for (pos.x = buffer->xStart; |
| 102 |
pos.x < buffer->xStart + buffer->Size.x; |
| 103 |
pos.x++, bufTile++) |
| 104 |
{ |
| 105 |
if (MapIsTileIn(map, pos)) |
| 106 |
{ |
| 107 |
*bufTile = MapGetTile(map, pos); |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
*bufTile = NULL; |
| 112 |
} |
| 113 |
} |
| 114 |
bufTile += buffer->OrigSize.x - buffer->Size.x; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
void DrawBufferFix(DrawBuffer *buffer) |
| 120 |
{ |
| 121 |
int tileIdx = 0; |
| 122 |
for (int y = 0; y < Y_TILES; y++) |
| 123 |
{ |
| 124 |
for (int x = 0; x < buffer->Size.x; x++, tileIdx++) |
| 125 |
{ |
| 126 |
Tile **tile = CArrayGet(&buffer->tiles, tileIdx); |
| 127 |
if (*tile == NULL) continue; |
| 128 |
const struct vec2i mapTile = |
| 129 |
svec2i(x + buffer->xStart, y + buffer->yStart); |
| 130 |
(*tile)->outOfSight = !LOSTileIsVisible(&gMap, mapTile); |
| 131 |
} |
| 132 |
tileIdx += X_TILES - buffer->Size.x; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
static int CompareY(const void *v1, const void *v2); |
| 137 |
void DrawBufferSortDisplayList(DrawBuffer *buffer) |
| 138 |
{ |
| 139 |
qsort( |
| 140 |
buffer->displaylist.data, |
| 141 |
buffer->displaylist.size, |
| 142 |
buffer->displaylist.elemSize, |
| 143 |
CompareY); |
| 144 |
} |
| 145 |
static int CompareY(const void *v1, const void *v2) |
| 146 |
{ |
| 147 |
const Thing * const *t1 = v1; |
| 148 |
const Thing * const *t2 = v2; |
| 149 |
if ((*t1)->Pos.y < (*t2)->Pos.y) |
| 150 |
{ |
| 151 |
return -1; |
| 152 |
} |
| 153 |
else if ((*t1)->Pos.y > (*t2)->Pos.y) |
| 154 |
{ |
| 155 |
return 1; |
| 156 |
} |
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
const Tile **DrawBufferGetFirstTile(const DrawBuffer *b) |
| 161 |
{ |
| 162 |
return CArrayGet(&b->tiles, 0); |
| 163 |
} |