ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
(Generate patch)

Comparing deliantra/Deliantra-Client/Client.xs (file contents):
Revision 1.324 by root, Sun Nov 18 01:58:53 2018 UTC vs.
Revision 1.330 by root, Sun Nov 18 15:24:24 2018 UTC

205struct cf_layout { 205struct cf_layout {
206 PangoLayout *pl; 206 PangoLayout *pl;
207 float r, g, b, a; // default color for rgba mode 207 float r, g, b, a; // default color for rgba mode
208 int base_height; 208 int base_height;
209 DC__Font font; 209 DC__Font font;
210 rc_t *rc; 210 rc_t rc;
211}; 211};
212 212
213typedef cf_layout *DC__Layout; 213typedef cf_layout *DC__Layout;
214 214
215static DC__Font default_font; 215static DC__Font default_font;
296 maprow *row; 296 maprow *row;
297}; 297};
298 298
299typedef mapgrid *DC__Map; 299typedef mapgrid *DC__Map;
300 300
301static char * 301template<typename T>
302static void
302prepend (char *ptr, int sze, int inc) 303prepend (T *&ptr, int sze, int inc)
303{ 304{
304 char *p; 305 T *p;
305 306
306 New (0, p, sze + inc, char); 307 Newxz (p, sze + inc, T);
307 Zero (p, inc, char);
308 Move (ptr, p + inc, sze, char); 308 Move (ptr, p + inc, sze, T);
309 Safefree (ptr); 309 Safefree (ptr);
310 310
311 return p; 311 ptr = p;
312} 312}
313 313
314static char * 314template<typename T>
315static void
315append (char *ptr, int sze, int inc) 316append (T *&ptr, int sze, int inc)
316{ 317{
317 Renew (ptr, sze + inc, char); 318 Renew (ptr, sze + inc, T);
318 Zero (ptr + sze, inc, char); 319 Zero (ptr + sze, inc, T);
319
320 return ptr;
321} 320}
322
323#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type))
324#define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type))
325 321
326static void 322static void
327need_facenum (struct mapgrid *self, faceid face) 323need_facenum (struct mapgrid *self, faceid face)
328{ 324{
329 while (self->faces <= face) 325 while (self->faces <= face)
330 { 326 {
331 Append (tileid, self->face2tile, self->faces, self->faces); 327 append (self->face2tile, self->faces, self->faces);
332 self->faces *= 2; 328 self->faces *= 2;
333 } 329 }
334} 330}
335 331
336static void 332static void
337need_texid (struct mapgrid *self, int texid) 333need_texid (struct mapgrid *self, int texid)
338{ 334{
339 while (self->texs <= texid) 335 while (self->texs <= texid)
340 { 336 {
341 Append (maptex, self->tex, self->texs, self->texs); 337 append (self->tex, self->texs, self->texs);
342 self->texs *= 2; 338 self->texs *= 2;
343 } 339 }
344} 340}
345 341
346static maprow * 342static maprow *
347map_get_row (mapgrid *self, int y) 343map_get_row (mapgrid *self, int y)
348{ 344{
349 if (0 > y) 345 if (0 > y)
350 { 346 {
351 int extend = - y + MAP_EXTEND_Y; 347 int extend = - y + MAP_EXTEND_Y;
352 Prepend (maprow, self->row, self->rows, extend); 348 prepend (self->row, self->rows, extend);
353 349
354 self->rows += extend; 350 self->rows += extend;
355 self->y += extend; 351 self->y += extend;
356 y += extend; 352 y += extend;
357 } 353 }
358 else if (y >= self->rows) 354 else if (y >= self->rows)
359 { 355 {
360 int extend = y - self->rows + MAP_EXTEND_Y; 356 int extend = y - self->rows + MAP_EXTEND_Y;
361 Append (maprow, self->row, self->rows, extend); 357 append (self->row, self->rows, extend);
362 self->rows += extend; 358 self->rows += extend;
363 } 359 }
364 360
365 return self->row + y; 361 return self->row + y;
366} 362}
376 } 372 }
377 373
378 if (row->c0 > x) 374 if (row->c0 > x)
379 { 375 {
380 int extend = row->c0 - x + MAP_EXTEND_X; 376 int extend = row->c0 - x + MAP_EXTEND_X;
381 Prepend (mapcell, row->col, row->c1 - row->c0, extend); 377 prepend (row->col, row->c1 - row->c0, extend);
382 row->c0 -= extend; 378 row->c0 -= extend;
383 } 379 }
384 else if (x >= row->c1) 380 else if (x >= row->c1)
385 { 381 {
386 int extend = x - row->c1 + MAP_EXTEND_X; 382 int extend = x - row->c1 + MAP_EXTEND_X;
387 Append (mapcell, row->col, row->c1 - row->c0, extend); 383 append (row->col, row->c1 - row->c0, extend);
388 row->c1 += extend; 384 row->c1 += extend;
389 } 385 }
390 386
391 return row->col + (x - row->c0); 387 return row->col + (x - row->c0);
392} 388}
1369 tc_restore (); 1365 tc_restore ();
1370 1366
1371DC::Layout 1367DC::Layout
1372new (SV *klass) 1368new (SV *klass)
1373 CODE: 1369 CODE:
1374 New (0, RETVAL, 1, struct cf_layout); 1370 RETVAL = new cf_layout;
1375 1371
1376 RETVAL->pl = pango_layout_new (opengl_context); 1372 RETVAL->pl = pango_layout_new (opengl_context);
1377 RETVAL->r = 1.; 1373 RETVAL->r = 1.;
1378 RETVAL->g = 1.; 1374 RETVAL->g = 1.;
1379 RETVAL->b = 1.; 1375 RETVAL->b = 1.;
1380 RETVAL->a = 1.; 1376 RETVAL->a = 1.;
1381 RETVAL->base_height = MIN_FONT_HEIGHT; 1377 RETVAL->base_height = MIN_FONT_HEIGHT;
1382 RETVAL->font = 0; 1378 RETVAL->font = 0;
1383 RETVAL->rc = rc_alloc ();
1384 1379
1385 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); 1380 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
1386 layout_update_font (RETVAL); 1381 layout_update_font (RETVAL);
1387 OUTPUT: 1382 OUTPUT:
1388 RETVAL 1383 RETVAL
1389 1384
1390void 1385void
1391DESTROY (DC::Layout self) 1386DESTROY (DC::Layout self)
1392 CODE: 1387 CODE:
1393 g_object_unref (self->pl); 1388 g_object_unref (self->pl);
1394 rc_free (self->rc);
1395 Safefree (self); 1389 delete self;
1396 1390
1397void 1391void
1398set_text (DC::Layout self, SV *text_) 1392set_text (DC::Layout self, SV *text_)
1399 CODE: 1393 CODE:
1400{ 1394{
1654} 1648}
1655 1649
1656void 1650void
1657render (DC::Layout self, float x, float y, int flags = 0) 1651render (DC::Layout self, float x, float y, int flags = 0)
1658 CODE: 1652 CODE:
1659 rc_clear (self->rc); 1653 self->rc.clear ();
1660 pango_opengl_render_layout_subpixel ( 1654 pango_opengl_render_layout_subpixel (
1661 self->pl, 1655 self->pl,
1662 self->rc, 1656 &self->rc,
1663 x * PANGO_SCALE, y * PANGO_SCALE, 1657 x * PANGO_SCALE, y * PANGO_SCALE,
1664 self->r, self->g, self->b, self->a, 1658 self->r, self->g, self->b, self->a,
1665 flags 1659 flags
1666 ); 1660 );
1667 // we assume that context_change actually clears/frees stuff 1661 // we assume that context_change actually clears/frees stuff
1678 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1672 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
1679 GL_ONE , GL_ONE_MINUS_SRC_ALPHA); 1673 GL_ONE , GL_ONE_MINUS_SRC_ALPHA);
1680 glEnable (GL_ALPHA_TEST); 1674 glEnable (GL_ALPHA_TEST);
1681 glAlphaFunc (GL_GREATER, 7.f / 255.f); 1675 glAlphaFunc (GL_GREATER, 7.f / 255.f);
1682 1676
1683 rc_draw (self->rc); 1677 self->rc.draw ();
1684 1678
1685 glDisable (GL_ALPHA_TEST); 1679 glDisable (GL_ALPHA_TEST);
1686 glDisable (GL_BLEND); 1680 glDisable (GL_BLEND);
1687 glDisable (GL_TEXTURE_2D); 1681 glDisable (GL_TEXTURE_2D);
1688} 1682}
2058 self->ox += dx; self->x += dx; 2052 self->ox += dx; self->x += dx;
2059 self->oy += dy; self->y += dy; 2053 self->oy += dy; self->y += dy;
2060 2054
2061 while (self->y < 0) 2055 while (self->y < 0)
2062 { 2056 {
2063 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); 2057 prepend (self->row, self->rows, MAP_EXTEND_Y);
2064 2058
2065 self->rows += MAP_EXTEND_Y; 2059 self->rows += MAP_EXTEND_Y;
2066 self->y += MAP_EXTEND_Y; 2060 self->y += MAP_EXTEND_Y;
2067 } 2061 }
2068} 2062}
2227 int x, y, z; 2221 int x, y, z;
2228 2222
2229 static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k), also, static! 2223 static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k), also, static!
2230 int pl_x, pl_y; 2224 int pl_x, pl_y;
2231 maptex pl_tex; 2225 maptex pl_tex;
2232 rc_t *rc = rc_alloc (); 2226 rc_t rc;
2233 rc_t *rc_ov = rc_alloc (); 2227 rc_t rc_ov;
2234 rc_key_t key; 2228 rc_key_t key;
2235 rc_array_t *arr; 2229 rc_t::array_t *arr;
2236 2230
2237 pl_tex.name = 0; 2231 pl_tex.name = 0;
2238 2232
2239 // that's current max. sorry. 2233 // that's current max. sorry.
2240 if (sw > 255) sw = 255; 2234 if (sw > 255) sw = 255;
2277 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 2271 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2278 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 2272 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2279 2273
2280 for (z = 0; z <= 2; z++) 2274 for (z = 0; z <= 2; z++)
2281 { 2275 {
2282 uint32_t smooth_level[256 / 32]; // one bit for every possible smooth level 2276 std::bitset<256> smooth_level; // one bit for every possible smooth level
2283 smooth_key skey; 2277 smooth_key skey;
2284 smooth_hash smooth; 2278 smooth_hash smooth;
2285 memset (smooth_level, 0, sizeof (smooth_level));
2286 key.texname = -1; 2279 key.texname = -1;
2287 2280
2288 for (y = 0; y < sh; y++) 2281 for (y = 0; y < sh; y++)
2289 if (0 <= y + my && y + my < self->rows) 2282 if (0 <= y + my && y + my < self->rows)
2290 { 2283 {
2307 2300
2308 if (!tex.name) 2301 if (!tex.name)
2309 tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */ 2302 tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */
2310 2303
2311 key.texname = tex.name; 2304 key.texname = tex.name;
2312 arr = rc_array (rc, &key); 2305 arr = &rc.array (key);
2313 } 2306 }
2314 2307
2315 px = (x + 1) * Th - tex.w; 2308 px = (x + 1) * Th - tex.w;
2316 py = (y + 1) * Tw - tex.h; 2309 py = (y + 1) * Tw - tex.h;
2317 2310
2321 pl_y = py; 2314 pl_y = py;
2322 pl_tex = tex; 2315 pl_tex = tex;
2323 continue; 2316 continue;
2324 } 2317 }
2325 2318
2326 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2319 arr->t2f_v3f (0 , 0 , px , py , 0);
2327 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); 2320 arr->t2f_v3f (0 , tex.t, px , py + tex.h, 0);
2328 rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0); 2321 arr->t2f_v3f (tex.s, tex.t, px + tex.w, py + tex.h, 0);
2329 rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); 2322 arr->t2f_v3f (tex.s, 0 , px + tex.w, py , 0);
2330 2323
2331 // update smooth hash 2324 // update smooth hash
2332 if (tex.smoothtile) 2325 if (tex.smoothtile)
2333 { 2326 {
2334 skey.tile = tex.smoothtile; 2327 skey.tile = tex.smoothtile;
2335 skey.level = tex.smoothlevel; 2328 skey.level = tex.smoothlevel;
2336 2329
2337 smooth_level [tex.smoothlevel >> 5] |= ((uint32_t)1) << (tex.smoothlevel & 31); 2330 smooth_level[tex.smoothlevel] = 1;
2338 2331
2339 // add bits to current tile and all neighbours. skey.x|y is 2332 // add bits to current tile and all neighbours. skey.x|y is
2340 // shifted +1|+1 so we always stay positive. 2333 // shifted +1|+1 so we always stay positive.
2341 2334
2342 // bits is ___n cccc CCCC bbbb 2335 // bits is ___n cccc CCCC bbbb
2372 { 2365 {
2373 // overlays such as the speech bubble, probably more to come 2366 // overlays such as the speech bubble, probably more to come
2374 if (cell->flags & 1) 2367 if (cell->flags & 1)
2375 { 2368 {
2376 rc_key_t key_ov = key; 2369 rc_key_t key_ov = key;
2377 maptex tex = self->tex [TEXID_SPEECH]; 2370 maptex tex = self->tex[TEXID_SPEECH];
2378 rc_array_t *arr;
2379 int px = x * Tw + Tw * 2 / 32; 2371 int px = x * Tw + Tw * 2 / 32;
2380 int py = y * Th - Th * 6 / 32; 2372 int py = y * Th - Th * 6 / 32;
2381 2373
2382 key_ov.texname = tex.name; 2374 key_ov.texname = tex.name;
2383 arr = rc_array (rc_ov, &key_ov); 2375 rc_t::array_t &arr = rc_ov.array (key_ov);
2384 2376
2385 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2377 arr.t2f_v3f (0 , 0 , px , py , 0);
2386 rc_t2f_v3f (arr, 0 , tex.t, px , py + Th, 0); 2378 arr.t2f_v3f (0 , tex.t, px , py + Th, 0);
2387 rc_t2f_v3f (arr, tex.s, tex.t, px + Tw, py + Th, 0); 2379 arr.t2f_v3f (tex.s, tex.t, px + Tw, py + Th, 0);
2388 rc_t2f_v3f (arr, tex.s, 0 , px + Tw, py , 0); 2380 arr.t2f_v3f (tex.s, 0 , px + Tw, py , 0);
2389 } 2381 }
2390 } 2382 }
2391 } 2383 }
2392 } 2384 }
2393 2385
2394 rc_draw (rc); 2386 rc.draw ();
2395 rc_clear (rc); 2387 rc.clear ();
2396 2388
2397 // go through all smoothlevels, lowest to highest, then draw. 2389 // go through all smoothlevels, lowest to highest, then draw.
2398 // this is basically counting sort 2390 // this is basically counting sort
2399 { 2391 {
2400 int w, b; 2392 int w, b;
2401 2393
2402 glEnable (GL_TEXTURE_2D); 2394 glEnable (GL_TEXTURE_2D);
2403 glBegin (GL_QUADS); 2395 glBegin (GL_QUADS);
2404 for (w = 0; w < 256 / 32; ++w) 2396 for (int level = 0; level < smooth_level.size (); ++level)
2397 if (smooth_level[level])
2398 for (auto &&it = smooth.begin (); it != smooth.end (); ++it)
2405 { 2399 {
2406 uint32_t smask = smooth_level [w]; 2400 smooth_key &skey = it->first;
2407 if (smask) 2401 IV bits = it->second;
2408 for (b = 0; b < 32; ++b) 2402
2409 if (smask & (((uint32_t)1) << b)) 2403 if (!(bits & 0x1000)
2404 && skey.level == level
2405 && level > smooth_max [skey.x][skey.y])
2410 { 2406 {
2411 int level = (w << 5) | b; 2407 maptex tex = self->tex [skey.tile];
2408 int px = (((int)skey.x) - 1) * Tw;
2409 int py = (((int)skey.y) - 1) * Th;
2410 int border = bits & 15;
2411 int corner = (bits >> 8) & ~(bits >> 4) & 15;
2412 float dx = tex.s * .0625f; // 16 images/row
2413 float dy = tex.t * .5f ; // 2 images/column
2414
2412 HE *he; 2415 if (tex.name)
2413
2414 for (auto &&it = smooth.begin (); it != smooth.end (); ++it)
2415 { 2416 {
2416 smooth_key &skey = it->first; 2417 // this time avoiding texture state changes
2417 IV bits = it->second; 2418 // save gobs of state changes.
2418 2419 if (key.texname != tex.name)
2419 if (!(bits & 0x1000)
2420 && skey.level == level
2421 && level > smooth_max [skey.x][skey.y])
2422 { 2420 {
2423 maptex tex = self->tex [skey.tile];
2424 int px = (((int)skey.x) - 1) * Tw;
2425 int py = (((int)skey.y) - 1) * Th;
2426 int border = bits & 15;
2427 int corner = (bits >> 8) & ~(bits >> 4) & 15;
2428 float dx = tex.s * .0625f; // 16 images/row
2429 float dy = tex.t * .5f ; // 2 images/column
2430
2431 if (tex.name)
2432 {
2433 // this time avoiding texture state changes
2434 // save gobs of state changes.
2435 if (key.texname != tex.name)
2436 {
2437 self->tex [skey.tile].unused = 0; 2421 self->tex [skey.tile].unused = 0;
2438 2422
2439 glEnd (); 2423 glEnd ();
2440 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); 2424 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name);
2441 glBegin (GL_QUADS); 2425 glBegin (GL_QUADS);
2442 } 2426 }
2443 2427
2444 if (border) 2428 if (border)
2445 { 2429 {
2446 float ox = border * dx; 2430 float ox = border * dx;
2447 2431
2448 glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); 2432 glTexCoord2f (ox , 0.f ); glVertex2i (px , py );
2449 glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); 2433 glTexCoord2f (ox , dy ); glVertex2i (px , py + Th);
2450 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); 2434 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th);
2451 glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); 2435 glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py );
2452 } 2436 }
2453 2437
2454 if (corner) 2438 if (corner)
2455 { 2439 {
2456 float ox = corner * dx; 2440 float ox = corner * dx;
2457 2441
2458 glTexCoord2f (ox , dy ); glVertex2i (px , py ); 2442 glTexCoord2f (ox , dy ); glVertex2i (px , py );
2459 glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); 2443 glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th);
2460 glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); 2444 glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th);
2461 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); 2445 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py );
2462 }
2463 }
2464 } 2446 }
2465 } 2447 }
2466 } 2448 }
2467 } 2449 }
2468 2450
2469 glEnd (); 2451 glEnd ();
2470 glDisable (GL_TEXTURE_2D); 2452 glDisable (GL_TEXTURE_2D);
2471 key.texname = -1; 2453 key.texname = -1;
2472 } 2454 }
2477 maptex tex = pl_tex; 2459 maptex tex = pl_tex;
2478 int px = pl_x + sdx; 2460 int px = pl_x + sdx;
2479 int py = pl_y + sdy; 2461 int py = pl_y + sdy;
2480 2462
2481 key.texname = tex.name; 2463 key.texname = tex.name;
2482 arr = rc_array (rc, &key); 2464 rc_t::array_t &arr = rc.array (key);
2483 2465
2484 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2466 arr.t2f_v3f (0 , 0 , px , py , 0);
2485 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); 2467 arr.t2f_v3f (0 , tex.t, px , py + tex.h, 0);
2486 rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0); 2468 arr.t2f_v3f (tex.s, tex.t, px + tex.w, py + tex.h, 0);
2487 rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); 2469 arr.t2f_v3f (tex.s, 0 , px + tex.w, py , 0);
2488 2470
2489 rc_draw (rc); 2471 rc.draw ();
2490 } 2472 }
2491 2473
2492 rc_draw (rc_ov); 2474 rc_ov.draw ();
2493 rc_clear (rc_ov); 2475 rc_ov.clear ();
2494 2476
2495 glDisable (GL_BLEND); 2477 glDisable (GL_BLEND);
2496 rc_free (rc);
2497 rc_free (rc_ov);
2498 2478
2499 // top layer: overlays such as the health bar 2479 // top layer: overlays such as the health bar
2500 for (y = 0; y < sh; y++) 2480 for (y = 0; y < sh; y++)
2501 if (0 <= y + my && y + my < self->rows) 2481 if (0 <= y + my && y + my < self->rows)
2502 { 2482 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines