Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
glut_support.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 * glut_support.cpp - description
3 * --------------------------
4 * begin : December 28, 2001
5 * copyright : (C) 2001 by David Ranger
6 * email : ussreliant@users.sourceforge.net
7 ***************************************************************************/
8 
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * any later version. *
15 * *
16 ***************************************************************************/
17 
18 #include <png.h>
19 #include "glut_support.h"
20 #include "gfx/sprite.h"
21 #include "vs_globals.h"
22 #include "gfx/aux_texture.h"
23 #include "vs_globals.h"
24 #include "config_xml.h"
25 #include "gfx/vsimage.h"
26 #include "vsfilesystem.h"
27 #include "gldrv/gl_globals.h"
28 using namespace VSFileSystem;
29 
30 #define isspAce( chr ) \
31  ( (chr == '\t') || (chr == '\n') || (chr == '\v') || (chr == '\f') || (chr == '\r') \
32  || (chr == ' ') )
33 
34 float colors[] = {1, 1, 1, 1};
35 
36 //x,y must be -1 to 1, with -1,-1 being the bottom left corner. x,y must be the top left corner to draw
37 void ShowColor( float x, float y, float wid, float hei, float red, float green, float blue, float alpha )
38 {
39  float cols[4] = {red, green, blue, alpha};
40  if (wid < 0 || hei < 0) {
41  printf( "Cannot draw color with negative height or width\n" );
42  return;
43  }
44  //Make sure we don't exceed the program
45  if (x+wid > 1) wid = 1-x;
46  if (y-hei < -1) hei = -1+y;
47 #ifdef DEBUG
48  cout<<"Displaying color at "<<x<<","<<y<<"\n";
49  cout<<"with the dimensions of "<<wid<<","<<hei<<"\n";
50  cout<<"With the color "<<red<<","<<green<<","<<blue<<","<<alpha<<"\n";
51  cout<<"-----------------------------\n";
52 #endif
53  glDisable( GL_TEXTURE_2D );
54  glBegin( GL_QUADS );
55  glColor4fv( cols );
56  glVertex2f( x, y-hei );
57  glColor4fv( cols );
58  glVertex2f( x, y );
59  glColor4fv( cols );
60  glVertex2f( x+wid, y );
61  glColor4fv( cols );
62  glVertex2f( x+wid, y-hei );
63  glEnd();
64  glEnable( GL_TEXTURE_2D );
65 }
66 
67 float word_length( const char *str )
68 {
69  float length = 0;
70  while ( *str && *str != '\\' && ( !isspAce( *str ) ) ) {
71  char mychar = *str++;
72  length += glutStrokeWidth( GLUT_STROKE_ROMAN, mychar );
73  }
74  return length/2500;
75 }
76 void ShowText( float x, float y, float wid, int size, const char *str, int no_end )
77 {
78  static float rescale_font = XMLSupport::parse_float( vs_config->getVariable( "graphics", "gui_font_scale", ".75" ) );
79  float font_size_float = rescale_font*5./100;
80 
81  int cur;
82  float font_size = size*rescale_font;
83  float width = 0;
84  float cur_width = 0;
85  float end = 0;
86  font_size /= 10000;
87 
88  glDisable( GL_TEXTURE_2D );
89  glLoadIdentity();
91  glEnable( GL_LINE_SMOOTH );
92  glLineWidth( wid );
93  float page_wid = wid/rescale_font;
94  glTranslatef( x, y, 0 );
95  glScalef( font_size, font_size, 1 );
96  end = no_end ? 0 : glutStrokeWidth( GLUT_STROKE_ROMAN, 'A' );
97  end /= 2500;
98  //if (no_end == 1) { end = 0; }
99  int h = 0;
100  for (cur = 0; str[cur] != '\0'; cur++) {
101  cur_width = glutStrokeWidth( GLUT_STROKE_ROMAN, str[cur] );
102  cur_width /= 2500;
103  if ( (width+end+word_length( str+cur ) > page_wid || str[cur] == '\\') && str[cur+1] != '\0' ) {
104  if (no_end == 0) {
105  width += cur_width;
106  for (int i = 1; i <= 3; i++)
107  glutStrokeCharacter( GLUT_STROKE_ROMAN, '.' );
108  break;
109  } else {
110  width = 0;
111  glLoadIdentity();
113  glEnable( GL_LINE_SMOOTH );
114  glLineWidth( wid );
115  glTranslatef( x, y-(++h)*font_size_float, 0 );
116  glScalef( font_size, font_size, 1 );
117  }
118  } else {
119  width += cur_width;
120  }
121  if (str[cur] != '\\')
122  glutStrokeCharacter( GLUT_STROKE_ROMAN, str[cur] );
123  }
124  glLoadIdentity();
126  glDisable( GL_LINE_SMOOTH );
127 }
128 
129 float WidthOfChar( char chr )
130 {
131  float width = glutStrokeWidth( GLUT_STROKE_ROMAN, chr );
132  width /= 2500;
133  return width;
134 }
135 
136 static int mmx = 0;
137 static int mmy = 0;
138 void SetSoftwareMousePosition( int x, int y )
139 {
140  mmx = x;
141  mmy = y;
142 }
146 {
147  //glutSetCursor(GLUT_CURSOR_INHERIT);
148  //GFXViewPort (0,0,g_game.x_resolution,g_game.y_resolution);
149  GFXHudMode( true );
150  GFXColor4f( 1, 1, 1, 1 );
151 
154  GFXDisable( LIGHTING );
155  GFXDisable( CULLFACE );
156  GFXClear( clr );
159  GFXDisable( TEXTURE1 );
160  GFXEnable( TEXTURE0 );
161 }
162 void DrawGlutMouse( int mousex, int mousey, VSSprite *spr )
163 {
165  GFXColor4f( 1, 1, 1, 1 );
166  GFXDisable( TEXTURE1 );
167  GFXEnable( TEXTURE0 );
169  GFXDisable( LIGHTING );
170  float sizex = 0, sizey = 0;
171  spr->GetSize( sizex, sizey );
172  float tempx = 0, tempy = 0;
173  spr->GetPosition( tempx, tempy );
174  spr->SetPosition( tempx+ -1+.5*sizex+float(mousex)
175  /(.5*g_game.x_resolution), tempy+1+.5*sizey-float(mousey)/(.5*g_game.y_resolution) );
176  spr->Draw();
177  GFXDisable( TEXTURE0 );
178  GFXEnable( TEXTURE0 );
179  spr->SetPosition( tempx, tempy );
180 }
181 extern void ConditionalCursorDraw( bool );
182 void EndGUIFrame( MousePointerStyle pointerStyle )
183 {
184  static VSSprite MouseOverVSSprite( "mouseover.spr", BILINEAR, GFXTRUE );
185  static VSSprite MouseVSSprite( "mouse.spr", BILINEAR, GFXTRUE );
186  static Texture dummy( "white.bmp", 0, NEAREST, TEXTURE2D, TEXTURE_2D, GFXTRUE );
187 
188  if (pointerStyle != MOUSE_POINTER_NONE) {
189  dummy.MakeActive();
190  GFXDisable( CULLFACE );
191 
192  VSSprite *whichSprite = &MouseVSSprite;
193  switch(pointerStyle) {
194  case MOUSE_POINTER_NORMAL: whichSprite = &MouseVSSprite; break;
195  case MOUSE_POINTER_HOVER: whichSprite = &MouseOverVSSprite; break;
196  }
197 
198  DrawGlutMouse( mmx, mmy, whichSprite );
199 
200  //GFXEndScene();bad things...only call this once
201  GFXHudMode( false );
202  GFXEnable( CULLFACE );
203  ConditionalCursorDraw( true );
204  }
205 }
206