Jump to content
  • Advertisement
Sign in to follow this  
game mercenary

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

This topic is 4776 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

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].surface = SDL_LoadBMP("images/backtile.bmp");
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
you.surface = SDL_LoadBMP("images/you.bmp");

//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 this post


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

Also, make sure your images are loading by checking the return of SDL_LoadBMP.

Cheers,
--Brian

Share this post


Link to post
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 this post


Link to post
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 up
for(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 this post


Link to post
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 this post


Link to post
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 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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!