DEADSOFTWARE

22bbcd99fa87e6f298419a021677baf85b43b349
[flatwaifu.git] / src / gl / render.c
1 #include "glob.h"
2 #include "render.h"
3 #include "system.h"
4 #include "files.h"
5 #include "memory.h"
6 #include "misc.h"
7 #include "error.h"
9 #include "menu.h"
10 #include "game.h"
11 #include "dots.h"
12 #include "items.h"
14 #include "sound.h" // snd_vol
15 #include "music.h" // mus_vol
17 #include "fx.h"
18 #include "player.h"
19 #include "monster.h"
20 #include "weapons.h"
21 #include "smoke.h"
22 #include "view.h"
23 #include "switch.h" // sw_secrets
25 #include "cp866.h"
27 #ifdef __APPLE__
28 # include <OpenGL/gl.h>
29 #else
30 # include <GL/gl.h>
31 #endif
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <assert.h>
37 #define VGA_TRANSPARENT_COLOR 0
38 #define DEFAULT_SKY_COLOR 0x97
39 #define MANCOLOR 0xD0
40 #define PLAYER_COLOR_OFFSET 7
41 #define MAXAIR 1091
42 #define ANIT 5
43 #define PL_FLASH 90
45 #pragma pack(1)
46 typedef struct vgaimg {
47 word w, h;
48 short x, y;
49 byte data[];
50 } vgaimg;
52 typedef struct rgb {
53 byte r, g, b;
54 } rgb;
56 typedef struct rgba {
57 byte r, g, b, a;
58 } rgba;
59 #pragma pack()
61 typedef struct node {
62 struct cache *base;
63 struct node *left, *right;
64 struct node *up;
65 int l, t, r, b;
66 int leaf;
67 } node;
69 typedef struct cache {
70 struct cache *next;
71 struct node root;
72 GLuint id;
73 } cache;
75 typedef struct image {
76 node *n;
77 GLint x, y;
78 GLuint w, h;
79 int res;
80 } image;
82 /* Render Specific */
83 int SCRW = 320; // public
84 int SCRH = 200; // public
85 static rgb playpal[256];
86 static byte bright[256];
87 static GLuint lastTexture;
88 static cache *root;
90 /* Game */
91 static image scrnh[3]; // TITLEPIC INTERPIC ENDPIC
92 static image ltn[2][2];
94 /* Smoke */
95 static image smk_spr[SMSN];
96 static image smk_fspr[FLSN];
98 /* Effects */
99 static image fx_spr[15];
100 static char fx_sprd[15];
102 /* Weapons */
103 static image wp_spr[49*2];
104 static char wp_sprd[49*2];
106 /* Items */
107 static image item_spr[58];
108 static char item_sprd[58];
110 /* Player */
111 static image plr_spr[27*2];
112 static image plr_msk[27*2];
113 static char plr_sprd[27*2];
114 static image plr_wpn[11][6];
116 /* Monsters */
117 static image pl_spr[2];
118 static image pl_msk[2];
119 static image mn_spr[MN_TN][29*2];
120 static image mn_man_msk[29*2];
121 static char mn_sprd[MN_TN][29*2];
122 static image mn_fspr[8];
123 static image mn_sgun[2];
125 /* Misc */
126 static image sth[22];
127 static image bfh[160 - '!'];
128 static image sfh[160 - '!'];
129 static image stone;
130 static image stone2;
131 static image keys[3];
132 static int prx = 0;
133 static int pry = 0;
135 /* Menu */
136 static int gm_tm;
137 static image msklh[2];
138 static image mbarl;
139 static image mbarm;
140 static image mbarr;
141 static image mbaro;
142 static image mslotl;
143 static image mslotm;
144 static image mslotr;
146 /* Map */
147 static const char *anm[ANIT - 1][5] = {
148 {"WALL22_1", "WALL23_1", "WALL23_2", NULL, NULL},
149 {"WALL58_1", "WALL58_2", "WALL58_3", NULL, NULL},
150 {"W73A_1", "W73A_2", NULL, NULL, NULL},
151 {"RP2_1", "RP2_2", "RP2_3", "RP2_4", NULL}
152 };
153 static int max_wall_width;
154 static int max_wall_height;
155 static int max_textures;
156 static image walp[256];
157 static byte walani[256];
158 static image anip[ANIT][5];
159 static byte anic[ANIT];
160 static image horiz;
162 /* Texture cache */
164 // https://blackpawn.com/texts/lightmaps/
165 static node *R_node_alloc (node *p, int w, int h) {
166 assert(p);
167 assert(w > 0);
168 assert(h > 0);
169 if (p->left) {
170 assert(p->right);
171 node *n = R_node_alloc(p->left, w, h);
172 return n ? n : R_node_alloc(p->right, w, h);
173 } else {
174 int pw = p->r - p->l + 1;
175 int ph = p->b - p->t + 1;
176 if (p->leaf || pw < w || ph < h) {
177 return NULL;
178 } else if (pw == w && ph == h) {
179 p->leaf = 1;
180 return p;
181 } else {
182 p->left = malloc(sizeof(node));
183 p->right = malloc(sizeof(node));
184 if (pw - w > ph - h) {
185 *p->left = (node) {
186 .up = p,
187 .l = p->l,
188 .t = p->t,
189 .r = p->l + w - 1,
190 .b = p->b
191 };
192 *p->right = (node) {
193 .up = p,
194 .l = p->l + w,
195 .t = p->t,
196 .r = p->r,
197 .b = p->b
198 };
199 } else {
200 *p->left = (node) {
201 .up = p,
202 .l = p->l,
203 .t = p->t,
204 .r = p->r,
205 .b = p->t + h - 1
206 };
207 *p->right = (node) {
208 .up = p,
209 .l = p->l,
210 .t = p->t + h,
211 .r = p->r,
212 .b = p->b
213 };
215 return R_node_alloc(p->left, w, h);
220 static int R_node_have_leaf (node *n) {
221 return n && (n->leaf || R_node_have_leaf(n->left) || R_node_have_leaf(n->right));
224 static void R_node_free_recursive (node *n) {
225 if (n) {
226 R_node_free_recursive(n->left);
227 R_node_free_recursive(n->right);
228 free(n);
232 static void R_node_free (node *n) {
233 if (n) {
234 //logo("free node %p {%i:%i:%i:%i}\n", n, n->l, n->t, n->r, n->b);
235 assert(n->leaf);
236 assert(n->left == NULL);
237 assert(n->right == NULL);
238 n->leaf = 0;
239 n->base = NULL;
240 node *p = n->up;
241 while (p != NULL) {
242 assert(p->leaf == 0);
243 assert(p->left);
244 assert(p->right);
245 if (R_node_have_leaf(p) == 0) {
246 R_node_free_recursive(p->left);
247 p->left = NULL;
248 R_node_free_recursive(p->right);
249 p->right = NULL;
250 p = p->up;
251 } else {
252 p = NULL;
258 static void R_cache_get_max_texture_size (int *w, int *h) {
259 GLint size = 0;
260 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
261 size = min(max(size, 0), 512); // more can be buggy on older hardware
262 *w = size;
263 *h = size;
266 static void R_gl_bind_texture (GLuint id) {
267 if (id != lastTexture) {
268 glBindTexture(GL_TEXTURE_2D, id);
272 static cache *R_cache_new (void) {
273 int w, h;
274 GLuint id;
275 cache *c = NULL;
276 R_cache_get_max_texture_size(&w, &h);
277 if (w && h) {
278 glGenTextures(1, &id);
279 if (id) {
280 R_gl_bind_texture(id);
281 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
282 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
283 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
284 c = malloc(sizeof(cache));
285 if (c != NULL) {
286 *c = (cache) {
287 .id = id,
288 .root.r = w - 1,
289 .root.b = h - 1
290 };
291 } else {
292 glDeleteTextures(1, &id);
296 //logo("new cache %p\n", c);
297 return c;
300 static void R_cache_free (cache *root, int freetexture) {
301 cache *next;
302 cache *c = root;
303 while (c != NULL) {
304 next = c->next;
305 R_node_free_recursive(c->root.left);
306 R_node_free_recursive(c->root.right);
307 if (freetexture && c->id != 0) {
308 glDeleteTextures(1, &c->id);
310 free(c);
311 c = next;
315 static node *R_cache_alloc (cache *root, int w, int h) {
316 assert(root);
317 assert(w > 0 && h > 0);
318 node *n = NULL;
319 cache *p = NULL;
320 cache *c = root;
321 int maxw, maxh;
322 R_cache_get_max_texture_size(&maxw, &maxh);
323 if (w <= maxw && h <= maxh) {
324 while (c && !n) {
325 n = R_node_alloc(&c->root, w, h);
326 if (n) {
327 assert(n->leaf);
328 n->base = c;
330 p = c;
331 c = c->next;
333 if (!n) {
334 c = R_cache_new();
335 if (c) {
336 p->next = c;
337 n = R_node_alloc(&c->root, w, h);
338 if (n) {
339 assert(n->leaf);
340 n->base = c;
345 if (n) {
346 //logo("new node %p {%i:%i:%i:%i}\n", n, n->l, n->t, n->r, n->b);
347 } else {
348 logo("new node failed {%i:%i}\n", w, h);
350 return n;
353 static void R_cache_update (node *n, const void *data, int x, int y, int w, int h) {
354 assert(n);
355 assert(n->leaf);
356 assert(n->base);
357 assert(data);
358 assert(x >= 0);
359 assert(y >= 0);
360 assert(n->l + x + w - 1 <= n->r);
361 assert(n->t + y + h - 1 <= n->b);
362 R_gl_bind_texture(n->base->id);
363 glTexSubImage2D(GL_TEXTURE_2D, 0, n->l + x, n->t + y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
366 /* Generic helpers */
368 static void R_init_playpal (void) {
369 int i;
370 byte *vgapal = M_lock(F_getresid("PLAYPAL"));
371 for (i = 0; i < 256; i++) {
372 playpal[i] = (rgb) {
373 .r = vgapal[i * 3 + 0] * 255 / 63,
374 .g = vgapal[i * 3 + 1] * 255 / 63,
375 .b = vgapal[i * 3 + 2] * 255 / 63,
376 };
377 bright[i] = ((int)vgapal[i * 3 + 0] + vgapal[i * 3 + 1] + vgapal[i * 3 + 2]) * 8 / (63 * 3);
379 M_unlock(vgapal);
382 static vgaimg *R_getvga (int id) {
383 int loaded = M_was_locked(id);
384 vgaimg *v = M_lock(id);
385 if (v != NULL && !loaded) {
386 v->w = short2host(v->w);
387 v->h = short2host(v->h);
388 v->x = short2host(v->x);
389 v->y = short2host(v->y);
391 return v;
394 static rgba *R_extract_flame_spr (vgaimg *v) {
395 static const byte flametab[16] = {
396 0xBC, 0xBA, 0xB8, 0xB6, 0xB4, 0xB2, 0xB0, 0xD5,
397 0xD6, 0xD7, 0xA1, 0xA0, 0xE3, 0xE2, 0xE1, 0xE0
398 };
399 int i, j;
400 rgba *s = malloc(v->w * v->h * sizeof(rgba));
401 if (s != NULL) {
402 for (j = 0; j < v->h; j++) {
403 for (i = 0; i < v->w; i++) {
404 int k = j * v->w + i;
405 byte c = v->data[k] + bright[DEFAULT_SKY_COLOR];
406 s[k] = (rgba) {
407 .r = playpal[flametab[c]].r,
408 .g = playpal[flametab[c]].g,
409 .b = playpal[flametab[c]].b,
410 .a = v->data[k] == VGA_TRANSPARENT_COLOR ? 0x00 : 0xFF,
411 };
415 return s;
418 static rgba *R_extract_smoke_spr (vgaimg *v) {
419 int i, j;
420 rgba *s = malloc(v->w * v->h * sizeof(rgba));
421 if (s != NULL) {
422 for (j = 0; j < v->h; j++) {
423 for (i = 0; i < v->w; i++) {
424 int k = j * v->w + i;
425 byte c = ((v->data[k] + bright[DEFAULT_SKY_COLOR]) + 0x60) ^ 0x0F;
426 byte a = 0xFF - ((int)playpal[c].r + playpal[c].g + playpal[c].b) / 3;
427 s[k] = (rgba) {
428 .r = playpal[c].r,
429 .g = playpal[c].g,
430 .b = playpal[c].b,
431 .a = v->data[k] == VGA_TRANSPARENT_COLOR ? 0x00 : a,
432 };
436 return s;
439 static rgba *R_extract_mask_spr (vgaimg *v) {
440 int i, j;
441 rgba *s = malloc(v->w * v->h * sizeof(rgba));
442 if (s != NULL) {
443 for (j = 0; j < v->h; j++) {
444 for (i = 0; i < v->w; i++) {
445 int k = j * v->w + i;
446 byte c = v->data[k];
447 if (c >= 0x70 && c <= 0x7F) {
448 byte mask = c - 0x70;
449 mask = 0xFF - ((mask << 4) | mask);
450 s[k] = (rgba) {
451 .r = mask,
452 .g = mask,
453 .b = mask,
454 .a = 0xFF,
455 };
456 } else {
457 s[k] = (rgba) {
458 .r = 0,
459 .g = 0,
460 .b = 0,
461 .a = 0,
462 };
467 return s;
470 static rgba *R_extract_rgba_spr (vgaimg *v) {
471 int i, j;
472 rgba *s = malloc(v->w * v->h * sizeof(rgba));
473 if (s != NULL) {
474 for (j = 0; j < v->h; j++) {
475 for (i = 0; i < v->w; i++) {
476 int k = j * v->w + i;
477 byte c = v->data[k];
478 s[k] = (rgba) {
479 .r = playpal[c].r,
480 .g = playpal[c].g,
481 .b = playpal[c].b,
482 .a = c == VGA_TRANSPARENT_COLOR ? 0x00 : 0xFF,
483 };
487 return s;
490 /* OpenGL helpers */
492 static image R_gl_create_image (const rgba *buf, int w, int h) {
493 node *n = R_cache_alloc(root, w, h);
494 if (n) {
495 R_cache_update(n, buf, 0, 0, w, h);
497 return (image) {
498 .n = n,
499 .w = w,
500 .h = h,
501 .res = -1
502 };
505 static image R_gl_get_special_image (int id, rgba *(*fn)(vgaimg*)) {
506 image img;
507 //char name[8];
508 //F_getresname(name, id);
509 //logo("load image: %.8s\n", name);
510 vgaimg *v = R_getvga(id);
511 if (v != NULL) {
512 rgba *buf = (*fn)(v);
513 img = R_gl_create_image(buf, v->w, v->h);
514 img.x = v->x;
515 img.y = v->y;
516 img.res = id;
517 M_unlock(v);
518 free(buf);
519 } else {
520 img = (image) {
521 .res = id
522 };
524 return img;
527 static image R_gl_getimage (int id) {
528 return R_gl_get_special_image(id, &R_extract_rgba_spr);
531 static image R_gl_loadimage (const char name[8]) {
532 return R_gl_getimage(F_getresid(name));
535 static image R_gl_get_special_spr (const char n[4], int s, int d, rgba *(*fn)(vgaimg*)) {
536 return R_gl_get_special_image(F_getsprid(n, s, d), fn);
539 static void R_gl_free_image (image *img) {
540 if (img->n != NULL && img->res >= 0) {
541 R_node_free(img->n);
543 img->n = NULL;
544 img->res = -1;
547 static void R_gl_draw_quad (int x, int y, int w, int h) {
548 glBegin(GL_QUADS);
549 glVertex2i(x + w, y);
550 glVertex2i(x, y);
551 glVertex2i(x, y + h);
552 glVertex2i(x + w, y + h);
553 glEnd();
556 static void R_gl_draw_textured (image *img, int x, int y, int w, int h, int flip) {
557 if (img->n) {
558 GLfloat nw = img->n->base->root.r + 1;
559 GLfloat nh = img->n->base->root.b + 1;
560 GLfloat ax = (flip ? img->n->l : img->n->r + 1) / nw;
561 GLfloat bx = (flip ? img->n->r + 1 : img->n->l) / nh;
562 GLfloat ay = (img->n->t) / nw;
563 GLfloat by = (img->n->b + 1) / nh;
564 R_gl_bind_texture(img->n->base->id);
565 glEnable(GL_TEXTURE_2D);
566 glBegin(GL_QUADS);
567 glTexCoord2f(ax, ay); glVertex2i(x + w, y);
568 glTexCoord2f(bx, ay); glVertex2i(x, y);
569 glTexCoord2f(bx, by); glVertex2i(x, y + h);
570 glTexCoord2f(ax, by); glVertex2i(x + w, y + h);
571 glEnd();
572 } else {
573 glColor3ub(255, 0, 0);
574 glDisable(GL_BLEND);
575 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
576 glDisable(GL_TEXTURE_2D);
577 R_gl_draw_quad(x, y, w, h);
581 /* fit image into rectangle without applying offset and transparency */
582 static void R_gl_draw_image_ext (image *img, int x, int y, int w, int h) {
583 glDisable(GL_BLEND);
584 glColor3ub(255, 255, 255);
585 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
586 R_gl_draw_textured(img, x, y, w, h, 0);
589 /* draw sprite with offset and coloring */
590 static void R_gl_draw_image_color (image *img, int x, int y, int flip) {
591 int xx = flip ? x - img->w + img->x : x - img->x;
592 glEnable(GL_BLEND);
593 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
594 R_gl_draw_textured(img, xx, y - img->y, img->w, img->h, flip);
597 /* draw sprite with offset */
598 static void R_gl_draw_image (image *img, int x, int y, int flip) {
599 glColor3ub(255, 255, 255);
600 R_gl_draw_image_color(img, x, y, flip);
603 static void R_gl_set_color (byte c) {
604 glColor3ub(playpal[c].r, playpal[c].g, playpal[c].b);
607 static void R_gl_setclip (int x, int y, int w, int h) {
608 glScissor(x, SCRH - h - y, w, h);
611 static void R_gl_setmatrix (void) {
612 glScissor(0, 0, SCRW, SCRH);
613 glViewport(0, 0, SCRW, SCRH);
614 glMatrixMode(GL_PROJECTION);
615 glLoadIdentity();
616 glOrtho(0, SCRW, SCRH, 0, 0, 1);
617 glMatrixMode(GL_MODELVIEW);
618 glLoadIdentity();
621 /* --- Misc --- */
623 static image Z_getspr (const char n[4], int s, int d, char *dir) {
624 int h = F_getsprid(n, s, d);
625 if (dir != NULL) {
626 *dir = (h & 0x8000) ? 1 : 0;
628 return R_gl_getimage(h);
631 static image *Z_get_char_image (image *img, int ch) {
632 image *p = NULL;
633 ch = cp866_toupper(ch);
634 if (ch > 32 && ch < 160) {
635 p = &img[ch - '!'];
637 return p;
640 static int Z_get_char_width_generic (image *img, int off, int ch) {
641 image *p = Z_get_char_image(img, ch);
642 return p == NULL ? off : p->w - 1;
645 static int Z_putch_generic (image *img, int off, int ch) {
646 image *p = Z_get_char_image(img, ch);
647 int w = p == NULL ? off : p->w - 1;
648 if (p != NULL && p->n != NULL) {
649 R_gl_draw_image(p, prx, pry, 0);
651 prx += w;
652 return w;
655 static int Z_get_string_width_generic (image *img, int off, const char *fmt, va_list ap) {
656 int i, w, ww;
657 char buf[80];
658 vsprintf(buf, fmt, ap);
659 for (i = w = ww = 0; buf[i]; ++i) {
660 switch (buf[i]) {
661 case '\n':
662 case '\r':
663 ww = max(w, ww);
664 w = 0;
665 break;
666 default:
667 w += Z_get_char_width_generic(img, off, (byte)buf[i]);
670 return max(w, ww);
673 static int Z_printf_generic (image *img, int off, const char *fmt, va_list ap) {
674 int i, w, ww;
675 char buf[80];
676 vsprintf(buf, fmt, ap);
677 for (i = w = ww = 0; buf[i]; ++i) {
678 switch (buf[i]) {
679 case '\n':
680 pry += off + 1;
681 case '\r':
682 w = max(w, ww);
683 prx = 0;
684 w = 0;
685 break;
686 default:
687 w += Z_putch_generic(img, off, (byte)buf[i]);
690 return w;
693 static void Z_gotoxy (int x, int y) {
694 prx = x;
695 pry = y;
698 static int Z_get_big_string_width (const char *fmt, ...) {
699 va_list a;
700 va_start(a, fmt);
701 int w = Z_get_string_width_generic(bfh, 12, fmt, a);
702 va_end(a);
703 return w;
706 static int Z_printbf (const char *fmt, ...) {
707 va_list a;
708 va_start(a, fmt);
709 int w = Z_printf_generic(bfh, 12, fmt, a);
710 va_end(a);
711 return w;
714 static int Z_get_small_string_width (const char *fmt, ...) {
715 va_list a;
716 va_start(a, fmt);
717 int w = Z_get_string_width_generic(sfh, 7, fmt, a);
718 va_end(a);
719 return w;
722 static int Z_printsf (const char *fmt, ...) {
723 va_list a;
724 va_start(a, fmt);
725 int w =Z_printf_generic(sfh, 7, fmt, a);
726 va_end(a);
727 return w;
730 static void Z_printhf (const char *fmt, ...) {
731 int i, c;
732 char buf[80];
733 va_list a;
734 va_start(a, fmt);
735 vsprintf(buf, fmt, a);
736 va_end(a);
737 for (i = 0; buf[i]; ++i) {
738 switch (buf[i]) {
739 case '0':
740 case '1':
741 case '2':
742 case '3':
743 case '4':
744 case '5':
745 case '6':
746 case '7':
747 case '8':
748 case '9':
749 c = buf[i] - '0';
750 break;
751 case '-':
752 c = 10;
753 break;
754 case '%':
755 c = 11;
756 break;
757 case '\n':
758 pry += 19;
759 case '\r':
760 c = -1;
761 prx = 0;
762 break;
763 default:
764 c = -1;
765 break;
767 if (c >= 0) {
768 R_gl_draw_image(&sth[c], prx, pry, 0);
770 prx += 14;
774 /* --- Menu --- */
776 static image *PL_getspr (int s, int d, int msk) {
777 int i = (s - 'A') * 2 + d;
778 return msk ? &plr_msk[i] : &plr_spr[i];
781 #define SCROLLER_MIDDLE 10
782 #define TEXTFIELD_MIDDLE 2
784 static void get_entry_size (const menu_t *m, int i, int *w, int *h) {
785 assert(m != NULL);
786 assert(i >= 0);
787 assert(w != NULL);
788 assert(h != NULL);
789 int x = 0;
790 int y = 0;
791 int type = 0;
792 menu_msg_t msg;
793 msg.type = GM_GETENTRY;
794 assert(GM_send(m, i, &msg));
795 type = msg.integer.i;
796 switch (type) {
797 case GM_BUTTON:
798 case GM_SCROLLER:
799 case GM_TEXTFIELD:
800 case GM_TEXTFIELD_BUTTON:
801 msg.type = GM_GETCAPTION;
802 if (GM_send(m, i, &msg)) {
803 x = Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
805 break;
806 case GM_SMALL_BUTTON:
807 msg.type = GM_GETCAPTION;
808 if (GM_send(m, i, &msg)) {
809 x = Z_get_small_string_width("%.*s", msg.string.maxlen, msg.string.s);
811 break;
812 default:
813 assert(0);
815 switch (type) {
816 case GM_BUTTON:
817 msg.type = GM_GETSTR;
818 if (GM_send(m, i, &msg)) {
819 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
821 y = 16;
822 break;
823 case GM_SMALL_BUTTON:
824 msg.type = GM_GETSTR;
825 if (GM_send(m, i, &msg)) {
826 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
828 y = 12;
829 break;
830 case GM_SCROLLER:
831 x += (SCROLLER_MIDDLE + 2) * 8;
832 y = 16;
833 break;
834 case GM_TEXTFIELD:
835 case GM_TEXTFIELD_BUTTON:
836 msg.type = GM_GETSTR;
837 if (GM_send(m, i, &msg)) {
838 x += (msg.string.maxlen + 2) * 8;
839 } else {
840 x += (TEXTFIELD_MIDDLE + 2) * 8;
842 y = 16;
843 break;
844 default:
845 assert(0);
847 *w = x;
848 *h = y;
851 static void get_menu_size (const menu_t *m, int *w, int *h) {
852 assert(m != NULL);
853 assert(w != NULL);
854 assert(h != NULL);
855 int i, n, x, y, xx, yy, type;
856 menu_msg_t msg;
857 msg.type = GM_QUERY;
858 if (GM_send_this(m, &msg)) {
859 n = msg.integer.b;
860 type = msg.integer.s;
861 x = 0;
862 y = 0;
863 msg.type = GM_GETTITLE;
864 if (GM_send_this(m, &msg)) {
865 switch (type) {
866 case GM_BIG: x = Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); break;
867 case GM_SMALL: x = Z_get_small_string_width("%.*s", msg.string.maxlen, msg.string.s); break;
868 default: assert(0);
871 for (i = 0; i < n; i++) {
872 get_entry_size(m, i, &xx, &yy);
873 x = max(x, xx);
874 y += yy;
876 *w = x;
877 *h = y;
878 } else {
879 *w = 0;
880 *h = 0;
884 static int GM_draw (void) {
885 int i, j, n, x, y, xoff, yoff, cur, w, type, recv;
886 const menu_t *m = GM_get();
887 menu_msg_t msg;
888 if (m != NULL) {
889 get_menu_size(m, &x, &y);
890 x = SCRW / 2 - x / 2;
891 y = SCRH / 2 - y / 2;
892 // --- title ---
893 msg.type = GM_QUERY;
894 if (GM_send_this(m, &msg)) {
895 cur = msg.integer.i;
896 n = msg.integer.a;
897 type = msg.integer.s;
898 msg.type = GM_GETTITLE;
899 if (GM_send_this(m, &msg)) {
900 Z_gotoxy(x, y - 10);
901 switch (type) {
902 case GM_SMALL: yoff = 8; Z_printsf("%.*s", msg.string.maxlen, msg.string.s); break;
903 case GM_BIG: yoff = 20; Z_printbf("%.*s", msg.string.maxlen, msg.string.s); break;
904 default: assert(0);
906 } else {
907 yoff = 0;
909 for (i = 0; i < n; i++) {
910 msg.type = GM_GETENTRY;
911 if (GM_send(m, i, &msg)) {
912 type = msg.integer.i;
913 if (i == cur) {
914 if (type == GM_SMALL_BUTTON) {
915 Z_gotoxy(x - 8, y + yoff);
916 Z_printsf(">");
917 } else {
918 R_gl_draw_image(&msklh[(gm_tm / 6) & 1], x - 25, y + yoff - 8, 0);
921 msg.type = GM_GETCAPTION;
922 if (GM_send(m, i, &msg)) {
923 Z_gotoxy(x, y + yoff);
924 if (type == GM_SMALL_BUTTON) {
925 xoff = Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
926 } else {
927 xoff = Z_printbf("%.*s", msg.string.maxlen, msg.string.s);
929 } else {
930 xoff = 0;
932 switch (type) {
933 case GM_BUTTON:
934 case GM_SMALL_BUTTON:
935 msg.type = GM_GETSTR;
936 if (GM_send(m, i, &msg)) {
937 Z_gotoxy(x + xoff, y + yoff);
938 if (type == GM_SMALL_BUTTON) {
939 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
940 } else {
941 Z_printbf("%.*s", msg.string.maxlen, msg.string.s);
944 yoff += type == GM_BUTTON ? 16 : 12;
945 break;
946 case GM_TEXTFIELD:
947 case GM_TEXTFIELD_BUTTON:
948 yoff += 9;
949 msg.type = GM_GETSTR;
950 recv = GM_send(m, i, &msg);
951 w = recv ? msg.string.maxlen : TEXTFIELD_MIDDLE;
952 R_gl_draw_image(&mslotl, x + xoff, y + yoff, 0);
953 for (j = 1; j <= w; j++) {
954 R_gl_draw_image(&mslotm, x + xoff + j * 8, y + yoff, 0);
956 R_gl_draw_image(&mslotr, x + xoff + j * 8, y + yoff, 0);
957 Z_gotoxy(x + xoff + 4, y + yoff - 7);
958 if (input && i == cur) {
959 Z_printsf("%.*s_", imax, ibuf);
960 } else if (recv) {
961 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
963 yoff += 7;
964 break;
965 case GM_SCROLLER:
966 R_gl_draw_image(&mbarl, x + xoff, y + yoff, 0);
967 for (j = 1; j < SCROLLER_MIDDLE; j++) {
968 R_gl_draw_image(&mbarm, x + xoff + j * 8, y + yoff, 0);
970 R_gl_draw_image(&mbarr, x + xoff + j * 8, y + yoff, 0);
971 msg.type = GM_GETINT;
972 if (GM_send(m, i, &msg)) {
973 int lev = (msg.integer.i - msg.integer.a) * ((SCROLLER_MIDDLE - 2) * 8) / msg.integer.b;
974 R_gl_draw_image(&mbaro, x + xoff + lev + 8, y + yoff, 0);
976 yoff += 16;
977 break;
978 default:
979 assert(0);
985 return m != NULL;
988 /* --- View --- */
990 static void R_draw_fld (byte *fld, int minx, int miny, int maxx, int maxy, int fg) {
991 int i, j;
992 assert(minx >= 0 && minx <= FLDW);
993 assert(miny >= 0 && miny <= FLDH);
994 assert(maxx >= 0 && maxx <= FLDW);
995 assert(maxy >= 0 && maxy <= FLDH);
996 for (j = miny; j < maxy; j++) {
997 for (i = minx; i < maxx; i++) {
998 byte id = fld[j * FLDW + i];
999 if (id != 0) {
1000 if (walp[id].res < 0) {
1001 if (fg) {
1002 switch (R_get_special_id(id)) {
1003 case 1:
1004 glColor4ub(0, 0, 255, 127);
1005 break;
1006 case 2:
1007 glColor4ub(0, 127, 0, 127);
1008 break;
1009 case 3:
1010 glColor4ub(127, 0, 0, 127);
1011 break;
1012 default:
1013 glColor4ub(0, 0, 0, 127);
1014 break;
1016 glEnable(GL_BLEND);
1017 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1018 glDisable(GL_TEXTURE_2D);
1019 R_gl_draw_quad(i * CELW, j * CELW, CELW, CELH);
1021 } else {
1022 R_gl_draw_image(&walp[id], i * CELW, j * CELH, 0);
1029 static void R_draw_dots (void) {
1030 int i;
1031 glDisable(GL_BLEND);
1032 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1033 glDisable(GL_TEXTURE_2D);
1034 glBegin(GL_POINTS);
1035 for (i = 0; i < MAXDOT; i++) {
1036 if (dot[i].t != 0) {
1037 R_gl_set_color(dot[i].c);
1038 glVertex2i(dot[i].o.x, dot[i].o.y + 1);
1041 glEnd();
1044 static void R_draw_items (void) {
1045 int i, s;
1046 for (i = 0; i < MAXITEM; ++i) {
1047 s = -1;
1048 if (it[i].t && it[i].s >= 0) {
1049 switch (it[i].t & 0x7FFF) {
1050 case I_ARM1:
1051 s = it[i].s / 9 + 18;
1052 break;
1053 case I_ARM2:
1054 s = it[i].s / 9 + 20;
1055 break;
1056 case I_MEGA:
1057 s = it[i].s / 2 + 22;
1058 break;
1059 case I_INVL:
1060 s = it[i].s / 2 + 26;
1061 break;
1062 case I_SUPER:
1063 case I_RTORCH:
1064 case I_GTORCH:
1065 case I_BTORCH:
1066 s = it[i].s / 2 + (it[i].t - I_SUPER) * 4 + 35;
1067 break;
1068 case I_GOR1: case I_FCAN:
1069 s = it[i].s / 2 + (it[i].t - I_GOR1) * 3 + 51;
1070 break;
1071 case I_AQUA:
1072 s = 30;
1073 break;
1074 case I_SUIT:
1075 s = 34;
1076 break;
1077 case I_KEYR:
1078 case I_KEYG:
1079 case I_KEYB:
1080 s = (it[i].t & 0x7FFF) - I_KEYR + 31;
1081 break;
1082 case I_GUN2:
1083 s = 57;
1084 break;
1085 default:
1086 s = (it[i].t & 0x7FFF) - 1;
1089 if (s >= 0) {
1090 R_gl_draw_image(&item_spr[s], it[i].o.x, it[i].o.y, item_sprd[s]);
1095 static int standspr (player_t *p) {
1096 if (p->f & PLF_UP) {
1097 return 'X';
1098 } else if (p->f & PLF_DOWN) {
1099 return 'Z';
1100 } else {
1101 return 'E';
1105 static int wpnspr (player_t *p) {
1106 if (p->f & PLF_UP) {
1107 return 'C';
1108 } else if(p->f & PLF_DOWN) {
1109 return 'E';
1110 } else {
1111 return 'A';
1115 static void R_draw_player (player_t *p) {
1116 enum {STAND, GO, DIE, SLOP, DEAD, MESS, OUT, FALL}; // copypasted from player.c!
1117 static const int wytab[] = {-1, -2, -1, 0};
1118 int s = 'A';
1119 int w = 0;
1120 int wx = 0;
1121 int wy = 0;
1122 switch (p->st) {
1123 case STAND:
1124 if (p->f & PLF_FIRE) {
1125 s = standspr(p) + 1;
1126 w = wpnspr(p) + 1;
1127 } else if (p->pain) {
1128 s = 'G';
1129 w = 'A';
1130 wx = p->d ? 2 : -2;
1131 wy = 1;
1132 } else {
1133 s = standspr(p);
1134 w = wpnspr(p);
1136 break;
1137 case DEAD:
1138 s = 'N';
1139 break;
1140 case MESS:
1141 s = 'W';
1142 break;
1143 case GO:
1144 if (p->pain) {
1145 s = 'G';
1146 w = 'A';
1147 wx = p->d ? 2 : -2;
1148 wy = 1;
1149 } else {
1150 s = plr_goanim[p->s / 8];
1151 w = (p->f & PLF_FIRE) ? 'B' : 'A';
1152 wx = p->d ? 2 : -2;
1153 wy = 1 + wytab[s - 'A'];
1155 break;
1156 case DIE:
1157 s = plr_dieanim[p->s];
1158 break;
1159 case SLOP:
1160 s = plr_slopanim[p->s];
1161 break;
1162 case OUT:
1163 s = 0;
1164 break;
1166 if (p->wpn == 0) {
1167 w = 0;
1169 if (w) {
1170 R_gl_draw_image(&plr_wpn[(int)p->wpn][w -'A'], p->o.x + wx, p->o.y + wy, p->d);
1172 if (s) {
1173 R_gl_draw_image(&plr_spr[(s - 'A') * 2 + p->d], p->o.x, p->o.y, plr_sprd[(s - 'A') * 2 + p->d]);
1174 R_gl_set_color(p->color + PLAYER_COLOR_OFFSET);
1175 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]);
1179 static void R_draw_monsters (void) {
1180 enum {SLEEP, GO, RUN, CLIMB, DIE, DEAD, ATTACK, SHOOT, PAIN, WAIT, REVIVE, RUNOUT}; // copypasted from monster.c!
1181 int i;
1182 for (i = 0; i < MAXMN; i++) {
1183 if (mn[i].t != MN_NONE) {
1184 int x = mn[i].o.x;
1185 int y = mn[i].o.y;
1186 if (mn[i].t < MN__LAST) {
1187 if ((mn[i].t != MN_SOUL && mn[i].t != MN_PAIN) || mn[i].st != DEAD) {
1188 int ap = mn[i].ap[mn[i].ac];
1189 int d = (ap - 'A') * 2 + mn[i].d;
1190 int dir = mn_sprd[mn[i].t - 1][d];
1191 if (mn[i].t == MN_MAN && (ap == 'E' || ap == 'F')) {
1192 R_gl_draw_image(&mn_sgun[ap - 'E'], x, y, mn[i].d);
1194 R_gl_draw_image(&mn_spr[mn[i].t - 1][d], x, y, dir);
1195 if (mn[i].t == MN_MAN) {
1196 R_gl_set_color(MANCOLOR + PLAYER_COLOR_OFFSET);
1197 R_gl_draw_image_color(&mn_man_msk[d], x, y, dir);
1200 if (mn[i].t == MN_VILE && mn[i].st == SHOOT) {
1201 R_gl_draw_image(&mn_fspr[mn[i].ac / 3], mn[i].tx, mn[i].ty, 0);
1203 } else if (mn[i].t == MN_PL_DEAD || mn[i].t == MN_PL_MESS) {
1204 int type = mn[i].t - MN_PL_DEAD;
1205 R_gl_draw_image(&pl_spr[type], x, y, 0);
1206 R_gl_set_color(mn[i].d);
1207 R_gl_draw_image_color(&pl_msk[type], x, y, 0);
1213 static void R_draw_weapons (void) {
1214 enum {NONE, ROCKET, PLASMA, APLASMA, BALL1, BALL2, BALL7, BFGBALL, BFGHIT, MANF, REVF, FIRE}; // copypasted from weapons.c!
1215 int i, s, d, x, y;
1216 for (i = 0; i < MAXWPN; ++i) {
1217 s = -1;
1218 d = 0;
1219 switch (wp[i].t) {
1220 case REVF:
1221 case ROCKET:
1222 d = wp[i].s;
1223 if (d < 2) {
1224 d = wp[i].o.xv > 0 ? 1 : 0;
1225 x = abs(wp[i].o.xv);
1226 y = wp[i].o.yv;
1227 s = 0;
1228 if (y < 0) {
1229 if (-y >= x) {
1230 s = 30;
1232 } else if (y > 0) {
1233 if (y >= x / 2) {
1234 s = 31;
1237 } else {
1238 s = (d - 2) / 2 + 1;
1239 d = 0;
1241 break;
1242 case MANF:
1243 s=wp[i].s;
1244 if (s >= 2) {
1245 s /= 2;
1246 break;
1248 case PLASMA:
1249 case APLASMA:
1250 case BALL1:
1251 case BALL7:
1252 case BALL2:
1253 s = wp[i].s;
1254 if (s >= 2) {
1255 s = s / 2 + 1;
1257 switch (wp[i].t) {
1258 case PLASMA:
1259 s += 4;
1260 break;
1261 case APLASMA:
1262 s += 11;
1263 break;
1264 case BALL1:
1265 s += 32;
1266 break;
1267 case BALL2:
1268 s += 42;
1269 break;
1270 case BALL7:
1271 s += 37;
1272 d = wp[i].o.xv >= 0 ? 1 : 0;
1273 break;
1274 case MANF:
1275 s += 47;
1276 d= wp[i].o.xv>=0 ? 1 : 0;
1277 break;
1279 break;
1280 case BFGBALL:
1281 s = wp[i].s;
1282 if (s >= 2) {
1283 s = s / 2 + 1;
1285 s += 18;
1286 break;
1287 case BFGHIT:
1288 s = wp[i].s / 2 + 26;
1289 break;
1291 if (s >= 0) {
1292 R_gl_draw_image(&wp_spr[s * 2 + d], wp[i].o.x, wp[i].o.y, wp_sprd[s * 2 + d]);
1297 static void R_draw_smoke (void) {
1298 int i, s;
1299 for (i = 0; i < MAXSMOK; ++i) {
1300 if (sm[i].t) {
1301 switch (sm[i].s) {
1302 case 0:
1303 s = sm[i].t;
1304 if (s >= (SMSN - 1) * 3) {
1305 s = 0;
1306 } else {
1307 s = SMSN - 1 - s / 3;
1309 R_gl_draw_image(&smk_spr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1310 break;
1311 case 1:
1312 s = sm[i].t;
1313 if (s >= FLSN - 1) {
1314 s = 0;
1315 } else {
1316 s = FLSN - 1 - s;
1318 R_gl_draw_image(&smk_fspr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1319 break;
1325 static void R_draw_effects (void) {
1326 enum {NONE, TFOG, IFOG, BUBL}; // copypasted from fx.c
1327 int i, s;
1328 for (i = 0; i < MAXFX; ++i) {
1329 switch (fx[i].t) {
1330 case TFOG:
1331 s = fx[i].s / 2;
1332 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1333 break;
1334 case IFOG:
1335 s = fx[i].s / 2 + 10;
1336 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1337 break;
1338 case BUBL:
1339 glDisable(GL_BLEND);
1340 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1341 glDisable(GL_TEXTURE_2D);
1342 glBegin(GL_POINTS);
1343 R_gl_set_color(0xC0 + fx[i].s);
1344 glVertex2i(fx[i].x >> 8, (fx[i].y >> 8) + 1);
1345 glEnd();
1346 break;
1351 static int get_pu_st (int t) {
1352 if (t >= PL_FLASH) {
1353 return 1;
1354 } else if((t / 9) & 1) {
1355 return 0;
1356 } else {
1357 return 1;
1361 static void R_draw_view (int x, int y, int w, int h, int camx, int camy) {
1362 glPushMatrix();
1363 R_gl_setclip(x, y, w, h);
1364 glTranslatef(x, y, 0);
1365 if (w_horiz && horiz.n != NULL) {
1366 R_gl_draw_image_ext(&horiz, 0, 0, w, h);
1367 if (sky_type == 2 && lt_time < 0) {
1368 image *tanderbolt = &ltn[lt_type][lt_time < -5 ? 0 : 1];
1369 if (!lt_side) {
1370 R_gl_draw_image(tanderbolt, 0, lt_ypos, 0);
1371 } else {
1372 R_gl_draw_image(tanderbolt, w - 1, lt_ypos, 1);
1375 } else {
1376 glDisable(GL_BLEND);
1377 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1378 glDisable(GL_TEXTURE_2D);
1379 R_gl_set_color(DEFAULT_SKY_COLOR);
1380 R_gl_draw_quad(0, 0, w, h);
1382 int maxx = min((camx + w) / CELW + 1, FLDW);
1383 int maxy = min((camy + h) / CELH + 1, FLDH);
1384 int minx = max((camx - max_wall_width) / CELW, 0);
1385 int miny = max((camy - max_wall_height) / CELH, 0);
1386 glTranslatef(-camx, -camy, 0);
1387 R_draw_fld((byte*)fldb, minx, miny, maxx, maxy, 0);
1388 R_draw_dots();
1389 R_draw_items();
1390 R_draw_player(&pl1);
1391 if (_2pl) {
1392 R_draw_player(&pl2);
1394 R_draw_monsters();
1395 R_draw_weapons();
1396 R_draw_smoke();
1397 R_draw_effects();
1398 R_draw_fld((byte*)fldf, minx, miny, maxx, maxy, 1);
1399 glTranslatef(camx, camy, 0);
1400 if (sky_type == 2 && (lt_time == -4 || lt_time == -2)) {
1401 glColor4ub(255, 255, 255, 255);
1402 glEnable(GL_BLEND);
1403 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1404 glDisable(GL_TEXTURE_2D);
1405 R_gl_draw_quad(0, 0, w, h);
1407 glPopMatrix();
1410 static void R_draw_player_view (player_t *p, int x, int y, int w, int h) {
1411 p->looky = min(max(p->looky, -SCRH / 4), SCRH / 4); // TODO remove writeback
1412 int st = stone.w;
1413 int cw = w - st;
1414 int cx = min(max(p->o.x, cw / 2), FLDW * CELW - cw / 2);
1415 int cy = min(max(p->o.y - 12 + p->looky, h / 2), FLDH * CELH - h / 2);
1416 int camx = max(cx - cw / 2, 0);
1417 int camy = max(cy - h / 2, 0);
1418 glPushMatrix();
1419 R_draw_view(x, y + 1, cw, h - 2, camx, camy);
1420 glTranslatef(x, y, 0);
1421 if (p->invl) {
1422 if (get_pu_st(p->invl)) {
1423 glEnable(GL_BLEND);
1424 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1425 glDisable(GL_TEXTURE_2D);
1426 glColor4ub(191, 191, 191, 255);
1427 R_gl_draw_quad(0, 0, cw, h);
1429 } else {
1430 if (p->suit && get_pu_st(p->suit)) {
1431 glEnable(GL_BLEND);
1432 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1433 glDisable(GL_TEXTURE_2D);
1434 glColor4ub(0, 255, 0, 192);
1435 R_gl_draw_quad(0, 0, cw, h);
1437 int f = min(max(p->pain * 3, 0), 255);
1438 if (f > 0) {
1439 glEnable(GL_BLEND);
1440 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1441 glDisable(GL_TEXTURE_2D);
1442 glColor4ub(255, 0, 0, f);
1443 R_gl_draw_quad(0, 0, cw, h);
1446 R_gl_setclip(x, y, w, h);
1447 glTranslatef(-x + cw, 0, 0);
1448 R_gl_draw_image(&stone, 0, 0, 0);
1449 int i = stone.h;
1450 while (i < h) {
1451 R_gl_draw_image(&stone2, 0, i, 0);
1452 i += stone2.h;
1454 if (p->drawst & PL_DRAWAIR) {
1455 if (p->air < PL_AIR) {
1456 int a = min(max(p->air, 0), MAXAIR) * 100 / MAXAIR;
1457 glDisable(GL_BLEND);
1458 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1459 glDisable(GL_TEXTURE_2D);
1460 R_gl_set_color(0xC8);
1461 R_gl_draw_quad(10, 49, a, 2);
1464 if (p->drawst & PL_DRAWLIFE) {
1465 Z_gotoxy(10, 7);
1466 Z_printhf("%3d%%", p->life);
1468 if (p->drawst & PL_DRAWARMOR) {
1469 Z_gotoxy(10, 7 + 19);
1470 Z_printhf("%3d%%", p->armor);
1472 if (p->drawst & PL_DRAWWPN) {
1473 switch(p->wpn) {
1474 case 2:
1475 case 5:
1476 i = p->ammo;
1477 break;
1478 case 3:
1479 case 4:
1480 case 9:
1481 i = p->shel;
1482 break;
1483 case 6:
1484 i = p->rock;
1485 break;
1486 case 7:
1487 case 8:
1488 i = p->cell;
1489 break;
1490 case 10:
1491 i = p->fuel;
1492 break;
1493 default:
1494 i = -1;
1495 break;
1497 // weapon
1498 if (p->wpn >= 0) {
1499 R_gl_draw_image(&sth[12 + p->wpn], st - 88, 58 + 19, 0);
1501 // ammo
1502 if (p->wpn >= 2) {
1503 Z_gotoxy(st - 10 - 5 * 14, 58 + 2);
1504 Z_printhf("%5d", i);
1507 if (p->drawst & PL_DRAWFRAG && g_dm) {
1508 Z_gotoxy(st - 5 - 5 * 14, 77 + 5);
1509 Z_printhf("%5d", p->frag);
1511 if (p->drawst & PL_DRAWKEYS) {
1512 int x, k, n;
1513 for (k = p->keys >> 4, n = 0, x = st - 75; n < 3; n++, k >>= 1, x += 9) {
1514 if (k & 1) {
1515 R_gl_draw_image(&keys[n], x, 91, 0);
1519 if (p->drawst & PL_DRAWLIVES && !_2pl) {
1520 Z_gotoxy(st - 35, 17);
1521 Z_printhf("%d", p->lives);
1523 glPopMatrix();
1526 /* --- Game --- */
1528 static void pl_info (player_t *p, int x, int y) {
1529 dword t = p->kills * 10920 / g_time;
1530 Z_gotoxy(x + 25, y); Z_printbf("KILLS");
1531 Z_gotoxy(x + 25, y + 15); Z_printbf("KPM");
1532 Z_gotoxy(x + 25, y + 30); Z_printbf("SECRETS %u / %u", p->secrets, sw_secrets);
1533 Z_gotoxy(x + 255, y); Z_printbf("%u", p->kills);
1534 Z_gotoxy(x + 255, y + 15); Z_printbf("%u.%u", t / 10, t % 10);
1537 static void R_draw_intermission (void) {
1538 int cx = SCRW / 2;
1539 word hr, mn, sc, h;
1540 Z_gotoxy(cx - 14*12/2, 20);
1541 Z_printbf("LEVEL COMPLETE");
1542 Z_calc_time(g_time, &hr, &mn, &sc);
1543 Z_gotoxy(cx - 12*12/2, 40);
1544 Z_printbf("TIME %u:%02u:%02u", hr, mn, sc);
1545 h = 40 + SCRH / 10;
1546 if (_2pl) {
1547 Z_gotoxy(cx - 10*12/2, h);
1548 Z_printbf("PLAYER ONE");
1549 h += 20;
1551 pl_info(&pl1, cx - 160, h);
1552 if (_2pl) {
1553 h += 30 + SCRH / 10;
1554 Z_gotoxy(cx - 10*12/2, h);
1555 Z_printbf("PLAYER TWO");
1556 h += 20;
1557 pl_info(&pl2, cx - 160, h);
1561 static void W_act (void) {
1562 int i, a;
1563 if (g_time % 3 == 0) {
1564 for (i = 1; i < max_textures; i++) {
1565 a = walani[i];
1566 if (a != 0) {
1567 anic[a]++;
1568 if (anip[a][anic[a]].res == -1) {
1569 anic[a] = 0;
1571 walp[i] = anip[a][anic[a]];
1577 void R_draw (void) {
1578 W_act();
1579 glClearColor(0, 0, 0, 1);
1580 glClear(GL_COLOR_BUFFER_BIT);
1581 glEnable(GL_SCISSOR_TEST);
1582 R_gl_setmatrix();
1583 switch (g_st) {
1584 case GS_ENDANIM:
1585 case GS_END2ANIM:
1586 case GS_DARKEN:
1587 case GS_BVIDEO:
1588 case GS_EVIDEO:
1589 case GS_END3ANIM:
1590 break;
1591 case GS_TITLE:
1592 R_gl_draw_image_ext(&scrnh[0], 0, 0, SCRW, SCRH);
1593 break;
1594 case GS_INTER:
1595 R_gl_draw_image_ext(&scrnh[1], 0, 0, SCRW, SCRH);
1596 R_draw_intermission();
1597 break;
1598 case GS_ENDSCR:
1599 R_gl_draw_image_ext(&scrnh[2], 0, 0, SCRW, SCRH);
1600 break;
1601 case GS_GAME:
1602 if (_2pl) {
1603 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH / 2);
1604 R_draw_player_view(&pl2, 0, SCRH / 2, SCRW, SCRH / 2);
1605 } else {
1606 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH);
1608 R_gl_setclip(0, 0, SCRW, SCRH);
1609 break;
1611 GM_draw();
1612 Y_swap_buffers();
1615 static void R_alloc (void) {
1616 char s[10];
1617 int i, j, n;
1618 logo("R_alloc: load graphics\n");
1619 /* Game */
1620 scrnh[0] = R_gl_loadimage("TITLEPIC");
1621 scrnh[1] = R_gl_loadimage("INTERPIC");
1622 scrnh[2] = R_gl_loadimage("ENDPIC");
1623 for (i = 0; i < 2; i++) {
1624 sprintf(s, "LTN%c", '1' + i);
1625 for (j = 0; j < 2; j++) {
1626 ltn[i][j] = Z_getspr(s, j, 0, NULL);
1629 /* Smoke */
1630 for (i = 0; i < SMSN; i++) {
1631 smk_spr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_smoke_spr);
1633 for (i = 0; i < FLSN; i++) {
1634 smk_fspr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_flame_spr);
1636 /* Effects */
1637 for (i = 0; i < 10; i++) {
1638 fx_spr[i] = Z_getspr("TFOG", i, 0, fx_sprd + i);
1640 for (; i < 15; i++) {
1641 fx_spr[i] = Z_getspr("IFOG", i - 10, 0, fx_sprd + i);
1643 /* Weapons */
1644 for (i = 0; i < 4; i++) {
1645 wp_spr[i * 2] = Z_getspr("MISL", i, 1, wp_sprd + i * 2);
1646 wp_spr[i * 2 + 1] = Z_getspr("MISL", i, 2, wp_sprd + i * 2 + 1);
1648 for (; i < 6; i++) {
1649 wp_spr[i * 2] = Z_getspr("PLSS", i - 4, 1, wp_sprd + i * 2);
1650 wp_spr[i * 2 + 1] = Z_getspr("PLSS", i - 4, 2, wp_sprd + i * 2 + 1);
1652 for (; i < 11; i++) {
1653 wp_spr[i * 2] = Z_getspr("PLSE", i - 6, 1, wp_sprd + i * 2);
1654 wp_spr[i * 2 + 1] = Z_getspr("PLSE", i - 6, 2, wp_sprd + i * 2 + 1);
1656 for (; i < 13; i++) {
1657 wp_spr[i * 2] = Z_getspr("APLS", i - 11, 1, wp_sprd + i * 2);
1658 wp_spr[i * 2 + 1] = Z_getspr("APLS", i - 11, 2, wp_sprd + i * 2 + 1);
1660 for (; i < 18; i++) {
1661 wp_spr[i * 2] = Z_getspr("APBX", i - 13, 1, wp_sprd + i * 2);
1662 wp_spr[i * 2 + 1] = Z_getspr("APBX", i - 13, 2, wp_sprd + i * 2 + 1);
1664 for(; i < 20; i++) {
1665 wp_spr[i * 2] = Z_getspr("BFS1", i - 18, 1, wp_sprd + i * 2);
1666 wp_spr[i * 2 + 1] = Z_getspr("BFS1", i - 18, 2, wp_sprd + i * 2 + 1);
1668 for (; i < 26; i++) {
1669 wp_spr[i * 2] = Z_getspr("BFE1", i - 20, 1, wp_sprd + i * 2);
1670 wp_spr[i * 2 + 1] = Z_getspr("BFE1", i - 20, 2, wp_sprd + i * 2 + 1);
1672 for (; i < 30; i++) {
1673 wp_spr[i * 2] = Z_getspr("BFE2", i - 26, 1, wp_sprd + i * 2);
1674 wp_spr[i * 2 + 1] = Z_getspr("BFE2", i - 26, 2, wp_sprd + i * 2 + 1);
1676 for (; i < 32; i++) {
1677 wp_spr[i * 2] = Z_getspr("MISL", i - 30 + 4, 1, wp_sprd + i * 2);
1678 wp_spr[i * 2 + 1] = Z_getspr("MISL", i - 30 + 4, 2, wp_sprd + i * 2 + 1);
1680 for (; i < 37; i++) {
1681 wp_spr[i * 2] = Z_getspr("BAL1", i - 32, 1, wp_sprd + i * 2);
1682 wp_spr[i * 2 + 1] = Z_getspr("BAL1", i - 32, 2, wp_sprd + i * 2 + 1);
1684 for (; i < 42; i++) {
1685 wp_spr[i * 2] = Z_getspr("BAL7", i - 37, 1, wp_sprd + i * 2);
1686 wp_spr[i * 2 + 1] = Z_getspr("BAL7", i - 37, 2, wp_sprd + i * 2 + 1);
1688 for (; i < 47; i++) {
1689 wp_spr[i * 2] = Z_getspr("BAL2", i - 42, 1, wp_sprd + i * 2);
1690 wp_spr[i * 2 + 1] = Z_getspr("BAL2", i - 42, 2, wp_sprd + i * 2 + 1);
1692 for (; i < 49; i++) {
1693 wp_spr[i * 2] = Z_getspr("MANF", i - 47, 1, wp_sprd + i * 2);
1694 wp_spr[i * 2 + 1] = Z_getspr("MANF", i - 47, 2, wp_sprd + i * 2 + 1);
1696 /* Items */
1697 static const char snm[18][4] = {
1698 "CLIP", "SHEL", "ROCK", "CELL", "AMMO", "SBOX", "BROK", "CELP",
1699 "STIM", "MEDI", "BPAK",
1700 "CSAW", "SHOT", "SGN2", "MGUN", "LAUN", "PLAS", "BFUG"
1701 };
1702 static const char n4[4][4] = {
1703 "SOUL", "SMRT", "SMGT", "SMBT"
1704 };
1705 static const char n3[2][4] = {
1706 "GOR1", "FCAN"
1707 };
1708 for (i = 0; i < 18; i++) {
1709 item_spr[i] = Z_getspr(snm[i], 0, 0, item_sprd + i);
1711 for (; i < 20; i++) {
1712 item_spr[i] = Z_getspr("ARM1", i - 18, 0, item_sprd + i);
1713 item_spr[i + 2] = Z_getspr("ARM2", i - 18, 0, item_sprd + i);
1715 i+=2;
1716 for (; i < 26; i++) {
1717 item_spr[i] = Z_getspr("MEGA", i - 22, 0, item_sprd + i);
1719 for (; i < 30; i++) {
1720 item_spr[i] = Z_getspr("PINV", i - 26, 0, item_sprd + i);
1722 item_spr[30] = Z_getspr("AQUA", 0, 0, item_sprd + 30);
1723 item_spr[31] = Z_getspr("KEYR", 0, 0, item_sprd + 31);
1724 item_spr[32] = Z_getspr("KEYG", 0, 0, item_sprd + 32);
1725 item_spr[33] = Z_getspr("KEYB", 0, 0, item_sprd + 33);
1726 item_spr[34] = Z_getspr("SUIT", 0, 0, item_sprd + 34);
1727 for (n = 35, j = 0; j < 4; j++) {
1728 for (i = 0; i < 4; i++, n++) {
1729 item_spr[n] = Z_getspr(n4[j], i, 0, item_sprd + n);
1732 for (j = 0; j < 2; j++) {
1733 for (i = 0; i < 3; i++, n++) {
1734 item_spr[n] = Z_getspr(n3[j], i, 0, item_sprd + n);
1737 item_spr[57] = Z_getspr("GUN2", 0, 0, item_sprd + 57);
1738 /* Player */
1739 for (i = 0; i < 27; i++) {
1740 plr_spr[i * 2] = Z_getspr("PLAY", i, 1, plr_sprd + i * 2);
1741 plr_msk[i * 2] = R_gl_get_special_spr("PLAY", i, 1, &R_extract_mask_spr);
1742 plr_spr[i * 2 + 1] = Z_getspr("PLAY", i, 2, plr_sprd + i * 2 + 1);
1743 plr_msk[i * 2 + 1] = R_gl_get_special_spr("PLAY", i, 2, &R_extract_mask_spr);
1745 strncpy(s, "PWPx", 4);
1746 for (i = 1; i < 11; i++) {
1747 s[3] = (i < 10 ? '0' : 'A' - 10) + i;
1748 for (j = 0; j < 6; j++) {
1749 plr_wpn[i][j] = Z_getspr(s, j, 1, NULL);
1752 /* Monsters */
1753 static const char msn[MN_TN][4] = {
1754 "SARG", "TROO", "POSS", "SPOS", "CYBR", "CPOS", "BOSS", "BOS2", "HEAD", "SKUL",
1755 "PAIN", "SPID", "BSPI", "FATT", "SKEL", "VILE", "FISH", "BAR1", "ROBO", "PLAY"
1756 };
1757 static const int mms[MN_TN] = {
1758 14*2, 21*2, 21*2, 21*2, 16*2, 20*2, 15*2, 15*2, 12*2, 11*2,
1759 13*2, 19*2, 16*2, 20*2, 17*2, 29*2, 6*2, 2*2, 17*2, 23*2
1760 };
1761 mn_sgun[0] = Z_getspr("PWP4", 0, 1, NULL);
1762 mn_sgun[1] = Z_getspr("PWP4", 1, 1, NULL);
1763 for (j = 0; j < MN_TN; j++) {
1764 for (i = 0; i < mms[j]; i++) {
1765 mn_spr[j][i] = Z_getspr(msn[j], i / 2, (i & 1) + 1, &mn_sprd[j][i]);
1766 if (j == MN_MAN - 1) {
1767 mn_man_msk[i] = R_gl_get_special_spr(msn[j], i / 2, (i & 1) + 1, &R_extract_mask_spr);
1770 if (j == MN_BARREL - 1) {
1771 for (i = 4; i < 14; i++) {
1772 mn_spr[j][i] = Z_getspr("BEXP", i / 2 - 2, (i & 1) + 1, &mn_sprd[j][i]);
1776 for (i = 0; i < 8; i++) {
1777 mn_fspr[i] = Z_getspr("FIRE", i, 0, NULL);
1779 pl_spr[0] = Z_getspr("PLAY", 'N' - 'A', 0, NULL);
1780 pl_msk[0] = R_gl_get_special_spr("PLAY", 'N' - 'A', 0, &R_extract_mask_spr);
1781 pl_spr[1] = Z_getspr("PLAY", 'W' - 'A', 0, NULL);
1782 pl_msk[1] = R_gl_get_special_spr("PLAY", 'W' - 'A', 0, &R_extract_mask_spr);
1783 /* Misc */
1784 static const char mnm[22][8]={
1785 "STTNUM0", "STTNUM1", "STTNUM2", "STTNUM3", "STTNUM4",
1786 "STTNUM5", "STTNUM6", "STTNUM7", "STTNUM8", "STTNUM9",
1787 "STTMINUS", "STTPRCNT",
1788 "FISTA0", "CSAWA0", "PISTA0", "SHOTA0", "SGN2A0", "MGUNA0", "LAUNA0",
1789 "PLASA0", "BFUGA0", "GUN2A0"
1790 };
1791 stone = R_gl_loadimage("STONE");
1792 stone2 = R_gl_loadimage("STONE2");
1793 keys[0] = R_gl_loadimage("KEYRA0");
1794 keys[1] = R_gl_loadimage("KEYGA0");
1795 keys[2] = R_gl_loadimage("KEYBA0");
1796 for (i = 0; i < 22; i++) {
1797 sth[i] = R_gl_loadimage(mnm[i]);
1799 strcpy(s, "STBF_*");
1800 for (i = '!'; i < 160; i++) {
1801 s[5] = i;
1802 bfh[i - '!'] = R_gl_getimage(F_findres(s));
1804 for (i = '!'; i < 160; i++) {
1805 sprintf(s, "STCFN%03d", i);
1806 sfh[i - '!'] = R_gl_getimage(F_findres(s));
1808 strcpy(s, "WINUM*");
1809 for (i = '0'; i <= '9'; i++) {
1810 s[5] = i;
1811 bfh[i - '!'] = R_gl_loadimage(s);
1813 bfh[':' - '!'] = R_gl_loadimage("WICOLON");
1814 // menu
1815 msklh[0] = R_gl_loadimage("M_SKULL1");
1816 msklh[1] = R_gl_loadimage("M_SKULL2");
1817 mbarl = R_gl_loadimage("M_THERML");
1818 mbarm = R_gl_loadimage("M_THERMM");
1819 mbarr = R_gl_loadimage("M_THERMR");
1820 mbaro = R_gl_loadimage("M_THERMO");
1821 mslotl = R_gl_loadimage("M_LSLEFT");
1822 mslotm = R_gl_loadimage("M_LSCNTR");
1823 mslotr = R_gl_loadimage("M_LSRGHT");
1824 // walls
1825 for (i = 1; i < ANIT; i++) {
1826 for (j = 0; j < 5 && anm[i - 1][j]; j++) {
1827 anip[i][j] = R_gl_loadimage(anm[i - 1][j]);
1829 for(; j < 5; j++) {
1830 anip[i][j] = (image) {
1831 .n = NULL,
1832 .w = 8,
1833 .h = 8,
1834 .res = -1,
1835 };
1840 static void R_reload_textures (void);
1842 void R_set_videomode (int w, int h, int fullscreen) {
1843 assert(w > 0);
1844 assert(h > 0);
1845 int was = Y_videomode_setted();
1846 if (root != NULL) {
1847 R_cache_free(root, 0);
1848 root = NULL;
1850 int res = Y_set_videomode_opengl(w, h, fullscreen);
1851 if (res == 0) {
1852 if (was == 0) {
1853 ERR_failinit("Unable to set video mode\n");
1856 Y_get_videomode(&SCRW, &SCRH);
1857 root = R_cache_new();
1858 assert(root);
1859 R_alloc();
1860 R_reload_textures();
1863 static int video_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
1864 static int cur;
1865 static int w, h, fullscreen;
1866 static char buf[16];
1867 static int buflen;
1868 static int vmode;
1869 const videomode_t *v;
1870 enum { VIDEOMODE, FULLSCREEN, APPLY, __NUM__ };
1871 static const simple_menu_t sm = {
1872 GM_BIG, "Video", NULL,
1874 { "Mode: ", NULL },
1875 { "Fullscreen: ", NULL },
1876 { "Apply ", NULL },
1878 };
1879 if (msg->type == GM_ENTER) {
1880 Y_get_videomode(&w, &h);
1881 fullscreen = Y_get_fullscreen();
1882 v = Y_get_videomode_list_opengl(fullscreen);
1883 vmode = 0;
1884 while (vmode < v->n && v->modes[vmode].w != w && v->modes[vmode].h != h) {
1885 vmode += 1;
1887 if (vmode < v->n) {
1888 w = v->modes[vmode].w;
1889 h = v->modes[vmode].h;
1891 snprintf(buf, 16, "%ix%i", w, h);
1892 buflen = strlen(buf);
1893 return 1;
1895 if (i == VIDEOMODE) {
1896 switch (msg->type) {
1897 case GM_GETSTR: return GM_init_str(msg, buf, buflen);
1898 case GM_SELECT:
1899 v = Y_get_videomode_list_opengl(fullscreen);
1900 vmode = vmode + 1 >= v->n ? 0 : vmode + 1;
1901 if (v->n > 0) {
1902 w = v->modes[vmode].w;
1903 h = v->modes[vmode].h;
1904 } else {
1905 Y_get_videomode(&w, &h);
1907 snprintf(buf, 16, "%ix%i", w, h);
1908 buflen = strlen(buf);
1909 return 1;
1911 } else if (i == FULLSCREEN) {
1912 switch (msg->type) {
1913 case GM_GETSTR: return GM_init_str(msg, fullscreen ? "Yes" : "No ", 3);
1914 case GM_SELECT: fullscreen = !fullscreen; return 1;
1916 } else if (i == APPLY) {
1917 switch (msg->type) {
1918 case GM_SELECT: R_set_videomode(w, h, fullscreen); return 1;
1921 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
1924 static const menu_t video_menu = {
1925 NULL, &video_menu_handler
1926 };
1928 const menu_t *R_menu (void) {
1929 return &video_menu;
1932 void R_init (void) {
1933 logo("R_init: intialize opengl render\n");
1934 R_init_playpal();
1935 R_set_videomode(SCRW, SCRH, 0);
1938 void R_done (void) {
1939 R_cache_free(root, 1);
1940 Y_unset_videomode();
1941 root = NULL;
1944 void R_get_name (int n, char s[8]) {
1945 assert(n >= 0 && n < 256);
1946 if (walp[n].res == -1) {
1947 memset(s, 0, 8);
1948 } else if (walp[n].res == -2) {
1949 memcpy(s, "_WATER_", 8);
1950 s[7] = '0' + (intptr_t)walp[n].n - 1;
1951 } else if (walani[n] > 0) {
1952 memcpy(s, anm[walani[n] - 1][0], 8);
1953 } else {
1954 F_getresname(s, walp[n].res & 0x7FFF);
1958 static short getani (char n[8]) {
1959 short i = 0;
1960 while (i < ANIT - 1 && strncasecmp(n, anm[i][0], 8) != 0) {
1961 i++;
1963 return i < ANIT - 1 ? i + 1 : 0;
1966 int R_get_special_id (int n) {
1967 assert(n >= 0 && n <= 256);
1968 return walp[n].res == -2 ? (intptr_t)walp[n].n : -1;
1971 static void R_reload_textures (void) {
1972 int i;
1973 char s[8];
1974 for (i = 0; i < max_textures; i++) {
1975 R_get_name(i, s);
1976 if (walp[i].res >= 0) {
1977 walp[i] = R_gl_getimage(walp[i].res);
1980 if (horiz.n) {
1981 horiz = R_gl_getimage(horiz.res);
1985 void R_begin_load (void) {
1986 int i;
1987 for (i = 0; i < 256; i++) {
1988 if (walp[i].n != NULL && walp[i].res >= 0 && walani[i] == 0) {
1989 R_gl_free_image(&walp[i]);
1991 memset(&walp[i], 0, sizeof(image));
1992 walp[i].res = -1;
1993 walswp[i] = i;
1994 walani[i] = 0;
1996 memset(anic, 0, sizeof(anic));
1997 max_wall_width = 0;
1998 max_wall_height = 0;
1999 max_textures = 1;
2002 void R_load (char s[8], int f) {
2003 assert(max_textures < 256);
2004 if (!s[0]) {
2005 walp[max_textures] = (image) {
2006 .n = NULL,
2007 .x = 0,
2008 .y = 0,
2009 .w = 0,
2010 .h = 0,
2011 .res = -1,
2012 };
2013 } else if (strncasecmp(s, "_WATER_", 7) == 0) {
2014 walp[max_textures] = (image) {
2015 .n = (void*)((intptr_t)s[7] - '0' + 1),
2016 .x = 0,
2017 .y = 0,
2018 .w = 8,
2019 .h = 8,
2020 .res = -2,
2021 };
2022 } else {
2023 walp[max_textures] = R_gl_loadimage(s);
2024 if (f) {
2025 walp[max_textures].res |= 0x8000;
2027 if (s[0] == 'S' && s[1] == 'W' && s[4] == '_') {
2028 walswp[max_textures] = 0;
2030 walani[max_textures] = getani(s);
2032 max_wall_width = max(max_wall_width, walp[max_textures].w);
2033 max_wall_height = max(max_wall_height, walp[max_textures].h);
2034 max_textures++;
2037 void R_end_load (void) {
2038 int i, j, k, g;
2039 char s[8];
2040 j = max_textures;
2041 for (i = 1; i < 256 && j < 256; i++) {
2042 if (walswp[i] == 0) {
2043 R_get_name(i, s);
2044 s[5] ^= 1;
2045 g = F_getresid(s) | (walp[i].res & 0x8000);
2046 k = 1;
2047 while (k < 256 && walp[k].res != g) {
2048 k += 1;
2050 if (k >= 256) {
2051 k = j;
2052 j += 1;
2053 max_textures += 1;
2054 walp[k] = R_gl_getimage(g);
2055 walf[k] = g & 0x8000 ? 1 : 0;
2057 walswp[i] = k;
2058 walswp[k] = i;
2063 void R_loadsky (int sky) {
2064 char s[6];
2065 strcpy(s, "RSKYx");
2066 s[4] = '0' + sky;
2067 R_gl_free_image(&horiz);
2068 horiz = R_gl_loadimage(s);