随笔 - 29  文章 - 13  trackbacks - 0
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

c++

Linux

Tools

搜索

  •  

最新评论

阅读排行榜

评论排行榜

上一篇中我们绘制了一个彩色的三角型
这次我们让它动起来

#include <GL/gl.h>
#include <SDL/SDL.h>
bool running=true;
float y=0.0;

void initGL()
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_SetVideoMode(600,300,16,SDL_OPENGL);
    
}
void destroyGL()
{
    SDL_Quit();
}
void draw()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glRotatef(y,0,1,0);
    glBegin(GL_TRIANGLES);
        glColor3f(1,0,0);
        glVertex3f(0,0,0);
        glColor3f(0,1,0);
        glVertex3f(1,0,0);
        glColor3f(0,0,1);
        glVertex3f(0,1,0);
    glEnd();

    SDL_GL_SwapBuffers();

}
void quit()
{
    running=false;
}
void onKeyDown(const SDL_Event& event)
{
    switch(event.key.keysym.sym)
    {
    case SDLK_ESCAPE:
    quit();
    break;
    case SDLK_LEFT:
    y+=0.1;
    break;
    case SDLK_RIGHT:
    y-=0.1;
    break;

    
    }
}
void loop()
{
    SDL_Event event;
    while(running)
    {
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
            case SDL_QUIT:
                quit();
            break;
            case SDL_KEYDOWN:
                onKeyDown(event);
            break;
            }
        }
        draw();
        SDL_Delay(50);
    }

}
int main(int argc,char* argv[])
{
    initGL();
    loop();
    destroyGL();    
    return 0;
}
#include <GL/gl.h>
#include <SDL/SDL.h>
bool running=true;
float y=0.0;

void initGL()
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_SetVideoMode(600,300,16,SDL_OPENGL);
   
}
void destroyGL()
{
    SDL_Quit();
}
void draw()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glRotatef(y,0,1,0);
    glBegin(GL_TRIANGLES);
        glColor3f(1,0,0);
        glVertex3f(0,0,0);
        glColor3f(0,1,0);
        glVertex3f(1,0,0);
        glColor3f(0,0,1);
        glVertex3f(0,1,0);
    glEnd();

    SDL_GL_SwapBuffers();

}
void quit()
{
    running=false;
}
void onKeyDown(const SDL_Event& event)
{
    switch(event.key.keysym.sym)
    {
    case SDLK_ESCAPE:
    quit();
    break;
    case SDLK_LEFT:
    y+=0.1;
    break;
    case SDLK_RIGHT:
    y-=0.1;
    break;

   
    }
}
void loop()
{
    SDL_Event event;
    while(running)
    {
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
            case SDL_QUIT:
                quit();
            break;
            case SDL_KEYDOWN:
                onKeyDown(event);
            break;
            }
        }
        draw();
        SDL_Delay(50);
    }

}
int main(int argc,char* argv[])
{
    initGL();
    loop();
    destroyGL();   
    return 0;
}

posted on 2006-09-28 16:46 四海 阅读(526) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理