SDL Errors

Started by
10 comments, last by Drew_Benton 19 years, 2 months ago
I'm trying to learn SDL with the cone3d tutorials, and after reading and typing the first two tutorials, I decided to try to make my own framework to better understand it. Thing.cpp is the name of the file.

#include <SDL/SDL.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
//Throw up the Video and Audio
void setup()
{
         if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)<0)
    {
        cout << "Unable to r0xx0r." << SDL_GetError();
        system("PAUSE");
        return;
    }
atexit(SDL_Quit);

//Set up the drawing surface.
SDL_Surface *screen;
SDL_Surface *icon;
screen = SDL_SetVideoMode(640,480,32, SDL_HWSURFACE|SDL_DOUBLEBUF);
}




int main()
{
    setup();
    system("PAUSE");
    return 0;
}



Now, when I compile, I get the following error messages:

2 C:\APPLICATIONS\DEV-CPP\include\c++\3.3.1\backward\iostream.h:31,               from Thing.cpp In file included from C:/APPLICATIONS/DEV-CPP/include/c++/3.3.1/backward/iostream.h:31,               from Thing.cpp 

2 C:\Applications\Dev-CPP\Programs\SDL\Thing.cpp                  from Thing.cpp 

2 C:\APPLICATIONS\DEV-CPP\include\c++\3.3.1\backward\backward_warning.h:32 #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated. 

 C:\APPLICATIONS\DEV-CPP\lib\libSDLmain.a(SDL_win32_main.o.b)(.text+0x3bd) In function `console_main': 

  [Linker error] undefined reference to `SDL_main' 

 C:\APPLICATIONS\DEV-CPP\lib\libSDLmain.a(SDL_win32_main.o.b)(.text+0x3bd) In function `console_main': 

  [Linker error] undefined reference to `SDL_main' 

Any ideas as to what I might be doing wrong?
---There are 2 kinds of people: those who know hexadecimal, and those who don't.
Advertisement
Do you have the correct libraries linked? For Dev-C++ you use:

-lmingw32
-lSDLmain
-lSDL

in project -> project options -> parameters -> linker

About that warning, I think its basically telling you that you're including deprecated headers. You should use:

#include <iostream>
#include <cstdio>
#include <cstdlib>
the rug - funpowered.com
this is that I am using
#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h> //or #include <iostream>
int main()


Should be:

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


That is definitly the error. (I tried it out [wink])
First change, use standard headers:
#include <iostream>#include <cstdlib>using namespace std;


As stated previously, second change:
int main( int argc, char* argv[])


And now the logic:

There's a problem here since your SDL_Surface is local to the setup() function. Your program is going to run through the setup() function one time, creating the SDL window, then when it returns to main all that will be gone.
- A momentary maniac with casual delusions.
Quote:Original post by Rhaal
And now the logic:

There's a problem here since your SDL_Surface is local to the setup() function. Your program is going to run through the setup() function one time, creating the SDL window, then when it returns to main all that will be gone.


I would also like to point out that, assuming the OP adds more code for his game, it is fine to do that - although it is really counter intuitive (and probabally an accidental mistake). Usually what is done is either make 'screen' global or something a lot of people do not do/know - just make a call to SDL_GetVideoSurface whenever you want to retrieve the current video surface. However, I'd say sitck with Rhaal's advice and make it global or alternatively use the SDL_GetVideoSurface and you will not have a 'screen' variable.

- Drew
Thanks Drew (and the rest)!!! I have a question, though. Why do I have to put the declarations in main()? What does that do?
---There are 2 kinds of people: those who know hexadecimal, and those who don't.
Quote:Original post by Meagermanx
Thanks Drew (and the rest)!!! I have a question, though. Why do I have to put the declarations in main()? What does that do?


It's more of variable scope. This is what you should probabally do.

#include <SDL/SDL.h>#include <iostream.h>#include <stdio.h>#include <stdlib.h>SDL_Surface *screen;SDL_Surface *icon;//Throw up the Video and Audiovoid setup(){         if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)<0)    {        cout << "Unable to r0xx0r." << SDL_GetError();        system("PAUSE");        return;    }atexit(SDL_Quit);//Set up the drawing surface.screen = SDL_SetVideoMode(640,480,32, SDL_HWSURFACE|SDL_DOUBLEBUF);}int main(){    setup();    system("PAUSE");    return 0;}


Now you can access the screen from any function in the main.cpp file. However, when you start using more and more files, I'd say just use the SDL_GetVideoSurface instead. It is a lot easier and less for you to keep track of.

- Drew
Okay, I changed my code:
#include <SDL/SDL.h>#include <cstdio>#include <iostream.h>#include <cstdlib>//Global Variabibbles.SDL_Surface *screen;SDL_Surface *icon;//Throw up the Video and Audio.void setup(){         if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)<0)    {        cout << "Unable to r0xx0r." << SDL_GetError();        system("PAUSE");        return;    }atexit(SDL_Quit);}screen = SDL_SetVideoMode(640,480,32, SDL_HWSURFACE|SDL_DOUBLEBUF);int main(int argc, char *argv[]){    setup();    system("PAUSE");    return 0;}


And if I take your advice, and switch <iostream.h> to <iostream>, my cout is undefined.

Now it says my assignment of screen is messing things up:

3 C:\APPLICATIONS\DEV-CPP\include\c++\3.3.1\backward\iostream.h:31,               from Thing.cpp In file included from C:/APPLICATIONS/DEV-CPP/include/c++/3.3.1/backward/iostream.h:31,               from Thing.cpp 3 C:\Applications\Dev-CPP\Programs\SDL\Thing.cpp                  from Thing.cpp 2 C:\APPLICATIONS\DEV-CPP\include\c++\3.3.1\backward\backward_warning.h:32 #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated. 22 C:\Applications\Dev-CPP\Programs\SDL\Thing.cpp ISO C++ forbids declaration of `screen' with no type 22 C:\Applications\Dev-CPP\Programs\SDL\Thing.cpp conflicting types for `int screen' 7 C:\Applications\Dev-CPP\Programs\SDL\Thing.cpp previous declaration as `SDL_Surface*screen' 


Any insight you feel like offering? :D
---There are 2 kinds of people: those who know hexadecimal, and those who don't.
screen = SDL_SetVideoMode(640,480,32, SDL_HWSURFACE|SDL_DOUBLEBUF);

Should be in here:
void setup(){    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)<0)    {        cout << "Unable to r0xx0r." << SDL_GetError();        system("PAUSE");        return;    }    atexit(SDL_Quit);    screen = SDL_SetVideoMode(640,480,32, SDL_HWSURFACE|SDL_DOUBLEBUF);}


You did the right thing by making the screen variable global, but you will still need to assign it inside a function to adhere to C++ syntax standards. Hope this helps.

- Drew

This topic is closed to new replies.

Advertisement