DEADSOFTWARE

28c5b86976ccb1cb97f6cf464698034798d88587
[flatwaifu.git] / src / gl / render.c
1 #include "glob.h"
2 #include "render.h"
3 #include "system.h"
4 #include "files.h"
5 #include "memory.h"
6 #include "misc.h"
7 #include "error.h"
9 #include "menu.h"
10 #include "game.h"
11 #include "dots.h"
12 #include "items.h"
14 #include "sound.h" // snd_vol
15 #include "music.h" // mus_vol
17 #include "fx.h"
18 #include "player.h"
19 #include "monster.h"
20 #include "weapons.h"
21 #include "smoke.h"
22 #include "view.h"
23 #include "switch.h" // sw_secrets
25 #include "cp866.h"
27 #ifdef __APPLE__
28 # include <OpenGL/gl.h>
29 #else
30 # include <GL/gl.h>
31 #endif
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <assert.h>
37 #define VGA_TRANSPARENT_COLOR 0
38 #define DEFAULT_SKY_COLOR 0x97
39 #define MANCOLOR 0xD0
40 #define PLAYER_COLOR_OFFSET 7
41 #define MAXAIR 1091
42 #define ANIT 5
43 #define PL_FLASH 90
45 #pragma pack(1)
46 typedef struct vgaimg {
47 word w, h;
48 short x, y;
49 byte data[];
50 } vgaimg;
52 typedef struct rgb {
53 byte r, g, b;
54 } rgb;
56 typedef struct rgba {
57 byte r, g, b, a;
58 } rgba;
59 #pragma pack()
61 typedef struct node {
62 struct cache *base;
63 struct node *left, *right;
64 struct node *up;
65 int l, t, r, b;
66 int leaf;
67 } node;
69 typedef struct cache {
70 struct cache *next;
71 struct node root;
72 GLuint id;
73 } cache;
75 typedef struct image {
76 node *n;
77 GLint x, y;
78 GLuint w, h;
79 int res;
80 } image;
82 /* Render Specific */
83 static int SCRW;
84 static int SCRH;
85 static float screen_scale;
86 static int screen_width = 320;
87 static int screen_height = 200;
88 static byte screen_full = 0;
89 static int init_screen_width = 0;
90 static int init_screen_height = 0;
91 static byte init_screen_full = 0xFF;
92 static rgb playpal[256];
93 static byte bright[256];
94 static GLuint lastTexture;
95 static cache *root;
97 /* Game */
98 static image scrnh[3]; // TITLEPIC INTERPIC ENDPIC
99 static image ltn[2][2];
101 /* Smoke */
102 static image smk_spr[SMSN];
103 static image smk_fspr[FLSN];
105 /* Effects */
106 static image fx_spr[15];
107 static char fx_sprd[15];
109 /* Weapons */
110 static image wp_spr[49*2];
111 static char wp_sprd[49*2];
113 /* Items */
114 static image item_spr[58];
115 static char item_sprd[58];
117 /* Player */
118 static image plr_spr[27*2];
119 static image plr_msk[27*2];
120 static char plr_sprd[27*2];
121 static image plr_wpn[11][6];
123 /* Monsters */
124 static image pl_spr[2];
125 static image pl_msk[2];
126 static image mn_spr[MN_TN][29*2];
127 static image mn_man_msk[29*2];
128 static char mn_sprd[MN_TN][29*2];
129 static image mn_fspr[8];
130 static image mn_sgun[2];
132 /* Misc */
133 static image sth[22];
134 static image bfh[160 - '!'];
135 static image sfh[160 - '!'];
136 static image stone;
137 static image stone2;
138 static image keys[3];
139 static int prx = 0;
140 static int pry = 0;
142 /* Menu */
143 static int gm_tm;
144 static image msklh[2];
145 static image mbarl;
146 static image mbarm;
147 static image mbarr;
148 static image mbaro;
149 static image mslotl;
150 static image mslotm;
151 static image mslotr;
153 /* Map */
154 static const char *anm[ANIT - 1][5] = {
155 {"WALL22_1", "WALL23_1", "WALL23_2", NULL, NULL},
156 {"WALL58_1", "WALL58_2", "WALL58_3", NULL, NULL},
157 {"W73A_1", "W73A_2", NULL, NULL, NULL},
158 {"RP2_1", "RP2_2", "RP2_3", "RP2_4", NULL}
159 };
160 static byte w_horiz = 1;
161 static int max_wall_width;
162 static int max_wall_height;
163 static int max_textures;
164 static image walp[256];
165 static byte walswp[256];
166 static byte walani[256];
167 static image anip[ANIT][5];
168 static byte anic[ANIT];
169 static image horiz;
171 /* Texture cache */
173 // https://blackpawn.com/texts/lightmaps/
174 static node *R_node_alloc (node *p, int w, int h) {
175 assert(p);
176 assert(w > 0);
177 assert(h > 0);
178 if (p->left) {
179 assert(p->right);
180 node *n = R_node_alloc(p->left, w, h);
181 return n ? n : R_node_alloc(p->right, w, h);
182 } else {
183 int pw = p->r - p->l + 1;
184 int ph = p->b - p->t + 1;
185 if (p->leaf || pw < w || ph < h) {
186 return NULL;
187 } else if (pw == w && ph == h) {
188 p->leaf = 1;
189 return p;
190 } else {
191 p->left = malloc(sizeof(node));
192 p->right = malloc(sizeof(node));
193 if (pw - w > ph - h) {
194 *p->left = (node) {
195 .up = p,
196 .l = p->l,
197 .t = p->t,
198 .r = p->l + w - 1,
199 .b = p->b
200 };
201 *p->right = (node) {
202 .up = p,
203 .l = p->l + w,
204 .t = p->t,
205 .r = p->r,
206 .b = p->b
207 };
208 } else {
209 *p->left = (node) {
210 .up = p,
211 .l = p->l,
212 .t = p->t,
213 .r = p->r,
214 .b = p->t + h - 1
215 };
216 *p->right = (node) {
217 .up = p,
218 .l = p->l,
219 .t = p->t + h,
220 .r = p->r,
221 .b = p->b
222 };
224 return R_node_alloc(p->left, w, h);
229 static int R_node_have_leaf (node *n) {
230 return n && (n->leaf || R_node_have_leaf(n->left) || R_node_have_leaf(n->right));
233 static void R_node_free_recursive (node *n) {
234 if (n) {
235 R_node_free_recursive(n->left);
236 R_node_free_recursive(n->right);
237 free(n);
241 static void R_node_free (node *n) {
242 if (n) {
243 //logo("free node %p {%i:%i:%i:%i}\n", n, n->l, n->t, n->r, n->b);
244 assert(n->leaf);
245 assert(n->left == NULL);
246 assert(n->right == NULL);
247 n->leaf = 0;
248 n->base = NULL;
249 node *p = n->up;
250 while (p != NULL) {
251 assert(p->leaf == 0);
252 assert(p->left);
253 assert(p->right);
254 if (R_node_have_leaf(p) == 0) {
255 R_node_free_recursive(p->left);
256 p->left = NULL;
257 R_node_free_recursive(p->right);
258 p->right = NULL;
259 p = p->up;
260 } else {
261 p = NULL;
267 static void R_cache_get_max_texture_size (int *w, int *h) {
268 GLint size = 0;
269 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
270 size = min(max(size, 0), 512); // more can be buggy on older hardware
271 *w = size;
272 *h = size;
275 static void R_gl_bind_texture (GLuint id) {
276 if (id != lastTexture) {
277 glBindTexture(GL_TEXTURE_2D, id);
281 static cache *R_cache_new (void) {
282 int w, h;
283 GLuint id;
284 cache *c = NULL;
285 R_cache_get_max_texture_size(&w, &h);
286 if (w && h) {
287 glGenTextures(1, &id);
288 if (id) {
289 R_gl_bind_texture(id);
290 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
291 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
292 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
293 c = malloc(sizeof(cache));
294 if (c != NULL) {
295 *c = (cache) {
296 .id = id,
297 .root.r = w - 1,
298 .root.b = h - 1
299 };
300 } else {
301 glDeleteTextures(1, &id);
305 //logo("new cache %p\n", c);
306 return c;
309 static void R_cache_free (cache *root, int freetexture) {
310 cache *next;
311 cache *c = root;
312 while (c != NULL) {
313 next = c->next;
314 R_node_free_recursive(c->root.left);
315 R_node_free_recursive(c->root.right);
316 if (freetexture && c->id != 0) {
317 glDeleteTextures(1, &c->id);
319 free(c);
320 c = next;
324 static node *R_cache_alloc (cache *root, int w, int h) {
325 assert(root);
326 assert(w > 0 && h > 0);
327 node *n = NULL;
328 cache *p = NULL;
329 cache *c = root;
330 int maxw, maxh;
331 R_cache_get_max_texture_size(&maxw, &maxh);
332 if (w <= maxw && h <= maxh) {
333 while (c && !n) {
334 n = R_node_alloc(&c->root, w, h);
335 if (n) {
336 assert(n->leaf);
337 n->base = c;
339 p = c;
340 c = c->next;
342 if (!n) {
343 c = R_cache_new();
344 if (c) {
345 p->next = c;
346 n = R_node_alloc(&c->root, w, h);
347 if (n) {
348 assert(n->leaf);
349 n->base = c;
354 if (n) {
355 //logo("new node %p {%i:%i:%i:%i}\n", n, n->l, n->t, n->r, n->b);
356 } else {
357 logo("new node failed {%i:%i}\n", w, h);
359 return n;
362 static void R_cache_update (node *n, const void *data, int x, int y, int w, int h) {
363 assert(n);
364 assert(n->leaf);
365 assert(n->base);
366 assert(data);
367 assert(x >= 0);
368 assert(y >= 0);
369 assert(n->l + x + w - 1 <= n->r);
370 assert(n->t + y + h - 1 <= n->b);
371 R_gl_bind_texture(n->base->id);
372 glTexSubImage2D(GL_TEXTURE_2D, 0, n->l + x, n->t + y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
375 /* Generic helpers */
377 static void R_init_playpal (void) {
378 int i;
379 byte *vgapal = M_lock(F_getresid("PLAYPAL"));
380 for (i = 0; i < 256; i++) {
381 playpal[i] = (rgb) {
382 .r = vgapal[i * 3 + 0] * 255 / 63,
383 .g = vgapal[i * 3 + 1] * 255 / 63,
384 .b = vgapal[i * 3 + 2] * 255 / 63,
385 };
386 bright[i] = ((int)vgapal[i * 3 + 0] + vgapal[i * 3 + 1] + vgapal[i * 3 + 2]) * 8 / (63 * 3);
388 M_unlock(vgapal);
391 static vgaimg *R_getvga (int id) {
392 int loaded = M_was_locked(id);
393 vgaimg *v = M_lock(id);
394 if (v != NULL && !loaded) {
395 v->w = short2host(v->w);
396 v->h = short2host(v->h);
397 v->x = short2host(v->x);
398 v->y = short2host(v->y);
400 return v;
403 static rgba *R_extract_flame_spr (vgaimg *v) {
404 static const byte flametab[16] = {
405 0xBC, 0xBA, 0xB8, 0xB6, 0xB4, 0xB2, 0xB0, 0xD5,
406 0xD6, 0xD7, 0xA1, 0xA0, 0xE3, 0xE2, 0xE1, 0xE0
407 };
408 int i, j;
409 rgba *s = malloc(v->w * v->h * sizeof(rgba));
410 if (s != NULL) {
411 for (j = 0; j < v->h; j++) {
412 for (i = 0; i < v->w; i++) {
413 int k = j * v->w + i;
414 byte c = v->data[k] + bright[DEFAULT_SKY_COLOR];
415 s[k] = (rgba) {
416 .r = playpal[flametab[c]].r,
417 .g = playpal[flametab[c]].g,
418 .b = playpal[flametab[c]].b,
419 .a = v->data[k] == VGA_TRANSPARENT_COLOR ? 0x00 : 0xFF,
420 };
424 return s;
427 static rgba *R_extract_smoke_spr (vgaimg *v) {
428 int i, j;
429 rgba *s = malloc(v->w * v->h * sizeof(rgba));
430 if (s != NULL) {
431 for (j = 0; j < v->h; j++) {
432 for (i = 0; i < v->w; i++) {
433 int k = j * v->w + i;
434 byte c = ((v->data[k] + bright[DEFAULT_SKY_COLOR]) + 0x60) ^ 0x0F;
435 byte a = 0xFF - ((int)playpal[c].r + playpal[c].g + playpal[c].b) / 3;
436 s[k] = (rgba) {
437 .r = playpal[c].r,
438 .g = playpal[c].g,
439 .b = playpal[c].b,
440 .a = v->data[k] == VGA_TRANSPARENT_COLOR ? 0x00 : a,
441 };
445 return s;
448 static rgba *R_extract_mask_spr (vgaimg *v) {
449 int i, j;
450 rgba *s = malloc(v->w * v->h * sizeof(rgba));
451 if (s != NULL) {
452 for (j = 0; j < v->h; j++) {
453 for (i = 0; i < v->w; i++) {
454 int k = j * v->w + i;
455 byte c = v->data[k];
456 if (c >= 0x70 && c <= 0x7F) {
457 byte mask = c - 0x70;
458 mask = 0xFF - ((mask << 4) | mask);
459 s[k] = (rgba) {
460 .r = mask,
461 .g = mask,
462 .b = mask,
463 .a = 0xFF,
464 };
465 } else {
466 s[k] = (rgba) {
467 .r = 0,
468 .g = 0,
469 .b = 0,
470 .a = 0,
471 };
476 return s;
479 static rgba *R_extract_rgba_spr (vgaimg *v) {
480 int i, j;
481 rgba *s = malloc(v->w * v->h * sizeof(rgba));
482 if (s != NULL) {
483 for (j = 0; j < v->h; j++) {
484 for (i = 0; i < v->w; i++) {
485 int k = j * v->w + i;
486 byte c = v->data[k];
487 s[k] = (rgba) {
488 .r = playpal[c].r,
489 .g = playpal[c].g,
490 .b = playpal[c].b,
491 .a = c == VGA_TRANSPARENT_COLOR ? 0x00 : 0xFF,
492 };
496 return s;
499 /* OpenGL helpers */
501 static image R_gl_create_image (const rgba *buf, int w, int h) {
502 node *n = R_cache_alloc(root, w, h);
503 if (n) {
504 R_cache_update(n, buf, 0, 0, w, h);
506 return (image) {
507 .n = n,
508 .w = w,
509 .h = h,
510 .res = -1
511 };
514 static image R_gl_get_special_image (int id, rgba *(*fn)(vgaimg*)) {
515 image img;
516 //char name[8];
517 //F_getresname(name, id);
518 //logo("load image: %.8s\n", name);
519 vgaimg *v = R_getvga(id);
520 if (v != NULL) {
521 rgba *buf = (*fn)(v);
522 img = R_gl_create_image(buf, v->w, v->h);
523 img.x = v->x;
524 img.y = v->y;
525 img.res = id;
526 M_unlock(v);
527 free(buf);
528 } else {
529 img = (image) {
530 .res = id
531 };
533 return img;
536 static image R_gl_getimage (int id) {
537 return R_gl_get_special_image(id, &R_extract_rgba_spr);
540 static image R_gl_loadimage (const char name[8]) {
541 return R_gl_getimage(F_getresid(name));
544 static image R_gl_get_special_spr (const char n[4], int s, int d, rgba *(*fn)(vgaimg*)) {
545 return R_gl_get_special_image(F_getsprid(n, s, d), fn);
548 static void R_gl_free_image (image *img) {
549 if (img->n != NULL && img->res >= 0) {
550 R_node_free(img->n);
552 img->n = NULL;
553 img->res = -1;
556 static void R_gl_quad_vetexes (int x, int y, int w, int h) {
557 glVertex2i(x + w, y);
558 glVertex2i(x, y);
559 glVertex2i(x, y + h);
560 glVertex2i(x + w, y + h);
563 static void R_gl_draw_quad (int x, int y, int w, int h) {
564 glBegin(GL_QUADS);
565 R_gl_quad_vetexes(x, y, w, h);
566 glEnd();
569 static void R_gl_draw_textured (image *img, int x, int y, int w, int h, int flip) {
570 if (img->n) {
571 GLfloat nw = img->n->base->root.r + 1;
572 GLfloat nh = img->n->base->root.b + 1;
573 GLfloat ax = (flip ? img->n->l : img->n->r + 1) / nw;
574 GLfloat bx = (flip ? img->n->r + 1 : img->n->l) / nh;
575 GLfloat ay = (img->n->t) / nw;
576 GLfloat by = (img->n->b + 1) / nh;
577 R_gl_bind_texture(img->n->base->id);
578 glEnable(GL_TEXTURE_2D);
579 glBegin(GL_QUADS);
580 glTexCoord2f(ax, ay); glVertex2i(x + w, y);
581 glTexCoord2f(bx, ay); glVertex2i(x, y);
582 glTexCoord2f(bx, by); glVertex2i(x, y + h);
583 glTexCoord2f(ax, by); glVertex2i(x + w, y + h);
584 glEnd();
585 } else {
586 glColor3ub(255, 0, 0);
587 glDisable(GL_BLEND);
588 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
589 glDisable(GL_TEXTURE_2D);
590 R_gl_draw_quad(x, y, w, h);
594 /* fit image into rectangle without applying offset and transparency */
595 static void R_gl_draw_image_ext (image *img, int x, int y, int w, int h) {
596 glDisable(GL_BLEND);
597 glColor3ub(255, 255, 255);
598 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
599 R_gl_draw_textured(img, x, y, w, h, 0);
602 /* draw sprite with offset and coloring */
603 static void R_gl_draw_image_color (image *img, int x, int y, int flip) {
604 int xx = flip ? x - img->w + img->x : x - img->x;
605 glEnable(GL_BLEND);
606 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
607 R_gl_draw_textured(img, xx, y - img->y, img->w, img->h, flip);
610 /* draw sprite with offset */
611 static void R_gl_draw_image (image *img, int x, int y, int flip) {
612 glColor3ub(255, 255, 255);
613 R_gl_draw_image_color(img, x, y, flip);
616 static void R_gl_set_color (byte c) {
617 glColor3ub(playpal[c].r, playpal[c].g, playpal[c].b);
620 static void R_gl_setclip (int x, int y, int w, int h) {
621 glScissor(x * screen_scale, (SCRH - h - y) * screen_scale, w * screen_scale, h * screen_scale);
624 static void R_gl_setmatrix (void) {
625 SCRW = screen_width / screen_scale;
626 SCRH = screen_height / screen_scale;
627 glScissor(0, 0, screen_width, screen_height);
628 glViewport(0, 0, screen_width, screen_height);
629 glMatrixMode(GL_PROJECTION);
630 glLoadIdentity();
631 glOrtho(0, SCRW, SCRH, 0, 0, 1);
632 glMatrixMode(GL_MODELVIEW);
633 glLoadIdentity();
636 /* --- Misc --- */
638 static image Z_getspr (const char n[4], int s, int d, char *dir) {
639 int h = F_getsprid(n, s, d);
640 if (dir != NULL) {
641 *dir = (h & 0x8000) ? 1 : 0;
643 return R_gl_getimage(h);
646 static image *Z_get_char_image (image *img, int ch) {
647 image *p = NULL;
648 ch = cp866_toupper(ch);
649 if (ch > 32 && ch < 160) {
650 p = &img[ch - '!'];
652 return p;
655 static int Z_get_char_width_generic (image *img, int off, int ch) {
656 image *p = Z_get_char_image(img, ch);
657 return p == NULL ? off : p->w - 1;
660 static int Z_putch_generic (image *img, int off, int ch) {
661 image *p = Z_get_char_image(img, ch);
662 int w = p == NULL ? off : p->w - 1;
663 if (p != NULL && p->n != NULL) {
664 R_gl_draw_image(p, prx, pry, 0);
666 prx += w;
667 return w;
670 static int Z_get_string_width_generic (image *img, int off, const char *fmt, va_list ap) {
671 int i, w, ww;
672 char buf[80];
673 vsprintf(buf, fmt, ap);
674 for (i = w = ww = 0; buf[i]; ++i) {
675 switch (buf[i]) {
676 case '\n':
677 case '\r':
678 ww = max(w, ww);
679 w = 0;
680 break;
681 default:
682 w += Z_get_char_width_generic(img, off, (byte)buf[i]);
685 return max(w, ww);
688 static int Z_printf_generic (image *img, int off, const char *fmt, va_list ap) {
689 int i, w, ww;
690 char buf[80];
691 vsprintf(buf, fmt, ap);
692 for (i = w = ww = 0; buf[i]; ++i) {
693 switch (buf[i]) {
694 case '\n':
695 pry += off + 1;
696 case '\r':
697 w = max(w, ww);
698 prx = 0;
699 w = 0;
700 break;
701 default:
702 w += Z_putch_generic(img, off, (byte)buf[i]);
705 return w;
708 static void Z_gotoxy (int x, int y) {
709 prx = x;
710 pry = y;
713 static int Z_get_big_string_width (const char *fmt, ...) {
714 va_list a;
715 va_start(a, fmt);
716 int w = Z_get_string_width_generic(bfh, 12, fmt, a);
717 va_end(a);
718 return w;
721 static int Z_printbf (const char *fmt, ...) {
722 va_list a;
723 va_start(a, fmt);
724 int w = Z_printf_generic(bfh, 12, fmt, a);
725 va_end(a);
726 return w;
729 static int Z_get_small_string_width (const char *fmt, ...) {
730 va_list a;
731 va_start(a, fmt);
732 int w = Z_get_string_width_generic(sfh, 7, fmt, a);
733 va_end(a);
734 return w;
737 static int Z_printsf (const char *fmt, ...) {
738 va_list a;
739 va_start(a, fmt);
740 int w =Z_printf_generic(sfh, 7, fmt, a);
741 va_end(a);
742 return w;
745 static void Z_printhf (const char *fmt, ...) {
746 int i, c;
747 char buf[80];
748 va_list a;
749 va_start(a, fmt);
750 vsprintf(buf, fmt, a);
751 va_end(a);
752 for (i = 0; buf[i]; ++i) {
753 switch (buf[i]) {
754 case '0':
755 case '1':
756 case '2':
757 case '3':
758 case '4':
759 case '5':
760 case '6':
761 case '7':
762 case '8':
763 case '9':
764 c = buf[i] - '0';
765 break;
766 case '-':
767 c = 10;
768 break;
769 case '%':
770 c = 11;
771 break;
772 case '\n':
773 pry += 19;
774 case '\r':
775 c = -1;
776 prx = 0;
777 break;
778 default:
779 c = -1;
780 break;
782 if (c >= 0) {
783 R_gl_draw_image(&sth[c], prx, pry, 0);
785 prx += 14;
789 /* --- Menu --- */
791 static image *PL_getspr (int s, int d, int msk) {
792 int i = (s - 'A') * 2 + d;
793 return msk ? &plr_msk[i] : &plr_spr[i];
796 #define SCROLLER_MIDDLE 10
797 #define TEXTFIELD_MIDDLE 2
799 static void get_entry_size (const menu_t *m, int i, int *w, int *h) {
800 assert(m != NULL);
801 assert(i >= 0);
802 assert(w != NULL);
803 assert(h != NULL);
804 int x = 0;
805 int y = 0;
806 int type = 0;
807 menu_msg_t msg;
808 msg.type = GM_GETENTRY;
809 assert(GM_send(m, i, &msg));
810 type = msg.integer.i;
811 switch (type) {
812 case GM_BUTTON:
813 case GM_SCROLLER:
814 case GM_TEXTFIELD:
815 case GM_TEXTFIELD_BUTTON:
816 msg.type = GM_GETCAPTION;
817 if (GM_send(m, i, &msg)) {
818 x = Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
820 break;
821 case GM_SMALL_BUTTON:
822 msg.type = GM_GETCAPTION;
823 if (GM_send(m, i, &msg)) {
824 x = Z_get_small_string_width("%.*s", msg.string.maxlen, msg.string.s);
826 break;
827 default:
828 assert(0);
830 switch (type) {
831 case GM_BUTTON:
832 msg.type = GM_GETSTR;
833 if (GM_send(m, i, &msg)) {
834 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
836 y = 16;
837 break;
838 case GM_SMALL_BUTTON:
839 msg.type = GM_GETSTR;
840 if (GM_send(m, i, &msg)) {
841 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
843 y = 12;
844 break;
845 case GM_SCROLLER:
846 x += (SCROLLER_MIDDLE + 2) * 8;
847 y = 16;
848 break;
849 case GM_TEXTFIELD:
850 case GM_TEXTFIELD_BUTTON:
851 msg.type = GM_GETSTR;
852 if (GM_send(m, i, &msg)) {
853 x += (msg.string.maxlen + 2) * 8;
854 } else {
855 x += (TEXTFIELD_MIDDLE + 2) * 8;
857 y = 16;
858 break;
859 default:
860 assert(0);
862 *w = x;
863 *h = y;
866 static void get_menu_size (const menu_t *m, int *w, int *h) {
867 assert(m != NULL);
868 assert(w != NULL);
869 assert(h != NULL);
870 int i, n, x, y, xx, yy, type;
871 menu_msg_t msg;
872 msg.type = GM_QUERY;
873 if (GM_send_this(m, &msg)) {
874 n = msg.integer.b;
875 type = msg.integer.s;
876 x = 0;
877 y = 0;
878 msg.type = GM_GETTITLE;
879 if (GM_send_this(m, &msg)) {
880 switch (type) {
881 case GM_BIG: x = Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); break;
882 case GM_SMALL: x = Z_get_small_string_width("%.*s", msg.string.maxlen, msg.string.s); break;
883 default: assert(0);
886 for (i = 0; i < n; i++) {
887 get_entry_size(m, i, &xx, &yy);
888 x = max(x, xx);
889 y += yy;
891 *w = x;
892 *h = y;
893 } else {
894 *w = 0;
895 *h = 0;
899 static int GM_draw (void) {
900 int i, j, n, x, y, xoff, yoff, cur, w, type, recv;
901 const menu_t *m = GM_get();
902 menu_msg_t msg;
903 if (m != NULL) {
904 get_menu_size(m, &x, &y);
905 x = SCRW / 2 - x / 2;
906 y = SCRH / 2 - y / 2;
907 // --- title ---
908 msg.type = GM_QUERY;
909 if (GM_send_this(m, &msg)) {
910 cur = msg.integer.i;
911 n = msg.integer.a;
912 type = msg.integer.s;
913 msg.type = GM_GETTITLE;
914 if (GM_send_this(m, &msg)) {
915 Z_gotoxy(x, y - 10);
916 switch (type) {
917 case GM_SMALL: yoff = 8; Z_printsf("%.*s", msg.string.maxlen, msg.string.s); break;
918 case GM_BIG: yoff = 20; Z_printbf("%.*s", msg.string.maxlen, msg.string.s); break;
919 default: assert(0);
921 } else {
922 yoff = 0;
924 for (i = 0; i < n; i++) {
925 msg.type = GM_GETENTRY;
926 if (GM_send(m, i, &msg)) {
927 type = msg.integer.i;
928 if (i == cur) {
929 if (type == GM_SMALL_BUTTON) {
930 Z_gotoxy(x - 8, y + yoff);
931 Z_printsf(">");
932 } else {
933 R_gl_draw_image(&msklh[(gm_tm / 6) & 1], x - 25, y + yoff - 8, 0);
936 msg.type = GM_GETCAPTION;
937 if (GM_send(m, i, &msg)) {
938 Z_gotoxy(x, y + yoff);
939 if (type == GM_SMALL_BUTTON) {
940 xoff = Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
941 } else {
942 xoff = Z_printbf("%.*s", msg.string.maxlen, msg.string.s);
944 } else {
945 xoff = 0;
947 switch (type) {
948 case GM_BUTTON:
949 case GM_SMALL_BUTTON:
950 msg.type = GM_GETSTR;
951 if (GM_send(m, i, &msg)) {
952 Z_gotoxy(x + xoff, y + yoff);
953 if (type == GM_SMALL_BUTTON) {
954 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
955 } else {
956 Z_printbf("%.*s", msg.string.maxlen, msg.string.s);
959 yoff += type == GM_BUTTON ? 16 : 12;
960 break;
961 case GM_TEXTFIELD:
962 case GM_TEXTFIELD_BUTTON:
963 yoff += 9;
964 msg.type = GM_GETSTR;
965 recv = GM_send(m, i, &msg);
966 w = recv ? msg.string.maxlen : TEXTFIELD_MIDDLE;
967 R_gl_draw_image(&mslotl, x + xoff, y + yoff, 0);
968 for (j = 1; j <= w; j++) {
969 R_gl_draw_image(&mslotm, x + xoff + j * 8, y + yoff, 0);
971 R_gl_draw_image(&mslotr, x + xoff + j * 8, y + yoff, 0);
972 Z_gotoxy(x + xoff + 4, y + yoff - 7);
973 if (input && i == cur) {
974 Z_printsf("%.*s_", imax, ibuf);
975 } else if (recv) {
976 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
978 yoff += 7;
979 break;
980 case GM_SCROLLER:
981 R_gl_draw_image(&mbarl, x + xoff, y + yoff, 0);
982 for (j = 1; j < SCROLLER_MIDDLE; j++) {
983 R_gl_draw_image(&mbarm, x + xoff + j * 8, y + yoff, 0);
985 R_gl_draw_image(&mbarr, x + xoff + j * 8, y + yoff, 0);
986 msg.type = GM_GETINT;
987 if (GM_send(m, i, &msg)) {
988 int lev = (msg.integer.i - msg.integer.a) * ((SCROLLER_MIDDLE - 2) * 8) / msg.integer.b;
989 R_gl_draw_image(&mbaro, x + xoff + lev + 8, y + yoff, 0);
991 yoff += 16;
992 break;
993 default:
994 assert(0);
1000 return m != NULL;
1003 /* --- View --- */
1005 static void R_draw_fld (byte *fld, int minx, int miny, int maxx, int maxy, int fg) {
1006 int i, j;
1007 assert(minx >= 0 && minx <= FLDW);
1008 assert(miny >= 0 && miny <= FLDH);
1009 assert(maxx >= 0 && maxx <= FLDW);
1010 assert(maxy >= 0 && maxy <= FLDH);
1011 for (j = miny; j < maxy; j++) {
1012 for (i = minx; i < maxx; i++) {
1013 byte id = fld[j * FLDW + i];
1014 if (id != 0) {
1015 if (walp[id].res < 0) {
1016 if (fg) {
1017 switch (R_get_special_id(id)) {
1018 case 1:
1019 glColor4ub(0, 0, 255, 127);
1020 break;
1021 case 2:
1022 glColor4ub(0, 127, 0, 127);
1023 break;
1024 case 3:
1025 glColor4ub(127, 0, 0, 127);
1026 break;
1027 default:
1028 glColor4ub(0, 0, 0, 127);
1029 break;
1031 glEnable(GL_BLEND);
1032 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1033 glDisable(GL_TEXTURE_2D);
1034 R_gl_draw_quad(i * CELW, j * CELW, CELW, CELH);
1036 } else {
1037 R_gl_draw_image(&walp[id], i * CELW, j * CELH, 0);
1044 static void R_draw_dots (void) {
1045 int i;
1046 glDisable(GL_BLEND);
1047 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1048 glDisable(GL_TEXTURE_2D);
1049 glBegin(GL_QUADS);
1050 for (i = 0; i < MAXDOT; i++) {
1051 if (dot[i].t != 0) {
1052 R_gl_set_color(dot[i].c);
1053 R_gl_quad_vetexes(dot[i].o.x, dot[i].o.y, 1, 1);
1056 glEnd();
1059 static void R_draw_items (void) {
1060 int i, s;
1061 for (i = 0; i < MAXITEM; ++i) {
1062 s = -1;
1063 if (it[i].t && it[i].s >= 0) {
1064 switch (it[i].t & 0x7FFF) {
1065 case I_ARM1:
1066 s = it[i].s / 9 + 18;
1067 break;
1068 case I_ARM2:
1069 s = it[i].s / 9 + 20;
1070 break;
1071 case I_MEGA:
1072 s = it[i].s / 2 + 22;
1073 break;
1074 case I_INVL:
1075 s = it[i].s / 2 + 26;
1076 break;
1077 case I_SUPER:
1078 case I_RTORCH:
1079 case I_GTORCH:
1080 case I_BTORCH:
1081 s = it[i].s / 2 + (it[i].t - I_SUPER) * 4 + 35;
1082 break;
1083 case I_GOR1: case I_FCAN:
1084 s = it[i].s / 2 + (it[i].t - I_GOR1) * 3 + 51;
1085 break;
1086 case I_AQUA:
1087 s = 30;
1088 break;
1089 case I_SUIT:
1090 s = 34;
1091 break;
1092 case I_KEYR:
1093 case I_KEYG:
1094 case I_KEYB:
1095 s = (it[i].t & 0x7FFF) - I_KEYR + 31;
1096 break;
1097 case I_GUN2:
1098 s = 57;
1099 break;
1100 default:
1101 s = (it[i].t & 0x7FFF) - 1;
1104 if (s >= 0) {
1105 R_gl_draw_image(&item_spr[s], it[i].o.x, it[i].o.y, item_sprd[s]);
1110 static int standspr (player_t *p) {
1111 if (p->f & PLF_UP) {
1112 return 'X';
1113 } else if (p->f & PLF_DOWN) {
1114 return 'Z';
1115 } else {
1116 return 'E';
1120 static int wpnspr (player_t *p) {
1121 if (p->f & PLF_UP) {
1122 return 'C';
1123 } else if(p->f & PLF_DOWN) {
1124 return 'E';
1125 } else {
1126 return 'A';
1130 static void R_draw_player (player_t *p) {
1131 enum {STAND, GO, DIE, SLOP, DEAD, MESS, OUT, FALL}; // copypasted from player.c!
1132 static const int wytab[] = {-1, -2, -1, 0};
1133 int s = 'A';
1134 int w = 0;
1135 int wx = 0;
1136 int wy = 0;
1137 switch (p->st) {
1138 case STAND:
1139 if (p->f & PLF_FIRE) {
1140 s = standspr(p) + 1;
1141 w = wpnspr(p) + 1;
1142 } else if (p->pain) {
1143 s = 'G';
1144 w = 'A';
1145 wx = p->d ? 2 : -2;
1146 wy = 1;
1147 } else {
1148 s = standspr(p);
1149 w = wpnspr(p);
1151 break;
1152 case DEAD:
1153 s = 'N';
1154 break;
1155 case MESS:
1156 s = 'W';
1157 break;
1158 case GO:
1159 if (p->pain) {
1160 s = 'G';
1161 w = 'A';
1162 wx = p->d ? 2 : -2;
1163 wy = 1;
1164 } else {
1165 s = plr_goanim[p->s / 8];
1166 w = (p->f & PLF_FIRE) ? 'B' : 'A';
1167 wx = p->d ? 2 : -2;
1168 wy = 1 + wytab[s - 'A'];
1170 break;
1171 case DIE:
1172 s = plr_dieanim[p->s];
1173 break;
1174 case SLOP:
1175 s = plr_slopanim[p->s];
1176 break;
1177 case OUT:
1178 s = 0;
1179 break;
1181 if (p->wpn == 0) {
1182 w = 0;
1184 if (w) {
1185 R_gl_draw_image(&plr_wpn[(int)p->wpn][w -'A'], p->o.x + wx, p->o.y + wy, p->d);
1187 if (s) {
1188 R_gl_draw_image(&plr_spr[(s - 'A') * 2 + p->d], p->o.x, p->o.y, plr_sprd[(s - 'A') * 2 + p->d]);
1189 R_gl_set_color(p->color + PLAYER_COLOR_OFFSET);
1190 R_gl_draw_image_color(&plr_msk[(s - 'A') * 2 + p->d], p->o.x, p->o.y, plr_sprd[(s - 'A') * 2 + p->d]);
1194 static void R_draw_monsters (void) {
1195 enum {SLEEP, GO, RUN, CLIMB, DIE, DEAD, ATTACK, SHOOT, PAIN, WAIT, REVIVE, RUNOUT}; // copypasted from monster.c!
1196 int i;
1197 for (i = 0; i < MAXMN; i++) {
1198 if (mn[i].t != MN_NONE) {
1199 int x = mn[i].o.x;
1200 int y = mn[i].o.y;
1201 if (mn[i].t < MN__LAST) {
1202 if ((mn[i].t != MN_SOUL && mn[i].t != MN_PAIN) || mn[i].st != DEAD) {
1203 int ap = mn[i].ap[mn[i].ac];
1204 int d = (ap - 'A') * 2 + mn[i].d;
1205 int dir = mn_sprd[mn[i].t - 1][d];
1206 if (mn[i].t == MN_MAN && (ap == 'E' || ap == 'F')) {
1207 R_gl_draw_image(&mn_sgun[ap - 'E'], x, y, mn[i].d);
1209 R_gl_draw_image(&mn_spr[mn[i].t - 1][d], x, y, dir);
1210 if (mn[i].t == MN_MAN) {
1211 R_gl_set_color(MANCOLOR + PLAYER_COLOR_OFFSET);
1212 R_gl_draw_image_color(&mn_man_msk[d], x, y, dir);
1215 if (mn[i].t == MN_VILE && mn[i].st == SHOOT) {
1216 R_gl_draw_image(&mn_fspr[mn[i].ac / 3], mn[i].tx, mn[i].ty, 0);
1218 } else if (mn[i].t == MN_PL_DEAD || mn[i].t == MN_PL_MESS) {
1219 int type = mn[i].t - MN_PL_DEAD;
1220 R_gl_draw_image(&pl_spr[type], x, y, 0);
1221 R_gl_set_color(mn[i].d);
1222 R_gl_draw_image_color(&pl_msk[type], x, y, 0);
1228 static void R_draw_weapons (void) {
1229 enum {NONE, ROCKET, PLASMA, APLASMA, BALL1, BALL2, BALL7, BFGBALL, BFGHIT, MANF, REVF, FIRE}; // copypasted from weapons.c!
1230 int i, s, d, x, y;
1231 for (i = 0; i < MAXWPN; ++i) {
1232 s = -1;
1233 d = 0;
1234 switch (wp[i].t) {
1235 case REVF:
1236 case ROCKET:
1237 d = wp[i].s;
1238 if (d < 2) {
1239 d = wp[i].o.xv > 0 ? 1 : 0;
1240 x = abs(wp[i].o.xv);
1241 y = wp[i].o.yv;
1242 s = 0;
1243 if (y < 0) {
1244 if (-y >= x) {
1245 s = 30;
1247 } else if (y > 0) {
1248 if (y >= x / 2) {
1249 s = 31;
1252 } else {
1253 s = (d - 2) / 2 + 1;
1254 d = 0;
1256 break;
1257 case MANF:
1258 s=wp[i].s;
1259 if (s >= 2) {
1260 s /= 2;
1261 break;
1263 case PLASMA:
1264 case APLASMA:
1265 case BALL1:
1266 case BALL7:
1267 case BALL2:
1268 s = wp[i].s;
1269 if (s >= 2) {
1270 s = s / 2 + 1;
1272 switch (wp[i].t) {
1273 case PLASMA:
1274 s += 4;
1275 break;
1276 case APLASMA:
1277 s += 11;
1278 break;
1279 case BALL1:
1280 s += 32;
1281 break;
1282 case BALL2:
1283 s += 42;
1284 break;
1285 case BALL7:
1286 s += 37;
1287 d = wp[i].o.xv >= 0 ? 1 : 0;
1288 break;
1289 case MANF:
1290 s += 47;
1291 d= wp[i].o.xv>=0 ? 1 : 0;
1292 break;
1294 break;
1295 case BFGBALL:
1296 s = wp[i].s;
1297 if (s >= 2) {
1298 s = s / 2 + 1;
1300 s += 18;
1301 break;
1302 case BFGHIT:
1303 s = wp[i].s / 2 + 26;
1304 break;
1306 if (s >= 0) {
1307 R_gl_draw_image(&wp_spr[s * 2 + d], wp[i].o.x, wp[i].o.y, wp_sprd[s * 2 + d]);
1312 static void R_draw_smoke (void) {
1313 int i, s;
1314 for (i = 0; i < MAXSMOK; ++i) {
1315 if (sm[i].t) {
1316 switch (sm[i].s) {
1317 case 0:
1318 s = sm[i].t;
1319 if (s >= (SMSN - 1) * 3) {
1320 s = 0;
1321 } else {
1322 s = SMSN - 1 - s / 3;
1324 R_gl_draw_image(&smk_spr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1325 break;
1326 case 1:
1327 s = sm[i].t;
1328 if (s >= FLSN - 1) {
1329 s = 0;
1330 } else {
1331 s = FLSN - 1 - s;
1333 R_gl_draw_image(&smk_fspr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1334 break;
1340 static void R_draw_effects (void) {
1341 enum {NONE, TFOG, IFOG, BUBL}; // copypasted from fx.c
1342 int i, s;
1343 glPointSize(screen_scale);
1344 for (i = 0; i < MAXFX; ++i) {
1345 switch (fx[i].t) {
1346 case TFOG:
1347 s = fx[i].s / 2;
1348 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1349 break;
1350 case IFOG:
1351 s = fx[i].s / 2 + 10;
1352 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1353 break;
1354 case BUBL:
1355 glDisable(GL_BLEND);
1356 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1357 glDisable(GL_TEXTURE_2D);
1358 R_gl_set_color(0xC0 + fx[i].s);
1359 R_gl_draw_quad(fx[i].x >> 8, fx[i].y >> 8, 1, 1);
1360 break;
1365 static int get_pu_st (int t) {
1366 if (t >= PL_FLASH) {
1367 return 1;
1368 } else if((t / 9) & 1) {
1369 return 0;
1370 } else {
1371 return 1;
1375 static void R_draw_view (int x, int y, int w, int h, int camx, int camy) {
1376 glPushMatrix();
1377 R_gl_setclip(x, y, w, h);
1378 glTranslatef(x, y, 0);
1379 if (w_horiz && horiz.n != NULL) {
1380 R_gl_draw_image_ext(&horiz, 0, 0, w, h);
1381 if (sky_type == 2 && lt_time < 0) {
1382 image *tanderbolt = &ltn[lt_type][lt_time < -5 ? 0 : 1];
1383 if (!lt_side) {
1384 R_gl_draw_image(tanderbolt, 0, lt_ypos, 0);
1385 } else {
1386 R_gl_draw_image(tanderbolt, w - 1, lt_ypos, 1);
1389 } else {
1390 glDisable(GL_BLEND);
1391 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1392 glDisable(GL_TEXTURE_2D);
1393 R_gl_set_color(DEFAULT_SKY_COLOR);
1394 R_gl_draw_quad(0, 0, w, h);
1396 int maxx = min((camx + w) / CELW + 1, FLDW);
1397 int maxy = min((camy + h) / CELH + 1, FLDH);
1398 int minx = max((camx - max_wall_width) / CELW, 0);
1399 int miny = max((camy - max_wall_height) / CELH, 0);
1400 glTranslatef(-camx, -camy, 0);
1401 R_draw_fld((byte*)fldb, minx, miny, maxx, maxy, 0);
1402 R_draw_dots();
1403 R_draw_items();
1404 R_draw_player(&pl1);
1405 if (_2pl) {
1406 R_draw_player(&pl2);
1408 R_draw_monsters();
1409 R_draw_weapons();
1410 R_draw_smoke();
1411 R_draw_effects();
1412 R_draw_fld((byte*)fldf, minx, miny, maxx, maxy, 1);
1413 glTranslatef(camx, camy, 0);
1414 if (sky_type == 2 && (lt_time == -4 || lt_time == -2)) {
1415 glColor4ub(255, 255, 255, 255);
1416 glEnable(GL_BLEND);
1417 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1418 glDisable(GL_TEXTURE_2D);
1419 R_gl_draw_quad(0, 0, w, h);
1421 glPopMatrix();
1424 static void R_draw_player_view (player_t *p, int x, int y, int w, int h) {
1425 p->looky = min(max(p->looky, -SCRH / 4), SCRH / 4); // TODO remove writeback
1426 int st = stone.w;
1427 int cw = w - st;
1428 int cx = min(max(p->o.x, cw / 2), FLDW * CELW - cw / 2);
1429 int cy = min(max(p->o.y - 12 + p->looky, h / 2), FLDH * CELH - h / 2);
1430 int camx = max(cx - cw / 2, 0);
1431 int camy = max(cy - h / 2, 0);
1432 glPushMatrix();
1433 R_draw_view(x, y + 1, cw, h - 2, camx, camy);
1434 glTranslatef(x, y, 0);
1435 if (p->invl) {
1436 if (get_pu_st(p->invl)) {
1437 glEnable(GL_BLEND);
1438 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1439 glDisable(GL_TEXTURE_2D);
1440 glColor4ub(191, 191, 191, 255);
1441 R_gl_draw_quad(0, 0, cw, h);
1443 } else {
1444 if (p->suit && get_pu_st(p->suit)) {
1445 glEnable(GL_BLEND);
1446 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1447 glDisable(GL_TEXTURE_2D);
1448 glColor4ub(0, 255, 0, 192);
1449 R_gl_draw_quad(0, 0, cw, h);
1451 int f = min(max(p->pain * 3, 0), 255);
1452 if (f > 0) {
1453 glEnable(GL_BLEND);
1454 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1455 glDisable(GL_TEXTURE_2D);
1456 glColor4ub(255, 0, 0, f);
1457 R_gl_draw_quad(0, 0, cw, h);
1460 R_gl_setclip(x, y, w, h);
1461 glTranslatef(-x + cw, 0, 0);
1462 R_gl_draw_image(&stone, 0, 0, 0);
1463 int i = stone.h;
1464 while (i < h) {
1465 R_gl_draw_image(&stone2, 0, i, 0);
1466 i += stone2.h;
1468 if (p->drawst & PL_DRAWAIR) {
1469 if (p->air < PL_AIR) {
1470 int a = min(max(p->air, 0), MAXAIR) * 100 / MAXAIR;
1471 glDisable(GL_BLEND);
1472 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1473 glDisable(GL_TEXTURE_2D);
1474 R_gl_set_color(0xC8);
1475 R_gl_draw_quad(10, 49, a, 2);
1478 if (p->drawst & PL_DRAWLIFE) {
1479 Z_gotoxy(10, 7);
1480 Z_printhf("%3d%%", p->life);
1482 if (p->drawst & PL_DRAWARMOR) {
1483 Z_gotoxy(10, 7 + 19);
1484 Z_printhf("%3d%%", p->armor);
1486 if (p->drawst & PL_DRAWWPN) {
1487 switch(p->wpn) {
1488 case 2:
1489 case 5:
1490 i = p->ammo;
1491 break;
1492 case 3:
1493 case 4:
1494 case 9:
1495 i = p->shel;
1496 break;
1497 case 6:
1498 i = p->rock;
1499 break;
1500 case 7:
1501 case 8:
1502 i = p->cell;
1503 break;
1504 case 10:
1505 i = p->fuel;
1506 break;
1507 default:
1508 i = -1;
1509 break;
1511 // weapon
1512 if (p->wpn >= 0) {
1513 R_gl_draw_image(&sth[12 + p->wpn], st - 88, 58 + 19, 0);
1515 // ammo
1516 if (p->wpn >= 2) {
1517 Z_gotoxy(st - 10 - 5 * 14, 58 + 2);
1518 Z_printhf("%5d", i);
1521 if (p->drawst & PL_DRAWFRAG && g_dm) {
1522 Z_gotoxy(st - 5 - 5 * 14, 77 + 5);
1523 Z_printhf("%5d", p->frag);
1525 if (p->drawst & PL_DRAWKEYS) {
1526 int x, k, n;
1527 for (k = p->keys >> 4, n = 0, x = st - 75; n < 3; n++, k >>= 1, x += 9) {
1528 if (k & 1) {
1529 R_gl_draw_image(&keys[n], x, 91, 0);
1533 if (p->drawst & PL_DRAWLIVES && !_2pl) {
1534 Z_gotoxy(st - 35, 17);
1535 Z_printhf("%d", p->lives);
1537 glPopMatrix();
1540 /* --- Game --- */
1542 static void pl_info (player_t *p, int x, int y) {
1543 dword t = p->kills * 10920 / g_time;
1544 Z_gotoxy(x + 25, y); Z_printbf("KILLS");
1545 Z_gotoxy(x + 25, y + 15); Z_printbf("KPM");
1546 Z_gotoxy(x + 25, y + 30); Z_printbf("SECRETS %u / %u", p->secrets, sw_secrets);
1547 Z_gotoxy(x + 255, y); Z_printbf("%u", p->kills);
1548 Z_gotoxy(x + 255, y + 15); Z_printbf("%u.%u", t / 10, t % 10);
1551 static void R_draw_intermission (void) {
1552 int cx = SCRW / 2;
1553 word hr, mn, sc, h;
1554 Z_gotoxy(cx - 14*12/2, 20);
1555 Z_printbf("LEVEL COMPLETE");
1556 Z_calc_time(g_time, &hr, &mn, &sc);
1557 Z_gotoxy(cx - 12*12/2, 40);
1558 Z_printbf("TIME %u:%02u:%02u", hr, mn, sc);
1559 h = 40 + SCRH / 10;
1560 if (_2pl) {
1561 Z_gotoxy(cx - 10*12/2, h);
1562 Z_printbf("PLAYER ONE");
1563 h += 20;
1565 pl_info(&pl1, cx - 160, h);
1566 if (_2pl) {
1567 h += 30 + SCRH / 10;
1568 Z_gotoxy(cx - 10*12/2, h);
1569 Z_printbf("PLAYER TWO");
1570 h += 20;
1571 pl_info(&pl2, cx - 160, h);
1575 static void W_act (void) {
1576 int i, a;
1577 if (g_time % 3 == 0) {
1578 for (i = 1; i < max_textures; i++) {
1579 a = walani[i];
1580 if (a != 0) {
1581 anic[a]++;
1582 if (anip[a][anic[a]].res == -1) {
1583 anic[a] = 0;
1585 walp[i] = anip[a][anic[a]];
1591 void R_draw (void) {
1592 W_act();
1593 glClearColor(0, 0, 0, 1);
1594 glClear(GL_COLOR_BUFFER_BIT);
1595 glEnable(GL_SCISSOR_TEST);
1596 R_gl_setmatrix();
1597 switch (g_st) {
1598 case GS_ENDANIM:
1599 case GS_END2ANIM:
1600 case GS_DARKEN:
1601 case GS_BVIDEO:
1602 case GS_EVIDEO:
1603 case GS_END3ANIM:
1604 break;
1605 case GS_TITLE:
1606 R_gl_draw_image_ext(&scrnh[0], 0, 0, SCRW, SCRH);
1607 break;
1608 case GS_INTER:
1609 R_gl_draw_image_ext(&scrnh[1], 0, 0, SCRW, SCRH);
1610 R_draw_intermission();
1611 break;
1612 case GS_ENDSCR:
1613 R_gl_draw_image_ext(&scrnh[2], 0, 0, SCRW, SCRH);
1614 break;
1615 case GS_GAME:
1616 if (_2pl) {
1617 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH / 2);
1618 R_draw_player_view(&pl2, 0, SCRH / 2, SCRW, SCRH / 2);
1619 } else {
1620 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH);
1622 R_gl_setclip(0, 0, SCRW, SCRH);
1623 break;
1625 GM_draw();
1626 Y_swap_buffers();
1629 static void R_alloc (void) {
1630 char s[10];
1631 int i, j, n;
1632 logo("R_alloc: load graphics\n");
1633 /* Game */
1634 scrnh[0] = R_gl_loadimage("TITLEPIC");
1635 scrnh[1] = R_gl_loadimage("INTERPIC");
1636 scrnh[2] = R_gl_loadimage("ENDPIC");
1637 for (i = 0; i < 2; i++) {
1638 sprintf(s, "LTN%c", '1' + i);
1639 for (j = 0; j < 2; j++) {
1640 ltn[i][j] = Z_getspr(s, j, 0, NULL);
1643 /* Smoke */
1644 for (i = 0; i < SMSN; i++) {
1645 smk_spr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_smoke_spr);
1647 for (i = 0; i < FLSN; i++) {
1648 smk_fspr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_flame_spr);
1650 /* Effects */
1651 for (i = 0; i < 10; i++) {
1652 fx_spr[i] = Z_getspr("TFOG", i, 0, fx_sprd + i);
1654 for (; i < 15; i++) {
1655 fx_spr[i] = Z_getspr("IFOG", i - 10, 0, fx_sprd + i);
1657 /* Weapons */
1658 for (i = 0; i < 4; i++) {
1659 wp_spr[i * 2] = Z_getspr("MISL", i, 1, wp_sprd + i * 2);
1660 wp_spr[i * 2 + 1] = Z_getspr("MISL", i, 2, wp_sprd + i * 2 + 1);
1662 for (; i < 6; i++) {
1663 wp_spr[i * 2] = Z_getspr("PLSS", i - 4, 1, wp_sprd + i * 2);
1664 wp_spr[i * 2 + 1] = Z_getspr("PLSS", i - 4, 2, wp_sprd + i * 2 + 1);
1666 for (; i < 11; i++) {
1667 wp_spr[i * 2] = Z_getspr("PLSE", i - 6, 1, wp_sprd + i * 2);
1668 wp_spr[i * 2 + 1] = Z_getspr("PLSE", i - 6, 2, wp_sprd + i * 2 + 1);
1670 for (; i < 13; i++) {
1671 wp_spr[i * 2] = Z_getspr("APLS", i - 11, 1, wp_sprd + i * 2);
1672 wp_spr[i * 2 + 1] = Z_getspr("APLS", i - 11, 2, wp_sprd + i * 2 + 1);
1674 for (; i < 18; i++) {
1675 wp_spr[i * 2] = Z_getspr("APBX", i - 13, 1, wp_sprd + i * 2);
1676 wp_spr[i * 2 + 1] = Z_getspr("APBX", i - 13, 2, wp_sprd + i * 2 + 1);
1678 for(; i < 20; i++) {
1679 wp_spr[i * 2] = Z_getspr("BFS1", i - 18, 1, wp_sprd + i * 2);
1680 wp_spr[i * 2 + 1] = Z_getspr("BFS1", i - 18, 2, wp_sprd + i * 2 + 1);
1682 for (; i < 26; i++) {
1683 wp_spr[i * 2] = Z_getspr("BFE1", i - 20, 1, wp_sprd + i * 2);
1684 wp_spr[i * 2 + 1] = Z_getspr("BFE1", i - 20, 2, wp_sprd + i * 2 + 1);
1686 for (; i < 30; i++) {
1687 wp_spr[i * 2] = Z_getspr("BFE2", i - 26, 1, wp_sprd + i * 2);
1688 wp_spr[i * 2 + 1] = Z_getspr("BFE2", i - 26, 2, wp_sprd + i * 2 + 1);
1690 for (; i < 32; i++) {
1691 wp_spr[i * 2] = Z_getspr("MISL", i - 30 + 4, 1, wp_sprd + i * 2);
1692 wp_spr[i * 2 + 1] = Z_getspr("MISL", i - 30 + 4, 2, wp_sprd + i * 2 + 1);
1694 for (; i < 37; i++) {
1695 wp_spr[i * 2] = Z_getspr("BAL1", i - 32, 1, wp_sprd + i * 2);
1696 wp_spr[i * 2 + 1] = Z_getspr("BAL1", i - 32, 2, wp_sprd + i * 2 + 1);
1698 for (; i < 42; i++) {
1699 wp_spr[i * 2] = Z_getspr("BAL7", i - 37, 1, wp_sprd + i * 2);
1700 wp_spr[i * 2 + 1] = Z_getspr("BAL7", i - 37, 2, wp_sprd + i * 2 + 1);
1702 for (; i < 47; i++) {
1703 wp_spr[i * 2] = Z_getspr("BAL2", i - 42, 1, wp_sprd + i * 2);
1704 wp_spr[i * 2 + 1] = Z_getspr("BAL2", i - 42, 2, wp_sprd + i * 2 + 1);
1706 for (; i < 49; i++) {
1707 wp_spr[i * 2] = Z_getspr("MANF", i - 47, 1, wp_sprd + i * 2);
1708 wp_spr[i * 2 + 1] = Z_getspr("MANF", i - 47, 2, wp_sprd + i * 2 + 1);
1710 /* Items */
1711 static const char snm[18][4] = {
1712 "CLIP", "SHEL", "ROCK", "CELL", "AMMO", "SBOX", "BROK", "CELP",
1713 "STIM", "MEDI", "BPAK",
1714 "CSAW", "SHOT", "SGN2", "MGUN", "LAUN", "PLAS", "BFUG"
1715 };
1716 static const char n4[4][4] = {
1717 "SOUL", "SMRT", "SMGT", "SMBT"
1718 };
1719 static const char n3[2][4] = {
1720 "GOR1", "FCAN"
1721 };
1722 for (i = 0; i < 18; i++) {
1723 item_spr[i] = Z_getspr(snm[i], 0, 0, item_sprd + i);
1725 for (; i < 20; i++) {
1726 item_spr[i] = Z_getspr("ARM1", i - 18, 0, item_sprd + i);
1727 item_spr[i + 2] = Z_getspr("ARM2", i - 18, 0, item_sprd + i);
1729 i+=2;
1730 for (; i < 26; i++) {
1731 item_spr[i] = Z_getspr("MEGA", i - 22, 0, item_sprd + i);
1733 for (; i < 30; i++) {
1734 item_spr[i] = Z_getspr("PINV", i - 26, 0, item_sprd + i);
1736 item_spr[30] = Z_getspr("AQUA", 0, 0, item_sprd + 30);
1737 item_spr[31] = Z_getspr("KEYR", 0, 0, item_sprd + 31);
1738 item_spr[32] = Z_getspr("KEYG", 0, 0, item_sprd + 32);
1739 item_spr[33] = Z_getspr("KEYB", 0, 0, item_sprd + 33);
1740 item_spr[34] = Z_getspr("SUIT", 0, 0, item_sprd + 34);
1741 for (n = 35, j = 0; j < 4; j++) {
1742 for (i = 0; i < 4; i++, n++) {
1743 item_spr[n] = Z_getspr(n4[j], i, 0, item_sprd + n);
1746 for (j = 0; j < 2; j++) {
1747 for (i = 0; i < 3; i++, n++) {
1748 item_spr[n] = Z_getspr(n3[j], i, 0, item_sprd + n);
1751 item_spr[57] = Z_getspr("GUN2", 0, 0, item_sprd + 57);
1752 /* Player */
1753 for (i = 0; i < 27; i++) {
1754 plr_spr[i * 2] = Z_getspr("PLAY", i, 1, plr_sprd + i * 2);
1755 plr_msk[i * 2] = R_gl_get_special_spr("PLAY", i, 1, &R_extract_mask_spr);
1756 plr_spr[i * 2 + 1] = Z_getspr("PLAY", i, 2, plr_sprd + i * 2 + 1);
1757 plr_msk[i * 2 + 1] = R_gl_get_special_spr("PLAY", i, 2, &R_extract_mask_spr);
1759 strncpy(s, "PWPx", 4);
1760 for (i = 1; i < 11; i++) {
1761 s[3] = (i < 10 ? '0' : 'A' - 10) + i;
1762 for (j = 0; j < 6; j++) {
1763 plr_wpn[i][j] = Z_getspr(s, j, 1, NULL);
1766 /* Monsters */
1767 static const char msn[MN_TN][4] = {
1768 "SARG", "TROO", "POSS", "SPOS", "CYBR", "CPOS", "BOSS", "BOS2", "HEAD", "SKUL",
1769 "PAIN", "SPID", "BSPI", "FATT", "SKEL", "VILE", "FISH", "BAR1", "ROBO", "PLAY"
1770 };
1771 static const int mms[MN_TN] = {
1772 14*2, 21*2, 21*2, 21*2, 16*2, 20*2, 15*2, 15*2, 12*2, 11*2,
1773 13*2, 19*2, 16*2, 20*2, 17*2, 29*2, 6*2, 2*2, 17*2, 23*2
1774 };
1775 mn_sgun[0] = Z_getspr("PWP4", 0, 1, NULL);
1776 mn_sgun[1] = Z_getspr("PWP4", 1, 1, NULL);
1777 for (j = 0; j < MN_TN; j++) {
1778 for (i = 0; i < mms[j]; i++) {
1779 mn_spr[j][i] = Z_getspr(msn[j], i / 2, (i & 1) + 1, &mn_sprd[j][i]);
1780 if (j == MN_MAN - 1) {
1781 mn_man_msk[i] = R_gl_get_special_spr(msn[j], i / 2, (i & 1) + 1, &R_extract_mask_spr);
1784 if (j == MN_BARREL - 1) {
1785 for (i = 4; i < 14; i++) {
1786 mn_spr[j][i] = Z_getspr("BEXP", i / 2 - 2, (i & 1) + 1, &mn_sprd[j][i]);
1790 for (i = 0; i < 8; i++) {
1791 mn_fspr[i] = Z_getspr("FIRE", i, 0, NULL);
1793 pl_spr[0] = Z_getspr("PLAY", 'N' - 'A', 0, NULL);
1794 pl_msk[0] = R_gl_get_special_spr("PLAY", 'N' - 'A', 0, &R_extract_mask_spr);
1795 pl_spr[1] = Z_getspr("PLAY", 'W' - 'A', 0, NULL);
1796 pl_msk[1] = R_gl_get_special_spr("PLAY", 'W' - 'A', 0, &R_extract_mask_spr);
1797 /* Misc */
1798 static const char mnm[22][8]={
1799 "STTNUM0", "STTNUM1", "STTNUM2", "STTNUM3", "STTNUM4",
1800 "STTNUM5", "STTNUM6", "STTNUM7", "STTNUM8", "STTNUM9",
1801 "STTMINUS", "STTPRCNT",
1802 "FISTA0", "CSAWA0", "PISTA0", "SHOTA0", "SGN2A0", "MGUNA0", "LAUNA0",
1803 "PLASA0", "BFUGA0", "GUN2A0"
1804 };
1805 stone = R_gl_loadimage("STONE");
1806 stone2 = R_gl_loadimage("STONE2");
1807 keys[0] = R_gl_loadimage("KEYRA0");
1808 keys[1] = R_gl_loadimage("KEYGA0");
1809 keys[2] = R_gl_loadimage("KEYBA0");
1810 for (i = 0; i < 22; i++) {
1811 sth[i] = R_gl_loadimage(mnm[i]);
1813 strcpy(s, "STBF_*");
1814 for (i = '!'; i < 160; i++) {
1815 s[5] = i;
1816 bfh[i - '!'] = R_gl_getimage(F_findres(s));
1818 for (i = '!'; i < 160; i++) {
1819 sprintf(s, "STCFN%03d", i);
1820 sfh[i - '!'] = R_gl_getimage(F_findres(s));
1822 strcpy(s, "WINUM*");
1823 for (i = '0'; i <= '9'; i++) {
1824 s[5] = i;
1825 bfh[i - '!'] = R_gl_loadimage(s);
1827 bfh[':' - '!'] = R_gl_loadimage("WICOLON");
1828 // menu
1829 msklh[0] = R_gl_loadimage("M_SKULL1");
1830 msklh[1] = R_gl_loadimage("M_SKULL2");
1831 mbarl = R_gl_loadimage("M_THERML");
1832 mbarm = R_gl_loadimage("M_THERMM");
1833 mbarr = R_gl_loadimage("M_THERMR");
1834 mbaro = R_gl_loadimage("M_THERMO");
1835 mslotl = R_gl_loadimage("M_LSLEFT");
1836 mslotm = R_gl_loadimage("M_LSCNTR");
1837 mslotr = R_gl_loadimage("M_LSRGHT");
1838 // walls
1839 for (i = 1; i < ANIT; i++) {
1840 for (j = 0; j < 5 && anm[i - 1][j]; j++) {
1841 anip[i][j] = R_gl_loadimage(anm[i - 1][j]);
1843 for(; j < 5; j++) {
1844 anip[i][j] = (image) {
1845 .n = NULL,
1846 .w = 8,
1847 .h = 8,
1848 .res = -1,
1849 };
1854 static void R_reload_textures (void);
1856 void R_set_videomode (int w, int h, int fullscreen) {
1857 assert(w > 0);
1858 assert(h > 0);
1859 int was = Y_videomode_setted();
1860 if (root != NULL) {
1861 R_cache_free(root, 0);
1862 root = NULL;
1864 int res = Y_set_videomode_opengl(w, h, fullscreen);
1865 if (res == 0) {
1866 if (was == 0) {
1867 ERR_failinit("Unable to set video mode\n");
1870 Y_get_videomode(&screen_width, &screen_height);
1871 screen_full = Y_get_fullscreen();
1872 screen_scale = max(1, screen_width / 320);
1873 root = R_cache_new();
1874 assert(root);
1875 R_alloc();
1876 R_reload_textures();
1879 static int video_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
1880 static int cur;
1881 static int w, h, fullscreen;
1882 static char buf[16];
1883 static int buflen;
1884 static int vmode;
1885 const videomode_t *v;
1886 enum { VIDEOMODE, FULLSCREEN, APPLY, __NUM__ };
1887 static const simple_menu_t sm = {
1888 GM_BIG, "Video", NULL,
1890 { "Mode: ", NULL },
1891 { "Fullscreen: ", NULL },
1892 { "Apply ", NULL },
1894 };
1895 if (msg->type == GM_ENTER) {
1896 Y_get_videomode(&w, &h);
1897 fullscreen = Y_get_fullscreen();
1898 v = Y_get_videomode_list_opengl(fullscreen);
1899 vmode = 0;
1900 while (vmode < v->n && v->modes[vmode].w != w && v->modes[vmode].h != h) {
1901 vmode += 1;
1903 if (vmode < v->n) {
1904 w = v->modes[vmode].w;
1905 h = v->modes[vmode].h;
1907 snprintf(buf, 16, "%ix%i", w, h);
1908 buflen = strlen(buf);
1909 return 1;
1911 if (i == VIDEOMODE) {
1912 switch (msg->type) {
1913 case GM_GETSTR: return GM_init_str(msg, buf, buflen);
1914 case GM_SELECT:
1915 v = Y_get_videomode_list_opengl(fullscreen);
1916 vmode = vmode + 1 >= v->n ? 0 : vmode + 1;
1917 if (v->n > 0) {
1918 w = v->modes[vmode].w;
1919 h = v->modes[vmode].h;
1920 } else {
1921 Y_get_videomode(&w, &h);
1923 snprintf(buf, 16, "%ix%i", w, h);
1924 buflen = strlen(buf);
1925 return 1;
1927 } else if (i == FULLSCREEN) {
1928 switch (msg->type) {
1929 case GM_GETSTR: return GM_init_str(msg, fullscreen ? "Yes" : "No ", 3);
1930 case GM_SELECT: fullscreen = !fullscreen; return 1;
1932 } else if (i == APPLY) {
1933 switch (msg->type) {
1934 case GM_SELECT: R_set_videomode(w, h, fullscreen); return 1;
1937 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
1940 static const menu_t video_menu = {
1941 NULL, &video_menu_handler
1942 };
1944 const menu_t *R_menu (void) {
1945 return &video_menu;
1948 const cfg_t *R_args (void) {
1949 static const cfg_t args[] = {
1950 { "fullscr", &init_screen_full, Y_SW_ON },
1951 { "window", &init_screen_full, Y_SW_OFF },
1952 { "width", &init_screen_width, Y_DWORD },
1953 { "height", &init_screen_height, Y_DWORD },
1954 { NULL, NULL, 0 } // end
1955 };
1956 return args;
1959 const cfg_t *R_conf (void) {
1960 static const cfg_t conf[] = {
1961 { "sky", &w_horiz, Y_SW_ON },
1962 { "fullscreen", &screen_full, Y_SW_ON },
1963 { "screen_width", &screen_width, Y_DWORD },
1964 { "screen_height", &screen_height, Y_DWORD },
1965 { NULL, NULL, 0 } // end
1966 };
1967 return conf;
1970 void R_init (void) {
1971 logo("R_init: intialize opengl render\n");
1972 R_init_playpal();
1973 init_screen_width = init_screen_width > 0 ? init_screen_width : screen_width;
1974 init_screen_height = init_screen_height > 0 ? init_screen_height : screen_height;
1975 init_screen_full = init_screen_full != 0xFF ? init_screen_full : screen_full;
1976 R_set_videomode(init_screen_width, init_screen_height, init_screen_full);
1979 void R_done (void) {
1980 R_cache_free(root, 1);
1981 Y_unset_videomode();
1982 root = NULL;
1985 void R_get_name (int n, char s[8]) {
1986 assert(n >= 0 && n < 256);
1987 if (walp[n].res == -1) {
1988 memset(s, 0, 8);
1989 } else if (walp[n].res == -2) {
1990 memcpy(s, "_WATER_", 8);
1991 s[7] = '0' + (intptr_t)walp[n].n - 1;
1992 } else if (walani[n] > 0) {
1993 memcpy(s, anm[walani[n] - 1][0], 8);
1994 } else {
1995 F_getresname(s, walp[n].res & 0x7FFF);
1999 static short getani (char n[8]) {
2000 short i = 0;
2001 while (i < ANIT - 1 && strncasecmp(n, anm[i][0], 8) != 0) {
2002 i++;
2004 return i < ANIT - 1 ? i + 1 : 0;
2007 int R_get_special_id (int n) {
2008 assert(n >= 0 && n <= 256);
2009 return walp[n].res == -2 ? (intptr_t)walp[n].n : -1;
2012 static void R_reload_textures (void) {
2013 int i;
2014 char s[8];
2015 for (i = 0; i < max_textures; i++) {
2016 R_get_name(i, s);
2017 if (walp[i].res >= 0) {
2018 walp[i] = R_gl_getimage(walp[i].res);
2021 if (horiz.n) {
2022 horiz = R_gl_getimage(horiz.res);
2026 void R_begin_load (void) {
2027 int i;
2028 for (i = 0; i < 256; i++) {
2029 if (walp[i].n != NULL && walp[i].res >= 0 && walani[i] == 0) {
2030 R_gl_free_image(&walp[i]);
2032 memset(&walp[i], 0, sizeof(image));
2033 walp[i].res = -1;
2034 walswp[i] = i;
2035 walani[i] = 0;
2037 memset(anic, 0, sizeof(anic));
2038 max_wall_width = 0;
2039 max_wall_height = 0;
2040 max_textures = 1;
2043 void R_load (char s[8], int f) {
2044 assert(max_textures < 256);
2045 if (!s[0]) {
2046 walp[max_textures] = (image) {
2047 .n = NULL,
2048 .x = 0,
2049 .y = 0,
2050 .w = 0,
2051 .h = 0,
2052 .res = -1,
2053 };
2054 } else if (strncasecmp(s, "_WATER_", 7) == 0) {
2055 walp[max_textures] = (image) {
2056 .n = (void*)((intptr_t)s[7] - '0' + 1),
2057 .x = 0,
2058 .y = 0,
2059 .w = 8,
2060 .h = 8,
2061 .res = -2,
2062 };
2063 } else {
2064 walp[max_textures] = R_gl_loadimage(s);
2065 if (f) {
2066 walp[max_textures].res |= 0x8000;
2068 if (s[0] == 'S' && s[1] == 'W' && s[4] == '_') {
2069 walswp[max_textures] = 0;
2071 walani[max_textures] = getani(s);
2073 max_wall_width = max(max_wall_width, walp[max_textures].w);
2074 max_wall_height = max(max_wall_height, walp[max_textures].h);
2075 max_textures++;
2078 void R_end_load (void) {
2079 int i, j, k, g;
2080 char s[8];
2081 j = max_textures;
2082 for (i = 1; i < 256 && j < 256; i++) {
2083 if (walswp[i] == 0) {
2084 R_get_name(i, s);
2085 s[5] ^= 1;
2086 g = F_getresid(s) | (walp[i].res & 0x8000);
2087 k = 1;
2088 while (k < 256 && walp[k].res != g) {
2089 k += 1;
2091 if (k >= 256) {
2092 k = j;
2093 j += 1;
2094 max_textures += 1;
2095 walp[k] = R_gl_getimage(g);
2096 walf[k] = g & 0x8000 ? 1 : 0;
2098 walswp[i] = k;
2099 walswp[k] = i;
2104 void R_loadsky (int sky) {
2105 char s[6];
2106 strcpy(s, "RSKYx");
2107 s[4] = '0' + sky;
2108 R_gl_free_image(&horiz);
2109 horiz = R_gl_loadimage(s);
2112 void R_switch_texture (int x, int y) {
2113 assert(x >= 0 && x < FLDW);
2114 assert(y >= 0 && y < FLDH);
2115 fldb[y][x] = walswp[fldb[y][x]];
2118 int R_get_swp (int n) {
2119 assert(n >= 0 && n < 256);
2120 return walswp[n];