- Timestamp:
- Nov 29, 2004, 1:04:28 PM (20 years ago)
- 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 3 1 /* 4 2 orxonox - the future of 3D-vertical-scrollers … … 16 14 */ 17 15 18 19 16 #include "hud.h" 20 17 … … 27 24 } 28 25 26 bool 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 76 GLvoid 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 98 GLvoid Hud::KillFont(GLvoid) { 99 glDeleteLists(base,256); 100 } 101 102 GLvoid 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 122 void 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 136 int 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 148 void 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 165 void 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 217 int 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 2 2 #define HUD_H 3 3 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 4 16 class Hud { 5 6 17 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; 7 25 Hud (); 8 26 ~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]; 10 44 }; 11 45 46 12 47 #endif -
orxonox/branches/sound/sound/sound_control.cc
r2979 r3020 18 18 using namespace std; 19 19 20 SoundControl* SoundControl::instance = NULL; 20 int sfx_channel1 = -1; 21 int sfx_channel2 = -1; 22 int finished = 0; 23 static SoundControl* instance = NULL; 24 static SoundControl* sound = SoundControl::getInstance(); 21 25 int volume = SDL_MIX_MAXVOLUME; 22 26 int track_number = 1; 23 27 static Mix_Music* music = NULL; 24 int audio_rate = MIX_DEFAULT_FREQUENCY, audio_channels = MIX_DEFAULT_CHANNELS, audio_buffers = 16384, bits = 0; 28 int audio_rate = 44100, audio_channels = MIX_DEFAULT_CHANNELS, 29 audio_buffers = 16384, bits = 0; 25 30 Uint16 audio_format = MIX_DEFAULT_FORMAT; 31 SDL_Event event; 26 32 27 33 … … 33 39 */ 34 40 SoundControl::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) { 39 42 printf("SDL_Init: INIT_AUDIO error.\n"); 40 43 } 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)) { 44 45 printf("Mix_OpenAudio: Failed to open audio!\n"); 45 46 } 46 47 47 initialise(); 48 48 } 49 49 50 51 50 /** 52 51 \brief Default destructor 53 52 */ 54 SoundControl::~SoundControl () { 53 SoundControl::~SoundControl () { 55 54 } 56 55 … … 71 70 72 71 /** 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 74 73 */ 75 74 void SoundControl::initialise() { 76 77 // Print some info78 75 Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); 79 76 bits=audio_format&0xFF; … … 82 79 } 83 80 84 85 /** 86 \brief Sets the number of output Channels (should not be used) 81 /** 82 \brief Sets the number of output Channels 87 83 */ 88 84 void SoundControl::setNumberOfChannels (int number_of_channels) { … … 90 86 } 91 87 92 93 88 /** 94 89 \brief May be called from any WorldEntity to play a .xm file 95 90 \param filename: self-explanatory 96 91 */ 97 intSoundControl::playMod (char* fileName) {92 void SoundControl::playMod (char* fileName) { 98 93 Mix_Chunk* chunk = NULL; 99 94 chunk = Mix_LoadWAV(fileName); … … 103 98 } 104 99 105 106 100 /** 107 101 \brief May be called from any WorldEntity to play a .wav file 108 102 \param filename: self-explanatory 109 103 */ 110 intSoundControl::playWav (char* fileName) {104 void SoundControl::playWav (char* fileName) { 111 105 Mix_Chunk* chunk = NULL; 112 106 chunk = Mix_LoadWAV(fileName); … … 116 110 } 117 111 118 119 112 /** 120 113 \brief May be called from any WorldEntity to play a .ogg file 121 114 \param filename: self-explanatory 122 115 */ 123 intSoundControl::playOgg (char* fileName) {116 void SoundControl::playOgg (char* fileName) { 124 117 Mix_Music* music = NULL; 125 118 music = Mix_LoadMUS(fileName); 126 if(Mix_PlayMusic(music, 1) == -1) {119 if(Mix_PlayMusic(music, 1) == -1) { 127 120 printf("Mix_PlayMusic: %s\n",Mix_GetError()); 128 121 } 129 122 Mix_HookMusicFinished(musicDone); 130 123 } 131 132 124 133 125 /** … … 150 142 } 151 143 152 153 144 /** 154 145 \brief Rewinds music to the beginning … … 158 149 } 159 150 160 161 151 /** 162 152 \brief Rewinds the music 5 seconds … … 166 156 } 167 157 168 169 158 /** 170 159 \brief Forwards the music 5 seconds … … 174 163 } 175 164 176 177 165 /** 178 166 \brief Pauses music output … … 182 170 } 183 171 184 185 172 /** 186 173 \brief this function pauses music output … … 189 176 Mix_ResumeMusic(); 190 177 } 191 192 178 193 179 /** … … 199 185 music = NULL; 200 186 } 187 188 /** 189 \brief Handles input events 190 */ 191 void 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 226 int 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 7 7 8 8 class SoundControl { 9 10 9 public: 11 10 static SoundControl* getInstance(); 12 11 static void deleteInstance(); 13 12 void setNumberOfChannels(int number_of_channels); 14 intplayMod(char* filename);15 intplayWav(char* filename);16 intplayOgg(char* filename);13 void playMod(char* filename); 14 void playWav(char* filename); 15 void playOgg(char* filename); 17 16 void volumeUp(); 18 17 void volumeDown(); … … 23 22 void resumeMusic(); 24 23 static void musicDone(); 24 void handleKey(SDL_KeyboardEvent key); 25 int main (void); 26 static SoundControl* instance; 25 27 int volume; 26 28 int track_number; … … 28 30 int done; 29 31 Uint16 audio_format; 30 31 protected: 32 int sfx_channel1; 33 int sfx_channel2; 34 int finished; 35 private: 32 36 void initialise(); 33 37 SoundControl(); 34 38 ~SoundControl(); 35 36 private:37 static SoundControl* instance;38 39 }; 39 40
Note: See TracChangeset
for help on using the changeset viewer.