c - SDL_SetRenderTarget (SDL2)

标签 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

最佳答案

试试这个

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

解释:您正在按值而不是按引用传递纹理指针,这导致了此问题。 当您按值传递任何东西时,会创建该事物的另一个副本,因此在您的情况下,在按值传递指针后,您有两个指针(纹理和最终纹理)指向相同的位置,但在步骤之后

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

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

关于c - SDL_SetRenderTarget (SDL2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25832602/

相关文章:

windows - 使用SDL2 crate 运行和构建程序时出现问题

python - PATH_MAX和NAME_MAX有什么关系,如何获取?

c - 如何编写 C 代码从包含数字和字符串的数据文件中读取数字

c - 将 SDL2 与 gcc 链接

c++ - SDL2 : Is it bad to move everything instead of using the view-port?

c++ - 如何在 SDL 中获取屏幕尺寸

c++ - SDL 在 Linux 上运行几秒钟后没有响应

c - 为什么 1/10 等于零,除非我使用变量?

C中三个整数的比较

c - 使用管道的IPC机制来完成程序,以便子进程和父进程打印相同的输出