openGL错误未经处理的异常访问冲突(仅适用于windows 7) 你好,我现在用C++和OpenGL制作了一个非常简单的游戏,但是不知道我的程序的哪个部分在Windows XP和8中出错。它在我的Windows7中运行得非常好

openGL错误未经处理的异常访问冲突(仅适用于windows 7) 你好,我现在用C++和OpenGL制作了一个非常简单的游戏,但是不知道我的程序的哪个部分在Windows XP和8中出错。它在我的Windows7中运行得非常好,c++,windows,opengl,C++,Windows,Opengl,编译是由VisualStudio6.0完成的。在大多数情况下,我在mingw中使用GCC,但我发现,在处理opengl时,使用GCC编译的程序在屏幕上不断产生一些意想不到的白色眼泪(如撕裂/撕裂/撕裂) 无论如何,这个程序在Windows7中运行没有问题。我不太担心malloc/free,但就目前看来,它做得很好 我用我朋友的电脑在WindowsXP和8.1中测试了这个程序,发现它在这两个系统中都不能正常运行。我在xp计算机上安装了另一个visual studio 6.0并运行了调试。令人惊讶的

编译是由VisualStudio6.0完成的。在大多数情况下,我在mingw中使用GCC,但我发现,在处理opengl时,使用GCC编译的程序在屏幕上不断产生一些意想不到的白色眼泪(如撕裂/撕裂/撕裂)

无论如何,这个程序在Windows7中运行没有问题。我不太担心malloc/free,但就目前看来,它做得很好

我用我朋友的电脑在WindowsXP和8.1中测试了这个程序,发现它在这两个系统中都不能正常运行。我在xp计算机上安装了另一个visual studio 6.0并运行了调试。令人惊讶的是,虽然win7中的visual studio没有这样做,但xp vs6.0告诉我“XXXX.exe(OPENGL32.DLL)中未处理的异常:0xC0000005:Acces违例”,反汇编代码如下:(错误是黄色箭头指向的地方)

不幸的是,我对汇编语言没有理解,我的代码在阅读时似乎仍然很好。。但它确实导致了一个错误,所以我不得不在这里寻求帮助

您可以在这里看到整个cpp源代码

#include <cstdlib>
#include <ctime>
#include <cmath>
#include <list>
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
using namespace std;

#define DELAY 1

#define TOP 0
#define BOTTOM 1
#define RIGHT 2
#define LEFT 3

#define X0Y1 0
#define XHY1 1
#define X1Y1 2
#define X1YH 3
#define X1Y0 4

#define PLUS 1
#define MINUS -1

void display();
void add(int n);
GLdouble *aniData();
void move();
void arrowPress(int key, int x, int y);
void arrowUp(int key, int x, int y);
bool upDownPress(), rightLeftPress();
GLdouble dRand(GLdouble min, GLdouble max);
int iRand(int min, int max);

const GLdouble unit = 1.0 / 256.0, sideLen = 1.0 / 32.0, maxAttack = 1.0 / 8.0;
const int maxMove = (int)(maxAttack + 2.0) / unit + 5;
GLdouble x = -sideLen / 2.0, y = -sideLen / 2.0;
GLboolean upPress = GL_FALSE, downPress = GL_FALSE, rightPress = GL_FALSE, leftPress = GL_FALSE;
list<GLdouble *> dList;
list<GLdouble *>::iterator it;
unsigned k = -8, attacks = 0;

struct attacker {
    GLdouble    width, height, ax, ay;
    int     side, slope, xSign, ySign;
    void        init()
    {
        width = dRand(unit, maxAttack);
        height = dRand(unit, maxAttack);
        side = iRand(0, 3);
        bool fromOtherSide = false;
        switch (side) {
        case TOP:
topI:
            ax = dRand(-1.0 - unit, 1.0 - width + unit);
            ay = 1.0 + unit;
            if (!fromOtherSide) {
                slope = iRand(0, 4);
                if (slope == X1Y0) {
                    if (iRand(0, 1) == 0)
                        goto rightI;
                    goto leftI;
                }
            }
            xSign = 2 * (rand() % 2) - 1;
            ySign = MINUS;
            break;
        case BOTTOM:
bottomI:
            ax = dRand(-1.0 - unit, 1.0 - width + unit);
            ay = -1.0 - height - unit;
            if (!fromOtherSide) {
                slope = iRand(0, 4);
                if (slope == X1Y0) {
                    if (iRand(0, 1) == 0)
                        goto rightI;
                    goto leftI;
                }
            }
            xSign = 2 * (rand() % 2) - 1;
            ySign = PLUS;
            break;
        case RIGHT:
rightI:
            ax = 1.0 + unit;
            ay = dRand(-1.0 - unit, 1.0 - height + unit);
            if (!fromOtherSide) {
                slope = iRand(0, 4);
                if (slope == X0Y1) {
                    if (iRand(0, 1) == 0)
                        goto topI;
                    goto bottomI;
                }
            }
            xSign = MINUS;
            ySign = 2 * (rand() % 2) - 1;
            break;
        case LEFT:
leftI:
            ax = -1.0 - width - unit;
            ay = dRand(-1.0 - unit, 1.0 - height + unit);
            if (!fromOtherSide) {
                slope = iRand(0, 4);
                if (slope == X0Y1) {
                    if (iRand(0, 1) == 0)
                        goto topI;
                    goto bottomI;
                }
            }
            xSign = PLUS;
            ySign = 2 * (rand() % 2) - 1;
            break;
        }
    }
};
struct attacker *makeAttacker()
{
    return (struct attacker *)malloc(sizeof(struct attacker));
}

