简体   繁体   English

SDL窗口-渲染Sprite C ++

[英]SDL Window - Rendering a sprite C++

I'm not sure where I'm going wrong when rendering a sprite. 我不确定渲染精灵时哪里出了问题。 Of what I have been taught in lectures, to render a PNG you must firstly have a surface that you load the file to, then you must have a texture and create texture from surface, you then free the space and then it should output it? 在我的讲座中,要渲染PNG,首先必须具有一个将文件加载到的表面,然后必须具有纹理并从该表面创建纹理,然后释放空间然后将其输出? Where am I going wrong here? 我在哪里错了? Could it be file path? 可能是文件路径吗? If so I have tried putting the full directory in and everything? 如果是这样,我尝试将完整目录放进去吗?

Here is my code: 这是我的代码:

case 2:
{
    //game code..............

    SDL_SetRenderDrawColor(renderer, 255, 210, 0, 0);
    SDL_RenderClear(renderer);

    SDL_Texture* sprite;
    SDL_Texture* blockt;
    SDL_Texture* points;
    SDL_Surface* blockS;
    SDL_Surface* windowS;
    SDL_Surface* temp;

    blockS = IMG_Load("Barriers.png");
    blockt = SDL_CreateTextureFromSurface(renderer, blockS);
    SDL_FreeSurface(blockS);

    SDL_RenderPresent(renderer);
}
break;

Try to ignore the temp and stuff I was just trying out different things I have seen and left some code in there. 尝试忽略临时性和临时性,而我只是尝试尝试所见过的不同事物,并在其中保留一些代码。 Basically just need to know why it isn't working. 基本上只需要知道为什么它不起作用。 I have SDL_INIT_EVERYTHING at the top as well as the IMG_Init(SDL_INIT_EVERYTHING) and I have included the SDL_image.h header. 我在顶部还有SDL_INIT_EVERYTHING以及IMG_Init(SDL_INIT_EVERYTHING),并且包含了SDL_image.h标头。

You have to actually draw the texture using SDL_RenderCopy (before SDL_RenderPresent ). 您实际上必须使用SDL_RenderCopy绘制纹理(在SDL_RenderPresent之前)。

Note that, once you have the texture, freeing the original surface is irrelevant (although you do want to free it at some point). 请注意,一旦有了纹理,则释放原始表面是无关紧要的(尽管您确实希望在某个时候释放它)。 What you do with it won't change that texture; 使用它所做的不会改变纹理。 they are separate entities. 它们是独立的实体。 All you need to do is render the texture. 您需要做的就是渲染纹理。

Also, go take a look at the wiki ( here's the rendering category page ). 另外,请查看Wiki( 这是渲染类别页面 )。 It should give you a better understanding of what you can do. 它应该使您对可以做什么有更好的了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM