File: gl_unit.c

package info (click to toggle)
glhack 1.2-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 24,604 kB
  • ctags: 18,992
  • sloc: ansic: 208,570; cpp: 13,139; yacc: 2,005; makefile: 1,161; lex: 377; sh: 321; awk: 89; sed: 11
file content (304 lines) | stat: -rw-r--r-- 6,690 bytes parent folder | download | duplicates (16)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* Copyright (C) 2002 Andrew Apted <ajapted@users.sourceforge.net> */
/* NetHack may be freely redistributed.  See license for details.  */

/*
 * SDL/GL window port for NetHack & Slash'EM.
 *
 * Unit system.  Sorts the units (GL images) based on texture id,
 * which can give _much_ faster results.  The startup routine also
 * initialises the GL into the required state.
 */

#include "hack.h"

#if defined(GL_GRAPHICS)  /* hardware only */

#define WINGL_INTERNAL
#include "winGL.h"


int sdlgl_tex_max = 0;


#define MAX_UNITS  2048

static struct GraphicUnit unit_array[MAX_UNITS];
static short unit_map[MAX_UNITS];
static int num_units = 0;
static int unit_overlap = 0;


void sdlgl_unit_startup(void)
{
  const char *str;

  /* query some GL properties */

  str = (const char *) glGetString(GL_VERSION);
  if (! str)
  {
    sdlgl_error("OpenGL load problem ! (version string is NULL)\n");
  }

  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &sdlgl_tex_max);
  if (sdlgl_tex_max < 128)
  {
    sdlgl_error("OpenGL texture limit too small (%d).\n", sdlgl_tex_max);
  }
  if (sdlgl_tex_max > 2048)
    sdlgl_tex_max = 2048;

#if 0  /* DEBUGGING */
  str = (const char *) glGetString(GL_VERSION);
  sdlgl_warning("OpenGL Version: %s\n", str);
  
  str = (const char *) glGetString(GL_VENDOR);
  sdlgl_warning("OpenGL Vendor: %s\n", str);
  
  str = (const char *) glGetString(GL_RENDERER);
  sdlgl_warning("OpenGL Renderer: %s\n", str);

  sdlgl_warning("OpenGL Texture Size: %d\n", sdlgl_tex_max);
#endif
 
  /* initialise all important GL state */

  glDisable(GL_FOG);
  glDisable(GL_BLEND);
  glDisable(GL_LIGHTING);
  glDisable(GL_TEXTURE_2D);
  glDisable(GL_CULL_FACE);
  glDisable(GL_DEPTH_TEST);
  glDisable(GL_SCISSOR_TEST);
  glDisable(GL_STENCIL_TEST);

  glEnable(GL_LINE_SMOOTH);

  glDisable(GL_POLYGON_SMOOTH);
  glDisable(GL_DITHER);

  glEnable(GL_NORMALIZE);

  glFrontFace(GL_CW);
  glShadeModel(GL_SMOOTH);
  glDepthFunc(GL_LEQUAL);
  glAlphaFunc(GL_GREATER, 1.0 / 32.0);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  glHint(GL_FOG_HINT, GL_FASTEST);
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

  /* turn off lighting stuff */
  glDisable(GL_LIGHTING);
  glDisable(GL_COLOR_MATERIAL);

  glNormal3f(0.0, 0.0, 1.0);

  /* setup the GL matrices for drawing 2D stuff */

  glViewport(0, 0, sdlgl_width, sdlgl_height);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0.0, (float)sdlgl_width, 
          0.0, (float)sdlgl_height, -1.0, 1.0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  /* clear screen */
  glClearColor(0.0, 0.0, 0.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT);
}

void sdlgl_unit_shutdown(void)
{
  /* does nothing */
}

/* ---------------------------------------------------------------- */


static int unit_compare(const void *p1, const void *p2)
{
  const struct GraphicUnit *A = unit_array + *((short *) p1);
  const struct GraphicUnit *B = unit_array + *((short *) p2);

  if (A->pass != B->pass)
    return A->pass - B->pass;

  if (A->tex_id != B->tex_id)
    return (int)A->tex_id - (int)B->tex_id;

  return (int)A->color - (int)B->color;
}

