DEADSOFTWARE

render: opengl: add screen scaling
[flatwaifu.git] / src / gl / render.c
1 #include "glob.h"
2 #include "render.h"
3 #include "system.h"
4 #include "files.h"
5 #include "memory.h"
6 #include "misc.h"
7 #include "error.h"
9 #include "menu.h"
10 #include "game.h"
11 #include "dots.h"
12 #include "items.h"
14 #include "sound.h" // snd_vol
15 #include "music.h" // mus_vol
17 #include "fx.h"
18 #include "player.h"
19 #include "monster.h"
20 #include "weapons.h"
21 #include "smoke.h"
22 #include "view.h"
23 #include "switch.h" // sw_secrets
25 #include "cp866.h"
27 #ifdef __APPLE__
28 # include <OpenGL/gl.h>
29 #else
30 # include <GL/gl.h>
31 #endif
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <assert.h>
37 #define VGA_TRANSPARENT_COLOR 0
38 #define DEFAULT_SKY_COLOR 0x97
39 #define MANCOLOR 0xD0
40 #define PLAYER_COLOR_OFFSET 7
41 #define MAXAIR 1091
42 #define ANIT 5
43 #define PL_FLASH 90
45 #pragma pack(1)
46 typedef struct vgaimg {
47 word w, h;
48 short x, y;
49 byte data[];
50 } vgaimg;
52 typedef struct rgb {
53 byte r, g, b;
54 } rgb;
56 typedef struct rgba {
57 byte r, g, b, a;
58 } rgba;
59 #pragma pack()
61 typedef struct node {
62 struct cache *base;
63 struct node *left, *right;
64 struct node *up;
65 int l, t, r, b;
66 int leaf;
67 } node;
69 typedef struct cache {
70 struct cache *next;
71 struct node root;
72 GLuint id;
73 } cache;
75 typedef struct image {
76 node *n;
77 GLint x, y;
78 GLuint w, h;
79 int res;
80 } image;
82 /* Render Specific */
83 static int SCRW = 320;
84 static int SCRH = 200;
85 static int screen_width = 320;
86 static int screen_height = 200;
87 static float screen_scale = 1.0;
88 static rgb playpal[256];
89 static byte bright[256];
90 static GLuint lastTexture;
91 static cache *root;
93 /* Game */
94 static image scrnh[3]; // TITLEPIC INTERPIC ENDPIC
95 static image ltn[2][2];
97 /* Smoke */
98 static image smk_spr[SMSN];
99 static image smk_fspr[FLSN];
101 /* Effects */
102 static image fx_spr[15];
103 static char fx_sprd[15];
105 /* Weapons */
106 static image wp_spr[49*2];
107 static char wp_sprd[49*2];
109 /* Items */
110 static image item_spr[58];
111 static char item_sprd[58];
113 /* Player */
114 static image plr_spr[27*2];
115 static image plr_msk[27*2];
116 static char plr_sprd[27*2];
117 static image plr_wpn[11][6];
119 /* Monsters */
120 static image pl_spr[2];
121 static image pl_msk[2];
122 static image mn_spr[MN_TN][29*2];
123 static image mn_man_msk[29*2];
124 static char mn_sprd[MN_TN][29*2];
125 static image mn_fspr[8];
126 static image mn_sgun[2];
128 /* Misc */
129 static image sth[22];
130 static image bfh[160 - '!'];
131 static image sfh[160 - '!'];
132 static image stone;
133 static image stone2;
134 static image keys[3];
135 static int prx = 0;
136 static int pry = 0;
138 /* Menu */
139 static int gm_tm;
140 static image msklh[2];
141 static image mbarl;
142 static image mbarm;
143 static image mbarr;
144 static image mbaro;
145 static image mslotl;
146 static image mslotm;
147 static image mslotr;
149 /* Map */
150 static const char *anm[ANIT - 1][5] = {
151 {"WALL22_1", "WALL23_1", "WALL23_2", NULL, NULL},
152 {"WALL58_1", "WALL58_2", "WALL58_3", NULL, NULL},
153 {"W73A_1", "W73A_2", NULL, NULL, NULL},
154 {"RP2_1", "RP2_2", "RP2_3", "RP2_4", NULL}
155 };
156 static int max_wall_width;
157 static int max_wall_height;
158 static int max_textures;
159 static image walp[256];
160 static byte walani[256];
161 static image anip[ANIT][5];
162 static byte anic[ANIT];
163 static image horiz;
165 /* Texture cache */
167 // https://blackpawn.com/texts/lightmaps/
168 static node *R_node_alloc (node *p, int w, int h) {
169 assert(p);
170 assert(w > 0);
171 assert(h > 0);
172 if (p->left) {
173 assert(p->right);
174 node *n = R_node_alloc(p->left, w, h);
175 return n ? n : R_node_alloc(p->right, w, h);
176 } else {
177 int pw = p->r - p->l + 1;
178 int ph = p->b - p->t + 1;
179 if (p->leaf || pw < w || ph < h) {
180 return NULL;
181 } else if (pw == w && ph == h) {
182 p->leaf = 1;
183 return p;
184 } else {
185 p->left = malloc(sizeof(node));
186 p->right = malloc(sizeof(node));
187 if (pw - w > ph - h) {
188 *p->left = (node) {
189 .up = p,
190 .l = p->l,
191 .t = p->t,
192 .r = p->l + w - 1,
193 .b = p->b
194 };
195 *p->right = (node) {
196 .up = p,
197 .l = p->l + w,
198 .t = p->t,
199 .r = p->r,
200 .b = p->b
201 };
202 } else {
203 *p->left = (node) {
204 .up = p,
205 .l = p->l,
206 .t = p->t,
207 .r = p->r,
208 .b = p->t + h - 1
209 };
210 *p->right = (node) {
211 .up = p,
212 .l = p->l,
213 .t = p->t + h,
214 .r = p->r,
215 .b = p->b
216 };
218 return R_node_alloc(p->left, w, h);
223 static int R_node_have_leaf (node *n) {
224 return n && (n->leaf || R_node_have_leaf(n->left) || R_node_have_leaf(n->right));
227 static void R_node_free_recursive (node *n) {
228 if (n) {
229 R_node_free_recursive(n->left);
230 R_node_free_recursive(n->right);
231 free(n);
235 static void R_node_free (node *n) {
236 if (n) {
237 //logo("free node %p {%i:%i:%i:%i}\n", n, n->l, n->t, n->r, n->b);
238 assert(n->leaf);
239 assert(n->left == NULL);
240 assert(n->right == NULL);
241 n->leaf = 0;
242 n->base = NULL;
243 node *p = n->up;
244 while (p != NULL) {
245 assert(p->leaf == 0);
246 assert(p->left);
247 assert(p->right);
248 if (R_node_have_leaf(p) == 0) {
249 R_node_free_recursive(p->left);
250 p->left = NULL;
251 R_node_free_recursive(p->right);
252 p->right = NULL;
253 p = p->up;
254 } else {
255 p = NULL;
261 static void R_cache_get_max_texture_size (int *w, int *h) {
262 GLint size = 0;
263 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
264 size = min(max(size, 0), 512); // more can be buggy on older hardware
265 *w = size;
266 *h = size;
269 static void R_gl_bind_texture (GLuint id) {
270 if (id != lastTexture) {
271 glBindTexture(GL_TEXTURE_2D, id);
275 static cache *R_cache_new (void) {
276 int w, h;
277 GLuint id;
278 cache *c = NULL;
279 R_cache_get_max_texture_size(&w, &h);
280 if (w && h) {
281 glGenTextures(1, &id);
282 if (id) {
283 R_gl_bind_texture(id);
284 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
285 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
286 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
287 c = malloc(sizeof(cache));
288 if (c != NULL) {
289 *c = (cache) {
290 .id = id,
291 .root.r = w - 1,
292 .root.b = h - 1
293 };
294 } else {
295 glDeleteTextures(1, &id);
299 //logo("new cache %p\n", c);
300 return c;
303 static void R_cache_free (cache *root, int freetexture) {
304 cache *next;
305 cache *c = root;
306 while (c != NULL) {
307 next = c->next;
308 R_node_free_recursive(c->root.left);
309 R_node_free_recursive(c->root.right);
310 if (freetexture && c->id != 0) {
311 glDeleteTextures(1, &c->id);
313 free(c);
314 c = next;
318 static node *R_cache_alloc (cache *root, int w, int h) {
319 assert(root);
320 assert(w > 0 && h > 0);
321 node *n = NULL;
322 cache *p = NULL;
323 cache *c = root;
324 int maxw, maxh;
325 R_cache_get_max_texture_size(&maxw, &maxh);
326 if (w <= maxw && h <= maxh) {
327 while (c && !n) {
328 n = R_node_alloc(&c->root, w, h);
329 if (n) {
330 assert(n->leaf);
331 n->base = c;
333 p = c;
334 c = c->next;
336 if (!n) {
337 c = R_cache_new();
338 if (c) {
339 p->next = c;
340 n = R_node_alloc(&c->root, w, h);
341 if (n) {
342 assert(n->leaf);
343 n->base = c;
348 if (n) {
349 //logo("new node %p {%i:%i:%i:%i}\n", n, n->l, n->t, n->r, n->b);
350 } else {
351 logo("new node failed {%i:%i}\n", w, h);
353 return n;
356 static void R_cache_update (node *n, const void *data, int x, int y, int w, int h) {
357 assert(n);
358 assert(n->leaf);
359 assert(n->base);
360 assert(data);
361 assert(x >= 0);
362 assert(y >= 0);
363 assert(n->l + x + w - 1 <= n->r);
364 assert(n->t + y + h - 1 <= n->b);
365 R_gl_bind_texture(n->base->id);
366 glTexSubImage2D(GL_TEXTURE_2D, 0, n->l + x, n->t + y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data);
369 /* Generic helpers */
371 static void R_init_playpal (void) {
372 int i;
373 byte *vgapal = M_lock(F_getresid("PLAYPAL"));
374 for (i = 0; i < 256; i++) {
375 playpal[i] = (rgb) {
376 .r = vgapal[i * 3 + 0] * 255 / 63,
377 .g = vgapal[i * 3 + 1] * 255 / 63,
378 .b = vgapal[i * 3 + 2] * 255 / 63,
379 };
380 bright[i] = ((int)vgapal[i * 3 + 0] + vgapal[i * 3 + 1] + vgapal[i * 3 + 2]) * 8 / (63 * 3);
382 M_unlock(vgapal);
385 static vgaimg *R_getvga (int id) {
386 int loaded = M_was_locked(id);
387 vgaimg *v = M_lock(id);
388 if (v != NULL && !loaded) {
389 v->w = short2host(v->w);
390 v->h = short2host(v->h);
391 v->x = short2host(v->x);
392 v->y = short2host(v->y);
394 return v;
397 static rgba *R_extract_flame_spr (vgaimg *v) {
398 static const byte flametab[16] = {
399 0xBC, 0xBA, 0xB8, 0xB6, 0xB4, 0xB2, 0xB0, 0xD5,
400 0xD6, 0xD7, 0xA1, 0xA0, 0xE3, 0xE2, 0xE1, 0xE0
401 };
402 int i, j;
403 rgba *s = malloc(v->w * v->h * sizeof(rgba));
404 if (s != NULL) {
405 for (j = 0; j < v->h; j++) {
406 for (i = 0; i < v->w; i++) {
407 int k = j * v->w + i;
408 byte c = v->data[k] + bright[DEFAULT_SKY_COLOR];
409 s[k] = (rgba) {
410 .r = playpal[flametab[c]].r,
411 .g = playpal[flametab[c]].g,
412 .b = playpal[flametab[c]].b,
413 .a = v->data[k] == VGA_TRANSPARENT_COLOR ? 0x00 : 0xFF,
414 };
418 return s;
421 static rgba *R_extract_smoke_spr (vgaimg *v) {
422 int i, j;
423 rgba *s = malloc(v->w * v->h * sizeof(rgba));
424 if (s != NULL) {
425 for (j = 0; j < v->h; j++) {
426 for (i = 0; i < v->w; i++) {
427 int k = j * v->w + i;
428 byte c = ((v->data[k] + bright[DEFAULT_SKY_COLOR]) + 0x60) ^ 0x0F;
429 byte a = 0xFF - ((int)playpal[c].r + playpal[c].g + playpal[c].b) / 3;
430 s[k] = (rgba) {
431 .r = playpal[c].r,
432 .g = playpal[c].g,
433 .b = playpal[c].b,
434 .a = v->data[k] == VGA_TRANSPARENT_COLOR ? 0x00 : a,
435 };
439 return s;
442 static rgba *R_extract_mask_spr (vgaimg *v) {
443 int i, j;
444 rgba *s = malloc(v->w * v->h * sizeof(rgba));
445 if (s != NULL) {
446 for (j = 0; j < v->h; j++) {
447 for (i = 0; i < v->w; i++) {
448 int k = j * v->w + i;
449 byte c = v->data[k];
450 if (c >= 0x70 && c <= 0x7F) {
451 byte mask = c - 0x70;
452 mask = 0xFF - ((mask << 4) | mask);
453 s[k] = (rgba) {
454 .r = mask,
455 .g = mask,
456 .b = mask,
457 .a = 0xFF,
458 };
459 } else {
460 s[k] = (rgba) {
461 .r = 0,
462 .g = 0,
463 .b = 0,
464 .a = 0,
465 };
470 return s;
473 static rgba *R_extract_rgba_spr (vgaimg *v) {
474 int i, j;
475 rgba *s = malloc(v->w * v->h * sizeof(rgba));
476 if (s != NULL) {
477 for (j = 0; j < v->h; j++) {
478 for (i = 0; i < v->w; i++) {
479 int k = j * v->w + i;
480 byte c = v->data[k];
481 s[k] = (rgba) {
482 .r = playpal[c].r,
483 .g = playpal[c].g,
484 .b = playpal[c].b,
485 .a = c == VGA_TRANSPARENT_COLOR ? 0x00 : 0xFF,
486 };
490 return s;
493 /* OpenGL helpers */
495 static image R_gl_create_image (const rgba *buf, int w, int h) {
496 node *n = R_cache_alloc(root, w, h);
497 if (n) {
498 R_cache_update(n, buf, 0, 0, w, h);
500 return (image) {
501 .n = n,
502 .w = w,
503 .h = h,
504 .res = -1
505 };
508 static image R_gl_get_special_image (int id, rgba *(*fn)(vgaimg*)) {
509 image img;
510 //char name[8];
511 //F_getresname(name, id);
512 //logo("load image: %.8s\n", name);
513 vgaimg *v = R_getvga(id);
514 if (v != NULL) {
515 rgba *buf = (*fn)(v);
516 img = R_gl_create_image(buf, v->w, v->h);
517 img.x = v->x;
518 img.y = v->y;
519 img.res = id;
520 M_unlock(v);
521 free(buf);
522 } else {
523 img = (image) {
524 .res = id
525 };
527 return img;
530 static image R_gl_getimage (int id) {
531 return R_gl_get_special_image(id, &R_extract_rgba_spr);
534 static image R_gl_loadimage (const char name[8]) {
535 return R_gl_getimage(F_getresid(name));
538 static image R_gl_get_special_spr (const char n[4], int s, int d, rgba *(*fn)(vgaimg*)) {
539 return R_gl_get_special_image(F_getsprid(n, s, d), fn);
542 static void R_gl_free_image (image *img) {
543 if (img->n != NULL && img->res >= 0) {
544 R_node_free(img->n);
546 img->n = NULL;
547 img->res = -1;
550 static void R_gl_quad_vetexes (int x, int y, int w, int h) {
551 glVertex2i(x + w, y);
552 glVertex2i(x, y);
553 glVertex2i(x, y + h);
554 glVertex2i(x + w, y + h);
557 static void R_gl_draw_quad (int x, int y, int w, int h) {
558 glBegin(GL_QUADS);
559 R_gl_quad_vetexes(x, y, w, h);
560 glEnd();
563 static void R_gl_draw_textured (image *img, int x, int y, int w, int h, int flip) {
564 if (img->n) {
565 GLfloat nw = img->n->base->root.r + 1;
566 GLfloat nh = img->n->base->root.b + 1;
567 GLfloat ax = (flip ? img->n->l : img->n->r + 1) / nw;
568 GLfloat bx = (flip ? img->n->r + 1 : img->n->l) / nh;
569 GLfloat ay = (img->n->t) / nw;
570 GLfloat by = (img->n->b + 1) / nh;
571 R_gl_bind_texture(img->n->base->id);
572 glEnable(GL_TEXTURE_2D);
573 glBegin(GL_QUADS);
574 glTexCoord2f(ax, ay); glVertex2i(x + w, y);
575 glTexCoord2f(bx, ay); glVertex2i(x, y);
576 glTexCoord2f(bx, by); glVertex2i(x, y + h);
577 glTexCoord2f(ax, by); glVertex2i(x + w, y + h);
578 glEnd();
579 } else {
580 glColor3ub(255, 0, 0);
581 glDisable(GL_BLEND);
582 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
583 glDisable(GL_TEXTURE_2D);
584 R_gl_draw_quad(x, y, w, h);
588 /* fit image into rectangle without applying offset and transparency */
589 static void R_gl_draw_image_ext (image *img, int x, int y, int w, int h) {
590 glDisable(GL_BLEND);
591 glColor3ub(255, 255, 255);
592 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
593 R_gl_draw_textured(img, x, y, w, h, 0);
596 /* draw sprite with offset and coloring */
597 static void R_gl_draw_image_color (image *img, int x, int y, int flip) {
598 int xx = flip ? x - img->w + img->x : x - img->x;
599 glEnable(GL_BLEND);
600 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
601 R_gl_draw_textured(img, xx, y - img->y, img->w, img->h, flip);
604 /* draw sprite with offset */
605 static void R_gl_draw_image (image *img, int x, int y, int flip) {
606 glColor3ub(255, 255, 255);
607 R_gl_draw_image_color(img, x, y, flip);
610 static void R_gl_set_color (byte c) {
611 glColor3ub(playpal[c].r, playpal[c].g, playpal[c].b);
614 static void R_gl_setclip (int x, int y, int w, int h) {
615 glScissor(x * screen_scale, (SCRH - h - y) * screen_scale, w * screen_scale, h * screen_scale);
618 static void R_gl_setmatrix (void) {
619 SCRW = screen_width / screen_scale;
620 SCRH = screen_height / screen_scale;
621 glScissor(0, 0, screen_width, screen_height);
622 glViewport(0, 0, screen_width, screen_height);
623 glMatrixMode(GL_PROJECTION);
624 glLoadIdentity();
625 glOrtho(0, SCRW, SCRH, 0, 0, 1);
626 glMatrixMode(GL_MODELVIEW);
627 glLoadIdentity();
630 /* --- Misc --- */
632 static image Z_getspr (const char n[4], int s, int d, char *dir) {
633 int h = F_getsprid(n, s, d);
634 if (dir != NULL) {
635 *dir = (h & 0x8000) ? 1 : 0;
637 return R_gl_getimage(h);
640 static image *Z_get_char_image (image *img, int ch) {
641 image *p = NULL;
642 ch = cp866_toupper(ch);
643 if (ch > 32 && ch < 160) {
644 p = &img[ch - '!'];
646 return p;
649 static int Z_get_char_width_generic (image *img, int off, int ch) {
650 image *p = Z_get_char_image(img, ch);
651 return p == NULL ? off : p->w - 1;
654 static int Z_putch_generic (image *img, int off, int ch) {
655 image *p = Z_get_char_image(img, ch);
656 int w = p == NULL ? off : p->w - 1;
657 if (p != NULL && p->n != NULL) {
658 R_gl_draw_image(p, prx, pry, 0);
660 prx += w;
661 return w;
664 static int Z_get_string_width_generic (image *img, int off, const char *fmt, va_list ap) {
665 int i, w, ww;
666 char buf[80];
667 vsprintf(buf, fmt, ap);
668 for (i = w = ww = 0; buf[i]; ++i) {
669 switch (buf[i]) {
670 case '\n':
671 case '\r':
672 ww = max(w, ww);
673 w = 0;
674 break;
675 default:
676 w += Z_get_char_width_generic(img, off, (byte)buf[i]);
679 return max(w, ww);
682 static int Z_printf_generic (image *img, int off, const char *fmt, va_list ap) {
683 int i, w, ww;
684 char buf[80];
685 vsprintf(buf, fmt, ap);
686 for (i = w = ww = 0; buf[i]; ++i) {
687 switch (buf[i]) {
688 case '\n':
689 pry += off + 1;
690 case '\r':
691 w = max(w, ww);
692 prx = 0;
693 w = 0;
694 break;
695 default:
696 w += Z_putch_generic(img, off, (byte)buf[i]);
699 return w;
702 static void Z_gotoxy (int x, int y) {
703 prx = x;
704 pry = y;
707 static int Z_get_big_string_width (const char *fmt, ...) {
708 va_list a;
709 va_start(a, fmt);
710 int w = Z_get_string_width_generic(bfh, 12, fmt, a);
711 va_end(a);
712 return w;
715 static int Z_printbf (const char *fmt, ...) {
716 va_list a;
717 va_start(a, fmt);
718 int w = Z_printf_generic(bfh, 12, fmt, a);
719 va_end(a);
720 return w;
723 static int Z_get_small_string_width (const char *fmt, ...) {
724 va_list a;
725 va_start(a, fmt);
726 int w = Z_get_string_width_generic(sfh, 7, fmt, a);
727 va_end(a);
728 return w;
731 static int Z_printsf (const char *fmt, ...) {
732 va_list a;
733 va_start(a, fmt);
734 int w =Z_printf_generic(sfh, 7, fmt, a);
735 va_end(a);
736 return w;
739 static void Z_printhf (const char *fmt, ...) {
740 int i, c;
741 char buf[80];
742 va_list a;
743 va_start(a, fmt);
744 vsprintf(buf, fmt, a);
745 va_end(a);
746 for (i = 0; buf[i]; ++i) {
747 switch (buf[i]) {
748 case '0':
749 case '1':
750 case '2':
751 case '3':
752 case '4':
753 case '5':
754 case '6':
755 case '7':
756 case '8':
757 case '9':
758 c = buf[i] - '0';
759 break;
760 case '-':
761 c = 10;
762 break;
763 case '%':
764 c = 11;
765 break;
766 case '\n':
767 pry += 19;
768 case '\r':
769 c = -1;
770 prx = 0;
771 break;
772 default:
773 c = -1;
774 break;
776 if (c >= 0) {
777 R_gl_draw_image(&sth[c], prx, pry, 0);
779 prx += 14;
783 /* --- Menu --- */
785 static image *PL_getspr (int s, int d, int msk) {
786 int i = (s - 'A') * 2 + d;
787 return msk ? &plr_msk[i] : &plr_spr[i];
790 #define SCROLLER_MIDDLE 10
791 #define TEXTFIELD_MIDDLE 2
793 static void get_entry_size (const menu_t *m, int i, int *w, int *h) {
794 assert(m != NULL);
795 assert(i >= 0);
796 assert(w != NULL);
797 assert(h != NULL);
798 int x = 0;
799 int y = 0;
800 int type = 0;
801 menu_msg_t msg;
802 msg.type = GM_GETENTRY;
803 assert(GM_send(m, i, &msg));
804 type = msg.integer.i;
805 switch (type) {
806 case GM_BUTTON:
807 case GM_SCROLLER:
808 case GM_TEXTFIELD:
809 case GM_TEXTFIELD_BUTTON:
810 msg.type = GM_GETCAPTION;
811 if (GM_send(m, i, &msg)) {
812 x = Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
814 break;
815 case GM_SMALL_BUTTON:
816 msg.type = GM_GETCAPTION;
817 if (GM_send(m, i, &msg)) {
818 x = Z_get_small_string_width("%.*s", msg.string.maxlen, msg.string.s);
820 break;
821 default:
822 assert(0);
824 switch (type) {
825 case GM_BUTTON:
826 msg.type = GM_GETSTR;
827 if (GM_send(m, i, &msg)) {
828 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
830 y = 16;
831 break;
832 case GM_SMALL_BUTTON:
833 msg.type = GM_GETSTR;
834 if (GM_send(m, i, &msg)) {
835 x += Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s);
837 y = 12;
838 break;
839 case GM_SCROLLER:
840 x += (SCROLLER_MIDDLE + 2) * 8;
841 y = 16;
842 break;
843 case GM_TEXTFIELD:
844 case GM_TEXTFIELD_BUTTON:
845 msg.type = GM_GETSTR;
846 if (GM_send(m, i, &msg)) {
847 x += (msg.string.maxlen + 2) * 8;
848 } else {
849 x += (TEXTFIELD_MIDDLE + 2) * 8;
851 y = 16;
852 break;
853 default:
854 assert(0);
856 *w = x;
857 *h = y;
860 static void get_menu_size (const menu_t *m, int *w, int *h) {
861 assert(m != NULL);
862 assert(w != NULL);
863 assert(h != NULL);
864 int i, n, x, y, xx, yy, type;
865 menu_msg_t msg;
866 msg.type = GM_QUERY;
867 if (GM_send_this(m, &msg)) {
868 n = msg.integer.b;
869 type = msg.integer.s;
870 x = 0;
871 y = 0;
872 msg.type = GM_GETTITLE;
873 if (GM_send_this(m, &msg)) {
874 switch (type) {
875 case GM_BIG: x = Z_get_big_string_width("%.*s", msg.string.maxlen, msg.string.s); break;
876 case GM_SMALL: x = Z_get_small_string_width("%.*s", msg.string.maxlen, msg.string.s); break;
877 default: assert(0);
880 for (i = 0; i < n; i++) {
881 get_entry_size(m, i, &xx, &yy);
882 x = max(x, xx);
883 y += yy;
885 *w = x;
886 *h = y;
887 } else {
888 *w = 0;
889 *h = 0;
893 static int GM_draw (void) {
894 int i, j, n, x, y, xoff, yoff, cur, w, type, recv;
895 const menu_t *m = GM_get();
896 menu_msg_t msg;
897 if (m != NULL) {
898 get_menu_size(m, &x, &y);
899 x = SCRW / 2 - x / 2;
900 y = SCRH / 2 - y / 2;
901 // --- title ---
902 msg.type = GM_QUERY;
903 if (GM_send_this(m, &msg)) {
904 cur = msg.integer.i;
905 n = msg.integer.a;
906 type = msg.integer.s;
907 msg.type = GM_GETTITLE;
908 if (GM_send_this(m, &msg)) {
909 Z_gotoxy(x, y - 10);
910 switch (type) {
911 case GM_SMALL: yoff = 8; Z_printsf("%.*s", msg.string.maxlen, msg.string.s); break;
912 case GM_BIG: yoff = 20; Z_printbf("%.*s", msg.string.maxlen, msg.string.s); break;
913 default: assert(0);
915 } else {
916 yoff = 0;
918 for (i = 0; i < n; i++) {
919 msg.type = GM_GETENTRY;
920 if (GM_send(m, i, &msg)) {
921 type = msg.integer.i;
922 if (i == cur) {
923 if (type == GM_SMALL_BUTTON) {
924 Z_gotoxy(x - 8, y + yoff);
925 Z_printsf(">");
926 } else {
927 R_gl_draw_image(&msklh[(gm_tm / 6) & 1], x - 25, y + yoff - 8, 0);
930 msg.type = GM_GETCAPTION;
931 if (GM_send(m, i, &msg)) {
932 Z_gotoxy(x, y + yoff);
933 if (type == GM_SMALL_BUTTON) {
934 xoff = Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
935 } else {
936 xoff = Z_printbf("%.*s", msg.string.maxlen, msg.string.s);
938 } else {
939 xoff = 0;
941 switch (type) {
942 case GM_BUTTON:
943 case GM_SMALL_BUTTON:
944 msg.type = GM_GETSTR;
945 if (GM_send(m, i, &msg)) {
946 Z_gotoxy(x + xoff, y + yoff);
947 if (type == GM_SMALL_BUTTON) {
948 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
949 } else {
950 Z_printbf("%.*s", msg.string.maxlen, msg.string.s);
953 yoff += type == GM_BUTTON ? 16 : 12;
954 break;
955 case GM_TEXTFIELD:
956 case GM_TEXTFIELD_BUTTON:
957 yoff += 9;
958 msg.type = GM_GETSTR;
959 recv = GM_send(m, i, &msg);
960 w = recv ? msg.string.maxlen : TEXTFIELD_MIDDLE;
961 R_gl_draw_image(&mslotl, x + xoff, y + yoff, 0);
962 for (j = 1; j <= w; j++) {
963 R_gl_draw_image(&mslotm, x + xoff + j * 8, y + yoff, 0);
965 R_gl_draw_image(&mslotr, x + xoff + j * 8, y + yoff, 0);
966 Z_gotoxy(x + xoff + 4, y + yoff - 7);
967 if (input && i == cur) {
968 Z_printsf("%.*s_", imax, ibuf);
969 } else if (recv) {
970 Z_printsf("%.*s", msg.string.maxlen, msg.string.s);
972 yoff += 7;
973 break;
974 case GM_SCROLLER:
975 R_gl_draw_image(&mbarl, x + xoff, y + yoff, 0);
976 for (j = 1; j < SCROLLER_MIDDLE; j++) {
977 R_gl_draw_image(&mbarm, x + xoff + j * 8, y + yoff, 0);
979 R_gl_draw_image(&mbarr, x + xoff + j * 8, y + yoff, 0);
980 msg.type = GM_GETINT;
981 if (GM_send(m, i, &msg)) {
982 int lev = (msg.integer.i - msg.integer.a) * ((SCROLLER_MIDDLE - 2) * 8) / msg.integer.b;
983 R_gl_draw_image(&mbaro, x + xoff + lev + 8, y + yoff, 0);
985 yoff += 16;
986 break;
987 default:
988 assert(0);
994 return m != NULL;
997 /* --- View --- */
999 static void R_draw_fld (byte *fld, int minx, int miny, int maxx, int maxy, int fg) {
1000 int i, j;
1001 assert(minx >= 0 && minx <= FLDW);
1002 assert(miny >= 0 && miny <= FLDH);
1003 assert(maxx >= 0 && maxx <= FLDW);
1004 assert(maxy >= 0 && maxy <= FLDH);
1005 for (j = miny; j < maxy; j++) {
1006 for (i = minx; i < maxx; i++) {
1007 byte id = fld[j * FLDW + i];
1008 if (id != 0) {
1009 if (walp[id].res < 0) {
1010 if (fg) {
1011 switch (R_get_special_id(id)) {
1012 case 1:
1013 glColor4ub(0, 0, 255, 127);
1014 break;
1015 case 2:
1016 glColor4ub(0, 127, 0, 127);
1017 break;
1018 case 3:
1019 glColor4ub(127, 0, 0, 127);
1020 break;
1021 default:
1022 glColor4ub(0, 0, 0, 127);
1023 break;
1025 glEnable(GL_BLEND);
1026 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1027 glDisable(GL_TEXTURE_2D);
1028 R_gl_draw_quad(i * CELW, j * CELW, CELW, CELH);
1030 } else {
1031 R_gl_draw_image(&walp[id], i * CELW, j * CELH, 0);
1038 static void R_draw_dots (void) {
1039 int i;
1040 glDisable(GL_BLEND);
1041 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1042 glDisable(GL_TEXTURE_2D);
1043 glBegin(GL_QUADS);
1044 for (i = 0; i < MAXDOT; i++) {
1045 if (dot[i].t != 0) {
1046 R_gl_set_color(dot[i].c);
1047 R_gl_quad_vetexes(dot[i].o.x, dot[i].o.y, 1, 1);
1050 glEnd();
1053 static void R_draw_items (void) {
1054 int i, s;
1055 for (i = 0; i < MAXITEM; ++i) {
1056 s = -1;
1057 if (it[i].t && it[i].s >= 0) {
1058 switch (it[i].t & 0x7FFF) {
1059 case I_ARM1:
1060 s = it[i].s / 9 + 18;
1061 break;
1062 case I_ARM2:
1063 s = it[i].s / 9 + 20;
1064 break;
1065 case I_MEGA:
1066 s = it[i].s / 2 + 22;
1067 break;
1068 case I_INVL:
1069 s = it[i].s / 2 + 26;
1070 break;
1071 case I_SUPER:
1072 case I_RTORCH:
1073 case I_GTORCH:
1074 case I_BTORCH:
1075 s = it[i].s / 2 + (it[i].t - I_SUPER) * 4 + 35;
1076 break;
1077 case I_GOR1: case I_FCAN:
1078 s = it[i].s / 2 + (it[i].t - I_GOR1) * 3 + 51;
1079 break;
1080 case I_AQUA:
1081 s = 30;
1082 break;
1083 case I_SUIT:
1084 s = 34;
1085 break;
1086 case I_KEYR:
1087 case I_KEYG:
1088 case I_KEYB:
1089 s = (it[i].t & 0x7FFF) - I_KEYR + 31;
1090 break;
1091 case I_GUN2:
1092 s = 57;
1093 break;
1094 default:
1095 s = (it[i].t & 0x7FFF) - 1;
1098 if (s >= 0) {
1099 R_gl_draw_image(&item_spr[s], it[i].o.x, it[i].o.y, item_sprd[s]);
1104 static int standspr (player_t *p) {
1105 if (p->f & PLF_UP) {
1106 return 'X';
1107 } else if (p->f & PLF_DOWN) {
1108 return 'Z';
1109 } else {
1110 return 'E';
1114 static int wpnspr (player_t *p) {
1115 if (p->f & PLF_UP) {
1116 return 'C';
1117 } else if(p->f & PLF_DOWN) {
1118 return 'E';
1119 } else {
1120 return 'A';
1124 static void R_draw_player (player_t *p) {
1125 enum {STAND, GO, DIE, SLOP, DEAD, MESS, OUT, FALL}; // copypasted from player.c!
1126 static const int wytab[] = {-1, -2, -1, 0};
1127 int s = 'A';
1128 int w = 0;
1129 int wx = 0;
1130 int wy = 0;
1131 switch (p->st) {
1132 case STAND:
1133 if (p->f & PLF_FIRE) {
1134 s = standspr(p) + 1;
1135 w = wpnspr(p) + 1;
1136 } else if (p->pain) {
1137 s = 'G';
1138 w = 'A';
1139 wx = p->d ? 2 : -2;
1140 wy = 1;
1141 } else {
1142 s = standspr(p);
1143 w = wpnspr(p);
1145 break;
1146 case DEAD:
1147 s = 'N';
1148 break;
1149 case MESS:
1150 s = 'W';
1151 break;
1152 case GO:
1153 if (p->pain) {
1154 s = 'G';
1155 w = 'A';
1156 wx = p->d ? 2 : -2;
1157 wy = 1;
1158 } else {
1159 s = plr_goanim[p->s / 8];
1160 w = (p->f & PLF_FIRE) ? 'B' : 'A';
1161 wx = p->d ? 2 : -2;
1162 wy = 1 + wytab[s - 'A'];
1164 break;
1165 case DIE:
1166 s = plr_dieanim[p->s];
1167 break;
1168 case SLOP:
1169 s = plr_slopanim[p->s];
1170 break;
1171 case OUT:
1172 s = 0;
1173 break;
1175 if (p->wpn == 0) {
1176 w = 0;
1178 if (w) {
1179 R_gl_draw_image(&plr_wpn[(int)p->wpn][w -'A'], p->o.x + wx, p->o.y + wy, p->d);
1181 if (s) {
1182 R_gl_draw_image(&plr_spr[(s - 'A') * 2 + p->d], p->o.x, p->o.y, plr_sprd[(s - 'A') * 2 + p->d]);
1183 R_gl_set_color(p->color + PLAYER_COLOR_OFFSET);
1184 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]);
1188 static void R_draw_monsters (void) {
1189 enum {SLEEP, GO, RUN, CLIMB, DIE, DEAD, ATTACK, SHOOT, PAIN, WAIT, REVIVE, RUNOUT}; // copypasted from monster.c!
1190 int i;
1191 for (i = 0; i < MAXMN; i++) {
1192 if (mn[i].t != MN_NONE) {
1193 int x = mn[i].o.x;
1194 int y = mn[i].o.y;
1195 if (mn[i].t < MN__LAST) {
1196 if ((mn[i].t != MN_SOUL && mn[i].t != MN_PAIN) || mn[i].st != DEAD) {
1197 int ap = mn[i].ap[mn[i].ac];
1198 int d = (ap - 'A') * 2 + mn[i].d;
1199 int dir = mn_sprd[mn[i].t - 1][d];
1200 if (mn[i].t == MN_MAN && (ap == 'E' || ap == 'F')) {
1201 R_gl_draw_image(&mn_sgun[ap - 'E'], x, y, mn[i].d);
1203 R_gl_draw_image(&mn_spr[mn[i].t - 1][d], x, y, dir);
1204 if (mn[i].t == MN_MAN) {
1205 R_gl_set_color(MANCOLOR + PLAYER_COLOR_OFFSET);
1206 R_gl_draw_image_color(&mn_man_msk[d], x, y, dir);
1209 if (mn[i].t == MN_VILE && mn[i].st == SHOOT) {
1210 R_gl_draw_image(&mn_fspr[mn[i].ac / 3], mn[i].tx, mn[i].ty, 0);
1212 } else if (mn[i].t == MN_PL_DEAD || mn[i].t == MN_PL_MESS) {
1213 int type = mn[i].t - MN_PL_DEAD;
1214 R_gl_draw_image(&pl_spr[type], x, y, 0);
1215 R_gl_set_color(mn[i].d);
1216 R_gl_draw_image_color(&pl_msk[type], x, y, 0);
1222 static void R_draw_weapons (void) {
1223 enum {NONE, ROCKET, PLASMA, APLASMA, BALL1, BALL2, BALL7, BFGBALL, BFGHIT, MANF, REVF, FIRE}; // copypasted from weapons.c!
1224 int i, s, d, x, y;
1225 for (i = 0; i < MAXWPN; ++i) {
1226 s = -1;
1227 d = 0;
1228 switch (wp[i].t) {
1229 case REVF:
1230 case ROCKET:
1231 d = wp[i].s;
1232 if (d < 2) {
1233 d = wp[i].o.xv > 0 ? 1 : 0;
1234 x = abs(wp[i].o.xv);
1235 y = wp[i].o.yv;
1236 s = 0;
1237 if (y < 0) {
1238 if (-y >= x) {
1239 s = 30;
1241 } else if (y > 0) {
1242 if (y >= x / 2) {
1243 s = 31;
1246 } else {
1247 s = (d - 2) / 2 + 1;
1248 d = 0;
1250 break;
1251 case MANF:
1252 s=wp[i].s;
1253 if (s >= 2) {
1254 s /= 2;
1255 break;
1257 case PLASMA:
1258 case APLASMA:
1259 case BALL1:
1260 case BALL7:
1261 case BALL2:
1262 s = wp[i].s;
1263 if (s >= 2) {
1264 s = s / 2 + 1;
1266 switch (wp[i].t) {
1267 case PLASMA:
1268 s += 4;
1269 break;
1270 case APLASMA:
1271 s += 11;
1272 break;
1273 case BALL1:
1274 s += 32;
1275 break;
1276 case BALL2:
1277 s += 42;
1278 break;
1279 case BALL7:
1280 s += 37;
1281 d = wp[i].o.xv >= 0 ? 1 : 0;
1282 break;
1283 case MANF:
1284 s += 47;
1285 d= wp[i].o.xv>=0 ? 1 : 0;
1286 break;
1288 break;
1289 case BFGBALL:
1290 s = wp[i].s;
1291 if (s >= 2) {
1292 s = s / 2 + 1;
1294 s += 18;
1295 break;
1296 case BFGHIT:
1297 s = wp[i].s / 2 + 26;
1298 break;
1300 if (s >= 0) {
1301 R_gl_draw_image(&wp_spr[s * 2 + d], wp[i].o.x, wp[i].o.y, wp_sprd[s * 2 + d]);
1306 static void R_draw_smoke (void) {
1307 int i, s;
1308 for (i = 0; i < MAXSMOK; ++i) {
1309 if (sm[i].t) {
1310 switch (sm[i].s) {
1311 case 0:
1312 s = sm[i].t;
1313 if (s >= (SMSN - 1) * 3) {
1314 s = 0;
1315 } else {
1316 s = SMSN - 1 - s / 3;
1318 R_gl_draw_image(&smk_spr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1319 break;
1320 case 1:
1321 s = sm[i].t;
1322 if (s >= FLSN - 1) {
1323 s = 0;
1324 } else {
1325 s = FLSN - 1 - s;
1327 R_gl_draw_image(&smk_fspr[s], sm[i].x >> 8, (sm[i].y >> 8) + 1, 0);
1328 break;
1334 static void R_draw_effects (void) {
1335 enum {NONE, TFOG, IFOG, BUBL}; // copypasted from fx.c
1336 int i, s;
1337 glPointSize(screen_scale);
1338 for (i = 0; i < MAXFX; ++i) {
1339 switch (fx[i].t) {
1340 case TFOG:
1341 s = fx[i].s / 2;
1342 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1343 break;
1344 case IFOG:
1345 s = fx[i].s / 2 + 10;
1346 R_gl_draw_image(&fx_spr[s], fx[i].x, fx[i].y, fx_sprd[s]);
1347 break;
1348 case BUBL:
1349 glDisable(GL_BLEND);
1350 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1351 glDisable(GL_TEXTURE_2D);
1352 R_gl_set_color(0xC0 + fx[i].s);
1353 R_gl_draw_quad(fx[i].x >> 8, fx[i].y >> 8, 1, 1);
1354 break;
1359 static int get_pu_st (int t) {
1360 if (t >= PL_FLASH) {
1361 return 1;
1362 } else if((t / 9) & 1) {
1363 return 0;
1364 } else {
1365 return 1;
1369 static void R_draw_view (int x, int y, int w, int h, int camx, int camy) {
1370 glPushMatrix();
1371 R_gl_setclip(x, y, w, h);
1372 glTranslatef(x, y, 0);
1373 if (w_horiz && horiz.n != NULL) {
1374 R_gl_draw_image_ext(&horiz, 0, 0, w, h);
1375 if (sky_type == 2 && lt_time < 0) {
1376 image *tanderbolt = &ltn[lt_type][lt_time < -5 ? 0 : 1];
1377 if (!lt_side) {
1378 R_gl_draw_image(tanderbolt, 0, lt_ypos, 0);
1379 } else {
1380 R_gl_draw_image(tanderbolt, w - 1, lt_ypos, 1);
1383 } else {
1384 glDisable(GL_BLEND);
1385 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1386 glDisable(GL_TEXTURE_2D);
1387 R_gl_set_color(DEFAULT_SKY_COLOR);
1388 R_gl_draw_quad(0, 0, w, h);
1390 int maxx = min((camx + w) / CELW + 1, FLDW);
1391 int maxy = min((camy + h) / CELH + 1, FLDH);
1392 int minx = max((camx - max_wall_width) / CELW, 0);
1393 int miny = max((camy - max_wall_height) / CELH, 0);
1394 glTranslatef(-camx, -camy, 0);
1395 R_draw_fld((byte*)fldb, minx, miny, maxx, maxy, 0);
1396 R_draw_dots();
1397 R_draw_items();
1398 R_draw_player(&pl1);
1399 if (_2pl) {
1400 R_draw_player(&pl2);
1402 R_draw_monsters();
1403 R_draw_weapons();
1404 R_draw_smoke();
1405 R_draw_effects();
1406 R_draw_fld((byte*)fldf, minx, miny, maxx, maxy, 1);
1407 glTranslatef(camx, camy, 0);
1408 if (sky_type == 2 && (lt_time == -4 || lt_time == -2)) {
1409 glColor4ub(255, 255, 255, 255);
1410 glEnable(GL_BLEND);
1411 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1412 glDisable(GL_TEXTURE_2D);
1413 R_gl_draw_quad(0, 0, w, h);
1415 glPopMatrix();
1418 static void R_draw_player_view (player_t *p, int x, int y, int w, int h) {
1419 p->looky = min(max(p->looky, -SCRH / 4), SCRH / 4); // TODO remove writeback
1420 int st = stone.w;
1421 int cw = w - st;
1422 int cx = min(max(p->o.x, cw / 2), FLDW * CELW - cw / 2);
1423 int cy = min(max(p->o.y - 12 + p->looky, h / 2), FLDH * CELH - h / 2);
1424 int camx = max(cx - cw / 2, 0);
1425 int camy = max(cy - h / 2, 0);
1426 glPushMatrix();
1427 R_draw_view(x, y + 1, cw, h - 2, camx, camy);
1428 glTranslatef(x, y, 0);
1429 if (p->invl) {
1430 if (get_pu_st(p->invl)) {
1431 glEnable(GL_BLEND);
1432 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
1433 glDisable(GL_TEXTURE_2D);
1434 glColor4ub(191, 191, 191, 255);
1435 R_gl_draw_quad(0, 0, cw, h);
1437 } else {
1438 if (p->suit && get_pu_st(p->suit)) {
1439 glEnable(GL_BLEND);
1440 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1441 glDisable(GL_TEXTURE_2D);
1442 glColor4ub(0, 255, 0, 192);
1443 R_gl_draw_quad(0, 0, cw, h);
1445 int f = min(max(p->pain * 3, 0), 255);
1446 if (f > 0) {
1447 glEnable(GL_BLEND);
1448 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1449 glDisable(GL_TEXTURE_2D);
1450 glColor4ub(255, 0, 0, f);
1451 R_gl_draw_quad(0, 0, cw, h);
1454 R_gl_setclip(x, y, w, h);
1455 glTranslatef(-x + cw, 0, 0);
1456 R_gl_draw_image(&stone, 0, 0, 0);
1457 int i = stone.h;
1458 while (i < h) {
1459 R_gl_draw_image(&stone2, 0, i, 0);
1460 i += stone2.h;
1462 if (p->drawst & PL_DRAWAIR) {
1463 if (p->air < PL_AIR) {
1464 int a = min(max(p->air, 0), MAXAIR) * 100 / MAXAIR;
1465 glDisable(GL_BLEND);
1466 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1467 glDisable(GL_TEXTURE_2D);
1468 R_gl_set_color(0xC8);
1469 R_gl_draw_quad(10, 49, a, 2);
1472 if (p->drawst & PL_DRAWLIFE) {
1473 Z_gotoxy(10, 7);
1474 Z_printhf("%3d%%", p->life);
1476 if (p->drawst & PL_DRAWARMOR) {
1477 Z_gotoxy(10, 7 + 19);
1478 Z_printhf("%3d%%", p->armor);
1480 if (p->drawst & PL_DRAWWPN) {
1481 switch(p->wpn) {
1482 case 2:
1483 case 5:
1484 i = p->ammo;
1485 break;
1486 case 3:
1487 case 4:
1488 case 9:
1489 i = p->shel;
1490 break;
1491 case 6:
1492 i = p->rock;
1493 break;
1494 case 7:
1495 case 8:
1496 i = p->cell;
1497 break;
1498 case 10:
1499 i = p->fuel;
1500 break;
1501 default:
1502 i = -1;
1503 break;
1505 // weapon
1506 if (p->wpn >= 0) {
1507 R_gl_draw_image(&sth[12 + p->wpn], st - 88, 58 + 19, 0);
1509 // ammo
1510 if (p->wpn >= 2) {
1511 Z_gotoxy(st - 10 - 5 * 14, 58 + 2);
1512 Z_printhf("%5d", i);
1515 if (p->drawst & PL_DRAWFRAG && g_dm) {
1516 Z_gotoxy(st - 5 - 5 * 14, 77 + 5);
1517 Z_printhf("%5d", p->frag);
1519 if (p->drawst & PL_DRAWKEYS) {
1520 int x, k, n;
1521 for (k = p->keys >> 4, n = 0, x = st - 75; n < 3; n++, k >>= 1, x += 9) {
1522 if (k & 1) {
1523 R_gl_draw_image(&keys[n], x, 91, 0);
1527 if (p->drawst & PL_DRAWLIVES && !_2pl) {
1528 Z_gotoxy(st - 35, 17);
1529 Z_printhf("%d", p->lives);
1531 glPopMatrix();
1534 /* --- Game --- */
1536 static void pl_info (player_t *p, int x, int y) {
1537 dword t = p->kills * 10920 / g_time;
1538 Z_gotoxy(x + 25, y); Z_printbf("KILLS");
1539 Z_gotoxy(x + 25, y + 15); Z_printbf("KPM");
1540 Z_gotoxy(x + 25, y + 30); Z_printbf("SECRETS %u / %u", p->secrets, sw_secrets);
1541 Z_gotoxy(x + 255, y); Z_printbf("%u", p->kills);
1542 Z_gotoxy(x + 255, y + 15); Z_printbf("%u.%u", t / 10, t % 10);
1545 static void R_draw_intermission (void) {
1546 int cx = SCRW / 2;
1547 word hr, mn, sc, h;
1548 Z_gotoxy(cx - 14*12/2, 20);
1549 Z_printbf("LEVEL COMPLETE");
1550 Z_calc_time(g_time, &hr, &mn, &sc);
1551 Z_gotoxy(cx - 12*12/2, 40);
1552 Z_printbf("TIME %u:%02u:%02u", hr, mn, sc);
1553 h = 40 + SCRH / 10;
1554 if (_2pl) {
1555 Z_gotoxy(cx - 10*12/2, h);
1556 Z_printbf("PLAYER ONE");
1557 h += 20;
1559 pl_info(&pl1, cx - 160, h);
1560 if (_2pl) {
1561 h += 30 + SCRH / 10;
1562 Z_gotoxy(cx - 10*12/2, h);
1563 Z_printbf("PLAYER TWO");
1564 h += 20;
1565 pl_info(&pl2, cx - 160, h);
1569 static void W_act (void) {
1570 int i, a;
1571 if (g_time % 3 == 0) {
1572 for (i = 1; i < max_textures; i++) {
1573 a = walani[i];
1574 if (a != 0) {
1575 anic[a]++;
1576 if (anip[a][anic[a]].res == -1) {
1577 anic[a] = 0;
1579 walp[i] = anip[a][anic[a]];
1585 void R_draw (void) {
1586 W_act();
1587 glClearColor(0, 0, 0, 1);
1588 glClear(GL_COLOR_BUFFER_BIT);
1589 glEnable(GL_SCISSOR_TEST);
1590 R_gl_setmatrix();
1591 switch (g_st) {
1592 case GS_ENDANIM:
1593 case GS_END2ANIM:
1594 case GS_DARKEN:
1595 case GS_BVIDEO:
1596 case GS_EVIDEO:
1597 case GS_END3ANIM:
1598 break;
1599 case GS_TITLE:
1600 R_gl_draw_image_ext(&scrnh[0], 0, 0, SCRW, SCRH);
1601 break;
1602 case GS_INTER:
1603 R_gl_draw_image_ext(&scrnh[1], 0, 0, SCRW, SCRH);
1604 R_draw_intermission();
1605 break;
1606 case GS_ENDSCR:
1607 R_gl_draw_image_ext(&scrnh[2], 0, 0, SCRW, SCRH);
1608 break;
1609 case GS_GAME:
1610 if (_2pl) {
1611 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH / 2);
1612 R_draw_player_view(&pl2, 0, SCRH / 2, SCRW, SCRH / 2);
1613 } else {
1614 R_draw_player_view(&pl1, 0, 0, SCRW, SCRH);
1616 R_gl_setclip(0, 0, SCRW, SCRH);
1617 break;
1619 GM_draw();
1620 Y_swap_buffers();
1623 static void R_alloc (void) {
1624 char s[10];
1625 int i, j, n;
1626 logo("R_alloc: load graphics\n");
1627 /* Game */
1628 scrnh[0] = R_gl_loadimage("TITLEPIC");
1629 scrnh[1] = R_gl_loadimage("INTERPIC");
1630 scrnh[2] = R_gl_loadimage("ENDPIC");
1631 for (i = 0; i < 2; i++) {
1632 sprintf(s, "LTN%c", '1' + i);
1633 for (j = 0; j < 2; j++) {
1634 ltn[i][j] = Z_getspr(s, j, 0, NULL);
1637 /* Smoke */
1638 for (i = 0; i < SMSN; i++) {
1639 smk_spr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_smoke_spr);
1641 for (i = 0; i < FLSN; i++) {
1642 smk_fspr[i] = R_gl_get_special_spr("SMOK", i, 0, &R_extract_flame_spr);
1644 /* Effects */
1645 for (i = 0; i < 10; i++) {
1646 fx_spr[i] = Z_getspr("TFOG", i, 0, fx_sprd + i);
1648 for (; i < 15; i++) {
1649 fx_spr[i] = Z_getspr("IFOG", i - 10, 0, fx_sprd + i);
1651 /* Weapons */
1652 for (i = 0; i < 4; i++) {
1653 wp_spr[i * 2] = Z_getspr("MISL", i, 1, wp_sprd + i * 2);
1654 wp_spr[i * 2 + 1] = Z_getspr("MISL", i, 2, wp_sprd + i * 2 + 1);
1656 for (; i < 6; i++) {
1657 wp_spr[i * 2] = Z_getspr("PLSS", i - 4, 1, wp_sprd + i * 2);
1658 wp_spr[i * 2 + 1] = Z_getspr("PLSS", i - 4, 2, wp_sprd + i * 2 + 1);
1660 for (; i < 11; i++) {
1661 wp_spr[i * 2] = Z_getspr("PLSE", i - 6, 1, wp_sprd + i * 2);
1662 wp_spr[i * 2 + 1] = Z_getspr("PLSE", i - 6, 2, wp_sprd + i * 2 + 1);
1664 for (; i < 13; i++) {
1665 wp_spr[i * 2] = Z_getspr("APLS", i - 11, 1, wp_sprd + i * 2);
1666 wp_spr[i * 2 + 1] = Z_getspr("APLS", i - 11, 2, wp_sprd + i * 2 + 1);
1668 for (; i < 18; i++) {
1669 wp_spr[i * 2] = Z_getspr("APBX", i - 13, 1, wp_sprd + i * 2);
1670 wp_spr[i * 2 + 1] = Z_getspr("APBX", i - 13, 2, wp_sprd + i * 2 + 1);
1672 for(; i < 20; i++) {
1673 wp_spr[i * 2] = Z_getspr("BFS1", i - 18, 1, wp_sprd + i * 2);
1674 wp_spr[i * 2 + 1] = Z_getspr("BFS1", i - 18, 2, wp_sprd + i * 2 + 1);
1676 for (; i < 26; i++) {
1677 wp_spr[i * 2] = Z_getspr("BFE1", i - 20, 1, wp_sprd + i * 2);
1678 wp_spr[i * 2 + 1] = Z_getspr("BFE1", i - 20, 2, wp_sprd + i * 2 + 1);
1680 for (; i < 30; i++) {
1681 wp_spr[i * 2] = Z_getspr("BFE2", i - 26, 1, wp_sprd + i * 2);
1682 wp_spr[i * 2 + 1] = Z_getspr("BFE2", i - 26, 2, wp_sprd + i * 2 + 1);
1684 for (; i < 32; i++) {
1685 wp_spr[i * 2] = Z_getspr("MISL", i - 30 + 4, 1, wp_sprd + i * 2);
1686 wp_spr[i * 2 + 1] = Z_getspr("MISL", i - 30 + 4, 2, wp_sprd + i * 2 + 1);
1688 for (; i < 37; i++) {
1689 wp_spr[i * 2] = Z_getspr("BAL1", i - 32, 1, wp_sprd + i * 2);
1690 wp_spr[i * 2 + 1] = Z_getspr("BAL1", i - 32, 2, wp_sprd + i * 2 + 1);
1692 for (; i < 42; i++) {
1693 wp_spr[i * 2] = Z_getspr("BAL7", i - 37, 1, wp_sprd + i * 2);
1694 wp_spr[i * 2 + 1] = Z_getspr("BAL7", i - 37, 2, wp_sprd + i * 2 + 1);
1696 for (; i < 47; i++) {
1697 wp_spr[i * 2] = Z_getspr("BAL2", i - 42, 1, wp_sprd + i * 2);
1698 wp_spr[i * 2 + 1] = Z_getspr("BAL2", i - 42, 2, wp_sprd + i * 2 + 1);
1700 for (; i < 49; i++) {
1701 wp_spr[i * 2] = Z_getspr("MANF", i - 47, 1, wp_sprd + i * 2);
1702 wp_spr[i * 2 + 1] = Z_getspr("MANF", i - 47, 2, wp_sprd + i * 2 + 1);
1704 /* Items */
1705 static const char snm[18][4] = {
1706 "CLIP", "SHEL", "ROCK", "CELL", "AMMO", "SBOX", "BROK", "CELP",
1707 "STIM", "MEDI", "BPAK",
1708 "CSAW", "SHOT", "SGN2", "MGUN", "LAUN", "PLAS", "BFUG"
1709 };
1710 static const char n4[4][4] = {
1711 "SOUL", "SMRT", "SMGT", "SMBT"
1712 };
1713 static const char n3[2][4] = {
1714 "GOR1", "FCAN"
1715 };
1716 for (i = 0; i < 18; i++) {
1717 item_spr[i] = Z_getspr(snm[i], 0, 0, item_sprd + i);
1719 for (; i < 20; i++) {
1720 item_spr[i] = Z_getspr("ARM1", i - 18, 0, item_sprd + i);
1721 item_spr[i + 2] = Z_getspr("ARM2", i - 18, 0, item_sprd + i);
1723 i+=2;
1724 for (; i < 26; i++) {
1725 item_spr[i] = Z_getspr("MEGA", i - 22, 0, item_sprd + i);
1727 for (; i < 30; i++) {
1728 item_spr[i] = Z_getspr("PINV", i - 26, 0, item_sprd + i);
1730 item_spr[30] = Z_getspr("AQUA", 0, 0, item_sprd + 30);
1731 item_spr[31] = Z_getspr("KEYR", 0, 0, item_sprd + 31);
1732 item_spr[32] = Z_getspr("KEYG", 0, 0, item_sprd + 32);
1733 item_spr[33] = Z_getspr("KEYB", 0, 0, item_sprd + 33);
1734 item_spr[34] = Z_getspr("SUIT", 0, 0, item_sprd + 34);
1735 for (n = 35, j = 0; j < 4; j++) {
1736 for (i = 0; i < 4; i++, n++) {
1737 item_spr[n] = Z_getspr(n4[j], i, 0, item_sprd + n);
1740 for (j = 0; j < 2; j++) {
1741 for (i = 0; i < 3; i++, n++) {
1742 item_spr[n] = Z_getspr(n3[j], i, 0, item_sprd + n);
1745 item_spr[57] = Z_getspr("GUN2", 0, 0, item_sprd + 57);
1746 /* Player */
1747 for (i = 0; i < 27; i++) {
1748 plr_spr[i * 2] = Z_getspr("PLAY", i, 1, plr_sprd + i * 2);
1749 plr_msk[i * 2] = R_gl_get_special_spr("PLAY", i, 1, &R_extract_mask_spr);
1750 plr_spr[i * 2 + 1] = Z_getspr("PLAY", i, 2, plr_sprd + i * 2 + 1);
1751 plr_msk[i * 2 + 1] = R_gl_get_special_spr("PLAY", i, 2, &R_extract_mask_spr);
1753 strncpy(s, "PWPx", 4);
1754 for (i = 1; i < 11; i++) {
1755 s[3] = (i < 10 ? '0' : 'A' - 10) + i;
1756 for (j = 0; j < 6; j++) {
1757 plr_wpn[i][j] = Z_getspr(s, j, 1, NULL);
1760 /* Monsters */
1761 static const char msn[MN_TN][4] = {
1762 "SARG", "TROO", "POSS", "SPOS", "CYBR", "CPOS", "BOSS", "BOS2", "HEAD", "SKUL",
1763 "PAIN", "SPID", "BSPI", "FATT", "SKEL", "VILE", "FISH", "BAR1", "ROBO", "PLAY"
1764 };
1765 static const int mms[MN_TN] = {
1766 14*2, 21*2, 21*2, 21*2, 16*2, 20*2, 15*2, 15*2, 12*2, 11*2,
1767 13*2, 19*2, 16*2, 20*2, 17*2, 29*2, 6*2, 2*2, 17*2, 23*2
1768 };
1769 mn_sgun[0] = Z_getspr("PWP4", 0, 1, NULL);
1770 mn_sgun[1] = Z_getspr("PWP4", 1, 1, NULL);
1771 for (j = 0; j < MN_TN; j++) {
1772 for (i = 0; i < mms[j]; i++) {
1773 mn_spr[j][i] = Z_getspr(msn[j], i / 2, (i & 1) + 1, &mn_sprd[j][i]);
1774 if (j == MN_MAN - 1) {
1775 mn_man_msk[i] = R_gl_get_special_spr(msn[j], i / 2, (i & 1) + 1, &R_extract_mask_spr);
1778 if (j == MN_BARREL - 1) {
1779 for (i = 4; i < 14; i++) {
1780 mn_spr[j][i] = Z_getspr("BEXP", i / 2 - 2, (i & 1) + 1, &mn_sprd[j][i]);
1784 for (i = 0; i < 8; i++) {
1785 mn_fspr[i] = Z_getspr("FIRE", i, 0, NULL);
1787 pl_spr[0] = Z_getspr("PLAY", 'N' - 'A', 0, NULL);
1788 pl_msk[0] = R_gl_get_special_spr("PLAY", 'N' - 'A', 0, &R_extract_mask_spr);
1789 pl_spr[1] = Z_getspr("PLAY", 'W' - 'A', 0, NULL);
1790 pl_msk[1] = R_gl_get_special_spr("PLAY", 'W' - 'A', 0, &R_extract_mask_spr);
1791 /* Misc */
1792 static const char mnm[22][8]={
1793 "STTNUM0", "STTNUM1", "STTNUM2", "STTNUM3", "STTNUM4",
1794 "STTNUM5", "STTNUM6", "STTNUM7", "STTNUM8", "STTNUM9",
1795 "STTMINUS", "STTPRCNT",
1796 "FISTA0", "CSAWA0", "PISTA0", "SHOTA0", "SGN2A0", "MGUNA0", "LAUNA0",
1797 "PLASA0", "BFUGA0", "GUN2A0"
1798 };
1799 stone = R_gl_loadimage("STONE");
1800 stone2 = R_gl_loadimage("STONE2");
1801 keys[0] = R_gl_loadimage("KEYRA0");
1802 keys[1] = R_gl_loadimage("KEYGA0");
1803 keys[2] = R_gl_loadimage("KEYBA0");
1804 for (i = 0; i < 22; i++) {
1805 sth[i] = R_gl_loadimage(mnm[i]);
1807 strcpy(s, "STBF_*");
1808 for (i = '!'; i < 160; i++) {
1809 s[5] = i;
1810 bfh[i - '!'] = R_gl_getimage(F_findres(s));
1812 for (i = '!'; i < 160; i++) {
1813 sprintf(s, "STCFN%03d", i);
1814 sfh[i - '!'] = R_gl_getimage(F_findres(s));
1816 strcpy(s, "WINUM*");
1817 for (i = '0'; i <= '9'; i++) {
1818 s[5] = i;
1819 bfh[i - '!'] = R_gl_loadimage(s);
1821 bfh[':' - '!'] = R_gl_loadimage("WICOLON");
1822 // menu
1823 msklh[0] = R_gl_loadimage("M_SKULL1");
1824 msklh[1] = R_gl_loadimage("M_SKULL2");
1825 mbarl = R_gl_loadimage("M_THERML");
1826 mbarm = R_gl_loadimage("M_THERMM");
1827 mbarr = R_gl_loadimage("M_THERMR");
1828 mbaro = R_gl_loadimage("M_THERMO");
1829 mslotl = R_gl_loadimage("M_LSLEFT");
1830 mslotm = R_gl_loadimage("M_LSCNTR");
1831 mslotr = R_gl_loadimage("M_LSRGHT");
1832 // walls
1833 for (i = 1; i < ANIT; i++) {
1834 for (j = 0; j < 5 && anm[i - 1][j]; j++) {
1835 anip[i][j] = R_gl_loadimage(anm[i - 1][j]);
1837 for(; j < 5; j++) {
1838 anip[i][j] = (image) {
1839 .n = NULL,
1840 .w = 8,
1841 .h = 8,
1842 .res = -1,
1843 };
1848 static void R_reload_textures (void);
1850 void R_set_videomode (int w, int h, int fullscreen) {
1851 assert(w > 0);
1852 assert(h > 0);
1853 int was = Y_videomode_setted();
1854 if (root != NULL) {
1855 R_cache_free(root, 0);
1856 root = NULL;
1858 int res = Y_set_videomode_opengl(w, h, fullscreen);
1859 if (res == 0) {
1860 if (was == 0) {
1861 ERR_failinit("Unable to set video mode\n");
1864 Y_get_videomode(&screen_width, &screen_height);
1865 screen_scale = max(1, screen_width / 320);
1866 root = R_cache_new();
1867 assert(root);
1868 R_alloc();
1869 R_reload_textures();
1872 static int video_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
1873 static int cur;
1874 static int w, h, fullscreen;
1875 static char buf[16];
1876 static int buflen;
1877 static int vmode;
1878 const videomode_t *v;
1879 enum { VIDEOMODE, FULLSCREEN, APPLY, __NUM__ };
1880 static const simple_menu_t sm = {
1881 GM_BIG, "Video", NULL,
1883 { "Mode: ", NULL },
1884 { "Fullscreen: ", NULL },
1885 { "Apply ", NULL },
1887 };
1888 if (msg->type == GM_ENTER) {
1889 Y_get_videomode(&w, &h);
1890 fullscreen = Y_get_fullscreen();
1891 v = Y_get_videomode_list_opengl(fullscreen);
1892 vmode = 0;
1893 while (vmode < v->n && v->modes[vmode].w != w && v->modes[vmode].h != h) {
1894 vmode += 1;
1896 if (vmode < v->n) {
1897 w = v->modes[vmode].w;
1898 h = v->modes[vmode].h;
1900 snprintf(buf, 16, "%ix%i", w, h);
1901 buflen = strlen(buf);
1902 return 1;
1904 if (i == VIDEOMODE) {
1905 switch (msg->type) {
1906 case GM_GETSTR: return GM_init_str(msg, buf, buflen);
1907 case GM_SELECT:
1908 v = Y_get_videomode_list_opengl(fullscreen);
1909 vmode = vmode + 1 >= v->n ? 0 : vmode + 1;
1910 if (v->n > 0) {
1911 w = v->modes[vmode].w;
1912 h = v->modes[vmode].h;
1913 } else {
1914 Y_get_videomode(&w, &h);
1916 snprintf(buf, 16, "%ix%i", w, h);
1917 buflen = strlen(buf);
1918 return 1;
1920 } else if (i == FULLSCREEN) {
1921 switch (msg->type) {
1922 case GM_GETSTR: return GM_init_str(msg, fullscreen ? "Yes" : "No ", 3);
1923 case GM_SELECT: fullscreen = !fullscreen; return 1;
1925 } else if (i == APPLY) {
1926 switch (msg->type) {
1927 case GM_SELECT: R_set_videomode(w, h, fullscreen); return 1;
1930 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
1933 static const menu_t video_menu = {
1934 NULL, &video_menu_handler
1935 };
1937 const menu_t *R_menu (void) {
1938 return &video_menu;
1941 void R_init (void) {
1942 logo("R_init: intialize opengl render\n");
1943 R_init_playpal();
1944 R_set_videomode(SCRW, SCRH, 0);
1947 void R_done (void) {
1948 R_cache_free(root, 1);
1949 Y_unset_videomode();
1950 root = NULL;
1953 void R_get_name (int n, char s[8]) {
1954 assert(n >= 0 && n < 256);
1955 if (walp[n].res == -1) {
1956 memset(s, 0, 8);
1957 } else if (walp[n].res == -2) {
1958 memcpy(s, "_WATER_", 8);
1959 s[7] = '0' + (intptr_t)walp[n].n - 1;
1960 } else if (walani[n] > 0) {
1961 memcpy(s, anm[walani[n] - 1][0], 8);
1962 } else {
1963 F_getresname(s, walp[n].res & 0x7FFF);
1967 static short getani (char n[8]) {
1968 short i = 0;
1969 while (i < ANIT - 1 && strncasecmp(n, anm[i][0], 8) != 0) {
1970 i++;
1972 return i < ANIT - 1 ? i + 1 : 0;
1975 int R_get_special_id (int n) {
1976 assert(n >= 0 && n <= 256);
1977 return walp[n].res == -2 ? (intptr_t)walp[n].n : -1;
1980 static void R_reload_textures (void) {
1981 int i;
1982 char s[8];
1983 for (i = 0; i < max_textures; i++) {
1984 R_get_name(i, s);
1985 if (walp[i].res >= 0) {
1986 walp[i] = R_gl_getimage(walp[i].res);
1989 if (horiz.n) {
1990 horiz = R_gl_getimage(horiz.res);
1994 void R_begin_load (void) {
1995 int i;
1996 for (i = 0; i < 256; i++) {
1997 if (walp[i].n != NULL && walp[i].res >= 0 && walani[i] == 0) {
1998 R_gl_free_image(&walp[i]);
2000 memset(&walp[i], 0, sizeof(image));
2001 walp[i].res = -1;
2002 walswp[i] = i;
2003 walani[i] = 0;
2005 memset(anic, 0, sizeof(anic));
2006 max_wall_width = 0;
2007 max_wall_height = 0;
2008 max_textures = 1;
2011 void R_load (char s[8], int f) {
2012 assert(max_textures < 256);
2013 if (!s[0]) {
2014 walp[max_textures] = (image) {
2015 .n = NULL,
2016 .x = 0,
2017 .y = 0,
2018 .w = 0,
2019 .h = 0,
2020 .res = -1,
2021 };
2022 } else if (strncasecmp(s, "_WATER_", 7) == 0) {
2023 walp[max_textures] = (image) {
2024 .n = (void*)((intptr_t)s[7] - '0' + 1),
2025 .x = 0,
2026 .y = 0,
2027 .w = 8,
2028 .h = 8,
2029 .res = -2,
2030 };
2031 } else {
2032 walp[max_textures] = R_gl_loadimage(s);
2033 if (f) {
2034 walp[max_textures].res |= 0x8000;
2036 if (s[0] == 'S' && s[1] == 'W' && s[4] == '_') {
2037 walswp[max_textures] = 0;
2039 walani[max_textures] = getani(s);
2041 max_wall_width = max(max_wall_width, walp[max_textures].w);
2042 max_wall_height = max(max_wall_height, walp[max_textures].h);
2043 max_textures++;
2046 void R_end_load (void) {
2047 int i, j, k, g;
2048 char s[8];
2049 j = max_textures;
2050 for (i = 1; i < 256 && j < 256; i++) {
2051 if (walswp[i] == 0) {
2052 R_get_name(i, s);
2053 s[5] ^= 1;
2054 g = F_getresid(s) | (walp[i].res & 0x8000);
2055 k = 1;
2056 while (k < 256 && walp[k].res != g) {
2057 k += 1;
2059 if (k >= 256) {
2060 k = j;
2061 j += 1;
2062 max_textures += 1;
2063 walp[k] = R_gl_getimage(g);
2064 walf[k] = g & 0x8000 ? 1 : 0;
2066 walswp[i] = k;
2067 walswp[k] = i;
2072 void R_loadsky (int sky) {
2073 char s[6];
2074 strcpy(s, "RSKYx");
2075 s[4] = '0' + sky;
2076 R_gl_free_image(&horiz);
2077 horiz = R_gl_loadimage(s);