我正在研究其他人的早期版本的SDL的Pong代码,我正试图让它与SDL2一起运行。以下是我的代码的摘录,只有加载图像。当这段代码运行时,我会弹出一个窗口告诉我这个: Pong.exe中0x00A065AD处的未处理异常:0xC0000005:访问冲突读取位置0x00000004。在调试时,它也指向此行:
optimizedImage = SDL_ConvertSurface(loadedImage, windowSurface->format, 0);
错误列表显示警告LNK4098:defaultlib'msvcrt.lib'与使用其他库冲突;使用/ NODEFAULTLIB:库。
我不知道该怎么做。最重要的是,即使在调试结束后,SDL窗口也会显示,冻结,有时也不会关闭。 (它告诉我,当我尝试时存在访问冲突。)
#include <string>
#include <SDL.h>
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 475;
const int SCREEN_BPP = 2;
SDL_Window *window = NULL;
SDL_Surface *windowSurface = NULL;
SDL_Surface *ball = NULL;
SDL_Surface *background = NULL;
SDL_Surface *paddleL = NULL;
SDL_Surface *paddleR = NULL;
SDL_Event event;
SDL_Surface *loadImage(std::string fileName)
{
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;
loadedImage = SDL_LoadBMP(fileName.c_str());
if (loadedImage != NULL)
{
optimizedImage = SDL_ConvertSurface(loadedImage, windowSurface->format, 0);
SDL_FreeSurface(loadedImage);
if (optimizedImage != NULL)
{
SDL_SetColorKey(optimizedImage, SDL_TRUE,
SDL_MapRGB(optimizedImage->format, 255, 255, 255));
}
}
return optimizedImage;
}
bool init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
return false;
window = SDL_CreateWindow("Pong", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH,
WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL)
return false;
return true;
}
bool loadFiles()
{
ball = loadImage("ball.bmp");
background = loadImage("background.bmp");
paddleL = loadImage("paddleRed.bmp");
paddleR = loadImage("paddleBlue.bmp");
if (ball == NULL)
return false;
if (background == NULL)
return false;
if (paddleL == NULL || paddleR == NULL)
return false;
return true;
}
int main(int argc, char* args[])
{
windowSurface = SDL_GetWindowSurface(window);
if (init() == false)
return 1;
if (loadFiles() == false)
return 1;
SDL_FreeSurface(windowSurface);
SDL_FreeSurface(ball);
SDL_FreeSurface(background);
SDL_FreeSurface(paddleL);
SDL_FreeSurface(paddleR);
windowSurface = ball = background = paddleL = paddleR = nullptr;
SDL_DestroyWindow(window);
window = nullptr;
SDL_Quit();
return 0;
}
答案 0 :(得分:2)
您正在致电
windowSurface = SDL_GetWindowSurface(window);
前
window = SDL_CreateWindow("Pong", SDL_WINDOWPOS_CENTERED,
因此窗口仍为NULL,导致windowSurface
为NULL,导致windowSurface->format
导致崩溃。