static void flush_units(void)
{
  int i;
  int begun = 0;
  GLuint current_tex = 0x789ABCDE;
  
  if (num_units < 1)
    return;
  
  /* sort units into increasing texture ID */
  for (i=0; i < num_units; i++)
    unit_map[i] = i;

  if (! unit_overlap)
    qsort(unit_map, num_units, sizeof(short), unit_compare);

  glEnable(GL_TEXTURE_2D);

  /* now draw all the goodies */
  for (i=0; i < num_units; i++)
  {
    struct GraphicUnit *unit = unit_array + unit_map[i];

    if (unit->tex_id != current_tex)
    {
      if (begun)
        glEnd();

      current_tex = unit->tex_id;
      glBindTexture(GL_TEXTURE_2D, current_tex);
      
      glBegin(GL_QUADS);
      begun = 1;
    }

    /* we don't bother minimising color changes */
    glColor4f(
        GAMMA_F(RGB_RED(unit->color)),
        GAMMA_F(RGB_GRN(unit->color)),
        GAMMA_F(RGB_BLU(unit->color)), unit->trans);

    glTexCoord2f(unit->tx1, unit->ty1);
    glVertex2i(unit->x, unit->y);

    glTexCoord2f(unit->tx1, unit->ty2);
    glVertex2i(unit->x, unit->y + unit->h);

    glTexCoord2f(unit->tx2, unit->ty2);
    glVertex2i(unit->x + unit->w, unit->y + unit->h);

    glTexCoord2f(unit->tx2, unit->ty1);
    glVertex2i(unit->x + unit->w, unit->y);
  }

  if (begun)
    glEnd();

  glDisable(GL_TEXTURE_2D);

  /* we've drawn 'em all... */
  num_units = 0;
}

/* ---------------------------------------------------------------- */


void sdlgl_begin_units(int blending, int overlap)
{
  assert(num_units == 0);

  if (blending)
    glEnable(GL_BLEND);

  unit_overlap = overlap;
}

void sdlgl_add_unit(GLuint id, float tx1, float ty1, 
    float tx2, float ty2, short x, short y, short w, short h,
    short pass, rgbcol_t color, float trans)
{
  struct GraphicUnit *unit;
  
  if (num_units == MAX_UNITS)
    flush_units();

  assert(num_units < MAX_UNITS);

  unit = unit_array + num_units;
  num_units++;

  unit->tex_id = id;
  unit->tx1 = tx1;  unit->ty1 = ty1;
  unit->tx2 = tx2;  unit->ty2 = ty2;
  unit->x = x;  unit->y = y;
  unit->w = w;  unit->h = h;
  unit->pass  = pass;
  unit->color = color;
  unit->trans = trans;
}
  
void sdlgl_finish_units(void)
{
  if (num_units > 0)
    flush_units();

  glDisable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  assert(num_units == 0);
}


/* ---------------------------------------------------------------- */

void sdlgl_hw_make_screenshot(const char *prefix)
{
  unsigned char *pixels;
  unsigned char *p;
  
  char filename[256];
  char msgbuf[512];

  int x, crc1, crc2;
  int success;

  pixels = (unsigned char *) alloc(sdlgl_width * sdlgl_height * 3);

  glReadBuffer(GL_FRONT);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

  glReadPixels(0, 0, sdlgl_width, sdlgl_height, GL_RGB, 
      GL_UNSIGNED_BYTE, pixels);

  /* compute Adler CRC (per RFC 1950) on image.
   * We'll create a fairly unique filename using it.
   */
  crc1 = crc2 = 0;
  p = pixels;

  for (x=0; x < sdlgl_width * sdlgl_height * 3; x++, p++)
  {
    crc1 = (crc1 + (int) *p) % 65521;
    crc2 = (crc2 + crc1)     % 65521;
  }

  /* save image as a PPM (Portable PixMap) file.
   */
  sprintf(filename, "%s%04X%04X.ppm", prefix, crc2, crc1);

  success = sdlgl_save_ppm_file(filename, pixels, sdlgl_width, 
      sdlgl_height);

  free(pixels);

  /* FIXME: show a pop-up window ! */
  if (success)
  {
    sprintf(msgbuf, "Screenshot saved to: %s", filename);
    Sdlgl_raw_print(msgbuf);
  }
  else
  {
    sprintf(msgbuf, "Screenshot failed to open: %s", filename);
    Sdlgl_raw_print(msgbuf);
  }
}

#endif /* GL_GRAPHICS */
/*gl_unit.c*/