0

I'm trying to create a Snake game with C++ using Opengl, SDL and GL although I have everything Linked correctly I get a pile of errors when building, I have looked around and found that these errors are pretty common when using GL although I can't seem to pinpoint why I'm getting them as I've taken the precautions like including "windows.h" before including "GL/freeglut.h" can anyone help?

Errors included are:

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

Main 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);
};
im brett
  • 33
  • 2
  • 12
  • 1
    Please edit your question to include the *complete* error output, unedited. Also point out *where* in the code shown you get the errors (for example by adding a comment). Lastly, the errors you do show say something about `glAccum`, but you don't call that anywhere in the code you show. Please try to create a [Minimal, **Complete**, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. – Some programmer dude Mar 24 '16 at 07:31

1 Answers1

1

You're testing for the wrong preprocessor macro. Make it read like this

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

note the underscore in _WIN32. No need to test for _MINGW32_, as the other macro is defined there as well.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • it still doesn't change anything – im brett Mar 24 '16 at 09:31
  • @imbrett: Well the thing the compiler is tripping over is, that the APIENTRY and WINGDI macros (used in gl.h) are missing. They're defined in windows.h, so somewhere in your project you're including gl.h (directly or indirectly) before windows.h and that's causing the trouble. – datenwolf Mar 24 '16 at 09:36
  • If I show you the source code could you make an assumption as to where? – im brett Mar 24 '16 at 10:17
  • 1
    @imbrett: I'd approach this from a different angle: In header files only add includes to what you absolutely require. E.g. except for `Snake.hpp` none of the headers requires GL includes. Similar none of the headers except `Game.hpp` requires the SDL headers. Learn about header guards (an `#ifndef x #define x #endif` block around the header to avoid multiple include collisions). Last but not least, don't implement functions as part of the class declaration (in the header) but implement them in their own compilation unit (cpp file). Following these simple rules normally avoids trouble like this – datenwolf Mar 24 '16 at 16:51