DEADSOFTWARE

common: move endianness converters to common
[flatwaifu.git] / src / gl / render.c
1 /* Copyright (C) 2020 SovietPony
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
16 #include "glob.h"
17 #include "render.h"
18 #include "system.h"
19 #include "files.h"
20 #include "memory.h"
21 #include "misc.h"
22 #include "error.h"
24 #include "menu.h"
25 #include "game.h"
26 #include "dots.h"
27 #include "items.h"
29 #include "sound.h"
30 #include "music.h"
32 #include "fx.h"
33 #include "player.h"
34 #include "monster.h"
35 #include "weapons.h"
36 #include "smoke.h"
37 #include "view.h"
38 #include "switch.h" // sw_secrets
40 #include "cp866.h"
41 #include "common/endianness.h"
43 #ifdef __APPLE__
44 # include <OpenGL/gl.h>
45 #else
46 # include <GL/gl.h>
47 #endif
48 #include <stdarg.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <assert.h>
53 #define VGA_TRANSPARENT_COLOR 0
54 #define DEFAULT_SKY_COLOR 0x97
55 #define MANCOLOR 0xD0
56 #define PLAYER_COLOR_OFFSET 7
57 #define MAXAIR 1091
58 #define ANIT 5
59 #define PL_FLASH 90
61 #pragma pack(1)
62 typedef struct vgaimg {
63 word w, h;
64 short x, y;
65 byte data[];
66 } vgaimg;
68 typedef struct rgb {
69 byte r, g, b;
70 } rgb;
72 typedef struct rgba {
73 byte r, g, b, a;
74 } rgba;
75 #pragma pack()
77 typedef struct node {
78 struct cache *base;
79 struct node *left, *right;
80 struct node *up;
81 int l, t, r, b;
82 int leaf;
83 } node;
85 typedef struct cache {
86 struct cache *next;
87 struct node root;
88 GLuint id;
89 } cache;
91 typedef struct image {
92 node *n;
93 GLint x, y;
94 GLuint w, h;
95 int res;
96 } image;
98 /* Render Specific */
99 static int SCRW;
100 static int SCRH;
101 static float screen_scale;
102 static int screen_width = 320;
103 static int screen_height = 200;
104 static byte screen_full = 0;
105 static int init_screen_width = 0;
106 static int init_screen_height = 0;
107 static byte init_screen_full = 0xFF;
108 static rgb playpal[256];
109 static byte bright[256];
110 static GLuint lastTexture;
111 static cache *root;
113 /* Game */
114 static image scrnh[3]; // TITLEPIC INTERPIC ENDPIC
115 static image ltn[2][2];
117 /* Smoke */
118 static image smk_spr[SMSN];
119 static image smk_fspr[FLSN];
121 /* Effects */
122 static image fx_spr[15];
123 static char fx_sprd[15];
125 /* Weapons */
126 static image wp_spr[49*2];
127 static char wp_sprd[49*2];
129 /* Items */
130 static image item_spr[58];
131 static char item_sprd[58];
133 /* Player */
134 static image plr_spr[27*2];
135 static image plr_msk[27*2];
136 static char plr_sprd[27*2];
137 static image plr_wpn[11][6];
139 /* Monsters */
140 static image pl_spr[2];
141 static image pl_msk[2];
142 static image mn_spr[MN_TN][29*2];
143 static image mn_man_msk[29*2];
144 static char mn_sprd[MN_TN][29*2];
145 static image mn_fspr[8];
146 static image mn_sgun[2];
148 /* Misc */
149 static image sth[22];
150 static image bfh[160 - '!'];
151 static image sfh[160 - '!'];
152 static image stone;
153 static image stone2;
154 static image keys[3];
155 static int prx = 0;
156 static int pry = 0;
158 /* Menu */
159 static int gm_tm;
160 static image msklh[2];
161 static image mbarl;
162 static image mbarm;
163 static image mbarr;
164 static image mbaro;
165 static image mslotl;
166 static image mslotm;
167 static image mslotr;
169 /* Map */
170 static const char *anm[ANIT - 1][5] = {
171 {"WALL22_1", "WALL23_1", "WALL23_2", NULL, NULL},
172 {"WALL58_1", "WALL58_2", "WALL58_3", NULL, NULL},
173 {"W73A_1", "W73A_2", NULL, NULL, NULL},
174 {"RP2_1", "RP2_2", "RP2_3", "RP2_4", NULL}
175 };
176 static byte w_horiz = 1;
177 static int max_wall_width;
178 static int max_wall_height;
179 static int max_textures;
180 static image walp[256];
181 static byte walswp[256];
182 static byte walani[256];
183 static image anip[ANIT][5];
184 static byte anic[ANIT];
185 static image horiz;
187 /* Texture cache */
189 // https://blackpawn.com/texts/lightmaps/
190 static node *R_node_alloc (node *p, int w, int h) {
191 assert(p);
192 assert(w > 0);
193 assert(h > 0);
194 if (p->left) {
195 assert(p->right);
196 node *n = R_node_alloc(p->left, w, h);
197 return n ? n : R_node_alloc(p->right, w, h);
198 } else {
199 int pw = p->r - p->l + 1;
200 int ph = p->b - p->t + 1;
201 if (p->leaf || pw < w || ph < h) {
202 return NULL;
203 } else if (pw == w && ph == h) {
204 p->leaf = 1;
205 return p;
206 } else {
207 p->left = malloc(sizeof(node));
208 p->right = malloc(sizeof(node));
209 if (pw - w > ph - h) {
210 *p->left = (node) {
211 .up = p,
212 .l = p->l,
213 .t = p->t,
214 .r = p->l + w - 1,
215 .b = p->b
216 };
217 *p->right = (node) {
218 .up = p,
219 .l = p->l + w,
220 .t = p->t,
221 .r = p->r,
222 .b = p->b
223 };
224 } else {
225 *p->left = (node) {
226 .up = p,
227 .l = p->l,
228 .t = p->t,
229 .r = p->r,
230 .b = p->t + h - 1
231 };
232 *p->right = (node) {
233 .up = p,
234 .l = p->l,
235 .t = p->t + h,
236 .r = p->r,
237 .b = p->b
238 };
240 return R_node_alloc(p->left, w, h);
245 static int R_node_have_leaf (node *n) {
246 return n && (n->leaf || R_node_have_leaf(n->left) || R_node_have_leaf(n->right));
249 static void R_node_free_recursive (node *n) {
250 if (n) {
251 R_node_free_recursive(n->left);
252 R_node_free_recursive(n->right);
253 free(n);
257 static void R_node_free (node *n) {
258 if (n) {
259 //logo("free node %p {%i:%i:%i:%i}\n", n, n->l, n->t, n->r, n->b);
260 assert(n->leaf);
261 assert(n->left == NULL);
262 assert(n->right == NULL);
263 n->leaf = 0;
264 n->base = NULL;
265 node *p = n->up;
266 while (p != NULL) {
267 assert(p->leaf == 0);
268 assert(p->left);
269 assert(p->right);
270 if (R_node_have_leaf(p) == 0) {
271 R_node_free_recursive(p->left);
272 p->left = NULL;
273 R_node_free_recursive(p->right);
274 p->right = NULL;
275 p = p->up;
276 } else {
277 p = NULL;
283 static void R_cache_get_max_texture_size (int *w, int *h) {
284 GLint size = 0;
285 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
286 size = min(max(size, 0), 512); // more can be buggy on older hardware
287 *w = size;
288 *h = size;
291 static void R_gl_bind_texture (GLuint id) {
292 if (id != lastTexture) {
293 glBindTexture(GL_TEXTURE_2D, id);
297 static cache *R_cache_new (void) {
298 int w, h;
299 GLuint id;
300 cache *c = NULL;
301 R_cache_get_max_texture_size(&w, &h);
302 if (w && h) {
303 glGenTextures(1, &id);
304 if (id) {
305 R_gl_bind_texture(id);
306 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
307 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
308 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
309 c = malloc(sizeof(cache));
310 if (c != NULL) {
311 *c = (cache) {
312 .id = id,
313 .root.r = w - 1,
314 .root.b = h - 1
315 };
316 } else {
317 glDeleteTextures(1, &id);
321 //logo("new cache %p\n", c);
322 return c;
325 static void R_cache_free (cache *root, int freetexture) {
326 cache *next;
327 cache *c = root;
328 while (c != NULL) {
329 next = c->next;
330 R_node_free_recursive(c->root.left);
331 R_node_free_recursive(c->root.right);
332 if (freetexture && c->id != 0) {
333 glDeleteTextures(1, &c->id);
335 free(c);
336 c = next;
340 static node *R_cache_alloc (cache *root, int w, int h) {
341 assert(root);
342 assert(w > 0 && h > 0);
343 node *n = NULL;
344 cache *p = NULL;
345 cache *c = root;
346 int maxw, maxh;
347 R_cache_get_max_texture_size(&maxw, &maxh);
348 if (w <= maxw && h <= maxh) {
349 while (c && !n) {
350 n = R_node_alloc(&c->root, w, h);
351 if (n) {
352 assert(n->leaf);
353 n->base = c;
355 p = c;
356 c = c->next;
358 if (!n) {
359 c = R_cache_new();
360 if (c) {
361 p->next = c;
362 n = R_node_alloc(&c->root, w, h);
363 if (n) {
364 assert(n->leaf);
365 n->base = c;
370 if (n) {
371 //logo("new node %p {%i:%i:%i:%i}\n", n, n->l, n->t, n->r, n->b);
372 } else {
373 logo("new node failed {%i:%i}\n", w, h);
375 return n;
378 static void R_cache_update (node *n, const void *data, int x, int y, int w, int h) {
379 assert(n);
380 assert(n->leaf);
381 assert(n->base);
382 assert(data);
383 assert(x >= 0);
384 assert(y >= 0);
385 assert(n->l + x + w - 1 <= n->r);
386 assert(n->t + y + h - 1 <= n->b);
387 R_gl_bind_texture(n->base->id);
388 glTexSubImage2D(GL_TEXTURE_2D, 0, n->l + x, n->t + y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
391 /* Generic helpers */
393 static void R_init_playpal (void) {
394 int i;
395 byte *vgapal = M_lock(F_getresid("PLAYPAL"));
396 for (i = 0; i < 256; i++) {
397 playpal[i] = (rgb) {
398 .r = vgapal[i * 3 + 0] * 255 / 63,
399 .g = vgapal[i * 3 + 1] * 255 / 63,
400 .b = vgapal[i * 3 + 2] * 255 / 63,
401 };
402 bright[i] = ((int)vgapal[i * 3 + 0] + vgapal[i * 3 + 1] + vgapal[i * 3 + 2]) * 8 / (63 * 3);
404 M_unlock(vgapal);
407 static vgaimg *R_getvga (int id) {
408 int loaded = M_was_locked(id);
409 vgaimg *v = M_lock(id);
410 if (v != NULL && !loaded) {
411 v->w = short2host(v->w);
412 v->h = short2host(v->h);
413 v->x = short2host(v->x);
414 v->y = short2host(v->y);
416 return v;
419 static rgba *R_extract_flame_spr (vgaimg *v) {
420 static const byte flametab[16] = {
421 0xBC, 0xBA, 0xB8, 0xB6, 0xB4, 0xB2, 0xB0, 0xD5,
422 0xD6, 0xD7, 0xA1, 0xA0, 0xE3, 0xE2, 0xE1, 0xE0
423 };
424 int i, j;
425 rgba *s = malloc(v->w * v->h * sizeof(rgba));
426 if (s != NULL) {
427 for (j = 0; j < v->h; j++) {
428 for (i = 0; i < v->w; i++) {
429 int k = j * v->w + i;
430 byte c = v->data[k] + bright[DEFAULT_SKY_COLOR];
431 s[k] = (rgba) {
432 .r = playpal[flametab[c]].r,
433 .g = playpal[flametab[c]].g,
434 .b = playpal[flametab[c]].b,
435 .a = v->data[k] == VGA_TRANSPARENT_COLOR ? 0x00 : 0xFF,
436 };
440 return s;
443 static rgba *R_extract_smoke_spr (vgaimg *v) {
444 int i, j;
445 rgba *s = malloc(v->w * v->h * sizeof(rgba));
446 if (s != NULL) {
447 for (j = 0; j < v->h; j++) {
448 for (i = 0; i < v->w; i++) {
449 int k = j * v->w + i;
450 byte c = ((v->data[k] + bright[DEFAULT_SKY_COLOR]) + 0x60) ^ 0x0F;
451 byte a = 0xFF - ((int)playpal[c].r + playpal[c].g + playpal[c].b) / 3;
452 s[k] = (rgba) {
453 .r = playpal[c].r,
454 .g = playpal[c].g,
455 .b = playpal[c].b,
456 .a = v->data[k] == VGA_TRANSPARENT_COLOR ? 0x00 : a,
457 };
461 return s;
464 static rgba *R_extract_mask_spr (vgaimg *v) {
465 int i, j;
466 rgba *s = malloc(v->w * v->h * sizeof(rgba));
467 if (s != NULL) {
468 for (j = 0; j < v->h; j++) {
469 for (i = 0; i < v->w; i++) {
470 int k = j * v->w + i;
471 byte c = v->data[k];
472 if (c >= 0x70 && c <= 0x7F) {
473 byte mask = c - 0x70;
474 mask = 0xFF - ((mask << 4) | mask);
475 s[k] = (rgba) {
476 .r = mask,
477 .g = mask,
478 .b = mask,
479 .a = 0xFF,
480 };
481 } else {
482 s[k] = (rgba) {
483 .r = 0,
484 .g = 0,
485 .b = 0,
486 .a = 0,
487 };
492 return s;
495 static rgba *R_extract_rgba_spr (vgaimg *v) {
496 int i, j;
497 rgba *s = malloc(v->w * v->h * sizeof(rgba));
498 if (s != NULL) {
499 for (j = 0; j < v->h; j++) {
500 for (i = 0; i < v->w; i++) {
501 int k = j * v->w + i;
502 byte c = v->data[k];
503 s[k] = (rgba) {
504 .r = playpal[c].r,
505 .g = playpal[c].g,
506 .b = playpal[c].b,
507 .a = c == VGA_TRANSPARENT_COLOR ? 0x00 : 0xFF,
508 };
512 return s;
515 /* OpenGL helpers */
517 static image R_gl_create_image (const rgba *buf, int w, int h) {
518 node *n = R_cache_alloc(root, w, h);
519 if (n) {
520 R_cache_update(n, buf, 0, 0, w, h);
522 return (image) {
523 .n = n,
524 .w = w,
525 .h = h,
526 .res = -1
527 };
530 static image R_gl_get_special_image (int id, rgba *(*fn)(vgaimg*)) {
531 image img;
532 //char name[8];
533 //F_getresname(name, id);
534 //logo("load image: %.8s\n", name);
535 vgaimg *v = R_getvga(id);
536 if (v != NULL) {
537 rgba *buf = (*fn)(v);
538 img = R_gl_create_image(buf, v->w, v->h);
539 img.x = v->x;
540 img.y = v->y;
541 img.res = id;
542 M_unlock(v);
543 free(buf);
544 } else {
545 img = (image) {
546 .res = id
547 };
549 return img;
552 static image R_gl_getimage (int id) {
553 return R_gl_get_special_image(id, &R_extract_rgba_spr);
556 static image R_gl_loadimage (const char name[8]) {
557 return R_gl_getimage(F_getresid(name));
560 static image R_gl_get_special_spr (const char n[4], int s, int d, rgba *(*fn)(vgaimg*)) {
561 return R_gl_get_special_image(F_getsprid(n, s, d, NULL), fn);
564 static void R_gl_free_image (image *img) {
565 if (img->n != NULL && img->res >= 0) {
566 R_node_free(img->n);
568 img->n = NULL;
569 img->res = -1;
572 static void R_gl_draw_quad (int x, int y, int w, int h) {
573 glBegin(GL_QUADS);
574 glVertex2i(x + w, y);
575 glVertex2i(x, y);
576 glVertex2i(x, y + h);
577 glVertex2i(x + w, y + h);
578 glEnd();
581 static void R_gl_draw_textured (image *img, int x, int y, int w, int h, int flip) {
582 if (img->n) {
583 GLfloat nw = img->n->base->root.r + 1;
584 GLfloat nh = img->n->base->root.b + 1;
585 GLfloat ax = (flip ? img->n->l : img->n->r + 1) / nw;
586 GLfloat bx = (flip ? img->n->r + 1 : img->n->l) / nh;
587 GLfloat ay = (img->n->t) / nw;
588 GLfloat by = (img->n->b + 1) / nh;
589 R_gl_bind_texture(img->n->base->id);
590 glEnable(GL_TEXTURE_2D);
591 glBegin(GL_QUADS);
592 glTexCoord2f(ax, ay); glVertex2i(x + w, y);
593 glTexCoord2f(bx, ay); glVertex2i(x, y);
594 glTexCoord2f(bx, by); glVertex2i(x, y + h);
595 glTexCoord2f(ax, by); glVertex2i(x + w, y + h);
596 glEnd();
597 } else {
598 glColor3ub(255, 0, 0);
599 glDisable(GL_BLEND);
600 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
601 glDisable(GL_TEXTURE_2D);
602 R_gl_draw_quad(x, y, w, h);
606 /* fit image into rectangle without applying offset and transparency */
607 static void R_gl_draw_image_ext (image *img, int x, int y, int w, int h) {
608 glDisable(GL_BLEND);
609 glColor3ub(255, 255, 255);
610 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
611 R_gl_draw_textured(img, x, y, w, h, 0);
614 /* draw sprite with offset and coloring */
615 static void R_gl_draw_image_color (image *img, int x, int y, int flip) {
616 int xx = flip ? x - img->w + img->x : x - img->x;
617 glEnable(GL_BLEND);
618 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
619 R_gl_draw_textured(img, xx, y - img->y, img->w, img->h, flip);
622 /* draw sprite with offset */
623 static void R_gl_draw_image (image *img, int x, int y, int flip) {
624 glColor3ub(255, 255, 255);
625 R_gl_draw_image_color(img, x, y, flip);
628 static void R_gl_set_color (byte c) {
629 glColor3ub(playpal[c].r, playpal[c].g, playpal[c].b);
632 static void R_gl_setclip (int x, int y, int w, int h) {
633 glScissor(x * screen_scale, (SCRH - h - y) * screen_scale, w * screen_scale, h * screen_scale);
636 static void R_gl_setmatrix (void) {
637 SCRW = screen_width / screen_scale;
638 SCRH = screen_height / screen_scale;
639 glScissor(0, 0, screen_width, screen_height);
640 glViewport(0, 0, screen_width, screen_height);
641 glMatrixMode(GL_PROJECTION);
642 glLoadIdentity();
643 glOrtho(0, SCRW, SCRH, 0, 0, 1);
644 glMatrixMode(GL_MODELVIEW);
645 glLoadIdentity();
648 /* --- Misc --- */
650 static image Z_getspr (const char n[4], int s, int d, char *dir) {
651 int h = F_getsprid(n, s, d, dir);
652 return R_gl_getimage(h);
655 static image *Z_get_char_image (image *img, int ch) {
656 image *p = NULL;
657 ch = cp866_toupper(ch);
658 if (ch > 32 && ch < 160) {
659 p = &img[ch - '!'];
661 return p;
664 static int Z_get_char_width_generic (image *img, int off, int ch) {
665 image *p = Z_get_char_image(img, ch);
666 return p == NULL ? off : p->w - 1;
669 static int Z_putch_generic (image *img, int off, int ch) {
670 image *p = Z_get_char_image(img, ch);
671 int w = p == NULL ? off : p->w - 1;
672 if (p != NULL && p->n != NULL) {
673 R_gl_draw_image(p, prx, pry, 0);
675 prx += w;
676 return w;
679 static int Z_get_string_width_generic (image *img, int off, const char *fmt, va_list ap) {
680 int i, w, ww;
681 char buf[80];
682 vsprintf(buf, fmt, ap);
683 for (i = w = ww = 0; buf[i]; ++i) {
684 switch (buf[i]) {
685 case '\n':
686 case '\r':
687 ww = max(w, ww);
688 w = 0;
689 break;
690 default:
691 w += Z_get_char_width_generic(img, off, (byte)buf[i]);
694 return max(w, ww);
697 static int Z_printf_generic (image *img, int off, const char *fmt, va_list ap) {
698 int i, w, ww;
699 char buf[80];
700 vsprintf(buf, fmt, ap);
701 for (i = w = ww = 0; buf[i]; ++i) {
702 switch (buf[i]) {
703 case '\n':
704 pry += off + 1;
705 case '\r':
706 w = max(w, ww);
707 prx = 0;
708 w = 0;
709 break;
710 default:
711 w += Z_putch_generic(img, off, (byte)buf[i]);
714 return w;
717 static void Z_gotoxy (int x, int y) {
718 prx = x;
719 pry = y;
722 static int Z_get_big_string_width (const char *fmt, ...) {
723 va_list a;
724 va_start(a, fmt);
725 int w = Z_get_string_width_generic(bfh, 12, fmt, a);
726 va_end(a);
727 return w;
730 static int Z_printbf (const char *fmt, ...) {
731 va_list a;
732 va_start(a, fmt);
733 int w = Z_printf_generic(bfh, 12, fmt, a);
734 va_end(a);
735 return w;
738 static int Z_get_small_string_width (const char *fmt, ...) {
739 va_list a;
740 va_start(a, fmt);
741 int w = Z_get_string_width_generic(sfh, 7, fmt, a);
742 va_end(a);
743 return w;
746 static int Z_printsf (const char *fmt, ...) {
747 va_list a;
748 va_start(a, fmt);
749 int w =Z_printf_generic(sfh, 7, fmt, a);
750 va_end(a);
751 return w;
754 static void Z_printhf (const char *fmt, ...) {
755 int i, c;
756 char buf[80];
757 va_list a;
758 va_start(a, fmt);
759 vsprintf(buf, fmt, a);
760 va_end(a);
761 for (i = 0; buf[i]; ++i) {
762 switch (buf[i]) {
763 case '0':
764 case '1':
765 case '2':
766 case '3':
767 case '4':
768 case '5':
769 case '6':
770 case '7':
771 case '8':
772 case '9':
773 c = buf[i] - '0';
774 break;
775 case '-':
776 c = 10;
777 break;
778 case '%':
779 c = 11;
780 break;
781 case '\n':
782 pry += 19;
783 case '\r':
784 c = -1;
785 prx = 0;
786 break;
787 default:
788 c = -1;
789 break;
791 if (c >= 0) {
792 R_gl_draw_image(&sth[c], prx, pry, 0);
794 prx += 14;
798 /* --- Menu --- */
800 static image *PL_getspr (int s, int d, int msk) {
801 int i = (s - 'A') * 2 + d;
802 return msk ? &plr_msk[i] : &plr_spr[i];
805 #define SCROLLER_MIDDLE 10
806 #define TEXTFIELD_MIDDLE 2
808 static void get_entry_size (const menu_t *m, int i, int *w, int *h) {
809 assert(m != NULL);
810 assert(i >= 0);
811 assert(w != NULL);
812 assert(h != NULL);
813 int x = 0;
814 int y = 0;
815 int type = 0;
816 menu_msg_t msg;
817 msg.type = GM_GETENTRY;
818 if (GM_send(m, i, &msg)) {
819 type = msg.integer.i;
820 switch (type) {
821 case GM_BUTTON:
822 case GM_SCROLLER:
823 case GM_TEXTFIELD:
824 case GM_TEXTFIELD_BUTTON:
825 msg.type = GM_GETCAPTION;
826 if (GM_send(m, i, &msg)) {
827 x = Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
829 break;
830 case GM_SMALL_BUTTON:
831 msg.type = GM_GETCAPTION;
832 if (GM_send(m, i, &msg)) {
833 x = Z_get_small_string_width("%.*s", msg.string.maxlen, msg.string.s);
835 break;
836 default:
837 assert(0);
839 switch (type) {
840 case GM_BUTTON:
841 msg.type = GM_GETSTR;
842 if (GM_send(m, i, &msg)) {
843 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
845 y = 16;
846 break;
847 case GM_SMALL_BUTTON:
848 msg.type = GM_GETSTR;
849 if (GM_send(m, i, &msg)) {
850 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
852 y = 12;
853 break;
854 case GM_SCROLLER:
855 x += (SCROLLER_MIDDLE + 2) * 8;
856 y = 16;
857 break;
858 case GM_TEXTFIELD:
859 case GM_TEXTFIELD_BUTTON:
860 msg.type = GM_GETSTR;
861 if (GM_send(m, i, &msg)) {
862 x += (msg.string.maxlen + 2) * 8;
863 } else {
864 x += (TEXTFIELD_MIDDLE + 2) * 8;
866 y = 16;
867 break;
868 default:
869 assert(0);
872 *w = x;
873 *h = y;
876 static void get_menu_size (const menu_t *m, int *w, int *h) {
877 assert(m != NULL);
878 assert(w != NULL);
879 assert(h != NULL);
880 int i, n, x, y, xx, yy, type;
881 menu_msg_t msg;
882 msg.type = GM_QUERY;
883 if (GM_send_this(m, &msg)) {
884 n = msg.integer.b;
885 type = msg.integer.s;
886 x = 0;
887 y = 0;
888 msg.type = GM_GETTITLE;
889 if (GM_send_this(m, &msg)) {
890 switch (type) {
891 case GM_BIG: x = Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); break;
892 case GM_SMALL: x = Z_get_small_string_width("%.*s", msg.string.maxlen, msg.string.s); break;
893 default: assert(0);
896 for (i = 0; i < n; i++) {
897 get_entry_size(m, i, &xx, &yy);
898 x = max(x, xx);
899 y += yy;
901 *w = x;
902 *h = y;
903 } else {
904 *w = 0;
905 *h = 0;
909 static int GM_draw (void) {
910 int i, j, n, x, y, xoff, yoff, cur, w, type, recv;
911 const menu_t *m = GM_get();
912 menu_msg_t msg;
913 if (m != NULL) {
914 get_menu_size(m, &x, &y);
915 x = SCRW / 2 - x / 2;
916 y = SCRH / 2 - y / 2;
917 // --- title ---
918 msg.type = GM_QUERY;
919 if (GM_send_this(m, &msg)) {
920 cur = msg.integer.i;
921 n = msg.integer.a;
922 type = msg.integer.s;
923 msg.type = GM_GETTITLE;
924 if (GM_send_this(m, &msg)) {
925 Z_gotoxy(x, y - 10);
926 switch (type) {
927 case GM_SMALL: yoff = 8; Z_printsf("%.*s", msg.string.maxlen, msg.string.s); break;
928 case GM_BIG: yoff = 20; Z_printbf("%.*s", msg.string.maxlen, msg.string.s); break;
929 default: assert(0);
931 } else {
932 yoff = 0;
934 for (i = 0; i < n; i++) {
935 msg.type = GM_GETENTRY;
936 if (GM_send(m, i, &msg)) {
937 type = msg.integer.i;
938 if (i == cur) {
939 if (type == GM_SMALL_BUTTON) {
940 Z_gotoxy(x - 8, y + yoff);
941 Z_printsf(">");
942 } else {
943 R_gl_draw_image(&msklh[(gm_tm / 6) & 1], x - 25, y + yoff - 8, 0);
946 msg.type = GM_GETCAPTION;
947 if (GM_send(m, i, &msg)) {
948 Z_gotoxy(x, y + yoff);
949 if (type == GM_SMALL_BUTTON) {
950 xoff = Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
951 } else {
952 xoff = Z_printbf("%.*s", msg.string.maxlen, msg.string.s);
954 } else {
955 xoff = 0;
957 switch (type) {
958 case GM_BUTTON:
959 case GM_SMALL_BUTTON:
960 msg.type = GM_GETSTR;
961 if (GM_send(m, i, &msg)) {
962 Z_gotoxy(x + xoff, y + yoff);
963 if (type == GM_SMALL_BUTTON) {
964 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
965 } else {
966 Z_printbf("%.*s", msg.string.maxlen, msg.string.s);
969 yoff += type == GM_BUTTON ? 16 : 12;
970 break;
971 case GM_TEXTFIELD:
972 case GM_TEXTFIELD_BUTTON:
973 yoff += 9;
974 msg.type = GM_GETSTR;
975 recv = GM_send(m, i, &msg);
976 w = recv ? msg.string.maxlen : TEXTFIELD_MIDDLE;
977 R_gl_draw_image(&mslotl, x + xoff, y + yoff, 0);
978 for (j = 1; j <= w; j++) {
979 R_gl_draw_image(&mslotm, x + xoff + j * 8, y + yoff, 0);
981 R_gl_draw_image(&mslotr, x + xoff + j * 8, y + yoff, 0);
982 Z_gotoxy(x + xoff + 4, y + yoff - 7);
983 if (input && i == cur) {
984 Z_printsf("%.*s_", imax, ibuf);
985 } else if (recv) {
986 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
988 yoff += 7;
989 break;
990 case GM_SCROLLER:
991 R_gl_draw_image(&mbarl, x + xoff, y + yoff, 0);
992 for (j = 1; j < SCROLLER_MIDDLE; j++) {
993 R_gl_draw_image(&mbarm, x + xoff + j * 8, y + yoff, 0);
995 R_gl_draw_image(&mbarr, x + xoff + j * 8, y + yoff, 0);
996 msg.type = GM_GETINT;
997 if (GM_send(m, i, &msg)) {
998 int lev = (msg.integer.i - msg.integer.a) * ((SCROLLER_MIDDLE - 2) * 8) / msg.integer.b;
999 R_gl_draw_image(&mbaro, x + xoff + lev + 8, y + yoff, 0);
1001 yoff += 16;
1002 break;
1003 default:
1004 assert(0);
1010 return m != NULL;
1013 /* --- View --- */
1015 static void R_draw_fld (byte *fld, int minx, int miny, int maxx, int maxy, int fg) {
1016 int i, j;
1017 assert(minx >= 0 && minx <= FLDW);
1018 assert(miny >= 0 && miny <= FLDH);
1019 assert(maxx >= 0 && maxx <= FLDW);
1020 assert(maxy >= 0 && maxy <= FLDH);
1021 for (j = miny; j < maxy; j++) {
1022 for (i = minx; i < maxx; i++) {
1023 byte id = fld[j * FLDW + i];
1024 if (id != 0) {
1025 if (walp[id].res < 0) {
1026 if (fg) {
1027 switch (R_get_special_id(id)) {
1028 case 1:
1029 glColor4ub(0, 0, 255, 127);
1030 break;
1031 case 2:
1032 glColor4ub(0, 127, 0, 127);
1033 break;
1034 case 3:
1035 glColor4ub(127, 0, 0, 127);
1036 break;
1037 default:
1038 glColor4ub(0, 0, 0, 127);
1039 break;
1041 glEnable(GL_BLEND);
1042 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1043 glDisable(GL_TEXTURE_2D);
1044 R_gl_draw_quad(i * CELW, j * CELW, CELW, CELH);
1046 } else {
1047 R_gl_draw_image(&walp[id], i * CELW, j * CELH, 0);
1054 static void R_draw_dots (void) {
1055 int i;
1056 glDisable(GL_BLEND);
1057 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1058 glDisable(GL_TEXTURE_2D);
1059 glBegin(GL_QUADS);
1060 for (i = 0; i < MAXDOT; i++) {
1061 if (dot[i].t != 0) {
1062 int x = dot[i].o.x;
1063 int y = dot[i].o.y;
1064 R_gl_set_color(dot[i].c); glVertex2i(x + 1, y);
1065 R_gl_set_color(dot[i].c); glVertex2i(x, y);
1066 R_gl_set_color(dot[i].c); glVertex2i(x, y + 1);
1067 R_gl_set_color(dot[i].c); glVertex2i(x + 1, y + 1);
1070 glEnd();
1073 static void R_draw_items (void) {
1074 int i, s;
1075 for (i = 0; i < MAXITEM; ++i) {
1076 s = -1;
1077 if (it[i].t && it[i].s >= 0) {
1078 switch (it[i].t & 0x7FFF) {
1079 case I_ARM1:
1080 s = it[i].s / 9 + 18;
1081 break;
1082 case I_ARM2:
1083 s = it[i].s / 9 + 20;
1084 break;
1085 case I_MEGA:
1086 s = it[i].s / 2 + 22;
1087 break;
1088 case I_INVL:
1089 s = it[i].s / 2 + 26;
1090 break;
1091 case I_SUPER:
1092 case I_RTORCH:
1093 case I_GTORCH:
1094 case I_BTORCH:
1095 s = it[i].s / 2 + (it[i].t - I_SUPER) * 4 + 35;
1096 break;
1097 case I_GOR1: case I_FCAN:
1098 s = it[i].s / 2 + (it[i].t - I_GOR1) * 3 + 51;
1099 break;
1100 case I_AQUA:
1101 s = 30;
1102 break;
1103 case I_SUIT:
1104 s = 34;
1105 break;
1106 case I_KEYR:
1107 case I_KEYG:
1108 case I_KEYB:
1109 s = (it[i].t & 0x7FFF) - I_KEYR + 31;
1110 break;
1111 case I_GUN2:
1112 s = 57;
1113 break;
1114 default:
1115 s = (it[i].t & 0x7FFF) - 1;
1118 if (s >= 0) {
1119 R_gl_draw_image(&item_spr[s], it[i].o.x, it[i].o.y, item_sprd[s]);
1124 static int standspr (player_t *p) {
1125 if (p->f & PLF_UP) {
1126 return 'X';
1127 } else if (p->f & PLF_DOWN) {
1128 return 'Z';
1129 } else {
1130 return 'E';
1134 static int wpnspr (player_t *p) {
1135 if (p->f & PLF_UP) {
1136 return 'C';
1137 } else if(p->f & PLF_DOWN) {
1138 return 'E';
1139 } else {
1140 return 'A';
1144 static void R_draw_player (player_t *p) {
1145 enum {STAND, GO, DIE, SLOP, DEAD, MESS, OUT_, FALL}; // copypasted from player.c!
1146 static const int wytab[] = {-1, -2, -1, 0};
1147 int s = 'A';
1148 int w = 0;
1149 int wx = 0;
1150 int wy = 0;
1151 switch (p->st) {
1152 case STAND:
1153 if (p->f & PLF_FIRE) {
1154 s = standspr(p) + 1;
1155 w = wpnspr(p) + 1;
1156 } else if (p->pain) {
1157 s = 'G';
1158 w = 'A';
1159 wx = p->d ? 2 : -2;
1160 wy = 1;
1161 } else {
1162 s = standspr(p);
1163 w = wpnspr(p);
1165 break;
1166 case DEAD:
1167 s = 'N';
1168 break;
1169 case MESS:
1170 s = 'W';
1171 break;
1172 case GO:
1173 if (p->pain) {
1174 s = 'G';
1175 w = 'A';
1176 wx = p->d ? 2 : -2;
1177 wy = 1;
1178 } else {
1179 s = plr_goanim[p->s / 8];
1180 w = (p->f & PLF_FIRE) ? 'B' : 'A';
1181 wx = p->d ? 2 : -2;
1182 wy = 1 + wytab[s - 'A'];
1184 break;
1185 case DIE:
1186 s = plr_dieanim[p->s];
1187 break;
1188 case SLOP:
1189 s = plr_slopanim[p->s];
1190 break;
1191 case OUT_:
1192 s = 0;
1193 break;
1195 if (p->wpn == 0) {
1196 w = 0;
1198 if (w) {
1199 R_gl_draw_image(&plr_wpn[(int)p->wpn][w -'A'], p->o.x + wx, p->o.y + wy, p->d);
1201 if (s) {
1202 R_gl_draw_image(&plr_spr[(s - 'A') * 2 + p->d], p->o.x, p->o.y, plr_sprd[(s - 'A') * 2 + p->d]);
1203 R_gl_set_color(p->color + PLAYER_COLOR_OFFSET);
1204 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]);
1208 static void R_draw_monsters (void) {
1209 enum {SLEEP, GO, RUN, CLIMB, DIE, DEAD, ATTACK, SHOOT, PAIN, WAIT, REVIVE, RUNOUT}; // copypasted from monster.c!
1210 int i;
1211 for (i = 0; i < MAXMN; i++) {
1212 if (mn[i].t != MN_NONE) {
1213 int x = mn[i].o.x;
1214 int y = mn[i].o.y;
1215 if (mn[i].t < MN__LAST) {
1216 if ((mn[i].t != MN_SOUL && mn[i].t != MN_PAIN) || mn[i].st != DEAD) {
1217 int ap = mn[i].ap[mn[i].ac];
1218 int d = (ap - 'A') * 2 + mn[i].d;
1219 int dir = mn_sprd[mn[i].t - 1][d];
1220 if (mn[i].t == MN_MAN && (ap == 'E' || ap == 'F')) {
1221 R_gl_draw_image(&mn_sgun[ap - 'E'], x, y, mn[i].d);
1223 R_gl_draw_image(&mn_spr[mn[i].t - 1][d], x, y, dir);
1224 if (mn[i].t == MN_MAN) {
1225 R_gl_set_color(MANCOLOR + PLAYER_COLOR_OFFSET);
1226 R_gl_draw_image_color(&mn_man_msk[d], x, y, dir);
1229 if (mn[i].t == MN_VILE && mn[i].st == SHOOT) {
1230 R_gl_draw_image(&mn_fspr[mn[i].ac / 3], mn[i].tx, mn[i].ty, 0);
1232 } else if (mn[i].t == MN_PL_DEAD || mn[i].t == MN_PL_MESS) {
1233 int type = mn[i].t - MN_PL_DEAD;
1234 R_gl_draw_image(&pl_spr[type], x, y, 0);
1235 R_gl_set_color(mn[i].d);
1236 R_gl_draw_image_color(&pl_msk[type], x, y, 0);
1242 static void R_draw_weapons (void) {
1243 enum {NONE, ROCKET, PLASMA, APLASMA, BALL1, BALL2, BALL7, BFGBALL, BFGHIT, MANF, REVF, FIRE}; // copypasted from weapons.c!
1244 int i, s, d, x, y;
1245 for (i = 0; i < MAXWPN; ++i) {
1246 s = -1;
1247 d = 0;
1248 switch (wp[i].t) {
1249 case REVF:
1250 case ROCKET:
1251 d = wp[i].s;
1252 if (d < 2) {
1253 d = wp[i].o.xv > 0 ? 1 : 0;
1254 x = abs(wp[i].o.xv);
1255 y = wp[i].o.yv;
1256 s = 0;
1257 if (y < 0) {
1258 if (-y >= x) {
1259 s = 30;
1261 } else if (y > 0) {
1262 if (y >= x / 2) {
1263 s = 31;
1266 } else {
1267 s = (d - 2) / 2 + 1;
1268 d = 0;
1270 break;
1271 case MANF:
1272 s=wp[i].s;
1273 if (s >= 2) {
1274 s /= 2;
1275 break;
1277 case PLASMA:
1278 case APLASMA:
1279 case BALL1:
1280 case BALL7:
1281 case BALL2:
1282 s = wp[i].s;
1283 if (s >= 2) {
1284 s = s / 2 + 1;
1286 switch (wp[i].t) {
1287 case PLASMA:
1288 s += 4;
1289 break;
1290 case APLASMA:
1291 s += 11;
1292 break;
1293 case BALL1:
1294 s += 32;
1295 break;
1296 case BALL2:
1297 s += 42;
1298 break;
1299 case BALL7:
1300 s += 37;
1301 d = wp[i].o.xv >= 0 ? 1 : 0;
1302 break;
1303 case MANF:
1304 s += 47;
1305 d= wp[i].o.xv>=0 ? 1 : 0;
1306 break;
1308 break;
1309 case BFGBALL:
1310 s = wp[i].s;
1311 if (s >= 2) {
1312 s = s / 2 + 1;
1314 s += 18;
1315 break;
1316 case BFGHIT:
1317 s = wp[i].s / 2 + 26;
1318 break;
1320 if (s >= 0) {
1321 R_gl_draw_image(&wp_spr[s * 2 + d], wp[i].o.x, wp[i].o.y, wp_sprd[s * 2 + d]);
1326 static void R_draw_smoke (void) {
1327 int i, s;
1328 for (i = 0; i < MAXSMOK; ++i) {
1329 if (sm[i].t) {
1330 switch (sm[i].s) {
1331 case 0:
1332 s = sm[i].t;
1333 if (s >= (SMSN - 1) * 3) {
1334 s = 0;
1335 } else {
1336 s = SMSN - 1 - s / 3;
1338 R_gl_draw_image(&smk_spr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1339 break;
1340 case 1:
1341 s = sm[i].t;
1342 if (s >= FLSN - 1) {
1343 s = 0;
1344 } else {
1345 s = FLSN - 1 - s;
1347 R_gl_draw_image(&smk_fspr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1348 break;
1354 static void R_draw_effects (void) {
1355 enum {NONE, TFOG, IFOG, BUBL}; // copypasted from fx.c
1356 int i, s;
1357 for (i = 0; i < MAXFX; ++i) {
1358 switch (fx[i].t) {
1359 case TFOG:
1360 s = fx[i].s / 2;
1361 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1362 break;
1363 case IFOG:
1364 s = fx[i].s / 2 + 10;
1365 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1366 break;
1367 case BUBL:
1368 glDisable(GL_BLEND);
1369 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1370 glDisable(GL_TEXTURE_2D);
1371 R_gl_set_color(0xC0 + fx[i].s);
1372 R_gl_draw_quad(fx[i].x >> 8, fx[i].y >> 8, 1, 1);
1373 break;
1378 static int get_pu_st (int t) {
1379 if (t >= PL_FLASH) {
1380 return 1;
1381 } else if((t / 9) & 1) {
1382 return 0;
1383 } else {
1384 return 1;
1388 static void R_draw_view (int x, int y, int w, int h, int camx, int camy) {
1389 glPushMatrix();
1390 R_gl_setclip(x, y, w, h);
1391 glTranslatef(x, y, 0);
1392 if (w_horiz && horiz.n != NULL) {
1393 R_gl_draw_image_ext(&horiz, 0, 0, w, h);
1394 if (sky_type == 2 && lt_time < 0) {
1395 image *tanderbolt = &ltn[lt_type][lt_time < -5 ? 0 : 1];
1396 if (!lt_side) {
1397 R_gl_draw_image(tanderbolt, 0, lt_ypos, 0);
1398 } else {
1399 R_gl_draw_image(tanderbolt, w - 1, lt_ypos, 1);
1402 } else {
1403 glDisable(GL_BLEND);
1404 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1405 glDisable(GL_TEXTURE_2D);
1406 R_gl_set_color(DEFAULT_SKY_COLOR);
1407 R_gl_draw_quad(0, 0, w, h);
1409 int maxx = min((camx + w) / CELW + 1, FLDW);
1410 int maxy = min((camy + h) / CELH + 1, FLDH);
1411 int minx = max((camx - max_wall_width) / CELW, 0);
1412 int miny = max((camy - max_wall_height) / CELH, 0);
1413 glTranslatef(-camx, -camy, 0);
1414 R_draw_fld((byte*)fldb, minx, miny, maxx, maxy, 0);
1415 R_draw_dots();
1416 R_draw_items();
1417 R_draw_player(&pl1);
1418 if (_2pl) {
1419 R_draw_player(&pl2);
1421 R_draw_monsters();
1422 R_draw_weapons();
1423 R_draw_smoke();
1424 R_draw_effects();
1425 R_draw_fld((byte*)fldf, minx, miny, maxx, maxy, 1);
1426 glTranslatef(camx, camy, 0);
1427 if (sky_type == 2 && (lt_time == -4 || lt_time == -2)) {
1428 glColor4ub(255, 255, 255, 255);
1429 glEnable(GL_BLEND);
1430 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1431 glDisable(GL_TEXTURE_2D);
1432 R_gl_draw_quad(0, 0, w, h);
1434 glPopMatrix();
1437 static void R_draw_player_view (player_t *p, int x, int y, int w, int h) {
1438 p->looky = min(max(p->looky, -SCRH / 4), SCRH / 4); // TODO remove writeback
1439 int st = stone.w;
1440 int cw = w - st;
1441 int cx = min(max(p->o.x, cw / 2), FLDW * CELW - cw / 2);
1442 int cy = min(max(p->o.y - 12 + p->looky, h / 2), FLDH * CELH - h / 2);
1443 int camx = max(cx - cw / 2, 0);
1444 int camy = max(cy - h / 2, 0);
1445 glPushMatrix();
1446 R_draw_view(x, y + 1, cw, h - 2, camx, camy);
1447 glTranslatef(x, y, 0);
1448 if (p->invl) {
1449 if (get_pu_st(p->invl)) {
1450 glEnable(GL_BLEND);
1451 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1452 glDisable(GL_TEXTURE_2D);
1453 glColor4ub(191, 191, 191, 255);
1454 R_gl_draw_quad(0, 0, cw, h);
1456 } else {
1457 if (p->suit && get_pu_st(p->suit)) {
1458 glEnable(GL_BLEND);
1459 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1460 glDisable(GL_TEXTURE_2D);
1461 glColor4ub(0, 255, 0, 192);
1462 R_gl_draw_quad(0, 0, cw, h);
1464 int f = min(max(p->pain * 3, 0), 255);
1465 if (f > 0) {
1466 glEnable(GL_BLEND);
1467 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1468 glDisable(GL_TEXTURE_2D);
1469 glColor4ub(255, 0, 0, f);
1470 R_gl_draw_quad(0, 0, cw, h);
1473 R_gl_setclip(x, y, w, h);
1474 glTranslatef(-x + cw, 0, 0);
1475 R_gl_draw_image(&stone, 0, 0, 0);
1476 int i = stone.h;
1477 while (i < h) {
1478 R_gl_draw_image(&stone2, 0, i, 0);
1479 i += stone2.h;
1481 if (p->drawst & PL_DRAWAIR) {
1482 if (p->air < PL_AIR) {
1483 int a = min(max(p->air, 0), MAXAIR) * 100 / MAXAIR;
1484 glDisable(GL_BLEND);
1485 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1486 glDisable(GL_TEXTURE_2D);
1487 R_gl_set_color(0xC8);
1488 R_gl_draw_quad(10, 49, a, 2);
1491 if (p->drawst & PL_DRAWLIFE) {
1492 Z_gotoxy(10, 7);
1493 Z_printhf("%3d%%", p->life);
1495 if (p->drawst & PL_DRAWARMOR) {
1496 Z_gotoxy(10, 7 + 19);
1497 Z_printhf("%3d%%", p->armor);
1499 if (p->drawst & PL_DRAWWPN) {
1500 switch(p->wpn) {
1501 case 2:
1502 case 5:
1503 i = p->ammo;
1504 break;
1505 case 3:
1506 case 4:
1507 case 9:
1508 i = p->shel;
1509 break;
1510 case 6:
1511 i = p->rock;
1512 break;
1513 case 7:
1514 case 8:
1515 i = p->cell;
1516 break;
1517 case 10:
1518 i = p->fuel;
1519 break;
1520 default:
1521 i = -1;
1522 break;
1524 // weapon
1525 if (p->wpn >= 0) {
1526 R_gl_draw_image(&sth[12 + p->wpn], st - 88, 58 + 19, 0);
1528 // ammo
1529 if (p->wpn >= 2) {
1530 Z_gotoxy(st - 10 - 5 * 14, 58 + 2);
1531 Z_printhf("%5d", i);
1534 if (p->drawst & PL_DRAWFRAG && g_dm) {
1535 Z_gotoxy(st - 5 - 5 * 14, 77 + 5);
1536 Z_printhf("%5d", p->frag);
1538 if (p->drawst & PL_DRAWKEYS) {
1539 int x, k, n;
1540 for (k = p->keys >> 4, n = 0, x = st - 75; n < 3; n++, k >>= 1, x += 9) {
1541 if (k & 1) {
1542 R_gl_draw_image(&keys[n], x, 91, 0);
1546 if (p->drawst & PL_DRAWLIVES && !_2pl) {
1547 Z_gotoxy(st - 35, 17);
1548 Z_printhf("%d", p->lives);
1550 glPopMatrix();
1553 /* --- Game --- */
1555 static void pl_info (player_t *p, int x, int y) {
1556 dword t = p->kills * 10920 / g_time;
1557 Z_gotoxy(x + 25, y); Z_printbf("KILLS");
1558 Z_gotoxy(x + 25, y + 15); Z_printbf("KPM");
1559 Z_gotoxy(x + 25, y + 30); Z_printbf("SECRETS %u / %u", p->secrets, sw_secrets);
1560 Z_gotoxy(x + 255, y); Z_printbf("%u", p->kills);
1561 Z_gotoxy(x + 255, y + 15); Z_printbf("%u.%u", t / 10, t % 10);
1564 static void R_draw_intermission (void) {
1565 int cx = SCRW / 2;
1566 word hr, mn, sc, h;
1567 Z_gotoxy(cx - 14*12/2, 20);
1568 Z_printbf("LEVEL COMPLETE");
1569 Z_calc_time(g_time, &hr, &mn, &sc);
1570 Z_gotoxy(cx - 12*12/2, 40);
1571 Z_printbf("TIME %u:%02u:%02u", hr, mn, sc);
1572 h = 40 + SCRH / 10;
1573 if (_2pl) {
1574 Z_gotoxy(cx - 10*12/2, h);
1575 Z_printbf("PLAYER ONE");
1576 h += 20;
1578 pl_info(&pl1, cx - 160, h);
1579 if (_2pl) {
1580 h += 30 + SCRH / 10;
1581 Z_gotoxy(cx - 10*12/2, h);
1582 Z_printbf("PLAYER TWO");
1583 h += 20;
1584 pl_info(&pl2, cx - 160, h);
1588 static void W_act (void) {
1589 int i, a;
1590 if (g_time % 3 == 0) {
1591 for (i = 1; i < max_textures; i++) {
1592 a = walani[i];
1593 if (a != 0) {
1594 anic[a]++;
1595 if (anip[a][anic[a]].res == -1) {
1596 anic[a] = 0;
1598 walp[i] = anip[a][anic[a]];
1604 void R_draw (void) {
1605 W_act();
1606 glClearColor(0, 0, 0, 1);
1607 glClear(GL_COLOR_BUFFER_BIT);
1608 glEnable(GL_SCISSOR_TEST);
1609 R_gl_setmatrix();
1610 switch (g_st) {
1611 case GS_ENDANIM:
1612 case GS_END2ANIM:
1613 case GS_DARKEN:
1614 case GS_BVIDEO:
1615 case GS_EVIDEO:
1616 case GS_END3ANIM:
1617 break;
1618 case GS_TITLE:
1619 R_gl_draw_image_ext(&scrnh[0], 0, 0, SCRW, SCRH);
1620 break;
1621 case GS_INTER:
1622 R_gl_draw_image_ext(&scrnh[1], 0, 0, SCRW, SCRH);
1623 R_draw_intermission();
1624 break;
1625 case GS_ENDSCR:
1626 R_gl_draw_image_ext(&scrnh[2], 0, 0, SCRW, SCRH);
1627 break;
1628 case GS_GAME:
1629 if (_2pl) {
1630 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH / 2);
1631 R_draw_player_view(&pl2, 0, SCRH / 2, SCRW, SCRH / 2);
1632 } else {
1633 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH);
1635 R_gl_setclip(0, 0, SCRW, SCRH);
1636 break;
1638 GM_draw();
1639 Y_swap_buffers();
1642 static void R_alloc (void) {
1643 char s[10];
1644 int i, j, n;
1645 logo("R_alloc: load graphics\n");
1646 /* Game */
1647 scrnh[0] = R_gl_loadimage("TITLEPIC");
1648 scrnh[1] = R_gl_loadimage("INTERPIC");
1649 scrnh[2] = R_gl_loadimage("ENDPIC");
1650 for (i = 0; i < 2; i++) {
1651 sprintf(s, "LTN%c", '1' + i);
1652 for (j = 0; j < 2; j++) {
1653 ltn[i][j] = Z_getspr(s, j, 0, NULL);
1656 /* Smoke */
1657 for (i = 0; i < SMSN; i++) {
1658 smk_spr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_smoke_spr);
1660 for (i = 0; i < FLSN; i++) {
1661 smk_fspr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_flame_spr);
1663 /* Effects */
1664 for (i = 0; i < 10; i++) {
1665 fx_spr[i] = Z_getspr("TFOG", i, 0, fx_sprd + i);
1667 for (; i < 15; i++) {
1668 fx_spr[i] = Z_getspr("IFOG", i - 10, 0, fx_sprd + i);
1670 /* Weapons */
1671 for (i = 0; i < 4; i++) {
1672 wp_spr[i * 2] = Z_getspr("MISL", i, 1, wp_sprd + i * 2);
1673 wp_spr[i * 2 + 1] = Z_getspr("MISL", i, 2, wp_sprd + i * 2 + 1);
1675 for (; i < 6; i++) {
1676 wp_spr[i * 2] = Z_getspr("PLSS", i - 4, 1, wp_sprd + i * 2);
1677 wp_spr[i * 2 + 1] = Z_getspr("PLSS", i - 4, 2, wp_sprd + i * 2 + 1);
1679 for (; i < 11; i++) {
1680 wp_spr[i * 2] = Z_getspr("PLSE", i - 6, 1, wp_sprd + i * 2);
1681 wp_spr[i * 2 + 1] = Z_getspr("PLSE", i - 6, 2, wp_sprd + i * 2 + 1);
1683 for (; i < 13; i++) {
1684 wp_spr[i * 2] = Z_getspr("APLS", i - 11, 1, wp_sprd + i * 2);
1685 wp_spr[i * 2 + 1] = Z_getspr("APLS", i - 11, 2, wp_sprd + i * 2 + 1);
1687 for (; i < 18; i++) {
1688 wp_spr[i * 2] = Z_getspr("APBX", i - 13, 1, wp_sprd + i * 2);
1689 wp_spr[i * 2 + 1] = Z_getspr("APBX", i - 13, 2, wp_sprd + i * 2 + 1);
1691 for(; i < 20; i++) {
1692 wp_spr[i * 2] = Z_getspr("BFS1", i - 18, 1, wp_sprd + i * 2);
1693 wp_spr[i * 2 + 1] = Z_getspr("BFS1", i - 18, 2, wp_sprd + i * 2 + 1);
1695 for (; i < 26; i++) {
1696 wp_spr[i * 2] = Z_getspr("BFE1", i - 20, 1, wp_sprd + i * 2);
1697 wp_spr[i * 2 + 1] = Z_getspr("BFE1", i - 20, 2, wp_sprd + i * 2 + 1);
1699 for (; i < 30; i++) {
1700 wp_spr[i * 2] = Z_getspr("BFE2", i - 26, 1, wp_sprd + i * 2);
1701 wp_spr[i * 2 + 1] = Z_getspr("BFE2", i - 26, 2, wp_sprd + i * 2 + 1);
1703 for (; i < 32; i++) {
1704 wp_spr[i * 2] = Z_getspr("MISL", i - 30 + 4, 1, wp_sprd + i * 2);
1705 wp_spr[i * 2 + 1] = Z_getspr("MISL", i - 30 + 4, 2, wp_sprd + i * 2 + 1);
1707 for (; i < 37; i++) {
1708 wp_spr[i * 2] = Z_getspr("BAL1", i - 32, 1, wp_sprd + i * 2);
1709 wp_spr[i * 2 + 1] = Z_getspr("BAL1", i - 32, 2, wp_sprd + i * 2 + 1);
1711 for (; i < 42; i++) {
1712 wp_spr[i * 2] = Z_getspr("BAL7", i - 37, 1, wp_sprd + i * 2);
1713 wp_spr[i * 2 + 1] = Z_getspr("BAL7", i - 37, 2, wp_sprd + i * 2 + 1);
1715 for (; i < 47; i++) {
1716 wp_spr[i * 2] = Z_getspr("BAL2", i - 42, 1, wp_sprd + i * 2);
1717 wp_spr[i * 2 + 1] = Z_getspr("BAL2", i - 42, 2, wp_sprd + i * 2 + 1);
1719 for (; i < 49; i++) {
1720 wp_spr[i * 2] = Z_getspr("MANF", i - 47, 1, wp_sprd + i * 2);
1721 wp_spr[i * 2 + 1] = Z_getspr("MANF", i - 47, 2, wp_sprd + i * 2 + 1);
1723 /* Items */
1724 static const char snm[18][4] = {
1725 "CLIP", "SHEL", "ROCK", "CELL", "AMMO", "SBOX", "BROK", "CELP",
1726 "STIM", "MEDI", "BPAK",
1727 "CSAW", "SHOT", "SGN2", "MGUN", "LAUN", "PLAS", "BFUG"
1728 };
1729 static const char n4[4][4] = {
1730 "SOUL", "SMRT", "SMGT", "SMBT"
1731 };
1732 static const char n3[2][4] = {
1733 "GOR1", "FCAN"
1734 };
1735 for (i = 0; i < 18; i++) {
1736 item_spr[i] = Z_getspr(snm[i], 0, 0, item_sprd + i);
1738 for (; i < 20; i++) {
1739 item_spr[i] = Z_getspr("ARM1", i - 18, 0, item_sprd + i);
1740 item_spr[i + 2] = Z_getspr("ARM2", i - 18, 0, item_sprd + i);
1742 i+=2;
1743 for (; i < 26; i++) {
1744 item_spr[i] = Z_getspr("MEGA", i - 22, 0, item_sprd + i);
1746 for (; i < 30; i++) {
1747 item_spr[i] = Z_getspr("PINV", i - 26, 0, item_sprd + i);
1749 item_spr[30] = Z_getspr("AQUA", 0, 0, item_sprd + 30);
1750 item_spr[31] = Z_getspr("KEYR", 0, 0, item_sprd + 31);
1751 item_spr[32] = Z_getspr("KEYG", 0, 0, item_sprd + 32);
1752 item_spr[33] = Z_getspr("KEYB", 0, 0, item_sprd + 33);
1753 item_spr[34] = Z_getspr("SUIT", 0, 0, item_sprd + 34);
1754 for (n = 35, j = 0; j < 4; j++) {
1755 for (i = 0; i < 4; i++, n++) {
1756 item_spr[n] = Z_getspr(n4[j], i, 0, item_sprd + n);
1759 for (j = 0; j < 2; j++) {
1760 for (i = 0; i < 3; i++, n++) {
1761 item_spr[n] = Z_getspr(n3[j], i, 0, item_sprd + n);
1764 item_spr[57] = Z_getspr("GUN2", 0, 0, item_sprd + 57);
1765 /* Player */
1766 for (i = 0; i < 27; i++) {
1767 plr_spr[i * 2] = Z_getspr("PLAY", i, 1, plr_sprd + i * 2);
1768 plr_msk[i * 2] = R_gl_get_special_spr("PLAY", i, 1, &R_extract_mask_spr);
1769 plr_spr[i * 2 + 1] = Z_getspr("PLAY", i, 2, plr_sprd + i * 2 + 1);
1770 plr_msk[i * 2 + 1] = R_gl_get_special_spr("PLAY", i, 2, &R_extract_mask_spr);
1772 strncpy(s, "PWPx", 4);
1773 for (i = 1; i < 11; i++) {
1774 s[3] = (i < 10 ? '0' : 'A' - 10) + i;
1775 for (j = 0; j < 6; j++) {
1776 plr_wpn[i][j] = Z_getspr(s, j, 1, NULL);
1779 /* Monsters */
1780 static const char msn[MN_TN][4] = {
1781 "SARG", "TROO", "POSS", "SPOS", "CYBR", "CPOS", "BOSS", "BOS2", "HEAD", "SKUL",
1782 "PAIN", "SPID", "BSPI", "FATT", "SKEL", "VILE", "FISH", "BAR1", "ROBO", "PLAY"
1783 };
1784 static const int mms[MN_TN] = {
1785 14*2, 21*2, 21*2, 21*2, 16*2, 20*2, 15*2, 15*2, 12*2, 11*2,
1786 13*2, 19*2, 16*2, 20*2, 17*2, 29*2, 6*2, 2*2, 17*2, 23*2
1787 };
1788 mn_sgun[0] = Z_getspr("PWP4", 0, 1, NULL);
1789 mn_sgun[1] = Z_getspr("PWP4", 1, 1, NULL);
1790 for (j = 0; j < MN_TN; j++) {
1791 for (i = 0; i < mms[j]; i++) {
1792 mn_spr[j][i] = Z_getspr(msn[j], i / 2, (i & 1) + 1, &mn_sprd[j][i]);
1793 if (j == MN_MAN - 1) {
1794 mn_man_msk[i] = R_gl_get_special_spr(msn[j], i / 2, (i & 1) + 1, &R_extract_mask_spr);
1797 if (j == MN_BARREL - 1) {
1798 for (i = 4; i < 14; i++) {
1799 mn_spr[j][i] = Z_getspr("BEXP", i / 2 - 2, (i & 1) + 1, &mn_sprd[j][i]);
1803 for (i = 0; i < 8; i++) {
1804 mn_fspr[i] = Z_getspr("FIRE", i, 0, NULL);
1806 pl_spr[0] = Z_getspr("PLAY", 'N' - 'A', 0, NULL);
1807 pl_msk[0] = R_gl_get_special_spr("PLAY", 'N' - 'A', 0, &R_extract_mask_spr);
1808 pl_spr[1] = Z_getspr("PLAY", 'W' - 'A', 0, NULL);
1809 pl_msk[1] = R_gl_get_special_spr("PLAY", 'W' - 'A', 0, &R_extract_mask_spr);
1810 /* Misc */
1811 static const char mnm[22][8]={
1812 "STTNUM0", "STTNUM1", "STTNUM2", "STTNUM3", "STTNUM4",
1813 "STTNUM5", "STTNUM6", "STTNUM7", "STTNUM8", "STTNUM9",
1814 "STTMINUS", "STTPRCNT",
1815 "FISTA0", "CSAWA0", "PISTA0", "SHOTA0", "SGN2A0", "MGUNA0", "LAUNA0",
1816 "PLASA0", "BFUGA0", "GUN2A0"
1817 };
1818 stone = R_gl_loadimage("STONE");
1819 stone2 = R_gl_loadimage("STONE2");
1820 keys[0] = R_gl_loadimage("KEYRA0");
1821 keys[1] = R_gl_loadimage("KEYGA0");
1822 keys[2] = R_gl_loadimage("KEYBA0");
1823 for (i = 0; i < 22; i++) {
1824 sth[i] = R_gl_loadimage(mnm[i]);
1826 strcpy(s, "STBF_*");
1827 for (i = '!'; i < 160; i++) {
1828 s[5] = i;
1829 bfh[i - '!'] = R_gl_getimage(F_findres(s));
1831 for (i = '!'; i < 160; i++) {
1832 sprintf(s, "STCFN%03d", i);
1833 sfh[i - '!'] = R_gl_getimage(F_findres(s));
1835 strcpy(s, "WINUM*");
1836 for (i = '0'; i <= '9'; i++) {
1837 s[5] = i;
1838 bfh[i - '!'] = R_gl_loadimage(s);
1840 bfh[':' - '!'] = R_gl_loadimage("WICOLON");
1841 // menu
1842 msklh[0] = R_gl_loadimage("M_SKULL1");
1843 msklh[1] = R_gl_loadimage("M_SKULL2");
1844 mbarl = R_gl_loadimage("M_THERML");
1845 mbarm = R_gl_loadimage("M_THERMM");
1846 mbarr = R_gl_loadimage("M_THERMR");
1847 mbaro = R_gl_loadimage("M_THERMO");
1848 mslotl = R_gl_loadimage("M_LSLEFT");
1849 mslotm = R_gl_loadimage("M_LSCNTR");
1850 mslotr = R_gl_loadimage("M_LSRGHT");
1851 // walls
1852 for (i = 1; i < ANIT; i++) {
1853 for (j = 0; j < 5 && anm[i - 1][j]; j++) {
1854 anip[i][j] = R_gl_loadimage(anm[i - 1][j]);
1856 for(; j < 5; j++) {
1857 anip[i][j] = (image) {
1858 .n = NULL,
1859 .w = 8,
1860 .h = 8,
1861 .res = -1,
1862 };
1867 static void R_reload_textures (void);
1869 void R_set_videomode (int w, int h, int fullscreen) {
1870 assert(w > 0);
1871 assert(h > 0);
1872 int was = Y_videomode_setted();
1873 if (root != NULL) {
1874 R_cache_free(root, 0);
1875 root = NULL;
1877 int res = Y_set_videomode_opengl(w, h, fullscreen);
1878 if (res == 0) {
1879 if (was == 0) {
1880 ERR_failinit("Unable to set video mode\n");
1883 Y_get_videomode(&screen_width, &screen_height);
1884 screen_full = Y_get_fullscreen();
1885 screen_scale = max(1, screen_width / 320);
1886 root = R_cache_new();
1887 assert(root);
1888 R_alloc();
1889 R_reload_textures();
1892 static int video_menu_handler (menu_msg_t *msg, const menu_t *m, int i) {
1893 static int cur;
1894 static int w, h, fullscreen;
1895 static char buf[16];
1896 static int buflen;
1897 static int vmode;
1898 const videomode_t *v;
1899 enum { VIDEOMODE, FULLSCREEN, APPLY, __NUM__ };
1900 static const simple_menu_t sm = {
1901 GM_BIG, "Video", NULL,
1903 { "Mode: ", NULL },
1904 { "Fullscreen: ", NULL },
1905 { "Apply ", NULL },
1907 };
1908 if (msg->type == GM_ENTER) {
1909 Y_get_videomode(&w, &h);
1910 fullscreen = Y_get_fullscreen();
1911 v = Y_get_videomode_list_opengl(fullscreen);
1912 vmode = 0;
1913 while (vmode < v->n && v->modes[vmode].w != w && v->modes[vmode].h != h) {
1914 vmode += 1;
1916 if (vmode < v->n) {
1917 w = v->modes[vmode].w;
1918 h = v->modes[vmode].h;
1920 snprintf(buf, 16, "%ix%i", w, h);
1921 buflen = strlen(buf);
1922 return 1;
1924 if (i == VIDEOMODE) {
1925 switch (msg->type) {
1926 case GM_GETSTR: return GM_init_str(msg, buf, buflen);
1927 case GM_SELECT:
1928 v = Y_get_videomode_list_opengl(fullscreen);
1929 vmode = vmode + 1 >= v->n ? 0 : vmode + 1;
1930 if (v->n > 0) {
1931 w = v->modes[vmode].w;
1932 h = v->modes[vmode].h;
1933 } else {
1934 Y_get_videomode(&w, &h);
1936 snprintf(buf, 16, "%ix%i", w, h);
1937 buflen = strlen(buf);
1938 return 1;
1940 } else if (i == FULLSCREEN) {
1941 switch (msg->type) {
1942 case GM_GETSTR: return GM_init_str(msg, fullscreen ? "Yes" : "No ", 3);
1943 case GM_SELECT: fullscreen = !fullscreen; return 1;
1945 } else if (i == APPLY) {
1946 switch (msg->type) {
1947 case GM_SELECT: R_set_videomode(w, h, fullscreen); return 1;
1950 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
1953 const menu_t *R_menu (void) {
1954 static const menu_t m = { video_menu_handler };
1955 return &m;
1958 const cfg_t *R_args (void) {
1959 static const cfg_t args[] = {
1960 { "fullscr", &init_screen_full, Y_SW_ON },
1961 { "window", &init_screen_full, Y_SW_OFF },
1962 { "width", &init_screen_width, Y_DWORD },
1963 { "height", &init_screen_height, Y_DWORD },
1964 { NULL, NULL, 0 } // end
1965 };
1966 return args;
1969 const cfg_t *R_conf (void) {
1970 static const cfg_t conf[] = {
1971 { "sky", &w_horiz, Y_SW_ON },
1972 { "fullscreen", &screen_full, Y_SW_ON },
1973 { "screen_width", &screen_width, Y_DWORD },
1974 { "screen_height", &screen_height, Y_DWORD },
1975 { NULL, NULL, 0 } // end
1976 };
1977 return conf;
1980 void R_init (void) {
1981 logo("R_init: intialize opengl render\n");
1982 R_init_playpal();
1983 init_screen_width = init_screen_width > 0 ? init_screen_width : screen_width;
1984 init_screen_height = init_screen_height > 0 ? init_screen_height : screen_height;
1985 init_screen_full = init_screen_full != 0xFF ? init_screen_full : screen_full;
1986 R_set_videomode(init_screen_width, init_screen_height, init_screen_full);
1989 void R_done (void) {
1990 R_cache_free(root, 1);
1991 Y_unset_videomode();
1992 root = NULL;
1995 void R_get_name (int n, char s[8]) {
1996 assert(n >= 0 && n < 256);
1997 if (walp[n].res == -1) {
1998 memset(s, 0, 8);
1999 } else if (walp[n].res == -2) {
2000 memcpy(s, "_WATER_", 8);
2001 s[7] = '0' + (intptr_t)walp[n].n - 1;
2002 } else if (walani[n] > 0) {
2003 memcpy(s, anm[walani[n] - 1][0], 8);
2004 } else {
2005 F_getresname(s, walp[n].res & 0x7FFF);
2009 static short getani (char n[8]) {
2010 short i = 0;
2011 while (i < ANIT - 1 && cp866_strncasecmp(n, anm[i][0], 8) != 0) {
2012 i++;
2014 return i < ANIT - 1 ? i + 1 : 0;
2017 int R_get_special_id (int n) {
2018 assert(n >= 0 && n <= 256);
2019 return walp[n].res == -2 ? (intptr_t)walp[n].n : -1;
2022 static void R_reload_textures (void) {
2023 int i;
2024 char s[8];
2025 for (i = 0; i < max_textures; i++) {
2026 R_get_name(i, s);
2027 if (walp[i].res >= 0) {
2028 walp[i] = R_gl_getimage(walp[i].res);
2031 if (horiz.n) {
2032 horiz = R_gl_getimage(horiz.res);
2036 void R_begin_load (void) {
2037 int i;
2038 for (i = 0; i < 256; i++) {
2039 if (walp[i].n != NULL && walp[i].res >= 0 && walani[i] == 0) {
2040 R_gl_free_image(&walp[i]);
2042 memset(&walp[i], 0, sizeof(image));
2043 walp[i].res = -1;
2044 walswp[i] = i;
2045 walani[i] = 0;
2047 memset(anic, 0, sizeof(anic));
2048 max_wall_width = 0;
2049 max_wall_height = 0;
2050 max_textures = 1;
2053 void R_load (char s[8]) {
2054 assert(max_textures < 256);
2055 if (!s[0]) {
2056 walp[max_textures] = (image) {
2057 .n = NULL,
2058 .x = 0,
2059 .y = 0,
2060 .w = 0,
2061 .h = 0,
2062 .res = -1,
2063 };
2064 } else if (cp866_strncasecmp(s, "_WATER_", 7) == 0) {
2065 walp[max_textures] = (image) {
2066 .n = (void*)((intptr_t)s[7] - '0' + 1),
2067 .x = 0,
2068 .y = 0,
2069 .w = 8,
2070 .h = 8,
2071 .res = -2,
2072 };
2073 } else {
2074 walp[max_textures] = R_gl_loadimage(s);
2075 if (s[0] == 'S' && s[1] == 'W' && s[4] == '_') {
2076 walswp[max_textures] = 0;
2078 walani[max_textures] = getani(s);
2080 max_wall_width = max(max_wall_width, walp[max_textures].w);
2081 max_wall_height = max(max_wall_height, walp[max_textures].h);
2082 max_textures++;
2085 void R_end_load (void) {
2086 int i, j, k, g;
2087 char s[8];
2088 j = max_textures;
2089 for (i = 1; i < 256 && j < 256; i++) {
2090 if (walswp[i] == 0) {
2091 R_get_name(i, s);
2092 s[5] ^= 1;
2093 g = F_getresid(s);
2094 k = 1;
2095 while (k < 256 && walp[k].res != g) {
2096 k += 1;
2098 if (k >= 256) {
2099 k = j;
2100 j += 1;
2101 max_textures += 1;
2102 walp[k] = R_gl_getimage(g);
2103 walf[k] = walf[i];
2105 walswp[i] = k;
2106 walswp[k] = i;
2111 void R_loadsky (int sky) {
2112 char s[6];
2113 strcpy(s, "RSKYx");
2114 s[4] = '0' + sky;
2115 R_gl_free_image(&horiz);
2116 horiz = R_gl_loadimage(s);
2119 void R_switch_texture (int x, int y) {
2120 assert(x >= 0 && x < FLDW);
2121 assert(y >= 0 && y < FLDH);
2122 fldb[y][x] = walswp[fldb[y][x]];
2125 int R_get_swp (int n) {
2126 assert(n >= 0 && n < 256);
2127 return walswp[n];