Explosion Class (Particles lesson 19)
Hello,
I have created a Explosion class based in the lesson 19 Nehe gamedev. In my program I load a texture to the background when I press the space bar the explosion is created and show but the background image is replaced by black color.
I am using SDL with OpenGL, OpenGL draw all the graphics in the screen.
What can be happening???
Thanks,
Regards.
Quote: Original post by cHimura
Hello,
I have created a Explosion class based in the lesson 19 Nehe gamedev. In my program I load a texture to the background when I press the space bar the explosion is created and show but the background image is replaced by black color.
I am using SDL with OpenGL, OpenGL draw all the graphics in the screen.
What can be happening???
Thanks,
Regards.
You could post some code, that would be helpful!
Hello,
Yes, this is the code:
main.cpp
Explosion.cpp
Yes, this is the code:
main.cpp
#include <stdio.h>#include <stdlib.h>#include "SDL_opengl.h"#include "SDL_image.h"#include "SDL.h"#include <GL/gl.h>#include <GL/glu.h>#include "Explosion.h"#include <cstdlib>#include <ctime>/* screen width, height, and bit depth */#define SCREEN_WIDTH 640#define SCREEN_HEIGHT 480#define SCREEN_BPP 16/* Setup our booleans */#define TRUE 1#define FALSE 0GLuint background;GLenum texture_format = NULL;GLint nofcolors;/* This is our SDL surface */SDL_Surface *surface;Explosion *explosion;int loadBackground(){ SDL_Surface *bg; // this surface will tell us the details of the image if((bg = IMG_Load("images/bg.jpg"))){ //if((bg = SDL_LoadBMP("bg.bmp"))){ // Check that the image’s width is a power of 2 if ( (bg->w & (bg->w - 1)) != 0 ) { printf("warning: image.bmp’s width is not a power of 2\n"); } // Also check if the height is a power of 2 if ( (bg->h & (bg->h - 1)) != 0 ) { printf("warning: image.bmp’s height is not a power of 2\n"); } //get number of channels in the SDL surface nofcolors=bg->format->BytesPerPixel; //contains an alpha channel if(nofcolors==4) { if(bg->format->Rmask==0x000000ff) texture_format=GL_RGBA; else texture_format=GL_BGRA; } else if(nofcolors==3) //no alpha channel { if(bg->format->Rmask==0x000000ff) texture_format=GL_RGB; else texture_format=GL_BGR; } else { printf("warning: the image is not truecolor…this will break "); } // Have OpenGL generate a texture object handle for us glGenTextures(1, &background ); // Bind the texture object glBindTexture(GL_TEXTURE_2D, background); // Set the texture’s stretching properties glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexImage2D(GL_TEXTURE_2D, 0, nofcolors, bg->w, bg->h, 0, texture_format, GL_UNSIGNED_BYTE, bg->pixels); } else { printf("SDL could not load image.bmp: %s\n", SDL_GetError()); SDL_Quit(); return 1; } // Free the SDL_Surface only if it was successfully created if (bg) { SDL_FreeSurface(bg); } return 0;}/* function to release/destroy our resources and restoring the old desktop */void Quit( int returnCode ){ delete explosion; /* clean up the window */ SDL_Quit( ); /* and exit appropriately */ exit( returnCode );}/* function to reset our viewport after a window resize */int resizeWindow(int width, int height){ /* Height / width ration */ GLfloat ratio; /* Protect against a divide by zero */ if (height == 0) height = 1; ratio = (GLfloat)width / (GLfloat)height; /* Setup our viewport. */ glViewport(0, 0, (GLint)width, (GLint)height); /* change to the projection matrix and set our viewing volume. */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); /* Set our perspective */ //gluPerspective( 45.0f, ratio, 0.1f, 200.0f ); glOrtho(0,(GLint)width, (GLint)height, 0, -1, 1); /* Make sure we're chaning the model view and not the projection */ glMatrixMode(GL_MODELVIEW); /* Reset The View */ glLoadIdentity(); return(TRUE);}/* function to handle key press events */void handleKeyPress(SDL_keysym *keysym){ switch (keysym->sym) { case SDLK_ESCAPE: /* ESC key was pressed */ Quit(0); break; case SDLK_SPACE: explosion = new Explosion(rand()%SCREEN_WIDTH/1.0+1,rand()%SCREEN_HEIGHT/1.0+1); break; default: break; }}/* general OpenGL initialization function */int initGL(){ /* Enable smooth shading */ glShadeModel(GL_SMOOTH); /* Set the background black */ glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); /* Depth buffer setup */ glClearDepth( 1.0f ); /* Enables Depth Testing */ glDisable(GL_DEPTH_TEST); /* Enable Blending */ glEnable(GL_BLEND); /* Type Of Blending To Perform */ glBlendFunc( GL_SRC_ALPHA, GL_ONE ); /* Really Nice Perspective Calculations */ glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); /* Really Nice Point Smoothing */ glHint( GL_POINT_SMOOTH_HINT, GL_NICEST ); /* Enable Texture Mapping */ glEnable( GL_TEXTURE_2D ); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); /* Select Our Texture */ /* Reset all the particles */ /*for ( loop = 0; loop < MAX_PARTICLES; loop++ ) { int color = ( loop + 1 ) / ( MAX_PARTICLES / 12 ); float xi, yi, zi; xi = ( float )( ( rand( ) % 50 ) - 26.0f ) * 10.0f; yi = zi = ( float )( ( rand( ) % 50 ) - 25.0f ) * 10.0f; ResetParticle( loop, color, xi, yi, zi ); }*/ return(TRUE);}/* Here goes our drawing code */int drawGLScene(){ /* These are to calculate our fps */ static GLint T0 = 0; static GLint Frames = 0; /* Clear The Screen And The Depth Buffer */ glClear(GL_COLOR_BUFFER_BIT); //glClearColor(0.0f,0.0f,0.0f,0.0f); glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); //glDisable(GL_BLEND); // Cambio a la matriz de proyeccion //glMatrixMode(GL_PROJECTION); // Recupero de la pila, la matriz de proyeccion original // asi no tengo que volver a indicar a OpenGL como quiero // la proyeccion. Sencillamente la restablezco. //glPopMatrix(); // Cambio de nuevo a la matriz de modelo. //glMatrixMode(GL_MODELVIEW); //glTranslatef(0.0,0.0,0.0); //glColor4f(1.0f,1.0f,1.0f,0.5f); //glEnable(GL_BLEND); //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBindTexture(GL_TEXTURE_2D, background); //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); //glColor4f(1.0f,1.0f,1.0f,1.0f); glBegin(GL_QUADS); // Top-left vertex (corner) glTexCoord2i(0, 0); glVertex2i(0, 0); // Top-right vertex (corner) glTexCoord2i(1, 0); glVertex2i(SCREEN_WIDTH, 0); // Bottom-right vertex (corner) glTexCoord2i(1, 1); glVertex2i(SCREEN_WIDTH, SCREEN_HEIGHT); // Bottom-left vertex (corner) glTexCoord2i(0, 1); glVertex2i(0, SCREEN_HEIGHT); glEnd(); glLoadIdentity(); //glDisable(GL_BLEND); glDisable(GL_TEXTURE_2D); glTranslatef(100.0,100.0,0.0); glColor4f(1.0f,0.0f,0.0f,0.5f); glBegin(GL_QUADS); // Top-left vertex (corner) glTexCoord2i(0, 0); glVertex2i(0, 0); // Top-right vertex (corner) glTexCoord2i(1, 0); glVertex2i(100, 0); // Bottom-right vertex (corner) glTexCoord2i(1, 1); glVertex2i(100, 100); // Bottom-left vertex (corner) glTexCoord2i(0, 1); glVertex2i(0, 100); glEnd(); glLoadIdentity(); if(explosion != NULL) { if(explosion->isDead()) { delete explosion; explosion = NULL; printf("explosion borrada\n"); } else explosion->update(); } /* Draw it to the screen */ SDL_GL_SwapBuffers(); /* Gather our frames per second */ Frames++; { GLint t = SDL_GetTicks(); if (t - T0 >= 5000) { GLfloat seconds = (t - T0) / 1000.0; GLfloat fps = Frames / seconds; printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps); T0 = t; Frames = 0; } } return( TRUE );}int main( int argc, char **argv ){ /* Flags to pass to SDL_SetVideoMode */ int videoFlags; /* main loop variable */ int done = FALSE; /* used to collect events */ SDL_Event event; /* this holds some info about our display */ const SDL_VideoInfo *videoInfo; /* whether or not the window is active */ int isActive = TRUE; /* initialize SDL */ if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { fprintf( stderr, "Video initialization failed: %s\n", SDL_GetError( ) ); Quit( 1 ); } /* Fetch the video info */ videoInfo = SDL_GetVideoInfo( ); if ( !videoInfo ) { fprintf( stderr, "Video query failed: %s\n", SDL_GetError( ) ); Quit( 1 ); } /* the flags to pass to SDL_SetVideoMode */ videoFlags = SDL_OPENGL; /* Enable OpenGL in SDL */ videoFlags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */ videoFlags |= SDL_HWPALETTE; /* Store the palette in hardware */ videoFlags |= SDL_RESIZABLE; /* Enable window resizing */ /* This checks to see if surfaces can be stored in memory */ if ( videoInfo->hw_available ) videoFlags |= SDL_HWSURFACE; else videoFlags |= SDL_SWSURFACE; /* This checks if hardware blits can be done */ if ( videoInfo->blit_hw ) videoFlags |= SDL_HWACCEL; /* Sets up OpenGL double buffering */ SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); /* get a SDL surface */ surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, videoFlags ); /* Verify there is a surface */ if ( !surface ) { fprintf( stderr, "Video mode set failed: %s\n", SDL_GetError( ) ); Quit( 1 ); } /* Enable key repeat */ if ( ( SDL_EnableKeyRepeat( 100, SDL_DEFAULT_REPEAT_INTERVAL ) ) ) { fprintf( stderr, "Setting keyboard repeat failed: %s\n", SDL_GetError( ) ); Quit( 1 ); } /* initialize OpenGL */ if ( initGL( ) == FALSE ) { fprintf( stderr, "Could not initialize OpenGL.\n" ); Quit( 1 ); } /* Resize the initial window */ resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT ); //explosion = new Explosion(); explosion = NULL; srand(time(0)); loadBackground(); /* wait for events */ while ( !done ) { /* handle the events in the queue */ while ( SDL_PollEvent( &event ) ) { switch( event.type ) { case SDL_ACTIVEEVENT: /* Something's happend with our focus * If we lost focus or we are iconified, we * shouldn't draw the screen */ if ( event.active.gain == 0 ) isActive = FALSE; else isActive = TRUE; break; case SDL_VIDEORESIZE: /* handle resize event */ surface = SDL_SetVideoMode( event.resize.w, event.resize.h, 16, videoFlags ); if ( !surface ) { fprintf( stderr, "Could not get a surface after resize: %s\n", SDL_GetError( ) ); Quit( 1 ); } resizeWindow( event.resize.w, event.resize.h ); break; case SDL_KEYDOWN: /* handle key presses */ handleKeyPress( &event.key.keysym ); break; case SDL_QUIT: /* handle quit requests */ done = TRUE; break; default: break; } } /* If rainbow coloring is turned on, cycle the colors */ //if ( rainbow && ( delay > 25 ) ) //col = ( ++col ) % 12; /* draw the scene */ if (isActive) drawGLScene(); //delay++; } /* clean ourselves up and exit */ Quit(0); /* Should never get here */ return( 0 );}
Explosion.cpp
#include "Explosion.h"#include <iostream>GLfloat Explosion::colors[MAX_COLORS][3] ={ { 1.0f, 0.45f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}, { 0.0f, 0.0f, 0.0f}};/*GLfloat Explosion::colorsRED[MAX_COLORS][3] ={ { 1.0f, 0.0f, 0.0f}, { 0.9f, 0.0f, 0.0f}, { 0.8f, 0.0f, 0.0f}, { 0.7f, 0.0f, 0.0f}, { 0.6f, 0.0f, 0.0f}, { 0.5f, 0.0f, 0.0f}, { 0.4f, 0.0f, 0.0f}, { 0.3f, 0.0f, 0.0f}, { 0.2f, 0.0f, 0.0f}, { 0.1f, 0.0f, 0.0f}, { 0.2f, 0.0f, 0.0f}, { 0.3f, 0.0f, 0.0f}, { 0.4f, 0.0f, 0.0f}, { 0.5f, 0.0f, 0.0f}, { 0.6f, 0.0f, 0.0f}, { 0.7f, 0.0f, 0.0f}, { 0.8f, 0.0f, 0.0f}, { 0.9f, 0.0f, 0.0f}, { 1.0f, 0.0f, 0.0f}, { 0.9f, 0.0f, 0.0f}};*/Explosion::Explosion(double x, double y)//, int size, int numParticles){ this->x = x; this->y = y; //this->SIZE = size; //this->MAX_PARTICLES = numParticles; slowdown = 0.095f; /* Slow Down Particles */ zoom = -40.0f; col = 0; LoadGLTextures(); for(loop = 0; loop < MAX_PARTICLES; loop++) { int color = (loop + 1) / (MAX_PARTICLES / MAX_COLORS); float xi, yi, zi; xi = (float)((rand() % 50) - 26.0f) * 10.0f; yi = zi = (float)((rand() % 50) - 25.0f) * 10.0f; ResetParticle(loop, color, xi, yi, zi); } std::cout << ".:EXPLOSION NUEVA:." << std::endl;}Explosion::~Explosion(){ glDeleteTextures(1, &texture[1]);}/* function to load in bitmap as a GL texture */int Explosion::LoadGLTextures(){ /* Status indicator */ int Status = FALSE; /* Create storage space for the texture */ SDL_Surface *TextureImage[1]; /* Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit */ //if((TextureImage[0] = SDL_LoadBMP("data/particle.bmp"))) if((TextureImage[0] = IMG_Load("images/particle.png"))) { /* Set the status to true */ Status = TRUE; /* Create The Texture */ glGenTextures( 1, &texture[0] ); /* Typical Texture Generation Using Data From The Bitmap */ glBindTexture( GL_TEXTURE_2D, texture[0] ); /* Generate The Texture */ glTexImage2D( GL_TEXTURE_2D, 0, 3, TextureImage[0]->w, TextureImage[0]->h, 0, GL_BGR, GL_UNSIGNED_BYTE, TextureImage[0]->pixels ); /* Linear Filtering */ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } /* Free up any memory we may have used */ if(TextureImage[0]) SDL_FreeSurface(TextureImage[0]); return Status;}void Explosion::ResetParticle(int num, int color, float xDir, float yDir, float zDir){ /* Make the particels active */ particles[num].active = TRUE; /* Give the particles life */ particles[num].life = 1.0f; /* Random Fade Speed */ particles[num].fade = (float)(rand() %100) / 1000.0f + 0.05f; /* Select Red Rainbow Color */ particles[num].r = colors[<span class="cpp-number">0</span>];<br> <span class="cpp-comment">/* Select Green Rainbow Color */</span><br> particles[num].g = colors[<span class="cpp-number">1</span>];<br> <span class="cpp-comment">/* Select Blue Rainbow Color */</span><br> particles[num].b = colors[<span class="cpp-number">2</span>];<br> <span class="cpp-comment">/* Set the position on the X axis */</span><br> particles[num].x = x;<span class="cpp-comment">//100.0f;</span><br> <span class="cpp-comment">/* Set the position on the Y axis */</span><br> particles[num].y = y;<span class="cpp-comment">//100.0f;</span><br> <span class="cpp-comment">/* Set the position on the Z axis */</span><br> particles[num].z = <span class="cpp-number">0</span>.0f;<br> <span class="cpp-comment">/* Random Speed On X Axis */</span><br> particles[num].xi = xDir;<br> <span class="cpp-comment">/* Random Speed On Y Axi */</span><br> particles[num].yi = yDir;<br> <span class="cpp-comment">/* Random Speed On Z Axis */</span><br> particles[num].zi = zDir;<br> <span class="cpp-comment">/* Set Horizontal Pull To Zero */</span><br> particles[num].xg = <span class="cpp-number">0</span>.0f;<br> <span class="cpp-comment">/* Set Vertical Pull Downward */</span><br> particles[num].yg = -<span class="cpp-number">0</span>.8f;<br> <span class="cpp-comment">/* Set Pull On Z Axis To Zero */</span><br> particles[num].zg = <span class="cpp-number">0</span>.0f;<br><br> <span class="cpp-comment">//return;</span><br>}<br><br><span class="cpp-keyword">void</span> Explosion::update()<br>{<br> <span class="cpp-comment">//glLoadIdentity();</span><br><br> <span class="cpp-comment">//glTranslatef(0.0,0.0,0.0);</span><br><br> <span class="cpp-comment">//glEnable(GL_TEXTURE_2D);</span><br> <span class="cpp-comment">//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);</span><br> <span class="cpp-comment">//glBindTexture(GL_TEXTURE_2D, texture[0]);</span><br><br> <span class="cpp-comment">/* Modify each of the particles */</span><br> <span class="cpp-keyword">for</span>(loop = <span class="cpp-number">0</span>; loop < MAX_PARTICLES; loop++)<br> {<br> <span class="cpp-keyword">if</span>(particles[loop].active)<br> {<br> <span class="cpp-comment">/* Grab Our Particle X Position */</span><br> <span class="cpp-keyword">float</span> x = particles[loop].x;<br> <span class="cpp-comment">/* Grab Our Particle Y Position */</span><br> <span class="cpp-keyword">float</span> y = particles[loop].y;<br> <span class="cpp-comment">/* Particle Z Position + Zoom */</span><br> <span class="cpp-keyword">float</span> z = <span class="cpp-number">0</span>;<span class="cpp-comment">//particles[loop].z + zoom;</span><br><br> <span class="cpp-comment">/* Draw The Particle Using Our RGB Values,<br> * Fade The Particle Based On It's Life<br> */</span><br><br> <span class="cpp-comment">//glEnable(GL_BLEND);</span><br><br> <span class="cpp-comment">//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);</span><br><br> glColor4f(particles[loop].r,<br> particles[loop].g,<br> particles[loop].b,<br> particles[loop].life);<br><br> <span class="cpp-comment">/* Build Quad From A Triangle Strip */</span><br> glBegin( GL_TRIANGLE_STRIP );<br> <span class="cpp-comment">/* Top Right */</span><br> glTexCoord2d( <span class="cpp-number">1</span>, <span class="cpp-number">1</span> );<br> glVertex3f( x + SIZE, y + SIZE, z );<br> <span class="cpp-comment">/* Top Left */</span><br> glTexCoord2d(<span class="cpp-number">0</span>, <span class="cpp-number">1</span>);<br> glVertex3f( x - SIZE, y + SIZE, z );<br> <span class="cpp-comment">/* Bottom Right */</span><br> glTexCoord2d(<span class="cpp-number">1</span>, <span class="cpp-number">0</span>);<br> glVertex3f(x + SIZE, y - SIZE, z);<br> <span class="cpp-comment">/* Bottom Left */</span><br> glTexCoord2d(<span class="cpp-number">0</span>, <span class="cpp-number">0</span>);<br> glVertex3f(x - SIZE, y - SIZE, z);<br> glEnd();<br><br> <span class="cpp-comment">/* Move On The X Axis By X Speed */</span><br> particles[loop].x += particles[loop].xi/(slowdown * <span class="cpp-number">1000</span>);<br> <span class="cpp-comment">/* Move On The Y Axis By Y Speed */</span><br> particles[loop].y += particles[loop].yi/(slowdown * <span class="cpp-number">1000</span>);<br> <span class="cpp-comment">/* Move On The Z Axis By Z Speed */</span><br> particles[loop].z += <span class="cpp-number">0</span>;<span class="cpp-comment">//particles[loop].zi /(slowdown * 1000);</span><br><br> <span class="cpp-comment">/* Take Pull On X Axis Into Account */</span><br> particles[loop].xi += particles[loop].xg;<br> <span class="cpp-comment">/* Take Pull On Y Axis Into Account */</span><br> particles[loop].yi += particles[loop].yg;<br> <span class="cpp-comment">/* Take Pull On Z Axis Into Account */</span><br> particles[loop].zi += particles[loop].zg;<br><br> <span class="cpp-comment">/* Reduce Particles Life By 'Fade' */</span><br> particles[loop].life -= particles[loop].fade;<br> }<br> }<span class="cpp-comment">//for</span><br><br> glLoadIdentity();<br><br> <span class="cpp-comment">//glEnable(GL_BLEND);</span><br><br> <span class="cpp-comment">//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);</span><br>}<br><br><span class="cpp-keyword">bool</span> Explosion::isDead()<br>{<br> <span class="cpp-keyword">for</span> (loop = <span class="cpp-number">0</span>; loop < MAX_PARTICLES; loop++)<br> {<br> <span class="cpp-keyword">if</span> (particles[loop].life > <span class="cpp-number">0</span>)<br> <span class="cpp-keyword">return</span> <span class="cpp-keyword">false</span>;<br> }<br><br> <span class="cpp-keyword">return</span> <span class="cpp-keyword">true</span>;<br>}<br><br><br><br></pre></div><!–ENDSCRIPT–><br><br>Thanks,<br>Regards
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement