-
OpenGl & SDL issue
For a few hours I tought I would make something for fun in 3d so I desided as a first project it would have to be easy.
So I desided to go for a rotating 3d triangle with 3 different colors.
I made it and the code for it looks like this:
Code:
#include <SDL/SDL.h>
#include <gl/gl.h>
int main(int argc, char *argv[]){
SDL_Event event;
float RotSpeed = 0.0f;
SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(600, 300, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME);
glViewport(0, 0, 600, 300);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
int done;
for(done = 0; !done;){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,0.0f);
glRotatef(RotSpeed, 0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.87f, -0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(-0.87f, -0.5f);
glEnd();
RotSpeed +=1.0f;
SDL_GL_SwapBuffers();
SDL_PollEvent(&event);
if(event.key.keysym.sym == SDLK_ESCAPE)
{
done = 1;
}
}
SDL_Quit();
return(0);
}
It worked! My first 3d app worked.
Then I wanted to add more features to it. I wanted to somehow make it go faster if you clicked "F" and slower if you clicked "S".
The code:
Code:
#include <SDL/SDL.h>
#include <gl/gl.h>
int main(int argc, char *argv[]){
SDL_Event event;
SDL_Event event2;
SDL_Event event3;
float RotSpeed = 0.0f;
SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(600, 300, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME);
glViewport(0, 0, 600, 300);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
int done;
for(done = 0; !done;){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,0.0f);
glRotatef(RotSpeed, 0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.87f, -0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(-0.87f, -0.5f);
glEnd();
RotSpeed +=1.0f;
SDL_GL_SwapBuffers();
SDL_PollEvent(&event);
if(event.key.keysym.sym == SDLK_ESCAPE)
{
done = 1;
}
SDL_PollEvent (&event2);
if(event2.key.keysym.sym == SDLK_F)
{
RotSpeed +=10.0f;
}
SDL_PollEvent (&event3);
if(event3.key.keysym.sym == SDLK_S)
{
RotSpeed +=1.0f;
}
}
SDL_Quit();
return(0);
}
I've marked the places where the compiler gives an error.
I'm using dev-c++, btw.
Thanks,
//Arangol
-
What error?
Besides, you're writing the event loop wrong. Call PollEvent once and then decide what to do with it, not multiple times. And be absolutely sure to check the type of the event before anything else.
http://www.libsdl.org/intro.en/usingevents.html
-
Why have you got more than 1 event? I've never seen it done like that before.
Anyway I think it has something to do with SDLK_F. The key part of the constant should be lower case:
Code:
if(event3.key.keysym.sym == SDLK_s)
{
// ...
-
Thanks, works perfectly now. I changed the events to one and then it worked much faster.