OpenGl & SDL issue

This is a discussion on OpenGl & SDL issue within the Game Programming forums, part of the General Programming Boards category; For a few hours I tought I would make something for fun in 3d so I desided as a first ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    9

    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

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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)
    {
        // ...
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    9
    Thanks, works perfectly now. I changed the events to one and then it worked much faster.
    Last edited by Arangol; 06-18-2006 at 11:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL and SDL
    By ThLstN in forum Game Programming
    Replies: 2
    Last Post: 08-28-2008, 06:09 AM
  2. OpenGL using SDL --> Should I or shouldn't I?
    By Devils Child in forum C++ Programming
    Replies: 12
    Last Post: 02-01-2008, 12:18 PM
  3. SDL and OpenGL
    By ElastoManiac in forum Game Programming
    Replies: 2
    Last Post: 11-22-2006, 03:47 AM
  4. Replies: 5
    Last Post: 11-27-2005, 08:50 PM
  5. SDL or GLUT for Opengl?
    By drdroid in forum Game Programming
    Replies: 1
    Last Post: 07-17-2003, 01:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21