如果我要编写一份我想要完成的工作代码,那就是:
Sprite s1(img_path1);
Sprite s2(img_path2);
Sprite s[] = { s1, s2 }
我想在数组声明的{}
内创建对象,而不是先将对象放入变量然后放入数组中。
我在询问此问题之前检查了this帖子,我将首先澄清一些事项:
-std=c++11
我正在尝试创建一个这样的数组:
Sprite s[] =
{
Sprite(img_path1),
Sprite(img_path2)
};
执行该语句后,s
的内容与使用默认构造函数创建对象数组的内容相同:
Sprite s[2];
问题似乎与默认的复制构造函数无关,因为以下执行完全正常:
sp = Sprite(img_path);
Sprite sp1(sp);
std::cout << sp1.getAspectRatio();
相关代码:
/*
* Sprite.h
*
*/
#ifndef SPRITE_H
#define SPRITE_H
#include <GL/freeglut.h>
#include <FreeImage.h>
#include <iostream>
#include <stdio.h>
class Sprite
{
GLuint texture;
float aspectRatio;
public:
Sprite();
Sprite(std::string path);
Sprite(GLuint texture, float aspectRatio);
float getAspectRatio();
void draw(float x, float y, float alpha, float size);
virtual ~Sprite();
protected:
private:
};
#endif // SPRITE_H
/*
* Sprite.cpp
*
*/
#include "Sprite.h"
Sprite::Sprite()
{
//ctor
}
Sprite::Sprite(std::string file) {
GLuint texture = 0;
int sWidth, sHeight;
if (texture == 0)
{
FIBITMAP* bitmap = FreeImage_Load(
FreeImage_GetFileType(file.c_str(), 0),
file.c_str());
if (bitmap == NULL) {
printf("Could no load image file %s\n", file.c_str());
}
glGenTextures(1, &texture);
printf("%d\n", texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
FIBITMAP *pImage = FreeImage_ConvertTo32Bits(bitmap);
int nWidth = sWidth = FreeImage_GetWidth(pImage);
int nHeight = sHeight = FreeImage_GetHeight(pImage);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, nWidth, nHeight,
0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(pImage));
FreeImage_Unload(pImage);
FreeImage_Unload(bitmap);
}
this->texture = texture;
this->aspectRatio = (float)sWidth / sHeight;
if(texture == 0) {
std::cout << file << std::endl;
}
}
Sprite::Sprite(GLuint t, float as) : texture(t), aspectRatio(as) {}
void Sprite::draw(float x, float y, float alpha, float size) {
float h = size;
float w = size * aspectRatio;
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1.0, 1.0, 1.0, alpha);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(x, y);
glTexCoord2f(0.0f, 1.0f); glVertex2f(x, y + h);
glTexCoord2f(1.0f, 1.0f); glVertex2f(x + w, y + h);
glTexCoord2f(1.0f, 0.0f); glVertex2f(x + w, y);
glEnd();
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
glPopMatrix();
}
float Sprite::getAspectRatio() { return aspectRatio; }
Sprite::~Sprite()
{
//dtor
}
/*
* main.cpp (extract)
*
*/
#include "Globals.h"
#include <GL/freeglut.h>
#include "Sprite.h"
void render();
std::string img_path = "/home/saurabh/Dropbox/Code/C/OpenGL_Game/images/";
Sprite s[] =
{
Sprite(img_path + "gait-right-1.gif"),
Sprite(img_path + "gait-right-0.gif"),
Sprite(img_path + "gait-left-1.gif"),
Sprite(img_path + "gait-left-0.gif"),
Sprite(img_path + "gait-top-1.gif"),
Sprite(img_path + "gait-top-0.gif"),
Sprite(img_path + "gait-bottom-1.gif"),
Sprite(img_path + "gait-bottom-0.gif")
};
int main(int argn, char** argc) {
FreeImage_Initialise();
glutInit(&argn, argc);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(GV.windowWidth, GV.windowHeight);
glutCreateWindow("Monogatari");
glutDisplayFunc(render);
glutIdleFunc(render);
glutMainLoop();
FreeImage_DeInitialise();
}
void render() {
s[0].draw(0,0,1,0.5);
}