Sign in to follow this  
Meagermanx

SDL Errors

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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>

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 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.
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this