sdl_gl_setattribute 上的 sdl 应用程序段错误

发布于 2024-09-27 06:44:03 字数 3652 浏览 5 评论 0原文

我正在尝试编译此 示例并尝试一下。我已经纠正了人们在这个示例中遇到的主要错误,即他们会在调用 SDL_Init 之前调用 sdl_gl_setattribute,但在第一次 SDL_GL_SetAttribute 调用后我仍然遇到段错误。我之前在我的计算机上运行过带 opengl 应用程序的 sdl,并且我确信它可以与我的视频卡配合使用。

从这段代码中有人知道为什么它会出现段错误吗?或者,这段代码可以在其他人的计算机上运行吗?如果有什么区别的话,我在 ubuntu 10.04 上使用 freeglut3 来处理 opengl 的东西。

//compile with cc triangle.c -o triangle `sdl-config --libs --cflags` -lglut

#include <stdio.h>
#include "SDL.h"
#include <GL/gl.h>

int event_thread(void* nothing);

int main(int argc, char** argv) {
    float theta = 0.0f;

    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD);

    //first set buffer stuff, then doublebuf (if wanted), then SDL_SetVideoMode()
    if(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) { printf("couldn't set double buffering: %s\n", SDL_GetError()); }

    //go through and get the values to see if everything was set
    int red, green, blue, doublebuf;
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red);
    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green);
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &blue);
    SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuf);
    printf("red size, green size, blue size: <%d, %d, %d>\ndouble buffered? %s\n", red, green, blue, (doublebuf == 1 ? "yes" : "no"));

    //pass sdl_resizable if it's an opengl application that is windowed and not fullscreened
    SDL_Surface* screen = SDL_SetVideoMode(600, 300, 32, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME | SDL_RESIZABLE);
    if(screen == NULL) {
        printf("video error: %s\n", SDL_GetError());
    }

    //print video card memory
    const SDL_VideoInfo* info = SDL_GetVideoInfo();
    printf("video card memory (in megabytes): %d\n", info->video_mem);

    //set opengl params for drawing in 3d space
    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);

    //start up the event thread
    int done = 0;
    SDL_Thread* evt_thrd;
    evt_thrd = SDL_CreateThread(event_thread, (void*)&done);

    for(;!done;) {
        //clear and move to 0, 0, 0
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glTranslatef(0.0f, 0.0f, 0.0f);
        glRotatef(theta, 0.0f, 0.0f, 1.0f);

        //draw the triangle
        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 += 0.5f;
        SDL_GL_SwapBuffers();
    }

    SDL_Quit();
    return 0;
}

int event_thread(void* nothing) {
    int* done = (int*)nothing;
    SDL_Event event;

    while(1) {
        while(SDL_PollEvent(&event)) {
            if(event.type == SDL_QUIT || 
            (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) {
                *done = 1;
                return 0;
            }
        }
    }
}

I'm trying to compile this example and play around with it a bit. I've already corrected the main error the people were having with this example where they would call sdl_gl_setattribute before SDL_Init was called but I'm still getting a segfault right after the first SDL_GL_SetAttribute call. I've ran sdl with opengl apps before on my computer and I'm certain it's been working with my video card.

From this code does anybody know why it would segfault? Or, does this code work on anybody else's computer? If it makes any difference I'm on ubuntu 10.04 using freeglut3 for opengl stuff.

//compile with cc triangle.c -o triangle `sdl-config --libs --cflags` -lglut

#include <stdio.h>
#include "SDL.h"
#include <GL/gl.h>

int event_thread(void* nothing);

int main(int argc, char** argv) {
    float theta = 0.0f;

    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD);

    //first set buffer stuff, then doublebuf (if wanted), then SDL_SetVideoMode()
    if(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) { printf("couldn't set double buffering: %s\n", SDL_GetError()); }

    //go through and get the values to see if everything was set
    int red, green, blue, doublebuf;
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red);
    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green);
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &blue);
    SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuf);
    printf("red size, green size, blue size: <%d, %d, %d>\ndouble buffered? %s\n", red, green, blue, (doublebuf == 1 ? "yes" : "no"));

    //pass sdl_resizable if it's an opengl application that is windowed and not fullscreened
    SDL_Surface* screen = SDL_SetVideoMode(600, 300, 32, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME | SDL_RESIZABLE);
    if(screen == NULL) {
        printf("video error: %s\n", SDL_GetError());
    }

    //print video card memory
    const SDL_VideoInfo* info = SDL_GetVideoInfo();
    printf("video card memory (in megabytes): %d\n", info->video_mem);

    //set opengl params for drawing in 3d space
    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);

    //start up the event thread
    int done = 0;
    SDL_Thread* evt_thrd;
    evt_thrd = SDL_CreateThread(event_thread, (void*)&done);

    for(;!done;) {
        //clear and move to 0, 0, 0
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glTranslatef(0.0f, 0.0f, 0.0f);
        glRotatef(theta, 0.0f, 0.0f, 1.0f);

        //draw the triangle
        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 += 0.5f;
        SDL_GL_SwapBuffers();
    }

    SDL_Quit();
    return 0;
}

