Search code examples
cxcodesdl

Renderer troubles with SDL2 and Xcode


I try to write something in C with SDL2 but I have some troubles with the renderer.

My code is:

#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);
}

I already tried to ask for help in other forum's website, and I should obtain this image with mouse not in the text :

Mouse not on the text

And this image with mouse on the text :

Mouse on the text

But, for a reason that I don't know, here is the image I really obtain with mouse not on the text :

Mouse not on the text

And here is the result I obtain with mouse on the text :

Mouse on the text

Why when the mouse is on the text, the "background" is black ?

I'm sure that it is not a code's trouble because this code works with some other peoples... For your information, I'm in Xcode 9 (Xcode 10 have some bugs with OpenGL).

Someone have answers ?


Solution

  • I think the issue here is that you clear backbuffer with desired color only once. When next frame is rendered old backbuffer content is gone so you need to redraw it again. That is displayText should start with

    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);