在我看来,有两个例子应该同样有效。,但它只在一种情况下工作,在第二种情况下不起作用。,我需要在我的代码中使用第二种情况。我需要将纹理导入到另一个函数以分配纹理,并对纹理进行一些更改并将其复制到渲染器。但不起作用。有人能帮上忙吗。我是个初学者,所以我想这一定是一些基本的错误。谢谢所有看过这个的人。
这里有两种情况下的简化代码:
First case works:
/*---------------------------------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发布于 2014-09-14 12:31:37
尝尝这个
/*-------------------------------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)指向相同的位置,但在步骤之后。
final_texture = SDL_CreateTexture(renderer, /*...*/);final_texture指向新分配的纹理,而纹理指针保持不变。因此,要使纹理指针也指向新创建的纹理位置,必须将其作为引用传递。
https://stackoverflow.com/questions/25832602
复制相似问题