我在SDL_Texture上遇到麻烦
RPGTutorial.cpp
#include "stdafx.h"
int main(int argc, char *argv[])
{
bool quit = false;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = NULL;
window = SDL_CreateWindow("RPG Tutorial!", 100, 100, 600, 400, SDL_WINDOW_SHOWN);
if (window == NULL)
{
std::cout << "Window couldn't be created" << std::endl;
return 0;
}
SDL_Renderer* renderer = NULL;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
{
std::cout << "Renderer is not being created!" << std::endl;
SDL_DestroyWindow(window);
system("PAUSE");
return 0;
}
SDL_Event* mainEvent = new SDL_Event();
SDL_Texture* grass = NULL;
grass = IMG_LoadTexture(renderer, "Grass.bmp");
if (grass == NULL)
{
std::cout << "Grass Image was not found!" << std::endl;
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
delete mainEvent;
system("PAUSE");
return 0;
}
SDL_Rect grass_rect;
grass_rect.x = 0;
grass_rect.y = 0;
grass_rect.w = 64 * 2;
grass_rect.h = 64 * 2;
while (!quit && mainEvent->type != SDL_QUIT)
{
SDL_PollEvent(mainEvent);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, grass, NULL, &grass_rect);
SDL_RenderPresent(renderer);
}
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
delete mainEvent;
return 0;
}
stdafx.h
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <iostream>
我在RPGTutorial-> RPGTutorial-> Grass.bmp中有Grass.bmp
当我编译它时,它是成功的。它一直运行到直到我检查if(grass == NULL)的那部分代码为止,然后通过并退出。有人可以帮我知道为什么当我将图像放在.cpp文件所在的文件夹中时,为什么没有将我的草设置为该图像吗?我什至尝试添加一个Image文件夹来容纳它,但是它也不起作用。
最佳答案
如果您有时间,我建议您花一些时间进行Lazyfoo tutorials的学习,他们很棒。他在第二个教程“在屏幕上获取图像”中提到了此问题。
Visual Studio将当前工作目录更改为.vcxproj文件所在的位置。那将是您要在其中放置资源的目录。如果不确定该位置,则可以在direct.h标头MSDN Source For getcwd中使用_getcwd()
函数。