Last active
December 16, 2015 22:29
-
-
Save beakr/5506947 to your computer and use it in GitHub Desktop.
Got SDL C++ lib to work with Xcode 4. Make sure you have SDL.framework, Cocoa.framework, and OpenGL.framework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <SDL/SDL.h> | |
#include <SDL/SDL_opengl.h> | |
#define WIDTH 640 | |
#define HEIGHT 480 | |
#define WINDOW_TITLE "Hello, SDL!" | |
SDL_Surface *screen; | |
void render() { | |
glClearColor(0,0,0,0); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glBegin(GL_TRIANGLES); | |
{ | |
glColor3f(1,0,0); | |
glVertex2f(0,0); | |
glColor3f(0,1,0); | |
glVertex2f(.5,0); | |
glColor3f(0,0,1); | |
glVertex2f(.5,.5); | |
} | |
glEnd(); | |
} | |
extern "C" int main (int argc, char * argv[]) { | |
// Initialize | |
SDL_Init(SDL_INIT_VIDEO); | |
// Enable double-buffering | |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
// Create a OpenGL window | |
screen = SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL | | |
SDL_RESIZABLE); | |
if(!screen) | |
{ | |
printf("Couldn't set %dx%d GL video mode: %s\n", WIDTH, | |
HEIGHT, SDL_GetError()); | |
SDL_Quit(); | |
exit(2); | |
} | |
SDL_WM_SetCaption(WINDOW_TITLE, WINDOW_TITLE); | |
bool done = false; | |
while(!done) | |
{ | |
SDL_Event event; | |
// Rotates the triangle (this could be replaced with custom | |
// processing code) | |
// animate(); | |
// Respond to any events that occur | |
while(SDL_PollEvent(&event)) | |
{ | |
switch(event.type) | |
{ | |
case SDL_VIDEORESIZE: | |
screen = SDL_SetVideoMode(event.resize.w, | |
event.resize.h, 0, | |
SDL_OPENGL | SDL_RESIZABLE); | |
if(screen) | |
{ | |
// reshape(screen->w, screen->h); | |
} | |
else | |
{ | |
; // Oops, we couldn't resize for some reason. | |
// This should never happen | |
} | |
break; | |
case SDL_QUIT: | |
done = true; | |
break; | |
// ## INSERT CODE TO HANDLE ANY OTHER EVENTS HERE ## | |
} | |
} | |
// Check for escape | |
Uint8 *keys = SDL_GetKeyState(NULL); | |
if( keys[SDLK_ESCAPE] ) { | |
done = true; | |
} | |
// Draw the screen | |
render(); | |
SDL_GL_SwapBuffers(); | |
} | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment