Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here is the code....
C++
#include<iostream>
#include<glut.h>
#include "Hero.h"
#include"RGBA.h"

AnimatedSprite *hero;
AnimatedSprite *bg,*fg;
int currentAnim;
float L=0,R=480;
void display()
{ 

   hero = new AnimatedSprite("alladin2.bmp",56,90,0,0,2001);
   hero -> colorKey(255,0,255);

   bg = new AnimatedSprite("BlueBkg2.bmp",760,480,L,R,2002);
   bg -> colorKey(255,255,255);

   glEnable(GL_ALPHA_TEST);
   glAlphaFunc(GL_EQUAL,GL_ONE);
   currentAnim=hero->currentAnimState;
   hero->drawSprite();

}
 void UpdateSpriteAnimation(void)
{
	int currentAnim=hero->currentAnimState;
	hero->anim[currentAnim].currentFrames = (hero->anim[currentAnim].currentFrames++);
	if(hero->currentAnimState == hero->ANIM_WALK_LEFT)
	{
		if(hero->x >= L && hero->x <= R-10)
			hero->x -= 1.0;
		bg->anim[0].u -= 0.005;
		fg->anim[0].u -= 0.002;
	}
	if(hero->currentAnimState == hero->ANIM_WALK_RIGHT)
	{
		if(hero->x < R)
			hero->x += 1.0;
		bg->anim[0].u -= 0.005;
		fg->anim[0].u -= 0.002;
	}
}
 void myTimer (int tt)
 {
	 UpdateSpriteAnimation();
	 glutPostRedisplay();
	 glutTimerFunc(hero->FRAME_DELAY_SPRITE,myTimer,0);
 }
 void main(int argc, char **argv)
{
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);
  glutInitWindowSize(640, 480);
  glutInitWindowPosition(10, 10);
  glutCreateWindow("Sprites Sheet");
  
  
  glutDisplayFunc(display);
  glutDisplayFunc(UpdateSpriteAnimation);
  



   // glutIdleFunc(display);
  glutTimerFunc(500,myTimer,1);
  glutMainLoop();
}





the program break at start of function definition of updatespriteanimation.
Posted
Updated 1-Sep-14 6:55am
v2

1 solution

more careful programming woul prevent such error by check pointers for valid values.

C++
AnimatedSprite *hero = 0;//use good style
....

//quick fix
 display();
 glutDisplayFunc(UpdateSpriteAnimation);


You better resign your code that display() only loads at startup once, and you use the GLUT-API right by drawing in the glutDisplayFunc() callback.
 
Share this answer
 
Comments
Member 10099183 1-Sep-14 13:54pm    
The breakpoint occured at
int currentAnim=hero->currentAnimState;
this line
KarstenK 1-Sep-14 14:06pm    
Really? :-O

I would have bet a sixpack beer that it would happen AT THIS LINE ;-)
Member 10099183 1-Sep-14 14:10pm    
how can it be resolved??
KarstenK 2-Sep-14 2:17am    
See my "Quick fix" in the solution.

your hero is a object pointer and if it isnt pointing to a valid object you CRASH.

Lesson 1: In professional code is an ASSERT( hero != 0); and/or if( hero == 0) return; to avoid such WORST CASE SZENARIOS.
Member 10099183 2-Sep-14 11:48am    
Still it is not resolved :( after quick fix the break point occured in the display function definition.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900