DEADSOFTWARE

engine: use lib cp866 where needed
[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 static int count_menu_entries (const new_menu_t *m) {
782 assert(m != NULL);
783 int i = 0;
784 while (m->entries[i].type != 0) {
785 i += 1;
787 return i;
790 #define SCROLLER_MIDDLE 10
791 #define TEXTFIELD_MIDDLE 2
793 static void get_entry_size (const new_menu_t *m, int i, int *w, int *h) {
794 assert(m != NULL);
795 assert(i >= 0);
796 assert(w != NULL);
797 assert(h != NULL);
798 int x, y;
799 new_msg_t msg;
800 switch (m->entries[i].type) {
801 case GM_BUTTON:
802 case GM_SCROLLER:
803 case GM_TEXTFIELD:
804 case GM_TEXTFIELD_BUTTON:
805 x = Z_get_big_string_width("%s", m->entries[i].caption);
806 break;
807 case GM_SMALL_BUTTON:
808 x = Z_get_small_string_width("%s", m->entries[i].caption);
809 break;
810 default:
811 assert(0);
813 switch (m->entries[i].type) {
814 case GM_BUTTON:
815 msg.type = GM_GETSTR;
816 if (GM_send(m, i, &msg)) {
817 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
819 y = 16;
820 break;
821 case GM_SMALL_BUTTON:
822 msg.type = GM_GETSTR;
823 if (GM_send(m, i, &msg)) {
824 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
826 y = 12;
827 break;
828 case GM_SCROLLER:
829 x += (SCROLLER_MIDDLE + 2) * 8;
830 y = 16;
831 break;
832 case GM_TEXTFIELD:
833 case GM_TEXTFIELD_BUTTON:
834 msg.type = GM_GETSTR;
835 if (GM_send(m, i, &msg)) {
836 x += (msg.string.maxlen + 2) * 8;
837 } else {
838 x += (TEXTFIELD_MIDDLE + 2) * 8;
840 y = 16;
841 break;
842 default:
843 assert(0);
845 *w = x;
846 *h = y;
849 static void get_menu_size (const new_menu_t *m, int *w, int *h) {
850 assert(m != NULL);
851 assert(w != NULL);
852 assert(h != NULL);
853 int i, x, y, xx, yy;
854 int n = count_menu_entries(m);
855 switch (m->type) {
856 case GM_BIG: x = Z_get_big_string_width("%s", m->title); break;
857 case GM_SMALL: x = Z_get_small_string_width("%s", m->title); break;
858 default: assert(0);
860 y = 0;
861 for (i = 0; i < n; i++) {
862 get_entry_size(m, i, &xx, &yy);
863 x = max(x, xx);
864 y += yy;
866 *w = x;
867 *h = y;
870 static int GM_draw (void) {
871 int i, j, xoff, yoff, n, x, y, cur, curoff, w, recv;
872 const new_menu_t *m = GM_get();
873 new_msg_t msg;
874 if (m != NULL) {
875 get_menu_size(m, &x, &y);
876 x = SCRW / 2 - x / 2;
877 y = SCRH / 2 - y / 2;
878 // --- title ---
879 switch (m->type) {
880 case GM_BIG:
881 yoff = 20;
882 Z_gotoxy(x, y - 10);
883 Z_printbf("%s", m->title);
884 break;
885 case GM_SMALL:
886 yoff = 8;
887 Z_gotoxy(x, y - 10);
888 Z_printsf("%s", m->title);
889 break;
890 default:
891 assert(0);
893 // --- entries ---
894 curoff = yoff;
895 cur = GM_geti();
896 n = count_menu_entries(m);
897 for (i = 0; i < n; i++) {
898 if (i == cur) {
899 curoff = yoff;
900 if (m->entries[cur].type == GM_SMALL_BUTTON) {
901 Z_gotoxy(x - 8, y + curoff);
902 Z_printsf(">");
903 } else {
904 R_gl_draw_image(&msklh[(gm_tm / 6) & 1], x - 25, y + curoff - 8, 0);
907 switch (m->entries[i].type) {
908 case GM_BUTTON:
909 Z_gotoxy(x, y + yoff);
910 w = Z_printbf("%s", m->entries[i].caption);
911 msg.type = GM_GETSTR;
912 if (GM_send(m, i, &msg)) {
913 Z_gotoxy(x + w, y + yoff);
914 Z_printbf("%.*s", msg.string.maxlen, msg.string.s);
916 yoff += 16;
917 break;
918 case GM_SMALL_BUTTON:
919 Z_gotoxy(x, y + yoff);
920 w = Z_printsf("%s", m->entries[i].caption);
921 msg.type = GM_GETSTR;
922 if (GM_send(m, i, &msg)) {
923 Z_gotoxy(x + w, y + yoff);
924 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
926 yoff += 12;
927 break;
928 case GM_TEXTFIELD:
929 case GM_TEXTFIELD_BUTTON:
930 msg.type = GM_GETSTR;
931 recv = GM_send(m, i, &msg);
932 Z_gotoxy(x, y + yoff);
933 xoff = Z_printbf("%s", m->entries[i].caption);
934 yoff += 9;
935 w = (recv ? msg.string.maxlen : TEXTFIELD_MIDDLE) + 1;
936 R_gl_draw_image(&mslotl, x + xoff, y + yoff, 0);
937 for (j = 1; j < w; j++) {
938 R_gl_draw_image(&mslotm, x + xoff + j * 8, y + yoff, 0);
940 R_gl_draw_image(&mslotr, x + xoff + j * 8, y + yoff, 0);
941 Z_gotoxy(x + xoff + 4, y + yoff - 7);
942 if (input && i == cur) {
943 Z_printsf("%.*s_", imax, ibuf);
944 } else if (recv) {
945 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
947 yoff += 7;
948 break;
949 case GM_SCROLLER:
950 Z_gotoxy(x, y + yoff);
951 xoff = Z_printbf("%s", m->entries[i].caption);
952 R_gl_draw_image(&mbarl, x + xoff, y + yoff, 0);
953 for (j = 1; j < SCROLLER_MIDDLE + 1; j++) {
954 R_gl_draw_image(&mbarm, x + xoff + j * 8, y + yoff, 0);
956 R_gl_draw_image(&mbarr, x + xoff + j * 8, y + yoff, 0);
957 msg.type = GM_GETINT;
958 if (GM_send(m, i, &msg)) {
959 int lev = (msg.integer.i - msg.integer.a) * ((SCROLLER_MIDDLE - 1) * 8) / msg.integer.b;
960 R_gl_draw_image(&mbaro, x + xoff + lev + 8, y + yoff, 0);
962 yoff += 16;
963 break;
964 default:
965 assert(0);
969 return m != NULL;
972 /* --- View --- */
974 static void R_draw_fld (byte *fld, int minx, int miny, int maxx, int maxy, int fg) {
975 int i, j;
976 assert(minx >= 0 && minx <= FLDW);
977 assert(miny >= 0 && miny <= FLDH);
978 assert(maxx >= 0 && maxx <= FLDW);
979 assert(maxy >= 0 && maxy <= FLDH);
980 for (j = miny; j < maxy; j++) {
981 for (i = minx; i < maxx; i++) {
982 byte id = fld[j * FLDW + i];
983 if (id != 0) {
984 if (walp[id].res < 0) {
985 if (fg) {
986 switch (R_get_special_id(id)) {
987 case 1:
988 glColor4ub(0, 0, 255, 127);
989 break;
990 case 2:
991 glColor4ub(0, 127, 0, 127);
992 break;
993 case 3:
994 glColor4ub(127, 0, 0, 127);
995 break;
996 default:
997 glColor4ub(0, 0, 0, 127);
998 break;
1000 glEnable(GL_BLEND);
1001 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1002 glDisable(GL_TEXTURE_2D);
1003 R_gl_draw_quad(i * CELW, j * CELW, CELW, CELH);
1005 } else {
1006 R_gl_draw_image(&walp[id], i * CELW, j * CELH, 0);
1013 static void R_draw_dots (void) {
1014 int i;
1015 glDisable(GL_BLEND);
1016 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1017 glDisable(GL_TEXTURE_2D);
1018 glBegin(GL_POINTS);
1019 for (i = 0; i < MAXDOT; i++) {
1020 if (dot[i].t != 0) {
1021 R_gl_set_color(dot[i].c);
1022 glVertex2i(dot[i].o.x, dot[i].o.y + 1);
1025 glEnd();
1028 static void R_draw_items (void) {
1029 int i, s;
1030 for (i = 0; i < MAXITEM; ++i) {
1031 s = -1;
1032 if (it[i].t && it[i].s >= 0) {
1033 switch (it[i].t & 0x7FFF) {
1034 case I_ARM1:
1035 s = it[i].s / 9 + 18;
1036 break;
1037 case I_ARM2:
1038 s = it[i].s / 9 + 20;
1039 break;
1040 case I_MEGA:
1041 s = it[i].s / 2 + 22;
1042 break;
1043 case I_INVL:
1044 s = it[i].s / 2 + 26;
1045 break;
1046 case I_SUPER:
1047 case I_RTORCH:
1048 case I_GTORCH:
1049 case I_BTORCH:
1050 s = it[i].s / 2 + (it[i].t - I_SUPER) * 4 + 35;
1051 break;
1052 case I_GOR1: case I_FCAN:
1053 s = it[i].s / 2 + (it[i].t - I_GOR1) * 3 + 51;
1054 break;
1055 case I_AQUA:
1056 s = 30;
1057 break;
1058 case I_SUIT:
1059 s = 34;
1060 break;
1061 case I_KEYR:
1062 case I_KEYG:
1063 case I_KEYB:
1064 s = (it[i].t & 0x7FFF) - I_KEYR + 31;
1065 break;
1066 case I_GUN2:
1067 s = 57;
1068 break;
1069 default:
1070 s = (it[i].t & 0x7FFF) - 1;
1073 if (s >= 0) {
1074 R_gl_draw_image(&item_spr[s], it[i].o.x, it[i].o.y, item_sprd[s]);
1079 static int standspr (player_t *p) {
1080 if (p->f & PLF_UP) {
1081 return 'X';
1082 } else if (p->f & PLF_DOWN) {
1083 return 'Z';
1084 } else {
1085 return 'E';
1089 static int wpnspr (player_t *p) {
1090 if (p->f & PLF_UP) {
1091 return 'C';
1092 } else if(p->f & PLF_DOWN) {
1093 return 'E';
1094 } else {
1095 return 'A';
1099 static void R_draw_player (player_t *p) {
1100 enum {STAND, GO, DIE, SLOP, DEAD, MESS, OUT, FALL}; // copypasted from player.c!
1101 static const int wytab[] = {-1, -2, -1, 0};
1102 int s = 'A';
1103 int w = 0;
1104 int wx = 0;
1105 int wy = 0;
1106 switch (p->st) {
1107 case STAND:
1108 if (p->f & PLF_FIRE) {
1109 s = standspr(p) + 1;
1110 w = wpnspr(p) + 1;
1111 } else if (p->pain) {
1112 s = 'G';
1113 w = 'A';
1114 wx = p->d ? 2 : -2;
1115 wy = 1;
1116 } else {
1117 s = standspr(p);
1118 w = wpnspr(p);
1120 break;
1121 case DEAD:
1122 s = 'N';
1123 break;
1124 case MESS:
1125 s = 'W';
1126 break;
1127 case GO:
1128 if (p->pain) {
1129 s = 'G';
1130 w = 'A';
1131 wx = p->d ? 2 : -2;
1132 wy = 1;
1133 } else {
1134 s = plr_goanim[p->s / 8];
1135 w = (p->f & PLF_FIRE) ? 'B' : 'A';
1136 wx = p->d ? 2 : -2;
1137 wy = 1 + wytab[s - 'A'];
1139 break;
1140 case DIE:
1141 s = plr_dieanim[p->s];
1142 break;
1143 case SLOP:
1144 s = plr_slopanim[p->s];
1145 break;
1146 case OUT:
1147 s = 0;
1148 break;
1150 if (p->wpn == 0) {
1151 w = 0;
1153 if (w) {
1154 R_gl_draw_image(&plr_wpn[(int)p->wpn][w -'A'], p->o.x + wx, p->o.y + wy, p->d);
1156 if (s) {
1157 R_gl_draw_image(&plr_spr[(s - 'A') * 2 + p->d], p->o.x, p->o.y, plr_sprd[(s - 'A') * 2 + p->d]);
1158 R_gl_set_color(p->color + PLAYER_COLOR_OFFSET);
1159 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]);
1163 static void R_draw_monsters (void) {
1164 enum {SLEEP, GO, RUN, CLIMB, DIE, DEAD, ATTACK, SHOOT, PAIN, WAIT, REVIVE, RUNOUT}; // copypasted from monster.c!
1165 int i;
1166 for (i = 0; i < MAXMN; i++) {
1167 if (mn[i].t != MN_NONE) {
1168 int x = mn[i].o.x;
1169 int y = mn[i].o.y;
1170 if (mn[i].t < MN__LAST) {
1171 if ((mn[i].t != MN_SOUL && mn[i].t != MN_PAIN) || mn[i].st != DEAD) {
1172 int ap = mn[i].ap[mn[i].ac];
1173 int d = (ap - 'A') * 2 + mn[i].d;
1174 int dir = mn_sprd[mn[i].t - 1][d];
1175 if (mn[i].t == MN_MAN && (ap == 'E' || ap == 'F')) {
1176 R_gl_draw_image(&mn_sgun[ap - 'E'], x, y, mn[i].d);
1178 R_gl_draw_image(&mn_spr[mn[i].t - 1][d], x, y, dir);
1179 if (mn[i].t == MN_MAN) {
1180 R_gl_set_color(MANCOLOR + PLAYER_COLOR_OFFSET);
1181 R_gl_draw_image_color(&mn_man_msk[d], x, y, dir);
1184 if (mn[i].t == MN_VILE && mn[i].st == SHOOT) {
1185 R_gl_draw_image(&mn_fspr[mn[i].ac / 3], mn[i].tx, mn[i].ty, 0);
1187 } else if (mn[i].t == MN_PL_DEAD || mn[i].t == MN_PL_MESS) {
1188 int type = mn[i].t - MN_PL_DEAD;
1189 R_gl_draw_image(&pl_spr[type], x, y, 0);
1190 R_gl_set_color(mn[i].d);
1191 R_gl_draw_image_color(&pl_msk[type], x, y, 0);
1197 static void R_draw_weapons (void) {
1198 enum {NONE, ROCKET, PLASMA, APLASMA, BALL1, BALL2, BALL7, BFGBALL, BFGHIT, MANF, REVF, FIRE}; // copypasted from weapons.c!
1199 int i, s, d, x, y;
1200 for (i = 0; i < MAXWPN; ++i) {
1201 s = -1;
1202 d = 0;
1203 switch (wp[i].t) {
1204 case REVF:
1205 case ROCKET:
1206 d = wp[i].s;
1207 if (d < 2) {
1208 d = wp[i].o.xv > 0 ? 1 : 0;
1209 x = abs(wp[i].o.xv);
1210 y = wp[i].o.yv;
1211 s = 0;
1212 if (y < 0) {
1213 if (-y >= x) {
1214 s = 30;
1216 } else if (y > 0) {
1217 if (y >= x / 2) {
1218 s = 31;
1221 } else {
1222 s = (d - 2) / 2 + 1;
1223 d = 0;
1225 break;
1226 case MANF:
1227 s=wp[i].s;
1228 if (s >= 2) {
1229 s /= 2;
1230 break;
1232 case PLASMA:
1233 case APLASMA:
1234 case BALL1:
1235 case BALL7:
1236 case BALL2:
1237 s = wp[i].s;
1238 if (s >= 2) {
1239 s = s / 2 + 1;
1241 switch (wp[i].t) {
1242 case PLASMA:
1243 s += 4;
1244 break;
1245 case APLASMA:
1246 s += 11;
1247 break;
1248 case BALL1:
1249 s += 32;
1250 break;
1251 case BALL2:
1252 s += 42;
1253 break;
1254 case BALL7:
1255 s += 37;
1256 d = wp[i].o.xv >= 0 ? 1 : 0;
1257 break;
1258 case MANF:
1259 s += 47;
1260 d= wp[i].o.xv>=0 ? 1 : 0;
1261 break;
1263 break;
1264 case BFGBALL:
1265 s = wp[i].s;
1266 if (s >= 2) {
1267 s = s / 2 + 1;
1269 s += 18;
1270 break;
1271 case BFGHIT:
1272 s = wp[i].s / 2 + 26;
1273 break;
1275 if (s >= 0) {
1276 R_gl_draw_image(&wp_spr[s * 2 + d], wp[i].o.x, wp[i].o.y, wp_sprd[s * 2 + d]);
1281 static void R_draw_smoke (void) {
1282 int i, s;
1283 for (i = 0; i < MAXSMOK; ++i) {
1284 if (sm[i].t) {
1285 switch (sm[i].s) {
1286 case 0:
1287 s = sm[i].t;
1288 if (s >= (SMSN - 1) * 3) {
1289 s = 0;
1290 } else {
1291 s = SMSN - 1 - s / 3;
1293 R_gl_draw_image(&smk_spr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1294 break;
1295 case 1:
1296 s = sm[i].t;
1297 if (s >= FLSN - 1) {
1298 s = 0;
1299 } else {
1300 s = FLSN - 1 - s;
1302 R_gl_draw_image(&smk_fspr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1303 break;
1309 static void R_draw_effects (void) {
1310 enum {NONE, TFOG, IFOG, BUBL}; // copypasted from fx.c
1311 int i, s;
1312 for (i = 0; i < MAXFX; ++i) {
1313 switch (fx[i].t) {
1314 case TFOG:
1315 s = fx[i].s / 2;
1316 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1317 break;
1318 case IFOG:
1319 s = fx[i].s / 2 + 10;
1320 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1321 break;
1322 case BUBL:
1323 glDisable(GL_BLEND);
1324 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1325 glDisable(GL_TEXTURE_2D);
1326 glBegin(GL_POINTS);
1327 R_gl_set_color(0xC0 + fx[i].s);
1328 glVertex2i(fx[i].x >> 8, (fx[i].y >> 8) + 1);
1329 glEnd();
1330 break;
1335 static int get_pu_st (int t) {
1336 if (t >= PL_FLASH) {
1337 return 1;
1338 } else if((t / 9) & 1) {
1339 return 0;
1340 } else {
1341 return 1;
1345 static void R_draw_view (int x, int y, int w, int h, int camx, int camy) {
1346 glPushMatrix();
1347 R_gl_setclip(x, y, w, h);
1348 glTranslatef(x, y, 0);
1349 if (w_horiz && horiz.n != NULL) {
1350 R_gl_draw_image_ext(&horiz, 0, 0, w, h);
1351 if (sky_type == 2 && lt_time < 0) {
1352 image *tanderbolt = &ltn[lt_type][lt_time < -5 ? 0 : 1];
1353 if (!lt_side) {
1354 R_gl_draw_image(tanderbolt, 0, lt_ypos, 0);
1355 } else {
1356 R_gl_draw_image(tanderbolt, w - 1, lt_ypos, 1);
1359 } else {
1360 glDisable(GL_BLEND);
1361 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1362 glDisable(GL_TEXTURE_2D);
1363 R_gl_set_color(DEFAULT_SKY_COLOR);
1364 R_gl_draw_quad(0, 0, w, h);
1366 int maxx = min((camx + w) / CELW + 1, FLDW);
1367 int maxy = min((camy + h) / CELH + 1, FLDH);
1368 int minx = max((camx - max_wall_width) / CELW, 0);
1369 int miny = max((camy - max_wall_height) / CELH, 0);
1370 glTranslatef(-camx, -camy, 0);
1371 R_draw_fld((byte*)fldb, minx, miny, maxx, maxy, 0);
1372 R_draw_dots();
1373 R_draw_items();
1374 R_draw_player(&pl1);
1375 if (_2pl) {
1376 R_draw_player(&pl2);
1378 R_draw_monsters();
1379 R_draw_weapons();
1380 R_draw_smoke();
1381 R_draw_effects();
1382 R_draw_fld((byte*)fldf, minx, miny, maxx, maxy, 1);
1383 glTranslatef(camx, camy, 0);
1384 if (sky_type == 2 && (lt_time == -4 || lt_time == -2)) {
1385 glColor4ub(255, 255, 255, 255);
1386 glEnable(GL_BLEND);
1387 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1388 glDisable(GL_TEXTURE_2D);
1389 R_gl_draw_quad(0, 0, w, h);
1391 glPopMatrix();
1394 static void R_draw_player_view (player_t *p, int x, int y, int w, int h) {
1395 p->looky = min(max(p->looky, -SCRH / 4), SCRH / 4); // TODO remove writeback
1396 int st = stone.w;
1397 int cw = w - st;
1398 int cx = min(max(p->o.x, cw / 2), FLDW * CELW - cw / 2);
1399 int cy = min(max(p->o.y - 12 + p->looky, h / 2), FLDH * CELH - h / 2);
1400 int camx = max(cx - cw / 2, 0);
1401 int camy = max(cy - h / 2, 0);
1402 glPushMatrix();
1403 R_draw_view(x, y + 1, cw, h - 2, camx, camy);
1404 glTranslatef(x, y, 0);
1405 if (p->invl) {
1406 if (get_pu_st(p->invl)) {
1407 glEnable(GL_BLEND);
1408 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1409 glDisable(GL_TEXTURE_2D);
1410 glColor4ub(191, 191, 191, 255);
1411 R_gl_draw_quad(0, 0, cw, h);
1413 } else {
1414 if (p->suit && get_pu_st(p->suit)) {
1415 glEnable(GL_BLEND);
1416 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1417 glDisable(GL_TEXTURE_2D);
1418 glColor4ub(0, 255, 0, 192);
1419 R_gl_draw_quad(0, 0, cw, h);
1421 int f = min(max(p->pain * 3, 0), 255);
1422 if (f > 0) {
1423 glEnable(GL_BLEND);
1424 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1425 glDisable(GL_TEXTURE_2D);
1426 glColor4ub(255, 0, 0, f);
1427 R_gl_draw_quad(0, 0, cw, h);
1430 R_gl_setclip(x, y, w, h);
1431 glTranslatef(-x + cw, 0, 0);
1432 R_gl_draw_image(&stone, 0, 0, 0);
1433 int i = stone.h;
1434 while (i < h) {
1435 R_gl_draw_image(&stone2, 0, i, 0);
1436 i += stone2.h;
1438 if (p->drawst & PL_DRAWAIR) {
1439 if (p->air < PL_AIR) {
1440 int a = min(max(p->air, 0), MAXAIR) * 100 / MAXAIR;
1441 glDisable(GL_BLEND);
1442 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1443 glDisable(GL_TEXTURE_2D);
1444 R_gl_set_color(0xC8);
1445 R_gl_draw_quad(10, 49, a, 2);
1448 if (p->drawst & PL_DRAWLIFE) {
1449 Z_gotoxy(10, 7);
1450 Z_printhf("%3d%%", p->life);
1452 if (p->drawst & PL_DRAWARMOR) {
1453 Z_gotoxy(10, 7 + 19);
1454 Z_printhf("%3d%%", p->armor);
1456 if (p->drawst & PL_DRAWWPN) {
1457 switch(p->wpn) {
1458 case 2:
1459 case 5:
1460 i = p->ammo;
1461 break;
1462 case 3:
1463 case 4:
1464 case 9:
1465 i = p->shel;
1466 break;
1467 case 6:
1468 i = p->rock;
1469 break;
1470 case 7:
1471 case 8:
1472 i = p->cell;
1473 break;
1474 case 10:
1475 i = p->fuel;
1476 break;
1477 default:
1478 i = -1;
1479 break;
1481 // weapon
1482 if (p->wpn >= 0) {
1483 R_gl_draw_image(&sth[12 + p->wpn], st - 88, 58 + 19, 0);
1485 // ammo
1486 if (p->wpn >= 2) {
1487 Z_gotoxy(st - 10 - 5 * 14, 58 + 2);
1488 Z_printhf("%5d", i);
1491 if (p->drawst & PL_DRAWFRAG && g_dm) {
1492 Z_gotoxy(st - 5 - 5 * 14, 77 + 5);
1493 Z_printhf("%5d", p->frag);
1495 if (p->drawst & PL_DRAWKEYS) {
1496 int x, k, n;
1497 for (k = p->keys >> 4, n = 0, x = st - 75; n < 3; n++, k >>= 1, x += 9) {
1498 if (k & 1) {
1499 R_gl_draw_image(&keys[n], x, 91, 0);
1503 if (p->drawst & PL_DRAWLIVES && !_2pl) {
1504 Z_gotoxy(st - 35, 17);
1505 Z_printhf("%d", p->lives);
1507 glPopMatrix();
1510 /* --- Game --- */
1512 static void pl_info (player_t *p, int x, int y) {
1513 dword t = p->kills * 10920 / g_time;
1514 Z_gotoxy(x + 25, y); Z_printbf("KILLS");
1515 Z_gotoxy(x + 25, y + 15); Z_printbf("KPM");
1516 Z_gotoxy(x + 25, y + 30); Z_printbf("SECRETS %u / %u", p->secrets, sw_secrets);
1517 Z_gotoxy(x + 255, y); Z_printbf("%u", p->kills);
1518 Z_gotoxy(x + 255, y + 15); Z_printbf("%u.%u", t / 10, t % 10);
1521 static void R_draw_intermission (void) {
1522 int cx = SCRW / 2;
1523 word hr, mn, sc, h;
1524 Z_gotoxy(cx - 14*12/2, 20);
1525 Z_printbf("LEVEL COMPLETE");
1526 Z_calc_time(g_time, &hr, &mn, &sc);
1527 Z_gotoxy(cx - 12*12/2, 40);
1528 Z_printbf("TIME %u:%02u:%02u", hr, mn, sc);
1529 h = 40 + SCRH / 10;
1530 if (_2pl) {
1531 Z_gotoxy(cx - 10*12/2, h);
1532 Z_printbf("PLAYER ONE");
1533 h += 20;
1535 pl_info(&pl1, cx - 160, h);
1536 if (_2pl) {
1537 h += 30 + SCRH / 10;
1538 Z_gotoxy(cx - 10*12/2, h);
1539 Z_printbf("PLAYER TWO");
1540 h += 20;
1541 pl_info(&pl2, cx - 160, h);
1545 static void W_act (void) {
1546 int i, a;
1547 if (g_time % 3 == 0) {
1548 for (i = 1; i < max_textures; i++) {
1549 a = walani[i];
1550 if (a != 0) {
1551 anic[a]++;
1552 if (anip[a][anic[a]].res == -1) {
1553 anic[a] = 0;
1555 walp[i] = anip[a][anic[a]];
1561 void R_draw (void) {
1562 W_act();
1563 glClearColor(0, 0, 0, 1);
1564 glClear(GL_COLOR_BUFFER_BIT);
1565 glEnable(GL_SCISSOR_TEST);
1566 R_gl_setmatrix();
1567 switch (g_st) {
1568 case GS_ENDANIM:
1569 case GS_END2ANIM:
1570 case GS_DARKEN:
1571 case GS_BVIDEO:
1572 case GS_EVIDEO:
1573 case GS_END3ANIM:
1574 break;
1575 case GS_TITLE:
1576 R_gl_draw_image_ext(&scrnh[0], 0, 0, SCRW, SCRH);
1577 break;
1578 case GS_INTER:
1579 R_gl_draw_image_ext(&scrnh[1], 0, 0, SCRW, SCRH);
1580 R_draw_intermission();
1581 break;
1582 case GS_ENDSCR:
1583 R_gl_draw_image_ext(&scrnh[2], 0, 0, SCRW, SCRH);
1584 break;
1585 case GS_GAME:
1586 if (_2pl) {
1587 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH / 2);
1588 R_draw_player_view(&pl2, 0, SCRH / 2, SCRW, SCRH / 2);
1589 } else {
1590 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH);
1592 R_gl_setclip(0, 0, SCRW, SCRH);
1593 break;
1595 GM_draw();
1596 Y_swap_buffers();
1599 static void R_alloc (void) {
1600 char s[10];
1601 int i, j, n;
1602 logo("R_alloc: load graphics\n");
1603 /* Game */
1604 scrnh[0] = R_gl_loadimage("TITLEPIC");
1605 scrnh[1] = R_gl_loadimage("INTERPIC");
1606 scrnh[2] = R_gl_loadimage("ENDPIC");
1607 for (i = 0; i < 2; i++) {
1608 sprintf(s, "LTN%c", '1' + i);
1609 for (j = 0; j < 2; j++) {
1610 ltn[i][j] = Z_getspr(s, j, 0, NULL);
1613 /* Smoke */
1614 for (i = 0; i < SMSN; i++) {
1615 smk_spr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_smoke_spr);
1617 for (i = 0; i < FLSN; i++) {
1618 smk_fspr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_flame_spr);
1620 /* Effects */
1621 for (i = 0; i < 10; i++) {
1622 fx_spr[i] = Z_getspr("TFOG", i, 0, fx_sprd + i);
1624 for (; i < 15; i++) {
1625 fx_spr[i] = Z_getspr("IFOG", i - 10, 0, fx_sprd + i);
1627 /* Weapons */
1628 for (i = 0; i < 4; i++) {
1629 wp_spr[i * 2] = Z_getspr("MISL", i, 1, wp_sprd + i * 2);
1630 wp_spr[i * 2 + 1] = Z_getspr("MISL", i, 2, wp_sprd + i * 2 + 1);
1632 for (; i < 6; i++) {
1633 wp_spr[i * 2] = Z_getspr("PLSS", i - 4, 1, wp_sprd + i * 2);
1634 wp_spr[i * 2 + 1] = Z_getspr("PLSS", i - 4, 2, wp_sprd + i * 2 + 1);
1636 for (; i < 11; i++) {
1637 wp_spr[i * 2] = Z_getspr("PLSE", i - 6, 1, wp_sprd + i * 2);
1638 wp_spr[i * 2 + 1] = Z_getspr("PLSE", i - 6, 2, wp_sprd + i * 2 + 1);
1640 for (; i < 13; i++) {
1641 wp_spr[i * 2] = Z_getspr("APLS", i - 11, 1, wp_sprd + i * 2);
1642 wp_spr[i * 2 + 1] = Z_getspr("APLS", i - 11, 2, wp_sprd + i * 2 + 1);
1644 for (; i < 18; i++) {
1645 wp_spr[i * 2] = Z_getspr("APBX", i - 13, 1, wp_sprd + i * 2);
1646 wp_spr[i * 2 + 1] = Z_getspr("APBX", i - 13, 2, wp_sprd + i * 2 + 1);
1648 for(; i < 20; i++) {
1649 wp_spr[i * 2] = Z_getspr("BFS1", i - 18, 1, wp_sprd + i * 2);
1650 wp_spr[i * 2 + 1] = Z_getspr("BFS1", i - 18, 2, wp_sprd + i * 2 + 1);
1652 for (; i < 26; i++) {
1653 wp_spr[i * 2] = Z_getspr("BFE1", i - 20, 1, wp_sprd + i * 2);
1654 wp_spr[i * 2 + 1] = Z_getspr("BFE1", i - 20, 2, wp_sprd + i * 2 + 1);
1656 for (; i < 30; i++) {
1657 wp_spr[i * 2] = Z_getspr("BFE2", i - 26, 1, wp_sprd + i * 2);
1658 wp_spr[i * 2 + 1] = Z_getspr("BFE2", i - 26, 2, wp_sprd + i * 2 + 1);
1660 for (; i < 32; i++) {
1661 wp_spr[i * 2] = Z_getspr("MISL", i - 30 + 4, 1, wp_sprd + i * 2);
1662 wp_spr[i * 2 + 1] = Z_getspr("MISL", i - 30 + 4, 2, wp_sprd + i * 2 + 1);
1664 for (; i < 37; i++) {
1665 wp_spr[i * 2] = Z_getspr("BAL1", i - 32, 1, wp_sprd + i * 2);
1666 wp_spr[i * 2 + 1] = Z_getspr("BAL1", i - 32, 2, wp_sprd + i * 2 + 1);
1668 for (; i < 42; i++) {
1669 wp_spr[i * 2] = Z_getspr("BAL7", i - 37, 1, wp_sprd + i * 2);
1670 wp_spr[i * 2 + 1] = Z_getspr("BAL7", i - 37, 2, wp_sprd + i * 2 + 1);
1672 for (; i < 47; i++) {
1673 wp_spr[i * 2] = Z_getspr("BAL2", i - 42, 1, wp_sprd + i * 2);
1674 wp_spr[i * 2 + 1] = Z_getspr("BAL2", i - 42, 2, wp_sprd + i * 2 + 1);
1676 for (; i < 49; i++) {
1677 wp_spr[i * 2] = Z_getspr("MANF", i - 47, 1, wp_sprd + i * 2);
1678 wp_spr[i * 2 + 1] = Z_getspr("MANF", i - 47, 2, wp_sprd + i * 2 + 1);
1680 /* Items */
1681 static const char snm[18][4] = {
1682 "CLIP", "SHEL", "ROCK", "CELL", "AMMO", "SBOX", "BROK", "CELP",
1683 "STIM", "MEDI", "BPAK",
1684 "CSAW", "SHOT", "SGN2", "MGUN", "LAUN", "PLAS", "BFUG"
1685 };
1686 static const char n4[4][4] = {
1687 "SOUL", "SMRT", "SMGT", "SMBT"
1688 };
1689 static const char n3[2][4] = {
1690 "GOR1", "FCAN"
1691 };
1692 for (i = 0; i < 18; i++) {
1693 item_spr[i] = Z_getspr(snm[i], 0, 0, item_sprd + i);
1695 for (; i < 20; i++) {
1696 item_spr[i] = Z_getspr("ARM1", i - 18, 0, item_sprd + i);
1697 item_spr[i + 2] = Z_getspr("ARM2", i - 18, 0, item_sprd + i);
1699 i+=2;
1700 for (; i < 26; i++) {
1701 item_spr[i] = Z_getspr("MEGA", i - 22, 0, item_sprd + i);
1703 for (; i < 30; i++) {
1704 item_spr[i] = Z_getspr("PINV", i - 26, 0, item_sprd + i);
1706 item_spr[30] = Z_getspr("AQUA", 0, 0, item_sprd + 30);
1707 item_spr[31] = Z_getspr("KEYR", 0, 0, item_sprd + 31);
1708 item_spr[32] = Z_getspr("KEYG", 0, 0, item_sprd + 32);
1709 item_spr[33] = Z_getspr("KEYB", 0, 0, item_sprd + 33);
1710 item_spr[34] = Z_getspr("SUIT", 0, 0, item_sprd + 34);
1711 for (n = 35, j = 0; j < 4; j++) {
1712 for (i = 0; i < 4; i++, n++) {
1713 item_spr[n] = Z_getspr(n4[j], i, 0, item_sprd + n);
1716 for (j = 0; j < 2; j++) {
1717 for (i = 0; i < 3; i++, n++) {
1718 item_spr[n] = Z_getspr(n3[j], i, 0, item_sprd + n);
1721 item_spr[57] = Z_getspr("GUN2", 0, 0, item_sprd + 57);
1722 /* Player */
1723 for (i = 0; i < 27; i++) {
1724 plr_spr[i * 2] = Z_getspr("PLAY", i, 1, plr_sprd + i * 2);
1725 plr_msk[i * 2] = R_gl_get_special_spr("PLAY", i, 1, &R_extract_mask_spr);
1726 plr_spr[i * 2 + 1] = Z_getspr("PLAY", i, 2, plr_sprd + i * 2 + 1);
1727 plr_msk[i * 2 + 1] = R_gl_get_special_spr("PLAY", i, 2, &R_extract_mask_spr);
1729 strncpy(s, "PWPx", 4);
1730 for (i = 1; i < 11; i++) {
1731 s[3] = (i < 10 ? '0' : 'A' - 10) + i;
1732 for (j = 0; j < 6; j++) {
1733 plr_wpn[i][j] = Z_getspr(s, j, 1, NULL);
1736 /* Monsters */
1737 static const char msn[MN_TN][4] = {
1738 "SARG", "TROO", "POSS", "SPOS", "CYBR", "CPOS", "BOSS", "BOS2", "HEAD", "SKUL",
1739 "PAIN", "SPID", "BSPI", "FATT", "SKEL", "VILE", "FISH", "BAR1", "ROBO", "PLAY"
1740 };
1741 static const int mms[MN_TN] = {
1742 14*2, 21*2, 21*2, 21*2, 16*2, 20*2, 15*2, 15*2, 12*2, 11*2,
1743 13*2, 19*2, 16*2, 20*2, 17*2, 29*2, 6*2, 2*2, 17*2, 23*2
1744 };
1745 mn_sgun[0] = Z_getspr("PWP4", 0, 1, NULL);
1746 mn_sgun[1] = Z_getspr("PWP4", 1, 1, NULL);
1747 for (j = 0; j < MN_TN; j++) {
1748 for (i = 0; i < mms[j]; i++) {
1749 mn_spr[j][i] = Z_getspr(msn[j], i / 2, (i & 1) + 1, &mn_sprd[j][i]);
1750 if (j == MN_MAN - 1) {
1751 mn_man_msk[i] = R_gl_get_special_spr(msn[j], i / 2, (i & 1) + 1, &R_extract_mask_spr);
1754 if (j == MN_BARREL - 1) {
1755 for (i = 4; i < 14; i++) {
1756 mn_spr[j][i] = Z_getspr("BEXP", i / 2 - 2, (i & 1) + 1, &mn_sprd[j][i]);
1760 for (i = 0; i < 8; i++) {
1761 mn_fspr[i] = Z_getspr("FIRE", i, 0, NULL);
1763 pl_spr[0] = Z_getspr("PLAY", 'N' - 'A', 0, NULL);
1764 pl_msk[0] = R_gl_get_special_spr("PLAY", 'N' - 'A', 0, &R_extract_mask_spr);
1765 pl_spr[1] = Z_getspr("PLAY", 'W' - 'A', 0, NULL);
1766 pl_msk[1] = R_gl_get_special_spr("PLAY", 'W' - 'A', 0, &R_extract_mask_spr);
1767 /* Misc */
1768 static const char mnm[22][8]={
1769 "STTNUM0", "STTNUM1", "STTNUM2", "STTNUM3", "STTNUM4",
1770 "STTNUM5", "STTNUM6", "STTNUM7", "STTNUM8", "STTNUM9",
1771 "STTMINUS", "STTPRCNT",
1772 "FISTA0", "CSAWA0", "PISTA0", "SHOTA0", "SGN2A0", "MGUNA0", "LAUNA0",
1773 "PLASA0", "BFUGA0", "GUN2A0"
1774 };
1775 stone = R_gl_loadimage("STONE");
1776 stone2 = R_gl_loadimage("STONE2");
1777 keys[0] = R_gl_loadimage("KEYRA0");
1778 keys[1] = R_gl_loadimage("KEYGA0");
1779 keys[2] = R_gl_loadimage("KEYBA0");
1780 for (i = 0; i < 22; i++) {
1781 sth[i] = R_gl_loadimage(mnm[i]);
1783 strcpy(s, "STBF_*");
1784 for (i = '!'; i < 160; i++) {
1785 s[5] = i;
1786 bfh[i - '!'] = R_gl_getimage(F_findres(s));
1788 for (i = '!'; i < 160; i++) {
1789 sprintf(s, "STCFN%03d", i);
1790 sfh[i - '!'] = R_gl_getimage(F_findres(s));
1792 strcpy(s, "WINUM*");
1793 for (i = '0'; i <= '9'; i++) {
1794 s[5] = i;
1795 bfh[i - '!'] = R_gl_loadimage(s);
1797 bfh[':' - '!'] = R_gl_loadimage("WICOLON");
1798 // menu
1799 msklh[0] = R_gl_loadimage("M_SKULL1");
1800 msklh[1] = R_gl_loadimage("M_SKULL2");
1801 mbarl = R_gl_loadimage("M_THERML");
1802 mbarm = R_gl_loadimage("M_THERMM");
1803 mbarr = R_gl_loadimage("M_THERMR");
1804 mbaro = R_gl_loadimage("M_THERMO");
1805 mslotl = R_gl_loadimage("M_LSLEFT");
1806 mslotm = R_gl_loadimage("M_LSCNTR");
1807 mslotr = R_gl_loadimage("M_LSRGHT");
1808 // walls
1809 for (i = 1; i < ANIT; i++) {
1810 for (j = 0; j < 5 && anm[i - 1][j]; j++) {
1811 anip[i][j] = R_gl_loadimage(anm[i - 1][j]);
1813 for(; j < 5; j++) {
1814 anip[i][j] = (image) {
1815 .n = NULL,
1816 .w = 8,
1817 .h = 8,
1818 .res = -1,
1819 };
1824 static void R_reload_textures (void);
1826 void R_set_videomode (int w, int h, int fullscreen) {
1827 assert(w > 0);
1828 assert(h > 0);
1829 int was = Y_videomode_setted();
1830 if (root != NULL) {
1831 R_cache_free(root, 0);
1832 root = NULL;
1834 int res = Y_set_videomode_opengl(w, h, fullscreen);
1835 if (res == 0) {
1836 if (was == 0) {
1837 ERR_failinit("Unable to set video mode\n");
1840 Y_get_videomode(&SCRW, &SCRH);
1841 root = R_cache_new();
1842 assert(root);
1843 R_alloc();
1844 R_reload_textures();
1847 static int R_video_menu_handler (new_msg_t *msg, const new_menu_t *m, void *data) {
1848 const videomode_t *v;
1849 intptr_t i = (intptr_t)data;
1850 static int w, h;
1851 static int vmode;
1852 static int fullscreen;
1853 static char str[16];
1854 switch (i) {
1855 case -1:
1856 switch (msg->type) {
1857 case GM_ENTER:
1858 Y_get_videomode(&w, &h);
1859 fullscreen = Y_get_fullscreen();
1860 v = Y_get_videomode_list_opengl(fullscreen);
1861 vmode = 0;
1862 while (vmode < v->n && v->modes[vmode].w != w && v->modes[vmode].h != h) {
1863 vmode += 1;
1865 if (vmode < v->n) {
1866 w = v->modes[vmode].w;
1867 h = v->modes[vmode].h;
1869 snprintf(str, 16, "%ix%i", w, h);
1870 return 1;
1872 break;
1873 case 0:
1874 switch (msg->type) {
1875 case GM_SELECT:
1876 v = Y_get_videomode_list_opengl(fullscreen);
1877 vmode = vmode + 1 >= v->n ? 0 : vmode + 1;
1878 if (v->n > 0) {
1879 w = v->modes[vmode].w;
1880 h = v->modes[vmode].h;
1881 } else {
1882 Y_get_videomode(&w, &h);
1884 snprintf(str, 16, "%ix%i", w, h);
1885 return 1;
1886 case GM_GETSTR:
1887 return GM_init_str(msg, str, 16);
1889 break;
1890 case 1:
1891 switch (msg->type) {
1892 case GM_SELECT: fullscreen = !fullscreen; return 1;
1893 case GM_GETSTR: return GM_init_str(msg, fullscreen ? "Yes" : "No", 3);
1895 break;
1896 case 2:
1897 switch (msg->type) {
1898 case GM_SELECT: R_set_videomode(w, h, fullscreen); return 1;
1901 return 0;
1904 static const new_menu_t video_menu = {
1905 GM_BIG, "Video", (void*)-1, &R_video_menu_handler,
1907 { GM_BUTTON, "Videomode: ", (void*)0, &R_video_menu_handler, NULL },
1908 { GM_BUTTON, "Fullscreen: ", (void*)1, &R_video_menu_handler, NULL },
1909 { GM_BUTTON, "Apply", (void*)2, &R_video_menu_handler, NULL },
1910 { 0, NULL, NULL, NULL, NULL } // end
1912 };
1914 const new_menu_t *R_menu (void) {
1915 return &video_menu;
1918 void R_init (void) {
1919 logo("R_init: intialize opengl render\n");
1920 R_init_playpal();
1921 R_set_videomode(SCRW, SCRH, 0);
1924 void R_done (void) {
1925 R_cache_free(root, 1);
1926 Y_unset_videomode();
1927 root = NULL;
1930 void R_get_name (int n, char s[8]) {
1931 assert(n >= 0 && n < 256);
1932 if (walp[n].res == -1) {
1933 memset(s, 0, 8);
1934 } else if (walp[n].res == -2) {
1935 memcpy(s, "_WATER_", 8);
1936 s[7] = '0' + (intptr_t)walp[n].n - 1;
1937 } else if (walani[n] > 0) {
1938 memcpy(s, anm[walani[n] - 1][0], 8);
1939 } else {
1940 F_getresname(s, walp[n].res & 0x7FFF);
1944 static short getani (char n[8]) {
1945 short i = 0;
1946 while (i < ANIT - 1 && strncasecmp(n, anm[i][0], 8) != 0) {
1947 i++;
1949 return i < ANIT - 1 ? i + 1 : 0;
1952 int R_get_special_id (int n) {
1953 assert(n >= 0 && n <= 256);
1954 return walp[n].res == -2 ? (intptr_t)walp[n].n : -1;
1957 static void R_reload_textures (void) {
1958 int i;
1959 char s[8];
1960 for (i = 0; i < max_textures; i++) {
1961 R_get_name(i, s);
1962 if (walp[i].res >= 0) {
1963 walp[i] = R_gl_getimage(walp[i].res);
1966 if (horiz.n) {
1967 horiz = R_gl_getimage(horiz.res);
1971 void R_begin_load (void) {
1972 int i;
1973 for (i = 0; i < 256; i++) {
1974 if (walp[i].n != NULL && walp[i].res >= 0 && walani[i] == 0) {
1975 R_gl_free_image(&walp[i]);
1977 memset(&walp[i], 0, sizeof(image));
1978 walp[i].res = -1;
1979 walswp[i] = i;
1980 walani[i] = 0;
1982 memset(anic, 0, sizeof(anic));
1983 max_wall_width = 0;
1984 max_wall_height = 0;
1985 max_textures = 1;
1988 void R_load (char s[8], int f) {
1989 assert(max_textures < 256);
1990 if (!s[0]) {
1991 walp[max_textures] = (image) {
1992 .n = NULL,
1993 .x = 0,
1994 .y = 0,
1995 .w = 0,
1996 .h = 0,
1997 .res = -1,
1998 };
1999 } else if (strncasecmp(s, "_WATER_", 7) == 0) {
2000 walp[max_textures] = (image) {
2001 .n = (void*)((intptr_t)s[7] - '0' + 1),
2002 .x = 0,
2003 .y = 0,
2004 .w = 8,
2005 .h = 8,
2006 .res = -2,
2007 };
2008 } else {
2009 walp[max_textures] = R_gl_loadimage(s);
2010 if (f) {
2011 walp[max_textures].res |= 0x8000;
2013 if (s[0] == 'S' && s[1] == 'W' && s[4] == '_') {
2014 walswp[max_textures] = 0;
2016 walani[max_textures] = getani(s);
2018 max_wall_width = max(max_wall_width, walp[max_textures].w);
2019 max_wall_height = max(max_wall_height, walp[max_textures].h);
2020 max_textures++;
2023 void R_end_load (void) {
2024 int i, j, k, g;
2025 char s[8];
2026 j = max_textures;
2027 for (i = 1; i < 256 && j < 256; i++) {
2028 if (walswp[i] == 0) {
2029 R_get_name(i, s);
2030 s[5] ^= 1;
2031 g = F_getresid(s) | (walp[i].res & 0x8000);
2032 k = 1;
2033 while (k < 256 && walp[k].res != g) {
2034 k += 1;
2036 if (k >= 256) {
2037 k = j;
2038 j += 1;
2039 max_textures += 1;
2040 walp[k] = R_gl_getimage(g);
2041 walf[k] = g & 0x8000 ? 1 : 0;
2043 walswp[i] = k;
2044 walswp[k] = i;
2049 void R_loadsky (int sky) {
2050 char s[6];
2051 strcpy(s, "RSKYx");
2052 s[4] = '0' + sky;
2053 R_gl_free_image(&horiz);
2054 horiz = R_gl_loadimage(s);