why does this program hang? (sdl,c++)

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

Recommended Posts

the window doesnt even come up, all I get is a console window then it crashed. main doesnt even do one command before it hangs. main.cpp
/*
this is the course of the includes
declarations >> init >> keyhandle >> draw >> main
that is also the order the game is going to go in
*/

#include "draw.h"

int main(int argc, char *argv[])
{
game main;
main.init();
while(main.done ==0)
{
main.gameloop();
}
return 0;
}

int game::gameloop(){
game main;
draw();
keyhandle();
SDL_Flip(main.screen);
}


draw.h
#include "keyhandle.h"

int drawIMG(SDL_Surface* image,int x,int y,int w,int h)
{
game game;
SDL_Rect dest;
dest.x = x;
dest.y = y;
dest.w = w;
dest.h = h;
SDL_BlitSurface(image,NULL,game.screen,&dest);
}

int game::draw()
{
game game;
drawIMG(game.you.surface,game.you.xpos,game.you.ypos,game.you.xsize,game.you.ysize);
}


keyhandle.h
#include "init.h"

int game::keyhandle()
{
SDL_Event event;

while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT )  {  done++;  }

if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done++; }
}
}
Uint8 *keys;
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) { you.yspeed = -10; }
else
you.yspeed =0;

if ( keys[SDLK_DOWN]) { you.yspeed = 10; }
else
you.yspeed=0;

if ( keys[SDLK_LEFT] ) { you.xspeed = -10; }
else
you.xspeed = 0;

if ( keys[SDLK_RIGHT] ) { you.xspeed = 10; }
else
you.xspeed = 0;

if ( keys[SDLK_ESCAPE] ) {done++;}

}


init.h
#include "declarations.h"

int game::init()
{
int loop1,loop2,xtilepos,ytilepos;
xtilepos = -10;
ytilepos = -10;

//set the screen up
for(loop1=0;loop1<81;loop1++)
{
for(loop2=0;loop2<61;loop2++)
{
back[loop1][loop2].xpos= xtilepos;
back[loop1][loop2].ypos= ytilepos;
back[loop1][loop2].order=0;
back[loop1][loop2].xsize=10;
back[loop1][loop2].ysize=10;
xtilepos+=10;
xtilepos=0;
ytilepos+=10;
}
}
//init you

//init the window
screen = SDL_SetVideoMode(800
,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
SDL_Init(SDL_INIT_VIDEO);
}


declarations.h
#include <sdl.h>
#include <iostream>
using namespace std;
#include <stdio.h>

struct map_object
{
int xpos,ypos,xsize,ysize,order;
SDL_Surface *surface;
bool hit;
};

struct weapon
{
int xpos,ypos,xsize,ysize,order,damage,xspeed,yspeed;
SDL_Surface *surface;
bool hit, draw;
};

struct player
{
int xpos,ypos,xsize,ysize,order,damage,xspeed,yspeed;
SDL_Surface *surface;
weapon bullet;
bool hit,draw;
};

class game
{
public:
//function declaritons
int init(),keyhandle(),gameloop(),draw();
//game variables
SDL_Surface *screen;
map_object back[80][60];
player you;
int done;
};


I ran it in debug mode (dev-c++) and it said "an acess violation (segmentation fault) raised in your program"

Share on other sites
Question: Why do you load the same image 61*81 times?

Cheers,
--Brian

Share on other sites
1. I intend on using different images later
2. it cant get to that point so there is no point in doing that yet

Share on other sites
Found something that's definitely wrong (not sure if it's your bug):

Your loop goes while <81 or <61. Your array can only store 80 by 60 values (indexed 0-79 and 0-59). Thus, when your loop hits 80 and 60, you're accessing invalid memory.

Cheers,
--Brian

Share on other sites
Sorry, saw that right after I posted.
I don't believe it's hanging on the first line, but rather marking the first line in main as the row before the error. Going down through your code, I'd say your error is here(game::init()):
//set the screen upfor(loop1=0;loop1<81;loop1++){for(loop2=0;loop2<61;loop2++){

When you've got this definition of back:
map_object back[80][60];

This would give you an access violation since back contains objects 0 through 79, not 0 though 80. Try changing it and see if it works.

Share on other sites
fixed that, but the gui screen doesnt even come up, the init was never ran

EDIT: it still gives me the acess violation

Share on other sites
There's a good chance that your init *is* run, but your program crashes before the game screen shows up.

--Brian

Share on other sites
if I put cout << "start";
at the 1st line of main, it wont print to the file.

Share on other sites
Question, why on earth do you do this?

int game::draw(){   game game;   drawIMG(game.you.surface,game.you.xpos,game.you.ypos,game.you.xsize,game.you.ysize);}

I think that should be:

int game::draw(){   drawIMG(you.surface,you.xpos,you.ypos,you.xsize,you.ysize);}

And if you made it int, make sure your return something as well [wink]. See if that works, if not, we'll look some more. Oh and was said, you do not need to load the same image a total of 4941 times. I'm sure we can come up with a better alternative.

Share on other sites
lol, I have no idea.
it still hangs

EDIT: nvm, it works!

Share on other sites
thanks, you all get rated up for this