DEADSOFTWARE

menu: improve menu drawing
[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 #ifdef __APPLE__
26 # include <OpenGL/gl.h>
27 #else
28 # include <GL/gl.h>
29 #endif
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <assert.h>
36 #define VGA_TRANSPARENT_COLOR 0
37 #define DEFAULT_SKY_COLOR 0x97
38 #define MANCOLOR 0xD0
39 #define PLAYER_COLOR_OFFSET 7
40 #define MAXAIR 1091
41 #define ANIT 5
42 #define PL_FLASH 90
44 #pragma pack(1)
45 typedef struct vgaimg {
46 word w, h;
47 short x, y;
48 byte data[];
49 } vgaimg;
51 typedef struct rgb {
52 byte r, g, b;
53 } rgb;
55 typedef struct rgba {
56 byte r, g, b, a;
57 } rgba;
58 #pragma pack()
60 typedef struct node {
61 struct cache *base;
62 struct node *left, *right;
63 struct node *up;
64 int l, t, r, b;
65 int leaf;
66 } node;
68 typedef struct cache {
69 struct cache *next;
70 struct node root;
71 GLuint id;
72 } cache;
74 typedef struct image {
75 node *n;
76 GLint x, y;
77 GLuint w, h;
78 int res;
79 } image;
81 /* Render Specific */
82 int SCRW = 320; // public
83 int SCRH = 200; // public
84 static int gamma;
85 static int fullscreen;
86 static rgb playpal[256];
87 static byte bright[256];
88 static GLuint lastTexture;
89 static cache *root;
91 /* Game */
92 static image scrnh[3]; // TITLEPIC INTERPIC ENDPIC
93 static image ltn[2][2];
95 /* Smoke */
96 static image smk_spr[SMSN];
97 static image smk_fspr[FLSN];
99 /* Effects */
100 static image fx_spr[15];
101 static char fx_sprd[15];
103 /* Weapons */
104 static image wp_spr[49*2];
105 static char wp_sprd[49*2];
107 /* Items */
108 static image item_spr[58];
109 static char item_sprd[58];
111 /* Player */
112 static image plr_spr[27*2];
113 static image plr_msk[27*2];
114 static char plr_sprd[27*2];
115 static image plr_wpn[11][6];
117 /* Monsters */
118 static image pl_spr[2];
119 static image pl_msk[2];
120 static image mn_spr[MN_TN][29*2];
121 static image mn_man_msk[29*2];
122 static char mn_sprd[MN_TN][29*2];
123 static image mn_fspr[8];
124 static image mn_sgun[2];
126 /* Misc */
127 static image sth[22];
128 static image bfh[160 - '!'];
129 static image sfh[160 - '!'];
130 static image stone;
131 static image stone2;
132 static image keys[3];
133 static int prx = 0;
134 static int pry = 0;
136 /* Menu */
137 static int gm_tm;
138 static image msklh[2];
139 static image mbarl;
140 static image mbarm;
141 static image mbarr;
142 static image mbaro;
143 static image mslotl;
144 static image mslotm;
145 static image mslotr;
147 /* Map */
148 static const char *anm[ANIT - 1][5] = {
149 {"WALL22_1", "WALL23_1", "WALL23_2", NULL, NULL},
150 {"WALL58_1", "WALL58_2", "WALL58_3", NULL, NULL},
151 {"W73A_1", "W73A_2", NULL, NULL, NULL},
152 {"RP2_1", "RP2_2", "RP2_3", "RP2_4", NULL}
153 };
154 static int max_wall_width;
155 static int max_wall_height;
156 static int max_textures;
157 static image walp[256];
158 static byte walani[256];
159 static image anip[ANIT][5];
160 static byte anic[ANIT];
161 static image horiz;
163 /* Texture cache */
165 // https://blackpawn.com/texts/lightmaps/
166 static node *R_node_alloc (node *p, int w, int h) {
167 assert(p);
168 assert(w > 0);
169 assert(h > 0);
170 if (p->left) {
171 assert(p->right);
172 node *n = R_node_alloc(p->left, w, h);
173 return n ? n : R_node_alloc(p->right, w, h);
174 } else {
175 int pw = p->r - p->l + 1;
176 int ph = p->b - p->t + 1;
177 if (p->leaf || pw < w || ph < h) {
178 return NULL;
179 } else if (pw == w && ph == h) {
180 p->leaf = 1;
181 return p;
182 } else {
183 p->left = malloc(sizeof(node));
184 p->right = malloc(sizeof(node));
185 if (pw - w > ph - h) {
186 *p->left = (node) {
187 .up = p,
188 .l = p->l,
189 .t = p->t,
190 .r = p->l + w - 1,
191 .b = p->b
192 };
193 *p->right = (node) {
194 .up = p,
195 .l = p->l + w,
196 .t = p->t,
197 .r = p->r,
198 .b = p->b
199 };
200 } else {
201 *p->left = (node) {
202 .up = p,
203 .l = p->l,
204 .t = p->t,
205 .r = p->r,
206 .b = p->t + h - 1
207 };
208 *p->right = (node) {
209 .up = p,
210 .l = p->l,
211 .t = p->t + h,
212 .r = p->r,
213 .b = p->b
214 };
216 return R_node_alloc(p->left, w, h);
221 static int R_node_have_leaf (node *n) {
222 return n && (n->leaf || R_node_have_leaf(n->left) || R_node_have_leaf(n->right));
225 static void R_node_free_recursive (node *n) {
226 if (n) {
227 R_node_free_recursive(n->left);
228 R_node_free_recursive(n->right);
229 free(n);
233 static void R_node_free (node *n) {
234 if (n) {
235 //logo("free node %p {%i:%i:%i:%i}\n", n, n->l, n->t, n->r, n->b);
236 assert(n->leaf);
237 assert(n->left == NULL);
238 assert(n->right == NULL);
239 n->leaf = 0;
240 n->base = NULL;
241 node *p = n->up;
242 while (p != NULL) {
243 assert(p->leaf == 0);
244 assert(p->left);
245 assert(p->right);
246 if (R_node_have_leaf(p) == 0) {
247 R_node_free_recursive(p->left);
248 p->left = NULL;
249 R_node_free_recursive(p->right);
250 p->right = NULL;
251 p = p->up;
252 } else {
253 p = NULL;
259 static void R_cache_get_max_texture_size (int *w, int *h) {
260 GLint size = 0;
261 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
262 size = min(max(size, 0), 512); // more can be buggy on older hardware
263 *w = size;
264 *h = size;
267 static void R_gl_bind_texture (GLuint id) {
268 if (id != lastTexture) {
269 glBindTexture(GL_TEXTURE_2D, id);
273 static cache *R_cache_new (void) {
274 int w, h;
275 GLuint id;
276 cache *c = NULL;
277 R_cache_get_max_texture_size(&w, &h);
278 if (w && h) {
279 glGenTextures(1, &id);
280 if (id) {
281 R_gl_bind_texture(id);
282 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
283 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
284 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
285 c = malloc(sizeof(cache));
286 if (c != NULL) {
287 *c = (cache) {
288 .id = id,
289 .root.r = w - 1,
290 .root.b = h - 1
291 };
292 } else {
293 glDeleteTextures(1, &id);
297 //logo("new cache %p\n", c);
298 return c;
301 static void R_cache_free (cache *root, int freetexture) {
302 cache *next;
303 cache *c = root;
304 while (c != NULL) {
305 next = c->next;
306 R_node_free_recursive(c->root.left);
307 R_node_free_recursive(c->root.right);
308 if (freetexture && c->id != 0) {
309 glDeleteTextures(1, &c->id);
311 free(c);
312 c = next;
316 static node *R_cache_alloc (cache *root, int w, int h) {
317 assert(root);
318 assert(w > 0 && h > 0);
319 node *n = NULL;
320 cache *p = NULL;
321 cache *c = root;
322 int maxw, maxh;
323 R_cache_get_max_texture_size(&maxw, &maxh);
324 if (w <= maxw && h <= maxh) {
325 while (c && !n) {
326 n = R_node_alloc(&c->root, w, h);
327 if (n) {
328 assert(n->leaf);
329 n->base = c;
331 p = c;
332 c = c->next;
334 if (!n) {
335 c = R_cache_new();
336 if (c) {
337 p->next = c;
338 n = R_node_alloc(&c->root, w, h);
339 if (n) {
340 assert(n->leaf);
341 n->base = c;
346 if (n) {
347 //logo("new node %p {%i:%i:%i:%i}\n", n, n->l, n->t, n->r, n->b);
348 } else {
349 logo("new node failed {%i:%i}\n", w, h);
351 return n;
354 static void R_cache_update (node *n, const void *data, int x, int y, int w, int h) {
355 assert(n);
356 assert(n->leaf);
357 assert(n->base);
358 assert(data);
359 assert(x >= 0);
360 assert(y >= 0);
361 assert(n->l + x + w - 1 <= n->r);
362 assert(n->t + y + h - 1 <= n->b);
363 R_gl_bind_texture(n->base->id);
364 glTexSubImage2D(GL_TEXTURE_2D, 0, n->l + x, n->t + y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
367 /* Generic helpers */
369 static void R_init_playpal (void) {
370 int i;
371 byte *vgapal = M_lock(F_getresid("PLAYPAL"));
372 for (i = 0; i < 256; i++) {
373 playpal[i] = (rgb) {
374 .r = vgapal[i * 3 + 0] * 255 / 63,
375 .g = vgapal[i * 3 + 1] * 255 / 63,
376 .b = vgapal[i * 3 + 2] * 255 / 63,
377 };
378 bright[i] = ((int)vgapal[i * 3 + 0] + vgapal[i * 3 + 1] + vgapal[i * 3 + 2]) * 8 / (63 * 3);
380 M_unlock(vgapal);
383 static vgaimg *R_getvga (int id) {
384 int loaded = M_was_locked(id);
385 vgaimg *v = M_lock(id);
386 if (v != NULL && !loaded) {
387 v->w = short2host(v->w);
388 v->h = short2host(v->h);
389 v->x = short2host(v->x);
390 v->y = short2host(v->y);
392 return v;
395 static rgba *R_extract_flame_spr (vgaimg *v) {
396 static const byte flametab[16] = {
397 0xBC, 0xBA, 0xB8, 0xB6, 0xB4, 0xB2, 0xB0, 0xD5,
398 0xD6, 0xD7, 0xA1, 0xA0, 0xE3, 0xE2, 0xE1, 0xE0
399 };
400 int i, j;
401 rgba *s = malloc(v->w * v->h * sizeof(rgba));
402 if (s != NULL) {
403 for (j = 0; j < v->h; j++) {
404 for (i = 0; i < v->w; i++) {
405 int k = j * v->w + i;
406 byte c = v->data[k] + bright[DEFAULT_SKY_COLOR];
407 s[k] = (rgba) {
408 .r = playpal[flametab[c]].r,
409 .g = playpal[flametab[c]].g,
410 .b = playpal[flametab[c]].b,
411 .a = v->data[k] == VGA_TRANSPARENT_COLOR ? 0x00 : 0xFF,
412 };
416 return s;
419 static rgba *R_extract_smoke_spr (vgaimg *v) {
420 int i, j;
421 rgba *s = malloc(v->w * v->h * sizeof(rgba));
422 if (s != NULL) {
423 for (j = 0; j < v->h; j++) {
424 for (i = 0; i < v->w; i++) {
425 int k = j * v->w + i;
426 byte c = ((v->data[k] + bright[DEFAULT_SKY_COLOR]) + 0x60) ^ 0x0F;
427 byte a = 0xFF - ((int)playpal[c].r + playpal[c].g + playpal[c].b) / 3;
428 s[k] = (rgba) {
429 .r = playpal[c].r,
430 .g = playpal[c].g,
431 .b = playpal[c].b,
432 .a = v->data[k] == VGA_TRANSPARENT_COLOR ? 0x00 : a,
433 };
437 return s;
440 static rgba *R_extract_mask_spr (vgaimg *v) {
441 int i, j;
442 rgba *s = malloc(v->w * v->h * sizeof(rgba));
443 if (s != NULL) {
444 for (j = 0; j < v->h; j++) {
445 for (i = 0; i < v->w; i++) {
446 int k = j * v->w + i;
447 byte c = v->data[k];
448 if (c >= 0x70 && c <= 0x7F) {
449 byte mask = c - 0x70;
450 mask = 0xFF - ((mask << 4) | mask);
451 s[k] = (rgba) {
452 .r = mask,
453 .g = mask,
454 .b = mask,
455 .a = 0xFF,
456 };
457 } else {
458 s[k] = (rgba) {
459 .r = 0,
460 .g = 0,
461 .b = 0,
462 .a = 0,
463 };
468 return s;
471 static rgba *R_extract_rgba_spr (vgaimg *v) {
472 int i, j;
473 rgba *s = malloc(v->w * v->h * sizeof(rgba));
474 if (s != NULL) {
475 for (j = 0; j < v->h; j++) {
476 for (i = 0; i < v->w; i++) {
477 int k = j * v->w + i;
478 byte c = v->data[k];
479 s[k] = (rgba) {
480 .r = playpal[c].r,
481 .g = playpal[c].g,
482 .b = playpal[c].b,
483 .a = c == VGA_TRANSPARENT_COLOR ? 0x00 : 0xFF,
484 };
488 return s;
491 /* OpenGL helpers */
493 static image R_gl_create_image (const rgba *buf, int w, int h) {
494 node *n = R_cache_alloc(root, w, h);
495 if (n) {
496 R_cache_update(n, buf, 0, 0, w, h);
498 return (image) {
499 .n = n,
500 .w = w,
501 .h = h,
502 .res = -1
503 };
506 static image R_gl_get_special_image (int id, rgba *(*fn)(vgaimg*)) {
507 image img;
508 //char name[8];
509 //F_getresname(name, id);
510 //logo("load image: %.8s\n", name);
511 vgaimg *v = R_getvga(id);
512 if (v != NULL) {
513 rgba *buf = (*fn)(v);
514 img = R_gl_create_image(buf, v->w, v->h);
515 img.x = v->x;
516 img.y = v->y;
517 img.res = id;
518 M_unlock(v);
519 free(buf);
520 } else {
521 img = (image) {
522 .res = id
523 };
525 return img;
528 static image R_gl_getimage (int id) {
529 return R_gl_get_special_image(id, &R_extract_rgba_spr);
532 static image R_gl_loadimage (const char name[8]) {
533 return R_gl_getimage(F_getresid(name));
536 static image R_gl_get_special_spr (const char n[4], int s, int d, rgba *(*fn)(vgaimg*)) {
537 return R_gl_get_special_image(F_getsprid(n, s, d), fn);
540 static void R_gl_free_image (image *img) {
541 if (img->n != NULL && img->res >= 0) {
542 R_node_free(img->n);
544 img->n = NULL;
545 img->res = -1;
548 static void R_gl_draw_quad (int x, int y, int w, int h) {
549 glBegin(GL_QUADS);
550 glVertex2i(x + w, y);
551 glVertex2i(x, y);
552 glVertex2i(x, y + h);
553 glVertex2i(x + w, y + h);
554 glEnd();
557 static void R_gl_draw_textured (image *img, int x, int y, int w, int h, int flip) {
558 if (img->n) {
559 GLfloat nw = img->n->base->root.r + 1;
560 GLfloat nh = img->n->base->root.b + 1;
561 GLfloat ax = (flip ? img->n->l : img->n->r + 1) / nw;
562 GLfloat bx = (flip ? img->n->r + 1 : img->n->l) / nh;
563 GLfloat ay = (img->n->t) / nw;
564 GLfloat by = (img->n->b + 1) / nh;
565 R_gl_bind_texture(img->n->base->id);
566 glEnable(GL_TEXTURE_2D);
567 glBegin(GL_QUADS);
568 glTexCoord2f(ax, ay); glVertex2i(x + w, y);
569 glTexCoord2f(bx, ay); glVertex2i(x, y);
570 glTexCoord2f(bx, by); glVertex2i(x, y + h);
571 glTexCoord2f(ax, by); glVertex2i(x + w, y + h);
572 glEnd();
573 } else {
574 glColor3ub(255, 0, 0);
575 glDisable(GL_BLEND);
576 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
577 glDisable(GL_TEXTURE_2D);
578 R_gl_draw_quad(x, y, w, h);
582 /* fit image into rectangle without applying offset and transparency */
583 static void R_gl_draw_image_ext (image *img, int x, int y, int w, int h) {
584 glDisable(GL_BLEND);
585 glColor3ub(255, 255, 255);
586 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
587 R_gl_draw_textured(img, x, y, w, h, 0);
590 /* draw sprite with offset and coloring */
591 static void R_gl_draw_image_color (image *img, int x, int y, int flip) {
592 int xx = flip ? x - img->w + img->x : x - img->x;
593 glEnable(GL_BLEND);
594 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
595 R_gl_draw_textured(img, xx, y - img->y, img->w, img->h, flip);
598 /* draw sprite with offset */
599 static void R_gl_draw_image (image *img, int x, int y, int flip) {
600 glColor3ub(255, 255, 255);
601 R_gl_draw_image_color(img, x, y, flip);
604 static void R_gl_set_color (byte c) {
605 glColor3ub(playpal[c].r, playpal[c].g, playpal[c].b);
608 static void R_gl_setclip (int x, int y, int w, int h) {
609 glScissor(x, SCRH - h - y, w, h);
612 static void R_gl_setmatrix (void) {
613 glScissor(0, 0, SCRW, SCRH);
614 glViewport(0, 0, SCRW, SCRH);
615 glMatrixMode(GL_PROJECTION);
616 glLoadIdentity();
617 glOrtho(0, SCRW, SCRH, 0, 0, 1);
618 glMatrixMode(GL_MODELVIEW);
619 glLoadIdentity();
622 /* --- Misc --- */
624 static image Z_getspr (const char n[4], int s, int d, char *dir) {
625 int h = F_getsprid(n, s, d);
626 if (dir != NULL) {
627 *dir = (h & 0x8000) ? 1 : 0;
629 return R_gl_getimage(h);
632 static image *Z_get_char_image (image *img, int ch) {
633 image *p = NULL;
634 if (ch > 32 && ch < 160) {
635 p = &img[ch - '!'];
636 if (p->n == NULL) {
637 p = &img[toupper(ch) - '!'];
640 return p;
643 static int Z_get_char_width_generic (image *img, int off, int ch) {
644 image *p = Z_get_char_image(img, ch);
645 return p == NULL ? off : p->w - 1;
648 static int Z_putch_generic (image *img, int off, int ch) {
649 image *p = Z_get_char_image(img, ch);
650 int w = p == NULL ? off : p->w - 1;
651 if (p != NULL && p->n != NULL) {
652 R_gl_draw_image(p, prx, pry, 0);
654 prx += w;
655 return w;
658 static int Z_get_string_width_generic (image *img, int off, const char *fmt, va_list ap) {
659 int i, w, ww;
660 char buf[80];
661 vsprintf(buf, fmt, ap);
662 for (i = w = ww = 0; buf[i]; ++i) {
663 switch (buf[i]) {
664 case '\n':
665 case '\r':
666 ww = max(w, ww);
667 w = 0;
668 break;
669 default:
670 w += Z_get_char_width_generic(img, off, (byte)buf[i]);
673 return max(w, ww);
676 static int Z_printf_generic (image *img, int off, const char *fmt, va_list ap) {
677 int i, w, ww;
678 char buf[80];
679 vsprintf(buf, fmt, ap);
680 for (i = w = ww = 0; buf[i]; ++i) {
681 switch (buf[i]) {
682 case '\n':
683 pry += off + 1;
684 case '\r':
685 w = max(w, ww);
686 prx = 0;
687 w = 0;
688 break;
689 default:
690 w += Z_putch_generic(img, off, (byte)buf[i]);
693 return w;
696 static void Z_gotoxy (int x, int y) {
697 prx = x;
698 pry = y;
701 static int Z_get_big_string_width (const char *fmt, ...) {
702 va_list a;
703 va_start(a, fmt);
704 int w = Z_get_string_width_generic(bfh, 12, fmt, a);
705 va_end(a);
706 return w;
709 static int Z_printbf (const char *fmt, ...) {
710 va_list a;
711 va_start(a, fmt);
712 int w = Z_printf_generic(bfh, 12, fmt, a);
713 va_end(a);
714 return w;
717 static int Z_get_small_string_width (const char *fmt, ...) {
718 va_list a;
719 va_start(a, fmt);
720 int w = Z_get_string_width_generic(sfh, 7, fmt, a);
721 va_end(a);
722 return w;
725 static int Z_printsf (const char *fmt, ...) {
726 va_list a;
727 va_start(a, fmt);
728 int w =Z_printf_generic(sfh, 7, fmt, a);
729 va_end(a);
730 return w;
733 static void Z_printhf (const char *fmt, ...) {
734 int i, c;
735 char buf[80];
736 va_list a;
737 va_start(a, fmt);
738 vsprintf(buf, fmt, a);
739 va_end(a);
740 for (i = 0; buf[i]; ++i) {
741 switch (buf[i]) {
742 case '0':
743 case '1':
744 case '2':
745 case '3':
746 case '4':
747 case '5':
748 case '6':
749 case '7':
750 case '8':
751 case '9':
752 c = buf[i] - '0';
753 break;
754 case '-':
755 c = 10;
756 break;
757 case '%':
758 c = 11;
759 break;
760 case '\n':
761 pry += 19;
762 case '\r':
763 c = -1;
764 prx = 0;
765 break;
766 default:
767 c = -1;
768 break;
770 if (c >= 0) {
771 R_gl_draw_image(&sth[c], prx, pry, 0);
773 prx += 14;
777 /* --- Menu --- */
779 static image *PL_getspr (int s, int d, int msk) {
780 int i = (s - 'A') * 2 + d;
781 return msk ? &plr_msk[i] : &plr_spr[i];
784 static int count_menu_entries (const new_menu_t *m) {
785 assert(m != NULL);
786 int i = 0;
787 while (m->entries[i].type != 0) {
788 i += 1;
790 return i;
793 #define SCROLLER_MIDDLE 10
794 #define TEXTFIELD_MIDDLE 2
796 static void get_entry_size (const new_menu_t *m, int i, int *w, int *h) {
797 assert(m != NULL);
798 assert(i >= 0);
799 assert(w != NULL);
800 assert(h != NULL);
801 int x, y;
802 new_msg_t msg;
803 switch (m->entries[i].type) {
804 case GM_BUTTON:
805 case GM_SCROLLER:
806 case GM_TEXTFIELD:
807 case GM_TEXTFIELD_BUTTON:
808 x = Z_get_big_string_width("%s", m->entries[i].caption);
809 break;
810 case GM_SMALL_BUTTON:
811 x = Z_get_small_string_width("%s", m->entries[i].caption);
812 break;
813 default:
814 assert(0);
816 switch (m->entries[i].type) {
817 case GM_BUTTON:
818 msg.type = GM_GETSTR;
819 if (GM_send(m, i, &msg)) {
820 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
822 y = 16;
823 break;
824 case GM_SMALL_BUTTON:
825 msg.type = GM_GETSTR;
826 if (GM_send(m, i, &msg)) {
827 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
829 y = 12;
830 break;
831 case GM_SCROLLER:
832 x += (SCROLLER_MIDDLE + 2) * 8;
833 y = 16;
834 break;
835 case GM_TEXTFIELD:
836 case GM_TEXTFIELD_BUTTON:
837 msg.type = GM_GETSTR;
838 if (GM_send(m, i, &msg)) {
839 x += (msg.string.maxlen + 2) * 8;
840 } else {
841 x += (TEXTFIELD_MIDDLE + 2) * 8;
843 y = 16;
844 break;
845 default:
846 assert(0);
848 *w = x;
849 *h = y;
852 static void get_menu_size (const new_menu_t *m, int *w, int *h) {
853 assert(m != NULL);
854 assert(w != NULL);
855 assert(h != NULL);
856 int i, x, y, xx, yy;
857 int n = count_menu_entries(m);
858 switch (m->type) {
859 case GM_BIG: x = Z_get_big_string_width("%s", m->title); break;
860 case GM_SMALL: x = Z_get_small_string_width("%s", m->title); break;
861 default: assert(0);
863 for (i = 0; i < n; i++) {
864 get_entry_size(m, i, &xx, &yy);
865 x = max(x, xx);
866 y += yy;
868 *w = x;
869 *h = y;
872 static int GM_draw (void) {
873 int i, j, xoff, yoff, n, x, y, cur, curoff, w, recv;
874 const new_menu_t *m = GM_get();
875 new_msg_t msg;
876 if (m != NULL) {
877 get_menu_size(m, &x, &y);
878 x = SCRW / 2 - x / 2;
879 y = SCRH / 2 - y / 2;
880 // --- title ---
881 switch (m->type) {
882 case GM_BIG:
883 yoff = 20;
884 Z_gotoxy(x, y - 10);
885 Z_printbf("%s", m->title);
886 break;
887 case GM_SMALL:
888 yoff = 8;
889 Z_gotoxy(x, y - 10);
890 Z_printsf("%s", m->title);
891 break;
892 default:
893 assert(0);
895 // --- entries ---
896 curoff = yoff;
897 cur = GM_geti();
898 n = count_menu_entries(m);
899 for (i = 0; i < n; i++) {
900 if (i == cur) {
901 curoff = yoff;
902 if (m->entries[cur].type == GM_SMALL_BUTTON) {
903 Z_gotoxy(x - 8, y + curoff);
904 Z_printsf(">");
905 } else {
906 R_gl_draw_image(&msklh[(gm_tm / 6) & 1], x - 25, y + curoff - 8, 0);
909 switch (m->entries[i].type) {
910 case GM_BUTTON:
911 Z_gotoxy(x, y + yoff);
912 w = Z_printbf("%s", m->entries[i].caption);
913 msg.type = GM_GETSTR;
914 if (GM_send(m, i, &msg)) {
915 Z_gotoxy(x + w, y + yoff);
916 Z_printbf("%.*s", msg.string.maxlen, msg.string.s);
918 yoff += 16;
919 break;
920 case GM_SMALL_BUTTON:
921 Z_gotoxy(x, y + yoff);
922 w = Z_printsf("%s", m->entries[i].caption);
923 msg.type = GM_GETSTR;
924 if (GM_send(m, i, &msg)) {
925 Z_gotoxy(x + w, y + yoff);
926 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
928 yoff += 12;
929 break;
930 case GM_TEXTFIELD:
931 case GM_TEXTFIELD_BUTTON:
932 msg.type = GM_GETSTR;
933 recv = GM_send(m, i, &msg);
934 Z_gotoxy(x, y + yoff);
935 xoff = Z_printbf("%s", m->entries[i].caption);
936 yoff += 9;
937 w = (recv ? msg.string.maxlen : TEXTFIELD_MIDDLE) + 1;
938 R_gl_draw_image(&mslotl, x + xoff, y + yoff, 0);
939 for (j = 1; j < w; j++) {
940 R_gl_draw_image(&mslotm, x + xoff + j * 8, y + yoff, 0);
942 R_gl_draw_image(&mslotr, x + xoff + j * 8, y + yoff, 0);
943 Z_gotoxy(x + xoff + 4, y + yoff - 7);
944 if (input && i == cur) {
945 Z_printsf("%.*s_", imax, ibuf);
946 } else if (recv) {
947 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
949 yoff += 7;
950 break;
951 case GM_SCROLLER:
952 Z_gotoxy(x, y + yoff);
953 xoff = Z_printbf("%s", m->entries[i].caption);
954 R_gl_draw_image(&mbarl, x + xoff, y + yoff, 0);
955 for (j = 1; j < SCROLLER_MIDDLE + 1; j++) {
956 R_gl_draw_image(&mbarm, x + xoff + j * 8, y + yoff, 0);
958 R_gl_draw_image(&mbarr, x + xoff + j * 8, y + yoff, 0);
959 msg.type = GM_GETINT;
960 if (GM_send(m, i, &msg)) {
961 int lev = (msg.integer.i - msg.integer.a) * ((SCROLLER_MIDDLE - 1) * 8) / msg.integer.b;
962 R_gl_draw_image(&mbaro, x + xoff + lev + 8, y + yoff, 0);
964 yoff += 16;
965 break;
966 default:
967 assert(0);
971 return m != NULL;
974 /* --- View --- */
976 static void R_draw_fld (byte *fld, int minx, int miny, int maxx, int maxy, int fg) {
977 int i, j;
978 assert(minx >= 0 && minx <= FLDW);
979 assert(miny >= 0 && miny <= FLDH);
980 assert(maxx >= 0 && maxx <= FLDW);
981 assert(maxy >= 0 && maxy <= FLDH);
982 for (j = miny; j < maxy; j++) {
983 for (i = minx; i < maxx; i++) {
984 byte id = fld[j * FLDW + i];
985 if (id != 0) {
986 if (walp[id].res < 0) {
987 if (fg) {
988 switch (R_get_special_id(id)) {
989 case 1:
990 glColor4ub(0, 0, 255, 127);
991 break;
992 case 2:
993 glColor4ub(0, 127, 0, 127);
994 break;
995 case 3:
996 glColor4ub(127, 0, 0, 127);
997 break;
998 default:
999 glColor4ub(0, 0, 0, 127);
1000 break;
1002 glEnable(GL_BLEND);
1003 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1004 glDisable(GL_TEXTURE_2D);
1005 R_gl_draw_quad(i * CELW, j * CELW, CELW, CELH);
1007 } else {
1008 R_gl_draw_image(&walp[id], i * CELW, j * CELH, 0);
1015 static void R_draw_dots (void) {
1016 int i;
1017 glDisable(GL_BLEND);
1018 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1019 glDisable(GL_TEXTURE_2D);
1020 glBegin(GL_POINTS);
1021 for (i = 0; i < MAXDOT; i++) {
1022 if (dot[i].t != 0) {
1023 R_gl_set_color(dot[i].c);
1024 glVertex2i(dot[i].o.x, dot[i].o.y + 1);
1027 glEnd();
1030 static void R_draw_items (void) {
1031 int i, s;
1032 for (i = 0; i < MAXITEM; ++i) {
1033 s = -1;
1034 if (it[i].t && it[i].s >= 0) {
1035 switch (it[i].t & 0x7FFF) {
1036 case I_ARM1:
1037 s = it[i].s / 9 + 18;
1038 break;
1039 case I_ARM2:
1040 s = it[i].s / 9 + 20;
1041 break;
1042 case I_MEGA:
1043 s = it[i].s / 2 + 22;
1044 break;
1045 case I_INVL:
1046 s = it[i].s / 2 + 26;
1047 break;
1048 case I_SUPER:
1049 case I_RTORCH:
1050 case I_GTORCH:
1051 case I_BTORCH:
1052 s = it[i].s / 2 + (it[i].t - I_SUPER) * 4 + 35;
1053 break;
1054 case I_GOR1: case I_FCAN:
1055 s = it[i].s / 2 + (it[i].t - I_GOR1) * 3 + 51;
1056 break;
1057 case I_AQUA:
1058 s = 30;
1059 break;
1060 case I_SUIT:
1061 s = 34;
1062 break;
1063 case I_KEYR:
1064 case I_KEYG:
1065 case I_KEYB:
1066 s = (it[i].t & 0x7FFF) - I_KEYR + 31;
1067 break;
1068 case I_GUN2:
1069 s = 57;
1070 break;
1071 default:
1072 s = (it[i].t & 0x7FFF) - 1;
1075 if (s >= 0) {
1076 R_gl_draw_image(&item_spr[s], it[i].o.x, it[i].o.y, item_sprd[s]);
1081 static int standspr (player_t *p) {
1082 if (p->f & PLF_UP) {
1083 return 'X';
1084 } else if (p->f & PLF_DOWN) {
1085 return 'Z';
1086 } else {
1087 return 'E';
1091 static int wpnspr (player_t *p) {
1092 if (p->f & PLF_UP) {
1093 return 'C';
1094 } else if(p->f & PLF_DOWN) {
1095 return 'E';
1096 } else {
1097 return 'A';
1101 static void R_draw_player (player_t *p) {
1102 enum {STAND, GO, DIE, SLOP, DEAD, MESS, OUT, FALL}; // copypasted from player.c!
1103 static const int wytab[] = {-1, -2, -1, 0};
1104 int s = 'A';
1105 int w = 0;
1106 int wx = 0;
1107 int wy = 0;
1108 switch (p->st) {
1109 case STAND:
1110 if (p->f & PLF_FIRE) {
1111 s = standspr(p) + 1;
1112 w = wpnspr(p) + 1;
1113 } else if (p->pain) {
1114 s = 'G';
1115 w = 'A';
1116 wx = p->d ? 2 : -2;
1117 wy = 1;
1118 } else {
1119 s = standspr(p);
1120 w = wpnspr(p);
1122 break;
1123 case DEAD:
1124 s = 'N';
1125 break;
1126 case MESS:
1127 s = 'W';
1128 break;
1129 case GO:
1130 if (p->pain) {
1131 s = 'G';
1132 w = 'A';
1133 wx = p->d ? 2 : -2;
1134 wy = 1;
1135 } else {
1136 s = plr_goanim[p->s / 8];
1137 w = (p->f & PLF_FIRE) ? 'B' : 'A';
1138 wx = p->d ? 2 : -2;
1139 wy = 1 + wytab[s - 'A'];
1141 break;
1142 case DIE:
1143 s = plr_dieanim[p->s];
1144 break;
1145 case SLOP:
1146 s = plr_slopanim[p->s];
1147 break;
1148 case OUT:
1149 s = 0;
1150 break;
1152 if (p->wpn == 0) {
1153 w = 0;
1155 if (w) {
1156 R_gl_draw_image(&plr_wpn[(int)p->wpn][w -'A'], p->o.x + wx, p->o.y + wy, p->d);
1158 if (s) {
1159 R_gl_draw_image(&plr_spr[(s - 'A') * 2 + p->d], p->o.x, p->o.y, plr_sprd[(s - 'A') * 2 + p->d]);
1160 R_gl_set_color(p->color + PLAYER_COLOR_OFFSET);
1161 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]);
1165 static void R_draw_monsters (void) {
1166 enum {SLEEP, GO, RUN, CLIMB, DIE, DEAD, ATTACK, SHOOT, PAIN, WAIT, REVIVE, RUNOUT}; // copypasted from monster.c!
1167 int i;
1168 for (i = 0; i < MAXMN; i++) {
1169 if (mn[i].t != MN_NONE) {
1170 int x = mn[i].o.x;
1171 int y = mn[i].o.y;
1172 if (mn[i].t < MN__LAST) {
1173 if ((mn[i].t != MN_SOUL && mn[i].t != MN_PAIN) || mn[i].st != DEAD) {
1174 int ap = mn[i].ap[mn[i].ac];
1175 int d = (ap - 'A') * 2 + mn[i].d;
1176 int dir = mn_sprd[mn[i].t - 1][d];
1177 if (mn[i].t == MN_MAN && (ap == 'E' || ap == 'F')) {
1178 R_gl_draw_image(&mn_sgun[ap - 'E'], x, y, mn[i].d);
1180 R_gl_draw_image(&mn_spr[mn[i].t - 1][d], x, y, dir);
1181 if (mn[i].t == MN_MAN) {
1182 R_gl_set_color(MANCOLOR + PLAYER_COLOR_OFFSET);
1183 R_gl_draw_image_color(&mn_man_msk[d], x, y, dir);
1186 if (mn[i].t == MN_VILE && mn[i].st == SHOOT) {
1187 R_gl_draw_image(&mn_fspr[mn[i].ac / 3], mn[i].tx, mn[i].ty, 0);
1189 } else if (mn[i].t == MN_PL_DEAD || mn[i].t == MN_PL_MESS) {
1190 int type = mn[i].t - MN_PL_DEAD;
1191 R_gl_draw_image(&pl_spr[type], x, y, 0);
1192 R_gl_set_color(mn[i].d);
1193 R_gl_draw_image_color(&pl_msk[type], x, y, 0);
1199 static void R_draw_weapons (void) {
1200 enum {NONE, ROCKET, PLASMA, APLASMA, BALL1, BALL2, BALL7, BFGBALL, BFGHIT, MANF, REVF, FIRE}; // copypasted from weapons.c!
1201 int i, s, d, x, y;
1202 for (i = 0; i < MAXWPN; ++i) {
1203 s = -1;
1204 d = 0;
1205 switch (wp[i].t) {
1206 case REVF:
1207 case ROCKET:
1208 d = wp[i].s;
1209 if (d < 2) {
1210 d = wp[i].o.xv > 0 ? 1 : 0;
1211 x = abs(wp[i].o.xv);
1212 y = wp[i].o.yv;
1213 s = 0;
1214 if (y < 0) {
1215 if (-y >= x) {
1216 s = 30;
1218 } else if (y > 0) {
1219 if (y >= x / 2) {
1220 s = 31;
1223 } else {
1224 s = (d - 2) / 2 + 1;
1225 d = 0;
1227 break;
1228 case MANF:
1229 s=wp[i].s;
1230 if (s >= 2) {
1231 s /= 2;
1232 break;
1234 case PLASMA:
1235 case APLASMA:
1236 case BALL1:
1237 case BALL7:
1238 case BALL2:
1239 s = wp[i].s;
1240 if (s >= 2) {
1241 s = s / 2 + 1;
1243 switch (wp[i].t) {
1244 case PLASMA:
1245 s += 4;
1246 break;
1247 case APLASMA:
1248 s += 11;
1249 break;
1250 case BALL1:
1251 s += 32;
1252 break;
1253 case BALL2:
1254 s += 42;
1255 break;
1256 case BALL7:
1257 s += 37;
1258 d = wp[i].o.xv >= 0 ? 1 : 0;
1259 break;
1260 case MANF:
1261 s += 47;
1262 d= wp[i].o.xv>=0 ? 1 : 0;
1263 break;
1265 break;
1266 case BFGBALL:
1267 s = wp[i].s;
1268 if (s >= 2) {
1269 s = s / 2 + 1;
1271 s += 18;
1272 break;
1273 case BFGHIT:
1274 s = wp[i].s / 2 + 26;
1275 break;
1277 if (s >= 0) {
1278 R_gl_draw_image(&wp_spr[s * 2 + d], wp[i].o.x, wp[i].o.y, wp_sprd[s * 2 + d]);
1283 static void R_draw_smoke (void) {
1284 int i, s;
1285 for (i = 0; i < MAXSMOK; ++i) {
1286 if (sm[i].t) {
1287 switch (sm[i].s) {
1288 case 0:
1289 s = sm[i].t;
1290 if (s >= (SMSN - 1) * 3) {
1291 s = 0;
1292 } else {
1293 s = SMSN - 1 - s / 3;
1295 R_gl_draw_image(&smk_spr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1296 break;
1297 case 1:
1298 s = sm[i].t;
1299 if (s >= FLSN - 1) {
1300 s = 0;
1301 } else {
1302 s = FLSN - 1 - s;
1304 R_gl_draw_image(&smk_fspr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1305 break;
1311 static void R_draw_effects (void) {
1312 enum {NONE, TFOG, IFOG, BUBL}; // copypasted from fx.c
1313 int i, s;
1314 for (i = 0; i < MAXFX; ++i) {
1315 switch (fx[i].t) {
1316 case TFOG:
1317 s = fx[i].s / 2;
1318 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1319 break;
1320 case IFOG:
1321 s = fx[i].s / 2 + 10;
1322 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1323 break;
1324 case BUBL:
1325 glDisable(GL_BLEND);
1326 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1327 glDisable(GL_TEXTURE_2D);
1328 glBegin(GL_POINTS);
1329 R_gl_set_color(0xC0 + fx[i].s);
1330 glVertex2i(fx[i].x >> 8, (fx[i].y >> 8) + 1);
1331 glEnd();
1332 break;
1337 static int get_pu_st (int t) {
1338 if (t >= PL_FLASH) {
1339 return 1;
1340 } else if((t / 9) & 1) {
1341 return 0;
1342 } else {
1343 return 1;
1347 static void R_draw_view (int x, int y, int w, int h, int camx, int camy) {
1348 glPushMatrix();
1349 R_gl_setclip(x, y, w, h);
1350 glTranslatef(x, y, 0);
1351 if (w_horiz && horiz.n != NULL) {
1352 R_gl_draw_image_ext(&horiz, 0, 0, w, h);
1353 if (sky_type == 2 && lt_time < 0) {
1354 image *tanderbolt = &ltn[lt_type][lt_time < -5 ? 0 : 1];
1355 if (!lt_side) {
1356 R_gl_draw_image(tanderbolt, 0, lt_ypos, 0);
1357 } else {
1358 R_gl_draw_image(tanderbolt, w - 1, lt_ypos, 1);
1361 } else {
1362 glDisable(GL_BLEND);
1363 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1364 glDisable(GL_TEXTURE_2D);
1365 R_gl_set_color(DEFAULT_SKY_COLOR);
1366 R_gl_draw_quad(0, 0, w, h);
1368 int maxx = min((camx + w) / CELW + 1, FLDW);
1369 int maxy = min((camy + h) / CELH + 1, FLDH);
1370 int minx = max((camx - max_wall_width) / CELW, 0);
1371 int miny = max((camy - max_wall_height) / CELH, 0);
1372 glTranslatef(-camx, -camy, 0);
1373 R_draw_fld((byte*)fldb, minx, miny, maxx, maxy, 0);
1374 R_draw_dots();
1375 R_draw_items();
1376 R_draw_player(&pl1);
1377 if (_2pl) {
1378 R_draw_player(&pl2);
1380 R_draw_monsters();
1381 R_draw_weapons();
1382 R_draw_smoke();
1383 R_draw_effects();
1384 R_draw_fld((byte*)fldf, minx, miny, maxx, maxy, 1);
1385 glTranslatef(camx, camy, 0);
1386 if (sky_type == 2 && (lt_time == -4 || lt_time == -2)) {
1387 glColor4ub(255, 255, 255, 255);
1388 glEnable(GL_BLEND);
1389 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1390 glDisable(GL_TEXTURE_2D);
1391 R_gl_draw_quad(0, 0, w, h);
1393 glPopMatrix();
1396 static void R_draw_player_view (player_t *p, int x, int y, int w, int h) {
1397 p->looky = min(max(p->looky, -SCRH / 4), SCRH / 4); // TODO remove writeback
1398 int st = stone.w;
1399 int cw = w - st;
1400 int cx = min(max(p->o.x, cw / 2), FLDW * CELW - cw / 2);
1401 int cy = min(max(p->o.y - 12 + p->looky, h / 2), FLDH * CELH - h / 2);
1402 int camx = max(cx - cw / 2, 0);
1403 int camy = max(cy - h / 2, 0);
1404 glPushMatrix();
1405 R_draw_view(x, y + 1, cw, h - 2, camx, camy);
1406 glTranslatef(x, y, 0);
1407 if (p->invl) {
1408 if (get_pu_st(p->invl)) {
1409 glEnable(GL_BLEND);
1410 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1411 glDisable(GL_TEXTURE_2D);
1412 glColor4ub(191, 191, 191, 255);
1413 R_gl_draw_quad(0, 0, cw, h);
1415 } else {
1416 if (p->suit && get_pu_st(p->suit)) {
1417 glEnable(GL_BLEND);
1418 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1419 glDisable(GL_TEXTURE_2D);
1420 glColor4ub(0, 255, 0, 192);
1421 R_gl_draw_quad(0, 0, cw, h);
1423 int f = min(max(p->pain * 3, 0), 255);
1424 if (f > 0) {
1425 glEnable(GL_BLEND);
1426 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1427 glDisable(GL_TEXTURE_2D);
1428 glColor4ub(255, 0, 0, f);
1429 R_gl_draw_quad(0, 0, cw, h);
1432 R_gl_setclip(x, y, w, h);
1433 glTranslatef(-x + cw, 0, 0);
1434 R_gl_draw_image(&stone, 0, 0, 0);
1435 int i = stone.h;
1436 while (i < h) {
1437 R_gl_draw_image(&stone2, 0, i, 0);
1438 i += stone2.h;
1440 if (p->drawst & PL_DRAWAIR) {
1441 if (p->air < PL_AIR) {
1442 int a = min(max(p->air, 0), MAXAIR) * 100 / MAXAIR;
1443 glDisable(GL_BLEND);
1444 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1445 glDisable(GL_TEXTURE_2D);
1446 R_gl_set_color(0xC8);
1447 R_gl_draw_quad(10, 49, a, 2);
1450 if (p->drawst & PL_DRAWLIFE) {
1451 Z_gotoxy(10, 7);
1452 Z_printhf("%3d%%", p->life);
1454 if (p->drawst & PL_DRAWARMOR) {
1455 Z_gotoxy(10, 7 + 19);
1456 Z_printhf("%3d%%", p->armor);
1458 if (p->drawst & PL_DRAWWPN) {
1459 switch(p->wpn) {
1460 case 2:
1461 case 5:
1462 i = p->ammo;
1463 break;
1464 case 3:
1465 case 4:
1466 case 9:
1467 i = p->shel;
1468 break;
1469 case 6:
1470 i = p->rock;
1471 break;
1472 case 7:
1473 case 8:
1474 i = p->cell;
1475 break;
1476 case 10:
1477 i = p->fuel;
1478 break;
1479 default:
1480 i = -1;
1481 break;
1483 // weapon
1484 if (p->wpn >= 0) {
1485 R_gl_draw_image(&sth[12 + p->wpn], st - 88, 58 + 19, 0);
1487 // ammo
1488 if (p->wpn >= 2) {
1489 Z_gotoxy(st - 10 - 5 * 14, 58 + 2);
1490 Z_printhf("%5d", i);
1493 if (p->drawst & PL_DRAWFRAG && g_dm) {
1494 Z_gotoxy(st - 5 - 5 * 14, 77 + 5);
1495 Z_printhf("%5d", p->frag);
1497 if (p->drawst & PL_DRAWKEYS) {
1498 int x, k, n;
1499 for (k = p->keys >> 4, n = 0, x = st - 75; n < 3; n++, k >>= 1, x += 9) {
1500 if (k & 1) {
1501 R_gl_draw_image(&keys[n], x, 91, 0);
1505 if (p->drawst & PL_DRAWLIVES && !_2pl) {
1506 Z_gotoxy(st - 35, 17);
1507 Z_printhf("%d", p->lives);
1509 glPopMatrix();
1512 /* --- Game --- */
1514 static void pl_info (player_t *p, int x, int y) {
1515 dword t = p->kills * 10920 / g_time;
1516 Z_gotoxy(x + 25, y); Z_printbf("KILLS");
1517 Z_gotoxy(x + 25, y + 15); Z_printbf("KPM");
1518 Z_gotoxy(x + 25, y + 30); Z_printbf("SECRETS %u / %u", p->secrets, sw_secrets);
1519 Z_gotoxy(x + 255, y); Z_printbf("%u", p->kills);
1520 Z_gotoxy(x + 255, y + 15); Z_printbf("%u.%u", t / 10, t % 10);
1523 static void R_draw_intermission (void) {
1524 int cx = SCRW / 2;
1525 word hr, mn, sc, h;
1526 Z_gotoxy(cx - 14*12/2, 20);
1527 Z_printbf("LEVEL COMPLETE");
1528 Z_calc_time(g_time, &hr, &mn, &sc);
1529 Z_gotoxy(cx - 12*12/2, 40);
1530 Z_printbf("TIME %u:%02u:%02u", hr, mn, sc);
1531 h = 40 + SCRH / 10;
1532 if (_2pl) {
1533 Z_gotoxy(cx - 10*12/2, h);
1534 Z_printbf("PLAYER ONE");
1535 h += 20;
1537 pl_info(&pl1, cx - 160, h);
1538 if (_2pl) {
1539 h += 30 + SCRH / 10;
1540 Z_gotoxy(cx - 10*12/2, h);
1541 Z_printbf("PLAYER TWO");
1542 h += 20;
1543 pl_info(&pl2, cx - 160, h);
1547 static void W_act (void) {
1548 int i, a;
1549 if (g_time % 3 == 0) {
1550 for (i = 1; i < max_textures; i++) {
1551 a = walani[i];
1552 if (a != 0) {
1553 anic[a]++;
1554 if (anip[a][anic[a]].res == -1) {
1555 anic[a] = 0;
1557 walp[i] = anip[a][anic[a]];
1563 void R_draw (void) {
1564 W_act();
1565 glClearColor(0, 0, 0, 1);
1566 glClear(GL_COLOR_BUFFER_BIT);
1567 glEnable(GL_SCISSOR_TEST);
1568 R_gl_setmatrix();
1569 switch (g_st) {
1570 case GS_ENDANIM:
1571 case GS_END2ANIM:
1572 case GS_DARKEN:
1573 case GS_BVIDEO:
1574 case GS_EVIDEO:
1575 case GS_END3ANIM:
1576 break;
1577 case GS_TITLE:
1578 R_gl_draw_image_ext(&scrnh[0], 0, 0, SCRW, SCRH);
1579 break;
1580 case GS_INTER:
1581 R_gl_draw_image_ext(&scrnh[1], 0, 0, SCRW, SCRH);
1582 R_draw_intermission();
1583 break;
1584 case GS_ENDSCR:
1585 R_gl_draw_image_ext(&scrnh[2], 0, 0, SCRW, SCRH);
1586 break;
1587 case GS_GAME:
1588 if (_2pl) {
1589 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH / 2);
1590 R_draw_player_view(&pl2, 0, SCRH / 2, SCRW, SCRH / 2);
1591 } else {
1592 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH);
1594 R_gl_setclip(0, 0, SCRW, SCRH);
1595 break;
1597 GM_draw();
1598 Y_swap_buffers();
1601 void R_alloc (void) {
1602 char s[10];
1603 int i, j, n;
1604 logo("R_alloc: load graphics\n");
1605 /* Game */
1606 scrnh[0] = R_gl_loadimage("TITLEPIC");
1607 scrnh[1] = R_gl_loadimage("INTERPIC");
1608 scrnh[2] = R_gl_loadimage("ENDPIC");
1609 for (i = 0; i < 2; i++) {
1610 sprintf(s, "LTN%c", '1' + i);
1611 for (j = 0; j < 2; j++) {
1612 ltn[i][j] = Z_getspr(s, j, 0, NULL);
1615 /* Smoke */
1616 for (i = 0; i < SMSN; i++) {
1617 smk_spr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_smoke_spr);
1619 for (i = 0; i < FLSN; i++) {
1620 smk_fspr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_flame_spr);
1622 /* Effects */
1623 for (i = 0; i < 10; i++) {
1624 fx_spr[i] = Z_getspr("TFOG", i, 0, fx_sprd + i);
1626 for (; i < 15; i++) {
1627 fx_spr[i] = Z_getspr("IFOG", i - 10, 0, fx_sprd + i);
1629 /* Weapons */
1630 for (i = 0; i < 4; i++) {
1631 wp_spr[i * 2] = Z_getspr("MISL", i, 1, wp_sprd + i * 2);
1632 wp_spr[i * 2 + 1] = Z_getspr("MISL", i, 2, wp_sprd + i * 2 + 1);
1634 for (; i < 6; i++) {
1635 wp_spr[i * 2] = Z_getspr("PLSS", i - 4, 1, wp_sprd + i * 2);
1636 wp_spr[i * 2 + 1] = Z_getspr("PLSS", i - 4, 2, wp_sprd + i * 2 + 1);
1638 for (; i < 11; i++) {
1639 wp_spr[i * 2] = Z_getspr("PLSE", i - 6, 1, wp_sprd + i * 2);
1640 wp_spr[i * 2 + 1] = Z_getspr("PLSE", i - 6, 2, wp_sprd + i * 2 + 1);
1642 for (; i < 13; i++) {
1643 wp_spr[i * 2] = Z_getspr("APLS", i - 11, 1, wp_sprd + i * 2);
1644 wp_spr[i * 2 + 1] = Z_getspr("APLS", i - 11, 2, wp_sprd + i * 2 + 1);
1646 for (; i < 18; i++) {
1647 wp_spr[i * 2] = Z_getspr("APBX", i - 13, 1, wp_sprd + i * 2);
1648 wp_spr[i * 2 + 1] = Z_getspr("APBX", i - 13, 2, wp_sprd + i * 2 + 1);
1650 for(; i < 20; i++) {
1651 wp_spr[i * 2] = Z_getspr("BFS1", i - 18, 1, wp_sprd + i * 2);
1652 wp_spr[i * 2 + 1] = Z_getspr("BFS1", i - 18, 2, wp_sprd + i * 2 + 1);
1654 for (; i < 26; i++) {
1655 wp_spr[i * 2] = Z_getspr("BFE1", i - 20, 1, wp_sprd + i * 2);
1656 wp_spr[i * 2 + 1] = Z_getspr("BFE1", i - 20, 2, wp_sprd + i * 2 + 1);
1658 for (; i < 30; i++) {
1659 wp_spr[i * 2] = Z_getspr("BFE2", i - 26, 1, wp_sprd + i * 2);
1660 wp_spr[i * 2 + 1] = Z_getspr("BFE2", i - 26, 2, wp_sprd + i * 2 + 1);
1662 for (; i < 32; i++) {
1663 wp_spr[i * 2] = Z_getspr("MISL", i - 30 + 4, 1, wp_sprd + i * 2);
1664 wp_spr[i * 2 + 1] = Z_getspr("MISL", i - 30 + 4, 2, wp_sprd + i * 2 + 1);
1666 for (; i < 37; i++) {
1667 wp_spr[i * 2] = Z_getspr("BAL1", i - 32, 1, wp_sprd + i * 2);
1668 wp_spr[i * 2 + 1] = Z_getspr("BAL1", i - 32, 2, wp_sprd + i * 2 + 1);
1670 for (; i < 42; i++) {
1671 wp_spr[i * 2] = Z_getspr("BAL7", i - 37, 1, wp_sprd + i * 2);
1672 wp_spr[i * 2 + 1] = Z_getspr("BAL7", i - 37, 2, wp_sprd + i * 2 + 1);
1674 for (; i < 47; i++) {
1675 wp_spr[i * 2] = Z_getspr("BAL2", i - 42, 1, wp_sprd + i * 2);
1676 wp_spr[i * 2 + 1] = Z_getspr("BAL2", i - 42, 2, wp_sprd + i * 2 + 1);
1678 for (; i < 49; i++) {
1679 wp_spr[i * 2] = Z_getspr("MANF", i - 47, 1, wp_sprd + i * 2);
1680 wp_spr[i * 2 + 1] = Z_getspr("MANF", i - 47, 2, wp_sprd + i * 2 + 1);
1682 /* Items */
1683 static const char snm[18][4] = {
1684 "CLIP", "SHEL", "ROCK", "CELL", "AMMO", "SBOX", "BROK", "CELP",
1685 "STIM", "MEDI", "BPAK",
1686 "CSAW", "SHOT", "SGN2", "MGUN", "LAUN", "PLAS", "BFUG"
1687 };
1688 static const char n4[4][4] = {
1689 "SOUL", "SMRT", "SMGT", "SMBT"
1690 };
1691 static const char n3[2][4] = {
1692 "GOR1", "FCAN"
1693 };
1694 for (i = 0; i < 18; i++) {
1695 item_spr[i] = Z_getspr(snm[i], 0, 0, item_sprd + i);
1697 for (; i < 20; i++) {
1698 item_spr[i] = Z_getspr("ARM1", i - 18, 0, item_sprd + i);
1699 item_spr[i + 2] = Z_getspr("ARM2", i - 18, 0, item_sprd + i);
1701 i+=2;
1702 for (; i < 26; i++) {
1703 item_spr[i] = Z_getspr("MEGA", i - 22, 0, item_sprd + i);
1705 for (; i < 30; i++) {
1706 item_spr[i] = Z_getspr("PINV", i - 26, 0, item_sprd + i);
1708 item_spr[30] = Z_getspr("AQUA", 0, 0, item_sprd + 30);
1709 item_spr[31] = Z_getspr("KEYR", 0, 0, item_sprd + 31);
1710 item_spr[32] = Z_getspr("KEYG", 0, 0, item_sprd + 32);
1711 item_spr[33] = Z_getspr("KEYB", 0, 0, item_sprd + 33);
1712 item_spr[34] = Z_getspr("SUIT", 0, 0, item_sprd + 34);
1713 for (n = 35, j = 0; j < 4; j++) {
1714 for (i = 0; i < 4; i++, n++) {
1715 item_spr[n] = Z_getspr(n4[j], i, 0, item_sprd + n);
1718 for (j = 0; j < 2; j++) {
1719 for (i = 0; i < 3; i++, n++) {
1720 item_spr[n] = Z_getspr(n3[j], i, 0, item_sprd + n);
1723 item_spr[57] = Z_getspr("GUN2", 0, 0, item_sprd + 57);
1724 /* Player */
1725 for (i = 0; i < 27; i++) {
1726 plr_spr[i * 2] = Z_getspr("PLAY", i, 1, plr_sprd + i * 2);
1727 plr_msk[i * 2] = R_gl_get_special_spr("PLAY", i, 1, &R_extract_mask_spr);
1728 plr_spr[i * 2 + 1] = Z_getspr("PLAY", i, 2, plr_sprd + i * 2 + 1);
1729 plr_msk[i * 2 + 1] = R_gl_get_special_spr("PLAY", i, 2, &R_extract_mask_spr);
1731 strncpy(s, "PWPx", 4);
1732 for (i = 1; i < 11; i++) {
1733 s[3] = (i < 10 ? '0' : 'A' - 10) + i;
1734 for (j = 0; j < 6; j++) {
1735 plr_wpn[i][j] = Z_getspr(s, j, 1, NULL);
1738 /* Monsters */
1739 static const char msn[MN_TN][4] = {
1740 "SARG", "TROO", "POSS", "SPOS", "CYBR", "CPOS", "BOSS", "BOS2", "HEAD", "SKUL",
1741 "PAIN", "SPID", "BSPI", "FATT", "SKEL", "VILE", "FISH", "BAR1", "ROBO", "PLAY"
1742 };
1743 static const int mms[MN_TN] = {
1744 14*2, 21*2, 21*2, 21*2, 16*2, 20*2, 15*2, 15*2, 12*2, 11*2,
1745 13*2, 19*2, 16*2, 20*2, 17*2, 29*2, 6*2, 2*2, 17*2, 23*2
1746 };
1747 mn_sgun[0] = Z_getspr("PWP4", 0, 1, NULL);
1748 mn_sgun[1] = Z_getspr("PWP4", 1, 1, NULL);
1749 for (j = 0; j < MN_TN; j++) {
1750 for (i = 0; i < mms[j]; i++) {
1751 mn_spr[j][i] = Z_getspr(msn[j], i / 2, (i & 1) + 1, &mn_sprd[j][i]);
1752 if (j == MN_MAN - 1) {
1753 mn_man_msk[i] = R_gl_get_special_spr(msn[j], i / 2, (i & 1) + 1, &R_extract_mask_spr);
1756 if (j == MN_BARREL - 1) {
1757 for (i = 4; i < 14; i++) {
1758 mn_spr[j][i] = Z_getspr("BEXP", i / 2 - 2, (i & 1) + 1, &mn_sprd[j][i]);
1762 for (i = 0; i < 8; i++) {
1763 mn_fspr[i] = Z_getspr("FIRE", i, 0, NULL);
1765 pl_spr[0] = Z_getspr("PLAY", 'N' - 'A', 0, NULL);
1766 pl_msk[0] = R_gl_get_special_spr("PLAY", 'N' - 'A', 0, &R_extract_mask_spr);
1767 pl_spr[1] = Z_getspr("PLAY", 'W' - 'A', 0, NULL);
1768 pl_msk[1] = R_gl_get_special_spr("PLAY", 'W' - 'A', 0, &R_extract_mask_spr);
1769 /* Misc */
1770 static const char mnm[22][8]={
1771 "STTNUM0", "STTNUM1", "STTNUM2", "STTNUM3", "STTNUM4",
1772 "STTNUM5", "STTNUM6", "STTNUM7", "STTNUM8", "STTNUM9",
1773 "STTMINUS", "STTPRCNT",
1774 "FISTA0", "CSAWA0", "PISTA0", "SHOTA0", "SGN2A0", "MGUNA0", "LAUNA0",
1775 "PLASA0", "BFUGA0", "GUN2A0"
1776 };
1777 stone = R_gl_loadimage("STONE");
1778 stone2 = R_gl_loadimage("STONE2");
1779 keys[0] = R_gl_loadimage("KEYRA0");
1780 keys[1] = R_gl_loadimage("KEYGA0");
1781 keys[2] = R_gl_loadimage("KEYBA0");
1782 for (i = 0; i < 22; i++) {
1783 sth[i] = R_gl_loadimage(mnm[i]);
1785 strcpy(s, "STBF_*");
1786 for (i = '!'; i < 160; i++) {
1787 s[5] = i;
1788 bfh[i - '!'] = R_gl_getimage(F_findres(s));
1790 for (i = '!'; i < 160; i++) {
1791 sprintf(s, "STCFN%03d", i);
1792 sfh[i - '!'] = R_gl_getimage(F_findres(s));
1794 strcpy(s, "WINUM*");
1795 for (i = '0'; i <= '9'; i++) {
1796 s[5] = i;
1797 bfh[i - '!'] = R_gl_loadimage(s);
1799 bfh[':' - '!'] = R_gl_loadimage("WICOLON");
1800 // menu
1801 msklh[0] = R_gl_loadimage("M_SKULL1");
1802 msklh[1] = R_gl_loadimage("M_SKULL2");
1803 mbarl = R_gl_loadimage("M_THERML");
1804 mbarm = R_gl_loadimage("M_THERMM");
1805 mbarr = R_gl_loadimage("M_THERMR");
1806 mbaro = R_gl_loadimage("M_THERMO");
1807 mslotl = R_gl_loadimage("M_LSLEFT");
1808 mslotm = R_gl_loadimage("M_LSCNTR");
1809 mslotr = R_gl_loadimage("M_LSRGHT");
1810 // walls
1811 for (i = 1; i < ANIT; i++) {
1812 for (j = 0; j < 5 && anm[i - 1][j]; j++) {
1813 anip[i][j] = R_gl_loadimage(anm[i - 1][j]);
1815 for(; j < 5; j++) {
1816 anip[i][j] = (image) {
1817 .n = NULL,
1818 .w = 8,
1819 .h = 8,
1820 .res = -1,
1821 };
1826 static void R_reload_textures (void);
1828 void R_set_videomode (int w, int h, int fullscreen) {
1829 assert(w > 0);
1830 assert(h > 0);
1831 int was = Y_videomode_setted();
1832 if (root != NULL) {
1833 R_cache_free(root, 0);
1834 root = NULL;
1836 int res = Y_set_videomode_opengl(w, h, fullscreen);
1837 if (res == 0) {
1838 if (was == 0) {
1839 ERR_failinit("Unable to set video mode\n");
1841 } else {
1842 Y_get_videomode(&SCRW, &SCRH);
1843 root = R_cache_new();
1844 assert(root);
1845 R_alloc();
1846 R_reload_textures();
1850 void R_toggle_fullscreen (void) {
1851 R_cache_free(root, 0);
1852 Y_set_fullscreen(!Y_get_fullscreen());
1853 fullscreen = Y_get_fullscreen();
1854 Y_get_videomode(&SCRW, &SCRH);
1855 root = R_cache_new();
1856 assert(root);
1857 R_alloc();
1858 R_reload_textures();
1861 void R_init (void) {
1862 logo("R_init: intialize opengl render\n");
1863 R_init_playpal();
1864 R_set_videomode(SCRW, SCRH, fullscreen);
1867 void R_done (void) {
1868 R_cache_free(root, 1);
1869 Y_unset_videomode();
1870 root = NULL;
1873 void R_setgamma (int g) {
1874 gamma = g < 0 ? 0 : (g > 4 ? 4 : g);
1877 int R_getgamma (void) {
1878 return gamma;
1881 void R_get_name (int n, char s[8]) {
1882 assert(n >= 0 && n < 256);
1883 if (walp[n].res == -1) {
1884 memset(s, 0, 8);
1885 } else if (walp[n].res == -2) {
1886 memcpy(s, "_WATER_", 8);
1887 s[7] = '0' + (intptr_t)walp[n].n - 1;
1888 } else if (walani[n] > 0) {
1889 memcpy(s, anm[walani[n] - 1][0], 8);
1890 } else {
1891 F_getresname(s, walp[n].res & 0x7FFF);
1895 static short getani (char n[8]) {
1896 short i = 0;
1897 while (i < ANIT - 1 && strncasecmp(n, anm[i][0], 8) != 0) {
1898 i++;
1900 return i < ANIT - 1 ? i + 1 : 0;
1903 int R_get_special_id (int n) {
1904 assert(n >= 0 && n <= 256);
1905 return walp[n].res == -2 ? (intptr_t)walp[n].n : -1;
1908 static void R_reload_textures (void) {
1909 int i;
1910 char s[8];
1911 for (i = 0; i < max_textures; i++) {
1912 R_get_name(i, s);
1913 if (walp[i].res >= 0) {
1914 walp[i] = R_gl_getimage(walp[i].res);
1917 if (horiz.n) {
1918 horiz = R_gl_getimage(horiz.res);
1922 void R_begin_load (void) {
1923 int i;
1924 for (i = 0; i < 256; i++) {
1925 if (walp[i].n != NULL && walp[i].res >= 0 && walani[i] == 0) {
1926 R_gl_free_image(&walp[i]);
1928 memset(&walp[i], 0, sizeof(image));
1929 walp[i].res = -1;
1930 walswp[i] = i;
1931 walani[i] = 0;
1933 memset(anic, 0, sizeof(anic));
1934 max_wall_width = 0;
1935 max_wall_height = 0;
1936 max_textures = 1;
1939 void R_load (char s[8], int f) {
1940 assert(max_textures < 256);
1941 if (!s[0]) {
1942 walp[max_textures] = (image) {
1943 .n = NULL,
1944 .x = 0,
1945 .y = 0,
1946 .w = 0,
1947 .h = 0,
1948 .res = -1,
1949 };
1950 } else if (strncasecmp(s, "_WATER_", 7) == 0) {
1951 walp[max_textures] = (image) {
1952 .n = (void*)((intptr_t)s[7] - '0' + 1),
1953 .x = 0,
1954 .y = 0,
1955 .w = 8,
1956 .h = 8,
1957 .res = -2,
1958 };
1959 } else {
1960 walp[max_textures] = R_gl_loadimage(s);
1961 if (f) {
1962 walp[max_textures].res |= 0x8000;
1964 if (s[0] == 'S' && s[1] == 'W' && s[4] == '_') {
1965 walswp[max_textures] = 0;
1967 walani[max_textures] = getani(s);
1969 max_wall_width = max(max_wall_width, walp[max_textures].w);
1970 max_wall_height = max(max_wall_height, walp[max_textures].h);
1971 max_textures++;
1974 void R_end_load (void) {
1975 int i, j, k, g;
1976 char s[8];
1977 j = max_textures;
1978 for (i = 1; i < 256 && j < 256; i++) {
1979 if (walswp[i] == 0) {
1980 R_get_name(i, s);
1981 s[5] ^= 1;
1982 g = F_getresid(s) | (walp[i].res & 0x8000);
1983 k = 1;
1984 while (k < 256 && walp[k].res != g) {
1985 k += 1;
1987 if (k >= 256) {
1988 k = j;
1989 j += 1;
1990 max_textures += 1;
1991 walp[k] = R_gl_getimage(g);
1992 walf[k] = g & 0x8000 ? 1 : 0;
1994 walswp[i] = k;
1995 walswp[k] = i;
2000 void R_loadsky (int sky) {
2001 char s[6];
2002 strcpy(s, "RSKYx");
2003 s[4] = '0' + sky;
2004 R_gl_free_image(&horiz);
2005 horiz = R_gl_loadimage(s);