Sign in to follow this  
yoni0505

Skipping functions?

Recommended Posts

Hi.

I'm having a weird problem, I'm building a simple game to gain some skill and one of my functions is sort of ignored.

I'm using MSVC++ 2010.

It used to work before I added and modified some unrelated functions in the code.
I tried to put a breakpoint at the functions line, and when I debug I get a empty red breakpoint with a "!" sign that says:
"The breakpoint will not currently be hit. No executable code is associated with this line. Possible causes include: preprocessor directives or compiler/linker optimizations."

I checked the line that calls the function using a breakpoint and it does stop.

the function that doesn't work:
(global scope)

//check for specific input
void checkInput(SDL_KeyboardEvent *key) {
switch(key->keysym.sym) {
case SDLK_UP:
isTileFree(0,-1);
break;
case SDLK_DOWN:
isTileFree(0,1);
break;
case SDLK_LEFT:
isTileFree(-1,0);
break;
case SDLK_RIGHT:
isTileFree(1,0);
break;
}
}



and the line that called the function is placed at "main" :

//main loop
while(running) {
//check input
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
checkInput(&event.key);
break;
case SDL_QUIT:
running = 0;
break;
}
}
//update the screen
updateDisplay();

//dalay program
SDL_Delay(5);
}



I'm really confused cause it used to work before.

Share this post


Link to post
Share on other sites
What does isTileFree do? Does it just return true or false with no side effects? If that is the case then your checkInput function does absolutely nothing and the compiler will likely detect this and remove it.

Are you compiling in Debug or Release mode? Also try posting isTileFree so we can see what it does.

Share this post


Link to post
Share on other sites
If your compiling in release mode; and if isTileFree(...) does not affect any global state; it is valid for the compiler to analyze checkInput, see that it has no effect on the output of the program; and optimize it away.

Solution? Make checkInput actually do something, or compile in debug mode with optimizations disabled.

Edit: Wow, double ninja'ed.

Share this post


Link to post
Share on other sites
I tried both release and debug modes, no difference.
The function "checkInput" leads to the function "isTileFree" that leads to the function "movePlayer" that modify a global variable.


//check if a tile is free for the player to move to
void isTileFree(int x, int y) {
int posX = player.x+x;
int posY = player.y+y;
int tile = level.map[posY][posX];
switch(tile) {
case 0:
movePlayer(posX,posY);
break;
}
return;
}




//set the player's position
void movePlayer(int destX, int destY) {
player.x = destX;
player.y = destY;
}



It worked before I added and changed few things which doesn't effect those functions, could it be somehow related?

Share this post


Link to post
Share on other sites
It didn't change anything.

Perhaps someone more experienced then me can try to find the problem?

Here is the full code:
*Have to link "SDL.lib" and "SDLmain.lib" (need SDL).
*Have to place "SDL.dll" at the .EXE's folder.
*Have to place the images for the "graphics" at the poject's folder (pojectname\projectname).


code:

#include "SDL.h"
#include <cstdlib>

SDL_Surface *screen;

class cPlayer{
public:
int x,y;
SDL_Surface *image;
SDL_Rect src, dest;

void setImage(char* file_name) {
player.image = SDL_LoadBMP(file_name);
player.image = SDL_DisplayFormat(player.image);

player.src.x = 0;
player.src.y = 0;
player.src.w = player.image->w;
player.src.h = player.image->h;

player.dest.x = x*32;
player.dest.y = y*32;
player.dest.w = player.image->w;
player.dest.h = player.image->h;
}
}player;

class cTile{
public:
SDL_Surface *image;
SDL_Rect src, dest;
};

class cLevel{
public:
int map[15][20];
cTile tile[15][20];

void setMap(int map[15][20]) {
for(int y=0; y < 15; y++) {
for(int x=0; x < 20; x++) {
level.map[y][x] = map[y][x];
}
}
}
}level;

//set the player's position
void movePlayer(int destX, int destY) {
player.x = destX;
player.y = destY;
}

