标题可能有点奇怪,因为我不能100%确定问题的主要来源。
一些背景知识:我正在制作家庭库存管理计划
问题:我有一个名为'addItemForm'的类,它用于创建一个顾名思义的项目。但问题在于。
为了渲染表单的每个特定单独部分,我创建了一些SDL_Rects来包含每个纹理渲染的坐标:
SDL_Rect TformBackgroundRect;
SDL_Rect TinputBox;
SDL_Rect TinputBox2;
SDL_Rect TinputBox3;
SDL_Rect TinputNameRect;
SDL_Rect TinputQuantRect;
SDL_Rect TinputUnitRect;
然而,当我尝试使用它们(在构造函数中初始化之后)没有在屏幕上呈现任何内容。我之所以做的是在构造函数中创建SDL_Rects然后执行此操作:
formBackgroundRect = TformBackgroundRect;
inputBox = TinputBox;
inputBox2 = TinputBox2;
inputBox3 = TinputBox3;
inputNameRect = TinputNameRect;
inputQuantRect = TinputQuantRect;
inputUnitRect = TinputUnitRect;
然而,这也不起作用。当我尝试进行一些碰撞检测时,它也无法正常工作。我打印了inputBox(inputBox.x)的x值,它给了我一个-8589993460的值!?!?
好的,我写的内容对大多数人来说可能是胡言乱语,所以我要做的就是在下面发布我的addItemForm类的代码:
addItemForm.h
class addItemForm
{
public:
int xPos;
int yPos;
//These are the values for the width and height of the different textures.
int fbtWidth, fbtHeight;
int iFtWidth, iFtHeight;
int iNtWidth, iNtHeight;
int iQtWidth, iQtHeight;
int iUtWidth, iUtHeight;
SDL_Color fontColor;
TTF_Font* formFont;
SDL_Texture* formBackgroundTexture;
SDL_Texture* inputFieldTexture;
SDL_Texture* inputNameText;
SDL_Texture* inputQuantityText;
SDL_Texture* inputUnitText;
SDL_Rect formBackgroundRect;
SDL_Rect inputBox;
SDL_Rect inputBox2;
SDL_Rect inputBox3;
SDL_Rect inputNameRect;
SDL_Rect inputQuantRect;
SDL_Rect inputUnitRect;
SDL_Rect TformBackgroundRect;
SDL_Rect TinputBox;
SDL_Rect TinputBox2;
SDL_Rect TinputBox3;
SDL_Rect TinputNameRect;
SDL_Rect TinputQuantRect;
SDL_Rect TinputUnitRect;
std::string inputNameString;
std::string inputQuantString;
std::string inputUnitString;
addItemForm(int x, int y);
~addItemForm();
void loadTextures();
void render();
void update(int x, int y, SDL_Event e);
void inputValuestoField(int x, int y, SDL_Event e);
void free();
private:
};
addItemForm.cpp
Button addItemButton(72, 65);
Button cancelEntry(383, 310);
bool isViewable = false;
addItemForm::addItemForm(int x, int y)
{
xPos = x;
yPos = y;
formBackgroundTexture = NULL;
inputFieldTexture = NULL;
inputNameText = NULL;
inputQuantityText = NULL;
inputUnitText = NULL;
fbtWidth = 0;
fbtHeight = 0;
iFtWidth = 0;
iFtHeight = 0;
iNtWidth = 0;
iNtHeight = 0;
iQtWidth = 0;
iQtHeight = 0;
iUtWidth = 0;
iUtHeight = 0;
formFont = TTF_OpenFont("media/cambria.ttf", 15);
//The rects which will dictate where the textures are displayed onto the screen
formBackgroundRect;
inputBox;
inputBox2;
inputBox3;
inputNameRect;
inputQuantRect;
inputUnitRect;
//Defining the font colour:
fontColor;
fontColor.r = 0;
fontColor.b = 0;
fontColor.g = 0;
fontColor.a = 0;
addItemButton.loadFromFile("media/buttonAddItem.png");
cancelEntry.loadFromFile("media/buttonCancelEntry.png");
}
void addItemForm::loadTextures()
{
SDL_Surface* loadedSurface = NULL;
//loading Images
loadedSurface = IMG_Load("media/addItemForm/itemFormBG.png");
if(loadedSurface == NULL)
{
printf("Could Not load Image: itemFormBG\n");
printf("Error: %s\n", SDL_GetError());
}
else
{
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0, 0xFF, 0xFF) );
formBackgroundTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
fbtWidth = loadedSurface->w;
fbtHeight = loadedSurface->h;
loadedSurface = NULL;
}
loadedSurface = IMG_Load("media/addItemForm/inputTextField.png");
if(loadedSurface == NULL)
{
printf("Could not load Image: inputTextField.png\n");
printf("Error: %s\n", SDL_GetError());
}
else
{
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0, 0xFF, 0xFF) );
inputFieldTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
iFtWidth = loadedSurface->w;
iFtHeight = loadedSurface->h;
loadedSurface = NULL;
}
//Loading text
SDL_Texture* tmpTextTexture = NULL;
std::string textArray[3] = {"Item Name:", "Quantity:", "Unit:"};
for (int i = 0; i < 3; i++)
{
loadedSurface = TTF_RenderText_Solid(formFont, textArray[i].c_str(), fontColor);
tmpTextTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
switch (i)
{
case 0:
inputNameText = tmpTextTexture;
iNtWidth = loadedSurface->w;
iNtHeight = loadedSurface->h;
case 1:
inputQuantityText = tmpTextTexture;
iQtWidth = loadedSurface->w;
iQtHeight = loadedSurface->h;
case 2:
inputUnitText = tmpTextTexture;
iUtWidth = loadedSurface->w;
iUtHeight = loadedSurface->h;
}
loadedSurface = NULL;
tmpTextTexture = NULL;
}
SDL_FreeSurface(loadedSurface);
SDL_DestroyTexture(tmpTextTexture);
}
void addItemForm::render()
{
SDL_Rect TformBackgroundRect = { xPos, yPos, fbtWidth, fbtHeight};
SDL_Rect TinputBox = {xPos + 127, yPos + 72, iFtWidth, iFtHeight};
SDL_Rect TinputBox2 = {xPos + 127, yPos + 122, iFtWidth, iFtHeight};
SDL_Rect TinputBox3 = {xPos + 127, yPos + 165, iFtWidth, iFtHeight};
SDL_Rect TinputNameRect = {xPos + 15 , yPos + 71, iNtWidth, iNtHeight};
SDL_Rect TinputQuantRect = {xPos + 15, yPos + 121, iQtWidth, iQtHeight};
SDL_Rect TinputUnitRect = {xPos + 15, yPos + 164, iUtWidth, iUtHeight};
formBackgroundRect = TformBackgroundRect;
inputBox = TinputBox;
inputBox2 = TinputBox2;
inputBox3 = TinputBox3;
inputNameRect = TinputNameRect;
inputQuantRect = TinputQuantRect;
inputUnitRect = TinputUnitRect;
if(isViewable == true)
{
SDL_RenderCopy(gRenderer, formBackgroundTexture, NULL, &TformBackgroundRect);
SDL_RenderCopy(gRenderer, inputFieldTexture, NULL, &TinputBox);
SDL_RenderCopy(gRenderer, inputFieldTexture, NULL, &TinputBox2);
SDL_RenderCopy(gRenderer, inputFieldTexture, NULL, &TinputBox3);
SDL_RenderCopy(gRenderer, inputNameText, NULL, &TinputNameRect);
SDL_RenderCopy(gRenderer, inputQuantityText, NULL, &TinputQuantRect);
SDL_RenderCopy(gRenderer, inputUnitText, NULL, &TinputUnitRect);
cancelEntry.render();
}
addItemButton.render();
}
void addItemForm::update(int x, int y, SDL_Event e)
{
if( addItemButton.mouseAction(x, y, e) )
{
isViewable = true;
}
if( cancelEntry.mouseAction(x, y, e) )
{
isViewable = false;
}
inputValuestoField(x, y, e);
}
void addItemForm::inputValuestoField(int x, int y, SDL_Event e)
{
//Detect pressing on rect
//printf("InputBox.x : %i, inputBox.y: %i", inputBox.x, inputBox.y);
if( x >= inputBox.x && x <= (inputBox.x + inputBox.w) && y >= inputBox.y && y <= inputBox.y + inputBox.h && e.type == SDL_MOUSEBUTTONDOWN)
{
printf("\nHEYHEYHE PRESSING INPUTBOX 1");
}
//Start reading keyboard
//When enter is pressed/clicked away from box stop reading
//place text which was just input into the appropriate string (name, quantity, unit)
}
void addItemForm::free()
{
SDL_DestroyTexture(formBackgroundTexture);
SDL_DestroyTexture(inputFieldTexture);
SDL_DestroyTexture(inputNameText);
SDL_DestroyTexture(inputQuantityText);
SDL_DestroyTexture(inputUnitText);
addItemButton.free();
cancelEntry.free();
}
addItemForm::~addItemForm()
{
free();
xPos = 0;
yPos = 0;
formBackgroundTexture = NULL;
inputFieldTexture = NULL;
inputNameText = NULL;
inputQuantityText = NULL;
inputUnitText = NULL;
isViewable = false;
formBackgroundRect;
inputBox;
inputBox2;
inputBox3;
inputNameRect;
inputQuantRect;
inputUnitRect;
}
如果有人不确定我的意思,我很乐意回答有关我的代码的任何问题。我如何表达一切很可能很难遵循,而且我非常感谢那些试图提供帮助的人。