Xcode/TemplatesForXcodeLeopard/SDL OpenGL Application/main.c
author Sam Lantinga
Tue, 08 Feb 2011 10:04:09 -0800
changeset 5226 710d00cb3a6a
parent 3331 d44a0a913aa2
child 7191 75360622e65f
permissions -rw-r--r--
Made it possible to disable the rendering subsystem with configure --disable-render
slouken@3331
     1
slouken@3331
     2
/* Simple program:  Create a blank window, wait for keypress, quit.
slouken@3331
     3
slouken@3331
     4
   Please see the SDL documentation for details on using the SDL API:
slouken@3331
     5
   /Developer/Documentation/SDL/docs.html
slouken@3331
     6
*/
slouken@3331
     7
   
slouken@3331
     8
#include <stdio.h>
slouken@3331
     9
#include <stdlib.h>
slouken@3331
    10
#include <string.h>
slouken@3331
    11
#include <math.h>
slouken@3331
    12
slouken@3331
    13
#include "SDL.h"
slouken@3331
    14
slouken@3331
    15
extern void Atlantis_Init ();
slouken@3331
    16
extern void Atlantis_Reshape (int w, int h);
slouken@3331
    17
extern void Atlantis_Animate ();
slouken@3331
    18
extern void Atlantis_Display ();
slouken@3331
    19
slouken@3331
    20
static SDL_Surface *gScreen;
slouken@3331
    21
slouken@3331
    22
static void initAttributes ()
slouken@3331
    23
{
slouken@3331
    24
    // Setup attributes we want for the OpenGL context
slouken@3331
    25
    
slouken@3331
    26
    int value;
slouken@3331
    27
    
slouken@3331
    28
    // Don't set color bit sizes (SDL_GL_RED_SIZE, etc)
slouken@3331
    29
    //    Mac OS X will always use 8-8-8-8 ARGB for 32-bit screens and
slouken@3331
    30
    //    5-5-5 RGB for 16-bit screens
slouken@3331
    31
    
slouken@3331
    32
    // Request a 16-bit depth buffer (without this, there is no depth buffer)
slouken@3331
    33
    value = 16;
slouken@3331
    34
    SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, value);
slouken@3331
    35
    
slouken@3331
    36
    
slouken@3331
    37
    // Request double-buffered OpenGL
slouken@3331
    38
    //     The fact that windows are double-buffered on Mac OS X has no effect
slouken@3331
    39
    //     on OpenGL double buffering.
slouken@3331
    40
    value = 1;
slouken@3331
    41
    SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, value);
slouken@3331
    42
}
slouken@3331
    43
slouken@3331
    44
static void printAttributes ()
slouken@3331
    45
{
slouken@3331
    46
    // Print out attributes of the context we created
slouken@3331
    47
    int nAttr;
slouken@3331
    48
    int i;
slouken@3331
    49
    
slouken@3331
    50
    int  attr[] = { SDL_GL_RED_SIZE, SDL_GL_BLUE_SIZE, SDL_GL_GREEN_SIZE,
slouken@3331
    51
                    SDL_GL_ALPHA_SIZE, SDL_GL_BUFFER_SIZE, SDL_GL_DEPTH_SIZE };
slouken@3331
    52
                    
slouken@3331
    53
    char *desc[] = { "Red size: %d bits\n", "Blue size: %d bits\n", "Green size: %d bits\n",
slouken@3331
    54
                     "Alpha size: %d bits\n", "Color buffer size: %d bits\n", 
slouken@3331
    55
                     "Depth bufer size: %d bits\n" };
slouken@3331
    56
slouken@3331
    57
    nAttr = sizeof(attr) / sizeof(int);
slouken@3331
    58
    
slouken@3331
    59
    for (i = 0; i < nAttr; i++) {
slouken@3331
    60
    
slouken@3331
    61
        int value;
slouken@3331
    62
        SDL_GL_GetAttribute (attr[i], &value);
slouken@3331
    63
        printf (desc[i], value);
slouken@3331
    64
    } 
slouken@3331
    65
}
slouken@3331
    66
slouken@3331
    67
static void createSurface (int fullscreen)
slouken@3331
    68
{
slouken@3331
    69
    Uint32 flags = 0;
slouken@3331
    70
    
slouken@3331
    71
    flags = SDL_OPENGL;
slouken@3331
    72
    if (fullscreen)
slouken@3331
    73
        flags |= SDL_FULLSCREEN;
slouken@3331
    74
    
slouken@3331
    75
    // Create window
slouken@3331
    76
    gScreen = SDL_SetVideoMode (640, 480, 0, flags);
slouken@3331
    77
    if (gScreen == NULL) {
slouken@3331
    78
		
slouken@3331
    79
        fprintf (stderr, "Couldn't set 640x480 OpenGL video mode: %s\n",
slouken@3331
    80
                 SDL_GetError());
slouken@3331
    81
		SDL_Quit();
slouken@3331
    82
		exit(2);
slouken@3331
    83
	}
slouken@3331
    84
}
slouken@3331
    85
