Skipping functions?

Started by
12 comments, last by yoni0505 13 years, 4 months ago
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 inputvoid 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.
Advertisement
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.
Are you trying to debug a release build maybe?
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.

[size=1]Visit my website, rawrrawr.com

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 tovoid 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 positionvoid 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?
Quote:Original post by yoni0505
It worked before I added and changed few things which doesn't effect those functions, could it be somehow related?


Only thing I can think of is an incremental linker error.

Try rebuilding (debug).

Engineering Manager at Deloitte Australia

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 positionvoid movePlayer(int destX, int destY) {	player.x = destX;	player.y = destY;}//check if a tile is free for the player to move tovoid 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 inputvoid 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 screenvoid 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 screenvoid 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 tilevoid 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 screenvoid 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 tilevoid 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 mapvoid displayMap(int map[15][20]) {	for(int y=0; y < 15; y++) {		for(int x=0; x < 20; x++) {			displayTile(x,y);		}	}}//update the screenvoid 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]
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.
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.

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!

This topic is closed to new replies.

Advertisement