🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Sdl movement

Started by
19 comments, last by matthughson 19 years, 11 months ago
My image still wont move!

#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif
#include "SDL.h"

int main( int argc, char* argv[] )
{
  SDL_Init( SDL_INIT_VIDEO );  
  atexit( SDL_Quit );  
  SDL_Surface * screen = SDL_SetVideoMode( 800,600, 24, SDL_ANYFORMAT|SDL_DOUBLEBUF );  
  SDL_Surface * img = SDL_LoadBMP( "filename.bmp" );   
  SDL_Rect dest;  
  dest.x = 200;  
  dest.y = 300;  
  SDL_Event event;
  SDL_Event a;
  SDL_Event b;
  SDL_Event Event              
	 ;SDL_Rect coord
			 ;coord.x = 200;
			 coord.y = 500;
			 SDL_Surface * tdisplay = SDL_SetVideoMode( 800,600,24,SDL_ANYFORMAT|SDL_DOUBLEBUF );
			 SDL_Surface * imi = SDL_LoadBMP( "ad.bmp" );
             SDL_Surface * bckg = SDL_LoadBMP ( "bg.bmp" );
               float playerposx;
				   playerposx = 0.0;
				#define TICK_INTERVAL    30

Uint32 move(void)
{
    static Uint32 next_time = 0;
    Uint32 now;

    now = SDL_GetTicks();
    if ( next_time <= now ) {
        next_time = now+TICK_INTERVAL;
        return(0);
    }
    return(next_time-now);
}

   
 ;while(true) {		
	  SDL_BlitSurface(img,NULL,screen, &dest);		
	  SDL_PollEvent(&event);		
	  if(event.type == SDL_MOUSEBUTTONDOWN) 
	  {			
		  if(event.button.button == SDL_BUTTON_LEFT) 
		  {				
		    
			  SDL_BlitSurface(imi,NULL,tdisplay,&coord);				
		      SDL_Flip(tdisplay);
			  
		  ;}
	  }		
	  SDL_Event up;
case SDL_KEYDOWN:{     
	switch( up.key.keysym.sym )     
	{         
	case SDLK_UP:         
		{
          playerpposx += 1.0*now
	  else if(event.type == SDL_QUIT) { return 0; }		
	  SDL_Flip( screen );		
  }   
  return 0;
}

Compiling... opp2604957sdl.cpp c:\Documents and Settings\Randle\Desktop\randyx's stuff\PROJECTS\SDL\opp2604957sdl.cpp(31) : error C2601: 'move' : local function definitions are illegal c:\Documents and Settings\Randle\Desktop\randyx's stuff\PROJECTS\SDL\opp2604957sdl.cpp(58) : error C2046: illegal case c:\Documents and Settings\Randle\Desktop\randyx's stuff\PROJECTS\SDL\opp2604957sdl.cpp(63) : error C2065: 'playerpposx' : undeclared identifier c:\Documents and Settings\Randle\Desktop\randyx's stuff\PROJECTS\SDL\opp2604957sdl.cpp(64) : error C2065: 'now' : undeclared identifier c:\Documents and Settings\Randle\Desktop\randyx's stuff\PROJECTS\SDL\opp2604957sdl.cpp(64) : error C2143: syntax error : missing ';' before 'else' c:\Documents and Settings\Randle\Desktop\randyx's stuff\PROJECTS\SDL\opp2604957sdl.cpp(64) : error C2181: illegal else without matching if c:\Documents and Settings\Randle\Desktop\randyx's stuff\PROJECTS\SDL\opp2604957sdl.cpp(71) : fatal error C1075: end of file found before the left brace '{' at 'c:\Documents and Settings\Randle\Desktop\randyx's stuff\PROJECTS\SDL\opp2604957sdl.cpp(58)' was matched
Advertisement
C++ doesn't allow you to define nested functions, which is what you're trying to do. You're going to have to pull move out of main's definition and put it on its own.
1- Local function definitions are illegal in C++. You need to move the body of the mode() function, so it is above the body of the main() function, and not inside.

2- playerpposx has not been defined. playerposx has. Check your spelling!

3- the move( ) function returns the amount of time since the last movement step has occured. You need to use this time for your movement calculations. That is, wherever you used the "now" variable outside the move( ) function body, use "move()" instead.

Also notice "now" has only been defined in the "move" function : it is NOT defined outside that function.

4- remove all unnecessary {'s

5- check out your code blocks, so every code block begins with a { and ends with a }. As a rule of thumb, whenever you write a new block, write both { and }, and only then start adding code between them. That way you don't get missing braces.

6- add ";" after each operation. Remove unnecessary ;'s, for instance before "while" or "}"

7- "case SDL_KEYDOWN:" has no meaning here. If you want to use a case, create a switch block to put it in.

My suggestion : rewrite entirely that code, from nothing. No copy-pasting. Use the following guidelines:

- Add only one line at a time.
- If you want to add a block (an if block, an else block, a while block, a switch block, or a function body), create the { and } braces first, before writing anything else.
- Every time you've added one line or a "{}", compile your project. If it doesn't compile, find the error before doing ANYTHING else.
Also, something that might help greatly is understanding what you're doing. Before writing any code down, write down in english (or whatever your first language is) what you want the code to do, what steps should be taken, etc...

Then, repeat the process for each step you used above, until your entire project is only a series of commands that can be directly translated into C++.

And never, ever, copy-paste anything you haven't written yourself. Not only won't you understand what the copy-pasted code does (and thus you won't become autonomous), but you also create uncompilable code because you don't know how to connect everything.
can you explain a switch block?
Here's a page which explains switch statements: clicky, I would recommend that you read through the whole thing and not just that page as don't quite seem to have grasped the basics of C++ by your code.
end of file found before the left brace '{'

WHAT DOES THIS MEAN???????????????????????






It means you have missed out a closing brace somewhere in your code. Go through your files and check that all your { match up with a }.
Ok it compiled but the up key osnt respond but when I click the left mouse button it flickers and move but dosnt stay.Whats wrong?
You Know what MPG don't think that i am trying to flame you or something but you definetely need to go and learn the basic first, because it seems like you don't even know the simplest things and you want to program a game already. Trust me you are going to strungle so much if you don't know the basic.

This topic is closed to new replies.

Advertisement