Archived

This topic is now archived and is closed to further replies.

Whats wrong with this sdl code???

This topic is 4999 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Code: /* -- Include the precompiled libraries -- */ #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( 640, 480, 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 c ;SDL_Event d; SDL_Event e; SDL_Event z ;while(true) { SDL_BlitSurface(img,NULL,screen, &dest); SDL_Flip( screen ); // wait for user exit while( !( SDL_PollEvent(&event) && event.type == SDL_QUIT ) ) {} } if(SDL_MouseButtonEvent(SDL_BUTTON_LEFT,SDL_PRESSED,200300) { SDL_FreeSurface(screen) ;} return 0; } ERROR: c:\Documents and Settings\Randle\Desktop\randyx''s stuff\PROJECTS\SDL\opp2604957sdl.cpp(29) : error C2661: ''SDL_MouseButtonEvent::SDL_MouseButtonEvent'' : no overloaded function takes 3 arguments c:\Documents and Settings\Randle\Desktop\randyx''s stuff\PROJECTS\SDL\opp2604957sdl.cpp(29) : error C2143: syntax error : missing '')'' before ''{''

Share on other sites
if(SDL_MouseButtonEvent(SDL_BUTTON_LEFT,SDL_PRESSED,200300)

should be

if(SDL_MouseButtonEvent(SDL_MOUSEBUTTONDOWN,SDL_BUTTON_LEFT,SDL_PRESSED,200,300))
or
if(SDL_MouseButtonEvent(SDL_MOUSEBUTTONUP,SDL_BUTTON_LEFT,SDL_PRESSED,200,300))

In all fairness though the errors pretty much told you exactly what to fix.

Share on other sites
why are you making 7 SDL_Event structors one after the other? i really absolutely no need for that

Share on other sites
First, don''t free a surface that you got from SDL_SetVideoMode. It will automatically be freed by SDL_Quit. DO free the surface you got from SDL_LoadBMP.

As for the error you''re getting: SDL_MouseButtonEvent is a struct. Your code isn''t going to do what (I assume) you want to do.

Actually, I was just reading your code, and I''m really not sure WHAT you''re trying to do. Blit the surface to the screen once, then exit if the user clicks the mouse button or if the SDL_QUIT event is received?

Share on other sites
First of all your code looks like crap. No offence but if you want anyone to look at your code at least make it look pleasant.

SDL_Event a;SDL_Event b;SDL_Event c       // dude the semi colon goes at the end

if you get into bad coding habits you are going to stick with them for a long time. try some tabs here and there. sorry about the nagging but its for your own good.

As for the problem with SDL what stro said should fix it.

Share on other sites
quote:
Original post by QzarBaron
First of all your code looks like crap. No offence but if you want anyone to look at your code at least make it look pleasant.

Yea, but I think it was more due to formatting errors from the sloppy copy/paste job. MPG make sure you either use the "{code}{/code}" tags to properly format short bits of code, or the "{source}{/source}" tags to properly format (and color) longer code snippets (since the source tags create code a scrollable box and thus take up less space).

Be sure to replace the {} brackets with [] - I had to use {} so the forums wouldn't do any formatting

_________________________________________________________________

Drew Sikora
Staff Writer, Newsletter Editor - GameDev.net
Community Relations - Game Institute

Share on other sites
Stro: It still dosnnt work
QuazarBron: It only takes it like that

Share on other sites
I''m gonna have to agree with TylerK here. What in the worlds are you trying to do? Anyways looking over that code makes me somewhat nauseous - do you have a better-formatted example and what are the new errors?

_________________________________________________________________

Drew Sikora
Staff Writer, Newsletter Editor - GameDev.net
Community Relations - Game Institute

Share on other sites
/* -- Include the precompiled libraries -- */#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( 640, 480, 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;    while(true);  {SDL_BlitSurface(img,NULL,screen, &dest);    SDL_Flip( screen );   // wait for user exit    while( !( SDL_PollEvent(&event) && event.type == SDL_QUIT ) )  {}        }  if(SDL_MouseButtonEvent(SDL_MOUSEBUTTONDOWN,SDL_BUTTON_LEFT,200,300))  {	  SDL_FreeSurface(img)  ;}  return 0;  }

c:\Documents and Settings\Randle\Desktop\randyx''s stuff\PROJECTS\SDL\opp2604957sdl.cpp(27): error C2661: ''SDL_MouseButtonEvent::SDL_MouseButtonEvent'' : no overloaded function takes 4 arguments

Share on other sites
See my added comments. As you may see, we might be able to help a lot more if you were to put into plain english what you are trying to do.

quote:
Original post by MPG
  while(true); // this will compile. this will loop infinitely due to the semicolon.  {// if you removed the semicolon above, this will blit and flip every cycle. you don''t change the image, though, so it''s not necessary.  SDL_BlitSurface(img,NULL,screen, &dest);   SDL_Flip( screen );   // wait for user exit  // here you wait for the SDL_QUIT event. the syntax is kinda weird, but it should do what you want it to do, I think, if you include a break. But you should rethink this whole loop.  while( !( SDL_PollEvent(&event) && event.type == SDL_QUIT ) )  {}        }// There are no more events polled after you poll SDL_QUIT above.  At this point in the program, you are exiting no matter what. You need no if statement. Free the surface, then exit. //if(SDL_MouseButtonEvent(SDL_MOUSEBUTTONDOWN,SDL_BUTTON_LEFT,200,300))//  {    SDL_FreeSurface(img)//  ;}      return 0;}

c:\Documents and Settings\Randle\Desktop\randyx''s stuff\PROJECTS\SDL\opp2604957sdl.cpp(27): error C2661: ''SDL_MouseButtonEvent::SDL_MouseButtonEvent'' : no overloaded function takes 4 arguments

Share on other sites
From the SDL Docs SDL_MouseButtonEvent is a structure - not a function, if you want to respond to mouse button clicks try this:

SDL_PollEvent(&Event);if(Event.type == SDL_MOUSEBUTTONDOWN){   //check to see which button was pressed   if(Event.button.button == SDL_BUTTON_LEFT)   {      //do whatever you want to do here   }}

Check out this page on the SDL docs site which lists the SDL_MouseButtonEvent structure.

Share on other sites
#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( 640, 480, 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 = 100;			 coord.y = 100;  while(true);  {SDL_BlitSurface(img,NULL,screen, &dest);    SDL_Flip( screen );   // wait for user exit    while( !( SDL_PollEvent(&event) && event.type == SDL_QUIT ) )  {}   SDL_PollEvent(&Event);if(Event.type == SDL_MOUSEBUTTONDOWN){      	if(Event.button.button == SDL_BUTTON_LEFT)            {			 SDL_Surface *imi =SDL_LoadBMP("ad.bmp")                       ;while(true);			 {				 SDL_BlitSurface(imi,NULL,screen,&coord)				;SDL_Flip(screen);			 }		 }         }        return 0;}

c:\Documents and Settings\Randle\Desktop\randyx''s stuff\PROJECTS\SDL\opp2604957sdl.cpp(44): 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(8)'' was matched

Share on other sites
quote:
Original post by MPG
c:\Documents and Settings\Randle\Desktop\randyx''s stuff\PROJECTS\SDL\opp2604957sdl.cpp(44): 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(8)'' was matched

I highly suggest you start reading, and learning how to understand, the error messages your compiler outputs.

Seriously. If you really cannot figure out what the problem is simply from the information given by the error message, then I sure hope you''re good looking.

Share on other sites
I keep trying and it still dosnrt work.I need to knnow how many semicolons i need at the end.

Share on other sites
I don''t intend to be rude to you at all, but what you really need is to buy a book and learn the fundamentals of C++ programming. It''s clear from your posts and your sample code that you don''t have a good handle on what you''re trying to do, and to be honest, I don''t get the impression that you''re really reading and trying to understand the posts from people who are trying to help you.

With that said, here''s some constructive advice: forget about semicolons and braces for a minute, and try to put into plain english what it is that you''re trying to DO. I think you''ll get a lot more help that way.

Share on other sites
I''m trying to check if the left button is clicked then load an image.I''ve fixed eveything else except the last error

Share on other sites
Ok, good. So, you''ll want to lay your code out something like this meta code:

InitDisplay();while(true) {    if (PollEvent) {        if (TypeOfEvent == QUIT) { Exit Program; }        if (TypeOfEvent == LEFTMOUSEBUTTONCLICKED) {            BlitImage();            UpdateDisplay();        }    }}

I''ll leave it up to you to translate this into actual C++ code using SDL. At the very least, you should see the differences between the strucutre of my sample and what you posted.

• Forum Statistics

• Total Topics
628686
• Total Posts
2984237

• 16
• 13
• 13
• 10
• 10