summaryrefslogtreecommitdiff
path: root/g_src/graphics.h
blob: cf3ffee1da30fd396730ecc1127f04207c9e65cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#ifndef GRAPHICS_H
#define GRAPHICS_H

#include <string>
#include <map>
#include <cassert>
using std::string;

#include "GL/glew.h"
#include "g_basics.h"
#include "platform.h"
#include "basics.h"

enum Texture
{
	TEXTURE_MOUSE,
	TEXTURENUM
};

/* screen array layout
 *
 *
 * X*Y tiles of 4 bytes each in column-major order (FIXME: This is inefficient! It should be row-major!)
 * For each tile, byte 0 is the character, 1 is foreground color, 2 is bacground, and 3 denotes bold.
 *
 * As there are only 8 different colors and bold is a boolean, this leaves a lot of free space. Therefore,
 * without involving the graphics arrays, out-of-gamut values can be used for designating TTF objects.
 *
 * This means setting the bold byte to all 1s (0xff), and then using the other three bytes as a designator.
 *
 * Time will tell whether this was a good idea or not.
 */

// So yeah, we store "type" in the previously bold byte. This means we have quite a lot of free types yet.
#define GRAPHICSTYPE_TTF 0xff
// This type denotes a tile that is covered by truetype, but is not the tile it starts on.
#define GRAPHICSTYPE_TTFCONT 0xfe


class graphicst
{
  int lookup_pair(std::pair<int,int> color);
  long calculate_old_fps();
	public:
		long screenx,screeny;
		char screenf,screenb;
		char screenbright;

		unsigned char *screen;
		long *screentexpos;
		char *screentexpos_addcolor;
		unsigned char *screentexpos_grayscale;
		unsigned char *screentexpos_cf;
		unsigned char *screentexpos_cbr;

                // Calling this is not enough in itself. You also need to call swap_front/back.
                void resize(int x, int y);
                                
		long clipx[2],clipy[2];
		long tex_pos[TEXTURENUM];

		long rect_id;

		LARGE_INTEGER print_time[100];
		long print_index;
		char display_frames;

		short force_full_display_count;

		char original_rect;

                int dimx, dimy;

		graphicst()
			{
			print_index=0;
			display_frames=0;
			rect_id=-1;
			force_full_display_count=4;
			original_rect=1;

                        screentexpos = NULL;
                        screentexpos_addcolor = NULL;
                        screentexpos_grayscale = NULL;
                        screentexpos_cf = NULL;
                        screentexpos_cbr = NULL;
                        screen = NULL;
                        }

                void locate(long y,long x)
                {
                  // No point in clamping here, addchar clamps too.
                  screenx=x;
                  screeny=y;
                }
                void changecolor(short f,short b,char bright)
                {
                  screenf=f;
                  screenb=b;
                  screenbright=bright;
                }
                void addchar(unsigned char c,char advance=1)
                {
                  /* assert (screen_limit == screen + dimy * dimx * 4); */
                  unsigned char *s = screen + screenx*dimy*4 + screeny*4;
                  if (s < screen_limit) {
	if(screenx>=clipx[0]&&screenx<=clipx[1]&&
		screeny>=clipy[0]&&screeny<=clipy[1])
		{
                    *s++ = c;
                    *s++ = screenf;
                    *s++ = screenb;
                    *s++ = screenbright;
                    screentexpos[screenx*dimy + screeny]=0;
	}
                  }
                  screenx += advance;
                }
                void addchar(unsigned int x, unsigned int y, unsigned char c,
                             unsigned char f, unsigned char b, unsigned char bright) {
                  /* assert (screen_limit == screen + dimy * dimx * 4); */
                  unsigned char *s = screen + x*dimy*4 + y*4;
                  if (s >= screen && s < screen_limit) {
	if(x>=clipx[0]&&x<=clipx[1]&&
		y>=clipy[0]&&y<=clipy[1])
		{
                    *s++ = c;
                    *s++ = f;
                    *s++ = b;
                    *s++ = bright;
	}
                  }
                }
		void addcoloredst(const char *str,const char *colorstr);
		void addst(const string &str, justification just = justify_left, int space=0);
		void erasescreen_clip();
		void erasescreen();
                void erasescreen_rect(int x1, int x2, int y1, int y2);
		void setclipping(long x1,long x2,long y1,long y2);

		void add_tile(long texp,char addcolor);
		void add_tile_grayscale(long texp,char cf,char cbr);

		void prepare_graphics(string &src_dir);

		void gray_out_rect(long sx,long ex,long sy,long ey)
                {
                  long x,y;
                  for(x=sx;x<=ex;x++)
                    {
                      for(y=sy;y<=ey;y++)
                        {
                          screen[x*dimy*4 + y*4 + 1]=0;
                          screen[x*dimy*4 + y*4 + 2]=7;
                          screen[x*dimy*4 + y*4 + 3]=0;
                        }
                    }
                }
		void dim_colors(long x,long y,char dim);

		void rain_color_square(long x,long y);
		void snow_color_square(long x,long y);
		void color_square(long x,long y,unsigned char f,unsigned char b,unsigned char br);

		long border_start_x(){return 1;}
		long border_start_y(){return 1;}
		long border_end_x(){return 78;}
		long border_end_y(){return 23;}
		long text_width(){return 1;}
		long text_height(){return 1;}
		long window_element_height(long minus,char border)
			{
			long height=25;
			if(border)height-=2;
			height-=text_height()*minus;
			return height;
			}

                int mouse_x, mouse_y;
		void get_mouse_text_coords(int32_t &mx, int32_t &my);
                void draw_border(int x1, int x2, int y1, int y2);

                // Instead of doing costly bounds-checking calculations, we cache the end
                // of the arrays..
                unsigned char *screen_limit;
};

extern graphicst gps;
// From graphics.cpp
void render_things();

// Locates some area of the screen with free space for writing
class gps_locator {
  int y, last_x;
public:
  gps_locator(int y, int x) {
    this->y = y;
    last_x = x;
  }
  bool is_free(int x) {
    unsigned char c = gps.screen[x*gps.dimy*4 + y*4];
    switch (c) {
    case 0:
    case 20:
    case 176:
    case 177:
    case 178:
    case 219:
    case 254:
    case 255:
      return true;
    default:
      return false;
    }
  }
  void operator()(int sz) {
    // First, check if our cached slot will still do
    bool ok = true;
    for (int x = last_x; x < last_x + sz; x++)
      if (!is_free(x)) {
        ok = false;
        break;
      }
    if (ok) {
      // Yeah, okay
      gps.locate(y, last_x);
    } else {
      // Not so okay. Find a new spot.
      int run = 0, x = 0;
      for (; x < gps.dimx; x++) {
        if (is_free(x))
          run++;
        else run = 0;
        if (run > sz + 2) { // We pad things a bit for cleanliness.
          ok = true;
          x -= sz + 1;
          break;
        }
      }
      if (ok) {
        // Found a new spot.
        last_x = x;
        gps.locate(y, x);
      } else {
        // Damn it.
        gps.locate(y, last_x);
      }
    }
  }
};

#endif