Jump to content
  • Advertisement
Sign in to follow this  
MPG

I need some help with some errors

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

#include "SDL.h"

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



bool k = true;
int main( int argc, char* argv[] )
{
 atexit(SDL_Quit);
SDL_Init ( SDL_INIT_VIDEO );
SDL_Surface* screen;
screen = SDL_SetVideoMode(1024, 768, 32, SDL_DOUBLEBUF| SDL_ANYFORMAT);
SDL_Surface* hero;
SDL_Surface* enemy;
SDL_Surface* helaser;
SDL_Surface* enlaser;
hero = SDL_LoadBMP("ship.bmp");
enemy = SDL_LoadBMP("enemyship.bmp");
helaser = SDL_LoadBMP("helaser.bmp");
enlaser = SDL_LoadBMP("enlaser.bmp");
SDL_Rect ship;
ship.x = 500;
ship.y = 579;
SDL_Event global;
while (k == true)
{
	
    
SDL_BlitSurface(hero,NULL,screen,&ship);
SDL_PollEvent(&global);
	

  SDL_PollEvent(&global);
if ( global.type == SDL_QUIT )  
	  {  
		  k = false;
		  SDL_Quit();
	  }
        
		  
      if ( global.type == SDL_KEYDOWN )
      {
        if ( global.key.keysym.sym == SDLK_ESCAPE ) 
		{ 
			k = false; 
			SDL_Quit();
		}
	  }
		  }
      if ( global.type == SDL_KEYDOWN )
      {
        if ( global.key.keysym.sym == SDLK_UP ) 
		{ 
			ship.x = ship.x + 1;
SDL_BlitSurface(hero,NULL,screen,&ship);
        SDL_Flip(screen);
		}
      }
  }
if (global.type == SDL_KEYDOWN)
{
  if ( global.key.keysym.sym == SDLK_DOWN ) 
		{ 
			ship.y = ship.y -1;
SDL_BlitSurface(hero,NULL,screen,&ship);
        SDL_Flip(screen);
		}

  if ( global.key.keysym.sym == SDLK_RIGHT ) 
		{ 
			ship.x = ship.x - 1;
SDL_BlitSurface(hero,NULL,screen,&ship);
        SDL_Flip(screen);
		}

  if ( global.key.keysym.sym == SDLK_LEFT ) 
		{ 
			ship.x = ship.x + 1;
SDL_BlitSurface(hero,NULL,screen,&ship);
        SDL_Flip(screen);
		}
}
return (0);
}
   

errors: error C2059: syntax error : 'if' error C2143: syntax error : missing ';' before '{' error C2447: '{' : missing function header (old-style formal list?) error C2059: syntax error : 'return' error C2143: syntax error : missing ';' before '}'

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Can you please be a little more specific? Your error is in lack of context.

Share this post


Link to post
Share on other sites
You have too many }'s in your code, at several places. Indent your code properly and you'll see it more easily.

Share this post


Link to post
Share on other sites
I think I found your problem (touchwood).

Look at your event handler loop.


if ( global.type == SDL_QUIT )

{

k = false;

SDL_Quit();

}





if ( global.type == SDL_KEYDOWN )

{

if ( global.key.keysym.sym == SDLK_ESCAPE )

{

k = false;

SDL_Quit();

}

}

}

if ( global.type == SDL_KEYDOWN )

{

if ( global.key.keysym.sym == SDLK_UP )

{

ship.x = ship.x + 1;

SDL_BlitSurface(hero,NULL,screen,&ship);

SDL_Flip(screen);

}

}

}

if (global.type == SDL_KEYDOWN)

