… | |
… | |
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> |
… | |
… | |
204 | struct cf_layout { |
205 | struct cf_layout { |
205 | PangoLayout *pl; |
206 | PangoLayout *pl; |
206 | float r, g, b, a; // default color for rgba mode |
207 | float r, g, b, a; // default color for rgba mode |
207 | int base_height; |
208 | int base_height; |
208 | DC__Font font; |
209 | DC__Font font; |
209 | rc_t *rc; |
210 | rc_t rc; |
210 | }; |
211 | }; |
211 | |
212 | |
212 | typedef cf_layout *DC__Layout; |
213 | typedef cf_layout *DC__Layout; |
213 | |
214 | |
214 | static DC__Font default_font; |
215 | static DC__Font default_font; |
… | |
… | |
1368 | tc_restore (); |
1369 | tc_restore (); |
1369 | |
1370 | |
1370 | DC::Layout |
1371 | DC::Layout |
1371 | new (SV *klass) |
1372 | new (SV *klass) |
1372 | CODE: |
1373 | CODE: |
1373 | New (0, RETVAL, 1, struct cf_layout); |
1374 | RETVAL = new cf_layout; |
1374 | |
1375 | |
1375 | RETVAL->pl = pango_layout_new (opengl_context); |
1376 | RETVAL->pl = pango_layout_new (opengl_context); |
1376 | RETVAL->r = 1.; |
1377 | RETVAL->r = 1.; |
1377 | RETVAL->g = 1.; |
1378 | RETVAL->g = 1.; |
1378 | RETVAL->b = 1.; |
1379 | RETVAL->b = 1.; |
1379 | RETVAL->a = 1.; |
1380 | RETVAL->a = 1.; |
1380 | RETVAL->base_height = MIN_FONT_HEIGHT; |
1381 | RETVAL->base_height = MIN_FONT_HEIGHT; |
1381 | RETVAL->font = 0; |
1382 | RETVAL->font = 0; |
1382 | RETVAL->rc = rc_alloc (); |
|
|
1383 | |
1383 | |
1384 | pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); |
1384 | pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); |
1385 | layout_update_font (RETVAL); |
1385 | layout_update_font (RETVAL); |
1386 | OUTPUT: |
1386 | OUTPUT: |
1387 | RETVAL |
1387 | RETVAL |
1388 | |
1388 | |
1389 | void |
1389 | void |
1390 | DESTROY (DC::Layout self) |
1390 | DESTROY (DC::Layout self) |
1391 | CODE: |
1391 | CODE: |
1392 | g_object_unref (self->pl); |
1392 | g_object_unref (self->pl); |
1393 | rc_free (self->rc); |
|
|
1394 | Safefree (self); |
1393 | delete self; |
1395 | |
1394 | |
1396 | void |
1395 | void |
1397 | set_text (DC::Layout self, SV *text_) |
1396 | set_text (DC::Layout self, SV *text_) |
1398 | CODE: |
1397 | CODE: |
1399 | { |
1398 | { |
… | |
… | |
1653 | } |
1652 | } |
1654 | |
1653 | |
1655 | void |
1654 | void |
1656 | render (DC::Layout self, float x, float y, int flags = 0) |
1655 | render (DC::Layout self, float x, float y, int flags = 0) |
1657 | CODE: |
1656 | CODE: |
1658 | rc_clear (self->rc); |
1657 | self->rc.clear (); |
1659 | pango_opengl_render_layout_subpixel ( |
1658 | pango_opengl_render_layout_subpixel ( |
1660 | self->pl, |
1659 | self->pl, |
1661 | self->rc, |
1660 | &self->rc, |
1662 | x * PANGO_SCALE, y * PANGO_SCALE, |
1661 | x * PANGO_SCALE, y * PANGO_SCALE, |
1663 | self->r, self->g, self->b, self->a, |
1662 | self->r, self->g, self->b, self->a, |
1664 | flags |
1663 | flags |
1665 | ); |
1664 | ); |
1666 | // we assume that context_change actually clears/frees stuff |
1665 | // we assume that context_change actually clears/frees stuff |
… | |
… | |
1677 | gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, |
1676 | gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, |
1678 | GL_ONE , GL_ONE_MINUS_SRC_ALPHA); |
1677 | GL_ONE , GL_ONE_MINUS_SRC_ALPHA); |
1679 | glEnable (GL_ALPHA_TEST); |
1678 | glEnable (GL_ALPHA_TEST); |
1680 | glAlphaFunc (GL_GREATER, 7.f / 255.f); |
1679 | glAlphaFunc (GL_GREATER, 7.f / 255.f); |
1681 | |
1680 | |
1682 | rc_draw (self->rc); |
1681 | self->rc.draw (); |
1683 | |
1682 | |
1684 | glDisable (GL_ALPHA_TEST); |
1683 | glDisable (GL_ALPHA_TEST); |
1685 | glDisable (GL_BLEND); |
1684 | glDisable (GL_BLEND); |
1686 | glDisable (GL_TEXTURE_2D); |
1685 | glDisable (GL_TEXTURE_2D); |
1687 | } |
1686 | } |
… | |
… | |
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) |
2222 | 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: |
2223 | CODE: |
2225 | { |
2224 | { |
2226 | int x, y, z; |
2225 | int x, y, z; |
2227 | |
2226 | |
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) |
2227 | 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; |
2228 | int pl_x, pl_y; |
2232 | maptex pl_tex; |
2229 | maptex pl_tex; |
2233 | rc_t *rc = rc_alloc (); |
2230 | rc_t rc; |
2234 | rc_t *rc_ov = rc_alloc (); |
2231 | rc_t rc_ov; |
2235 | rc_key_t key; |
2232 | rc_key_t key; |
2236 | rc_array_t *arr; |
2233 | rc_t::array_t *arr; |
2237 | |
2234 | |
2238 | pl_tex.name = 0; |
2235 | pl_tex.name = 0; |
2239 | |
2236 | |
2240 | // that's current max. sorry. |
2237 | // that's current max. sorry. |
2241 | if (sw > 255) sw = 255; |
2238 | if (sw > 255) sw = 255; |
2242 | if (sh > 255) sh = 255; |
2239 | if (sh > 255) sh = 255; |
2243 | |
|
|
2244 | // clear key, in case of extra padding |
|
|
2245 | memset (&skey, 0, sizeof (skey)); |
|
|
2246 | |
2240 | |
2247 | memset (&key, 0, sizeof (key)); |
2241 | memset (&key, 0, sizeof (key)); |
2248 | key.r = 255; |
2242 | key.r = 255; |
2249 | key.g = 255; |
2243 | key.g = 255; |
2250 | key.b = 255; |
2244 | key.b = 255; |
… | |
… | |
2256 | my += self->y; |
2250 | my += self->y; |
2257 | |
2251 | |
2258 | // first pass: determine smooth_max |
2252 | // first pass: determine smooth_max |
2259 | // rather ugly, if you ask me |
2253 | // rather ugly, if you ask me |
2260 | // could also be stored inside mapcell and updated on change |
2254 | // could also be stored inside mapcell and updated on change |
2261 | memset (smooth_max, 0, sizeof (smooth_max)); |
2255 | memset (smooth_max, 0, sizeof (smooth_max[0]) * (sh + 1)); |
2262 | |
2256 | |
2263 | for (y = 0; y < sh; y++) |
2257 | for (y = 0; y < sh; y++) |
2264 | if (0 <= y + my && y + my < self->rows) |
2258 | if (0 <= y + my && y + my < self->rows) |
2265 | { |
2259 | { |
2266 | maprow *row = self->row + (y + my); |
2260 | maprow *row = self->row + (y + my); |
… | |
… | |
2281 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
2275 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
2282 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
2276 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
2283 | |
2277 | |
2284 | for (z = 0; z <= 2; z++) |
2278 | for (z = 0; z <= 2; z++) |
2285 | { |
2279 | { |
|
|
2280 | std::bitset<256> smooth_level; // one bit for every possible smooth level |
|
|
2281 | smooth_key skey; |
2286 | smooth_hash smooth; |
2282 | smooth_hash smooth; |
2287 | memset (smooth_level, 0, sizeof (smooth_level)); |
|
|
2288 | key.texname = -1; |
2283 | key.texname = -1; |
2289 | |
2284 | |
2290 | for (y = 0; y < sh; y++) |
2285 | for (y = 0; y < sh; y++) |
2291 | if (0 <= y + my && y + my < self->rows) |
2286 | if (0 <= y + my && y + my < self->rows) |
2292 | { |
2287 | { |
… | |
… | |
2309 | |
2304 | |
2310 | if (!tex.name) |
2305 | if (!tex.name) |
2311 | tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */ |
2306 | tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */ |
2312 | |
2307 | |
2313 | key.texname = tex.name; |
2308 | key.texname = tex.name; |
2314 | arr = rc_array (rc, &key); |
2309 | arr = &rc.array (key); |
2315 | } |
2310 | } |
2316 | |
2311 | |
2317 | px = (x + 1) * Th - tex.w; |
2312 | px = (x + 1) * Th - tex.w; |
2318 | py = (y + 1) * Tw - tex.h; |
2313 | py = (y + 1) * Tw - tex.h; |
2319 | |
2314 | |
… | |
… | |
2323 | pl_y = py; |
2318 | pl_y = py; |
2324 | pl_tex = tex; |
2319 | pl_tex = tex; |
2325 | continue; |
2320 | continue; |
2326 | } |
2321 | } |
2327 | |
2322 | |
2328 | rc_t2f_v3f (arr, 0 , 0 , px , py , 0); |
2323 | arr->t2f_v3f (0 , 0 , px , py , 0); |
2329 | rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); |
2324 | arr->t2f_v3f (0 , tex.t, px , py + tex.h, 0); |
2330 | rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0); |
2325 | arr->t2f_v3f (tex.s, tex.t, px + tex.w, py + tex.h, 0); |
2331 | rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); |
2326 | arr->t2f_v3f (tex.s, 0 , px + tex.w, py , 0); |
2332 | |
2327 | |
2333 | // update smooth hash |
2328 | // update smooth hash |
2334 | if (tex.smoothtile) |
2329 | if (tex.smoothtile) |
2335 | { |
2330 | { |
2336 | skey.tile = tex.smoothtile; |
2331 | skey.tile = tex.smoothtile; |
2337 | skey.level = tex.smoothlevel; |
2332 | skey.level = tex.smoothlevel; |
2338 | |
2333 | |
2339 | smooth_level [tex.smoothlevel >> 5] |= ((uint32_t)1) << (tex.smoothlevel & 31); |
2334 | smooth_level[tex.smoothlevel] = 1; |
2340 | |
2335 | |
2341 | // add bits to current tile and all neighbours. skey.x|y is |
2336 | // add bits to current tile and all neighbours. skey.x|y is |
2342 | // shifted +1|+1 so we always stay positive. |
2337 | // shifted +1|+1 so we always stay positive. |
2343 | |
2338 | |
2344 | // bits is ___n cccc CCCC bbbb |
2339 | // bits is ___n cccc CCCC bbbb |
… | |
… | |
2374 | { |
2369 | { |
2375 | // overlays such as the speech bubble, probably more to come |
2370 | // overlays such as the speech bubble, probably more to come |
2376 | if (cell->flags & 1) |
2371 | if (cell->flags & 1) |
2377 | { |
2372 | { |
2378 | rc_key_t key_ov = key; |
2373 | rc_key_t key_ov = key; |
2379 | maptex tex = self->tex [TEXID_SPEECH]; |
2374 | maptex tex = self->tex[TEXID_SPEECH]; |
2380 | rc_array_t *arr; |
|
|
2381 | int px = x * Tw + Tw * 2 / 32; |
2375 | int px = x * Tw + Tw * 2 / 32; |
2382 | int py = y * Th - Th * 6 / 32; |
2376 | int py = y * Th - Th * 6 / 32; |
2383 | |
2377 | |
2384 | key_ov.texname = tex.name; |
2378 | key_ov.texname = tex.name; |
2385 | arr = rc_array (rc_ov, &key_ov); |
2379 | rc_t::array_t &arr = rc_ov.array (key_ov); |
2386 | |
2380 | |
2387 | rc_t2f_v3f (arr, 0 , 0 , px , py , 0); |
2381 | arr.t2f_v3f (0 , 0 , px , py , 0); |
2388 | rc_t2f_v3f (arr, 0 , tex.t, px , py + Th, 0); |
2382 | arr.t2f_v3f (0 , tex.t, px , py + Th, 0); |
2389 | rc_t2f_v3f (arr, tex.s, tex.t, px + Tw, py + Th, 0); |
2383 | arr.t2f_v3f (tex.s, tex.t, px + Tw, py + Th, 0); |
2390 | rc_t2f_v3f (arr, tex.s, 0 , px + Tw, py , 0); |
2384 | arr.t2f_v3f (tex.s, 0 , px + Tw, py , 0); |
2391 | } |
2385 | } |
2392 | } |
2386 | } |
2393 | } |
2387 | } |
2394 | } |
2388 | } |
2395 | |
2389 | |
2396 | rc_draw (rc); |
2390 | rc.draw (); |
2397 | rc_clear (rc); |
2391 | rc.clear (); |
2398 | |
2392 | |
2399 | // go through all smoothlevels, lowest to highest, then draw. |
2393 | // go through all smoothlevels, lowest to highest, then draw. |
2400 | // this is basically counting sort |
2394 | // this is basically counting sort |
2401 | { |
2395 | { |
2402 | int w, b; |
2396 | int w, b; |
2403 | |
2397 | |
2404 | glEnable (GL_TEXTURE_2D); |
2398 | glEnable (GL_TEXTURE_2D); |
2405 | glBegin (GL_QUADS); |
2399 | glBegin (GL_QUADS); |
2406 | for (w = 0; w < 256 / 32; ++w) |
2400 | for (int level = 0; level < smooth_level.size (); ++level) |
|
|
2401 | if (smooth_level[level]) |
|
|
2402 | for (auto &&it = smooth.begin (); it != smooth.end (); ++it) |
2407 | { |
2403 | { |
2408 | uint32_t smask = smooth_level [w]; |
2404 | smooth_key &skey = it->first; |
2409 | if (smask) |
2405 | IV bits = it->second; |
2410 | for (b = 0; b < 32; ++b) |
2406 | |
2411 | if (smask & (((uint32_t)1) << b)) |
2407 | if (!(bits & 0x1000) |
|
|
2408 | && skey.level == level |
|
|
2409 | && level > smooth_max [skey.x][skey.y]) |
2412 | { |
2410 | { |
2413 | int level = (w << 5) | b; |
2411 | maptex tex = self->tex [skey.tile]; |
|
|
2412 | int px = (((int)skey.x) - 1) * Tw; |
|
|
2413 | int py = (((int)skey.y) - 1) * Th; |
|
|
2414 | int border = bits & 15; |
|
|
2415 | int corner = (bits >> 8) & ~(bits >> 4) & 15; |
|
|
2416 | float dx = tex.s * .0625f; // 16 images/row |
|
|
2417 | float dy = tex.t * .5f ; // 2 images/column |
|
|
2418 | |
2414 | HE *he; |
2419 | if (tex.name) |
2415 | |
|
|
2416 | for (auto &&it = smooth.begin (); it != smooth.end (); ++it) |
|
|
2417 | { |
2420 | { |
2418 | smooth_key &skey = it->first; |
2421 | // this time avoiding texture state changes |
2419 | IV bits = it->second; |
2422 | // save gobs of state changes. |
2420 | |
2423 | if (key.texname != tex.name) |
2421 | if (!(bits & 0x1000) |
|
|
2422 | && skey.level == level |
|
|
2423 | && level > smooth_max [skey.x][skey.y]) |
|
|
2424 | { |
2424 | { |
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; |
2425 | self->tex [skey.tile].unused = 0; |
2440 | |
2426 | |
2441 | glEnd (); |
2427 | glEnd (); |
2442 | glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); |
2428 | glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); |
2443 | glBegin (GL_QUADS); |
2429 | glBegin (GL_QUADS); |
2444 | } |
2430 | } |
2445 | |
2431 | |
2446 | if (border) |
2432 | if (border) |
2447 | { |
2433 | { |
2448 | float ox = border * dx; |
2434 | float ox = border * dx; |
2449 | |
2435 | |
2450 | glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); |
2436 | glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); |
2451 | glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); |
2437 | glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); |
2452 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); |
2438 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); |
2453 | glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); |
2439 | glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); |
2454 | } |
2440 | } |
2455 | |
2441 | |
2456 | if (corner) |
2442 | if (corner) |
2457 | { |
2443 | { |
2458 | float ox = corner * dx; |
2444 | float ox = corner * dx; |
2459 | |
2445 | |
2460 | glTexCoord2f (ox , dy ); glVertex2i (px , py ); |
2446 | glTexCoord2f (ox , dy ); glVertex2i (px , py ); |
2461 | glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); |
2447 | glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); |
2462 | glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); |
2448 | glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); |
2463 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); |
2449 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); |
2464 | } |
|
|
2465 | } |
|
|
2466 | } |
2450 | } |
2467 | } |
2451 | } |
2468 | } |
2452 | } |
2469 | } |
2453 | } |
2470 | |
2454 | |
2471 | glEnd (); |
2455 | glEnd (); |
2472 | glDisable (GL_TEXTURE_2D); |
2456 | glDisable (GL_TEXTURE_2D); |
2473 | key.texname = -1; |
2457 | key.texname = -1; |
2474 | } |
2458 | } |
… | |
… | |
2479 | maptex tex = pl_tex; |
2463 | maptex tex = pl_tex; |
2480 | int px = pl_x + sdx; |
2464 | int px = pl_x + sdx; |
2481 | int py = pl_y + sdy; |
2465 | int py = pl_y + sdy; |
2482 | |
2466 | |
2483 | key.texname = tex.name; |
2467 | key.texname = tex.name; |
2484 | arr = rc_array (rc, &key); |
2468 | rc_t::array_t &arr = rc.array (key); |
2485 | |
2469 | |
2486 | rc_t2f_v3f (arr, 0 , 0 , px , py , 0); |
2470 | arr.t2f_v3f (0 , 0 , px , py , 0); |
2487 | rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); |
2471 | arr.t2f_v3f (0 , tex.t, px , py + tex.h, 0); |
2488 | rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0); |
2472 | arr.t2f_v3f (tex.s, tex.t, px + tex.w, py + tex.h, 0); |
2489 | rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); |
2473 | arr.t2f_v3f (tex.s, 0 , px + tex.w, py , 0); |
2490 | |
2474 | |
2491 | rc_draw (rc); |
2475 | rc.draw (); |
2492 | } |
2476 | } |
2493 | |
2477 | |
2494 | rc_draw (rc_ov); |
2478 | rc_ov.draw (); |
2495 | rc_clear (rc_ov); |
2479 | rc_ov.clear (); |
2496 | |
2480 | |
2497 | glDisable (GL_BLEND); |
2481 | glDisable (GL_BLEND); |
2498 | rc_free (rc); |
|
|
2499 | rc_free (rc_ov); |
|
|
2500 | |
2482 | |
2501 | // top layer: overlays such as the health bar |
2483 | // top layer: overlays such as the health bar |
2502 | for (y = 0; y < sh; y++) |
2484 | for (y = 0; y < sh; y++) |
2503 | if (0 <= y + my && y + my < self->rows) |
2485 | if (0 <= y + my && y + my < self->rows) |
2504 | { |
2486 | { |