Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/sound/hud/hud.cc @ 3276

Last change on this file since 3276 was 3020, checked in by simon, 20 years ago

/branches/sound/sound, branches/sound/hud: Made a few changes like a version which no longer compiles… Ah life is so hard on me. If it would compile, you would need to make a folder Data in hud and add a tga file named Font.tga with the letters in it.

File size: 8.4 KB
RevLine 
[2974]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Simon Hofmann
13   co-programmer:
14*/
15
16#include "hud.h"
17
18using namespace std;
19
20Hud::Hud () {
21}
22
23Hud::~Hud () {
24}
25
[3020]26bool Hud::LoadTGA(TextureImage *texture, char *filename) {   
27  GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};//Uncompressed TGA Header
28  GLubyte TGAcompare[12];
29  GLubyte header[6];//First 6 bytes of header
30  GLuint bytesPerPixel;
31  GLuint imageSize;
32  GLuint temp;
33  GLuint type=GL_RGBA;//GL Mode is RBGA (32 BPP)
34  FILE *file = fopen(filename, "rb");
35  if( file==NULL || fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) || memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 || fread(header,1,sizeof(header),file)!=sizeof(header)) {
36      if (file == NULL) 
37        return false;
38      else {
39        fclose(file);
40        return false;
41      }
42    }
43  texture->width = header[1] * 256 + header[0];//width (highbyte*256+lowbyte)
44  texture->height = header[3] * 256 + header[2];//height (highbyte*256+lowbyte)
45  if(texture->width <= 0 || texture->height <=0 || (header[4]!=24 && header[4]!=32)) {
46    fclose(file);
47    return false;
48  }
49  texture->bpp = header[4];//TGA's bits per pixel
50  bytesPerPixel = texture->bpp/8;
51  imageSize = texture->width*texture->height*bytesPerPixel;//memory required
52  texture->imageData=(GLubyte *)malloc(imageSize);//Reserve Memory     
53  if(texture->imageData==NULL || fread(texture->imageData, 1, imageSize, file)!=imageSize) {
54      if(texture->imageData!=NULL)
55        free(texture->imageData);
56      fclose(file);
57      return false;
58    }
59  for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel) { 
60      temp=texture->imageData[i];//Swaps the 1st and 3rd byte (red & blue)
61      texture->imageData[i] = texture->imageData[i + 2];
62      texture->imageData[i + 2] = temp;
63    }
64  fclose (file);
65  glGenTextures(1, &texture[0].texID);  // Generate OpenGL texture IDs
66  glBindTexture(GL_TEXTURE_2D, texture[0].texID);
67  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
68  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
69  if (texture[0].bpp==24) {
70      type=GL_RGB;
71    }
72  glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].width, texture[0].height, 0, type, GL_UNSIGNED_BYTE, texture[0].imageData);
73  return true;
74}
75
76GLvoid Hud::BuildFont(GLvoid) {
77  base=glGenLists(256);
78  glBindTexture(GL_TEXTURE_2D, textures[0].texID);
79  for (int loop1=0; loop1<256; loop1++) {
80    float cx=float(loop1%16)/16.0f;//X of current character
81    float cy=float(loop1/16)/16.0f;//Y of current character
82    glNewList(base+loop1,GL_COMPILE);   
83    glBegin(GL_QUADS);//Use a quad for each character
84    glTexCoord2f(cx,1.0f-cy-0.0625f);
85    glVertex2d(0,16);
86    glTexCoord2f(cx+0.0625f,1.0f-cy-0.0625f);
87    glVertex2i(16,16);
88    glTexCoord2f(cx+0.0625f,1.0f-cy-0.001f);
89    glVertex2i(16,0);
90    glTexCoord2f(cx,1.0f-cy-0.001f);
91    glVertex2i(0,0);
92    glEnd();
93    glTranslated(14,0,0);//Move to the right of the character
94    glEndList();
95  }                                                                           
96}
97
98GLvoid Hud::KillFont(GLvoid)    {
99  glDeleteLists(base,256);
100}
101
102GLvoid Hud::glPrint(GLint x, GLint y, int set, const char *fmt, ...) {
103  char text[1024];
104  va_list ap;   
105  if (fmt == NULL)
106    return;
107  va_start(ap, fmt);//parses string for variables, converts to numbers
108  vsprintf(text, fmt, ap);
109  va_end(ap);
110  if (set>1) {
111    set=1;
112  }
113  glEnable(GL_TEXTURE_2D);//Enable texture mapping
114  glLoadIdentity();                                             
115  glTranslated(x,y,0);
116  glListBase(base-32+(128*set));
117  glScalef(1.0f,2.0f,1.0f);//Enlarge by factor 2
118  glCallLists(strlen(text),GL_UNSIGNED_BYTE, text);     
119  glDisable(GL_TEXTURE_2D);                                     
120}
121
122void Hud::Resize(int width, int height) {
123  swidth=width;                 // Set Scissor Width To Window Width
124  sheight=height;               // Set Scissor Height To Window Height
125  if (height==0) {
126    height=1;   
127  }
128  glViewport(0,0,width,height);
129  glMatrixMode(GL_PROJECTION);
130  glLoadIdentity();                                             
131  glOrtho(0.0f,640,480,0.0f,-1.0f,1.0f);
132  glMatrixMode(GL_MODELVIEW);                                   
133  glLoadIdentity();
134}
135
136int Hud::InitGL() {
137  if (!LoadTGA(&textures[0],"Data/Font.tga")) {//Load font texture
138    return false;                               
139  }
140  BuildFont();
141  glShadeModel(GL_SMOOTH);//enable smooth shading
142  glClearColor(0.0f, 0.0f, 0.0f, 0.5f);         
143  glClearDepth(1.0f);
144  glBindTexture(GL_TEXTURE_2D, textures[0].texID);
145  return true;                                                 
146}
147
148void Hud::handleKeyPress(SDL_keysym *keysym) {
149  switch(keysym->sym) {
150  case SDLK_ESCAPE:
151    SDL_Quit();
152    break;
153  case SDLK_UP:
154    scroller = -1;
155    break;
156  case SDLK_DOWN:
157    scroller = 1;
158    break; 
159  default:
160    break;
161  } 
162  return;
163}
164
165void Hud::DrawGLScene() {
166  char  *token;
167  int cnt=0;
168  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   // Clear Screen And Depth Buffer
169    glColor3f(1.0f,0.5f,0.5f);//Set color to bright red
170  glPrint(50,16,1,"Renderer");
171  glPrint(80,48,1,"Hersteller");
172  glPrint(66,80,1,"Version");
173  glColor3f(1.0f,0.7f,0.4f);//Set color to orange
174  glPrint(200,16,1,(char *)glGetString(GL_RENDERER));           
175  glPrint(200,48,1,(char *)glGetString(GL_VENDOR));
176  glPrint(200,80,1,(char *)glGetString(GL_VERSION));
177    glColor3f(0.5f,0.5f,1.0f);//Set color to bright blue
178  glPrint(192,432,1,"orxonox HUD");
179  glLoadIdentity();   
180  glColor3f(1.0f,1.0f,1.0f);//Set the color to white
181  glBegin(GL_LINE_STRIP);
182  glVertex2d(639,417);
183  glVertex2d(  0,417);
184  glVertex2d(  0,480);
185  glVertex2d(639,480);
186  glVertex2d(639,128);
187  glEnd();
188  glBegin(GL_LINE_STRIP);
189  glVertex2d(  0,128);
190  glVertex2d(639,128);                                                         
191  glVertex2d(639,  1);
192  glVertex2d(  0,  1);
193  glVertex2d(  0,417);
194  glEnd();
195  glScissor(1   ,int(0.135416f*sheight),swidth-2,int(0.597916f*sheight));//Scissor region
196  glEnable(GL_SCISSOR_TEST);
197  char* text=(char *)malloc(strlen((char *)glGetString(GL_EXTENSIONS))+1);
198  strcpy (text,(char *)glGetString(GL_EXTENSIONS));//Store extension list
199  token=strtok(text," ");
200  while(token!=NULL) {
201    cnt++;
202    if (cnt>maxtokens) {
203      maxtokens=cnt;
204    }
205    glColor3f(0.5f,1.0f,0.5f);//Set color to bright green
206    glPrint(0,96+(cnt*32)-scroll,0,"%i",cnt);
207    glColor3f(1.0f,1.0f,0.5f);  // Set Color To Yellow
208    glPrint(50,96+(cnt*32)-scroll,0,token);
209    token=strtok(NULL," ");
210  }
211  glDisable(GL_SCISSOR_TEST);
212  free(text); 
213  SDL_GL_SwapBuffers();
214  return;                                                       
215}
216
217int Hud::main(int argc, char **argv) {
218  int videoFlags;
219  int done = false;
220  SDL_Event event;
221  const SDL_VideoInfo *videoInfo;
222  int isActive = true;
223  if(SDL_Init(SDL_INIT_VIDEO) < 0) {
224    fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
225    SDL_Quit();
226    exit(0);
227  }
228  videoInfo = SDL_GetVideoInfo();
229  if(!videoInfo) {
230    fprintf(stderr, "Video query failed: %s\n", SDL_GetError());
231    SDL_Quit();
232    exit(0);
233  }
234  videoFlags  = SDL_OPENGL;          // Enable OpenGL in SDL
235  videoFlags |= SDL_GL_DOUBLEBUFFER; // Enable double buffering
236  videoFlags |= SDL_HWPALETTE;       // Store the palette in hardware
237  videoFlags |= SDL_RESIZABLE;       // Enable window resizing
238  if(videoInfo->hw_available)
239    videoFlags |= SDL_HWSURFACE;
240  else
241    videoFlags |= SDL_SWSURFACE;
242  if (videoInfo->blit_hw)
243    videoFlags |= SDL_HWACCEL;
244  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
245  surface = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, videoFlags);
246 
247  if (!surface) {
248      fprintf(stderr,  "Video mode set failed: %s\n", SDL_GetError());
249      SDL_Quit();
250      exit(1);
251    }
252  InitGL();
253  Resize(SCREEN_WIDTH, SCREEN_HEIGHT);
254  DrawGLScene();
255  while (!done) {
256      SDL_Delay(10);
257      while(SDL_PollEvent(&event)) {
258        switch(event.type) {
259        case SDL_ACTIVEEVENT:
260          if(event.active.gain == 0)
261            isActive = false;
262          else
263            isActive = true;
264          break;                           
265        case SDL_VIDEORESIZE:
266          surface = SDL_SetVideoMode(event.resize.w, event.resize.h, 16, videoFlags);
267          if(!surface) {
268            fprintf(stderr, "Could not get a surface after resize: %s\n", SDL_GetError());
269            SDL_Quit();
270            exit(1);
271          }
272          Resize(event.resize.w, event.resize.h);
273          break;
274        case SDL_KEYDOWN:
275          handleKeyPress(&event.key.keysym);
276          break;
277        case SDL_KEYUP:
278          scroller = 0;
279          break;
280        case SDL_QUIT:
281          done = true;
282          break;
283        default:
284          break;
285        }
286      }
287      if(scroller == -1)
288        if(scroll > 0)
289          scroll -= 2;
290      if(scroller ==  1)
291        if(scroll < 32*(maxtokens-9))
292          scroll += 2;
293      if(isActive)
294        DrawGLScene();
295  }
296  SDL_Quit();
297  exit(0);
298}
299
Note: See TracBrowser for help on using the repository browser.