使用gl时出现多个错误

时间:2016-03-24 07:28:26

标签: c++ opengl sdl glew freeglut

我试图使用Opengl,SDL和GL创建一个使用C ++的Snake游戏,虽然我已经正确连接所有内容我在构建时遇到了一堆错误,我环顾四周并发现这些错误很常见使用GL虽然我似乎无法确定为什么我得到它们,因为我采取了预防措施,例如包括" windows.h"在包括" GL / freeglut.h"之前有人可以帮忙吗?

错误包括:

error C2144: syntax error : 'void' should be preceded by ';'
Error   C2144   syntax error: 'void' should be preceded by ';'  OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157
Error   C2182   'APIENTRY': illegal use of type 'void'  OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157
Error   C2146   syntax error: missing ';' before identifier 'glAccum'   OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157
Error   C2144   syntax error: 'void' should be preceded by ';'  OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157
Error   C2182   'APIENTRY': illegal use of type 'void'  OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157
Error   C2146   syntax error: missing ';' before identifier 'glAccum'   OpenGLTutorial  c:\program files (x86)\windows kits\8.1\include\um\gl\gl.h  1157

主要cpp

#ifdef __MINGW32__
#include <windows.h>
#endif

#ifdef WIN32
#include <windows.h>
#endif

#include <GL/freeglut.h>
#include <SDL2/SDL.h>
#include <time.h>
#include "SnakeGame.hpp"


int main(void) {

    srand(time(NULL));

    SnakeGame *f = new SnakeGame();
    f->run();
    return 0;
}

Arena.hpp

#ifdef _WIN32
#include <windows.h>
#endif

#include <GL/freeglut.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

#include "Obstacle.hpp"
#include "Fruit.hpp"
#include "Snake.hpp"
#include "Font.hpp"

class Arena {

 public:

  Arena(void);
  virtual ~Arena(void);
  void update(void);
  inline void draw(void);
  Snake *snake;

 private:
  float rot;
  unsigned int score;
  Font *font;
  inline void correct_elements(void);
  void drawRainbowTriangle(void);
  void correct_fruit(void);
  void correct_obstacle(void);
  void correct_snake(void);
  bool is_snake_dead(void);
  bool is_snake_eating(void);
  Obstacle *obstacle;
  Fruit *fruit;
};

Element.hpp

#include <iostream>
#include <ostream>
#include <GL/freeglut.h>
#include <GL/gl.h>

class Element {

public:
  Element();
  virtual void draw() = 0;
  int getX();
  int getY();
  void setX(int it);
  void setY(int it);
  void display();

protected:
  int x;
  int y;
};

Font.hpp

#include <GL/freeglut.h>
#include <string.h>

class Font {

public:
  void bitmap_output(int x, int y, const char *string, void *font = GLUT_BITMAP_TIMES_ROMAN_24);


private:

};

Fruit.hpp

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

#include "Element.hpp"

class Fruit : public Element {

public:
  void draw();
};

Game.hpp

include <SDL2/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>

class Game {

 public:
  Game();
  ~Game();
  virtual void run()=0;
  int get_score();
  void set_score(int s);
  bool paused;
 private:

 protected:
  SDL_Renderer* displayRenderer;
  SDL_Window* displayWindow;
  SDL_RendererInfo displayRendererInfo;
};

Snake.hpp

#include <iostream>
#include <list>
#include <GL/gl.h>
#include <GL/freeglut.h>
#include "Element.hpp"

enum Dir {UP, DOWN, RIGHT, LEFT};

class Body : public Element {

public:
  void draw() {
    glColor3ub(0, 0, 255);
    glPushMatrix();
    glTranslatef(x, y, -y);
    glutSolidCube(1);
    glPopMatrix();
  }
};

class Snake {

public:
  Snake();
  ~Snake();
  void update(void);
  void draw(void);
  void grow(int size);
  void setDir(Dir direction);
  std::list<Body> body;
  std::list<Body>::iterator iter;

private:
  Dir dir;
};

Snake game.hpp

#ifdef _WIN32
#include <windows.h>
#endif

#include <stdlib.h>
#include <SDL2/SDL.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include "Arena.hpp"
#include "Game.hpp"

class SnakeGame : public Game {

 public:
  SnakeGame();
  virtual ~SnakeGame();
  void run();

 private:
  bool is; //is the game running

  Arena arena;
  void keyboard(const SDL_Event &event);
};

1 个答案:

答案 0 :(得分:1)

您正在测试错误的预处理器宏。把它读成这样的

#ifdef _WIN32
#include <windows.h>
#endif

请注意_WIN32中的下划线。无需测试_MINGW32_,因为其他宏也在那里定义。