• Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    eliot
    Posts: 564 from 2004/4/15
    Just for completeness, here is a better example to handle the input events:
    Code:

    #define GL_GLEXT_PROTOTYPES

    #ifdef __APPLE__

    #include <gl.h>
    #include <glu.h>

    #else

    #include <GL/gl.h>
    #include <GL/glu.h>

    #endif

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

    using namespace std;

    static void setupOpenGl(int width, int height) {
    const float ratio = (float) width / (float) height;

    glShadeModel(GL_SMOOTH);

    glCullFace(GL_BACK);
    glFrontFace(GL_CCW);
    glEnable(GL_CULL_FACE);

    glClearColor(0, 0, 0, 0);

    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, ratio, 1.0, 1024.0);
    }

    static void drawScreen() {
    static float angle = 0.0f;

    static GLfloat v0[] = {-1.0f, -1.0f, 1.0f};
    static GLfloat v1[] = {1.0f, -1.0f, 1.0f};
    static GLfloat v2[] = {1.0f, 1.0f, 1.0f};
    static GLfloat v3[] = {-1.0f, 1.0f, 1.0f};
    static GLfloat v4[] = {-1.0f, -1.0f, -1.0f};
    static GLfloat v5[] = {1.0f, -1.0f, -1.0f};
    static GLfloat v6[] = {1.0f, 1.0f, -1.0f};
    static GLfloat v7[] = {-1.0f, 1.0f, -1.0f};
    static GLubyte red[] = {255, 0, 0, 255};
    static GLubyte green[] = {0, 255, 0, 255};
    static GLubyte blue[] = {0, 0, 255, 255};
    static GLubyte white[] = {255, 255, 255, 255};
    static GLubyte yellow[] = {0, 255, 255, 255};
    static GLubyte black[] = {0, 0, 0, 255};
    static GLubyte orange[] = {255, 255, 0, 255};
    static GLubyte purple[] = {255, 0, 255, 0};

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glTranslatef(0.0, 0.0, -5.0);

    glRotatef(angle, 0.0, 1.0, 0.0);

    if (++angle > 360.0f) {
    angle = 0.0f;
    }

    glBegin(GL_TRIANGLES);

    glColor4ubv(red);
    glVertex3fv(v0);
    glColor4ubv(green);
    glVertex3fv(v1);
    glColor4ubv(blue);
    glVertex3fv(v2);

    glColor4ubv(red);
    glVertex3fv(v0);
    glColor4ubv(blue);
    glVertex3fv(v2);
    glColor4ubv(white);
    glVertex3fv(v3);

    glColor4ubv(green);
    glVertex3fv(v1);
    glColor4ubv(black);
    glVertex3fv(v5);
    glColor4ubv(orange);
    glVertex3fv(v6);

    glColor4ubv(green);
    glVertex3fv(v1);
    glColor4ubv(orange);
    glVertex3fv(v6);
    glColor4ubv(blue);
    glVertex3fv(v2);

    glColor4ubv(black);
    glVertex3fv(v5);
    glColor4ubv(yellow);
    glVertex3fv(v4);
    glColor4ubv(purple);
    glVertex3fv(v7);

    glColor4ubv(black);
    glVertex3fv(v5);
    glColor4ubv(purple);
    glVertex3fv(v7);
    glColor4ubv(orange);
    glVertex3fv(v6);

    glColor4ubv(yellow);
    glVertex3fv(v4);
    glColor4ubv(red);
    glVertex3fv(v0);
    glColor4ubv(white);
    glVertex3fv(v3);

    glColor4ubv(yellow);
    glVertex3fv(v4);
    glColor4ubv(white);
    glVertex3fv(v3);
    glColor4ubv(purple);
    glVertex3fv(v7);

    glColor4ubv(white);
    glVertex3fv(v3);
    glColor4ubv(blue);
    glVertex3fv(v2);
    glColor4ubv(orange);
    glVertex3fv(v6);

    glColor4ubv(white);
    glVertex3fv(v3);
    glColor4ubv(orange);
    glVertex3fv(v6);
    glColor4ubv(purple);
    glVertex3fv(v7);

    glColor4ubv(green);
    glVertex3fv(v1);
    glColor4ubv(red);
    glVertex3fv(v0);
    glColor4ubv(yellow);
    glVertex3fv(v4);

    glColor4ubv(green);
    glVertex3fv(v1);
    glColor4ubv(yellow);
    glVertex3fv(v4);
    glColor4ubv(black);
    glVertex3fv(v5);

    glEnd();

    SDL_GL_SwapBuffers();
    }

    int main(int argc, char *argv[]) {
    cout << "begin ..." << endl;

    int res = SDL_Init(SDL_INIT_VIDEO);
    if (res) {
    cout << "could not init sdl, result=" << res << ", error=" << SDL_GetError() << endl;
    return -1;
    }

    SDL_Surface *screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
    if (screen == nullptr) {
    cout << "could set display mode, error=" << SDL_GetError() << endl;
    return -2;
    }
    SDL_WM_SetCaption("sdl_test", nullptr);
    setupOpenGl(640, 480);

    SDL_Event events[16];
    bool b = true;
    while (b) {
    SDL_PumpEvents();
    res = SDL_PeepEvents(events, 16, SDL_GETEVENT,
    SDL_KEYDOWNMASK |
    SDL_KEYUPMASK |
    SDL_MOUSEMOTIONMASK |
    SDL_MOUSEBUTTONDOWNMASK |
    SDL_MOUSEBUTTONUPMASK |
    SDL_QUITMASK);
    for (int i = 0; i < res; ++i) {
    switch (events[i].type) {
    case SDL_KEYDOWN:
    cout << "key down event" << endl;
    break;
    case SDL_KEYUP:
    cout << "key up event" << endl;
    break;
    case SDL_MOUSEMOTION:
    cout << "mouse motion event, x=" << events[i].motion.x << ", y=" << events[i].motion.y << endl;
    break;
    case SDL_MOUSEBUTTONDOWN:
    cout << "mouse button down event" << endl;
    break;
    case SDL_MOUSEBUTTONUP:
    cout << "mouse button up event" << endl;
    break;
    case SDL_QUIT:
    cout << "quit event" << endl;
    b = false;
    break;
    }
    }
    drawScreen();
    SDL_Delay(40);
    }

    SDL_Quit();
    cout << "end!" << endl;
    return 0;
    }
    regards
    eliot
  • »09.02.17 - 12:19
    Profile