Jump to content
  • Advertisement
Sign in to follow this  
Ariste

Header file problem

This topic is 4849 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone. I'm having a lot of trouble with a header file I made. It's basically just a bunch of classes with all their variables, functions, definitions etc. The problem is that if I #include it, it makes the program hang, and if I try to declare an object from it I get one of those "LevEditor.exe has encountered a problem and needs to close. We are sorry for the inconvenience." I really have no idea what the problem with the header file is. Here is the source for the header, if it helps at all:
#include <SDL/SDL.h>

const int SCREENWIDTH  = 1024;
const int SCREENHEIGHT = 780;
const int SCREENBPP    = 32;



class Bar
{
      public:
             SDL_Surface* surface;
      
             SDL_Rect barRect;
             
             int hitsToDestroy;
      
             double barMiddleX;
      
             bool isDestroyed;
      
};

class Paddle
{
      
      public:
             SDL_Surface* surface;
      
             SDL_Rect paddleRect;
      
             double paddleMiddleX;
      
             Paddle();
};

Paddle::Paddle()
{
      surface = SDL_LoadBMP("paddle.bmp");
      paddleRect.x = SCREENWIDTH / 2;
      paddleRect.y = SCREENHEIGHT + 130;
      paddleRect.w = surface->w;
      paddleRect.h = surface->h;

      double paddleMiddleX = paddleRect.x + (.5 * paddleRect.w);
}



class Ball
{
      public:
             SDL_Surface* surface;
             
             
             SDL_Rect ballRect;
             
             double xVel;
             double yVel;
             double ballMiddleX;
             
             bool CheckCollision(Bar bar);
             bool CheckCollision(Paddle paddle);
             void collide(Bar bar);
             void collide(Paddle paddle);
             Ball();
};

Ball::Ball()
{
   surface = SDL_LoadBMP("ball.bmp");
   
   ballRect.x = SCREENWIDTH / 2;
   ballRect.y = SCREENHEIGHT / 2;
   ballRect.w = surface->w;
   ballRect.h = surface->h;
             
   ballMiddleX = ballRect.x + (ballRect.w/2);
   
   xVel = 2;
   yVel = 2;
}
             
            

bool Ball::CheckCollision(Bar bar)
{          
     /*Check if ball's Y axis is farther up than bar's Y axis and if ball's sides lie within bar's sides and if bar is destroyed*/ 
     if             ((ballRect.y < (bar.barRect.y + bar.barRect.h)) && (ballRect.x >= bar.barRect.x) && 
                         ((ballRect.x + ballRect.w) <= (bar.barRect.x + bar.barRect.w))
                                  && (bar.isDestroyed == false) && (yVel < 0)) 
                                                   return true;
     else return false;
}

bool Ball::CheckCollision(Paddle paddle)
{
     /*Check if ball's Y axis is farther down than paddle's Y axis and if ball's sides lie within paddle's sides*/
     if                   ((ballRect.y > paddle.paddleRect.y) && (ballRect.x >= paddle.paddleRect.x) && ((ballRect.x + ballRect.w) 
                                       <= (paddle.paddleRect.x + paddle.paddleRect.w)) && (yVel > 0))
                                                          return true;
     else return false;
}
     


void Ball::collide(Bar bar)
{
     /*Check which side of the bar the ball hit on and collide appropriately*/
     if ((ballMiddleX <= bar.barMiddleX) && xVel > 0) {yVel = -yVel; xVel = .5 * xVel;}
     if ((ballMiddleX <= bar.barMiddleX) && xVel < 0) {yVel = -yVel; xVel = 1.5 * xVel;}
     if ((ballMiddleX >  bar.barMiddleX) && xVel > 0) {yVel = -yVel; xVel = 1.5 * xVel;}
     if ((ballMiddleX >  bar.barMiddleX) && xVel < 0) {yVel = -yVel; xVel = .5 * xVel;}
     
} 

void Ball::collide(Paddle paddle)
{
     if ((ballMiddleX <= paddle.paddleMiddleX) && xVel > 0) {yVel = -yVel; xVel = .5 * xVel;}
     if ((ballMiddleX <= paddle.paddleMiddleX) && xVel < 0) {yVel = -yVel; xVel = 1.5 * xVel;}
     if ((ballMiddleX >  paddle.paddleMiddleX) && xVel < 0) {yVel = -yVel; xVel = 1.5 * xVel;}
     if ((ballMiddleX >  paddle.paddleMiddleX) && xVel > 0) {yVel = -yVel; xVel = .5 * xVel;}
}
                                                        
       
       
       
class Level
{
      public:
             Bar layout[10][4];
             SDL_Surface* background;
             Paddle paddle;
             Ball ball;
             
};

Like I said, it's just a bunch of classes and definitions, but when I #include it it causes the program to hang. I have no idea what's causing the problem. If anyone can help out, I'd really appreciate it. Thanks,

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure why you'd get those errors just by including the file, but one thing you might want to do is add inclusion guards.


#ifndef YOUR_FILE_H
#define YOUR_FILE_H

// your code goes here

#endif



But doing this will probably only fix linker errors, not runtime errors.
When you include this file, are you also calling code from one of these classes? If so, you might want to debug your code and step through it to find which line is causing the errors.

Share this post


Link to post
Share on other sites
If I do include code (declare a Level object, for instance), I get one of those messages saying that the program must close. Even if I don't include code from it, though, the program still hangs and won't run correctly. It really doesn't make sense. There's no linker problems or anything, the program compiles fine. It's runtime errors that have something to do with the header file, and I have no idea what's causing it.

Share this post


Link to post
Share on other sites
Run it through a debugger and tell us what line it hangs at.

EDIT: It's a "shot in the dark", but maybe the problem is with SDL_Surface* surface? After you load the .bmp file assert() that the pointer is valid. Also make sure that file is in the right place.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!