簡體   English   中英

使用GLEW和GLFW的VS2013中看似無關的錯誤

[英]Tons of seemingly unrelated errors in VS2013 using GLEW and GLFW

好的,首先,我確信我已正確鏈接了GLFW,並且幾乎可以肯定GLEW,因為我已經能夠使用兩者來運行示例代碼。 但是,最近,我收到了很多關於GLEW窗口是一個未聲明的標識符的錯誤,以及許多其他看似無關的錯誤。 據我所知, pixel.h (制作一個落沙游戲,此類描述元素的像素)與問題無關,僅是main.cpprender.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;
}

pixel.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));
        }
    }
}

最后是render.h:

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();
}

我發現了問題。 在將我的代碼與GLFW提供的示例代碼進行比較之后,我發現我完全忘記了調用glViewport() 這可能是由於我不小心以某種方式將其刪除了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM