SDL_SetRenderTarget(SDL2)

时间:2014-09-14 11:09:27

标签: c sdl-2

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

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

第一个案例有效:

/*---------------------------------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

1 个答案:

答案 0 :(得分:3)

试试这个

    /*-------------------------------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 指向新分配的纹理,而纹理指针保持不变。因此,要使纹理指针也指向新创建的纹理位置,您必须通过引用将其传递。