好的,首先,我确信我已经正确链接了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;
}
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();
}
答案 0 :(得分:2)
我发现了问题。在将我的代码与GLFW提供的示例代码进行比较后,我发现我完全忘记了调用glViewport()
。这可能是因为我以某种方式意外删除了它。