… | |
… | |
32 | #include <cstring> |
32 | #include <cstring> |
33 | #include <cstdio> |
33 | #include <cstdio> |
34 | #include <cstdlib> |
34 | #include <cstdlib> |
35 | |
35 | |
36 | #include <utility> |
36 | #include <utility> |
|
|
37 | #include <bitset> |
37 | |
38 | |
38 | #define USE_RWOPS 1 // for SDL_mixer:LoadMUS_RW |
39 | #define USE_RWOPS 1 // for SDL_mixer:LoadMUS_RW |
39 | |
40 | |
40 | #include <SDL.h> |
41 | #include <SDL.h> |
41 | #include <SDL_thread.h> |
42 | #include <SDL_thread.h> |
… | |
… | |
2223 | draw (DC::Map self, int mx, int my, int sw, int sh, int Tw, int Th, U32 player = 0xffffffff, int sdx = 0, int sdy = 0) |
2224 | draw (DC::Map self, int mx, int my, int sw, int sh, int Tw, int Th, U32 player = 0xffffffff, int sdx = 0, int sdy = 0) |
2224 | CODE: |
2225 | CODE: |
2225 | { |
2226 | { |
2226 | int x, y, z; |
2227 | int x, y, z; |
2227 | |
2228 | |
2228 | uint32_t smooth_level[256 / 32]; // one bit for every possible smooth level |
|
|
2229 | static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k) |
2229 | static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k), also, static! |
2230 | smooth_key skey; |
|
|
2231 | int pl_x, pl_y; |
2230 | int pl_x, pl_y; |
2232 | maptex pl_tex; |
2231 | maptex pl_tex; |
2233 | rc_t *rc = rc_alloc (); |
2232 | rc_t *rc = rc_alloc (); |
2234 | rc_t *rc_ov = rc_alloc (); |
2233 | rc_t *rc_ov = rc_alloc (); |
2235 | rc_key_t key; |
2234 | rc_key_t key; |
… | |
… | |
2238 | pl_tex.name = 0; |
2237 | pl_tex.name = 0; |
2239 | |
2238 | |
2240 | // that's current max. sorry. |
2239 | // that's current max. sorry. |
2241 | if (sw > 255) sw = 255; |
2240 | if (sw > 255) sw = 255; |
2242 | if (sh > 255) sh = 255; |
2241 | if (sh > 255) sh = 255; |
2243 | |
|
|
2244 | // clear key, in case of extra padding |
|
|
2245 | memset (&skey, 0, sizeof (skey)); |
|
|
2246 | |
2242 | |
2247 | memset (&key, 0, sizeof (key)); |
2243 | memset (&key, 0, sizeof (key)); |
2248 | key.r = 255; |
2244 | key.r = 255; |
2249 | key.g = 255; |
2245 | key.g = 255; |
2250 | key.b = 255; |
2246 | key.b = 255; |
… | |
… | |
2256 | my += self->y; |
2252 | my += self->y; |
2257 | |
2253 | |
2258 | // first pass: determine smooth_max |
2254 | // first pass: determine smooth_max |
2259 | // rather ugly, if you ask me |
2255 | // rather ugly, if you ask me |
2260 | // could also be stored inside mapcell and updated on change |
2256 | // could also be stored inside mapcell and updated on change |
2261 | memset (smooth_max, 0, sizeof (smooth_max)); |
2257 | memset (smooth_max, 0, sizeof (smooth_max[0]) * (sh + 1)); |
2262 | |
2258 | |
2263 | for (y = 0; y < sh; y++) |
2259 | for (y = 0; y < sh; y++) |
2264 | if (0 <= y + my && y + my < self->rows) |
2260 | if (0 <= y + my && y + my < self->rows) |
2265 | { |
2261 | { |
2266 | maprow *row = self->row + (y + my); |
2262 | maprow *row = self->row + (y + my); |
… | |
… | |
2281 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
2277 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
2282 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
2278 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
2283 | |
2279 | |
2284 | for (z = 0; z <= 2; z++) |
2280 | for (z = 0; z <= 2; z++) |
2285 | { |
2281 | { |
|
|
2282 | std::bitset<256> smooth_level; // one bit for every possible smooth level |
|
|
2283 | smooth_key skey; |
2286 | smooth_hash smooth; |
2284 | smooth_hash smooth; |
2287 | memset (smooth_level, 0, sizeof (smooth_level)); |
|
|
2288 | key.texname = -1; |
2285 | key.texname = -1; |
2289 | |
2286 | |
2290 | for (y = 0; y < sh; y++) |
2287 | for (y = 0; y < sh; y++) |
2291 | if (0 <= y + my && y + my < self->rows) |
2288 | if (0 <= y + my && y + my < self->rows) |
2292 | { |
2289 | { |
… | |
… | |
2334 | if (tex.smoothtile) |
2331 | if (tex.smoothtile) |
2335 | { |
2332 | { |
2336 | skey.tile = tex.smoothtile; |
2333 | skey.tile = tex.smoothtile; |
2337 | skey.level = tex.smoothlevel; |
2334 | skey.level = tex.smoothlevel; |
2338 | |
2335 | |
2339 | smooth_level [tex.smoothlevel >> 5] |= ((uint32_t)1) << (tex.smoothlevel & 31); |
2336 | smooth_level[tex.smoothlevel] = 1; |
2340 | |
2337 | |
2341 | // add bits to current tile and all neighbours. skey.x|y is |
2338 | // add bits to current tile and all neighbours. skey.x|y is |
2342 | // shifted +1|+1 so we always stay positive. |
2339 | // shifted +1|+1 so we always stay positive. |
2343 | |
2340 | |
2344 | // bits is ___n cccc CCCC bbbb |
2341 | // bits is ___n cccc CCCC bbbb |
… | |
… | |
2401 | { |
2398 | { |
2402 | int w, b; |
2399 | int w, b; |
2403 | |
2400 | |
2404 | glEnable (GL_TEXTURE_2D); |
2401 | glEnable (GL_TEXTURE_2D); |
2405 | glBegin (GL_QUADS); |
2402 | glBegin (GL_QUADS); |
2406 | for (w = 0; w < 256 / 32; ++w) |
2403 | for (int level = 0; level < smooth_level.size (); ++level) |
|
|
2404 | if (smooth_level[level]) |
|
|
2405 | for (auto &&it = smooth.begin (); it != smooth.end (); ++it) |
2407 | { |
2406 | { |
2408 | uint32_t smask = smooth_level [w]; |
2407 | smooth_key &skey = it->first; |
2409 | if (smask) |
2408 | IV bits = it->second; |
2410 | for (b = 0; b < 32; ++b) |
2409 | |
2411 | if (smask & (((uint32_t)1) << b)) |
2410 | if (!(bits & 0x1000) |
|
|
2411 | && skey.level == level |
|
|
2412 | && level > smooth_max [skey.x][skey.y]) |
2412 | { |
2413 | { |
2413 | int level = (w << 5) | b; |
2414 | maptex tex = self->tex [skey.tile]; |
|
|
2415 | int px = (((int)skey.x) - 1) * Tw; |
|
|
2416 | int py = (((int)skey.y) - 1) * Th; |
|
|
2417 | int border = bits & 15; |
|
|
2418 | int corner = (bits >> 8) & ~(bits >> 4) & 15; |
|
|
2419 | float dx = tex.s * .0625f; // 16 images/row |
|
|
2420 | float dy = tex.t * .5f ; // 2 images/column |
|
|
2421 | |
2414 | HE *he; |
2422 | if (tex.name) |
2415 | |
|
|
2416 | for (auto &&it = smooth.begin (); it != smooth.end (); ++it) |
|
|
2417 | { |
2423 | { |
2418 | smooth_key &skey = it->first; |
2424 | // this time avoiding texture state changes |
2419 | IV bits = it->second; |
2425 | // save gobs of state changes. |
2420 | |
2426 | if (key.texname != tex.name) |
2421 | if (!(bits & 0x1000) |
|
|
2422 | && skey.level == level |
|
|
2423 | && level > smooth_max [skey.x][skey.y]) |
|
|
2424 | { |
2427 | { |
2425 | maptex tex = self->tex [skey.tile]; |
|
|
2426 | int px = (((int)skey.x) - 1) * Tw; |
|
|
2427 | int py = (((int)skey.y) - 1) * Th; |
|
|
2428 | int border = bits & 15; |
|
|
2429 | int corner = (bits >> 8) & ~(bits >> 4) & 15; |
|
|
2430 | float dx = tex.s * .0625f; // 16 images/row |
|
|
2431 | float dy = tex.t * .5f ; // 2 images/column |
|
|
2432 | |
|
|
2433 | if (tex.name) |
|
|
2434 | { |
|
|
2435 | // this time avoiding texture state changes |
|
|
2436 | // save gobs of state changes. |
|
|
2437 | if (key.texname != tex.name) |
|
|
2438 | { |
|
|
2439 | self->tex [skey.tile].unused = 0; |
2428 | self->tex [skey.tile].unused = 0; |
2440 | |
2429 | |
2441 | glEnd (); |
2430 | glEnd (); |
2442 | glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); |
2431 | glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); |
2443 | glBegin (GL_QUADS); |
2432 | glBegin (GL_QUADS); |
2444 | } |
2433 | } |
2445 | |
2434 | |
2446 | if (border) |
2435 | if (border) |
2447 | { |
2436 | { |
2448 | float ox = border * dx; |
2437 | float ox = border * dx; |
2449 | |
2438 | |
2450 | glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); |
2439 | glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); |
2451 | glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); |
2440 | glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); |
2452 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); |
2441 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); |
2453 | glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); |
2442 | glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); |
2454 | } |
2443 | } |
2455 | |
2444 | |
2456 | if (corner) |
2445 | if (corner) |
2457 | { |
2446 | { |
2458 | float ox = corner * dx; |
2447 | float ox = corner * dx; |
2459 | |
2448 | |
2460 | glTexCoord2f (ox , dy ); glVertex2i (px , py ); |
2449 | glTexCoord2f (ox , dy ); glVertex2i (px , py ); |
2461 | glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); |
2450 | glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); |
2462 | glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); |
2451 | glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); |
2463 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); |
2452 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); |
2464 | } |
|
|
2465 | } |
|
|
2466 | } |
2453 | } |
2467 | } |
2454 | } |
2468 | } |
2455 | } |
2469 | } |
2456 | } |
2470 | |
2457 | |
2471 | glEnd (); |
2458 | glEnd (); |
2472 | glDisable (GL_TEXTURE_2D); |
2459 | glDisable (GL_TEXTURE_2D); |
2473 | key.texname = -1; |
2460 | key.texname = -1; |
2474 | } |
2461 | } |