int event_thread(void* nothing) {
    int* done = (int*)nothing;
    SDL_Event event;

    while(1) {
        while(SDL_PollEvent(&event)) {
            if(event.type == SDL_QUIT || 
            (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) {
                *done = 1;
                return 0;
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2024-10-04 06:44:03

试试这个版本:

//compile with cc triangle.c -o triangle `sdl-config --libs --cflags` -lglut

#include <stdio.h>
#include "SDL.h"
#include "SDL_opengl.h"

int event_thread(void* nothing);

int main(int argc, char** argv) {
    float theta = 0.0f;

    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD);

    //first set buffer stuff, then doublebuf (if wanted), then SDL_SetVideoMode()
    if(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) { printf("couldn't set double buffering: %s\n", SDL_GetError()); }

    //pass sdl_resizable if it's an opengl application that is windowed and not fullscreened
    SDL_Surface* screen = SDL_SetVideoMode(600, 300, 32, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME | SDL_RESIZABLE);
    if(screen == NULL) {
        printf("video error: %s\n", SDL_GetError());
    }

    //go through and get the values to see if everything was set
    int red, green, blue, doublebuf;
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red);
    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green);
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &blue);
    SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuf);
    printf("red size, green size, blue size: <%d, %d, %d>\ndouble buffered? %s\n", red, green, blue, (doublebuf == 1 ? "yes" : "no"));

    //print video card memory
    const SDL_VideoInfo* info = SDL_GetVideoInfo();
    printf("video card memory (in megabytes): %d\n", info->video_mem);

    //set opengl params for drawing in 3d space
    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);

    //start up the event thread
    int done = 0;
    SDL_Thread* evt_thrd;
    evt_thrd = SDL_CreateThread(event_thread, (void*)&done);

    for(;!done;) {
        //clear and move to 0, 0, 0
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glTranslatef(0.0f, 0.0f, 0.0f);
        glRotatef(theta, 0.0f, 0.0f, 1.0f);

        //draw the triangle
        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 += 0.5f;
        SDL_GL_SwapBuffers();
    }

    SDL_Quit();
    return 0;
}

int event_thread(void* nothing) {
    int* done = (int*)nothing;
    SDL_Event event;

    while(1) {
        while(SDL_PollEvent(&event)) {
            if(event.type == SDL_QUIT || 
            (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) {
                *done = 1;
                return 0;
            }
        }
    }
}

Try this version:

//compile with cc triangle.c -o triangle `sdl-config --libs --cflags` -lglut

#include <stdio.h>
#include "SDL.h"
#include "SDL_opengl.h"

int event_thread(void* nothing);

int main(int argc, char** argv) {
    float theta = 0.0f;

    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD);

    //first set buffer stuff, then doublebuf (if wanted), then SDL_SetVideoMode()
    if(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) { printf("couldn't set double buffering: %s\n", SDL_GetError()); }

    //pass sdl_resizable if it's an opengl application that is windowed and not fullscreened
    SDL_Surface* screen = SDL_SetVideoMode(600, 300, 32, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME | SDL_RESIZABLE);
    if(screen == NULL) {
        printf("video error: %s\n", SDL_GetError());
    }

    //go through and get the values to see if everything was set
    int red, green, blue, doublebuf;
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red);
    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green);
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &blue);
    SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuf);
    printf("red size, green size, blue size: <%d, %d, %d>\ndouble buffered? %s\n", red, green, blue, (doublebuf == 1 ? "yes" : "no"));

    //print video card memory
    const SDL_VideoInfo* info = SDL_GetVideoInfo();
    printf("video card memory (in megabytes): %d\n", info->video_mem);

    //set opengl params for drawing in 3d space
    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);

    //start up the event thread
    int done = 0;
    SDL_Thread* evt_thrd;
    evt_thrd = SDL_CreateThread(event_thread, (void*)&done);

    for(;!done;) {
        //clear and move to 0, 0, 0
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glTranslatef(0.0f, 0.0f, 0.0f);
        glRotatef(theta, 0.0f, 0.0f, 1.0f);

        //draw the triangle
        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 += 0.5f;
        SDL_GL_SwapBuffers();
    }

    SDL_Quit();
    return 0;
}

int event_thread(void* nothing) {
    int* done = (int*)nothing;
    SDL_Event event;

    while(1) {
        while(SDL_PollEvent(&event)) {
            if(event.type == SDL_QUIT || 
            (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) {
                *done = 1;
                return 0;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文