我遇到了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-&gt; RPGTutorial-&gt; Grass.bmp中有Grass.bmp 当我编译它时,它是成功的。它贯穿代码直到我到达检查是否(grass == NULL)的部分,然后它经过并退出。当我将图像放在.cpp文件所在的文件夹中时,有人可以帮助我知道为什么我的草没有被设置为图像吗?我甚至尝试添加一个Image文件夹来保存它,它也不起作用。
答案 0 :(得分:1)
如果你有时间,我建议你花一些时间来完成Lazyfoo tutorials他们太棒了。他在第二个教程“在屏幕上获取图像”中提到了这个问题。
Visual Studio将当前工作目录更改为.vcxproj文件所在的位置。这将是您要放置资源的目录。如果您不确定它在哪里,可以使用direct.h header MSDN Source For getcwd中的_getcwd()
函数