DEADSOFTWARE

portability: avoid errors on some compilers
[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 "common/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 <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <stdarg.h>
52 #include <assert.h>
54 #define VGA_TRANSPARENT_COLOR 0
55 #define DEFAULT_SKY_COLOR 0x97
56 #define MANCOLOR 0xD0
57 #define PLAYER_COLOR_OFFSET 7
58 #define MAXAIR 1091
59 #define ANIT 5
60 #define PL_FLASH 90
62 #pragma pack(1)
63 typedef struct vgaimg {
64 word w, h;
65 short x, y;
66 byte data[];
67 } vgaimg;
69 typedef struct rgb {
70 byte r, g, b;
71 } rgb;
73 typedef struct rgba {
74 byte r, g, b, a;
75 } rgba;
76 #pragma pack()
78 typedef struct node {
79 struct cache *base;
80 struct node *left, *right;
81 struct node *up;
82 int l, t, r, b;
83 int leaf;
84 } node;
86 typedef struct cache {
87 struct cache *next;
88 struct node root;
89 GLuint id;
90 } cache;
92 typedef struct image {
93 node *n;
94 GLint x, y;
95 GLuint w, h;
96 int res;
97 } image;
99 /* Render Specific */
100 static int SCRW;
101 static int SCRH;
102 static float screen_scale;
103 static int screen_width = 320;
104 static int screen_height = 200;
105 static byte screen_full = 0;
106 static int init_screen_width = 0;
107 static int init_screen_height = 0;
108 static byte init_screen_full = 0xFF;
109 static rgb playpal[256];
110 static byte bright[256];
111 static GLuint lastTexture;
112 static cache *root;
114 /* Game */
115 static image scrnh[3]; // TITLEPIC INTERPIC ENDPIC
116 static image ltn[2][2];
118 /* Smoke */
119 static image smk_spr[SMSN];
120 static image smk_fspr[FLSN];
122 /* Effects */
123 static image fx_spr[15];
124 static char fx_sprd[15];
126 /* Weapons */
127 static image wp_spr[49*2];
128 static char wp_sprd[49*2];
130 /* Items */
131 static image item_spr[58];
132 static char item_sprd[58];
134 /* Player */
135 static image plr_spr[27*2];
136 static image plr_msk[27*2];
137 static char plr_sprd[27*2];
138 static image plr_wpn[11][6];
140 /* Monsters */
141 static image pl_spr[2];
142 static image pl_msk[2];
143 static image mn_spr[MN_TN][29*2];
144 static image mn_man_msk[29*2];
145 static char mn_sprd[MN_TN][29*2];
146 static image mn_fspr[8];
147 static image mn_sgun[2];
149 /* Misc */
150 static image sth[22];
151 static image bfh[160 - '!'];
152 static image sfh[160 - '!'];
153 static image stone;
154 static image stone2;
155 static image keys[3];
156 static int prx = 0;
157 static int pry = 0;
159 /* Menu */
160 static int gm_tm;
161 static image msklh[2];
162 static image mbarl;
163 static image mbarm;
164 static image mbarr;
165 static image mbaro;
166 static image mslotl;
167 static image mslotm;
168 static image mslotr;
170 /* Map */
171 static const char *anm[ANIT - 1][5] = {
172 {"WALL22_1", "WALL23_1", "WALL23_2", NULL, NULL},
173 {"WALL58_1", "WALL58_2", "WALL58_3", NULL, NULL},
174 {"W73A_1", "W73A_2", NULL, NULL, NULL},
175 {"RP2_1", "RP2_2", "RP2_3", "RP2_4", NULL}
176 };
177 static byte w_horiz = 1;
178 static int max_wall_width;
179 static int max_wall_height;
180 static int max_textures;
181 static image walp[256];
182 static byte walswp[256];
183 static byte walani[256];
184 static image anip[ANIT][5];
185 static byte anic[ANIT];
186 static image horiz;
188 /* Texture cache */
190 // https://blackpawn.com/texts/lightmaps/
191 static node *R_node_alloc (node *p, int w, int h) {
192 assert(p);
193 assert(w > 0);
194 assert(h > 0);
195 if (p->left) {
196 assert(p->right);
197 node *n = R_node_alloc(p->left, w, h);
198 return n ? n : R_node_alloc(p->right, w, h);
199 } else {
200 int pw = p->r - p->l + 1;
201 int ph = p->b - p->t + 1;
202 if (p->leaf || pw < w || ph < h) {
203 return NULL;
204 } else if (pw == w && ph == h) {
205 p->leaf = 1;
206 return p;
207 } else {
208 p->left = malloc(sizeof(node));
209 p->right = malloc(sizeof(node));
210 if (pw - w > ph - h) {
211 *p->left = (node) {
212 .up = p,
213 .l = p->l,
214 .t = p->t,
215 .r = p->l + w - 1,
216 .b = p->b
217 };
218 *p->right = (node) {
219 .up = p,
220 .l = p->l + w,
221 .t = p->t,
222 .r = p->r,
223 .b = p->b
224 };
225 } else {
226 *p->left = (node) {
227 .up = p,
228 .l = p->l,
229 .t = p->t,
230 .r = p->r,
231 .b = p->t + h - 1
232 };
233 *p->right = (node) {
234 .up = p,
235 .l = p->l,
236 .t = p->t + h,
237 .r = p->r,
238 .b = p->b
239 };
241 return R_node_alloc(p->left, w, h);
246 static int R_node_have_leaf (node *n) {
247 return n && (n->leaf || R_node_have_leaf(n->left) || R_node_have_leaf(n->right));
250 static void R_node_free_recursive (node *n) {
251 if (n) {
252 R_node_free_recursive(n->left);
253 R_node_free_recursive(n->right);
254 free(n);
258 static void R_node_free (node *n) {
259 if (n) {
260 //logo("free node %p {%i:%i:%i:%i}\n", n, n->l, n->t, n->r, n->b);
261 assert(n->leaf);
262 assert(n->left == NULL);
263 assert(n->right == NULL);
264 n->leaf = 0;
265 n->base = NULL;
266 node *p = n->up;
267 while (p != NULL) {
268 assert(p->leaf == 0);
269 assert(p->left);
270 assert(p->right);
271 if (R_node_have_leaf(p) == 0) {
272 R_node_free_recursive(p->left);
273 p->left = NULL;
274 R_node_free_recursive(p->right);
275 p->right = NULL;
276 p = p->up;
277 } else {
278 p = NULL;
284 static void R_cache_get_max_texture_size (int *w, int *h) {
285 GLint size = 0;
286 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
287 size = min(max(size, 0), 512); // more can be buggy on older hardware
288 *w = size;
289 *h = size;
292 static void R_gl_bind_texture (GLuint id) {
293 if (id != lastTexture) {
294 glBindTexture(GL_TEXTURE_2D, id);
298 static cache *R_cache_new (void) {
299 int w, h;
300 GLuint id;
301 cache *c = NULL;
302 R_cache_get_max_texture_size(&w, &h);
303 if (w && h) {
304 glGenTextures(1, &id);
305 if (id) {
306 R_gl_bind_texture(id);
307 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
308 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
309 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
310 c = malloc(sizeof(cache));
311 if (c != NULL) {
312 *c = (cache) {
313 .id = id,
314 .root.r = w - 1,
315 .root.b = h - 1
316 };
317 } else {
318 glDeleteTextures(1, &id);
322 //logo("new cache %p\n", c);
323 return c;
326 static void R_cache_free (cache *root, int freetexture) {
327 cache *next;
328 cache *c = root;
329 while (c != NULL) {
330 next = c->next;
331 R_node_free_recursive(c->root.left);
332 R_node_free_recursive(c->root.right);
333 if (freetexture && c->id != 0) {
334 glDeleteTextures(1, &c->id);
336 free(c);
337 c = next;
341 static node *R_cache_alloc (cache *root, int w, int h) {
342 assert(root);
343 assert(w > 0 && h > 0);
344 node *n = NULL;
345 cache *p = NULL;
346 cache *c = root;
347 int maxw, maxh;
348 R_cache_get_max_texture_size(&maxw, &maxh);
349 if (w <= maxw && h <= maxh) {
350 while (c && !n) {
351 n = R_node_alloc(&c->root, w, h);
352 if (n) {
353 assert(n->leaf);
354 n->base = c;
356 p = c;
357 c = c->next;
359 if (!n) {
360 c = R_cache_new();
361 if (c) {
362 p->next = c;
363 n = R_node_alloc(&c->root, w, h);
364 if (n) {
365 assert(n->leaf);
366 n->base = c;
371 if (n) {
372 //logo("new node %p {%i:%i:%i:%i}\n", n, n->l, n->t, n->r, n->b);
373 } else {
374 logo("new node failed {%i:%i}\n", w, h);
376 return n;
379 static void R_cache_update (node *n, const void *data, int x, int y, int w, int h) {
380 assert(n);
381 assert(n->leaf);
382 assert(n->base);
383 assert(data);
384 assert(x >= 0);
385 assert(y >= 0);
386 assert(n->l + x + w - 1 <= n->r);
387 assert(n->t + y + h - 1 <= n->b);
388 R_gl_bind_texture(n->base->id);
389 glTexSubImage2D(GL_TEXTURE_2D, 0, n->l + x, n->t + y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
392 /* Generic helpers */
394 static void R_init_playpal (void) {
395 int i;
396 byte *vgapal = M_lock(F_getresid("PLAYPAL"));
397 for (i = 0; i < 256; i++) {
398 playpal[i] = (rgb) {
399 .r = vgapal[i * 3 + 0] * 255 / 63,
400 .g = vgapal[i * 3 + 1] * 255 / 63,
401 .b = vgapal[i * 3 + 2] * 255 / 63,
402 };
403 bright[i] = ((int)vgapal[i * 3 + 0] + vgapal[i * 3 + 1] + vgapal[i * 3 + 2]) * 8 / (63 * 3);
405 M_unlock(vgapal);
408 static vgaimg *R_getvga (int id) {
409 int loaded = M_was_locked(id);
410 vgaimg *v = M_lock(id);
411 if (v != NULL && !loaded) {
412 v->w = short2host(v->w);
413 v->h = short2host(v->h);
414 v->x = short2host(v->x);
415 v->y = short2host(v->y);
417 return v;
420 static rgba *R_extract_flame_spr (vgaimg *v) {
421 static const byte flametab[16] = {
422 0xBC, 0xBA, 0xB8, 0xB6, 0xB4, 0xB2, 0xB0, 0xD5,
423 0xD6, 0xD7, 0xA1, 0xA0, 0xE3, 0xE2, 0xE1, 0xE0
424 };
425 int i, j;
426 rgba *s = malloc(v->w * v->h * sizeof(rgba));
427 if (s != NULL) {
428 for (j = 0; j < v->h; j++) {
429 for (i = 0; i < v->w; i++) {
430 int k = j * v->w + i;
431 byte c = v->data[k] + bright[DEFAULT_SKY_COLOR];
432 s[k] = (rgba) {
433 .r = playpal[flametab[c]].r,
434 .g = playpal[flametab[c]].g,
435 .b = playpal[flametab[c]].b,
436 .a = v->data[k] == VGA_TRANSPARENT_COLOR ? 0x00 : 0xFF,
437 };
441 return s;
444 static rgba *R_extract_smoke_spr (vgaimg *v) {
445 int i, j;
446 rgba *s = malloc(v->w * v->h * sizeof(rgba));
447 if (s != NULL) {
448 for (j = 0; j < v->h; j++) {
449 for (i = 0; i < v->w; i++) {
450 int k = j * v->w + i;
451 byte c = ((v->data[k] + bright[DEFAULT_SKY_COLOR]) + 0x60) ^ 0x0F;
452 byte a = 0xFF - ((int)playpal[c].r + playpal[c].g + playpal[c].b) / 3;
453 s[k] = (rgba) {
454 .r = playpal[c].r,
455 .g = playpal[c].g,
456 .b = playpal[c].b,
457 .a = v->data[k] == VGA_TRANSPARENT_COLOR ? 0x00 : a,
458 };
462 return s;
465 static rgba *R_extract_mask_spr (vgaimg *v) {
466 int i, j;
467 rgba *s = malloc(v->w * v->h * sizeof(rgba));
468 if (s != NULL) {
469 for (j = 0; j < v->h; j++) {
470 for (i = 0; i < v->w; i++) {
471 int k = j * v->w + i;
472 byte c = v->data[k];
473 if (c >= 0x70 && c <= 0x7F) {
474 byte mask = c - 0x70;
475 mask = 0xFF - ((mask << 4) | mask);
476 s[k] = (rgba) {
477 .r = mask,
478 .g = mask,
479 .b = mask,
480 .a = 0xFF,
481 };
482 } else {
483 s[k] = (rgba) {
484 .r = 0,
485 .g = 0,
486 .b = 0,
487 .a = 0,
488 };
493 return s;
496 static rgba *R_extract_rgba_spr (vgaimg *v) {
497 int i, j;
498 rgba *s = malloc(v->w * v->h * sizeof(rgba));
499 if (s != NULL) {
500 for (j = 0; j < v->h; j++) {
501 for (i = 0; i < v->w; i++) {
502 int k = j * v->w + i;
503 byte c = v->data[k];
504 s[k] = (rgba) {
505 .r = playpal[c].r,
506 .g = playpal[c].g,
507 .b = playpal[c].b,
508 .a = c == VGA_TRANSPARENT_COLOR ? 0x00 : 0xFF,
509 };
513 return s;
516 /* OpenGL helpers */
518 static image R_gl_create_image (const rgba *buf, int w, int h) {
519 node *n = R_cache_alloc(root, w, h);
520 if (n) {
521 R_cache_update(n, buf, 0, 0, w, h);
523 return (image) {
524 .n = n,
525 .w = w,
526 .h = h,
527 .res = -1
528 };
531 static image R_gl_get_special_image (int id, rgba *(*fn)(vgaimg*)) {
532 image img;
533 //char name[8];
534 //F_getresname(name, id);
535 //logo("load image: %.8s\n", name);
536 vgaimg *v = R_getvga(id);
537 if (v != NULL) {
538 rgba *buf = (*fn)(v);
539 img = R_gl_create_image(buf, v->w, v->h);
540 img.x = v->x;
541 img.y = v->y;
542 img.res = id;
543 M_unlock(v);
544 free(buf);
545 } else {
546 img = (image) {
547 .res = id
548 };
550 return img;
553 static image R_gl_getimage (int id) {
554 return R_gl_get_special_image(id, &R_extract_rgba_spr);
557 static image R_gl_loadimage (const char name[8]) {
558 return R_gl_getimage(F_getresid(name));
561 static image R_gl_get_special_spr (const char n[4], int s, int d, rgba *(*fn)(vgaimg*)) {
562 return R_gl_get_special_image(F_getsprid(n, s, d, NULL), fn);
565 static void R_gl_free_image (image *img) {
566 if (img->n != NULL && img->res >= 0) {
567 R_node_free(img->n);
569 img->n = NULL;
570 img->res = -1;
573 static void R_gl_draw_quad (int x, int y, int w, int h) {
574 glBegin(GL_QUADS);
575 glVertex2i(x + w, y);
576 glVertex2i(x, y);
577 glVertex2i(x, y + h);
578 glVertex2i(x + w, y + h);
579 glEnd();
582 static void R_gl_draw_textured (image *img, int x, int y, int w, int h, int flip) {
583 if (img->n) {
584 GLfloat nw = img->n->base->root.r + 1;
585 GLfloat nh = img->n->base->root.b + 1;
586 GLfloat ax = (flip ? img->n->l : img->n->r + 1) / nw;
587 GLfloat bx = (flip ? img->n->r + 1 : img->n->l) / nh;
588 GLfloat ay = (img->n->t) / nw;
589 GLfloat by = (img->n->b + 1) / nh;
590 R_gl_bind_texture(img->n->base->id);
591 glEnable(GL_TEXTURE_2D);
592 glBegin(GL_QUADS);
593 glTexCoord2f(ax, ay); glVertex2i(x + w, y);
594 glTexCoord2f(bx, ay); glVertex2i(x, y);
595 glTexCoord2f(bx, by); glVertex2i(x, y + h);
596 glTexCoord2f(ax, by); glVertex2i(x + w, y + h);
597 glEnd();
598 } else {
599 glColor3ub(255, 0, 0);
600 glDisable(GL_BLEND);
601 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
602 glDisable(GL_TEXTURE_2D);
603 R_gl_draw_quad(x, y, w, h);
607 /* fit image into rectangle without applying offset and transparency */
608 static void R_gl_draw_image_ext (image *img, int x, int y, int w, int h) {
609 glDisable(GL_BLEND);
610 glColor3ub(255, 255, 255);
611 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
612 R_gl_draw_textured(img, x, y, w, h, 0);
615 /* draw sprite with offset and coloring */
616 static void R_gl_draw_image_color (image *img, int x, int y, int flip) {
617 int xx = flip ? x - img->w + img->x : x - img->x;
618 glEnable(GL_BLEND);
619 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
620 R_gl_draw_textured(img, xx, y - img->y, img->w, img->h, flip);
623 /* draw sprite with offset */
624 static void R_gl_draw_image (image *img, int x, int y, int flip) {
625 glColor3ub(255, 255, 255);
626 R_gl_draw_image_color(img, x, y, flip);
629 static void R_gl_set_color (byte c) {
630 glColor3ub(playpal[c].r, playpal[c].g, playpal[c].b);
633 static void R_gl_setclip (int x, int y, int w, int h) {
634 glScissor(x * screen_scale, (SCRH - h - y) * screen_scale, w * screen_scale, h * screen_scale);
637 static void R_gl_setmatrix (void) {
638 SCRW = screen_width / screen_scale;
639 SCRH = screen_height / screen_scale;
640 glScissor(0, 0, screen_width, screen_height);
641 glViewport(0, 0, screen_width, screen_height);
642 glMatrixMode(GL_PROJECTION);
643 glLoadIdentity();
644 glOrtho(0, SCRW, SCRH, 0, 0, 1);
645 glMatrixMode(GL_MODELVIEW);
646 glLoadIdentity();
649 /* --- Misc --- */
651 static image Z_getspr (const char n[4], int s, int d, char *dir) {
652 int h = F_getsprid(n, s, d, dir);
653 return R_gl_getimage(h);
656 static image *Z_get_char_image (image *img, int ch) {
657 image *p = NULL;
658 ch = cp866_toupper(ch);
659 if (ch > 32 && ch < 160) {
660 p = &img[ch - '!'];
662 return p;
665 static int Z_get_char_width_generic (image *img, int off, int ch) {
666 image *p = Z_get_char_image(img, ch);
667 return p == NULL ? off : p->w - 1;
670 static int Z_putch_generic (image *img, int off, int ch) {
671 image *p = Z_get_char_image(img, ch);
672 int w = p == NULL ? off : p->w - 1;
673 if (p != NULL && p->n != NULL) {
674 R_gl_draw_image(p, prx, pry, 0);
676 prx += w;
677 return w;
680 static int Z_get_string_width_generic (image *img, int off, const char *fmt, va_list ap) {
681 int i, w, ww;
682 char buf[80];
683 vsprintf(buf, fmt, ap);
684 for (i = w = ww = 0; buf[i]; ++i) {
685 switch (buf[i]) {
686 case '\n':
687 case '\r':
688 ww = max(w, ww);
689 w = 0;
690 break;
691 default:
692 w += Z_get_char_width_generic(img, off, (byte)buf[i]);
695 return max(w, ww);
698 static int Z_printf_generic (image *img, int off, const char *fmt, va_list ap) {
699 int i, w, ww;
700 char buf[80];
701 vsprintf(buf, fmt, ap);
702 for (i = w = ww = 0; buf[i]; ++i) {
703 switch (buf[i]) {
704 case '\n':
705 pry += off + 1;
706 case '\r':
707 w = max(w, ww);
708 prx = 0;
709 w = 0;
710 break;
711 default:
712 w += Z_putch_generic(img, off, (byte)buf[i]);
715 return w;
718 static void Z_gotoxy (int x, int y) {
719 prx = x;
720 pry = y;
723 static int Z_get_big_string_width (const char *fmt, ...) {
724 va_list a;
725 va_start(a, fmt);
726 int w = Z_get_string_width_generic(bfh, 12, fmt, a);
727 va_end(a);
728 return w;
731 static int Z_printbf (const char *fmt, ...) {
732 va_list a;
733 va_start(a, fmt);
734 int w = Z_printf_generic(bfh, 12, fmt, a);
735 va_end(a);
736 return w;
739 static int Z_get_small_string_width (const char *fmt, ...) {
740 va_list a;
741 va_start(a, fmt);
742 int w = Z_get_string_width_generic(sfh, 7, fmt, a);
743 va_end(a);
744 return w;
747 static int Z_printsf (const char *fmt, ...) {
748 va_list a;
749 va_start(a, fmt);
750 int w =Z_printf_generic(sfh, 7, fmt, a);
751 va_end(a);
752 return w;
755 static void Z_printhf (const char *fmt, ...) {
756 int i, c;
757 char buf[80];
758 va_list a;
759 va_start(a, fmt);
760 vsprintf(buf, fmt, a);
761 va_end(a);
762 for (i = 0; buf[i]; ++i) {
763 switch (buf[i]) {
764 case '0':
765 case '1':
766 case '2':
767 case '3':
768 case '4':
769 case '5':
770 case '6':
771 case '7':
772 case '8':
773 case '9':
774 c = buf[i] - '0';
775 break;
776 case '-':
777 c = 10;
778 break;
779 case '%':
780 c = 11;
781 break;
782 case '\n':
783 pry += 19;
784 case '\r':
785 c = -1;
786 prx = 0;
787 break;
788 default:
789 c = -1;
790 break;
792 if (c >= 0) {
793 R_gl_draw_image(&sth[c], prx, pry, 0);
795 prx += 14;
799 /* --- Menu --- */
801 static image *PL_getspr (int s, int d, int msk) {
802 int i = (s - 'A') * 2 + d;
803 return msk ? &plr_msk[i] : &plr_spr[i];
806 #define SCROLLER_MIDDLE 10
807 #define TEXTFIELD_MIDDLE 2
809 static void get_entry_size (const menu_t *m, int i, int *w, int *h) {
810 assert(m != NULL);
811 assert(i >= 0);
812 assert(w != NULL);
813 assert(h != NULL);
814 int x = 0;
815 int y = 0;
816 int type = 0;
817 menu_msg_t msg;
818 msg.type = GM_GETENTRY;
819 if (GM_send(m, i, &msg)) {
820 type = msg.integer.i;
821 switch (type) {
822 case GM_BUTTON:
823 case GM_SCROLLER:
824 case GM_TEXTFIELD:
825 case GM_TEXTFIELD_BUTTON:
826 msg.type = GM_GETCAPTION;
827 if (GM_send(m, i, &msg)) {
828 x = Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
830 break;
831 case GM_SMALL_BUTTON:
832 msg.type = GM_GETCAPTION;
833 if (GM_send(m, i, &msg)) {
834 x = Z_get_small_string_width("%.*s", msg.string.maxlen, msg.string.s);
836 break;
837 default:
838 assert(0);
840 switch (type) {
841 case GM_BUTTON:
842 msg.type = GM_GETSTR;
843 if (GM_send(m, i, &msg)) {
844 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
846 y = 16;
847 break;
848 case GM_SMALL_BUTTON:
849 msg.type = GM_GETSTR;
850 if (GM_send(m, i, &msg)) {
851 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
853 y = 12;
854 break;
855 case GM_SCROLLER:
856 x += (SCROLLER_MIDDLE + 2) * 8;
857 y = 16;
858 break;
859 case GM_TEXTFIELD:
860 case GM_TEXTFIELD_BUTTON:
861 msg.type = GM_GETSTR;
862 if (GM_send(m, i, &msg)) {
863 x += (msg.string.maxlen + 2) * 8;
864 } else {
865 x += (TEXTFIELD_MIDDLE + 2) * 8;
867 y = 16;
868 break;
869 default:
870 assert(0);
873 *w = x;
874 *h = y;
877 static void get_menu_size (const menu_t *m, int *w, int *h) {
878 assert(m != NULL);
879 assert(w != NULL);
880 assert(h != NULL);
881 int i, n, x, y, xx, yy, type;
882 menu_msg_t msg;
883 msg.type = GM_QUERY;
884 if (GM_send_this(m, &msg)) {
885 n = msg.integer.b;
886 type = msg.integer.s;
887 x = 0;
888 y = 0;
889 msg.type = GM_GETTITLE;
890 if (GM_send_this(m, &msg)) {
891 switch (type) {
892 case GM_BIG: x = Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); break;
893 case GM_SMALL: x = Z_get_small_string_width("%.*s", msg.string.maxlen, msg.string.s); break;
894 default: assert(0);
897 for (i = 0; i < n; i++) {
898 get_entry_size(m, i, &xx, &yy);
899 x = max(x, xx);
900 y += yy;
902 *w = x;
903 *h = y;
904 } else {
905 *w = 0;
906 *h = 0;
910 static int GM_draw (void) {
911 int i, j, n, x, y, xoff, yoff, cur, w, type, recv;
912 const menu_t *m = GM_get();
913 menu_msg_t msg;
914 if (m != NULL) {
915 get_menu_size(m, &x, &y);
916 x = SCRW / 2 - x / 2;
917 y = SCRH / 2 - y / 2;
918 // --- title ---
919 msg.type = GM_QUERY;
920 if (GM_send_this(m, &msg)) {
921 cur = msg.integer.i;
922 n = msg.integer.a;
923 type = msg.integer.s;
924 msg.type = GM_GETTITLE;
925 if (GM_send_this(m, &msg)) {
926 Z_gotoxy(x, y - 10);
927 switch (type) {
928 case GM_SMALL: yoff = 8; Z_printsf("%.*s", msg.string.maxlen, msg.string.s); break;
929 case GM_BIG: yoff = 20; Z_printbf("%.*s", msg.string.maxlen, msg.string.s); break;
930 default: assert(0);
932 } else {
933 yoff = 0;
935 for (i = 0; i < n; i++) {
936 msg.type = GM_GETENTRY;
937 if (GM_send(m, i, &msg)) {
938 type = msg.integer.i;
939 if (i == cur) {
940 if (type == GM_SMALL_BUTTON) {
941 Z_gotoxy(x - 8, y + yoff);
942 Z_printsf(">");
943 } else {
944 R_gl_draw_image(&msklh[(gm_tm / 6) & 1], x - 25, y + yoff - 8, 0);
947 msg.type = GM_GETCAPTION;
948 if (GM_send(m, i, &msg)) {
949 Z_gotoxy(x, y + yoff);
950 if (type == GM_SMALL_BUTTON) {
951 xoff = Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
952 } else {
953 xoff = Z_printbf("%.*s", msg.string.maxlen, msg.string.s);
955 } else {
956 xoff = 0;
958 switch (type) {
959 case GM_BUTTON:
960 case GM_SMALL_BUTTON:
961 msg.type = GM_GETSTR;
962 if (GM_send(m, i, &msg)) {
963 Z_gotoxy(x + xoff, y + yoff);
964 if (type == GM_SMALL_BUTTON) {
965 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
966 } else {
967 Z_printbf("%.*s", msg.string.maxlen, msg.string.s);
970 yoff += type == GM_BUTTON ? 16 : 12;
971 break;
972 case GM_TEXTFIELD:
973 case GM_TEXTFIELD_BUTTON:
974 yoff += 9;
975 msg.type = GM_GETSTR;
976 recv = GM_send(m, i, &msg);
977 w = recv ? msg.string.maxlen : TEXTFIELD_MIDDLE;
978 R_gl_draw_image(&mslotl, x + xoff, y + yoff, 0);
979 for (j = 1; j <= w; j++) {
980 R_gl_draw_image(&mslotm, x + xoff + j * 8, y + yoff, 0);
982 R_gl_draw_image(&mslotr, x + xoff + j * 8, y + yoff, 0);
983 Z_gotoxy(x + xoff + 4, y + yoff - 7);
984 if (input && i == cur) {
985 Z_printsf("%.*s_", imax, ibuf);
986 } else if (recv) {
987 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
989 yoff += 7;
990 break;
991 case GM_SCROLLER:
992 R_gl_draw_image(&mbarl, x + xoff, y + yoff, 0);
993 for (j = 1; j < SCROLLER_MIDDLE; j++) {
994 R_gl_draw_image(&mbarm, x + xoff + j * 8, y + yoff, 0);
996 R_gl_draw_image(&mbarr, x + xoff + j * 8, y + yoff, 0);
997 msg.type = GM_GETINT;
998 if (GM_send(m, i, &msg)) {
999 int lev = (msg.integer.i - msg.integer.a) * ((SCROLLER_MIDDLE - 2) * 8) / msg.integer.b;
1000 R_gl_draw_image(&mbaro, x + xoff + lev + 8, y + yoff, 0);
1002 yoff += 16;
1003 break;
1004 default:
1005 assert(0);
1011 return m != NULL;
1014 /* --- View --- */
1016 static void R_draw_fld (byte *fld, int minx, int miny, int maxx, int maxy, int fg) {
1017 int i, j;
1018 assert(minx >= 0 && minx <= FLDW);
1019 assert(miny >= 0 && miny <= FLDH);
1020 assert(maxx >= 0 && maxx <= FLDW);
1021 assert(maxy >= 0 && maxy <= FLDH);
1022 for (j = miny; j < maxy; j++) {
1023 for (i = minx; i < maxx; i++) {
1024 byte id = fld[j * FLDW + i];
1025 if (id != 0) {
1026 if (walp[id].res < 0) {
1027 if (fg) {
1028 switch (R_get_special_id(id)) {
1029 case 1:
1030 glColor4ub(0, 0, 255, 127);
1031 break;
1032 case 2:
1033 glColor4ub(0, 127, 0, 127);
1034 break;
1035 case 3:
1036 glColor4ub(127, 0, 0, 127);
1037 break;
1038 default:
1039 glColor4ub(0, 0, 0, 127);
1040 break;
1042 glEnable(GL_BLEND);
1043 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1044 glDisable(GL_TEXTURE_2D);
1045 R_gl_draw_quad(i * CELW, j * CELW, CELW, CELH);
1047 } else {
1048 R_gl_draw_image(&walp[id], i * CELW, j * CELH, 0);
1055 static void R_draw_dots (void) {
1056 int i;
1057 glDisable(GL_BLEND);
1058 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1059 glDisable(GL_TEXTURE_2D);
1060 glBegin(GL_QUADS);
1061 for (i = 0; i < MAXDOT; i++) {
1062 if (dot[i].t != 0) {
1063 int x = dot[i].o.x;
1064 int y = dot[i].o.y;
1065 R_gl_set_color(dot[i].c); glVertex2i(x + 1, y);
1066 R_gl_set_color(dot[i].c); glVertex2i(x, y);
1067 R_gl_set_color(dot[i].c); glVertex2i(x, y + 1);
1068 R_gl_set_color(dot[i].c); glVertex2i(x + 1, y + 1);
1071 glEnd();
1074 static void R_draw_items (void) {
1075 int i, s;
1076 for (i = 0; i < MAXITEM; ++i) {
1077 s = -1;
1078 if (it[i].t && it[i].s >= 0) {
1079 switch (it[i].t & 0x7FFF) {
1080 case I_ARM1:
1081 s = it[i].s / 9 + 18;
1082 break;
1083 case I_ARM2:
1084 s = it[i].s / 9 + 20;
1085 break;
1086 case I_MEGA:
1087 s = it[i].s / 2 + 22;
1088 break;
1089 case I_INVL:
1090 s = it[i].s / 2 + 26;
1091 break;
1092 case I_SUPER:
1093 case I_RTORCH:
1094 case I_GTORCH:
1095 case I_BTORCH:
1096 s = it[i].s / 2 + (it[i].t - I_SUPER) * 4 + 35;
1097 break;
1098 case I_GOR1: case I_FCAN:
1099 s = it[i].s / 2 + (it[i].t - I_GOR1) * 3 + 51;
1100 break;
1101 case I_AQUA:
1102 s = 30;
1103 break;
1104 case I_SUIT:
1105 s = 34;
1106 break;
1107 case I_KEYR:
1108 case I_KEYG:
1109 case I_KEYB:
1110 s = (it[i].t & 0x7FFF) - I_KEYR + 31;
1111 break;
1112 case I_GUN2:
1113 s = 57;
1114 break;
1115 default:
1116 s = (it[i].t & 0x7FFF) - 1;
1119 if (s >= 0) {
1120 R_gl_draw_image(&item_spr[s], it[i].o.x, it[i].o.y, item_sprd[s]);
1125 static int standspr (player_t *p) {
1126 if (p->f & PLF_UP) {
1127 return 'X';
1128 } else if (p->f & PLF_DOWN) {
1129 return 'Z';
1130 } else {
1131 return 'E';
1135 static int wpnspr (player_t *p) {
1136 if (p->f & PLF_UP) {
1137 return 'C';
1138 } else if(p->f & PLF_DOWN) {
1139 return 'E';
1140 } else {
1141 return 'A';
1145 static void R_draw_player (player_t *p) {
1146 enum {STAND, GO, DIE, SLOP, DEAD, MESS, OUT_, FALL}; // copypasted from player.c!
1147 static const int wytab[] = {-1, -2, -1, 0};
1148 int s = 'A';
1149 int w = 0;
1150 int wx = 0;
1151 int wy = 0;
1152 switch (p->st) {
1153 case STAND:
1154 if (p->f & PLF_FIRE) {
1155 s = standspr(p) + 1;
1156 w = wpnspr(p) + 1;
1157 } else if (p->pain) {
1158 s = 'G';
1159 w = 'A';
1160 wx = p->d ? 2 : -2;
1161 wy = 1;
1162 } else {
1163 s = standspr(p);
1164 w = wpnspr(p);
1166 break;
1167 case DEAD:
1168 s = 'N';
1169 break;
1170 case MESS:
1171 s = 'W';
1172 break;
1173 case GO:
1174 if (p->pain) {
1175 s = 'G';
1176 w = 'A';
1177 wx = p->d ? 2 : -2;
1178 wy = 1;
1179 } else {
1180 s = plr_goanim[p->s / 8];
1181 w = (p->f & PLF_FIRE) ? 'B' : 'A';
1182 wx = p->d ? 2 : -2;
1183 wy = 1 + wytab[s - 'A'];
1185 break;
1186 case DIE:
1187 s = plr_dieanim[p->s];
1188 break;
1189 case SLOP:
1190 s = plr_slopanim[p->s];
1191 break;
1192 case OUT_:
1193 s = 0;
1194 break;
1196 if (p->wpn == 0) {
1197 w = 0;
1199 if (w) {
1200 R_gl_draw_image(&plr_wpn[(int)p->wpn][w -'A'], p->o.x + wx, p->o.y + wy, p->d);
1202 if (s) {
1203 R_gl_draw_image(&plr_spr[(s - 'A') * 2 + p->d], p->o.x, p->o.y, plr_sprd[(s - 'A') * 2 + p->d]);
1204 R_gl_set_color(p->color + PLAYER_COLOR_OFFSET);
1205 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]);
1209 static void R_draw_monsters (void) {
1210 enum {SLEEP, GO, RUN, CLIMB, DIE, DEAD, ATTACK, SHOOT, PAIN, WAIT, REVIVE, RUNOUT}; // copypasted from monster.c!
1211 int i;
1212 for (i = 0; i < MAXMN; i++) {
1213 if (mn[i].t != MN_NONE) {
1214 int x = mn[i].o.x;
1215 int y = mn[i].o.y;
1216 if (mn[i].t < MN__LAST) {
1217 if ((mn[i].t != MN_SOUL && mn[i].t != MN_PAIN) || mn[i].st != DEAD) {
1218 int ap = mn[i].ap[mn[i].ac];
1219 int d = (ap - 'A') * 2 + mn[i].d;
1220 int dir = mn_sprd[mn[i].t - 1][d];
1221 if (mn[i].t == MN_MAN && (ap == 'E' || ap == 'F')) {
1222 R_gl_draw_image(&mn_sgun[ap - 'E'], x, y, mn[i].d);
1224 R_gl_draw_image(&mn_spr[mn[i].t - 1][d], x, y, dir);
1225 if (mn[i].t == MN_MAN) {
1226 R_gl_set_color(MANCOLOR + PLAYER_COLOR_OFFSET);
1227 R_gl_draw_image_color(&mn_man_msk[d], x, y, dir);
1230 if (mn[i].t == MN_VILE && mn[i].st == SHOOT) {
1231 R_gl_draw_image(&mn_fspr[mn[i].ac / 3], mn[i].tx, mn[i].ty, 0);
1233 } else if (mn[i].t == MN_PL_DEAD || mn[i].t == MN_PL_MESS) {
1234 int type = mn[i].t - MN_PL_DEAD;
1235 R_gl_draw_image(&pl_spr[type], x, y, 0);
1236 R_gl_set_color(mn[i].d);
1237 R_gl_draw_image_color(&pl_msk[type], x, y, 0);
1243 static void R_draw_weapons (void) {
1244 enum {NONE, ROCKET, PLASMA, APLASMA, BALL1, BALL2, BALL7, BFGBALL, BFGHIT, MANF, REVF, FIRE}; // copypasted from weapons.c!
1245 int i, s, d, x, y;
1246 for (i = 0; i < MAXWPN; ++i) {
1247 s = -1;
1248 d = 0;
1249 switch (wp[i].t) {
1250 case REVF:
1251 case ROCKET:
1252 d = wp[i].s;
1253 if (d < 2) {
1254 d = wp[i].o.xv > 0 ? 1 : 0;
1255 x = abs(wp[i].o.xv);
1256 y = wp[i].o.yv;
1257 s = 0;
1258 if (y < 0) {
1259 if (-y >= x) {
1260 s = 30;
1262 } else if (y > 0) {
1263 if (y >= x / 2) {
1264 s = 31;
1267 } else {
1268 s = (d - 2) / 2 + 1;
1269 d = 0;
1271 break;
1272 case MANF:
1273 s=wp[i].s;
1274 if (s >= 2) {
1275 s /= 2;
1276 break;
1278 case PLASMA:
1279 case APLASMA:
1280 case BALL1:
1281 case BALL7:
1282 case BALL2:
1283 s = wp[i].s;
1284 if (s >= 2) {
1285 s = s / 2 + 1;
1287 switch (wp[i].t) {
1288 case PLASMA:
1289 s += 4;
1290 break;
1291 case APLASMA:
1292 s += 11;
1293 break;
1294 case BALL1:
1295 s += 32;
1296 break;
1297 case BALL2:
1298 s += 42;
1299 break;
1300 case BALL7:
1301 s += 37;
1302 d = wp[i].o.xv >= 0 ? 1 : 0;
1303 break;
1304 case MANF:
1305 s += 47;
1306 d= wp[i].o.xv>=0 ? 1 : 0;
1307 break;
1309 break;
1310 case BFGBALL:
1311 s = wp[i].s;
1312 if (s >= 2) {
1313 s = s / 2 + 1;
1315 s += 18;
1316 break;
1317 case BFGHIT:
1318 s = wp[i].s / 2 + 26;
1319 break;
1321 if (s >= 0) {
1322 R_gl_draw_image(&wp_spr[s * 2 + d], wp[i].o.x, wp[i].o.y, wp_sprd[s * 2 + d]);
1327 static void R_draw_smoke (void) {
1328 int i, s;
1329 for (i = 0; i < MAXSMOK; ++i) {
1330 if (sm[i].t) {
1331 switch (sm[i].s) {
1332 case 0:
1333 s = sm[i].t;
1334 if (s >= (SMSN - 1) * 3) {
1335 s = 0;
1336 } else {
1337 s = SMSN - 1 - s / 3;
1339 R_gl_draw_image(&smk_spr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1340 break;
1341 case 1:
1342 s = sm[i].t;
1343 if (s >= FLSN - 1) {
1344 s = 0;
1345 } else {
1346 s = FLSN - 1 - s;
1348 R_gl_draw_image(&smk_fspr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1349 break;
1355 static void R_draw_effects (void) {
1356 enum {NONE, TFOG, IFOG, BUBL}; // copypasted from fx.c
1357 int i, s;
1358 for (i = 0; i < MAXFX; ++i) {
1359 switch (fx[i].t) {
1360 case TFOG:
1361 s = fx[i].s / 2;
1362 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1363 break;
1364 case IFOG:
1365 s = fx[i].s / 2 + 10;
1366 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1367 break;
1368 case BUBL:
1369 glDisable(GL_BLEND);
1370 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1371 glDisable(GL_TEXTURE_2D);
1372 R_gl_set_color(0xC0 + fx[i].s);
1373 R_gl_draw_quad(fx[i].x >> 8, fx[i].y >> 8, 1, 1);
1374 break;
1379 static int get_pu_st (int t) {
1380 if (t >= PL_FLASH) {
1381 return 1;
1382 } else if((t / 9) & 1) {
1383 return 0;
1384 } else {
1385 return 1;
1389 static void R_draw_view (int x, int y, int w, int h, int camx, int camy) {
1390 glPushMatrix();
1391 R_gl_setclip(x, y, w, h);
1392 glTranslatef(x, y, 0);
1393 if (w_horiz && horiz.n != NULL) {
1394 R_gl_draw_image_ext(&horiz, 0, 0, w, h);
1395 if (sky_type == 2 && lt_time < 0) {
1396 image *tanderbolt = &ltn[lt_type][lt_time < -5 ? 0 : 1];
1397 if (!lt_side) {
1398 R_gl_draw_image(tanderbolt, 0, lt_ypos, 0);
1399 } else {
1400 R_gl_draw_image(tanderbolt, w - 1, lt_ypos, 1);
1403 } else {
1404 glDisable(GL_BLEND);
1405 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1406 glDisable(GL_TEXTURE_2D);
1407 R_gl_set_color(DEFAULT_SKY_COLOR);
1408 R_gl_draw_quad(0, 0, w, h);
1410 int maxx = min((camx + w) / CELW + 1, FLDW);
1411 int maxy = min((camy + h) / CELH + 1, FLDH);
1412 int minx = max((camx - max_wall_width) / CELW, 0);
1413 int miny = max((camy - max_wall_height) / CELH, 0);
1414 glTranslatef(-camx, -camy, 0);
1415 R_draw_fld((byte*)fldb, minx, miny, maxx, maxy, 0);
1416 R_draw_dots();
1417 R_draw_items();
1418 R_draw_player(&pl1);
1419 if (_2pl) {
1420 R_draw_player(&pl2);
1422 R_draw_monsters();
1423 R_draw_weapons();
1424 R_draw_smoke();
1425 R_draw_effects();
1426 R_draw_fld((byte*)fldf, minx, miny, maxx, maxy, 1);
1427 glTranslatef(camx, camy, 0);
1428 if (sky_type == 2 && (lt_time == -4 || lt_time == -2)) {
1429 glColor4ub(255, 255, 255, 255);
1430 glEnable(GL_BLEND);
1431 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1432 glDisable(GL_TEXTURE_2D);
1433 R_gl_draw_quad(0, 0, w, h);
1435 glPopMatrix();
1438 static void R_draw_player_view (player_t *p, int x, int y, int w, int h) {
1439 p->looky = min(max(p->looky, -SCRH / 4), SCRH / 4); // TODO remove writeback
1440 int st = stone.w;
1441 int cw = w - st;
1442 int cx = min(max(p->o.x, cw / 2), FLDW * CELW - cw / 2);
1443 int cy = min(max(p->o.y - 12 + p->looky, h / 2), FLDH * CELH - h / 2);
1444 int camx = max(cx - cw / 2, 0);
1445 int camy = max(cy - h / 2, 0);
1446 glPushMatrix();
1447 R_draw_view(x, y + 1, cw, h - 2, camx, camy);
1448 glTranslatef(x, y, 0);
1449 if (p->invl) {
1450 if (get_pu_st(p->invl)) {
1451 glEnable(GL_BLEND);
1452 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1453 glDisable(GL_TEXTURE_2D);
1454 glColor4ub(191, 191, 191, 255);
1455 R_gl_draw_quad(0, 0, cw, h);
1457 } else {
1458 if (p->suit && get_pu_st(p->suit)) {
1459 glEnable(GL_BLEND);
1460 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1461 glDisable(GL_TEXTURE_2D);
1462 glColor4ub(0, 255, 0, 192);
1463 R_gl_draw_quad(0, 0, cw, h);
1465 int f = min(max(p->pain * 3, 0), 255);
1466 if (f > 0) {
1467 glEnable(GL_BLEND);
1468 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1469 glDisable(GL_TEXTURE_2D);
1470 glColor4ub(255, 0, 0, f);
1471 R_gl_draw_quad(0, 0, cw, h);
1474 R_gl_setclip(x, y, w, h);
1475 glTranslatef(-x + cw, 0, 0);
1476 R_gl_draw_image(&stone, 0, 0, 0);
1477 int i = stone.h;
1478 while (i < h) {
1479 R_gl_draw_image(&stone2, 0, i, 0);
1480 i += stone2.h;
1482 if (p->drawst & PL_DRAWAIR) {
1483 if (p->air < PL_AIR) {
1484 int a = min(max(p->air, 0), MAXAIR) * 100 / MAXAIR;
1485 glDisable(GL_BLEND);
1486 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1487 glDisable(GL_TEXTURE_2D);
1488 R_gl_set_color(0xC8);
1489 R_gl_draw_quad(10, 49, a, 2);
1492 if (p->drawst & PL_DRAWLIFE) {
1493 Z_gotoxy(10, 7);
1494 Z_printhf("%3d%%", p->life);
1496 if (p->drawst & PL_DRAWARMOR) {
1497 Z_gotoxy(10, 7 + 19);
1498 Z_printhf("%3d%%", p->armor);
1500 if (p->drawst & PL_DRAWWPN) {
1501 switch(p->wpn) {
1502 case 2:
1503 case 5:
1504 i = p->ammo;
1505 break;
1506 case 3:
1507 case 4:
1508 case 9:
1509 i = p->shel;
1510 break;
1511 case 6:
1512 i = p->rock;
1513 break;
1514 case 7:
1515 case 8:
1516 i = p->cell;
1517 break;
1518 case 10:
1519 i = p->fuel;
1520 break;
1521 default:
1522 i = -1;
1523 break;
1525 // weapon
1526 if (p->wpn >= 0) {
1527 R_gl_draw_image(&sth[12 + p->wpn], st - 88, 58 + 19, 0);
1529 // ammo
1530 if (p->wpn >= 2) {
1531 Z_gotoxy(st - 10 - 5 * 14, 58 + 2);
1532 Z_printhf("%5d", i);
1535 if (p->drawst & PL_DRAWFRAG && g_dm) {
1536 Z_gotoxy(st - 5 - 5 * 14, 77 + 5);
1537 Z_printhf("%5d", p->frag);
1539 if (p->drawst & PL_DRAWKEYS) {
1540 int x, k, n;
1541 for (k = p->keys >> 4, n = 0, x = st - 75; n < 3; n++, k >>= 1, x += 9) {
1542 if (k & 1) {
1543 R_gl_draw_image(&keys[n], x, 91, 0);
1547 if (p->drawst & PL_DRAWLIVES && !_2pl) {
1548 Z_gotoxy(st - 35, 17);
1549 Z_printhf("%d", p->lives);
1551 glPopMatrix();
1554 /* --- Game --- */
1556 static void pl_info (player_t *p, int x, int y) {
1557 dword t = p->kills * 10920 / g_time;
1558 Z_gotoxy(x + 25, y); Z_printbf("KILLS");
1559 Z_gotoxy(x + 25, y + 15); Z_printbf("KPM");
1560 Z_gotoxy(x + 25, y + 30); Z_printbf("SECRETS %u / %u", p->secrets, sw_secrets);
1561 Z_gotoxy(x + 255, y); Z_printbf("%u", p->kills);
1562 Z_gotoxy(x + 255, y + 15); Z_printbf("%u.%u", t / 10, t % 10);
1565 static void R_draw_intermission (void) {
1566 int cx = SCRW / 2;
1567 word hr, mn, sc, h;
1568 Z_gotoxy(cx - 14*12/2, 20);
1569 Z_printbf("LEVEL COMPLETE");
1570 Z_calc_time(g_time, &hr, &mn, &sc);
1571 Z_gotoxy(cx - 12*12/2, 40);
1572 Z_printbf("TIME %u:%02u:%02u", hr, mn, sc);
1573 h = 40 + SCRH / 10;
1574 if (_2pl) {
1575 Z_gotoxy(cx - 10*12/2, h);
1576 Z_printbf("PLAYER ONE");
1577 h += 20;
1579 pl_info(&pl1, cx - 160, h);
1580 if (_2pl) {
1581 h += 30 + SCRH / 10;
1582 Z_gotoxy(cx - 10*12/2, h);
1583 Z_printbf("PLAYER TWO");
1584 h += 20;
1585 pl_info(&pl2, cx - 160, h);
1589 static void W_act (void) {
1590 int i, a;
1591 if (g_time % 3 == 0) {
1592 for (i = 1; i < max_textures; i++) {
1593 a = walani[i];
1594 if (a != 0) {
1595 anic[a]++;
1596 if (anip[a][anic[a]].res == -1) {
1597 anic[a] = 0;
1599 walp[i] = anip[a][anic[a]];
1605 void R_draw (void) {
1606 W_act();
1607 glClearColor(0, 0, 0, 1);
1608 glClear(GL_COLOR_BUFFER_BIT);
1609 glEnable(GL_SCISSOR_TEST);
1610 R_gl_setmatrix();
1611 switch (g_st) {
1612 case GS_ENDANIM:
1613 case GS_END2ANIM:
1614 case GS_DARKEN:
1615 case GS_BVIDEO:
1616 case GS_EVIDEO:
1617 case GS_END3ANIM:
1618 break;
1619 case GS_TITLE:
1620 R_gl_draw_image_ext(&scrnh[0], 0, 0, SCRW, SCRH);
1621 break;
1622 case GS_INTER:
1623 R_gl_draw_image_ext(&scrnh[1], 0, 0, SCRW, SCRH);
1624 R_draw_intermission();
1625 break;
1626 case GS_ENDSCR:
1627 R_gl_draw_image_ext(&scrnh[2], 0, 0, SCRW, SCRH);
1628 break;
1629 case GS_GAME:
1630 if (_2pl) {
1631 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH / 2);
1632 R_draw_player_view(&pl2, 0, SCRH / 2, SCRW, SCRH / 2);
1633 } else {
1634 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH);
1636 R_gl_setclip(0, 0, SCRW, SCRH);
1637 break;
1639 GM_draw();
1640 Y_swap_buffers();
1643 static void R_alloc (void) {
1644 char s[10];
1645 int i, j, n;
1646 logo("R_alloc: load graphics\n");
1647 /* Game */
1648 scrnh[0] = R_gl_loadimage("TITLEPIC");
1649 scrnh[1] = R_gl_loadimage("INTERPIC");
1650 scrnh[2] = R_gl_loadimage("ENDPIC");
1651 for (i = 0; i < 2; i++) {
1652 sprintf(s, "LTN%c", '1' + i);
1653 for (j = 0; j < 2; j++) {
1654 ltn[i][j] = Z_getspr(s, j, 0, NULL);
1657 /* Smoke */
1658 for (i = 0; i < SMSN; i++) {
1659 smk_spr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_smoke_spr);
1661 for (i = 0; i < FLSN; i++) {
1662 smk_fspr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_flame_spr);
1664 /* Effects */
1665 for (i = 0; i < 10; i++) {
1666 fx_spr[i] = Z_getspr("TFOG", i, 0, fx_sprd + i);
1668 for (; i < 15; i++) {
1669 fx_spr[i] = Z_getspr("IFOG", i - 10, 0, fx_sprd + i);
1671 /* Weapons */
1672 for (i = 0; i < 4; i++) {
1673 wp_spr[i * 2] = Z_getspr("MISL", i, 1, wp_sprd + i * 2);
1674 wp_spr[i * 2 + 1] = Z_getspr("MISL", i, 2, wp_sprd + i * 2 + 1);
1676 for (; i < 6; i++) {
1677 wp_spr[i * 2] = Z_getspr("PLSS", i - 4, 1, wp_sprd + i * 2);
1678 wp_spr[i * 2 + 1] = Z_getspr("PLSS", i - 4, 2, wp_sprd + i * 2 + 1);
1680 for (; i < 11; i++) {
1681 wp_spr[i * 2] = Z_getspr("PLSE", i - 6, 1, wp_sprd + i * 2);
1682 wp_spr[i * 2 + 1] = Z_getspr("PLSE", i - 6, 2, wp_sprd + i * 2 + 1);
1684 for (; i < 13; i++) {
1685 wp_spr[i * 2] = Z_getspr("APLS", i - 11, 1, wp_sprd + i * 2);
1686 wp_spr[i * 2 + 1] = Z_getspr("APLS", i - 11, 2, wp_sprd + i * 2 + 1);
1688 for (; i < 18; i++) {
1689 wp_spr[i * 2] = Z_getspr("APBX", i - 13, 1, wp_sprd + i * 2);
1690 wp_spr[i * 2 + 1] = Z_getspr("APBX", i - 13, 2, wp_sprd + i * 2 + 1);
1692 for(; i < 20; i++) {
1693 wp_spr[i * 2] = Z_getspr("BFS1", i - 18, 1, wp_sprd + i * 2);
1694 wp_spr[i * 2 + 1] = Z_getspr("BFS1", i - 18, 2, wp_sprd + i * 2 + 1);
1696 for (; i < 26; i++) {
1697 wp_spr[i * 2] = Z_getspr("BFE1", i - 20, 1, wp_sprd + i * 2);
1698 wp_spr[i * 2 + 1] = Z_getspr("BFE1", i - 20, 2, wp_sprd + i * 2 + 1);
1700 for (; i < 30; i++) {
1701 wp_spr[i * 2] = Z_getspr("BFE2", i - 26, 1, wp_sprd + i * 2);
1702 wp_spr[i * 2 + 1] = Z_getspr("BFE2", i - 26, 2, wp_sprd + i * 2 + 1);
1704 for (; i < 32; i++) {
1705 wp_spr[i * 2] = Z_getspr("MISL", i - 30 + 4, 1, wp_sprd + i * 2);
1706 wp_spr[i * 2 + 1] = Z_getspr("MISL", i - 30 + 4, 2, wp_sprd + i * 2 + 1);
1708 for (; i < 37; i++) {
1709 wp_spr[i * 2] = Z_getspr("BAL1", i - 32, 1, wp_sprd + i * 2);
1710 wp_spr[i * 2 + 1] = Z_getspr("BAL1", i - 32, 2, wp_sprd + i * 2 + 1);
1712 for (; i < 42; i++) {
1713 wp_spr[i * 2] = Z_getspr("BAL7", i - 37, 1, wp_sprd + i * 2);
1714 wp_spr[i * 2 + 1] = Z_getspr("BAL7", i - 37, 2, wp_sprd + i * 2 + 1);
1716 for (; i < 47; i++) {
1717 wp_spr[i * 2] = Z_getspr("BAL2", i - 42, 1, wp_sprd + i * 2);
1718 wp_spr[i * 2 + 1] = Z_getspr("BAL2", i - 42, 2, wp_sprd + i * 2 + 1);
1720 for (; i < 49; i++) {
1721 wp_spr[i * 2] = Z_getspr("MANF", i - 47, 1, wp_sprd + i * 2);
1722 wp_spr[i * 2 + 1] = Z_getspr("MANF", i - 47, 2, wp_sprd + i * 2 + 1);
1724 /* Items */
1725 static const char snm[18][4] = {
1726 "CLIP", "SHEL", "ROCK", "CELL", "AMMO", "SBOX", "BROK", "CELP",
1727 "STIM", "MEDI", "BPAK",
1728 "CSAW", "SHOT", "SGN2", "MGUN", "LAUN", "PLAS", "BFUG"
1729 };
1730 static const char n4[4][4] = {
1731 "SOUL", "SMRT", "SMGT", "SMBT"
1732 };
1733 static const char n3[2][4] = {
1734 "GOR1", "FCAN"
1735 };
1736 for (i = 0; i < 18; i++) {
1737 item_spr[i] = Z_getspr(snm[i], 0, 0, item_sprd + i);
1739 for (; i < 20; i++) {
1740 item_spr[i] = Z_getspr("ARM1", i - 18, 0, item_sprd + i);
1741 item_spr[i + 2] = Z_getspr("ARM2", i - 18, 0, item_sprd + i);
1743 i+=2;
1744 for (; i < 26; i++) {
1745 item_spr[i] = Z_getspr("MEGA", i - 22, 0, item_sprd + i);
1747 for (; i < 30; i++) {
1748 item_spr[i] = Z_getspr("PINV", i - 26, 0, item_sprd + i);
1750 item_spr[30] = Z_getspr("AQUA", 0, 0, item_sprd + 30);
1751 item_spr[31] = Z_getspr("KEYR", 0, 0, item_sprd + 31);
1752 item_spr[32] = Z_getspr("KEYG", 0, 0, item_sprd + 32);
1753 item_spr[33] = Z_getspr("KEYB", 0, 0, item_sprd + 33);
1754 item_spr[34] = Z_getspr("SUIT", 0, 0, item_sprd + 34);
1755 for (n = 35, j = 0; j < 4; j++) {
1756 for (i = 0; i < 4; i++, n++) {
1757 item_spr[n] = Z_getspr(n4[j], i, 0, item_sprd + n);
1760 for (j = 0; j < 2; j++) {
1761 for (i = 0; i < 3; i++, n++) {
1762 item_spr[n] = Z_getspr(n3[j], i, 0, item_sprd + n);
1765 item_spr[57] = Z_getspr("GUN2", 0, 0, item_sprd + 57);
1766 /* Player */
1767 for (i = 0; i < 27; i++) {
1768 plr_spr[i * 2] = Z_getspr("PLAY", i, 1, plr_sprd + i * 2);
1769 plr_msk[i * 2] = R_gl_get_special_spr("PLAY", i, 1, &R_extract_mask_spr);
1770 plr_spr[i * 2 + 1] = Z_getspr("PLAY", i, 2, plr_sprd + i * 2 + 1);
1771 plr_msk[i * 2 + 1] = R_gl_get_special_spr("PLAY", i, 2, &R_extract_mask_spr);
1773 strncpy(s, "PWPx", 4);
1774 for (i = 1; i < 11; i++) {
1775 s[3] = (i < 10 ? '0' : 'A' - 10) + i;
1776 for (j = 0; j < 6; j++) {
1777 plr_wpn[i][j] = Z_getspr(s, j, 1, NULL);
1780 /* Monsters */
1781 static const char msn[MN_TN][4] = {
1782 "SARG", "TROO", "POSS", "SPOS", "CYBR", "CPOS", "BOSS", "BOS2", "HEAD", "SKUL",
1783 "PAIN", "SPID", "BSPI", "FATT", "SKEL", "VILE", "FISH", "BAR1", "ROBO", "PLAY"
1784 };
1785 static const int mms[MN_TN] = {
1786 14*2, 21*2, 21*2, 21*2, 16*2, 20*2, 15*2, 15*2, 12*2, 11*2,
1787 13*2, 19*2, 16*2, 20*2, 17*2, 29*2, 6*2, 2*2, 17*2, 23*2
1788 };
1789 mn_sgun[0] = Z_getspr("PWP4", 0, 1, NULL);
1790 mn_sgun[1] = Z_getspr("PWP4", 1, 1, NULL);
1791 for (j = 0; j < MN_TN; j++) {
1792 for (i = 0; i < mms[j]; i++) {
1793 mn_spr[j][i] = Z_getspr(msn[j], i / 2, (i & 1) + 1, &mn_sprd[j][i]);
1794 if (j == MN_MAN - 1) {
1795 mn_man_msk[i] = R_gl_get_special_spr(msn[j], i / 2, (i & 1) + 1, &R_extract_mask_spr);
1798 if (j == MN_BARREL - 1) {
1799 for (i = 4; i < 14; i++) {
1800 mn_spr[j][i] = Z_getspr("BEXP", i / 2 - 2, (i & 1) + 1, &mn_sprd[j][i]);
1804 for (i = 0; i < 8; i++) {
1805 mn_fspr[i] = Z_getspr("FIRE", i, 0, NULL);
1807 pl_spr[0] = Z_getspr("PLAY", 'N' - 'A', 0, NULL);
1808 pl_msk[0] = R_gl_get_special_spr("PLAY", 'N' - 'A', 0, &R_extract_mask_spr);
1809 pl_spr[1] = Z_getspr("PLAY", 'W' - 'A', 0, NULL);
1810 pl_msk[1] = R_gl_get_special_spr("PLAY", 'W' - 'A', 0, &R_extract_mask_spr);
1811 /* Misc */
1812 static const char mnm[22][8]={
1813 "STTNUM0", "STTNUM1", "STTNUM2", "STTNUM3", "STTNUM4",
1814 "STTNUM5", "STTNUM6", "STTNUM7", "STTNUM8", "STTNUM9",
1815 "STTMINUS", "STTPRCNT",
1816 "FISTA0", "CSAWA0", "PISTA0", "SHOTA0", "SGN2A0", "MGUNA0", "LAUNA0",
1817 "PLASA0", "BFUGA0", "GUN2A0"
1818 };
1819 stone = R_gl_loadimage("STONE");
1820 stone2 = R_gl_loadimage("STONE2");
1821 keys[0] = R_gl_loadimage("KEYRA0");
1822 keys[1] = R_gl_loadimage("KEYGA0");
1823 keys[2] = R_gl_loadimage("KEYBA0");
1824 for (i = 0; i < 22; i++) {
1825 sth[i] = R_gl_loadimage(mnm[i]);
1827 strcpy(s, "STBF_*");
1828 for (i = '!'; i < 160; i++) {
1829 s[5] = i;
1830 bfh[i - '!'] = R_gl_getimage(F_findres(s));
1832 for (i = '!'; i < 160; i++) {
1833 sprintf(s, "STCFN%03d", i);
1834 sfh[i - '!'] = R_gl_getimage(F_findres(s));
1836 strcpy(s, "WINUM*");
1837 for (i = '0'; i <= '9'; i++) {
1838 s[5] = i;
1839 bfh[i - '!'] = R_gl_loadimage(s);
1841 bfh[':' - '!'] = R_gl_loadimage("WICOLON");
1842 // menu
1843 msklh[0] = R_gl_loadimage("M_SKULL1");
1844 msklh[1] = R_gl_loadimage("M_SKULL2");
1845 mbarl = R_gl_loadimage("M_THERML");
1846 mbarm = R_gl_loadimage("M_THERMM");
1847 mbarr = R_gl_loadimage("M_THERMR");
1848 mbaro = R_gl_loadimage("M_THERMO");
1849 mslotl = R_gl_loadimage("M_LSLEFT");
1850 mslotm = R_gl_loadimage("M_LSCNTR");
1851 mslotr = R_gl_loadimage("M_LSRGHT");
1852 // walls
1853 for (i = 1; i < ANIT; i++) {
1854 for (j = 0; j < 5 && anm[i - 1][j]; j++) {
1855 anip[i][j] = R_gl_loadimage(anm[i - 1][j]);
1857 for(; j < 5; j++) {
1858 anip[i][j] = (image) {
1859 .n = NULL,
1860 .w = 8,
1861 .h = 8,
1862 .res = -1,
1863 };
1868 static void R_reload_textures (void);
1870 void R_set_videomode (int w, int h, int fullscreen) {
1871 assert(w > 0);
1872 assert(h > 0);
1873 int was = Y_videomode_setted();
1874 if (root != NULL) {
1875 R_cache_free(root, 0);
1876 root = NULL;
1878 int res = Y_set_videomode_opengl(w, h, fullscreen);
1879 if (res == 0) {
1880 if (was == 0) {
1881 ERR_failinit("Unable to set video mode\n");
1884 Y_get_videomode(&screen_width, &screen_height);
1885 screen_full = Y_get_fullscreen();
1886 screen_scale = max(1, screen_width / 320);
1887 root = R_cache_new();
1888 assert(root);
1889 R_alloc();
1890 R_reload_textures();
1893 static int video_menu_handler (menu_msg_t *msg, const menu_t *m, int i) {
1894 static int cur;
1895 static int w, h, fullscreen;
1896 static char buf[16];
1897 static int buflen;
1898 static int vmode;
1899 const videomode_t *v;
1900 enum { VIDEOMODE, FULLSCREEN, APPLY, __NUM__ };
1901 static const simple_menu_t sm = {
1902 GM_BIG, "Video", NULL,
1904 { "Mode: ", NULL },
1905 { "Fullscreen: ", NULL },
1906 { "Apply ", NULL },
1908 };
1909 if (msg->type == GM_ENTER) {
1910 Y_get_videomode(&w, &h);
1911 fullscreen = Y_get_fullscreen();
1912 v = Y_get_videomode_list_opengl(fullscreen);
1913 vmode = 0;
1914 while (vmode < v->n && v->modes[vmode].w != w && v->modes[vmode].h != h) {
1915 vmode += 1;
1917 if (vmode < v->n) {
1918 w = v->modes[vmode].w;
1919 h = v->modes[vmode].h;
1921 snprintf(buf, 16, "%ix%i", w, h);
1922 buflen = strlen(buf);
1923 return 1;
1925 if (i == VIDEOMODE) {
1926 switch (msg->type) {
1927 case GM_GETSTR: return GM_init_str(msg, buf, buflen);
1928 case GM_SELECT:
1929 v = Y_get_videomode_list_opengl(fullscreen);
1930 vmode = vmode + 1 >= v->n ? 0 : vmode + 1;
1931 if (v->n > 0) {
1932 w = v->modes[vmode].w;
1933 h = v->modes[vmode].h;
1934 } else {
1935 Y_get_videomode(&w, &h);
1937 snprintf(buf, 16, "%ix%i", w, h);
1938 buflen = strlen(buf);
1939 return 1;
1941 } else if (i == FULLSCREEN) {
1942 switch (msg->type) {
1943 case GM_GETSTR: return GM_init_str(msg, fullscreen ? "Yes" : "No ", 3);
1944 case GM_SELECT: fullscreen = !fullscreen; return 1;
1946 } else if (i == APPLY) {
1947 switch (msg->type) {
1948 case GM_SELECT: R_set_videomode(w, h, fullscreen); return 1;
1951 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
1954 const menu_t *R_menu (void) {
1955 static const menu_t m = { video_menu_handler };
1956 return &m;
1959 const cfg_t *R_args (void) {
1960 static const cfg_t args[] = {
1961 { "fullscr", &init_screen_full, Y_SW_ON },
1962 { "window", &init_screen_full, Y_SW_OFF },
1963 { "width", &init_screen_width, Y_DWORD },
1964 { "height", &init_screen_height, Y_DWORD },
1965 { NULL, NULL, 0 } // end
1966 };
1967 return args;
1970 const cfg_t *R_conf (void) {
1971 static const cfg_t conf[] = {
1972 { "sky", &w_horiz, Y_SW_ON },
1973 { "fullscreen", &screen_full, Y_SW_ON },
1974 { "screen_width", &screen_width, Y_DWORD },
1975 { "screen_height", &screen_height, Y_DWORD },
1976 { NULL, NULL, 0 } // end
1977 };
1978 return conf;
1981 void R_init (void) {
1982 logo("R_init: intialize opengl render\n");
1983 R_init_playpal();
1984 init_screen_width = init_screen_width > 0 ? init_screen_width : screen_width;
1985 init_screen_height = init_screen_height > 0 ? init_screen_height : screen_height;
1986 init_screen_full = init_screen_full != 0xFF ? init_screen_full : screen_full;
1987 R_set_videomode(init_screen_width, init_screen_height, init_screen_full);
1990 void R_done (void) {
1991 R_cache_free(root, 1);
1992 Y_unset_videomode();
1993 root = NULL;
1996 void R_get_name (int n, char s[8]) {
1997 assert(n >= 0 && n < 256);
1998 if (walp[n].res == -1) {
1999 memset(s, 0, 8);
2000 } else if (walp[n].res == -2) {
2001 memcpy(s, "_WATER_", 8);
2002 s[7] = '0' + (intptr_t)walp[n].n - 1;
2003 } else if (walani[n] > 0) {
2004 memcpy(s, anm[walani[n] - 1][0], 8);
2005 } else {
2006 F_getresname(s, walp[n].res & 0x7FFF);
2010 static short getani (char n[8]) {
2011 short i = 0;
2012 while (i < ANIT - 1 && cp866_strncasecmp(n, anm[i][0], 8) != 0) {
2013 i++;
2015 return i < ANIT - 1 ? i + 1 : 0;
2018 int R_get_special_id (int n) {
2019 assert(n >= 0 && n <= 256);
2020 return walp[n].res == -2 ? (intptr_t)walp[n].n : -1;
2023 static void R_reload_textures (void) {
2024 int i;
2025 char s[8];
2026 for (i = 0; i < max_textures; i++) {
2027 R_get_name(i, s);
2028 if (walp[i].res >= 0) {
2029 walp[i] = R_gl_getimage(walp[i].res);
2032 if (horiz.n) {
2033 horiz = R_gl_getimage(horiz.res);
2037 void R_begin_load (void) {
2038 int i;
2039 for (i = 0; i < 256; i++) {
2040 if (walp[i].n != NULL && walp[i].res >= 0 && walani[i] == 0) {
2041 R_gl_free_image(&walp[i]);
2043 memset(&walp[i], 0, sizeof(image));
2044 walp[i].res = -1;
2045 walswp[i] = i;
2046 walani[i] = 0;
2048 memset(anic, 0, sizeof(anic));
2049 max_wall_width = 0;
2050 max_wall_height = 0;
2051 max_textures = 1;
2054 void R_load (char s[8]) {
2055 assert(max_textures < 256);
2056 if (!s[0]) {
2057 walp[max_textures] = (image) {
2058 .n = NULL,
2059 .x = 0,
2060 .y = 0,
2061 .w = 0,
2062 .h = 0,
2063 .res = -1,
2064 };
2065 } else if (cp866_strncasecmp(s, "_WATER_", 7) == 0) {
2066 walp[max_textures] = (image) {
2067 .n = (void*)((intptr_t)s[7] - '0' + 1),
2068 .x = 0,
2069 .y = 0,
2070 .w = 8,
2071 .h = 8,
2072 .res = -2,
2073 };
2074 } else {
2075 walp[max_textures] = R_gl_loadimage(s);
2076 if (s[0] == 'S' && s[1] == 'W' && s[4] == '_') {
2077 walswp[max_textures] = 0;
2079 walani[max_textures] = getani(s);
2081 max_wall_width = max(max_wall_width, walp[max_textures].w);
2082 max_wall_height = max(max_wall_height, walp[max_textures].h);
2083 max_textures++;
2086 void R_end_load (void) {
2087 int i, j, k, g;
2088 char s[8];
2089 j = max_textures;
2090 for (i = 1; i < 256 && j < 256; i++) {
2091 if (walswp[i] == 0) {
2092 R_get_name(i, s);
2093 s[5] ^= 1;
2094 g = F_getresid(s);
2095 k = 1;
2096 while (k < 256 && walp[k].res != g) {
2097 k += 1;
2099 if (k >= 256) {
2100 k = j;
2101 j += 1;
2102 max_textures += 1;
2103 walp[k] = R_gl_getimage(g);
2104 walf[k] = walf[i];
2106 walswp[i] = k;
2107 walswp[k] = i;
2112 void R_loadsky (int sky) {
2113 char s[6];
2114 strcpy(s, "RSKYx");
2115 s[4] = '0' + sky;
2116 R_gl_free_image(&horiz);
2117 horiz = R_gl_loadimage(s);
2120 void R_switch_texture (int x, int y) {
2121 assert(x >= 0 && x < FLDW);
2122 assert(y >= 0 && y < FLDH);
2123 fldb[y][x] = walswp[fldb[y][x]];
2126 int R_get_swp (int n) {
2127 assert(n >= 0 && n < 256);
2128 return walswp[n];