Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ VS2013中使用GLEW和GLFW的大量看似无关的错误_C++_Opengl_Glew_Glfw - Fatal编程技术网

C++ VS2013中使用GLEW和GLFW的大量看似无关的错误

C++ VS2013中使用GLEW和GLFW的大量看似无关的错误,c++,opengl,glew,glfw,C++,Opengl,Glew,Glfw,好的,首先,我确信我已经正确地链接了GLFW,而且我几乎可以肯定GLEW,因为我已经能够使用这两个函数运行示例代码。尽管如此,到目前为止,我已经收到了很多关于GLEW窗口是一个未声明的标识符的错误,以及许多其他看起来不相关的错误。据我所知,pixel.h(制作一个落沙游戏,这个类描述元素的像素)与问题无关,只是main.cpp和render.h。是的,我知道将代码放在头中是一种不好的做法,我计划很快将其移动 错误: 1>------ Build started: Project: Sand

好的,首先,我确信我已经正确地链接了GLFW,而且我几乎可以肯定GLEW,因为我已经能够使用这两个函数运行示例代码。尽管如此,到目前为止,我已经收到了很多关于GLEW窗口是一个未声明的标识符的错误,以及许多其他看起来不相关的错误。据我所知,
pixel.h
(制作一个落沙游戏,这个类描述元素的像素)与问题无关,只是
main.cpp
render.h
。是的,我知道将代码放在头中是一种不好的做法,我计划很快将其移动

错误:

1>------ Build started: Project: Sands of the Pixels, Configuration: Release Win32 ------
1>  main.cpp
1>c:\users\spng453\documents\visual studio 2013\projects\sands of the pixels\sands of the pixels\render.h(41): error C2065: 'window' : undeclared identifier
1>c:\users\spng453\documents\visual studio 2013\projects\sands of the pixels\sands of the pixels\render.h(41): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\spng453\documents\visual studio 2013\projects\sands of the pixels\sands of the pixels\render.h(41): error C2365: 'glfwSwapBuffers' : redefinition; previous definition was 'function'
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\GL/glfw3.h(2209) : see declaration of 'glfwSwapBuffers'
1>c:\users\spng453\documents\visual studio 2013\projects\sands of the pixels\sands of the pixels\render.h(42): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\spng453\documents\visual studio 2013\projects\sands of the pixels\sands of the pixels\render.h(42): error C2556: 'int glfwPollEvents(void)' : overloaded function differs only by return type from 'void glfwPollEvents(void)'
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\GL/glfw3.h(1711) : see declaration of 'glfwPollEvents'
1>c:\users\spng453\documents\visual studio 2013\projects\sands of the pixels\sands of the pixels\render.h(42): error C2371: 'glfwPollEvents' : redefinition; different basic types
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\GL/glfw3.h(1711) : see declaration of 'glfwPollEvents'
1>c:\users\spng453\documents\visual studio 2013\projects\sands of the pixels\sands of the pixels\render.h(43): error C2059: syntax error : '}'
1>c:\users\spng453\documents\visual studio 2013\projects\sands of the pixels\sands of the pixels\render.h(43): error C2143: syntax error : missing ';' before '}'
1>main.cpp(18): error C2143: syntax error : missing ';' before '{'
1>main.cpp(18): error C2447: '{' : missing function header (old-style formal list?)
1>main.cpp(32): error C2065: 'error_callback' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
main.cpp:

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw3.h>
#include <iostream>
#include <string>
#include <vector>
const std::string VERSION = "0.0.0 not even alpha";
const std::string TITLE = "Sands of the Pixels " + VERSION;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
#include "pixel.h"
#include "render.h"



void error_callback(int error, const char* description)
{
    fputs(description, stderr);
}

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}

