Sign in to follow this  
Niddles

Screen Disappears

Recommended Posts

Niddles    216
Hello everyone, It's been a while. I've been quite busy, and haven't been able to program much so I've forgotten a bit. I am using SDL_image, SDL, and OpenGL. I am trying to just put a texture on the screen, after I do that, I can do the rest, but I'm having trouble remembering how to do it. I think my code is correct, but the screen just comes up and disappears. Could someone help me rid of this problem? Heres the code:
#include <SDL/SDL.h>
#include <gl/gl.h>
#include <SDL/SDL_image.h>
/*
 PLEASE NOTE: the program will require SDL.dll which is located in
              dev-c++'s dll directory. You have to copy it to you
			  program's home directory or the path.
 */
SDL_Surface* LoadPNG(char* filename);
SDL_Surface *test;
GLuint *bwah;
int main(int argc, char *argv[]){
  SDL_Event event;
  float theta = 0.0f;

  SDL_Init(SDL_INIT_VIDEO);
  SDL_SetVideoMode(600, 300, 0, SDL_OPENGL | SDL_HWSURFACE);

  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);
    test = LoadPNG("blah.png");
    glGenTextures(1, bwah);
    glBindTexture(GL_TEXTURE_2D, *bwah);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, test->w, test->h, 0, GL_RGBA, GL_UNSIGNED_INT, test->pixels);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	
  int done;
  for(done = 0; !done;){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_QUADS);
        glBindTexture(GL_TEXTURE_2D, *bwah);
        glVertex2f(0.0, 0.0); glTexCoord2f(0.0, 0.0);
        glVertex2f(0.0, 1.0); glTexCoord2f(0.0, 1.0);
        glVertex2f(1.0, 1.0); glTexCoord2f(1.0, 1.0);
        glVertex2f(1.0, 0.0); glTexCoord2f(1.0, 0.0);
    glEnd();
    SDL_GL_SwapBuffers();
    SDL_PollEvent(&event);
    if(event.key.keysym.sym == SDLK_ESCAPE)
      done = 1;
  }
  SDL_Quit();
  return(0);
}
SDL_Surface* LoadPNG(char* filename)
{
    SDL_Surface *image;
    SDL_RWops *rwop;
    rwop = SDL_RWFromFile(filename, "rb");
    image = IMG_LoadPNG_RW(rwop);
    if(!image)
    {
        printf("IMG_LoadPNG_RW: %s\n", IMG_GetError());
    }
    return image;
}

Thanks!

Share this post


Link to post
Share on other sites
jyk    2094
Just to address the problem at hand, this looks suspicious:
SDL_PollEvent(&event);
if(event.key.keysym.sym == SDLK_ESCAPE)
done = 1;
You might take another look at the documentation for SDL_PollEvent(). In short, the event is only good if SDL_PollEvent() returns a value of 1 (which means there was in fact an event pending). Also, you're not checking the type of the event (what if it's not a key event?).

Now actually, although your event-handling code is incorrect, I'm not sure that it's the cause of the problem. I also see that you're not doing any error checking when loading images, setting the video mode, etc., so it seems any one of these could be failing.

At the very least, you should add assertions where applicable so that you know what (if anything) is failing, and where. Even better, check for errors and output the return value of SDL_GetError() before bailing out of the program.

There are a few other issues here as well, but this should be enough to get started. Remember, you can't just assume that things will work - whenever a function can fail, you need to check for failure and respond accordingly.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this