Results 1 to 3 of 3
  1. #1
    Join Date
    May 2005
    Posts
    102

    Sprite animation program access violation at 0X0000000

    Hi,

    My program went nuts when I changed the clip_rects from 30 to 50 ( all the sprites are still the same size, just more columns). Access violations all over the place. I'm not a programmer so this isn't a pro program , but is intended just to run sprites I have made. Any help would be appreciated. Thx.

    Ps. If you suggest a more efficient way to do this please feel free show me the code. Thx

    Code:
    #include "SDL.h"
    #include "SDL_mixer.h"
    #include <time.h>
    #include <ctime>
    #include <cstdlib>
    #include <windows.h>
    
    using namespace std;
    
    void setrects(SDL_Rect* clip)
    {
    
    clip[0].x=0;
    clip[0].y=0; 
    clip[0].w=640;  
    clip[0].h=480; 
    
    clip[1].x=640;
    clip[1].y=0; 
    clip[1].w=640;  
    clip[1].h=480; 
    
    clip[2].x=1280;
    clip[2].y=0; 
    clip[2].w=640;  
    clip[2].h=480; 
    
    clip[3].x=1920;
    clip[3].y=0; 
    clip[3].w=640;  
    clip[3].h=480; 
    
    clip[4].x=2560;
    clip[4].y=0; 
    clip[4].w=640;  
    clip[4].h=480; 
    
    clip[5].x=3200;
    clip[5].y=0; 
    clip[5].w=640;  
    clip[5].h=480; 
    
    clip[6].x=3840;
    clip[6].y=0; 
    clip[6].w=640;  
    clip[6].h=480; 
    
    clip[7].x=4480;
    clip[7].y=0; 
    clip[7].w=640;  
    clip[7].h=480; 
    
    clip[8].x=5120;
    clip[8].y=0; 
    clip[8].w=640;  
    clip[8].h=480; 
    
    clip[9].x=5760;
    clip[9].y=0; 
    clip[9].w=640;  
    clip[9].h=480;  // row 1
    
    clip[10].x=0;
    clip[10].y=480; 
    clip[10].w=640;  
    clip[10].h=480; 
    
    clip[11].x=640;
    clip[11].y=480; 
    clip[11].w=640;  
    clip[11].h=480; 
    
    clip[12].x=1280;
    clip[12].y=480; 
    clip[12].w=640;  
    clip[12].h=480; 
    
    clip[13].x=1920;
    clip[13].y=480; 
    clip[13].w=640;  
    clip[13].h=480; 
    
    clip[14].x=2560;
    clip[14].y=480; 
    clip[14].w=640;  
    clip[14].h=480; 
    
    clip[15].x=3200;
    clip[15].y=480; 
    clip[15].w=640;  
    clip[15].h=480; 
    
    clip[16].x=3840;
    clip[16].y=480; 
    clip[16].w=640;  
    clip[16].h=480; 
    
    clip[17].x=4480;
    clip[17].y=480; 
    clip[17].w=640;  
    clip[17].h=480; 
    
    clip[18].x=5120;
    clip[18].y=480; 
    clip[18].w=640;  
    clip[18].h=480; 
    
    clip[19].x=5760;
    clip[19].y=480; 
    clip[19].w=640;  
    clip[19].h=480; // end row 2
    
    clip[20].x=0;
    clip[20].y=960; 
    clip[20].w=640;  
    clip[20].h=480; 
    
    clip[21].x=640;
    clip[21].y=960; 
    clip[21].w=640;  
    clip[21].h=480; 
    
    clip[22].x=1280;
    clip[22].y=960; 
    clip[22].w=640;  
    clip[22].h=480; 
    
    clip[23].x=1920;
    clip[23].y=960; 
    clip[23].w=640;  
    clip[23].h=480; 
    
    clip[24].x=2560;
    clip[24].y=960; 
    clip[24].w=640;  
    clip[24].h=480; 
    
    clip[25].x=3200;
    clip[25].y=960; 
    clip[25].w=640;  
    clip[25].h=480; 
    
    clip[26].x=3840;
    clip[26].y=960; 
    clip[26].w=640;  
    clip[26].h=480; 
    
    clip[27].x=4480;
    clip[27].y=960; 
    clip[27].w=640;  
    clip[27].h=480; 
    
    clip[28].x=5120;
    clip[28].y=960; 
    clip[28].w=640;  
    clip[28].h=480; 
    
    clip[29].x=5760;
    clip[29].y=960; 
    clip[29].w=640;  
    clip[29].h=480; // end row 3
    
     clip[30].x=0;
    clip[30].y=1440; 
    clip[30].w=640;  
    clip[30].h=480; 
     
     clip[31].x=640;
    clip[31].y=1440; 
    clip[31].w=640;  
    clip[31].h=480; 
    
    clip[32].x=1280;
    clip[32].y=1440; 
    clip[32].w=640;  
    clip[32].h=480; 
    
    clip[33].x=1920;
    clip[33].y=1440; 
    clip[33].w=640;  
    clip[33].h=480; 
    
    clip[34].x=2560;
    clip[34].y=1440; 
    clip[34].w=640;  
    clip[34].h=480; 
    
    clip[35].x=3200;
    clip[35].y=1440; 
    clip[35].w=640;  
    clip[35].h=480; 
    
    clip[36].x=3840;
    clip[36].y=1440; 
    clip[36].w=640;  
    clip[36].h=480; 
    
    clip[37].x=4480;
    clip[37].y=1440; 
    clip[37].w=640;  
    clip[37].h=480; 
    
    clip[38].x=5120;
    clip[38].y=1440; 
    clip[38].w=640;  
    clip[38].h=480; 
    
    clip[39].x=5760;
    clip[39].y=1440; 
    clip[39].w=640;  
    clip[39].h=480; // end row 4
    
    clip[40].x=0;
    clip[40].y=1920; 
    clip[40].w=640;  
    clip[40].h=480; 
    
    clip[41].x=640;
    clip[41].y=1920; 
    clip[41].w=640;  
    clip[41].h=480; 
    
    clip[42].x=1280;
    clip[42].y=1920; 
    clip[42].w=640;  
    clip[42].h=480; 
    
    clip[43].x=1920;
    clip[43].y=1920; 
    clip[43].w=640;  
    clip[43].h=480; 
    
    clip[44].x=2560;
    clip[44].y=1920; 
    clip[44].w=640;  
    clip[44].h=480; 
    
    clip[45].x=3200;
    clip[45].y=1920; 
    clip[45].w=640;  
    clip[45].h=480; 
    
    clip[46].x=3840;
    clip[46].y=1920; 
    clip[46].w=640;  
    clip[46].h=480; 
    
    clip[47].x=4480;
    clip[47].y=1920; 
    clip[47].w=640;  
    clip[47].h=480; 
    
    clip[48].x=5120;
    clip[48].y=1920; 
    clip[48].w=640;  
    clip[48].h=480; 
    
    clip[49].x=5760;
    clip[49].y=1920; 
    clip[49].w=640;  
    clip[49].h=480; // end row 5
    
    }
    
    
    int main( int argc, char** argv )
    {
    	SDL_Init(SDL_INIT_EVERYTHING);
    	Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096);
    	Mix_Music* music , *music2 , *music5 , *music6 , *music7 , *music8 ,  *music9 ,  *music10, *music11, *music12 , *music13, *music14 , *music15, *music16 ; 
    	music=Mix_LoadMUS("a/tank_buster.wav");
            music2=Mix_LoadMUS("b/tank_buster.wav");
    	music5=Mix_LoadMUS("a/airship_buster.wav");
    	music6=Mix_LoadMUS("b/bunker_buster.wav");
    	music7=Mix_LoadMUS("a/bunker_buster_2.wav");
    	music8=Mix_LoadMUS("b/airship_buster_2.wav");
    	music9=Mix_LoadMUS("a/bunker_buster.wav");
    	music10=Mix_LoadMUS("b/bunker_buster_2.wav");
    	music11=Mix_LoadMUS("a/tank_buster_2.wav");
    	music12=Mix_LoadMUS("b/tank_buster_2.wav");
    	music13=Mix_LoadMUS("a/airship_buster_2.wav");
    	music14=Mix_LoadMUS("b/airship_buster.wav");
            music15=Mix_LoadMUS("b/knock_down.wav");
            music16=Mix_LoadMUS("a/knock_down.wav");
    	
    	SDL_Surface* screen,*image, *image2, *image3 , *image4 ,*image5,*image6 ,*image7 ,  *image8, *image9,*image10,*image11, *image12, *image13, *image14;          
    	screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
    	bool a_knock_down_animation = false;
    	bool b_knock_down_animation = false;
    	bool running=true;
    	const int FPS=30;
                        Uint32 start;
                        SDL_Rect rect;
                        int frame=0;
                        Uint32 color=SDL_MapRGB(screen->format,0,0,0);
    
            image=SDL_DisplayFormat(SDL_LoadBMP("A/tank_buster.BMP"));
    	image2=SDL_DisplayFormat(SDL_LoadBMP("B/tank_buster.BMP"));
    	image3=SDL_DisplayFormat(SDL_LoadBMP("A/knock_down.bmp"));
    	image4=SDL_DisplayFormat(SDL_LoadBMP("B/knock_down.bmp"));
    	image5=SDL_DisplayFormat(SDL_LoadBMP("A/airship_buster.bmp"));
    	image6=SDL_DisplayFormat(SDL_LoadBMP("B/bunker_buster.bmp"));
    	image7=SDL_DisplayFormat(SDL_LoadBMP("A/bunker_buster_2.bmp"));
    	image8=SDL_DisplayFormat(SDL_LoadBMP("B/airship_buster_2.bmp"));
    	image9=SDL_DisplayFormat(SDL_LoadBMP("A/bunker_buster.bmp"));
    	image10=SDL_DisplayFormat(SDL_LoadBMP("B/bunker_buster_2.bmp"));
    	image11=SDL_DisplayFormat(SDL_LoadBMP("A/tank_buster_2.BMP"));
    	image12=SDL_DisplayFormat(SDL_LoadBMP("B/tank_buster_2.bmp"));
    	image13=SDL_DisplayFormat(SDL_LoadBMP("A/airship_buster_2.BMP"));
    	image14=SDL_DisplayFormat(SDL_LoadBMP("B/airship_buster.bmp"));
    	
    	
    	
    	SDL_FillRect(screen,&rect,color);
    	SDL_SetColorKey(image,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
    	SDL_SetColorKey(image2,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
    	SDL_SetColorKey(image3,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
    	SDL_SetColorKey(image4,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
    	SDL_SetColorKey(image5,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
    	SDL_SetColorKey(image6,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
    	SDL_SetColorKey(image7,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
    	SDL_SetColorKey(image8,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
    	SDL_SetColorKey(image9,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
    	SDL_SetColorKey(image10,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
    	SDL_SetColorKey(image11,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
    	SDL_SetColorKey(image12,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
    	SDL_SetColorKey(image13,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
    	SDL_SetColorKey(image14,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
    	
    	SDL_Rect rects[31];
    	setrects(rects);
    	int a_pax_health =6;
            int a_sam_health= 28;
            int a_baz_health= 33;
              
    
            int b_pax_health= 5;
            int b_sam_health= 10;
            int b_baz_health= 15;
            
    
                        
                        
    	
    	int i;
    	int random;
    	srand((unsigned)time(0));
    
    	for( i=0; i<5 ; i++)
    	{
    		random = (rand() % 4) +1 ;
    	}
    
    
    
    while(running)
            	{
    
            		SDL_WM_SetCaption("Domination!!",NULL);
            		start=SDL_GetTicks();
            		SDL_Event event;
            		while(SDL_PollEvent(&event))
            		{
    
            			switch(event.type)
            			{
            			case SDL_QUIT:
            			running=false;
            				break;
            			}
    
    				
            		 }
    
    
     
    
    switch(random)
    {
    
    	
    
    case 1:
    
    	     if(b_pax_health<=0 && b_sam_health<=0 && b_baz_health<= 0)
    	 {
    		 SDL_BlitSurface(image4,&rects[frame],screen,&rect) ;
                                b_knock_down_animation=true;
    	 }
    	 else if( b_baz_health > 0 && b_pax_health <= 0 && b_sam_health <= 0){
    		 SDL_BlitSurface(image6,&rects[frame],screen,&rect) ;  // b_bunker_buster_1
    		  
    	 }
    
         
    	 else if( b_sam_health > 0 && b_pax_health <= 0){
    		 SDL_BlitSurface(image14,&rects[frame],screen,&rect) ;  // b_airship_buster_1
    		 
    	 }
    	 
    	 else if( b_pax_health > 0 ){
                    SDL_BlitSurface(image2,&rects[frame],screen,&rect) ;  //  b_tank_buster_1
    	 
    	 }
    	 
    	 
      
    
    break;
    
    
    
    case 2:
    
    	     if(a_pax_health <=0 && a_sam_health <=0 && a_baz_health <= 0)
    	 {
    		 SDL_BlitSurface(image3,&rects[frame],screen,&rect) ;
                                a_knock_down_animation=true;
    							 
    	 }
             else if( a_baz_health > 0 && a_pax_health <=0  &&  a_sam_health <= 0){
    		 SDL_BlitSurface(image9,&rects[frame],screen,&rect) ;  // a_bunker_buster_1
    		  
    	 }
    		 
    	 else if( a_sam_health > 0 && a_pax_health <=0  ){
    		 SDL_BlitSurface(image5,&rects[frame],screen,&rect) ;  // a_airship_buster_1
    		 
    	 }
    	 else if( a_pax_health > 0 ){
                    SDL_BlitSurface(image,&rects[frame],screen,&rect) ;  //  a_tank_buster_1
    	 
    	 }
    	 
    	 
    	
    
    break;
    
    
    
    case 3:
    
    	   if(b_pax_health<=0 && b_sam_health<=0 && b_baz_health <= 0)
    	 {
    		 SDL_BlitSurface(image4,&rects[frame],screen,&rect) ;
                                b_knock_down_animation=true;
    	 }
    
    	 else if( b_baz_health > 0  &&  b_sam_health <= 0 && b_pax_health<=0 ){
    		 SDL_BlitSurface(image10,&rects[frame],screen,&rect) ;  // b_bunker_buster_2
    		  
    	 }
    	 else if( b_sam_health > 0 && b_pax_health <= 0 ){
    		 SDL_BlitSurface(image8,&rects[frame],screen,&rect) ;  // b_airship_buster_2
    		 
    	 }
    	 else if( b_pax_health > 0 ){
                    SDL_BlitSurface(image12,&rects[frame],screen,&rect) ;  //  b_tank_buster_2
    	
    	 }
    
      
    
    break;
    
    
    
    case 4:
    
    
    	
    	 
    
    	     if(a_pax_health<=0 && a_sam_health<=0 && a_baz_health<= 0)
    	 {
    		 SDL_BlitSurface(image3,&rects[frame],screen,&rect) ;   // knock_down
                                a_knock_down_animation=true;
    							
    
    	 }
    	 else if( a_baz_health > 0 && a_sam_health <= 0  &&  a_pax_health <= 0){
    		 SDL_BlitSurface(image7,&rects[frame],screen,&rect) ;  // a_bunker_buster_2
    		  
    	 }
    	
    	 else if( a_sam_health > 0 &&  a_pax_health <= 0 ){
    		 SDL_BlitSurface(image13,&rects[frame],screen,&rect) ;  // a_airship_buster_2
    		  
    	 }
    	 
    	 else if( a_pax_health > 0 ){
                    SDL_BlitSurface(image11,&rects[frame],screen,&rect) ;  //  a_tank_buster_2
    	
    	 }
    	
    break;
    
    
    
    
    
    }     // end switch
    
    
    
      SDL_Flip(screen);
      frame++;
      if(frame==50)
    {
      frame=0;
    
      if (a_knock_down_animation  ||  b_knock_down_animation) 
          running = false;
    	           
          random= rand() % 4 +1;
          Mix_HaltMusic();  
          if(random==1)
    {
          
       if( b_pax_health<=0 && b_sam_health<0 && b_baz_health <= 0)
          {
    	Mix_PlayMusic(music15,0);
    
          }
    
    	else if( b_baz_health > 0 && b_pax_health <= 0  &&  b_sam_health <= 0){      // b_baz_buster_1
    		 Mix_PlayMusic(music6,0);
    		 b_baz_health = b_baz_health - 1;
    		// b_baz_health = b_baz_health + 1;    // bunker
    		}
                                                                                     
    	else if( b_sam_health > 0 && b_pax_health <= 0 {        // b_airship_buster_1
    		Mix_PlayMusic(music14,0);
    		b_sam_health = b_sam_health - 1;
    	        }
    
    	else if(  b_pax_health > 0){
                                                                                
            	Mix_PlayMusic(music2,0);                                 // b/tank_buster_1
    		b_pax_health = b_pax_health - 1;
    			                                                                                 
                    }
    																				 
    																				 
    		}
    
    
    
    		if(random==2)
    		{
            			                                                               
                       if(a_pax_health<=0 && a_sam_health<=0 && a_baz_health <= 0){
    			Mix_PlayMusic(music16,0);
    
    			}
    		else if( a_baz_health > 0 && a_pax_health <= 0  &&  a_sam_health <= 0){      // a_baz_buster_1
    			Mix_PlayMusic(music9,0);
    			a_baz_health = a_baz_health - 1;          //  +6 power_bunker , -0 UNQ (block),  PWRbaz_UNQ +6 and blocks (unbeatable), -6 normal
    			}
    		else if(a_sam_health > 0 && a_pax_health <= 0 ){            // a_airship_buster_1
    			Mix_PlayMusic(music5,0);
    			a_sam_health = a_sam_health - 1;
    	                 }
    		else if(  a_pax_health > 0){                             
            		Mix_PlayMusic(music,0);                                      // a/tank_buster_1
    			a_pax_health = a_pax_health - 1;
    			                                                                                 
                            }
    																				 
    																				
    			} 
    
    
    
    		if(random==3)
    		{
                      if(b_pax_health<=0 && b_sam_health<=0 && b_baz_health <= 0){
    			Mix_PlayMusic(music15,0);
    
    			}
    																				 
    		else if(b_baz_health > 0 && b_pax_health <= 0  &&  b_sam_health <= 0){       // b_baz_buster_2
    			Mix_PlayMusic(music10,0);
    			b_baz_health = b_baz_health - 1;  //normal
    	       //       b_baz_health = b_baz_health - 0;  //  UNQ
    			}
    																				 
    		else if( b_sam_health > 0 && b_pax_health <= 0 ){                  // b_airship_buster_2
    			Mix_PlayMusic(music8,0);														   
    	      //        b_sam_health = b_sam_health - 0;   // UNQ
    			b_sam_health = b_sam_health - 1;   // normal
    																			
    	                 }
    		else if(  b_pax_health > 0){
                                                                                
            		Mix_PlayMusic(music12,0);                                           // b/tank_buster_2
    			b_pax_health = b_pax_health - 1;    // normal
    	     //	        b_pax_health = b_pax_health - 0;    // UNQ
    			                                                                                 
                            }
    
    			}
    
    
    			if(random==4)
    			{
    			   if(a_pax_health<=0 && a_sam_health<=0 && a_baz_health <= 0){
    				 Mix_PlayMusic(music16,0);
    
    				}
    				else if( a_baz_health > 0 && a_pax_health <= 0  &&  a_sam_health <= 0){      // a_baz_buster_2
    					Mix_PlayMusic(music7,0);
    					a_baz_health = a_baz_health - 1; // normal
    			     //	        a_baz_health = a_baz_health - 0; // UNQ  , equals a block
    					}
    
            			else if( a_sam_health > 0 && a_pax_health <= 0 ){                  // a_airship_buster_2
    					Mix_PlayMusic(music13,0);
    					a_sam_health = a_sam_health - 1; // normal
    			     //	        a_sam_health = a_sam_health - 0; // UNQ
    	                                }
    
    				else if(  a_pax_health > 0){
                                                                                
            				 Mix_PlayMusic(music11,0);                                          // a/tank_buster_2
    					 a_pax_health = a_pax_health - 1; // normal
    			    //	         a_pax_health = a_pax_health - 0; // UNQ
    			                                                                                 
                                             }
    																				 
    					 }
                                              													
    //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    																			 
    
                                                                                                            
                                                                                                                   
    																			 
            }
    
    	  
    
    	if(1000/FPS>SDL_GetTicks()-start )
        SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
    
    	}   
    
    
    	Mix_FreeMusic(music);
            Mix_FreeMusic(music2);
    	Mix_FreeMusic(music5);
    	Mix_FreeMusic(music6);
    	Mix_FreeMusic(music7);
    	Mix_FreeMusic(music8);
    	Mix_FreeMusic(music9);
    	Mix_FreeMusic(music10);
    	Mix_FreeMusic(music11);
    	Mix_FreeMusic(music12);
    	Mix_FreeMusic(music13);
    	Mix_FreeMusic(music14);
    	Mix_FreeMusic(music15);
    	Mix_FreeMusic(music16);
    	Mix_CloseAudio();
    
    
    
    	SDL_FreeSurface(image);
    	SDL_FreeSurface(image2);
    	SDL_FreeSurface(image3);
    	SDL_FreeSurface(image4);
    	SDL_FreeSurface(image5);
    	SDL_FreeSurface(image6);
    	SDL_FreeSurface(image7);
    	SDL_FreeSurface(image8);
    	SDL_FreeSurface(image9);
    	SDL_FreeSurface(image10);
    	SDL_FreeSurface(image11);
    	SDL_FreeSurface(image12);
    	SDL_FreeSurface(image13);
    	SDL_FreeSurface(image14);
    	
    	system("PAUSE");
    	SDL_Quit();
        return 0;
    
    
    }

  2. #2
    Join Date
    May 2005
    Posts
    102
    ...found the issue. It was
    SDL_Rect rects[30];
    was still set to 30. I had to change it to...
    SDL_Rect rects[50];

  3. #3
    Join Date
    Mar 2005
    Posts
    934
    One way to be more efficient is to use loops to cut back on repeating yourself in the code. For example, your clips are assigned to regularly-spaced coordinates, so you could do something like this:

    Code:
    /* Note: This is C code. */
    
    int x, y, clipNumber;
    
    clipNumber = 0;
    
    for(y = 0; y < 5; y++) {
    
     for(x = 0; x < 10; x++) {
    
      clip[clipNumber].x = x * 640;
      clip[clipNumber].y = y * 480;
      clip[clipNumber].w = 640;
      clip[clipNumber].y = 480;
    
      clipNumber++;
    
     }
    
    }
    You might also want to use the loop values to mathematically refer to your array indices with something like:

    Code:
    /* Note: This is C code. */
    
    int x, y, columns, rows, clipNumber;
    
    columns = 10;
    rows = 5;
    
    for(y = 0; y < rows; y++) {
    
     for(x = 0; x < columns; x++) {
    
      clipNumber = (y * columns) + x;
    
      clip[clipNumber].x = x * 640;
      clip[clipNumber].y = y * 480;
      clip[clipNumber].w = 640;
      clip[clipNumber].y = 480;
    
     }
    
    }
    "I don't WANT to pet the chicken."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •