tags:

views:

204

answers:

4

I'm using Dev-C++ 4.9.9.2 (don't ask why) and SDL 1.2.8.

Next I've created new project: SDL&GL. This project contains already some code:

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

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 | 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(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);
}

Next I compiled project and try to run it. After run the program shows for less than 1 second and immediately terminates. Debugger returns following error: "An Access Violation (Segmentation Fault) raised in your program".

I'm using Windows 2003 and Radeon x1950 PRO with latest drivers.

I've tested program on laptop with Windows XP and it works perfectly. Why this program doesn't work on my computer?

A: 

Run your program from a debugger and see why it stops. There simply is not enough information in your code to say anything about your particular problem.

Bahbar
Debugger returns following error: "An Access Violation (Segmentation Fault) raised in your program".
GTD
SegFault on which line ? in what function ?
Bahbar
It doesn't point to any line. Just message box with this error shows up.
GTD
That's not a debugger output then. You really need to use a debugger. A debugger will show you the exact stack trace that triggered the segfault, and with full symbols, which exact call triggered it (in source code)
Bahbar
A: 

My guess is it's crashing on SDL_PollEvent(). It returns 1/true if there is an event, 0/false if not. When it does return true, it will be a certain type of SDL_Event based on event.type. SDL_Event is a union of all SDL events and some info in one event is not guaranteed to be in the same order, type, etc as another. So, you just need to check the type of the event and handle it as necessary.. Check out the docs for more info of course. Something like this:

if (SDL_PollEvent(&event)) {
  switch (event.type) {
    case SDL_KEYUP:
      if (event.key.keysym.sym == SDLK_ESCAPE)
        done = 1;
  }
}
RayOK
That would not _crash_. It will read from uninitialized memory or a previous event value, so it could __quit__ prematurely. Good catch though!
Bahbar
+1  A: 

It works for me too. I'd try removing SDL_HWSURFACE and add SDL_DOUBLEBUF from the window call.

SDL_SetVideoMode(600, 300, 0, SDL_OPENGL | SDL_NOFRAME | SDL_DOUBLEBUF);

while(!done) looks prettier and easier to read. Since it's tagged with C++, why are you not using bools for this?

bool done = false;
while(!done){

You also want while(SDL_PollEvent(&event)), as there can be more than one event per frame.

while(SDL_PollEvent(&event))
{
    switch(event.type)
        case SDL_KEYDOWN:
            if(event.key.keysym.sym == SDLK_ESCAPE)
                done = true;
}
pbos
A: 

I finally found some time to solve this problem. I have completly uninstalled old card graphic drivers and install 9.8 ATI drivers with Catalyst Control Center. Now everything is working.

There where no problem in code itself. The problem was something in my system with graphic drivers. Anyway thanks for your answers!

GTD