int main() {
    generateWorld(); //generate the world before anything

    //start to do a bunch of opengl stuff
    glfwSetErrorCallback(error_callback);
    if (!glfwInit())
    {
        fputs("Failed to initialize GLFW\n", stderr);
        return -1;
    }
    GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, TITLE.c_str(), NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    glewExperimental = true; // Needed in core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetKeyCallback(window, key_callback);
    setUpOpenGL();

    while (!glfwWindowShouldClose(window))
    {
        render(window, world);
    }

    Pixel pix = Pixel(0, 0);
    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
const std::string VERSION=“0.0.0甚至不是alpha”;
const std::string TITLE=“像素的沙子”+版本;
屏幕宽度=640;
屏幕上的常数=480;
#包括“像素.h”
#包括“render.h”
void error_回调(int error,const char*description)
{
FPUT(描述、标准);
}
静态无效键_回调(GLFWwindow*窗口、int键、int扫描码、int操作、int mods)
{
如果(键==GLFW\U键\转义和操作==GLFW\U按)
glfwSetWindowShouldClose(窗口,GL_TRUE);
}
int main(){
generateWorld();//首先生成世界
//开始做一些opengl的东西
glfwSetErrorCallback(错误回调);
如果(!glfwInit())
{
fputs(“未能初始化GLFW\n”,stderr);
返回-1;
}
GLFWwindow*window=glfwCreateWindow(屏幕宽度,屏幕高度,TITLE.c_str(),NULL,NULL);
如果(!窗口)
{
glfwTerminate();
返回-1;
}
glewExperimental=true;//核心配置文件中需要
如果(glewInit()!=GLEW\u确定){
fprintf(stderr,“未能初始化GLEW\n”);
返回-1;
}
glfwMakeContextCurrent(窗口);
glfwSetKeyCallback(窗口、键回调);
setUpOpenGL();
而(!glfwWindowShouldClose(窗口))
{
渲染(窗口、世界);
}
像素pix=像素(0,0);
GLFW窗口(窗口);
glfwTerminate();
返回0;
}
像素h:

const int PIXEL_WIDTH = 10;
const int PIXEL_HEIGHT = 10;

enum Pixel_Types {
    AIR,
    DIRT,
    STONE
};

class Pixel
{
    int x, y;
    Pixel_Types type = AIR;
    public:
        Pixel() {}
        Pixel(int temp_x, int temp_y) : x(temp_x), y(temp_y) {}
        int getX() { return x; }
        int getY() { return y; }
        void setDeltaX(int temp_delta_x) { x += temp_delta_x; }
        void setDeltaY(int temp_delta_y) { x += temp_delta_y; }
        Pixel_Types getType() { return type; }
        void setTypeWithoutAddingToDrawableWorld(Pixel_Types temporary_pixel_type) {
            type = what_am_i_doing_with_my_life;
        }


};

std::vector<Pixel> world; //the world is a dynamically allocated thing
std::vector<Pixel> drawableWorld; //this is the portion of the world that you can draw. its cool, i know.

void setType(Pixel_Types temp_type, Pixel* this_pixel)  {
            this_pixel->setTypeWithoutAddingToDrawableWorld(temp_type);
            if (temp_type != AIR) {
                drawableWorld.push_back(*this_pixel);
            }
        }
Pixel* getPixelFromCoordinates(int x, int y)
{
    for (int pixel_index = 0; pixel_index < world.size(); pixel_index++) {
        if (world.at(pixel_index).getX() == x) {
            if (world.at(pixel_index).getY() == y) {
                return &world.at(pixel_index);
            }
        }
    }
}

void generateWorld()
{
    for (int world_generation_index = 0; world_generation_index < 4096; world_generation_index++) {
        int x = world_generation_index % 64; //the world is 64 pixels left and right, and 64 up and down. this math is pretty easy and just extrapolates that.
        std::cout << "X: " << x << std::endl;
        int y = floor(world_generation_index / 64); //both x and y start at 0
        std::cout << "Y: " << y << std::endl << std::endl;
        world.push_back(Pixel(x*PIXEL_WIDTH, y*PIXEL_HEIGHT));
        if (y <= 32) {
            setType(STONE, getPixelFromCoordinates(x, y));
        }
    }
}
const int PIXEL_WIDTH=10;
const int PIXEL_HEIGHT=10;
枚举像素类型{
空气
污垢
石
};
类像素
{
int x,y;
像素类型=空气;
公众:
像素(){}
像素(int temp_x,int temp_y):x(temp_x),y(temp_y){
int getX(){return x;}
int getY(){return y;}
void setDeltaX(int temp_delta_x){x+=temp_delta_x;}
void setDeltaY(int temp_delta_y){x+=temp_delta_y;}
像素类型getType(){return type;}
void settypewithout添加到绘图世界(像素类型临时像素类型){
类型=我在用我的生命做什么;
}
};
向量世界//世界是一个动态分配的东西
std::矢量绘图世界//这是世界上你可以画的部分。很酷,我知道。
无效设置类型(像素类型临时类型,像素*此像素){
此像素->设置类型不添加到绘图世界(临时类型);
如果(温度类型!=空气){
drawableWorld.push_back(*此_像素);
}
}
像素*getPixelFromCoordinates(整数x,整数y)
{
对于(int pixel_index=0;pixel_indexstd::cout我发现了问题。将我的代码与GLFW提供的示例代码进行比较后,我发现我完全忘记调用
glViewport()
。这可能是由于我无意中以某种方式删除了它。

pixel.h
在哪里?为什么
render.h
没有一个合适的
#include
s?render中需要的所有内容。h包含在main.cpp中(也称为glew和glfw)如果需要的话,我可以添加pixel.h。那么,你说的包含保护是什么意思?好吧,我理解现在没有包含保护的错误所在,但是在代码中没有其他地方包含渲染。h是包含保护的。所以,这不会对代码产生影响,至少目前是这样。测试后,我发现这个问题存在于
main.cpp
中的导入中。我将进一步研究这个问题。
void setUpOpenGL() {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, 1.0, -1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix();

    glClearColor(0.f, 0.f, 0.f, 1.f);
}

void render(GLFWwindow* window, std::vector<Pixel> world) {
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW); //just to make sure we are always operating on objects
    glLoadIdentity();

    glTranslatef(SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, 0.f ); //move the "cursor" to the center of the screen

    //Render quads
    for (int pixel_index = 0; pixel_index <= world.size(); pixel_index++) {
        Pixel pixel_being_drawn = world.at(pixel_index);
        //std::cout << "Drawing pixel at [" << pixel_being_drawn.getX() << "," << pixel_being_drawn.getY() << "]\n";
        glBegin(GL_QUADS);
        glColor3f(0.f, 1.f, 0.f);
        glVertex2f(pixel_being_drawn.getX(), SCREEN_HEIGHT-pixel_being_drawn.getY()); //gotta compensate for opengl's swapped y :(
        glVertex2f(pixel_being_drawn.getX()+PIXEL_WIDTH, SCREEN_HEIGHT - pixel_being_drawn.getY());
        glVertex2f(pixel_being_drawn.getX() + PIXEL_WIDTH, (SCREEN_HEIGHT - pixel_being_drawn.getY()) + PIXEL_HEIGHT);
        glVertex2f(pixel_being_drawn.getX(), (SCREEN_HEIGHT - pixel_being_drawn.getY()) + PIXEL_HEIGHT);
        glEnd();
        }
    }
    glfwSwapBuffers(window);
    glfwPollEvents();
}