int main()
{
    srand((unsigned)time(NULL));
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(200, 100);
    glutCreateWindow("¿?¿?¿?¿?¿?¿? 1.0");
    glutDisplayFunc(display);
    glutSpecialFunc(arrowPress);
    glutSpecialUpFunc(arrowUp);
    glutIdleFunc(move);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glutMainLoop();
    return 0;
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3d(0.0, 1.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex2d(x, y);
    glVertex2d(x, y + sideLen);
    glVertex2d(x + sideLen, y + sideLen);
    glVertex2d(x + sideLen, y);
    glEnd();
    if (k == 8 * maxMove) {
        k = -8;
        for (int i = 0; i < attacks; ++i) {
            free(*dList.begin());
            dList.erase(dList.begin());
        }
        ++attacks;
        add(attacks);
    }
    k += 8;
    it = dList.begin();
    glColor3d(1.0, 0.0, 1.0);
    for (int i = 0; i < attacks; ++i) {
        glBegin(GL_POLYGON);
        glVertex2d((*it)[k], (*it)[k + 1]);
        glVertex2d((*it)[k + 2], (*it)[k + 3]);
        glVertex2d((*it)[k + 4], (*it)[k + 5]);
        glVertex2d((*it)[k + 6], (*it)[k + 7]);
        glEnd();
        it++;
    }
    glutSwapBuffers();
}

void add(int n)
{
    for (int i = 0; i < n; ++i)
        dList.push_back(aniData());
}

GLdouble *aniData()
{
    GLdouble *d = (GLdouble *)malloc(8 * maxMove * sizeof(GLdouble));
    struct attacker *sample = makeAttacker();

    sample->init();
    GLdouble sx = sample->ax, sy = sample->ay;
    int i, signX = sample->xSign, signY = sample->ySign;
    switch (sample->slope) {
    case X0Y1:
        for (i = 0; i < 8 * maxMove; i += 8) {
            sy += unit * signY;
            d[i] = sx;
            d[i + 1] = sy;
            d[i + 2] = sx;
            d[i + 3] = sy + sample->height;
            d[i + 4] = sx + sample->width;
            d[i + 5] = sy + sample->height;
            d[i + 6] = sx + sample->width;
            d[i + 7] = sy;
        }
        break;
    case XHY1:
        for (i = 0; i < 8 * maxMove; i += 8) {
            sx += unit * 0.5 * signX;
            sy += unit * signY;
            d[i] = sx;
            d[i + 1] = sy;
            d[i + 2] = sx;
            d[i + 3] = sy + sample->height;
            d[i + 4] = sx + sample->width;
            d[i + 5] = sy + sample->height;
            d[i + 6] = sx + sample->width;
            d[i + 7] = sy;
        }
        break;
    case X1Y1:
        for (i = 0; i < 8 * maxMove; i += 8) {
            sx += unit * signX;
            sy += unit * signY;
            d[i] = sx;
            d[i + 1] = sy;
            d[i + 2] = sx;
            d[i + 3] = sy + sample->height;
            d[i + 4] = sx + sample->width;
            d[i + 5] = sy + sample->height;
            d[i + 6] = sx + sample->width;
            d[i + 7] = sy;
        }
        break;
    case X1YH:
        for (i = 0; i < 8 * maxMove; i += 8) {
            sx += unit * signX;
            sy += unit * 0.5 * signY;
            d[i] = sx;
            d[i + 1] = sy;
            d[i + 2] = sx;
            d[i + 3] = sy + sample->height;
            d[i + 4] = sx + sample->width;
            d[i + 5] = sy + sample->height;
            d[i + 6] = sx + sample->width;
            d[i + 7] = sy;
        }
        break;
    case X1Y0:
        for (i = 0; i < 8 * maxMove; i += 8) {
            sx += unit * signX;
            d[i] = sx;
            d[i + 1] = sy;
            d[i + 2] = sx;
            d[i + 3] = sy + sample->height;
            d[i + 4] = sx + sample->width;
            d[i + 5] = sy + sample->height;
            d[i + 6] = sx + sample->width;
            d[i + 7] = sy;
        }
        break;
    }
    free(sample);
    return d;
}

void move()
{
    if (!upDownPress()) {
        if (upPress == GL_TRUE && y + sideLen <= 1.0)
            y += 2 * unit;
        else if (downPress == GL_TRUE && y >= -1.0)
            y -= 2 * unit;
    }
    if (!rightLeftPress()) {
        if (rightPress == GL_TRUE && x + sideLen <= 1.0)
            x += 2 * unit;
        else if (leftPress == GL_TRUE && x >= -1.0)
            x -= 2 * unit;
    }
    glutPostRedisplay();
    Sleep(DELAY);
}

void arrowPress(int key, int x, int y)
{
    switch (key) {
    case GLUT_KEY_UP:
        upPress = GL_TRUE;
        break;
    case GLUT_KEY_DOWN:
        downPress = GL_TRUE;
        break;
    case GLUT_KEY_RIGHT:
        rightPress = GL_TRUE;
        break;
    case GLUT_KEY_LEFT:
        leftPress = GL_TRUE;
        break;
    }
}

void arrowUp(int key, int x, int y)
{
    switch (key) {
    case GLUT_KEY_UP:
        upPress = GL_FALSE;
        break;
    case GLUT_KEY_DOWN:
        downPress = GL_FALSE;
        break;
    case GLUT_KEY_RIGHT:
        rightPress = GL_FALSE;
        break;
    case GLUT_KEY_LEFT:
        leftPress = GL_FALSE;
        break;
    }
}

bool upDownPress()
{
    if (upPress == GL_TRUE && downPress == GL_TRUE)
        return true;
    return false;
}
bool rightLeftPress()
{
    if (rightPress == GL_TRUE && leftPress == GL_TRUE)
        return true;
    return false;
}

GLdouble dRand(GLdouble min, GLdouble max)
{
    return ((GLdouble)rand() / (GLdouble)RAND_MAX) * (max - min) + min;
}

int iRand(int min, int max)
{
    return rand() % (max + 1 - min) + min;
}
我逐行运行调试器,程序在执行glutMainLoop()时完全停止。调试器然后向我显示已反汇编的opengl32库。在这里,我打开了“调用堆栈”,它如上所述

所有问题都解决了

谢谢大家的回答

这是一个非常有用的建议,我应该使用向量而不是原始指针来进行更安全的内存分配。下面是这个简单游戏的完整代码。c++11的嵌套lambda函数非常有助于删除代码中的某些重复部分

#include <cstdlib>
#include <ctime>
#include <cmath>
#include <list>
#include <vector>
#include "glut.h"
using namespace std;

#define TOP 0
#define BOTTOM 1
#define RIGHT 2
#define LEFT 3

#define X0Y1 0
#define XHY1 1
#define X1Y1 2
#define X1YH 3
#define X1Y0 4

#define PLUS 1
#define MINUS -1

void display();
vector<double> aniData();
void move();
void arrowPress(int key, int x, int y);
void arrowUp(int key, int x, int y);
bool upDownPress(), rightLeftPress();
double dRand(double min, double max);
int iRand(int min, int max);

const double unit = 1.0 / 1280.0, sideLen = 1.0 / 32.0, maxAttack = 1.0 / 8.0;
const int maxMove = (int)((maxAttack + 2.0) / unit) + 5;
double mvx1 = -sideLen / 2.0, mvy1 = -sideLen / 2.0, mvx2, mvy2, mvx3, mvy3, mvx4, mvy4, avx1, avy1, avx2, avy2, avx3, avy3, avx4, avy4;
GLboolean upPress = GL_FALSE, downPress = GL_FALSE, rightPress = GL_FALSE, leftPress = GL_FALSE;
list<vector<double>> dList;
list<vector<double>>::iterator it;
vector<double>::iterator vit;
int k = -8, attacks = 0;

class Attacker{
public:
    double width, height, ax, ay;
    int side, slope, xSign, ySign;
    Attacker(){
        width = dRand(maxAttack / 8.0, maxAttack);
        height = dRand(maxAttack / 8.0, maxAttack);
        side = iRand(0, 3);
        bool fromOtherSide = false;
        switch (side){
        case TOP:
        topI :
            ax = dRand(-1.0 - unit, 1.0 - width + unit);
             ay = 1.0 + unit;
             if (!fromOtherSide){
                 slope = iRand(0, 4);
                 if (slope == X1Y0){
                     if (iRand(0, 1) == 0){
                         goto rightI;
                     }
                     goto leftI;
                 }
             }
             xSign = 2 * (rand() % 2) - 1;
             ySign = MINUS;
             break;
        case BOTTOM:
        bottomI :
            ax = dRand(-1.0 - unit, 1.0 - width + unit);
                ay = -1.0 - height - unit;
                if (!fromOtherSide){
                    slope = iRand(0, 4);
                    if (slope == X1Y0){
                        if (iRand(0, 1) == 0){
                            goto rightI;
                        }
                        goto leftI;
                    }
                }
                xSign = 2 * (rand() % 2) - 1;
                ySign = PLUS;
                break;
        case RIGHT:
        rightI :
            ax = 1.0 + unit;
               ay = dRand(-1.0 - unit, 1.0 - height + unit);
               if (!fromOtherSide){
                   slope = iRand(0, 4);
                   if (slope == X0Y1){
                       if (iRand(0, 1) == 0){
                           goto topI;
                       }
                       goto bottomI;
                   }
               }
               xSign = MINUS;
               ySign = 2 * (rand() % 2) - 1;
               break;
        case LEFT:
        leftI :
            ax = -1.0 - width - unit;
              ay = dRand(-1.0 - unit, 1.0 - height + unit);
              if (!fromOtherSide){
                  slope = iRand(0, 4);
                  if (slope == X0Y1){
                      if (iRand(0, 1) == 0){
                          goto topI;
                      }
                      goto bottomI;
                  }
              }
              xSign = PLUS;
              ySign = 2 * (rand() % 2) - 1;
              break;
        }
    }
};

int main(){
    srand((unsigned)time(NULL));
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(800, 650);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("닷지비스한거 1.0");
    glutDisplayFunc(display);
    glutSpecialFunc(arrowPress);
    glutSpecialUpFunc(arrowUp);
    glutIdleFunc(move);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glutMainLoop();
    return 0;
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3d(0.0, 1.0, 0.0);
    mvx2 = mvx1;
    mvy2 = mvy1 + sideLen;
    mvx3 = mvx1 + sideLen;
    mvy3 = mvy1 + sideLen;
    mvx4 = mvx1 + sideLen;
    mvy4 = mvy1;
    glBegin(GL_POLYGON);
    glVertex2d(mvx1, mvy1);
    glVertex2d(mvx2, mvy2);
    glVertex2d(mvx3, mvy3);
    glVertex2d(mvx4, mvy4);
    glEnd();
    if (k >= 8 * maxMove){
        k = 0;
        for (int i = 0; i < attacks; ++i){
            dList.erase(dList.begin());
        }
        ++attacks;
        for (int i = 0; i < attacks; ++i){
            dList.push_back(aniData());
        }
    }
    it = dList.begin();
    glColor3d(1.0, 0.0, 1.0);
    for (int i = 0; i < attacks; ++i){
        vit = it->begin();
        avx1 = *(vit + k);
        avy1 = *(vit + k + 1);
        avx2 = *(vit + k + 2);
        avy2 = *(vit + k + 3);
        avx3 = *(vit + k + 4);
        avy3 = *(vit + k + 5);
        avx4 = *(vit + k + 6);
        avy4 = *(vit + k + 7);
        glBegin(GL_POLYGON);
        glVertex2d(avx1, avy1);
        glVertex2d(avx2, avy2);
        glVertex2d(avx3, avy3);
        glVertex2d(avx4, avy4);
        glEnd();
        it++;
        if ((mvx1 >= avx1 && mvx1 <= avx3 && mvy1 >= avy1 && mvy1 <= avy3)
        || (mvx2 >= avx1 && mvx2 <= avx3 && mvy2 >= avy1 && mvy2 <= avy3)
        || (mvx3 >= avx1 && mvx3 <= avx3 && mvy3 >= avy1 && mvy3 <= avy3)
        || (mvx4 >= avx1 && mvx4 <= avx3 && mvy4 >= avy1 && mvy4 <= avy3)
        || (avx1 >= mvx1 && avx1 <= mvx3 && avy1 >= mvy1 && avy1 <= mvy3)
        || (avx2 >= mvx1 && avx2 <= mvx3 && avy2 >= mvy1 && avy2 <= mvy3)
        || (avx3 >= mvx1 && avx3 <= mvx3 && avy3 >= mvy1 && avy3 <= mvy3)
        || (avx4 >= mvx1 && avx4 <= mvx3 && avy4 >= mvy1 && avy4 <= mvy3)){
            exit(0);
        }
    }
    k += 8;
    glutSwapBuffers();
}

vector<double> aniData(){
    vector<double> v;
    Attacker sample;
    double sx = sample.ax, sy = sample.ay, sh = sample.height, sw = sample.width;
    int i, signX = sample.xSign, signY = sample.ySign;
    auto arrange = [&]{
        v.push_back(sx);
        v.push_back(sy);
        v.push_back(sx);
        v.push_back(sy + sh);
        v.push_back(sx + sw);
        v.push_back(sy + sh);
        v.push_back(sx + sw);
        v.push_back(sy);
    };
    switch (sample.slope){
    case X0Y1:
        for (i = 0; i < maxMove; ++i){
            sy += (double)(unit*signY);
            arrange();
        }
        break;
    case XHY1:
        for (i = 0; i < maxMove; ++i){
            sx += 0.5*(double)(unit*signX);
            sy += (double)(unit*signY);
            arrange();
        }
        break;
    case X1Y1:
        for (i = 0; i < maxMove; ++i){
            sx += (double)(unit*signX);
            sy += (double)(unit*signY);
            arrange();
        }
        break;
    case X1YH:
        for (i = 0; i < maxMove; ++i){
            sx += (double)(unit*signX);
            sy += 0.5*(double)(unit*signY);
            arrange();
        }
        break;
    case X1Y0:
        for (i = 0; i < maxMove; ++i){
            sx += (double)(unit*signX);
            arrange();
        }
        break;
    }
    return v;
}

void move(){
    if (!upDownPress()){
        if (upPress == GL_TRUE && mvy1 + sideLen <= 1.0){
            mvy1 += 0.8 * unit;
        }
        else if (downPress == GL_TRUE && mvy1 >= -1.0){
            mvy1 -= 0.8 * unit;
        }
    }
    if (!rightLeftPress()){
        if (rightPress == GL_TRUE && mvx1 + sideLen <= 1.0){
            mvx1 += 0.8 * unit;
        }
        else if (leftPress == GL_TRUE && mvx1 >= -1.0){
            mvx1 -= 0.8 * unit;
        }
    }
    glutPostRedisplay();
}

void arrowPress(int key, int x, int y){
    switch (key){
    case GLUT_KEY_UP:
        upPress = GL_TRUE;
        break;
    case GLUT_KEY_DOWN:
        downPress = GL_TRUE;
        break;
    case GLUT_KEY_RIGHT:
        rightPress = GL_TRUE;
        break;
    case GLUT_KEY_LEFT:
        leftPress = GL_TRUE;
        break;
    }
}

void arrowUp(int key, int x, int y){
    switch (key){
    case GLUT_KEY_UP:
        upPress = GL_FALSE;
        break;
    case GLUT_KEY_DOWN:
        downPress = GL_FALSE;
        break;
    case GLUT_KEY_RIGHT:
        rightPress = GL_FALSE;
        break;
    case GLUT_KEY_LEFT:
        leftPress = GL_FALSE;
        break;
    }
}

bool upDownPress(){
    if (upPress == GL_TRUE && downPress == GL_TRUE){
        return true;
    }
    return false;
}
bool rightLeftPress(){
    if (rightPress == GL_TRUE && leftPress == GL_TRUE){
        return true;
    }
    return false;
}

double dRand(double min, double max){
    return ((double)rand() / (double)RAND_MAX)*(max - min) + min;
}

int iRand(int min, int max){
    return rand() % (max + 1 - min) + min;
}
#包括
#包括
#包括
#包括
#包括
#包括“glut.h”
使用名称空间std;
#定义前0名
#定义底部1
#定义权利2
#定义左3
#定义X0Y1 0
#定义XHY1 1
#定义X1Y12
#定义X1YH 3
#定义X1Y0 4
#定义+1
#定义负-1
void display();
向量数据();
无效移动();
空箭头按(整数键,整数x,整数y);
无效箭头向上(整数键、整数x、整数y);
bool upDownPress(),rightLeftPress();
双阻力(双最小值,双最大值);
int-iRand(int-min,int-max);
常数双单位=1.0/1280.0,边距=1.0/32.0,最大攻击=1.0/8.0;
常量int maxMove=(int)((maxAttack+2.0)/单位)+5;
双mvx1=-sideLen/2.0,mvy1=-sideLen/2.0,mvx2,mvy2,mvx3,mvy3,mvx4,mvy4,avx1,avy1,avx2,avy2,avx3,avy3,avx4,avy4;
GLboolean upPress=GL\u FALSE,downPress=GL\u FALSE,rightPress=GL\u FALSE,leftPress=GL\u FALSE;
名单;
列表::迭代器;
向量::迭代器vit;
int k=-8,攻击=0;
类攻击者{
公众:
双倍宽度、高度、ax、ay;
内侧、坡度、xSign、ySign;
攻击者(){
宽度=德朗(maxAttack/8.0,maxAttack);
高度=德朗(maxAttack/8.0,maxAttack);
侧面=伊朗(0,3);
bool fromOtherSide=假;
开关(侧){
案例顶部:
topI:
ax=德朗(-1.0-单位,1.0-宽度+单位);
ay=1.0+单位;
如果(!从另一侧){
斜率=iRand(0,4);
如果(斜率=X1Y0){
if(iRand(0,1)=0){
右后藤;
}
左后藤;
}
}
xSign=2*(rand()%2)-1;
ySign=负;
打破
箱底:
我:
ax=德朗(-1.0-单位,1.0-宽度+单位);
ay=-1.0-高度-单位;
如果(!从另一侧){
斜率=iRand(0,4);
如果(斜率=X1Y0){
if(iRand(0,1)=0){
右后藤;
}
左后藤;
}
}
xSign=2*(rand()%2)-1;
ySign=PLUS;
打破
案例权利:
对:
ax=1.0+单位;
ay=德朗(-1.0-单位,1.0-高度+单位);
如果(!从另一侧){
斜率=iRand(0,4);
如果(斜率=X0Y1){
if(iRand(0,1)=0){
后藤岛;
}
后藤巴托米;
}
}
xSign=负;
ySign=2*(rand()%2)-1;
打破
案例左:
左撇子:
ax=-1.0-宽度-单位;
ay=德朗(-1.0-单位,1.0-高度+单位);
如果(!从另一侧){
斜率=iRand(0,4);
如果(斜率=X0Y1){
if(iRand(0,1)=0){
后藤岛;
}
后藤巴托米;
}
}
xSign=PLUS;
ySign=2*(rand()%2)-1;
打破
}
}
};
int main(){
srand((无符号)时间(NULL));
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800650);
glutInitWindowPosition(0,0);
创建窗口(“닷지비스한거 1.0");
glutDisplayFunc(显示器);
glutSpecialFunc(箭头压力机);
lupfunc(箭头向上);
glutIdleFunc(移动);
glClearColor(0.0,0.0,0.0,0.0);
glutMainLoop();
返回0;
}
无效显示(){
glClear(GLU颜色缓冲位);
glColor3d(0.0,1.0,0.0);
mvx2=mvx1;
mvy2=mvy1+sideLen;
mvx3=mvx1+sideLen;
mvy3=mvy1+sideLen;
mvx4=mvx1+sideLen;
mvy4=mvy1;
glBegin(GL_多边形);
glVertex2d(mvx1,mvy1);
glVertex2d(mvx2,mvy2);
glVertex2d(mvx3,mvy3);
glVertex2d(mvx4,mvy4);
格伦德();
如果(k>=8*maxMo
-> OPENGL32! 5e66e696()
OPENGL32! 5e6c1399()
OPENGL32! 5e6bd671()
OPENGL32! 5e6bd74a()
OPENGL32! 5e6ac7fc()
OPENGL32! 5e6ae640()
OPENGL32! 5e6cd115()
OPENGL32! 5e64c5c0()
GDI32! 77e56443()
GLUT32! 10009e55()
display() line 171
GLUT32! 100050c2()
GLUT32! 100048d6()
main() line 137
mainCRTStartup() line 206 + 25 bytes
KERNEL32! 7c7e7077()
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <list>
#include <vector>
#include "glut.h"
using namespace std;

#define TOP 0
#define BOTTOM 1
#define RIGHT 2
#define LEFT 3

#define X0Y1 0
#define XHY1 1
#define X1Y1 2
#define X1YH 3
#define X1Y0 4

#define PLUS 1
#define MINUS -1

void display();
vector<double> aniData();
void move();
void arrowPress(int key, int x, int y);
void arrowUp(int key, int x, int y);
bool upDownPress(), rightLeftPress();
double dRand(double min, double max);
int iRand(int min, int max);

const double unit = 1.0 / 1280.0, sideLen = 1.0 / 32.0, maxAttack = 1.0 / 8.0;
const int maxMove = (int)((maxAttack + 2.0) / unit) + 5;
double mvx1 = -sideLen / 2.0, mvy1 = -sideLen / 2.0, mvx2, mvy2, mvx3, mvy3, mvx4, mvy4, avx1, avy1, avx2, avy2, avx3, avy3, avx4, avy4;
GLboolean upPress = GL_FALSE, downPress = GL_FALSE, rightPress = GL_FALSE, leftPress = GL_FALSE;
list<vector<double>> dList;
list<vector<double>>::iterator it;
vector<double>::iterator vit;
int k = -8, attacks = 0;

class Attacker{
public:
    double width, height, ax, ay;
    int side, slope, xSign, ySign;
    Attacker(){
        width = dRand(maxAttack / 8.0, maxAttack);
        height = dRand(maxAttack / 8.0, maxAttack);
        side = iRand(0, 3);
        bool fromOtherSide = false;
        switch (side){
        case TOP:
        topI :
            ax = dRand(-1.0 - unit, 1.0 - width + unit);
             ay = 1.0 + unit;
             if (!fromOtherSide){
                 slope = iRand(0, 4);
                 if (slope == X1Y0){
                     if (iRand(0, 1) == 0){
                         goto rightI;
                     }
                     goto leftI;
                 }
             }
             xSign = 2 * (rand() % 2) - 1;
             ySign = MINUS;
             break;
        case BOTTOM:
        bottomI :
            ax = dRand(-1.0 - unit, 1.0 - width + unit);
                ay = -1.0 - height - unit;
                if (!fromOtherSide){
                    slope = iRand(0, 4);
                    if (slope == X1Y0){
                        if (iRand(0, 1) == 0){
                            goto rightI;
                        }
                        goto leftI;
                    }
                }
                xSign = 2 * (rand() % 2) - 1;
                ySign = PLUS;
                break;
        case RIGHT:
        rightI :
            ax = 1.0 + unit;
               ay = dRand(-1.0 - unit, 1.0 - height + unit);
               if (!fromOtherSide){
                   slope = iRand(0, 4);
                   if (slope == X0Y1){
                       if (iRand(0, 1) == 0){
                           goto topI;
                       }
                       goto bottomI;
                   }
               }
               xSign = MINUS;
               ySign = 2 * (rand() % 2) - 1;
               break;
        case LEFT:
        leftI :
            ax = -1.0 - width - unit;
              ay = dRand(-1.0 - unit, 1.0 - height + unit);
              if (!fromOtherSide){
                  slope = iRand(0, 4);
                  if (slope == X0Y1){
                      if (iRand(0, 1) == 0){
                          goto topI;
                      }
                      goto bottomI;
                  }
              }
              xSign = PLUS;
              ySign = 2 * (rand() % 2) - 1;
              break;
        }
    }
};

int main(){
    srand((unsigned)time(NULL));
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(800, 650);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("닷지비스한거 1.0");
    glutDisplayFunc(display);
    glutSpecialFunc(arrowPress);
    glutSpecialUpFunc(arrowUp);
    glutIdleFunc(move);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glutMainLoop();
    return 0;
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3d(0.0, 1.0, 0.0);
    mvx2 = mvx1;
    mvy2 = mvy1 + sideLen;
    mvx3 = mvx1 + sideLen;
    mvy3 = mvy1 + sideLen;
    mvx4 = mvx1 + sideLen;
    mvy4 = mvy1;
    glBegin(GL_POLYGON);
    glVertex2d(mvx1, mvy1);
    glVertex2d(mvx2, mvy2);
    glVertex2d(mvx3, mvy3);
    glVertex2d(mvx4, mvy4);
    glEnd();
    if (k >= 8 * maxMove){
        k = 0;
        for (int i = 0; i < attacks; ++i){
            dList.erase(dList.begin());
        }
        ++attacks;
        for (int i = 0; i < attacks; ++i){
            dList.push_back(aniData());
        }
    }
    it = dList.begin();
    glColor3d(1.0, 0.0, 1.0);
    for (int i = 0; i < attacks; ++i){
        vit = it->begin();
        avx1 = *(vit + k);
        avy1 = *(vit + k + 1);
        avx2 = *(vit + k + 2);
        avy2 = *(vit + k + 3);
        avx3 = *(vit + k + 4);
        avy3 = *(vit + k + 5);
        avx4 = *(vit + k + 6);
        avy4 = *(vit + k + 7);
        glBegin(GL_POLYGON);
        glVertex2d(avx1, avy1);
        glVertex2d(avx2, avy2);
        glVertex2d(avx3, avy3);
        glVertex2d(avx4, avy4);
        glEnd();
        it++;
        if ((mvx1 >= avx1 && mvx1 <= avx3 && mvy1 >= avy1 && mvy1 <= avy3)
        || (mvx2 >= avx1 && mvx2 <= avx3 && mvy2 >= avy1 && mvy2 <= avy3)
        || (mvx3 >= avx1 && mvx3 <= avx3 && mvy3 >= avy1 && mvy3 <= avy3)
        || (mvx4 >= avx1 && mvx4 <= avx3 && mvy4 >= avy1 && mvy4 <= avy3)
        || (avx1 >= mvx1 && avx1 <= mvx3 && avy1 >= mvy1 && avy1 <= mvy3)
        || (avx2 >= mvx1 && avx2 <= mvx3 && avy2 >= mvy1 && avy2 <= mvy3)
        || (avx3 >= mvx1 && avx3 <= mvx3 && avy3 >= mvy1 && avy3 <= mvy3)
        || (avx4 >= mvx1 && avx4 <= mvx3 && avy4 >= mvy1 && avy4 <= mvy3)){
            exit(0);
        }
    }
    k += 8;
    glutSwapBuffers();
}

vector<double> aniData(){
    vector<double> v;
    Attacker sample;
    double sx = sample.ax, sy = sample.ay, sh = sample.height, sw = sample.width;
    int i, signX = sample.xSign, signY = sample.ySign;
    auto arrange = [&]{
        v.push_back(sx);
        v.push_back(sy);
        v.push_back(sx);
        v.push_back(sy + sh);
        v.push_back(sx + sw);
        v.push_back(sy + sh);
        v.push_back(sx + sw);
        v.push_back(sy);
    };
    switch (sample.slope){
    case X0Y1:
        for (i = 0; i < maxMove; ++i){
            sy += (double)(unit*signY);
            arrange();
        }
        break;
    case XHY1:
        for (i = 0; i < maxMove; ++i){
            sx += 0.5*(double)(unit*signX);
            sy += (double)(unit*signY);
            arrange();
        }
        break;
    case X1Y1:
        for (i = 0; i < maxMove; ++i){
            sx += (double)(unit*signX);
            sy += (double)(unit*signY);
            arrange();
        }
        break;
    case X1YH:
        for (i = 0; i < maxMove; ++i){
            sx += (double)(unit*signX);
            sy += 0.5*(double)(unit*signY);
            arrange();
        }
        break;
    case X1Y0:
        for (i = 0; i < maxMove; ++i){
            sx += (double)(unit*signX);
            arrange();
        }
        break;
    }
    return v;
}

void move(){
    if (!upDownPress()){
        if (upPress == GL_TRUE && mvy1 + sideLen <= 1.0){
            mvy1 += 0.8 * unit;
        }
        else if (downPress == GL_TRUE && mvy1 >= -1.0){
            mvy1 -= 0.8 * unit;
        }
    }
    if (!rightLeftPress()){
        if (rightPress == GL_TRUE && mvx1 + sideLen <= 1.0){
            mvx1 += 0.8 * unit;
        }
        else if (leftPress == GL_TRUE && mvx1 >= -1.0){
            mvx1 -= 0.8 * unit;
        }
    }
    glutPostRedisplay();
}

void arrowPress(int key, int x, int y){
    switch (key){
    case GLUT_KEY_UP:
        upPress = GL_TRUE;
        break;
    case GLUT_KEY_DOWN:
        downPress = GL_TRUE;
        break;
    case GLUT_KEY_RIGHT:
        rightPress = GL_TRUE;
        break;
    case GLUT_KEY_LEFT:
        leftPress = GL_TRUE;
        break;
    }
}

void arrowUp(int key, int x, int y){
    switch (key){
    case GLUT_KEY_UP:
        upPress = GL_FALSE;
        break;
    case GLUT_KEY_DOWN:
        downPress = GL_FALSE;
        break;
    case GLUT_KEY_RIGHT:
        rightPress = GL_FALSE;
        break;
    case GLUT_KEY_LEFT:
        leftPress = GL_FALSE;
        break;
    }
}

bool upDownPress(){
    if (upPress == GL_TRUE && downPress == GL_TRUE){
        return true;
    }
    return false;
}
bool rightLeftPress(){
    if (rightPress == GL_TRUE && leftPress == GL_TRUE){
        return true;
    }
    return false;
}

double dRand(double min, double max){
    return ((double)rand() / (double)RAND_MAX)*(max - min) + min;
}

int iRand(int min, int max){
    return rand() % (max + 1 - min) + min;
}