Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3020 in orxonox.OLD for orxonox


Ignore:
Timestamp:
Nov 29, 2004, 1:04:28 PM (20 years ago)
Author:
simon
Message:

/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.

Location:
orxonox/branches/sound
Files:
2 added
2 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/sound/hud/hud.cc

    r2974 r3020  
    1 
    2 
    31/*
    42   orxonox - the future of 3D-vertical-scrollers
     
    1614*/
    1715
    18 
    1916#include "hud.h"
    2017
     
    2724}
    2825
     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
  • orxonox/branches/sound/hud/hud.h

    r2974 r3020  
    22#define HUD_H
    33
     4#include <stdlib.h>
     5#include <stdio.h>
     6#include <stdarg.h>
     7#include <string.h>
     8#include <GL/gl.h>
     9#include <GL/glu.h>
     10#include "SDL.h"
     11
     12#define SCREEN_WIDTH  640
     13#define SCREEN_HEIGHT 480
     14#define SCREEN_BPP     16
     15
    416class Hud {
    5 
    617 public:
     18  typedef struct {
     19    GLubyte *imageData; // Image Data
     20    GLuint bpp;         // Image bits per pixel.
     21    GLuint width;       // Image width
     22    GLuint height;      // Image height
     23    GLuint texID;       // Texture ID
     24  } TextureImage;
    725  Hud ();
    826  ~Hud ();
    9 
     27  bool LoadTGA(TextureImage *texture, char *filename);
     28  GLvoid BuildFont(GLvoid);
     29  GLvoid KillFont(GLvoid);
     30  GLvoid glPrint(GLint x, GLint y, int set, const char *fmt, ...);
     31  void Resize(int width, int height);
     32  int InitGL();
     33  void handleKeyPress(SDL_keysym *keysym);
     34  void DrawGLScene();
     35  int main( int argc, char **argv );
     36  SDL_Surface* surface;
     37  int scroll;
     38  int maxtokens;
     39  int swidth;
     40  int sheight;
     41  int scroller;
     42  GLuint base;
     43  TextureImage textures[1];
    1044};
    1145
     46
    1247#endif
  • orxonox/branches/sound/sound/sound_control.cc

    r2979 r3020  
    1818using namespace std;
    1919
    20 SoundControl* SoundControl::instance = NULL;
     20int sfx_channel1 = -1;
     21int sfx_channel2 = -1;
     22int finished = 0;
     23static SoundControl* instance = NULL;
     24static SoundControl* sound = SoundControl::getInstance();
    2125int volume = SDL_MIX_MAXVOLUME;
    2226int track_number = 1;
    2327static Mix_Music* music = NULL;
    24 int audio_rate = MIX_DEFAULT_FREQUENCY, audio_channels = MIX_DEFAULT_CHANNELS, audio_buffers = 16384, bits = 0;
     28int audio_rate = 44100, audio_channels = MIX_DEFAULT_CHANNELS,
     29audio_buffers = 16384, bits = 0;
    2530Uint16 audio_format = MIX_DEFAULT_FORMAT;
     31SDL_Event event;
    2632
    2733
     
    3339*/
    3440SoundControl::SoundControl () {
    35 
    36   /*
    37   initializing sound and calling Mix_OpenAudio
    38   if(SDL_Init(SDL_INIT_AUDIO)<0){
     41  if(SDL_Init(SDL_INIT_AUDIO)<0) {
    3942    printf("SDL_Init: INIT_AUDIO error.\n");
    4043  }
    41   */
    42  
    43   if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)){
     44  if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
    4445    printf("Mix_OpenAudio: Failed to open audio!\n");
    4546  }
    46  
    4747  initialise();
    4848}
    4949
    50 
    5150/**
    5251    \brief Default destructor
    5352*/
    54 SoundControl::~SoundControl () { 
     53SoundControl::~SoundControl () {
    5554}
    5655
     
    7170
    7271/**
    73     \brief Is called by SoundControl object to initiate all values
     72    \brief Is called by SoundControl object to initiate all values and to output some text
    7473*/
    7574void SoundControl::initialise() {
    76  
    77   // Print some info
    7875  Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
    7976  bits=audio_format&0xFF;
     
    8279}
    8380
    84 
    85 /**
    86     \brief Sets the number of output Channels (should not be used)
     81/**
     82    \brief Sets the number of output Channels
    8783*/
    8884void SoundControl::setNumberOfChannels (int number_of_channels) {
     
    9086}
    9187
    92 
    9388/**
    9489    \brief May be called from any WorldEntity to play a .xm file
    9590    \param filename: self-explanatory
    9691*/
    97 int SoundControl::playMod (char* fileName) {
     92void SoundControl::playMod (char* fileName) {
    9893  Mix_Chunk* chunk = NULL;
    9994  chunk = Mix_LoadWAV(fileName);
     
    10398}
    10499
    105 
    106100/**
    107101    \brief May be called from any WorldEntity to play a .wav file
    108102    \param filename: self-explanatory
    109103*/
    110 int SoundControl::playWav (char* fileName) {
     104void SoundControl::playWav (char* fileName) {
    111105  Mix_Chunk* chunk = NULL;
    112106  chunk = Mix_LoadWAV(fileName);
     
    116110}
    117111
    118 
    119112/**
    120113    \brief May be called from any WorldEntity to play a .ogg file
    121114    \param filename: self-explanatory
    122115*/
    123 int SoundControl::playOgg (char* fileName) {
     116void SoundControl::playOgg (char* fileName) {
    124117  Mix_Music* music = NULL;
    125118  music = Mix_LoadMUS(fileName);
    126   if(Mix_PlayMusic(music, 1) == -1){
     119  if(Mix_PlayMusic(music, 1) == -1) {
    127120    printf("Mix_PlayMusic: %s\n",Mix_GetError());
    128121  }
    129122  Mix_HookMusicFinished(musicDone);
    130123}
    131 
    132124
    133125/**
     
    150142}
    151143
    152 
    153144/**
    154145    \brief Rewinds music to the beginning
     
    158149}
    159150
    160 
    161151/**
    162152    \brief Rewinds the music 5 seconds
     
    166156}
    167157
    168 
    169158/**
    170159    \brief Forwards the music 5 seconds
     
    174163}
    175164
    176 
    177165/**
    178166    \brief Pauses music output
     
    182170}
    183171
    184 
    185172/**
    186173    \brief this function pauses music output
     
    189176  Mix_ResumeMusic();
    190177}
    191 
    192178
    193179/**
     
    199185  music = NULL;
    200186}
     187
     188/**
     189    \brief Handles input events
     190*/
     191void SoundControl::handleKey(SDL_KeyboardEvent key) {
     192  switch(key.keysym.sym) {
     193  case SDLK_a:
     194    if(key.type == SDL_KEYDOWN) {
     195      if(sfx_channel1 < 0) {
     196        sfx_channel1 = sound->playWav("sound1.wav");
     197      }
     198    } else {
     199      Mix_HaltChannel(sfx_channel1);
     200      sfx_channel1 = -1;
     201    }
     202    break;
     203  case SDLK_s:
     204    if(key.type == SDL_KEYDOWN) {
     205      if(sfx_channel2 < 0) {
     206        sfx_channel2 = sound->playWav("sound2.wav");
     207      }
     208    } else {
     209      Mix_HaltChannel(sfx_channel2);
     210      sfx_channel2 = -1;
     211    }
     212    break;
     213  case SDLK_m:
     214    if(key.state == SDL_PRESSED) {
     215      sound->playOgg("music.ogg");
     216    }
     217    break;
     218  case SDLK_q:
     219    finished = 1;
     220    break;
     221  default:
     222    break;
     223  }
     224}
     225
     226int SoundControl::main(void) {
     227  SDL_Surface* screen;
     228  SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
     229  screen = SDL_SetVideoMode(320, 240, 0, 0);
     230  while(!finished) {
     231    while(SDL_PollEvent(&event)) {
     232      switch(event.type) {
     233      case SDL_QUIT:
     234        finished = 1;
     235        break;
     236      case SDL_KEYDOWN:
     237      case SDL_KEYUP:
     238        SoundControl::handleKey(event.key);
     239        break;
     240      default:
     241        break;
     242      }
     243    }
     244    SDL_Delay(50);
     245  }
     246  deleteInstance();
     247  SDL_Quit();
     248  return 0;
     249}
  • orxonox/branches/sound/sound/sound_control.h

    r2979 r3020  
    77
    88class SoundControl {
    9 
    109 public:
    1110  static SoundControl* getInstance();
    1211  static void deleteInstance();
    1312  void setNumberOfChannels(int number_of_channels);
    14   int playMod(char* filename);
    15   int playWav(char* filename);
    16   int playOgg(char* filename);
     13  void playMod(char* filename);
     14  void playWav(char* filename);
     15  void playOgg(char* filename);
    1716  void volumeUp();
    1817  void volumeDown();
     
    2322  void resumeMusic();
    2423  static void musicDone();
     24  void handleKey(SDL_KeyboardEvent key);
     25  int main (void);
     26  static SoundControl* instance;
    2527  int volume;
    2628  int track_number;
     
    2830  int done;
    2931  Uint16 audio_format;
    30 
    31 protected:
     32  int sfx_channel1;
     33  int sfx_channel2;
     34  int finished;
     35private:
    3236  void initialise();
    3337  SoundControl();
    3438  ~SoundControl();
    35 
    36 private:
    37   static SoundControl* instance;
    3839};
    3940
Note: See TracChangeset for help on using the changeset viewer.