//check if a tile is free for the player to move to
void isTileFree(int x, int y) {
int posX = player.x+x;
int posY = player.y+y;
int tile = level.map[posY][posX];
switch(tile) {
case 0:
movePlayer(posX,posY);
break;
}
return;
}

//check for specific input
void checkInput(SDL_KeyboardEvent *key) {
switch(key->keysym.sym) {
case SDLK_UP:
isTileFree(0,-1);
break;
case SDLK_DOWN:
isTileFree(0,1);
break;
case SDLK_LEFT:
isTileFree(-1,0);
break;
case SDLK_RIGHT:
isTileFree(1,0);
break;
}
}

//load a .BMP file to the screen
void loadBMP(char* file_name, int x, int y) {
level.tile[y][x].image = SDL_LoadBMP(file_name);
if(level.tile[y][x].image == NULL) {
return;
}

level.tile[y][x].image = SDL_DisplayFormat(level.tile[y][x].image);

level.tile[y][x].src.x = 0;
level.tile[y][x].src.y = 0;
level.tile[y][x].src.w = level.tile[y][x].image->w;
level.tile[y][x].src.h = level.tile[y][x].image->h;

level.tile[y][x].dest.x = x*32;
level.tile[y][x].dest.y = y*32;
level.tile[y][x].dest.w = level.tile[y][x].image->w;
level.tile[y][x].dest.h = level.tile[y][x].image->h;
}

//display a BMP image on the screen
void displayBMP(SDL_Surface *image,SDL_Rect src, SDL_Rect dest, int x, int y) {
SDL_BlitSurface(image, &src, screen, &dest);
}

//check which image to draw for a tile
void loadTile(int tile,int x, int y) {
switch(tile) {
case 0:
loadBMP("tile0.bmp",x,y);
break;
case 1:
loadBMP("tile1.bmp",x,y);
break;
case 2:
loadBMP("tile2.bmp",x,y);
break;
}
}

//load a map to the screen
void loadMap(int map[15][20]) {
for(int y=0; y < 15; y++) {
for(int x=0; x < 20; x++) {
loadTile(map[y][x],x,y);
}
}
}

//display a tile
void displayTile(int x, int y) {
displayBMP(level.tile[y][x].image,level.tile[y][x].src,level.tile[y][x].dest,x,y);
}

//display a map
void displayMap(int map[15][20]) {
for(int y=0; y < 15; y++) {
for(int x=0; x < 20; x++) {
displayTile(x,y);
}
}
}

//update the screen
void updateDisplay() {
displayMap(level.map);
displayBMP(player.image,player.src,player.dest, player.x * 32, player.y * 32);
SDL_Flip(screen);
}

int main(int argc, char *argv[]) {
//initialize SDL
if(SDL_Init(SDL_INIT_VIDEO) != 0) {
return 1;
}
atexit(SDL_Quit);

//set video mode
screen = SDL_SetVideoMode(640,480,16, SDL_DOUBLEBUF);
if(screen == NULL) {
return 1;
}
//set stuff
SDL_WM_SetCaption("Tile Map", NULL);

SDL_Event event;

int running = 1;

player.x = 2;
player.y = 7;
player.setImage("player.bmp");

int map1[15][20] = {
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,0,2,0,2,0,2,2,2,0,2,0,0,0,2,0,0,0,0,1,
1,0,2,0,2,0,2,0,0,0,2,0,0,0,2,0,0,0,0,1,
1,0,2,2,2,0,2,2,2,0,2,0,0,0,2,0,0,0,0,1,
1,0,2,0,2,0,2,0,0,0,2,0,0,0,2,0,0,0,0,1,
1,0,2,0,2,0,2,2,2,0,2,2,2,0,2,2,2,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,2,0,2,0,2,2,2,0,2,2,2,0,2,0,2,0,0,1,
1,0,2,0,2,0,2,0,0,0,2,0,2,0,2,0,2,0,0,1,
1,0,0,2,0,0,2,2,2,0,2,2,2,0,2,2,2,0,0,1,
1,0,0,2,0,0,2,0,0,0,2,0,2,0,2,0,2,0,0,1,
1,0,0,2,0,0,2,2,2,0,2,0,2,0,2,0,2,0,0,1,
1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};

level.setMap(map1);

loadMap(level.map);

//main loop
while(running) {
//check input
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
checkInput(&event.key);
break;
case SDL_QUIT:
running = 0;
break;
}
}
//update the screen
updateDisplay();

