最初的问题是
我有三个班级A
,B
并且C
。A
有一个链接和调用到B
,它有一个链接和调用到C
。但是C
调用了A
静态方法。我就是无法让这个静态调用起作用。
但是人们对我截断代码的一些不准确之处嗤之以鼻,所以类的真实名称是 GameRenderer
,GameView
和FieldView
. GameRenderer
有一个链接和调用到GameView
,它有一个链接和调用到FieldView
。但是FieldView
调用了GameRenderer
静态方法createGlTextureFromResource
。我就是无法让这个静态调用起作用。
这是完整和真实的标题 GameRenderer.h
// GameRenderer.h
#ifndef GAME_RENDERER
#define GAME_RENDERER
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
namespace game{
class GameModel;
class GameView;
class GameRenderer {
jobject* context;
jobject* gameOverHandler;
static JNIEnv* env;
static jobject* jparent;
GameModel* gameModel;
GameView* gameView;
glm::mat4 mProjMatrix;
glm::mat4 mVMatrix;
public:
GameRenderer(jobject* context, jobject* gameOverHandler, JNIEnv* env, jobject* jparent);
void onSurfaceCreated();
void onDrawFrame();
void onSurfaceChanged(int width, int height);
void setMotionDirection();
static int* getImagePixels (int imageId, int width, int height);
static void createGlTextureFromResource (int resourceId, int textureId);
};
}
#endif
游戏视图.h
// GameView.h
#ifndef GAME_VIEW
#define GAME_VIEW
#include <glm/glm.hpp>
namespace game{
class GameModel;
class FieldView;
class GameView {
GameModel* gameModel;
FieldView* fieldView;
public:
GameView(GameModel* gameModel);
void draw(glm::mat4 mProjMatrix, glm::mat4 mVMatrix);
};
}
#endif
FieldView.h
// FieldView.h
#ifndef FIELD_VIEW
#define FIELD_VIEW
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include "GLESUtils.h"
namespace game{
class GameRenderer;
class FieldView {
static const int FLOAT_SIZE_BYTES = 4;
static const int TRIANGLE_VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES;
static const int TRIANGLE_VERTICES_DATA_POS_OFFSET = 0;
static const int TRIANGLE_VERTICES_DATA_UV_OFFSET = 3;
glm::mat4 mMMatrix;
glm::mat4 mMVPMatrix;
int mProgram;
int mTextureID;
int muMVPMatrixHandle;
int maPositionHandle;
int maTextureHandle;
public:
FieldView();
void draw(glm::mat4 mProjMatrix, glm::mat4 mVMatrix);
};
}
#endif
这是 FieldView.cpp 的剪辑版
// FieldView.cpp
#include <jni.h>
#include <string.h>
#include <android/log.h>
#include "FieldView.h"
#include "GameRenderer.h"
#define LOG_TAG "NDK_FieldView"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
namespace game{
static GLfloat mTriangleVerticesData[] = {...};
static const char mVertexShader[] = "...\n";
static const char mFragmentShader[] = "...\n";
FieldView::FieldView() {
mProgram = -1;
mTextureID = -1;
muMVPMatrixHandle = -1;
maPositionHandle = -1;
maTextureHandle = -1;
mProgram = GLESUtils::createProgram(mVertexShader, mFragmentShader);
GLESUtils::checkGlError("createProgram mProgram");
if (mProgram == 0) {
LOGI("program not created exiting");
return;
}
maPositionHandle = glGetAttribLocation(mProgram, "aPosition");
GLESUtils::checkGlError("glGetAttribLocation aPosition");
if (maPositionHandle == -1) {
return;
}
maTextureHandle = glGetAttribLocation(mProgram, "aTextureCoord");
GLESUtils::checkGlError("glGetAttribLocation aTextureCoord");
if (maTextureHandle == -1) {
return;
}
muMVPMatrixHandle = glGetUniformLocation(mProgram, "uMVPMatrix");
GLESUtils::checkGlError("glGetUniformLocation uMVPMatrix");
if (muMVPMatrixHandle == -1) {
return;
}
/*
* Create our texture. This has to be done each time the
* surface is created.
*/
GLuint textures[] = {0};
glGenTextures(1, &textures[0]);
mTextureID = textures[0];
glBindTexture(GL_TEXTURE_2D, mTextureID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// here is the actual problem, I get the following error on this line
// FieldView.cpp: In constructor 'game::FieldView::FieldView()':
// FieldView.cpp:89:9: error: incomplete type 'game::GameRenderer' used in nested name specifier
GameRenderer::createGlTextureFromResource(0x7f040004, mTextureID);
}
void FieldView::draw(glm::mat4 mProjMatrix, glm::mat4 mVMatrix) {
//... doesn't matter
}
}
如果我理解正确,我必须使用前向声明并且至少在一个地方不要使用包含标题来阻止循环,但我不能这样做,因为我在对象之间进行了调用,并且没有标题编译器拒绝这样做调用方法。编译器错误在 中,请参阅调用FieldView.cpp
附近代码中的注释。GameRenderer::createGlTextureFromResource(0x7f040004, mTextureID);
在所有这些编辑问题实际上保持不变之后:我在这里做错了什么?我想我在某处使用了非常糟糕的设计实践。
请原谅我最初的不准确。