我每次运行程序时都会得到相同的日志。
main.cpp有一个片段,其中每个变量都在本地范围内:
EDIT: you find at the bottom.
我创建了这些函数来启动游戏,运行菜单循环并关闭图形,如果出现问题则为gameNoError提供false值。但是最后一个函数给了我以下日志消息:
"没有当前窗口或/和渲染器关闭! SDL可能没有启动,已关闭已使用或错误的指针!"
(注意,我自己编写了所有日志消息,因为我想使用日志记录。)
所以,这意味着我在某处释放了指针的价值,但我无法找到原因?
也有我的近距离功能:
bool closeGraphics(FILE* logFile, SDL_Window* gameWindow, SDL_Renderer* gameRenderer)
{
bool success = false;
if (gameWindow == NULL or gameRenderer == NULL)
{
doLogging(logFile, "No present window or/and renderer to close! SDL possibly not started, closed already or wrong pointers used!");
success = false;
}
else
{
SDL_DestroyRenderer(gameRenderer);
gameRenderer = NULL;
SDL_DestroyWindow(gameWindow);
gameWindow = NULL;
doLogging(logFile, "Renderer and window destroyed successfully!");
}
TTF_Quit();
IMG_Quit();
SDL_Quit();
return success;
}
我只使用指针在菜单循环中进行渲染(如果你需要我也会粘贴它!)并且指针在范围内使用。
那么问题是什么?
修改: 我找到了一些东西:菜单循环也失去了指针!但是SDL开始了,我没有先做到这一点。所以我真的不知道是什么问题。
我项目中的文件:
main.cpp中:
#include <stdio.h>
#include <string>
#include <SDL.h>
#include "initialization/utility.hpp"
#include "initialization/init_closeGraphics.hpp"
#include "menuLoop.hpp"
int main(int argc, char* argv[])
{
bool gameNoError = true;
SDL_Renderer* gameRenderer = NULL;
SDL_Window* gameWindow = NULL;
FILE* logFile = fopen("errorLog.txt", "w");
doLogging(logFile, "Game started successfully!");
gameNoError = initGraphics(logFile, gameWindow, gameRenderer, "This is a title!", 800, 600);
if (gameNoError) gameNoError = doMenuLoop(logFile, gameWindow, gameRenderer, 800, 600);
if (gameNoError)
{
gameNoError = closeGraphics(logFile, gameWindow, gameRenderer);
}
else
{
closeGraphics(logFile, gameWindow, gameRenderer);
}
if (!gameNoError)
{
doLogging(logFile, "Game stopped with error/exception/problem!");
}
else
{
doLogging(logFile, "Game stopped successfully!");
}
return 0;
}
utility.hpp仅包含日志记录功能
init_close.cpp :(它有它的标题)
#include "init_closeGraphics.hpp"
bool initGraphics(FILE* logFile,
SDL_Window* gameWindow,
SDL_Renderer* gameRenderer,
const char* gameTitle,
int gameWindowWidth,
int gameWindowHeight)
{
bool success = true;
if (gameWindowWidth <= 0 or gameWindowHeight <= 0)
{
doLogging(logFile, "Get less then or equal with 0 window dimensions at SDL initialization!");
success = false;
}
else
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
doLogging(logFile, "Failed to initialize SDL!");
doLogging(logFile, SDL_GetError());
success = false;
}
else
{
gameWindow = SDL_CreateWindow(gameTitle,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
gameWindowWidth,
gameWindowHeight,
SDL_WINDOW_SHOWN);
if (gameWindow == NULL)
{
doLogging(logFile, "Failed to create window!");
doLogging(logFile, SDL_GetError());
success = false;
}
else
{
gameRenderer = SDL_CreateRenderer(gameWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (gameRenderer == NULL)
{
doLogging(logFile, "Failed to create renderer!");
doLogging(logFile, SDL_GetError());
success = false;
}
else
{
SDL_SetRenderDrawColor(gameRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
int IMG_FLAG = IMG_INIT_PNG;
if (!(IMG_Init(IMG_FLAG) & IMG_FLAG))
{
doLogging(logFile, "Failed to initialize SDL_image!");
doLogging(logFile, IMG_GetError());
success = false;
}
if (TTF_Init() == -1)
{
doLogging(logFile, "Failed to initialize SDL_ttf!");
doLogging(logFile, TTF_GetError());
success = false;
}
}
}
}
}
return success;
}
bool closeGraphics(FILE* logFile, SDL_Window* gameWindow, SDL_Renderer* gameRenderer)
{
bool success = false;
if (gameWindow == NULL or gameRenderer == NULL)
{
doLogging(logFile, "No present window or/and renderer to close! SDL possibly not started, closed already or wrong pointers used!");
success = false;
}
else
{
SDL_DestroyRenderer(gameRenderer);
gameRenderer = NULL;
SDL_DestroyWindow(gameWindow);
gameWindow = NULL;
doLogging(logFile, "Renderer and window destroyed successfully!");
}
TTF_Quit();
IMG_Quit();
SDL_Quit();
return success;
}
答案 0 :(得分:3)
您将指针变量按值传递给initGraphics
,因此您的呼叫网站不会看到任何更改。
如果您希望在致电main
时更新initGraphics
中的变量,请参考以下内容:
bool initGraphics(FILE* logFile,
SDL_Window*& gameWindow,
// ^
SDL_Renderer*& gameRenderer,
// ^
const char* gameTitle,
int gameWindowWidth,
int gameWindowHeight)