Jump to content
  • Advertisement
Sign in to follow this  
Drakkcon

Yet more OGL trouble. Any idea why this freezes?

This topic is 5128 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

#include <SDL/SDL.h>
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

#define KeyPressed event.key.keysym.sym

SDL_Event event;
float xtheta = 0.0f, ytheta = 0.0f, ztheta = 0.0f;

void SetupGlAndSDL();

int main(int Arg_N, char ** Arg_V)
{
  SetupGlAndSDL();
   
  for(int done = 0; !done;)
  {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0.0f,0.0f,-10.0f);
    glRotatef(xtheta, ytheta, ztheta, 1.0f);

    glBegin(GL_QUADS);
    /////////////////////////
    glColor3f(0.0f,1.0f,0.0f);			// Set The Color To Green
		glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Top)
		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Top)
		glVertex3f(-1.0f, 1.0f, 1.0f);			// Bottom Left Of The Quad (Top)
		glVertex3f( 1.0f, 1.0f, 1.0f);			// Bottom Right Of The Quad (Top)
		
		glColor3f(1.0f,0.5f,0.0f);			// Set The Color To Orange
		glVertex3f( 1.0f,-1.0f, 1.0f);			// Top Right Of The Quad (Bottom)
		glVertex3f(-1.0f,-1.0f, 1.0f);			// Top Left Of The Quad (Bottom)
		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Bottom)
		glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Bottom)
		
		glColor3f(1.0f,0.0f,0.0f);			// Set The Color To Red
		glVertex3f( 1.0f, 1.0f, 1.0f);			// Top Right Of The Quad (Front)
		glVertex3f(-1.0f, 1.0f, 1.0f);			// Top Left Of The Quad (Front)
		glVertex3f(-1.0f,-1.0f, 1.0f);			// Bottom Left Of The Quad (Front)
		glVertex3f( 1.0f,-1.0f, 1.0f);			// Bottom Right Of The Quad (Front)
		
	  glColor3f(1.0f,1.0f,0.0f);			// Set The Color To Yellow
		glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Back)
		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Back)
		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Back)
		glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Back)
		
		glColor3f(0.0f,0.0f,1.0f);			// Set The Color To Blue
		glVertex3f(-1.0f, 1.0f, 1.0f);			// Top Right Of The Quad (Left)
		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Left)
		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Left)
		glVertex3f(-1.0f,-1.0f, 1.0f);			// Bottom Right Of The Quad (Left)
		
		glColor3f(1.0f,0.0f,1.0f);			// Set The Color To Violet
		glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Right)
		glVertex3f( 1.0f, 1.0f, 1.0f);			// Top Left Of The Quad (Right)
		glVertex3f( 1.0f,-1.0f, 1.0f);			// Bottom Left Of The Quad (Right)
		glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Right)	
	  ////////////////////////////
    glEnd();						// Done Drawing The Quad
    
    SDL_GL_SwapBuffers();
    SDL_PollEvent(&event);
    
    if(KeyPressed == SDLK_RIGHT)
      ytheta += 0.5f;
    if(KeyPressed == SDLK_LEFT)
      ytheta -= 0.5f;
    if(KeyPressed == SDLK_x)
      ztheta += 0.5f;
    if(KeyPressed == SDLK_z)
      ztheta -= 0.5f;
    if(KeyPressed == SDLK_ESCAPE)
      done = 1;
  }
  

  SDL_Quit();
  
  return(0);
}

void SetupGlAndSDL()
{
    
  SDL_Init(SDL_INIT_VIDEO);
  SDL_SetVideoMode(1024, 768, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME | SDL_FULLSCREEN);
  
  glViewport(0, 0, 1024, 768);
  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);
  glLoadIdentity();
	gluPerspective(45.0f,1024.0f/768.0f,0.1f,100.0f);
	
  glMatrixMode(GL_MODELVIEW);

}  


The cube appears in the center of the screen, but no input is taken and it freezes. The only key that works is escape. Why's this?

Share this post


Link to post
Share on other sites
Advertisement
your not taking input properly. the polling loop should be a seperate loop from the main loop. try this:


while(SDL_PollEvent(&event))
{

if(KeyPressed == SDLK_RIGHT)
ytheta += 0.5f;
if(KeyPressed == SDLK_LEFT)
ytheta -= 0.5f;
if(KeyPressed == SDLK_x)
ztheta += 0.5f;
if(KeyPressed == SDLK_z)
ztheta -= 0.5f;
if(KeyPressed == SDLK_ESCAPE)
done = 1;
}

Share this post


Link to post
Share on other sites
yay, It works! Thanks alot!
How come I have to keep pressing the arrow on my keyboard though, instead of getting to hold it down?

Share this post


Link to post
Share on other sites
your checking for a single keydown event. this means when a key is pressed once, that event happends once.

i think there is some sort of SDL_EnableKeyRepeat function or something. however, that is not how i handle this. instead, i rectreive info directly using SDL_GetKeyState().

let me know how you get it worked out, im curious myself about the key repeat thing. if you need help using GetKeyState() just post.

Share this post


Link to post
Share on other sites
Thanks alot! Here's the fixed source

#include <SDL/SDL.h>
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

#define KeyPressed event.key.keysym.sym

SDL_Event event;
float xtheta = 0.0f, ytheta = 0.0f, ztheta = 0.0f;

void SetupGlAndSDL();

int main(int Arg_N, char ** Arg_V)
{
SetupGlAndSDL();

for(int done = 0; !done;)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-10.0f);
glRotatef(xtheta, ytheta, ztheta, 1.0f);

glBegin(GL_QUADS);
/////////////////////////
glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)

glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)

glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)

glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Back)

glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)

glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
////////////////////////////
glEnd(); // Done Drawing The Quad

SDL_GL_SwapBuffers();

while(SDL_PollEvent(&event))
{
if (event.key.state == SDL_PRESSED)
{
if(KeyPressed == SDLK_RIGHT)
ytheta += 0.5f;
if(KeyPressed == SDLK_LEFT)
ytheta -= 0.5f;
if(KeyPressed == SDLK_UP)
xtheta += 0.5f;
if(KeyPressed == SDLK_DOWN)
xtheta -= 0.5f;
if(KeyPressed == SDLK_x)
ztheta += 0.5f;
if(KeyPressed == SDLK_z)
ztheta -= 0.5f;
if(KeyPressed == SDLK_ESCAPE)
done = 1;
}
}
}


SDL_Quit();

return(0);
}

void SetupGlAndSDL()
{



SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(1024, 768, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME | SDL_FULLSCREEN);

glViewport(0, 0, 1024, 768);
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);
glLoadIdentity();
gluPerspective(45.0f,1024.0f/768.0f,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);

SDL_EnableKeyRepeat(1, 1);

}

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!