gpt4 book ai didi

c++ - SDL/OpenGL 浮点异常

转载 作者:太空宇宙 更新时间:2023-11-04 14:17:45 25 4
gpt4 key购买 nike

我正在用 C++ (SDL/OpenGL) 编写一个简单的乒乓球游戏,但是我在 Ubuntu 上编译时遇到了问题。当我使用 SDL_OPENGL 作为 SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL); 的标志时出现浮点异常它正在用 g++ 编译,它似乎编译使用 Visual C++ 没问题(没有错误)。

这是我的代码:

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

const int WIDTH = 640;
const int HEIGHT = 480;

struct Paddle
{
float x, y, w, h;
Paddle(float a, float b)
{
x = a;
y = b;
w = 10;
h = 80;
}
};

struct Ball
{
float x, y, r;
float velx, vely;
Ball(float a, float b)
{
x = a;
y = b;
r = 7;
velx = (float)0.2;
vely = (float)0.2;
}
};

bool testCollision(Ball b, Paddle p)
{
if (b.x + b.r < p.x) return false;
if (b.x > p.x + p.w) return false;
if (b.y + b.r < p.y) return false;
if (b.y > p.y + p.h) return false;
return true;
}

void init()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption("Pong~~", NULL);
SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL);
glClearColor(0, 0, 0, 1);
//glViewport(0, 0, WIDTH, HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glOrtho(0, WIDTH, HEIGHT, 0, -1, 1);
SDL_GL_SwapBuffers();
//glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char *argv[])
{
init();

Paddle p1(10.0, 80.0);
Paddle p2(620.0, 80.0);
Ball b(50, 50);

bool press[4] = {false, false, false, false};

SDL_Event event;
bool running = true;
while (running)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
running = false;
break;

case SDLK_w:
press[0] = true;
break;

case SDLK_s:
press[1] = true;
break;

case SDLK_UP:
press[2] = true;
break;

case SDLK_DOWN:
press[3] = true;
break;
}
}

else if (event.type == SDL_KEYUP)
{
switch (event.key.keysym.sym)
{
case SDLK_w:
press[0] = false;
break;

case SDLK_s:
press[1] = false;
break;

case SDLK_UP:
press[2] = false;
break;

case SDLK_DOWN:
press[3] = false;
break;
}
}
}

//Logic
if (press[0] == true) p1.y -= (float)0.2;
if (press[1] == true) p1.y += (float)0.2;
if (press[2] == true) p2.y -= (float)0.2;
if (press[3] == true) p2.y += (float)0.2;

if (p1.y < 0) p1.y = 0;
else if (p1.y + p1.h > HEIGHT) p1.y = HEIGHT - p1.h;
if (p2.y < 0) p2.y = 0;
else if (p2.y + p2.h > HEIGHT) p2.y = HEIGHT - p2.h;

b.x += b.velx;
b.y += b.vely;

if (b.x < 0) b.velx = -b.velx;
else if (b.x + b.r > WIDTH) b.velx = -b.velx;
if (b.y < 0) b.vely = -b.vely;
else if (b.y + b.r > HEIGHT) b.vely = -b.vely;

if (testCollision(b, p1) == true) b.velx = -b.velx;
else if (testCollision(b, p2) == true) b.velx = -b.velx;
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();

//Draw ball
glBegin(GL_QUADS);
glColor4ub(255, 5, 255, 255);
glVertex2f(b.x, b.y);
glVertex2f(b.x + b.r, b.y);
glVertex2f(b.x + b.r, b.y + b.r);
glVertex2f(b.x, b.y + b.r);
glEnd();

//Draw paddles
glBegin(GL_QUADS);
glColor4ub(255, 5, 5, 255);
glVertex2f(p1.x, p1.y);
glVertex2f(p1.x + p1.w, p1.y);
glVertex2f(p1.x + p1.w, p1.y + p1.h);
glVertex2f(p1.x, p1.y + p1.h);

glVertex2f(p2.x, p2.y);
glVertex2f(p2.x + p2.w, p2.y);
glVertex2f(p2.x + p2.w, p2.y + p2.h);
glVertex2f(p2.x, p2.y + p2.h);
glEnd();

glPopMatrix();
SDL_GL_SwapBuffers();
}
return 0;
}

最佳答案

[已解决]

首先,我说错了。当我说它不是编译时,它是;该程序实际上没有运行,在 Ubuntu 11.10 的终端中给我一个模棱两可的“浮点异常”错误。在与我的 friend 研究了数小时的问题后,无果,我做了最后一次尝试并更新了我的图形驱动程序。是的,这解决了所有问题。

要更新我的 AMD (fglrx) 图形驱动程序,我只需打开终端并输入:

sudo apt-get install fglrx-updates-dev

继续并重新启动,并激活专有图形驱动程序!

关于c++ - SDL/OpenGL 浮点异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9901577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com