Jump to content
  • Advertisement
Sign in to follow this  
biscuitz

OpenGL I can't work OpenGL...

This topic is 4889 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm getting tired of looking at the tutorials I've found. They're making my brain hurt. I keep trying to figure out how to make it work in SDL and I'm lost. I think I'm not initializing the OpenGL right or something. What am I doing wrong?
#include <sdl\sdl.h>
#include <gl\gl.h>
#include <gl\glu.h>

//Setup variables
int width=800,height=600;

int main(int argc, char **argv)
{
  SDL_Init(SDL_INIT_VIDEO);
  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,32);
  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
  SDL_Surface *screen=SDL_SetVideoMode(width,height,0,SDL_OPENGL|SDL_HWSURFACE);
  
    //Setup OpenGL
    glEnable(GL_CULL_FACE);
    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0,(GLfloat)width/(GLfloat)height,1.0,1024.0);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
      glVertex3f(0.0,1.0,2.0);
      glVertex3f(-1.0,-1.0,2.0);
      glVertex3f(1.0,-1.0,2.0);
    glEnd();
    glFlush();

  SDL_Event event;
  
  bool loop=1;
  while(loop)
  {
    if(SDL_PollEvent(&event))
    switch(event.type)
    {
      case SDL_QUIT:
        loop=0;
        break;
      case SDL_KEYDOWN:
        switch(event.key.keysym.sym)
        {
          case SDLK_ESCAPE:
            loop=0;
        }
    }
    
    SDL_Delay(1);
  }

   SDL_Quit();

   return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
When the program starts it just shows a window but it's see through. There's no black screen. Just what's behind the window.

P.S. Is this supposed to be in the SDL section?

Share this post


Link to post
Share on other sites
I see some errors...

But you could use this...

#include <SDL/SDL.h>
#include <gl/gl.h>
int main(int Arg_N, char ** Arg_V)
{
SDL_Event event;
float theta = 0.0f;
SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(800, 600, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME);

glViewport(0, 0, 800, 600);
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);

for(int done = 0; !done;)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,0.0f);
glRotatef(theta, 0.0f, 0.0f, 1.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();

theta += .5f;
SDL_GL_SwapBuffers();

SDL_PollEvent(&event);
if(event.key.keysym.sym == SDLK_ESCAPE)
done = 1;
}
SDL_Quit();
return(0);
}

Share this post


Link to post
Share on other sites
Okay thanks. Now I have something to build on. :)

Just wondering, where did you find that code? I saw that in a tutorial somewhere but I saw so many tutorials I forgot. lol

[EDIT]
Whoops that's why it's so familiar. It's a template for Dev-C++ (duh).

[Edited by - biscuitz on July 29, 2005 11:30:49 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!