Conditional Compilation Errors

Member
Posts: 254
Joined: 2005.10
Post: #1
I am trying to write some simple SDL code in preperation for a Carlosvision entry. I want to make the code cross-platform, but for some reason my conditional compilation stuff fails! Here is the code:

Code:
/* This file contains the main routine to start up the game.
* Routines used in this file should be initialization routines only.
* Author: Michael Martz
* Date: 14 June 2006
*/

//-- Set the OS we are building for --\\
#define MACOSX

#ifdef MACOSX
    #include "SDL.h"
    #include <OpenGL/gl.h>
#endif

#ifdef WINDOWS
    #include "SDL.h"
    #include <gl.h>
#endif

#ifdef LINUX
    #include "SDL.h"
    #include <GL/gl.h>
#endif

using namespace std;

/* Initialize OpenGL Attributes */
// Returns: true if successful, false otherwise
void initOpenGL();

/* Create an OpenGL Context */
// Returns: the OpenGL context
SDL_Surface* makeContext();

int main(int argc, char *argv[])
{
    //-- Initialize SDL Subsystems --\\
    // Assumption: SDL_INIT returns a non-zero value only if an error occurs.
    if(SDL_Init(SDL_INIT_EVERYTHING))
    {
        fprintf(stderr, "Couldn't initialize SDL: %s\n",
                SDL_GetError());
        exit(1);
    }
    
    //-- Initialize OpenGL --\\
    initOpenGL();
    
    // Create a surface to draw on.
    SDL_Surface* oglContext = makeContext();

    // Cleanup SDL
    SDL_Quit();

    return 0;
}

/* Initialize OpenGL Attributes */
// Returns: true if successful, false otherwise
void initOpenGL()
{
    // Since we need 3D to handle our 'rotated' view, setup the depth buffer.
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    
    // Make OpenGL depth buffered.
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, true);
    
    // On Mac OS X you don’t need to set the size of the frame buffer’s red, green, blue, and alpha
    //    components. If you create a draw context with 32 bit color, Mac OS X uses 8 bits for each
    //    of the four components. On other operating systems it may be necessesary to do so. A little
    //    preprocessor work will help here.
    #ifndef MACOSX
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    #endif
}

/* Create an OpenGL Context */
// Returns: the OpenGL context
SDL_Surface* makeContext()
{
    return SDL_SetVideoMode(800, 600, 32, SDL_OPENGL | SDL_FULLSCREEN);
}

But #include "SDL.h" never executes apparently so none of the SDL calls work! What am I doing wrong?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
my test says it works:

Code:
keithb@keithbxppc ~
$ cat > test.h
#define X
#if defined(X)
x
#endif
#if defined(Y)
y
#endif

keithb@keithbxppc ~
$ gcc -E test.h
# 1 "test.h"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "test.h"


x

so something else is going on Rasp
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #3
I tried cleaning the target in XCode, and switching to release mode (to disable zero-link). I even tried messing with the indentaiton on my preprocessor statements. So far I got nothing... any ideas?
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #4
Hmm... Ok so wrapping the whole thing with a #ifndef MACOSX .... #endif clears the SDL errors. But now I get a "#endif without #if" error!
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
Sounds like maybe you're conflicting with somebody else using the same macro. Why not use the standard defines for those OSes, which are set by the compiler:

__APPLE__
_WIN32
linux

Then you don't have to make the definition yourself, and you can be sure that nobody will tread on your toes.
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #6
Because I couldn't find the documentation anywhere when I looked earlier. Grin
Thanks that fixed it.
Quote this message in a reply
Member
Posts: 111
Joined: 2002.06
Post: #7
You could avoid the conditional compilation by including the header file SDL_OpenGL.h instead of gl.h. Including SDL_OpenGL.h will include gl.h and glu.h for you, and you won't have to deal with the hassle of finding the correct path to the gl.h file.

Mark Szymczyk
http://www.meandmark.com
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #8
Hmm... so why didn't you just put that on your SDL and OpenGL tutorial? I'll be sure to use that instead to save me some coding, but I'm still going to need a bit of platform dependent code here and there so its nice to know how to do it.
Quote this message in a reply
Member
Posts: 111
Joined: 2002.06
Post: #9
Blacktiger Wrote:Hmm... so why didn't you just put that on your SDL and OpenGL tutorial?
I did mention using the SDL_OpenGL.h header file in the tutorial. Here's a quote from the tutorial.
Quote:To use OpenGL with SDL, you must either include the SDL header file SDL_OpenGL.h or the header file gl.h from the OpenGL framework.

Mark Szymczyk
http://www.meandmark.com
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Bizarre Conditional Operator Problem in C++ kingofsquirrels 2 2,101 May 23, 2006 02:56 PM
Last Post: kingofsquirrels