DEADSOFTWARE

menu: change videomode at runtime
[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 rgb playpal[256];
85 static byte bright[256];
86 static GLuint lastTexture;
87 static cache *root;
89 /* Game */
90 static image scrnh[3]; // TITLEPIC INTERPIC ENDPIC
91 static image ltn[2][2];
93 /* Smoke */
94 static image smk_spr[SMSN];
95 static image smk_fspr[FLSN];
97 /* Effects */
98 static image fx_spr[15];
99 static char fx_sprd[15];
101 /* Weapons */
102 static image wp_spr[49*2];
103 static char wp_sprd[49*2];
105 /* Items */
106 static image item_spr[58];
107 static char item_sprd[58];
109 /* Player */
110 static image plr_spr[27*2];
111 static image plr_msk[27*2];
112 static char plr_sprd[27*2];
113 static image plr_wpn[11][6];
115 /* Monsters */
116 static image pl_spr[2];
117 static image pl_msk[2];
118 static image mn_spr[MN_TN][29*2];
119 static image mn_man_msk[29*2];
120 static char mn_sprd[MN_TN][29*2];
121 static image mn_fspr[8];
122 static image mn_sgun[2];
124 /* Misc */
125 static image sth[22];
126 static image bfh[160 - '!'];
127 static image sfh[160 - '!'];
128 static image stone;
129 static image stone2;
130 static image keys[3];
131 static int prx = 0;
132 static int pry = 0;
134 /* Menu */
135 static int gm_tm;
136 static image msklh[2];
137 static image mbarl;
138 static image mbarm;
139 static image mbarr;
140 static image mbaro;
141 static image mslotl;
142 static image mslotm;
143 static image mslotr;
145 /* Map */
146 static const char *anm[ANIT - 1][5] = {
147 {"WALL22_1", "WALL23_1", "WALL23_2", NULL, NULL},
148 {"WALL58_1", "WALL58_2", "WALL58_3", NULL, NULL},
149 {"W73A_1", "W73A_2", NULL, NULL, NULL},
150 {"RP2_1", "RP2_2", "RP2_3", "RP2_4", NULL}
151 };
152 static int max_wall_width;
153 static int max_wall_height;
154 static int max_textures;
155 static image walp[256];
156 static byte walani[256];
157 static image anip[ANIT][5];
158 static byte anic[ANIT];
159 static image horiz;
161 /* Texture cache */
163 // https://blackpawn.com/texts/lightmaps/
164 static node *R_node_alloc (node *p, int w, int h) {
165 assert(p);
166 assert(w > 0);
167 assert(h > 0);
168 if (p->left) {
169 assert(p->right);
170 node *n = R_node_alloc(p->left, w, h);
171 return n ? n : R_node_alloc(p->right, w, h);
172 } else {
173 int pw = p->r - p->l + 1;
174 int ph = p->b - p->t + 1;
175 if (p->leaf || pw < w || ph < h) {
176 return NULL;
177 } else if (pw == w && ph == h) {
178 p->leaf = 1;
179 return p;
180 } else {
181 p->left = malloc(sizeof(node));
182 p->right = malloc(sizeof(node));
183 if (pw - w > ph - h) {
184 *p->left = (node) {
185 .up = p,
186 .l = p->l,
187 .t = p->t,
188 .r = p->l + w - 1,
189 .b = p->b
190 };
191 *p->right = (node) {
192 .up = p,
193 .l = p->l + w,
194 .t = p->t,
195 .r = p->r,
196 .b = p->b
197 };
198 } else {
199 *p->left = (node) {
200 .up = p,
201 .l = p->l,
202 .t = p->t,
203 .r = p->r,
204 .b = p->t + h - 1
205 };
206 *p->right = (node) {
207 .up = p,
208 .l = p->l,
209 .t = p->t + h,
210 .r = p->r,
211 .b = p->b
212 };
214 return R_node_alloc(p->left, w, h);
219 static int R_node_have_leaf (node *n) {
220 return n && (n->leaf || R_node_have_leaf(n->left) || R_node_have_leaf(n->right));
223 static void R_node_free_recursive (node *n) {
224 if (n) {
225 R_node_free_recursive(n->left);
226 R_node_free_recursive(n->right);
227 free(n);
231 static void R_node_free (node *n) {
232 if (n) {
233 //logo("free node %p {%i:%i:%i:%i}\n", n, n->l, n->t, n->r, n->b);
234 assert(n->leaf);
235 assert(n->left == NULL);
236 assert(n->right == NULL);
237 n->leaf = 0;
238 n->base = NULL;
239 node *p = n->up;
240 while (p != NULL) {
241 assert(p->leaf == 0);
242 assert(p->left);
243 assert(p->right);
244 if (R_node_have_leaf(p) == 0) {
245 R_node_free_recursive(p->left);
246 p->left = NULL;
247 R_node_free_recursive(p->right);
248 p->right = NULL;
249 p = p->up;
250 } else {
251 p = NULL;
257 static void R_cache_get_max_texture_size (int *w, int *h) {
258 GLint size = 0;
259 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
260 size = min(max(size, 0), 512); // more can be buggy on older hardware
261 *w = size;
262 *h = size;
265 static void R_gl_bind_texture (GLuint id) {
266 if (id != lastTexture) {
267 glBindTexture(GL_TEXTURE_2D, id);
271 static cache *R_cache_new (void) {
272 int w, h;
273 GLuint id;
274 cache *c = NULL;
275 R_cache_get_max_texture_size(&w, &h);
276 if (w && h) {
277 glGenTextures(1, &id);
278 if (id) {
279 R_gl_bind_texture(id);
280 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
281 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
282 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
283 c = malloc(sizeof(cache));
284 if (c != NULL) {
285 *c = (cache) {
286 .id = id,
287 .root.r = w - 1,
288 .root.b = h - 1
289 };
290 } else {
291 glDeleteTextures(1, &id);
295 //logo("new cache %p\n", c);
296 return c;
299 static void R_cache_free (cache *root, int freetexture) {
300 cache *next;
301 cache *c = root;
302 while (c != NULL) {
303 next = c->next;
304 R_node_free_recursive(c->root.left);
305 R_node_free_recursive(c->root.right);
306 if (freetexture && c->id != 0) {
307 glDeleteTextures(1, &c->id);
309 free(c);
310 c = next;
314 static node *R_cache_alloc (cache *root, int w, int h) {
315 assert(root);
316 assert(w > 0 && h > 0);
317 node *n = NULL;
318 cache *p = NULL;
319 cache *c = root;
320 int maxw, maxh;
321 R_cache_get_max_texture_size(&maxw, &maxh);
322 if (w <= maxw && h <= maxh) {
323 while (c && !n) {
324 n = R_node_alloc(&c->root, w, h);
325 if (n) {
326 assert(n->leaf);
327 n->base = c;
329 p = c;
330 c = c->next;
332 if (!n) {
333 c = R_cache_new();
334 if (c) {
335 p->next = c;
336 n = R_node_alloc(&c->root, w, h);
337 if (n) {
338 assert(n->leaf);
339 n->base = c;
344 if (n) {
345 //logo("new node %p {%i:%i:%i:%i}\n", n, n->l, n->t, n->r, n->b);
346 } else {
347 logo("new node failed {%i:%i}\n", w, h);
349 return n;
352 static void R_cache_update (node *n, const void *data, int x, int y, int w, int h) {
353 assert(n);
354 assert(n->leaf);
355 assert(n->base);
356 assert(data);
357 assert(x >= 0);
358 assert(y >= 0);
359 assert(n->l + x + w - 1 <= n->r);
360 assert(n->t + y + h - 1 <= n->b);
361 R_gl_bind_texture(n->base->id);
362 glTexSubImage2D(GL_TEXTURE_2D, 0, n->l + x, n->t + y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
365 /* Generic helpers */
367 static void R_init_playpal (void) {
368 int i;
369 byte *vgapal = M_lock(F_getresid("PLAYPAL"));
370 for (i = 0; i < 256; i++) {
371 playpal[i] = (rgb) {
372 .r = vgapal[i * 3 + 0] * 255 / 63,
373 .g = vgapal[i * 3 + 1] * 255 / 63,
374 .b = vgapal[i * 3 + 2] * 255 / 63,
375 };
376 bright[i] = ((int)vgapal[i * 3 + 0] + vgapal[i * 3 + 1] + vgapal[i * 3 + 2]) * 8 / (63 * 3);
378 M_unlock(vgapal);
381 static vgaimg *R_getvga (int id) {
382 int loaded = M_was_locked(id);
383 vgaimg *v = M_lock(id);
384 if (v != NULL && !loaded) {
385 v->w = short2host(v->w);
386 v->h = short2host(v->h);
387 v->x = short2host(v->x);
388 v->y = short2host(v->y);
390 return v;
393 static rgba *R_extract_flame_spr (vgaimg *v) {
394 static const byte flametab[16] = {
395 0xBC, 0xBA, 0xB8, 0xB6, 0xB4, 0xB2, 0xB0, 0xD5,
396 0xD6, 0xD7, 0xA1, 0xA0, 0xE3, 0xE2, 0xE1, 0xE0
397 };
398 int i, j;
399 rgba *s = malloc(v->w * v->h * sizeof(rgba));
400 if (s != NULL) {
401 for (j = 0; j < v->h; j++) {
402 for (i = 0; i < v->w; i++) {
403 int k = j * v->w + i;
404 byte c = v->data[k] + bright[DEFAULT_SKY_COLOR];
405 s[k] = (rgba) {
406 .r = playpal[flametab[c]].r,
407 .g = playpal[flametab[c]].g,
408 .b = playpal[flametab[c]].b,
409 .a = v->data[k] == VGA_TRANSPARENT_COLOR ? 0x00 : 0xFF,
410 };
414 return s;
417 static rgba *R_extract_smoke_spr (vgaimg *v) {
418 int i, j;
419 rgba *s = malloc(v->w * v->h * sizeof(rgba));
420 if (s != NULL) {
421 for (j = 0; j < v->h; j++) {
422 for (i = 0; i < v->w; i++) {
423 int k = j * v->w + i;
424 byte c = ((v->data[k] + bright[DEFAULT_SKY_COLOR]) + 0x60) ^ 0x0F;
425 byte a = 0xFF - ((int)playpal[c].r + playpal[c].g + playpal[c].b) / 3;
426 s[k] = (rgba) {
427 .r = playpal[c].r,
428 .g = playpal[c].g,
429 .b = playpal[c].b,
430 .a = v->data[k] == VGA_TRANSPARENT_COLOR ? 0x00 : a,
431 };
435 return s;
438 static rgba *R_extract_mask_spr (vgaimg *v) {
439 int i, j;
440 rgba *s = malloc(v->w * v->h * sizeof(rgba));
441 if (s != NULL) {
442 for (j = 0; j < v->h; j++) {
443 for (i = 0; i < v->w; i++) {
444 int k = j * v->w + i;
445 byte c = v->data[k];
446 if (c >= 0x70 && c <= 0x7F) {
447 byte mask = c - 0x70;
448 mask = 0xFF - ((mask << 4) | mask);
449 s[k] = (rgba) {
450 .r = mask,
451 .g = mask,
452 .b = mask,
453 .a = 0xFF,
454 };
455 } else {
456 s[k] = (rgba) {
457 .r = 0,
458 .g = 0,
459 .b = 0,
460 .a = 0,
461 };
466 return s;
469 static rgba *R_extract_rgba_spr (vgaimg *v) {
470 int i, j;
471 rgba *s = malloc(v->w * v->h * sizeof(rgba));
472 if (s != NULL) {
473 for (j = 0; j < v->h; j++) {
474 for (i = 0; i < v->w; i++) {
475 int k = j * v->w + i;
476 byte c = v->data[k];
477 s[k] = (rgba) {
478 .r = playpal[c].r,
479 .g = playpal[c].g,
480 .b = playpal[c].b,
481 .a = c == VGA_TRANSPARENT_COLOR ? 0x00 : 0xFF,
482 };
486 return s;
489 /* OpenGL helpers */
491 static image R_gl_create_image (const rgba *buf, int w, int h) {
492 node *n = R_cache_alloc(root, w, h);
493 if (n) {
494 R_cache_update(n, buf, 0, 0, w, h);
496 return (image) {
497 .n = n,
498 .w = w,
499 .h = h,
500 .res = -1
501 };
504 static image R_gl_get_special_image (int id, rgba *(*fn)(vgaimg*)) {
505 image img;
506 //char name[8];
507 //F_getresname(name, id);
508 //logo("load image: %.8s\n", name);
509 vgaimg *v = R_getvga(id);
510 if (v != NULL) {
511 rgba *buf = (*fn)(v);
512 img = R_gl_create_image(buf, v->w, v->h);
513 img.x = v->x;
514 img.y = v->y;
515 img.res = id;
516 M_unlock(v);
517 free(buf);
518 } else {
519 img = (image) {
520 .res = id
521 };
523 return img;
526 static image R_gl_getimage (int id) {
527 return R_gl_get_special_image(id, &R_extract_rgba_spr);
530 static image R_gl_loadimage (const char name[8]) {
531 return R_gl_getimage(F_getresid(name));
534 static image R_gl_get_special_spr (const char n[4], int s, int d, rgba *(*fn)(vgaimg*)) {
535 return R_gl_get_special_image(F_getsprid(n, s, d), fn);
538 static void R_gl_free_image (image *img) {
539 if (img->n != NULL && img->res >= 0) {
540 R_node_free(img->n);
542 img->n = NULL;
543 img->res = -1;
546 static void R_gl_draw_quad (int x, int y, int w, int h) {
547 glBegin(GL_QUADS);
548 glVertex2i(x + w, y);
549 glVertex2i(x, y);
550 glVertex2i(x, y + h);
551 glVertex2i(x + w, y + h);
552 glEnd();
555 static void R_gl_draw_textured (image *img, int x, int y, int w, int h, int flip) {
556 if (img->n) {
557 GLfloat nw = img->n->base->root.r + 1;
558 GLfloat nh = img->n->base->root.b + 1;
559 GLfloat ax = (flip ? img->n->l : img->n->r + 1) / nw;
560 GLfloat bx = (flip ? img->n->r + 1 : img->n->l) / nh;
561 GLfloat ay = (img->n->t) / nw;
562 GLfloat by = (img->n->b + 1) / nh;
563 R_gl_bind_texture(img->n->base->id);
564 glEnable(GL_TEXTURE_2D);
565 glBegin(GL_QUADS);
566 glTexCoord2f(ax, ay); glVertex2i(x + w, y);
567 glTexCoord2f(bx, ay); glVertex2i(x, y);
568 glTexCoord2f(bx, by); glVertex2i(x, y + h);
569 glTexCoord2f(ax, by); glVertex2i(x + w, y + h);
570 glEnd();
571 } else {
572 glColor3ub(255, 0, 0);
573 glDisable(GL_BLEND);
574 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
575 glDisable(GL_TEXTURE_2D);
576 R_gl_draw_quad(x, y, w, h);
580 /* fit image into rectangle without applying offset and transparency */
581 static void R_gl_draw_image_ext (image *img, int x, int y, int w, int h) {
582 glDisable(GL_BLEND);
583 glColor3ub(255, 255, 255);
584 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
585 R_gl_draw_textured(img, x, y, w, h, 0);
588 /* draw sprite with offset and coloring */
589 static void R_gl_draw_image_color (image *img, int x, int y, int flip) {
590 int xx = flip ? x - img->w + img->x : x - img->x;
591 glEnable(GL_BLEND);
592 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
593 R_gl_draw_textured(img, xx, y - img->y, img->w, img->h, flip);
596 /* draw sprite with offset */
597 static void R_gl_draw_image (image *img, int x, int y, int flip) {
598 glColor3ub(255, 255, 255);
599 R_gl_draw_image_color(img, x, y, flip);
602 static void R_gl_set_color (byte c) {
603 glColor3ub(playpal[c].r, playpal[c].g, playpal[c].b);
606 static void R_gl_setclip (int x, int y, int w, int h) {
607 glScissor(x, SCRH - h - y, w, h);
610 static void R_gl_setmatrix (void) {
611 glScissor(0, 0, SCRW, SCRH);
612 glViewport(0, 0, SCRW, SCRH);
613 glMatrixMode(GL_PROJECTION);
614 glLoadIdentity();
615 glOrtho(0, SCRW, SCRH, 0, 0, 1);
616 glMatrixMode(GL_MODELVIEW);
617 glLoadIdentity();
620 /* --- Misc --- */
622 static image Z_getspr (const char n[4], int s, int d, char *dir) {
623 int h = F_getsprid(n, s, d);
624 if (dir != NULL) {
625 *dir = (h & 0x8000) ? 1 : 0;
627 return R_gl_getimage(h);
630 static image *Z_get_char_image (image *img, int ch) {
631 image *p = NULL;
632 if (ch > 32 && ch < 160) {
633 p = &img[ch - '!'];
634 if (p->n == NULL) {
635 p = &img[toupper(ch) - '!'];
638 return p;
641 static int Z_get_char_width_generic (image *img, int off, int ch) {
642 image *p = Z_get_char_image(img, ch);
643 return p == NULL ? off : p->w - 1;
646 static int Z_putch_generic (image *img, int off, int ch) {
647 image *p = Z_get_char_image(img, ch);
648 int w = p == NULL ? off : p->w - 1;
649 if (p != NULL && p->n != NULL) {
650 R_gl_draw_image(p, prx, pry, 0);
652 prx += w;
653 return w;
656 static int Z_get_string_width_generic (image *img, int off, const char *fmt, va_list ap) {
657 int i, w, ww;
658 char buf[80];
659 vsprintf(buf, fmt, ap);
660 for (i = w = ww = 0; buf[i]; ++i) {
661 switch (buf[i]) {
662 case '\n':
663 case '\r':
664 ww = max(w, ww);
665 w = 0;
666 break;
667 default:
668 w += Z_get_char_width_generic(img, off, (byte)buf[i]);
671 return max(w, ww);
674 static int Z_printf_generic (image *img, int off, const char *fmt, va_list ap) {
675 int i, w, ww;
676 char buf[80];
677 vsprintf(buf, fmt, ap);
678 for (i = w = ww = 0; buf[i]; ++i) {
679 switch (buf[i]) {
680 case '\n':
681 pry += off + 1;
682 case '\r':
683 w = max(w, ww);
684 prx = 0;
685 w = 0;
686 break;
687 default:
688 w += Z_putch_generic(img, off, (byte)buf[i]);
691 return w;
694 static void Z_gotoxy (int x, int y) {
695 prx = x;
696 pry = y;
699 static int Z_get_big_string_width (const char *fmt, ...) {
700 va_list a;
701 va_start(a, fmt);
702 int w = Z_get_string_width_generic(bfh, 12, fmt, a);
703 va_end(a);
704 return w;
707 static int Z_printbf (const char *fmt, ...) {
708 va_list a;
709 va_start(a, fmt);
710 int w = Z_printf_generic(bfh, 12, fmt, a);
711 va_end(a);
712 return w;
715 static int Z_get_small_string_width (const char *fmt, ...) {
716 va_list a;
717 va_start(a, fmt);
718 int w = Z_get_string_width_generic(sfh, 7, fmt, a);
719 va_end(a);
720 return w;
723 static int Z_printsf (const char *fmt, ...) {
724 va_list a;
725 va_start(a, fmt);
726 int w =Z_printf_generic(sfh, 7, fmt, a);
727 va_end(a);
728 return w;
731 static void Z_printhf (const char *fmt, ...) {
732 int i, c;
733 char buf[80];
734 va_list a;
735 va_start(a, fmt);
736 vsprintf(buf, fmt, a);
737 va_end(a);
738 for (i = 0; buf[i]; ++i) {
739 switch (buf[i]) {
740 case '0':
741 case '1':
742 case '2':
743 case '3':
744 case '4':
745 case '5':
746 case '6':
747 case '7':
748 case '8':
749 case '9':
750 c = buf[i] - '0';
751 break;
752 case '-':
753 c = 10;
754 break;
755 case '%':
756 c = 11;
757 break;
758 case '\n':
759 pry += 19;
760 case '\r':
761 c = -1;
762 prx = 0;
763 break;
764 default:
765 c = -1;
766 break;
768 if (c >= 0) {
769 R_gl_draw_image(&sth[c], prx, pry, 0);
771 prx += 14;
775 /* --- Menu --- */
777 static image *PL_getspr (int s, int d, int msk) {
778 int i = (s - 'A') * 2 + d;
779 return msk ? &plr_msk[i] : &plr_spr[i];
782 static int count_menu_entries (const new_menu_t *m) {
783 assert(m != NULL);
784 int i = 0;
785 while (m->entries[i].type != 0) {
786 i += 1;
788 return i;
791 #define SCROLLER_MIDDLE 10
792 #define TEXTFIELD_MIDDLE 2
794 static void get_entry_size (const new_menu_t *m, int i, int *w, int *h) {
795 assert(m != NULL);
796 assert(i >= 0);
797 assert(w != NULL);
798 assert(h != NULL);
799 int x, y;
800 new_msg_t msg;
801 switch (m->entries[i].type) {
802 case GM_BUTTON:
803 case GM_SCROLLER:
804 case GM_TEXTFIELD:
805 case GM_TEXTFIELD_BUTTON:
806 x = Z_get_big_string_width("%s", m->entries[i].caption);
807 break;
808 case GM_SMALL_BUTTON:
809 x = Z_get_small_string_width("%s", m->entries[i].caption);
810 break;
811 default:
812 assert(0);
814 switch (m->entries[i].type) {
815 case GM_BUTTON:
816 msg.type = GM_GETSTR;
817 if (GM_send(m, i, &msg)) {
818 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
820 y = 16;
821 break;
822 case GM_SMALL_BUTTON:
823 msg.type = GM_GETSTR;
824 if (GM_send(m, i, &msg)) {
825 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
827 y = 12;
828 break;
829 case GM_SCROLLER:
830 x += (SCROLLER_MIDDLE + 2) * 8;
831 y = 16;
832 break;
833 case GM_TEXTFIELD:
834 case GM_TEXTFIELD_BUTTON:
835 msg.type = GM_GETSTR;
836 if (GM_send(m, i, &msg)) {
837 x += (msg.string.maxlen + 2) * 8;
838 } else {
839 x += (TEXTFIELD_MIDDLE + 2) * 8;
841 y = 16;
842 break;
843 default:
844 assert(0);
846 *w = x;
847 *h = y;
850 static void get_menu_size (const new_menu_t *m, int *w, int *h) {
851 assert(m != NULL);
852 assert(w != NULL);
853 assert(h != NULL);
854 int i, x, y, xx, yy;
855 int n = count_menu_entries(m);
856 switch (m->type) {
857 case GM_BIG: x = Z_get_big_string_width("%s", m->title); break;
858 case GM_SMALL: x = Z_get_small_string_width("%s", m->title); break;
859 default: assert(0);
861 y = 0;
862 for (i = 0; i < n; i++) {
863 get_entry_size(m, i, &xx, &yy);
864 x = max(x, xx);
865 y += yy;
867 *w = x;
868 *h = y;
871 static int GM_draw (void) {
872 int i, j, xoff, yoff, n, x, y, cur, curoff, w, recv;
873 const new_menu_t *m = GM_get();
874 new_msg_t msg;
875 if (m != NULL) {
876 get_menu_size(m, &x, &y);
877 x = SCRW / 2 - x / 2;
878 y = SCRH / 2 - y / 2;
879 // --- title ---
880 switch (m->type) {
881 case GM_BIG:
882 yoff = 20;
883 Z_gotoxy(x, y - 10);
884 Z_printbf("%s", m->title);
885 break;
886 case GM_SMALL:
887 yoff = 8;
888 Z_gotoxy(x, y - 10);
889 Z_printsf("%s", m->title);
890 break;
891 default:
892 assert(0);
894 // --- entries ---
895 curoff = yoff;
896 cur = GM_geti();
897 n = count_menu_entries(m);
898 for (i = 0; i < n; i++) {
899 if (i == cur) {
900 curoff = yoff;
901 if (m->entries[cur].type == GM_SMALL_BUTTON) {
902 Z_gotoxy(x - 8, y + curoff);
903 Z_printsf(">");
904 } else {
905 R_gl_draw_image(&msklh[(gm_tm / 6) & 1], x - 25, y + curoff - 8, 0);
908 switch (m->entries[i].type) {
909 case GM_BUTTON:
910 Z_gotoxy(x, y + yoff);
911 w = Z_printbf("%s", m->entries[i].caption);
912 msg.type = GM_GETSTR;
913 if (GM_send(m, i, &msg)) {
914 Z_gotoxy(x + w, y + yoff);
915 Z_printbf("%.*s", msg.string.maxlen, msg.string.s);
917 yoff += 16;
918 break;
919 case GM_SMALL_BUTTON:
920 Z_gotoxy(x, y + yoff);
921 w = Z_printsf("%s", m->entries[i].caption);
922 msg.type = GM_GETSTR;
923 if (GM_send(m, i, &msg)) {
924 Z_gotoxy(x + w, y + yoff);
925 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
927 yoff += 12;
928 break;
929 case GM_TEXTFIELD:
930 case GM_TEXTFIELD_BUTTON:
931 msg.type = GM_GETSTR;
932 recv = GM_send(m, i, &msg);
933 Z_gotoxy(x, y + yoff);
934 xoff = Z_printbf("%s", m->entries[i].caption);
935 yoff += 9;
936 w = (recv ? msg.string.maxlen : TEXTFIELD_MIDDLE) + 1;
937 R_gl_draw_image(&mslotl, x + xoff, y + yoff, 0);
938 for (j = 1; j < w; j++) {
939 R_gl_draw_image(&mslotm, x + xoff + j * 8, y + yoff, 0);
941 R_gl_draw_image(&mslotr, x + xoff + j * 8, y + yoff, 0);
942 Z_gotoxy(x + xoff + 4, y + yoff - 7);
943 if (input && i == cur) {
944 Z_printsf("%.*s_", imax, ibuf);
945 } else if (recv) {
946 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
948 yoff += 7;
949 break;
950 case GM_SCROLLER:
951 Z_gotoxy(x, y + yoff);
952 xoff = Z_printbf("%s", m->entries[i].caption);
953 R_gl_draw_image(&mbarl, x + xoff, y + yoff, 0);
954 for (j = 1; j < SCROLLER_MIDDLE + 1; j++) {
955 R_gl_draw_image(&mbarm, x + xoff + j * 8, y + yoff, 0);
957 R_gl_draw_image(&mbarr, x + xoff + j * 8, y + yoff, 0);
958 msg.type = GM_GETINT;
959 if (GM_send(m, i, &msg)) {
960 int lev = (msg.integer.i - msg.integer.a) * ((SCROLLER_MIDDLE - 1) * 8) / msg.integer.b;
961 R_gl_draw_image(&mbaro, x + xoff + lev + 8, y + yoff, 0);
963 yoff += 16;
964 break;
965 default:
966 assert(0);
970 return m != NULL;
973 /* --- View --- */
975 static void R_draw_fld (byte *fld, int minx, int miny, int maxx, int maxy, int fg) {
976 int i, j;
977 assert(minx >= 0 && minx <= FLDW);
978 assert(miny >= 0 && miny <= FLDH);
979 assert(maxx >= 0 && maxx <= FLDW);
980 assert(maxy >= 0 && maxy <= FLDH);
981 for (j = miny; j < maxy; j++) {
982 for (i = minx; i < maxx; i++) {
983 byte id = fld[j * FLDW + i];
984 if (id != 0) {
985 if (walp[id].res < 0) {
986 if (fg) {
987 switch (R_get_special_id(id)) {
988 case 1:
989 glColor4ub(0, 0, 255, 127);
990 break;
991 case 2:
992 glColor4ub(0, 127, 0, 127);
993 break;
994 case 3:
995 glColor4ub(127, 0, 0, 127);
996 break;
997 default:
998 glColor4ub(0, 0, 0, 127);
999 break;
1001 glEnable(GL_BLEND);
1002 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1003 glDisable(GL_TEXTURE_2D);
1004 R_gl_draw_quad(i * CELW, j * CELW, CELW, CELH);
1006 } else {
1007 R_gl_draw_image(&walp[id], i * CELW, j * CELH, 0);
1014 static void R_draw_dots (void) {
1015 int i;
1016 glDisable(GL_BLEND);
1017 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1018 glDisable(GL_TEXTURE_2D);
1019 glBegin(GL_POINTS);
1020 for (i = 0; i < MAXDOT; i++) {
1021 if (dot[i].t != 0) {
1022 R_gl_set_color(dot[i].c);
1023 glVertex2i(dot[i].o.x, dot[i].o.y + 1);
1026 glEnd();
1029 static void R_draw_items (void) {
1030 int i, s;
1031 for (i = 0; i < MAXITEM; ++i) {
1032 s = -1;
1033 if (it[i].t && it[i].s >= 0) {
1034 switch (it[i].t & 0x7FFF) {
1035 case I_ARM1:
1036 s = it[i].s / 9 + 18;
1037 break;
1038 case I_ARM2:
1039 s = it[i].s / 9 + 20;
1040 break;
1041 case I_MEGA:
1042 s = it[i].s / 2 + 22;
1043 break;
1044 case I_INVL:
1045 s = it[i].s / 2 + 26;
1046 break;
1047 case I_SUPER:
1048 case I_RTORCH:
1049 case I_GTORCH:
1050 case I_BTORCH:
1051 s = it[i].s / 2 + (it[i].t - I_SUPER) * 4 + 35;
1052 break;
1053 case I_GOR1: case I_FCAN:
1054 s = it[i].s / 2 + (it[i].t - I_GOR1) * 3 + 51;
1055 break;
1056 case I_AQUA:
1057 s = 30;
1058 break;
1059 case I_SUIT:
1060 s = 34;
1061 break;
1062 case I_KEYR:
1063 case I_KEYG:
1064 case I_KEYB:
1065 s = (it[i].t & 0x7FFF) - I_KEYR + 31;
1066 break;
1067 case I_GUN2:
1068 s = 57;
1069 break;
1070 default:
1071 s = (it[i].t & 0x7FFF) - 1;
1074 if (s >= 0) {
1075 R_gl_draw_image(&item_spr[s], it[i].o.x, it[i].o.y, item_sprd[s]);
1080 static int standspr (player_t *p) {
1081 if (p->f & PLF_UP) {
1082 return 'X';
1083 } else if (p->f & PLF_DOWN) {
1084 return 'Z';
1085 } else {
1086 return 'E';
1090 static int wpnspr (player_t *p) {
1091 if (p->f & PLF_UP) {
1092 return 'C';
1093 } else if(p->f & PLF_DOWN) {
1094 return 'E';
1095 } else {
1096 return 'A';
1100 static void R_draw_player (player_t *p) {
1101 enum {STAND, GO, DIE, SLOP, DEAD, MESS, OUT, FALL}; // copypasted from player.c!
1102 static const int wytab[] = {-1, -2, -1, 0};
1103 int s = 'A';
1104 int w = 0;
1105 int wx = 0;
1106 int wy = 0;
1107 switch (p->st) {
1108 case STAND:
1109 if (p->f & PLF_FIRE) {
1110 s = standspr(p) + 1;
1111 w = wpnspr(p) + 1;
1112 } else if (p->pain) {
1113 s = 'G';
1114 w = 'A';
1115 wx = p->d ? 2 : -2;
1116 wy = 1;
1117 } else {
1118 s = standspr(p);
1119 w = wpnspr(p);
1121 break;
1122 case DEAD:
1123 s = 'N';
1124 break;
1125 case MESS:
1126 s = 'W';
1127 break;
1128 case GO:
1129 if (p->pain) {
1130 s = 'G';
1131 w = 'A';
1132 wx = p->d ? 2 : -2;
1133 wy = 1;
1134 } else {
1135 s = plr_goanim[p->s / 8];
1136 w = (p->f & PLF_FIRE) ? 'B' : 'A';
1137 wx = p->d ? 2 : -2;
1138 wy = 1 + wytab[s - 'A'];
1140 break;
1141 case DIE:
1142 s = plr_dieanim[p->s];
1143 break;
1144 case SLOP:
1145 s = plr_slopanim[p->s];
1146 break;
1147 case OUT:
1148 s = 0;
1149 break;
1151 if (p->wpn == 0) {
1152 w = 0;
1154 if (w) {
1155 R_gl_draw_image(&plr_wpn[(int)p->wpn][w -'A'], p->o.x + wx, p->o.y + wy, p->d);
1157 if (s) {
1158 R_gl_draw_image(&plr_spr[(s - 'A') * 2 + p->d], p->o.x, p->o.y, plr_sprd[(s - 'A') * 2 + p->d]);
1159 R_gl_set_color(p->color + PLAYER_COLOR_OFFSET);
1160 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]);
1164 static void R_draw_monsters (void) {
1165 enum {SLEEP, GO, RUN, CLIMB, DIE, DEAD, ATTACK, SHOOT, PAIN, WAIT, REVIVE, RUNOUT}; // copypasted from monster.c!
1166 int i;
1167 for (i = 0; i < MAXMN; i++) {
1168 if (mn[i].t != MN_NONE) {
1169 int x = mn[i].o.x;
1170 int y = mn[i].o.y;
1171 if (mn[i].t < MN__LAST) {
1172 if ((mn[i].t != MN_SOUL && mn[i].t != MN_PAIN) || mn[i].st != DEAD) {
1173 int ap = mn[i].ap[mn[i].ac];
1174 int d = (ap - 'A') * 2 + mn[i].d;
1175 int dir = mn_sprd[mn[i].t - 1][d];
1176 if (mn[i].t == MN_MAN && (ap == 'E' || ap == 'F')) {
1177 R_gl_draw_image(&mn_sgun[ap - 'E'], x, y, mn[i].d);
1179 R_gl_draw_image(&mn_spr[mn[i].t - 1][d], x, y, dir);
1180 if (mn[i].t == MN_MAN) {
1181 R_gl_set_color(MANCOLOR + PLAYER_COLOR_OFFSET);
1182 R_gl_draw_image_color(&mn_man_msk[d], x, y, dir);
1185 if (mn[i].t == MN_VILE && mn[i].st == SHOOT) {
1186 R_gl_draw_image(&mn_fspr[mn[i].ac / 3], mn[i].tx, mn[i].ty, 0);
1188 } else if (mn[i].t == MN_PL_DEAD || mn[i].t == MN_PL_MESS) {
1189 int type = mn[i].t - MN_PL_DEAD;
1190 R_gl_draw_image(&pl_spr[type], x, y, 0);
1191 R_gl_set_color(mn[i].d);
1192 R_gl_draw_image_color(&pl_msk[type], x, y, 0);
1198 static void R_draw_weapons (void) {
1199 enum {NONE, ROCKET, PLASMA, APLASMA, BALL1, BALL2, BALL7, BFGBALL, BFGHIT, MANF, REVF, FIRE}; // copypasted from weapons.c!
1200 int i, s, d, x, y;
1201 for (i = 0; i < MAXWPN; ++i) {
1202 s = -1;
1203 d = 0;
1204 switch (wp[i].t) {
1205 case REVF:
1206 case ROCKET:
1207 d = wp[i].s;
1208 if (d < 2) {
1209 d = wp[i].o.xv > 0 ? 1 : 0;
1210 x = abs(wp[i].o.xv);
1211 y = wp[i].o.yv;
1212 s = 0;
1213 if (y < 0) {
1214 if (-y >= x) {
1215 s = 30;
1217 } else if (y > 0) {
1218 if (y >= x / 2) {
1219 s = 31;
1222 } else {
1223 s = (d - 2) / 2 + 1;
1224 d = 0;
1226 break;
1227 case MANF:
1228 s=wp[i].s;
1229 if (s >= 2) {
1230 s /= 2;
1231 break;
1233 case PLASMA:
1234 case APLASMA:
1235 case BALL1:
1236 case BALL7:
1237 case BALL2:
1238 s = wp[i].s;
1239 if (s >= 2) {
1240 s = s / 2 + 1;
1242 switch (wp[i].t) {
1243 case PLASMA:
1244 s += 4;
1245 break;
1246 case APLASMA:
1247 s += 11;
1248 break;
1249 case BALL1:
1250 s += 32;
1251 break;
1252 case BALL2:
1253 s += 42;
1254 break;
1255 case BALL7:
1256 s += 37;
1257 d = wp[i].o.xv >= 0 ? 1 : 0;
1258 break;
1259 case MANF:
1260 s += 47;
1261 d= wp[i].o.xv>=0 ? 1 : 0;
1262 break;
1264 break;
1265 case BFGBALL:
1266 s = wp[i].s;
1267 if (s >= 2) {
1268 s = s / 2 + 1;
1270 s += 18;
1271 break;
1272 case BFGHIT:
1273 s = wp[i].s / 2 + 26;
1274 break;
1276 if (s >= 0) {
1277 R_gl_draw_image(&wp_spr[s * 2 + d], wp[i].o.x, wp[i].o.y, wp_sprd[s * 2 + d]);
1282 static void R_draw_smoke (void) {
1283 int i, s;
1284 for (i = 0; i < MAXSMOK; ++i) {
1285 if (sm[i].t) {
1286 switch (sm[i].s) {
1287 case 0:
1288 s = sm[i].t;
1289 if (s >= (SMSN - 1) * 3) {
1290 s = 0;
1291 } else {
1292 s = SMSN - 1 - s / 3;
1294 R_gl_draw_image(&smk_spr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1295 break;
1296 case 1:
1297 s = sm[i].t;
1298 if (s >= FLSN - 1) {
1299 s = 0;
1300 } else {
1301 s = FLSN - 1 - s;
1303 R_gl_draw_image(&smk_fspr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1304 break;
1310 static void R_draw_effects (void) {
1311 enum {NONE, TFOG, IFOG, BUBL}; // copypasted from fx.c
1312 int i, s;
1313 for (i = 0; i < MAXFX; ++i) {
1314 switch (fx[i].t) {
1315 case TFOG:
1316 s = fx[i].s / 2;
1317 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1318 break;
1319 case IFOG:
1320 s = fx[i].s / 2 + 10;
1321 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1322 break;
1323 case BUBL:
1324 glDisable(GL_BLEND);
1325 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1326 glDisable(GL_TEXTURE_2D);
1327 glBegin(GL_POINTS);
1328 R_gl_set_color(0xC0 + fx[i].s);
1329 glVertex2i(fx[i].x >> 8, (fx[i].y >> 8) + 1);
1330 glEnd();
1331 break;
1336 static int get_pu_st (int t) {
1337 if (t >= PL_FLASH) {
1338 return 1;
1339 } else if((t / 9) & 1) {
1340 return 0;
1341 } else {
1342 return 1;
1346 static void R_draw_view (int x, int y, int w, int h, int camx, int camy) {
1347 glPushMatrix();
1348 R_gl_setclip(x, y, w, h);
1349 glTranslatef(x, y, 0);
1350 if (w_horiz && horiz.n != NULL) {
1351 R_gl_draw_image_ext(&horiz, 0, 0, w, h);
1352 if (sky_type == 2 && lt_time < 0) {
1353 image *tanderbolt = &ltn[lt_type][lt_time < -5 ? 0 : 1];
1354 if (!lt_side) {
1355 R_gl_draw_image(tanderbolt, 0, lt_ypos, 0);
1356 } else {
1357 R_gl_draw_image(tanderbolt, w - 1, lt_ypos, 1);
1360 } else {
1361 glDisable(GL_BLEND);
1362 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1363 glDisable(GL_TEXTURE_2D);
1364 R_gl_set_color(DEFAULT_SKY_COLOR);
1365 R_gl_draw_quad(0, 0, w, h);
1367 int maxx = min((camx + w) / CELW + 1, FLDW);
1368 int maxy = min((camy + h) / CELH + 1, FLDH);
1369 int minx = max((camx - max_wall_width) / CELW, 0);
1370 int miny = max((camy - max_wall_height) / CELH, 0);
1371 glTranslatef(-camx, -camy, 0);
1372 R_draw_fld((byte*)fldb, minx, miny, maxx, maxy, 0);
1373 R_draw_dots();
1374 R_draw_items();
1375 R_draw_player(&pl1);
1376 if (_2pl) {
1377 R_draw_player(&pl2);
1379 R_draw_monsters();
1380 R_draw_weapons();
1381 R_draw_smoke();
1382 R_draw_effects();
1383 R_draw_fld((byte*)fldf, minx, miny, maxx, maxy, 1);
1384 glTranslatef(camx, camy, 0);
1385 if (sky_type == 2 && (lt_time == -4 || lt_time == -2)) {
1386 glColor4ub(255, 255, 255, 255);
1387 glEnable(GL_BLEND);
1388 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1389 glDisable(GL_TEXTURE_2D);
1390 R_gl_draw_quad(0, 0, w, h);
1392 glPopMatrix();
1395 static void R_draw_player_view (player_t *p, int x, int y, int w, int h) {
1396 p->looky = min(max(p->looky, -SCRH / 4), SCRH / 4); // TODO remove writeback
1397 int st = stone.w;
1398 int cw = w - st;
1399 int cx = min(max(p->o.x, cw / 2), FLDW * CELW - cw / 2);
1400 int cy = min(max(p->o.y - 12 + p->looky, h / 2), FLDH * CELH - h / 2);
1401 int camx = max(cx - cw / 2, 0);
1402 int camy = max(cy - h / 2, 0);
1403 glPushMatrix();
1404 R_draw_view(x, y + 1, cw, h - 2, camx, camy);
1405 glTranslatef(x, y, 0);
1406 if (p->invl) {
1407 if (get_pu_st(p->invl)) {
1408 glEnable(GL_BLEND);
1409 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1410 glDisable(GL_TEXTURE_2D);
1411 glColor4ub(191, 191, 191, 255);
1412 R_gl_draw_quad(0, 0, cw, h);
1414 } else {
1415 if (p->suit && get_pu_st(p->suit)) {
1416 glEnable(GL_BLEND);
1417 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1418 glDisable(GL_TEXTURE_2D);
1419 glColor4ub(0, 255, 0, 192);
1420 R_gl_draw_quad(0, 0, cw, h);
1422 int f = min(max(p->pain * 3, 0), 255);
1423 if (f > 0) {
1424 glEnable(GL_BLEND);
1425 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1426 glDisable(GL_TEXTURE_2D);
1427 glColor4ub(255, 0, 0, f);
1428 R_gl_draw_quad(0, 0, cw, h);
1431 R_gl_setclip(x, y, w, h);
1432 glTranslatef(-x + cw, 0, 0);
1433 R_gl_draw_image(&stone, 0, 0, 0);
1434 int i = stone.h;
1435 while (i < h) {
1436 R_gl_draw_image(&stone2, 0, i, 0);
1437 i += stone2.h;
1439 if (p->drawst & PL_DRAWAIR) {
1440 if (p->air < PL_AIR) {
1441 int a = min(max(p->air, 0), MAXAIR) * 100 / MAXAIR;
1442 glDisable(GL_BLEND);
1443 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1444 glDisable(GL_TEXTURE_2D);
1445 R_gl_set_color(0xC8);
1446 R_gl_draw_quad(10, 49, a, 2);
1449 if (p->drawst & PL_DRAWLIFE) {
1450 Z_gotoxy(10, 7);
1451 Z_printhf("%3d%%", p->life);
1453 if (p->drawst & PL_DRAWARMOR) {
1454 Z_gotoxy(10, 7 + 19);
1455 Z_printhf("%3d%%", p->armor);
1457 if (p->drawst & PL_DRAWWPN) {
1458 switch(p->wpn) {
1459 case 2:
1460 case 5:
1461 i = p->ammo;
1462 break;
1463 case 3:
1464 case 4:
1465 case 9:
1466 i = p->shel;
1467 break;
1468 case 6:
1469 i = p->rock;
1470 break;
1471 case 7:
1472 case 8:
1473 i = p->cell;
1474 break;
1475 case 10:
1476 i = p->fuel;
1477 break;
1478 default:
1479 i = -1;
1480 break;
1482 // weapon
1483 if (p->wpn >= 0) {
1484 R_gl_draw_image(&sth[12 + p->wpn], st - 88, 58 + 19, 0);
1486 // ammo
1487 if (p->wpn >= 2) {
1488 Z_gotoxy(st - 10 - 5 * 14, 58 + 2);
1489 Z_printhf("%5d", i);
1492 if (p->drawst & PL_DRAWFRAG && g_dm) {
1493 Z_gotoxy(st - 5 - 5 * 14, 77 + 5);
1494 Z_printhf("%5d", p->frag);
1496 if (p->drawst & PL_DRAWKEYS) {
1497 int x, k, n;
1498 for (k = p->keys >> 4, n = 0, x = st - 75; n < 3; n++, k >>= 1, x += 9) {
1499 if (k & 1) {
1500 R_gl_draw_image(&keys[n], x, 91, 0);
1504 if (p->drawst & PL_DRAWLIVES && !_2pl) {
1505 Z_gotoxy(st - 35, 17);
1506 Z_printhf("%d", p->lives);
1508 glPopMatrix();
1511 /* --- Game --- */
1513 static void pl_info (player_t *p, int x, int y) {
1514 dword t = p->kills * 10920 / g_time;
1515 Z_gotoxy(x + 25, y); Z_printbf("KILLS");
1516 Z_gotoxy(x + 25, y + 15); Z_printbf("KPM");
1517 Z_gotoxy(x + 25, y + 30); Z_printbf("SECRETS %u / %u", p->secrets, sw_secrets);
1518 Z_gotoxy(x + 255, y); Z_printbf("%u", p->kills);
1519 Z_gotoxy(x + 255, y + 15); Z_printbf("%u.%u", t / 10, t % 10);
1522 static void R_draw_intermission (void) {
1523 int cx = SCRW / 2;
1524 word hr, mn, sc, h;
1525 Z_gotoxy(cx - 14*12/2, 20);
1526 Z_printbf("LEVEL COMPLETE");
1527 Z_calc_time(g_time, &hr, &mn, &sc);
1528 Z_gotoxy(cx - 12*12/2, 40);
1529 Z_printbf("TIME %u:%02u:%02u", hr, mn, sc);
1530 h = 40 + SCRH / 10;
1531 if (_2pl) {
1532 Z_gotoxy(cx - 10*12/2, h);
1533 Z_printbf("PLAYER ONE");
1534 h += 20;
1536 pl_info(&pl1, cx - 160, h);
1537 if (_2pl) {
1538 h += 30 + SCRH / 10;
1539 Z_gotoxy(cx - 10*12/2, h);
1540 Z_printbf("PLAYER TWO");
1541 h += 20;
1542 pl_info(&pl2, cx - 160, h);
1546 static void W_act (void) {
1547 int i, a;
1548 if (g_time % 3 == 0) {
1549 for (i = 1; i < max_textures; i++) {
1550 a = walani[i];
1551 if (a != 0) {
1552 anic[a]++;
1553 if (anip[a][anic[a]].res == -1) {
1554 anic[a] = 0;
1556 walp[i] = anip[a][anic[a]];
1562 void R_draw (void) {
1563 W_act();
1564 glClearColor(0, 0, 0, 1);
1565 glClear(GL_COLOR_BUFFER_BIT);
1566 glEnable(GL_SCISSOR_TEST);
1567 R_gl_setmatrix();
1568 switch (g_st) {
1569 case GS_ENDANIM:
1570 case GS_END2ANIM:
1571 case GS_DARKEN:
1572 case GS_BVIDEO:
1573 case GS_EVIDEO:
1574 case GS_END3ANIM:
1575 break;
1576 case GS_TITLE:
1577 R_gl_draw_image_ext(&scrnh[0], 0, 0, SCRW, SCRH);
1578 break;
1579 case GS_INTER:
1580 R_gl_draw_image_ext(&scrnh[1], 0, 0, SCRW, SCRH);
1581 R_draw_intermission();
1582 break;
1583 case GS_ENDSCR:
1584 R_gl_draw_image_ext(&scrnh[2], 0, 0, SCRW, SCRH);
1585 break;
1586 case GS_GAME:
1587 if (_2pl) {
1588 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH / 2);
1589 R_draw_player_view(&pl2, 0, SCRH / 2, SCRW, SCRH / 2);
1590 } else {
1591 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH);
1593 R_gl_setclip(0, 0, SCRW, SCRH);
1594 break;
1596 GM_draw();
1597 Y_swap_buffers();
1600 static void R_alloc (void) {
1601 char s[10];
1602 int i, j, n;
1603 logo("R_alloc: load graphics\n");
1604 /* Game */
1605 scrnh[0] = R_gl_loadimage("TITLEPIC");
1606 scrnh[1] = R_gl_loadimage("INTERPIC");
1607 scrnh[2] = R_gl_loadimage("ENDPIC");
1608 for (i = 0; i < 2; i++) {
1609 sprintf(s, "LTN%c", '1' + i);
1610 for (j = 0; j < 2; j++) {
1611 ltn[i][j] = Z_getspr(s, j, 0, NULL);
1614 /* Smoke */
1615 for (i = 0; i < SMSN; i++) {
1616 smk_spr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_smoke_spr);
1618 for (i = 0; i < FLSN; i++) {
1619 smk_fspr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_flame_spr);
1621 /* Effects */
1622 for (i = 0; i < 10; i++) {
1623 fx_spr[i] = Z_getspr("TFOG", i, 0, fx_sprd + i);
1625 for (; i < 15; i++) {
1626 fx_spr[i] = Z_getspr("IFOG", i - 10, 0, fx_sprd + i);
1628 /* Weapons */
1629 for (i = 0; i < 4; i++) {
1630 wp_spr[i * 2] = Z_getspr("MISL", i, 1, wp_sprd + i * 2);
1631 wp_spr[i * 2 + 1] = Z_getspr("MISL", i, 2, wp_sprd + i * 2 + 1);
1633 for (; i < 6; i++) {
1634 wp_spr[i * 2] = Z_getspr("PLSS", i - 4, 1, wp_sprd + i * 2);
1635 wp_spr[i * 2 + 1] = Z_getspr("PLSS", i - 4, 2, wp_sprd + i * 2 + 1);
1637 for (; i < 11; i++) {
1638 wp_spr[i * 2] = Z_getspr("PLSE", i - 6, 1, wp_sprd + i * 2);
1639 wp_spr[i * 2 + 1] = Z_getspr("PLSE", i - 6, 2, wp_sprd + i * 2 + 1);
1641 for (; i < 13; i++) {
1642 wp_spr[i * 2] = Z_getspr("APLS", i - 11, 1, wp_sprd + i * 2);
1643 wp_spr[i * 2 + 1] = Z_getspr("APLS", i - 11, 2, wp_sprd + i * 2 + 1);
1645 for (; i < 18; i++) {
1646 wp_spr[i * 2] = Z_getspr("APBX", i - 13, 1, wp_sprd + i * 2);
1647 wp_spr[i * 2 + 1] = Z_getspr("APBX", i - 13, 2, wp_sprd + i * 2 + 1);
1649 for(; i < 20; i++) {
1650 wp_spr[i * 2] = Z_getspr("BFS1", i - 18, 1, wp_sprd + i * 2);
1651 wp_spr[i * 2 + 1] = Z_getspr("BFS1", i - 18, 2, wp_sprd + i * 2 + 1);
1653 for (; i < 26; i++) {
1654 wp_spr[i * 2] = Z_getspr("BFE1", i - 20, 1, wp_sprd + i * 2);
1655 wp_spr[i * 2 + 1] = Z_getspr("BFE1", i - 20, 2, wp_sprd + i * 2 + 1);
1657 for (; i < 30; i++) {
1658 wp_spr[i * 2] = Z_getspr("BFE2", i - 26, 1, wp_sprd + i * 2);
1659 wp_spr[i * 2 + 1] = Z_getspr("BFE2", i - 26, 2, wp_sprd + i * 2 + 1);
1661 for (; i < 32; i++) {
1662 wp_spr[i * 2] = Z_getspr("MISL", i - 30 + 4, 1, wp_sprd + i * 2);
1663 wp_spr[i * 2 + 1] = Z_getspr("MISL", i - 30 + 4, 2, wp_sprd + i * 2 + 1);
1665 for (; i < 37; i++) {
1666 wp_spr[i * 2] = Z_getspr("BAL1", i - 32, 1, wp_sprd + i * 2);
1667 wp_spr[i * 2 + 1] = Z_getspr("BAL1", i - 32, 2, wp_sprd + i * 2 + 1);
1669 for (; i < 42; i++) {
1670 wp_spr[i * 2] = Z_getspr("BAL7", i - 37, 1, wp_sprd + i * 2);
1671 wp_spr[i * 2 + 1] = Z_getspr("BAL7", i - 37, 2, wp_sprd + i * 2 + 1);
1673 for (; i < 47; i++) {
1674 wp_spr[i * 2] = Z_getspr("BAL2", i - 42, 1, wp_sprd + i * 2);
1675 wp_spr[i * 2 + 1] = Z_getspr("BAL2", i - 42, 2, wp_sprd + i * 2 + 1);
1677 for (; i < 49; i++) {
1678 wp_spr[i * 2] = Z_getspr("MANF", i - 47, 1, wp_sprd + i * 2);
1679 wp_spr[i * 2 + 1] = Z_getspr("MANF", i - 47, 2, wp_sprd + i * 2 + 1);
1681 /* Items */
1682 static const char snm[18][4] = {
1683 "CLIP", "SHEL", "ROCK", "CELL", "AMMO", "SBOX", "BROK", "CELP",
1684 "STIM", "MEDI", "BPAK",
1685 "CSAW", "SHOT", "SGN2", "MGUN", "LAUN", "PLAS", "BFUG"
1686 };
1687 static const char n4[4][4] = {
1688 "SOUL", "SMRT", "SMGT", "SMBT"
1689 };
1690 static const char n3[2][4] = {
1691 "GOR1", "FCAN"
1692 };
1693 for (i = 0; i < 18; i++) {
1694 item_spr[i] = Z_getspr(snm[i], 0, 0, item_sprd + i);
1696 for (; i < 20; i++) {
1697 item_spr[i] = Z_getspr("ARM1", i - 18, 0, item_sprd + i);
1698 item_spr[i + 2] = Z_getspr("ARM2", i - 18, 0, item_sprd + i);
1700 i+=2;
1701 for (; i < 26; i++) {
1702 item_spr[i] = Z_getspr("MEGA", i - 22, 0, item_sprd + i);
1704 for (; i < 30; i++) {
1705 item_spr[i] = Z_getspr("PINV", i - 26, 0, item_sprd + i);
1707 item_spr[30] = Z_getspr("AQUA", 0, 0, item_sprd + 30);
1708 item_spr[31] = Z_getspr("KEYR", 0, 0, item_sprd + 31);
1709 item_spr[32] = Z_getspr("KEYG", 0, 0, item_sprd + 32);
1710 item_spr[33] = Z_getspr("KEYB", 0, 0, item_sprd + 33);
1711 item_spr[34] = Z_getspr("SUIT", 0, 0, item_sprd + 34);
1712 for (n = 35, j = 0; j < 4; j++) {
1713 for (i = 0; i < 4; i++, n++) {
1714 item_spr[n] = Z_getspr(n4[j], i, 0, item_sprd + n);
1717 for (j = 0; j < 2; j++) {
1718 for (i = 0; i < 3; i++, n++) {
1719 item_spr[n] = Z_getspr(n3[j], i, 0, item_sprd + n);
1722 item_spr[57] = Z_getspr("GUN2", 0, 0, item_sprd + 57);
1723 /* Player */
1724 for (i = 0; i < 27; i++) {
1725 plr_spr[i * 2] = Z_getspr("PLAY", i, 1, plr_sprd + i * 2);
1726 plr_msk[i * 2] = R_gl_get_special_spr("PLAY", i, 1, &R_extract_mask_spr);
1727 plr_spr[i * 2 + 1] = Z_getspr("PLAY", i, 2, plr_sprd + i * 2 + 1);
1728 plr_msk[i * 2 + 1] = R_gl_get_special_spr("PLAY", i, 2, &R_extract_mask_spr);
1730 strncpy(s, "PWPx", 4);
1731 for (i = 1; i < 11; i++) {
1732 s[3] = (i < 10 ? '0' : 'A' - 10) + i;
1733 for (j = 0; j < 6; j++) {
1734 plr_wpn[i][j] = Z_getspr(s, j, 1, NULL);
1737 /* Monsters */
1738 static const char msn[MN_TN][4] = {
1739 "SARG", "TROO", "POSS", "SPOS", "CYBR", "CPOS", "BOSS", "BOS2", "HEAD", "SKUL",
1740 "PAIN", "SPID", "BSPI", "FATT", "SKEL", "VILE", "FISH", "BAR1", "ROBO", "PLAY"
1741 };
1742 static const int mms[MN_TN] = {
1743 14*2, 21*2, 21*2, 21*2, 16*2, 20*2, 15*2, 15*2, 12*2, 11*2,
1744 13*2, 19*2, 16*2, 20*2, 17*2, 29*2, 6*2, 2*2, 17*2, 23*2
1745 };
1746 mn_sgun[0] = Z_getspr("PWP4", 0, 1, NULL);
1747 mn_sgun[1] = Z_getspr("PWP4", 1, 1, NULL);
1748 for (j = 0; j < MN_TN; j++) {
1749 for (i = 0; i < mms[j]; i++) {
1750 mn_spr[j][i] = Z_getspr(msn[j], i / 2, (i & 1) + 1, &mn_sprd[j][i]);
1751 if (j == MN_MAN - 1) {
1752 mn_man_msk[i] = R_gl_get_special_spr(msn[j], i / 2, (i & 1) + 1, &R_extract_mask_spr);
1755 if (j == MN_BARREL - 1) {
1756 for (i = 4; i < 14; i++) {
1757 mn_spr[j][i] = Z_getspr("BEXP", i / 2 - 2, (i & 1) + 1, &mn_sprd[j][i]);
1761 for (i = 0; i < 8; i++) {
1762 mn_fspr[i] = Z_getspr("FIRE", i, 0, NULL);
1764 pl_spr[0] = Z_getspr("PLAY", 'N' - 'A', 0, NULL);
1765 pl_msk[0] = R_gl_get_special_spr("PLAY", 'N' - 'A', 0, &R_extract_mask_spr);
1766 pl_spr[1] = Z_getspr("PLAY", 'W' - 'A', 0, NULL);
1767 pl_msk[1] = R_gl_get_special_spr("PLAY", 'W' - 'A', 0, &R_extract_mask_spr);
1768 /* Misc */
1769 static const char mnm[22][8]={
1770 "STTNUM0", "STTNUM1", "STTNUM2", "STTNUM3", "STTNUM4",
1771 "STTNUM5", "STTNUM6", "STTNUM7", "STTNUM8", "STTNUM9",
1772 "STTMINUS", "STTPRCNT",
1773 "FISTA0", "CSAWA0", "PISTA0", "SHOTA0", "SGN2A0", "MGUNA0", "LAUNA0",
1774 "PLASA0", "BFUGA0", "GUN2A0"
1775 };
1776 stone = R_gl_loadimage("STONE");
1777 stone2 = R_gl_loadimage("STONE2");
1778 keys[0] = R_gl_loadimage("KEYRA0");
1779 keys[1] = R_gl_loadimage("KEYGA0");
1780 keys[2] = R_gl_loadimage("KEYBA0");
1781 for (i = 0; i < 22; i++) {
1782 sth[i] = R_gl_loadimage(mnm[i]);
1784 strcpy(s, "STBF_*");
1785 for (i = '!'; i < 160; i++) {
1786 s[5] = i;
1787 bfh[i - '!'] = R_gl_getimage(F_findres(s));
1789 for (i = '!'; i < 160; i++) {
1790 sprintf(s, "STCFN%03d", i);
1791 sfh[i - '!'] = R_gl_getimage(F_findres(s));
1793 strcpy(s, "WINUM*");
1794 for (i = '0'; i <= '9'; i++) {
1795 s[5] = i;
1796 bfh[i - '!'] = R_gl_loadimage(s);
1798 bfh[':' - '!'] = R_gl_loadimage("WICOLON");
1799 // menu
1800 msklh[0] = R_gl_loadimage("M_SKULL1");
1801 msklh[1] = R_gl_loadimage("M_SKULL2");
1802 mbarl = R_gl_loadimage("M_THERML");
1803 mbarm = R_gl_loadimage("M_THERMM");
1804 mbarr = R_gl_loadimage("M_THERMR");
1805 mbaro = R_gl_loadimage("M_THERMO");
1806 mslotl = R_gl_loadimage("M_LSLEFT");
1807 mslotm = R_gl_loadimage("M_LSCNTR");
1808 mslotr = R_gl_loadimage("M_LSRGHT");
1809 // walls
1810 for (i = 1; i < ANIT; i++) {
1811 for (j = 0; j < 5 && anm[i - 1][j]; j++) {
1812 anip[i][j] = R_gl_loadimage(anm[i - 1][j]);
1814 for(; j < 5; j++) {
1815 anip[i][j] = (image) {
1816 .n = NULL,
1817 .w = 8,
1818 .h = 8,
1819 .res = -1,
1820 };
1825 static void R_reload_textures (void);
1827 void R_set_videomode (int w, int h, int fullscreen) {
1828 assert(w > 0);
1829 assert(h > 0);
1830 int was = Y_videomode_setted();
1831 if (root != NULL) {
1832 R_cache_free(root, 0);
1833 root = NULL;
1835 int res = Y_set_videomode_opengl(w, h, fullscreen);
1836 if (res == 0) {
1837 if (was == 0) {
1838 ERR_failinit("Unable to set video mode\n");
1841 Y_get_videomode(&SCRW, &SCRH);
1842 root = R_cache_new();
1843 assert(root);
1844 R_alloc();
1845 R_reload_textures();
1848 static int R_video_menu_handler (new_msg_t *msg, const new_menu_t *m, void *data) {
1849 const videomode_t *v;
1850 intptr_t i = (intptr_t)data;
1851 static int w, h;
1852 static int vmode;
1853 static int fullscreen;
1854 static char str[16];
1855 switch (i) {
1856 case -1:
1857 switch (msg->type) {
1858 case GM_ENTER:
1859 Y_get_videomode(&w, &h);
1860 fullscreen = Y_get_fullscreen();
1861 v = Y_get_videomode_list_opengl(fullscreen);
1862 vmode = 0;
1863 while (vmode < v->n && v->modes[vmode].w != w && v->modes[vmode].h != h) {
1864 vmode += 1;
1866 if (vmode < v->n) {
1867 w = v->modes[vmode].w;
1868 h = v->modes[vmode].h;
1870 snprintf(str, 16, "%ix%i", w, h);
1871 return 1;
1873 break;
1874 case 0:
1875 switch (msg->type) {
1876 case GM_SELECT:
1877 v = Y_get_videomode_list_opengl(fullscreen);
1878 vmode = vmode + 1 >= v->n ? 0 : vmode + 1;
1879 if (v->n > 0) {
1880 w = v->modes[vmode].w;
1881 h = v->modes[vmode].h;
1882 } else {
1883 Y_get_videomode(&w, &h);
1885 snprintf(str, 16, "%ix%i", w, h);
1886 return 1;
1887 case GM_GETSTR:
1888 return GM_init_str(msg, str, 16);
1890 break;
1891 case 1:
1892 switch (msg->type) {
1893 case GM_SELECT: fullscreen = !fullscreen; return 1;
1894 case GM_GETSTR: return GM_init_str(msg, fullscreen ? "Yes" : "No", 3);
1896 break;
1897 case 2:
1898 switch (msg->type) {
1899 case GM_SELECT: R_set_videomode(w, h, fullscreen); return 1;
1902 return 0;
1905 static const new_menu_t video_menu = {
1906 GM_BIG, "Video", (void*)-1, &R_video_menu_handler,
1908 { GM_BUTTON, "Videomode: ", (void*)0, &R_video_menu_handler, NULL },
1909 { GM_BUTTON, "Fullscreen: ", (void*)1, &R_video_menu_handler, NULL },
1910 { GM_BUTTON, "Apply", (void*)2, &R_video_menu_handler, NULL },
1911 { 0, NULL, NULL, NULL, NULL } // end
1913 };
1915 const new_menu_t *R_menu (void) {
1916 return &video_menu;
1919 void R_init (void) {
1920 logo("R_init: intialize opengl render\n");
1921 R_init_playpal();
1922 R_set_videomode(SCRW, SCRH, 0);
1925 void R_done (void) {
1926 R_cache_free(root, 1);
1927 Y_unset_videomode();
1928 root = NULL;
1931 void R_get_name (int n, char s[8]) {
1932 assert(n >= 0 && n < 256);
1933 if (walp[n].res == -1) {
1934 memset(s, 0, 8);
1935 } else if (walp[n].res == -2) {
1936 memcpy(s, "_WATER_", 8);
1937 s[7] = '0' + (intptr_t)walp[n].n - 1;
1938 } else if (walani[n] > 0) {
1939 memcpy(s, anm[walani[n] - 1][0], 8);
1940 } else {
1941 F_getresname(s, walp[n].res & 0x7FFF);
1945 static short getani (char n[8]) {
1946 short i = 0;
1947 while (i < ANIT - 1 && strncasecmp(n, anm[i][0], 8) != 0) {
1948 i++;
1950 return i < ANIT - 1 ? i + 1 : 0;
1953 int R_get_special_id (int n) {
1954 assert(n >= 0 && n <= 256);
1955 return walp[n].res == -2 ? (intptr_t)walp[n].n : -1;
1958 static void R_reload_textures (void) {
1959 int i;
1960 char s[8];
1961 for (i = 0; i < max_textures; i++) {
1962 R_get_name(i, s);
1963 if (walp[i].res >= 0) {
1964 walp[i] = R_gl_getimage(walp[i].res);
1967 if (horiz.n) {
1968 horiz = R_gl_getimage(horiz.res);
1972 void R_begin_load (void) {
1973 int i;
1974 for (i = 0; i < 256; i++) {
1975 if (walp[i].n != NULL && walp[i].res >= 0 && walani[i] == 0) {
1976 R_gl_free_image(&walp[i]);
1978 memset(&walp[i], 0, sizeof(image));
1979 walp[i].res = -1;
1980 walswp[i] = i;
1981 walani[i] = 0;
1983 memset(anic, 0, sizeof(anic));
1984 max_wall_width = 0;
1985 max_wall_height = 0;
1986 max_textures = 1;
1989 void R_load (char s[8], int f) {
1990 assert(max_textures < 256);
1991 if (!s[0]) {
1992 walp[max_textures] = (image) {
1993 .n = NULL,
1994 .x = 0,
1995 .y = 0,
1996 .w = 0,
1997 .h = 0,
1998 .res = -1,
1999 };
2000 } else if (strncasecmp(s, "_WATER_", 7) == 0) {
2001 walp[max_textures] = (image) {
2002 .n = (void*)((intptr_t)s[7] - '0' + 1),
2003 .x = 0,
2004 .y = 0,
2005 .w = 8,
2006 .h = 8,
2007 .res = -2,
2008 };
2009 } else {
2010 walp[max_textures] = R_gl_loadimage(s);
2011 if (f) {
2012 walp[max_textures].res |= 0x8000;
2014 if (s[0] == 'S' && s[1] == 'W' && s[4] == '_') {
2015 walswp[max_textures] = 0;
2017 walani[max_textures] = getani(s);
2019 max_wall_width = max(max_wall_width, walp[max_textures].w);
2020 max_wall_height = max(max_wall_height, walp[max_textures].h);
2021 max_textures++;
2024 void R_end_load (void) {
2025 int i, j, k, g;
2026 char s[8];
2027 j = max_textures;
2028 for (i = 1; i < 256 && j < 256; i++) {
2029 if (walswp[i] == 0) {
2030 R_get_name(i, s);
2031 s[5] ^= 1;
2032 g = F_getresid(s) | (walp[i].res & 0x8000);
2033 k = 1;
2034 while (k < 256 && walp[k].res != g) {
2035 k += 1;
2037 if (k >= 256) {
2038 k = j;
2039 j += 1;
2040 max_textures += 1;
2041 walp[k] = R_gl_getimage(g);
2042 walf[k] = g & 0x8000 ? 1 : 0;
2044 walswp[i] = k;
2045 walswp[k] = i;
2050 void R_loadsky (int sky) {
2051 char s[6];
2052 strcpy(s, "RSKYx");
2053 s[4] = '0' + sky;
2054 R_gl_free_image(&horiz);
2055 horiz = R_gl_loadimage(s);