{

if ( global.key.keysym.sym == SDLK_DOWN )

{

ship.y = ship.y -1;

SDL_BlitSurface(hero,NULL,screen,&ship);

SDL_Flip(screen);

}



if ( global.key.keysym.sym == SDLK_RIGHT )

{

ship.x = ship.x - 1;

SDL_BlitSurface(hero,NULL,screen,&ship);

SDL_Flip(screen);

}



if ( global.key.keysym.sym == SDLK_LEFT )

{

ship.x = ship.x + 1;

SDL_BlitSurface(hero,NULL,screen,&ship);

SDL_Flip(screen);

}




Instead, you could have:



if(event.type==SDL_KEYDOWN)
{

if(event.key.keysym.sym==SDLK_ESCAPE) // It's in with the other keypress options, that way it won't get called automatically by mistake
{
k=false;
SDL_Quit;
}

else if(event.key.keysym.sym==SDLK_DOWN)
{
// blah blah
}

else if(event.key.keysym.sym==SDLK_UP)
{
//Yet more code
}

// When you've finished all the keydown events you want to check for

} // Close the SDL_KEYDOWN if statement here






Personally I'd use a switch statement there, but you're using an if statement so I'll keep it that way for now. Notice how SDLK_ESCAPE is in that if statement and not on its own.

You see how I've put all the keypress events into the one keydown handler? That's bound to help solve your problem, it stops the event handler from getting confused and getting caught at the Quit statement, which I suspect was your problem before.

Another problem I can see is that you're not checking for keys being released. Do this by creating a separate if statement block within your event handler, and do the same as before. For example:



if(event.type==SDL_KEYUP) // A key has been released?
{

// Which key?

if(event.key.keysym.sym==SDLK_DOWN)
{
//Code
}

else if(event.key.keysym.sym==SDLK_UP)
{
//More code
}

} // Finish the if-statement block as before, once you've programmed in all your keypresses





What you could then do is make each key have a bool variable to determine if it's pressed or not. So let's say you had
bool left_pressed
. left_pressed is set to true if you press left, e.g.:


if(event.key.keysym.sym==SDLK_LEFT)
{
left_pressed=true;
}

// If it's the key up handler you would use...

left_pressed=false;



At the bottom of your event handler loop, you could have:



if(left_pressed)
{

// Change position variable accordingly

}



You'd also be better to have an organised, central gameloop instead of calling SDL_FlipScreen(); every time you press a key.

So you could have:



int main(int argc, char *argv[])
{

// SDL initialisation code etc.

while(k) // k is true, it gets set to false when you press escape. If it's
// false
// the loop falls through and return 0; is called, which
// causes the program to quit
{

DetectKeyPresses();

DrawScene();

}

// You could have a proper shutdown function to free all your surfaces, but it
// doesn't matter for now.

return 0;

}






And you change the position variables with the DetectKeyPresses function, and DrawScene draws your sprites at their new locations. That's why the bool variable comes into handy for the keypresses, the game will run through the loop so fast it creates the impression of real time motion.


Phew, long post, I hope I've helped! [smile]

That should at least help fix your problems for now, let me know how you got on,

ukdeveloper.

Share this post


Link to post
Share on other sites
It still won't work:
enum state { UP = 2,LEFT,RIGHT,DOWN,STOP };
state image;
image = STOP;
while (k == true)
{


SDL_BlitSurface(hero,NULL,screen,&ship);
SDL_PollEvent(&global);
if ( global.type == SDL_QUIT )
{
k = false;
SDL_Quit();
}


if ( global.type == SDL_KEYUP )
{
if ( global.key.keysym.sym == SDLK_ESCAPE )
{
k = false;
SDL_Quit();
}

if ( global.key.keysym.sym == SDLK_UP )
{
image = UP;
}
}
if(image == UP)
{
ship.x += 1;
SDL_BlitSurface(hero,NULL,screen,&ship);
SDL_Flip(screen);
}



return (0);
}
}









If I press any key it closes

Share this post


Link to post
Share on other sites
Try making a while loop with your keypress handler:


while(k)
{

SDL_Event event;

while(SDL_PollEvent(&event))
{

if(event.type==SDL_KEYDOWN)
{

if(event.key.keysym.sym==SDLK_KEYUP)
{
//code
}

else if (event.key.keysym.sym==SDLK_KEYDOWN)
{
// more code
}

} // Close while loop for event handler here

} // Close while loop for k here




That may fix your problem.

HTH,

ukdeveloper.

Share this post


Link to post
Share on other sites
For some reason when I do that the window opens then closes, can anyone else help me?

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!