繁体   English   中英

SDL_SetRenderTarget(SDL2)

[英]SDL_SetRenderTarget (SDL2)

我有两个例子,在我看来应该同样适用。 但它适用于一种情况,而不是第二种情况。 我需要在我的代码中使用第二种情况。 我需要将纹理地址引用到另一个函数来分配纹理并对纹理进行一些更改并将其复制到渲染器。 但它不起作用。 任何人都可以帮忙解决这个问题 我是初学者,所以我认为这肯定是一些基本错误。 谢谢所有阅读此内容的人。

这里有两种情况下的简化代码:

第一个案例有效:

/*---------------------------------in main.c----------------------------*/

#include "global.h"

int main(/*...*/)
{
    create_txt_texture();
    to_screen(); /*Here there is red color on a screen*/

    return 0;
}

    /*-------------------------------in program.c-------------------------*/

#include "global.h"

int create_txt_texture()
{
    texture = SDL_CreateTexture(renderer, /*...*/);

    txt_to_bitmap();

    return 0;
}

void txt_to_bitmap()
{
    SDL_SetRenderTarget(renderer, texture);

    SDL_SetRenderDrawColor(renderer,255,0,0,255); 
    SDL_RenderClear(renderer);  /*filling target with red color*/

    SDL_SetRenderTarget(renderer, NULL);
}

void to_screen()
{
    SDL_RenderCopy(renderer, texture, NULL, NULL);
    SDL_RenderPresent(renderer); /*Here there is red color on a screen*/
}

    /*-------------------------------in global.h-------------------------*/

#ifndef __GLOBAL_H__
#define __GLOBAL_H__

SDL_Renderer *renderer;
SDL_Texture *texture;

void to_screen();
void txt_to_bitmap();
int create_txt_texture();

#endif

第二种情况不起作用:

/*-------------------------------in main.c-------------------------------*/

#include "global.h"

int main(/*...*/)
{
    create_txt_texture(texture);
    to_screen(); /*Here there is not red color on a screen*/

    return 0;
}

    /*-------------------------------in program.c-------------------------*/

#include "global.h"

int create_txt_texture(SDL_Texture *final_texture)
{
    final_texture = SDL_CreateTexture(renderer, /*...*/);

    txt_to_bitmap(final_texture);

    return 0;
}

void txt_to_bitmap(SDL_Texture *final_texture)
{
    SDL_SetRenderTarget(renderer, final_texture);

    SDL_SetRenderDrawColor(renderer,255,0,0,255); 
    SDL_RenderClear(renderer);  /*filling target with red color*/

    SDL_SetRenderTarget(renderer, NULL);
}

void to_screen()
{
    SDL_RenderCopy(renderer, texture, NULL, NULL);
    SDL_RenderPresent(renderer); /*Here there is not red color on a screen*/
}

    /*-------------------------------in global.h---------------------------*/

#ifndef __GLOBAL_H__
#define __GLOBAL_H__

SDL_Renderer *renderer;
SDL_Texture *texture;

void to_screen();
void txt_to_bitmap(SDL_Texture *final_texture);
int create_txt_texture(SDL_Texture *final_texture);

#endif

尝试这个

    /*-------------------------------in main.c-------------------------------*/

#include "global.h"

int main(/*...*/)
{
    create_txt_texture(texture);
    to_screen(); /*Here there is not red color on a screen*/

    return 0;
}

    /*-------------------------------in program.c-------------------------*/

#include "global.h"

int create_txt_texture(SDL_Texture*& final_texture)
{
    final_texture = SDL_CreateTexture(renderer, /*...*/);

    txt_to_bitmap(final_texture);

    return 0;
}

void txt_to_bitmap(SDL_Texture *final_texture)
{
    SDL_SetRenderTarget(renderer, final_texture);

    SDL_SetRenderDrawColor(renderer,255,0,0,255); 
    SDL_RenderClear(renderer);  /*filling target with red color*/

    SDL_SetRenderTarget(renderer, NULL);
}

void to_screen()
{
    SDL_RenderCopy(renderer, texture, NULL, NULL);
    SDL_RenderPresent(renderer); /*Here there is not red color on a screen*/
}

    /*-------------------------------in global.h---------------------------*/

#ifndef __GLOBAL_H__
#define __GLOBAL_H__

SDL_Renderer *renderer;
SDL_Texture *texture;

void to_screen();
void txt_to_bitmap(SDL_Texture*& final_texture);
int create_txt_texture(SDL_Texture *final_texture);

#endif

说明:您正在通过值而不是引用传递纹理指针,这会导致此问题。 当你按值传递任何东西时,会创建该东西的另一个副本,所以在你的情况下,在按值传递指针后,你有两个指针(texture和final_texture)指向同一个位置,但是在步骤之后

final_texture = SDL_CreateTexture(renderer, /*...*/);

你的final_texture指向新分配的纹理,而纹理指针保持不变。 因此,要使纹理指针也指向新创建的纹理位置,您必须通过引用将其传递。

暂无
暂无

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

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