C++ 为什么我的程序要消耗大量内存?
我正在做一个基于层的游戏(以产生视差效果),我需要从每一层裁剪一幅图像来组成一个场景。但有些东西正在积累在记忆中,我不知道是什么。我在代码中填入了C++ 为什么我的程序要消耗大量内存?,c++,image,opengl,memory,C++,Image,Opengl,Memory,我正在做一个基于层的游戏(以产生视差效果),我需要从每一层裁剪一幅图像来组成一个场景。但有些东西正在积累在记忆中,我不知道是什么。我在代码中填入了delete[],这是一个急需解决的问题 ImageClass.h #include <math.h> class Image { public: Image(int w, int h){ width = w; height = h; pixels = new int[w*h*3]; } Ima
delete[]
,这是一个急需解决的问题
ImageClass.h
#include <math.h>
class Image {
public:
Image(int w, int h){
width = w; height = h;
pixels = new int[w*h*3];
}
Image(){}
void setPixel(int a, int r, int g, int b, int x, int y){
pixels[x + y * width] = (a << 24) | (r << 16) | (g << 8) | b;
}
int getPixel(int x, int y){
return pixels[x+y*width];
}
int getWidth(){ return width; }
int getHeight(){ return height; }
int* getPixels() {
return pixels;
}
void composition(int xi, int yi, int *foreground, int fw, int fh) {
for (int i = yi; (i < fh) && (i < height); i++) {
for (int j = xi; (j < fw) && (j < width); j++) {
int aF = (foreground[j + i * fw] >> 24) & 0xff;
int rF = (foreground[j + i * fw] >> 16) & 0xff;
int gF = (foreground[j + i * fw] >> 8) & 0xff;
int bF = (foreground[j + i * fw]) & 0xff;
int rI = (pixels[j + i * width] >> 16) & 0xff;
int gI = (pixels[j + i * width] >> 8) & 0xff;
int bI = (pixels[j + i * width]) & 0xff;
float am = aF / (float)255;
int c1 = (rF * am) + rI * (1 - am);
int c2 = (gF * am) + gI * (1 - am);
int c3 = (bF * am) + bI * (1 - am);
pixels[j + i * width] = (aF << 24) | (c1 << 16) | (c2 << 8) | c3;
}
}
delete[] foreground;
}
private:
int *pixels;
int width, height;
};
#include <fstream>
#include "ImageClass.h"
using namespace std;
class Layer {
private:
Image *img;
float scrollY;
float scrollX;
float ypos;
float xpos;
public:
Layer(){
img = NULL;
scrollX = 0;
scrollY = 0;
ypos = 0;
xpos = 0;
}
Layer(Image *pImg, float sy, float sx, float yp, float xp) {
img = pImg;
scrollX = sx;
scrollY = sy;
ypos = yp;
xpos = xp;
}
Layer(char* path, float sy, float sx, float yp, float xp) {
ifstream arg(path);
if (!arg.is_open()) {
exit(EXIT_FAILURE);
}
char word[10];
//Reading type
arg >> word;
//Reading width
int argWidth;
arg >> word;
argWidth = atoi(word);
//Reading height
int argHeight;
arg >> word;
argHeight = atoi(word);
//Reading max
arg >> word;
//Creating an image with arg size
img = new Image(argWidth, argHeight);
int a, r, g, b;
for (int i = 0; i < argHeight; i++) {
for (int j = 0; j < argWidth; j++) {
arg >> word;
a = atoi(word);
arg >> word;
r = atoi(word);
arg >> word;
g = atoi(word);
arg >> word;
b = atoi(word);
img->setPixel(a, r, g, b, j, i);
}
}
arg.close();
//End of reading
scrollX = sx;
scrollY = sy;
ypos = yp;
xpos = xp;
}
int* subImage(int wp, int hp) {
int* subPixels = new int[wp * hp];
for (int i = 0; (i < hp) && (i < img->getHeight()); i++) {
for (int j = 0; (j < wp) && (j < img->getWidth()); j++) {
subPixels[j + i * wp] = img->getPixel(j + xpos, i + ypos);
}
}
return subPixels;
delete[] subPixels;
}
void horizontalScrolling(bool direction){
//if direction equals true, it goes to the right. Else, left
if (direction) {
xpos += scrollX;
} else {
xpos -= scrollX;
}
}
void verticalScrolling(bool direction){
//if direction equals true, it goes up. Else, down
if (direction) {
ypos -= scrollY;
} else {
ypos += scrollY;
}
}
float getScrollX() {
return scrollX;
}
float getScrollY() {
return scrollY;
}
float getPosX() {
return xpos;
}
float getPosY() {
return ypos;
}
void setPosX(float xp){
xpos = xp;
}
void setPosY(float yp){
ypos = yp;
}
Image* getImage() {
return img;
}
};
#包括
阶级形象{
公众:
图像(整数w,整数h){
宽度=w;高度=h;
像素=新整数[w*h*3];
}
Image(){}
void setPixel(int a、int r、int g、int b、int x、int y){
像素[x+y*宽度]=(a>16)&0xff;
intgf=(前景[j+i*fw]>>8)&0xff;
int bF=(前景[j+i*fw])&0xff;
int rI=(像素[j+i*宽度]>>16)&0xff;
intgi=(像素[j+i*宽度]>>8)&0xff;
int bI=(像素[j+i*宽度])&0xff;
浮点am=aF/(浮点)255;
int c1=(rF*am)+rI*(1-am);
int c2=(gF*am)+gI*(1-am);
INTC3=(bF*am)+bI*(1-am);
像素[j+i*宽度]=(aF>字;
argWidth=atoi(字);
//阅读高度
智力高度;
arg>>单词;
argHeight=atoi(字);
//最大读数
arg>>单词;
//创建具有arg大小的图像
img=新图像(argWidth、argHeight);
int a,r,g,b;
对于(int i=0;i>单词;
a=原子(字);
arg>>单词;
r=atoi(字);
arg>>单词;
g=原子(字);
arg>>单词;
b=原子(字);
img->setPixel(a,r,g,b,j,i);
}
}
arg.close();
//阅读结束
x=sx;
scrollY=sy;
ypos=yp;
xpos=xp;
}
int*子映像(int wp、int hp){
int*子像素=新的int[wp*hp];
对于(inti=0;(igetHeight());i++){
对于(intj=0;(jgetWidth();j++){
亚像素[j+i*wp]=img->getPixel(j+xpos,i+ypos);
}
}
返回子像素;
删除[]个子像素;
}
无效水平滚动(布尔方向){
//如果方向等于真,它就向右,否则就向左
如果(指示){
xpos+=scrollX;
}否则{
xpos-=scrollX;
}
}
无效垂直滚动(布尔方向){
//如果方向等于真,则上升,否则下降
如果(指示){
ypos-=卷轴;
}否则{
ypos+=滚动;
}
}
float getScrollX(){
返回scrollX;
}
float getScrollY(){
返回滚动条;
}
float getPosX(){
返回XPO;
}
float getPosY(){
返回YPO;
}
void setPosX(浮点xp){
xpos=xp;
}
无效设置位置(浮动yp){
ypos=yp;
}
Image*getImage(){
返回img;
}
};
#包括“Layer.h”
#包括
课堂场景{
私人:
整数宽度;
内部高度;
层*层[10];
图像*最终场景;
公众:
场景(int w,int h){
宽度=w;
高度=h;
最终场景=新图像(w,h);
加载层();
}
void loadLayers(){
//加载层0
层[0]=新层(“背景”,1,1.5,0,0);
层[1]=新层(“岛”,5,5,0,0);
}
void mountsecene(){
删除[]最后一期;
最终场景=新图像(宽度、高度);
最终场景->合成(0,0,层[0]->子图像(宽度,高度),宽度,高度);
最终场景->合成(0,0,层[1]->子图像(宽度,高度),宽度,高度);
}
无效场景滚动(布尔方向){
int cont=0;
int layersCreated=2;
while(contgetImage()->getWidth()-width;
int posX=layers[cont]>getPosX();
如果(指示){
if(posX<(delta)){
层[cont]->水平滚动(方向);
}
}否则{
if(posX)
> 0) {
层[cont]->水平滚动(方向);
}
}
cont++;
}
}
无效场景滚动(布尔方向){
int cont=0;
int layersCreated=2;
while(contgetImage()->getHeight()-height;
int posY=layers[cont]->getPosY();
如果(指示){
如果(posY>0){
层[cont]->垂直滚动(方向);
}
}否则{
if(posY垂直滚动(方向);
}
}
cont++;
}
}
int*getScene(){
返回finalScene->getPixels();
}
图层*getLayer(int i){
返回层[i];
}
};
我在代码中填充了delete[],这是一个急需解决的问题
你需要更严格一点。每一个新[]
都需要用一个删除[]来平衡
#include "Layer.h"
#include <math.h>
class Scene {
private:
int width;
int height;
Layer *layers[10];
Image *finalScene;
public:
Scene(int w, int h) {
width = w;
height = h;
finalScene = new Image(w, h);
loadLayers();
}
void loadLayers() {
//loading layer 0
layers[0] = new Layer("Background", 1, 1.5, 0, 0);
layers[1] = new Layer("Island", 5, 5, 0, 0);
}
void mountScene() {
delete[] finalScene;
finalScene = new Image(width, height);
finalScene->composition(0, 0, layers[0]->subImage(width, height), width, height);
finalScene->composition(0, 0, layers[1]->subImage(width, height), width, height);
}
void sceneHScrolling(bool direction) {
int cont = 0;
int layersCreated = 2;
while (cont < layersCreated) {
int delta = layers[cont]->getImage()->getWidth() - width;
int posX = layers[cont]->getPosX();
if (direction) {
if (posX < (delta)) {
layers[cont]->horizontalScrolling(direction);
}
} else {
if (posX
> 0) {
layers[cont]->horizontalScrolling(direction);
}
}
cont++;
}
}
void sceneVScrolling(bool direction) {
int cont = 0;
int layersCreated = 2;
while (cont < layersCreated) {
int delta = layers[cont]->getImage()->getHeight() - height;
int posY = layers[cont]->getPosY();
if (direction) {
if (posY > 0) {
layers[cont]->verticalScrolling(direction);
}
} else {
if (posY < delta) {
layers[cont]->verticalScrolling(direction);
}
}
cont++;
}
}
int* getScene() {
return finalScene->getPixels();
}
Layer* getLayer(int i){
return layers[i];
}
};