slouken@3331
    86
static void initGL ()
slouken@3331
    87
{
slouken@3331
    88
    Atlantis_Init ();
slouken@3331
    89
    Atlantis_Reshape (gScreen->w, gScreen->h);
slouken@3331
    90
}
slouken@3331
    91
slouken@3331
    92
static void drawGL ()
slouken@3331
    93
{
slouken@3331
    94
    Atlantis_Animate ();
slouken@3331
    95
    Atlantis_Display ();
slouken@3331
    96
}
slouken@3331
    97
slouken@3331
    98
static void mainLoop ()
slouken@3331
    99
{
slouken@3331
   100
    SDL_Event event;
slouken@3331
   101
    int done = 0;
slouken@3331
   102
    int fps = 24;
slouken@3331
   103
	int delay = 1000/fps;
slouken@3331
   104
    int thenTicks = -1;
slouken@3331
   105
    int nowTicks;
slouken@3331
   106
    
slouken@3331
   107
    while ( !done ) {
slouken@3331
   108
slouken@3331
   109
		/* Check for events */
slouken@3331
   110
		while ( SDL_PollEvent (&event) ) {
slouken@3331
   111
			switch (event.type) {
slouken@3331
   112
slouken@3331
   113
				case SDL_MOUSEMOTION:
slouken@3331
   114
					break;
slouken@3331
   115
				case SDL_MOUSEBUTTONDOWN:
slouken@3331
   116
					break;
slouken@3331
   117
				case SDL_KEYDOWN:
slouken@3331
   118
					/* Any keypress quits the app... */
slouken@3331
   119
				case SDL_QUIT:
slouken@3331
   120
					done = 1;
slouken@3331
   121
					break;
slouken@3331
   122
				default:
slouken@3331
   123
					break;
slouken@3331
   124
			}
slouken@3331
   125
		}
slouken@3331
   126
    
slouken@3331
   127
        // Draw at 24 hz
slouken@3331
   128
        //     This approach is not normally recommended - it is better to
slouken@3331
   129
        //     use time-based animation and run as fast as possible
slouken@3331
   130
        drawGL ();
slouken@3331
   131
        SDL_GL_SwapBuffers ();
slouken@3331
   132
slouken@3331
   133
        // Time how long each draw-swap-delay cycle takes
slouken@3331
   134
        // and adjust delay to get closer to target framerate
slouken@3331
   135
        if (thenTicks > 0) {
slouken@3331
   136
            nowTicks = SDL_GetTicks ();
slouken@3331
   137
            delay += (1000/fps - (nowTicks-thenTicks));
slouken@3331
   138
            thenTicks = nowTicks;
slouken@3331
   139
            if (delay < 0)
slouken@3331
   140
                delay = 1000/fps;
slouken@3331
   141
        }
slouken@3331
   142
        else {
slouken@3331
   143
            thenTicks = SDL_GetTicks ();
slouken@3331
   144
        }
slouken@3331
   145
slouken@3331
   146
        SDL_Delay (delay);
slouken@3331
   147
	}
slouken@3331
   148
}
slouken@3331
   149
slouken@3331
   150
int main(int argc, char *argv[])
slouken@3331
   151
{
slouken@3331
   152
	// Init SDL video subsystem
slouken@3331
   153
	if ( SDL_Init (SDL_INIT_VIDEO) < 0 ) {
slouken@3331
   154
		
slouken@3331
   155
        fprintf(stderr, "Couldn't initialize SDL: %s\n",
slouken@3331
   156
			SDL_GetError());
slouken@3331
   157
		exit(1);
slouken@3331
   158
	}
slouken@3331
   159
slouken@3331
   160
    // Set GL context attributes
slouken@3331
   161
    initAttributes ();
slouken@3331
   162
    
slouken@3331
   163
    // Create GL context
slouken@3331
   164
    createSurface (0);
slouken@3331
   165
    
slouken@3331
   166
    // Get GL context attributes
slouken@3331
   167
    printAttributes ();
slouken@3331
   168
    
slouken@3331
   169
    // Init GL state
slouken@3331
   170
    initGL ();
slouken@3331
   171
    
slouken@3331
   172
    // Draw, get events...
slouken@3331
   173
    mainLoop ();
slouken@3331
   174
    
slouken@3331
   175
    // Cleanup
slouken@3331
   176
	SDL_Quit();
slouken@3331
   177
	
slouken@3331
   178
    return 0;
slouken@3331
   179
}