PDA

View Full Version : Conditional Compilation Errors


Blacktiger
2006.06.15, 12:02 AM
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:


/* 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?

OneSadCookie
2006.06.15, 12:08 AM
my test says it works:

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 :p

Blacktiger
2006.06.15, 12:15 AM
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?

Blacktiger
2006.06.15, 12:17 AM
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!

OneSadCookie
2006.06.15, 12:20 AM
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.

Blacktiger
2006.06.15, 12:25 AM
Because I couldn't find the documentation anywhere when I looked earlier. :D
Thanks that fixed it.

szymczyk
2006.06.15, 03:52 PM
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.

Blacktiger
2006.06.15, 06:38 PM
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.

szymczyk
2006.06.16, 01:42 AM
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.
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.