Originally Posted by
Arlanthir
@ Both
W00t!
Worked like a charm, -Wall and -g gave no output too, so I'm thinking no warnings of any sort =) Thank you!
@ Wybiral
What is SDL and what are the differences? I'm extremely new to this, so I'm just really following instructions..
OK... In the same way that you use GLUT to initialize a window and do things like handle user events (mouse/keys) you can use SDL for the same thing.
The main reason I advocate SDL over GLUT is this:
Code:
glutIdleFunc(main_loop_function);
glutMainLoop();
You have to pass a function to GLUT and then call "glutMainLoop()"
This hands the program flow over to GLUT so certain things can't be done, meaning: sometimes you can't incorporate a system that requires letting GLUT take over.
SDL on the other hand is very different... It doesn't control the program flow so it's up to you. SDL also has better mouse and key functions and it's even handling system is very easy to use. SDL also has several helper libraries like: SDL_image (which helps you load various image formats for use in SDL and OpenGL), SDL_net (a networking library), as well as other libraries for 2d graphics too.
Here's a modified SDL version of that GLUT demo:
Code:
#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#define window_width 640
#define window_height 480
// Keydown booleans
bool key[321];
// Process pending events
bool events()
{
SDL_Event event;
if( SDL_PollEvent(&event) )
{
switch( event.type )
{
case SDL_KEYDOWN : key[ event.key.keysym.sym ]=true ; break;
case SDL_KEYUP : key[ event.key.keysym.sym ]=false; break;
case SDL_QUIT : return false; break;
}
}
return true;
}
void main_loop_function()
{
float angle;
while( events() )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,0, -10);
glRotatef(angle, 0, 0, 1);
glBegin(GL_QUADS);
glColor3ub(255, 000, 000); glVertex2f(-1, 1);
glColor3ub(000, 255, 000); glVertex2f( 1, 1);
glColor3ub(000, 000, 255); glVertex2f( 1, -1);
glColor3ub(255, 255, 000); glVertex2f(-1, -1);
glEnd();
SDL_GL_SwapBuffers();
// Check keypresses
if( key[SDLK_RIGHT] ){ angle-=0.5; }
if( key[SDLK_LEFT ] ){ angle+=0.5; }
}
}
// Initialze OpenGL perspective matrix
void GL_Setup(int width, int height)
{
glViewport( 0, 0, width, height );
glMatrixMode( GL_PROJECTION );
glEnable( GL_DEPTH_TEST );
gluPerspective( 45, (float)width/height, 0.1, 100 );
glMatrixMode( GL_MODELVIEW );
}
int main()
{
// Initialize SDL with best video mode
SDL_Init(SDL_INIT_VIDEO);
const SDL_VideoInfo* info = SDL_GetVideoInfo();
int vidFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
if (info->hw_available) {vidFlags |= SDL_HWSURFACE;}
else {vidFlags |= SDL_SWSURFACE;}
int bpp = info->vfmt->BitsPerPixel;
SDL_SetVideoMode(window_width, window_height, bpp, vidFlags);
GL_Setup(window_width, window_height);
main_loop_function();
}
I added some extra stuff to show how you could process key events (as well as window exit's because SDL does NOT close the window for you, you have to end the loop)
You can compile that with:
Code:
g++ something.cpp -lSDL -lGLU -lGL
Also, as I mentioned SDL_image... You can find it in synaptic too, it makes texture loading VERY VERY easy (including: bmp, png, gif, jpg, pcx...)