//dalay program
SDL_Delay(5);
}
return 0;
}




images:
http://www.mediafire.com/?wd5bjdsmka0rxhd

This is some unique error [depressed]

Share this post


Link to post
Share on other sites
I've ran into errors like this many times, and in my experience, it has to do with outdated files. clean, and then rebuild, and if that doesn't work, try reloading the IDE and then the above.

Share this post


Link to post
Share on other sites
Quote:

I tried to put a breakpoint at the functions line


Maybe you have done this, but just to be sure:
Where exactly did you put the breakpoint? Did you try several lines in your function? Maybe VS is right and there is no executable code associated with that line. Normally VS tries to shift the breakpoint to the next statement in these cases, but this may not be possible here because of the switch-statement.

If the obvious things can be excluded (i.e. debug symbols are loaded and the code matches the debugged program), I would try the following:

1. break at the line where the function is called (you said that worked) and try to step _into_ the function (F11).

2. Put an empty statement in your function (just a semicolon) and try to break there.

Share this post


Link to post
Share on other sites
I tried to change the "checkInput" function's call location and it works outside the "while(SDL_PollEvent(&event))" loop, at the "while(running)" loop it works!

What can cause such a thing?! Thing is really weird!

Share this post


Link to post
Share on other sites
Wouldn't it be best to just run in debug (with debug release) without any break points. When the program stops because it found a problem, there should be a list of steps before the cause you should also be able to see if any of the values from the variables make sense (in the debugger viewer thing).

Also take a look at those supposed unrelated functions again. This happened the moment you messed with them which makes it seem that they were maybe more important than you originally thought.

Another thing which I do is I'll sometimes go the extent of leaving console messages to make sure that functions and the like are being executed.

....
cout << "Pass 0" << endl;
...
...
...
cout << "Pass 1" << endl;

etc

It's a longer step to deal with but it always helped me isolate the problem by finding out that a ptr was NULL or the data was passed etc but for me it's an extreme case and mostly happens when my projects get somewhat big.

Share this post


Link to post
Share on other sites
Actually I compiled your code and had no problems with it working. It's a message using your block images. I think the re-build thing is what needs to happen.

Since it seems you tried that and it didn't work, start another project and copy paste the files over and re-setup the libs.

also you need the SDLmain.lib file too.

Also your character won't move without the added lines in bold:

//set the player's position
void movePlayer(int destX, int destY) {
player.x = destX;
player.y = destY;
player.dest.x = destX*32;
player.dest.y = destY*32;

}

[Edited by - Asem on December 14, 2010 2:02:09 AM]

Share this post


Link to post
Share on other sites
I've had these issues many times before - and they are infuriating.

My usual solution is to Clean/Rebuild/Reload IDE - and then when you're debugging make sure you only press F5 - not Ctrl+F5 (which is a bad habit I have from VB6) as this appears to detach the Debugger.

Share this post


Link to post
Share on other sites
Quote:
Original post by Asem
Also take a look at those supposed unrelated functions again.


Quote:
Original post by Asem
Actually I compiled your code and had no problems with it working. It's a message using your block images. I think the re-build thing is what needs to happen.

Since it seems you tried that and it didn't work, start another project and copy paste the files over and re-setup the libs.

also you need the SDLmain.lib file too.

Also your character won't move without the added lines in bold:

//set the player's position
void movePlayer(int destX, int destY) {
player.x = destX;
player.y = destY;
player.dest.x = destX*32;
player.dest.y = destY*32;

}


Thank you guys, I didn't notice it!

[Edited by - yoni0505 on December 14, 2010 9:12:54 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this