SDL2 和 Xcode 的渲染器问题
Renderer troubles with SDL2 and Xcode
我尝试使用 SDL2 在 C 中编写一些东西,但我在渲染器方面遇到了一些问题。
我的代码是:
#include <stdbool.h>
#include "main.h"
#define WND_WIDTH 800
#define WND_HEIGHT 600
void displayText(SDL_Renderer *rdr,TTF_Font *font,char *str,SDL_Color *color,SDL_Rect *text_coo)
{
SDL_Surface *text=TTF_RenderText_Blended(font,str,*color);
SDL_Texture *tx_text=SDL_CreateTextureFromSurface(rdr,text);
SDL_QueryTexture(tx_text,NULL,NULL,&text_coo->w,&text_coo->h);
text_coo->x=(WND_WIDTH-text_coo->w)/2;
text_coo->y=(WND_HEIGHT-text_coo->h)/2;
SDL_RenderCopy(rdr,tx_text,NULL,text_coo);
SDL_RenderPresent(rdr);
SDL_DestroyTexture(tx_text);
SDL_FreeSurface(text);
}
int main(int argc,char *argv[])
{
SDL_Window *wnd;
SDL_Renderer *rdr;
SDL_Rect text_coo;
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
TTF_Font *font=TTF_OpenFont("/Users/coldpe/Documents/SDLProject2/SDLProject/Batang.ttf",24);
wnd=SDL_CreateWindow("Noname",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WND_WIDTH,WND_HEIGHT,SDL_WINDOW_HIDDEN);
rdr=SDL_CreateRenderer(wnd,-1,SDL_RENDERER_ACCELERATED);
SDL_ShowWindow(wnd);
SDL_SetRenderDrawColor(rdr,64,64,64,0xff);
SDL_RenderClear(rdr);
SDL_SetRenderDrawColor(rdr,128,128,128,0xff);
SDL_Rect r={WND_WIDTH/2-100,WND_HEIGHT/2-100,200,200};
SDL_RenderFillRect(rdr,&r);
SDL_SetRenderDrawColor(rdr,64,64,64,0xff);
SDL_Color normal_color={0,0,255,0xff};
SDL_Color selected_color={0,255,0,0xff};
SDL_Color *pc=&normal_color;
displayText(rdr,font,"Hello",pc,&text_coo);
bool done=false;
while(!done) {
SDL_Event event;
if(SDL_PollEvent(&event)) {
if(event.type==SDL_QUIT)
done=true;
if(event.type==SDL_MOUSEMOTION)
{
if(event.motion.x>=text_coo.x && event.motion.x<=text_coo.x+text_coo.w &&
event.motion.y>=text_coo.y && event.motion.y<=text_coo.y+text_coo.h) {
if(pc!=&selected_color) {
pc=&selected_color;
displayText(rdr,font,"Hello",pc,&text_coo);
}
}
else
if(pc!=&normal_color) {
pc=&normal_color;
displayText(rdr,font,"Hello",pc,&text_coo);
}
}
}
}
SDL_DestroyRenderer(rdr);
SDL_DestroyWindow(wnd);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
return(0);
}
我已经尝试在其他论坛的网站上寻求帮助,我应该用鼠标而不是在文本中获取这张图片:
还有这张图片,鼠标放在文字上:
但是,由于我不知道的原因,这是我用鼠标而不是在文本上真正获得的图像:
这是我用鼠标在文本上获得的结果:
为什么鼠标在文字上时,"background"是黑色的?
我确定这不是代码的问题,因为此代码适用于其他一些人...
供您参考,我在 Xcode 9(Xcode 10 有一些 OpenGL 错误)。
有人有答案吗?
我认为这里的问题是您只清除了一次具有所需颜色的后备缓冲区。渲染下一帧时,旧的后备缓冲区内容消失了,因此您需要重新绘制它。也就是说 displayText
应该以
开头
SDL_SetRenderDrawColor(rdr,64,64,64,0xff);
SDL_RenderClear(rdr);
SDL_SetRenderDrawColor(rdr,128,128,128,0xff);
SDL_Rect r={WND_WIDTH/2-100,WND_HEIGHT/2-100,200,200};
SDL_RenderFillRect(rdr,&r);
SDL_SetRenderDrawColor(rdr,64,64,64,0xff);
我尝试使用 SDL2 在 C 中编写一些东西,但我在渲染器方面遇到了一些问题。
我的代码是:
#include <stdbool.h>
#include "main.h"
#define WND_WIDTH 800
#define WND_HEIGHT 600
void displayText(SDL_Renderer *rdr,TTF_Font *font,char *str,SDL_Color *color,SDL_Rect *text_coo)
{
SDL_Surface *text=TTF_RenderText_Blended(font,str,*color);
SDL_Texture *tx_text=SDL_CreateTextureFromSurface(rdr,text);
SDL_QueryTexture(tx_text,NULL,NULL,&text_coo->w,&text_coo->h);
text_coo->x=(WND_WIDTH-text_coo->w)/2;
text_coo->y=(WND_HEIGHT-text_coo->h)/2;
SDL_RenderCopy(rdr,tx_text,NULL,text_coo);
SDL_RenderPresent(rdr);
SDL_DestroyTexture(tx_text);
SDL_FreeSurface(text);
}
int main(int argc,char *argv[])
{
SDL_Window *wnd;
SDL_Renderer *rdr;
SDL_Rect text_coo;
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
TTF_Font *font=TTF_OpenFont("/Users/coldpe/Documents/SDLProject2/SDLProject/Batang.ttf",24);
wnd=SDL_CreateWindow("Noname",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WND_WIDTH,WND_HEIGHT,SDL_WINDOW_HIDDEN);
rdr=SDL_CreateRenderer(wnd,-1,SDL_RENDERER_ACCELERATED);
SDL_ShowWindow(wnd);
SDL_SetRenderDrawColor(rdr,64,64,64,0xff);
SDL_RenderClear(rdr);
SDL_SetRenderDrawColor(rdr,128,128,128,0xff);
SDL_Rect r={WND_WIDTH/2-100,WND_HEIGHT/2-100,200,200};
SDL_RenderFillRect(rdr,&r);
SDL_SetRenderDrawColor(rdr,64,64,64,0xff);
SDL_Color normal_color={0,0,255,0xff};
SDL_Color selected_color={0,255,0,0xff};
SDL_Color *pc=&normal_color;
displayText(rdr,font,"Hello",pc,&text_coo);
bool done=false;
while(!done) {
SDL_Event event;
if(SDL_PollEvent(&event)) {
if(event.type==SDL_QUIT)
done=true;
if(event.type==SDL_MOUSEMOTION)
{
if(event.motion.x>=text_coo.x && event.motion.x<=text_coo.x+text_coo.w &&
event.motion.y>=text_coo.y && event.motion.y<=text_coo.y+text_coo.h) {
if(pc!=&selected_color) {
pc=&selected_color;
displayText(rdr,font,"Hello",pc,&text_coo);
}
}
else
if(pc!=&normal_color) {
pc=&normal_color;
displayText(rdr,font,"Hello",pc,&text_coo);
}
}
}
}
SDL_DestroyRenderer(rdr);
SDL_DestroyWindow(wnd);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
return(0);
}
我已经尝试在其他论坛的网站上寻求帮助,我应该用鼠标而不是在文本中获取这张图片:
还有这张图片,鼠标放在文字上:
但是,由于我不知道的原因,这是我用鼠标而不是在文本上真正获得的图像:
这是我用鼠标在文本上获得的结果:
为什么鼠标在文字上时,"background"是黑色的?
我确定这不是代码的问题,因为此代码适用于其他一些人... 供您参考,我在 Xcode 9(Xcode 10 有一些 OpenGL 错误)。
有人有答案吗?
我认为这里的问题是您只清除了一次具有所需颜色的后备缓冲区。渲染下一帧时,旧的后备缓冲区内容消失了,因此您需要重新绘制它。也就是说 displayText
应该以
SDL_SetRenderDrawColor(rdr,64,64,64,0xff);
SDL_RenderClear(rdr);
SDL_SetRenderDrawColor(rdr,128,128,128,0xff);
SDL_Rect r={WND_WIDTH/2-100,WND_HEIGHT/2-100,200,200};
SDL_RenderFillRect(rdr,&r);
SDL_SetRenderDrawColor(rdr,64,64,64,0xff);