Re: trying to do a blinking text cursor

"Naith" <[email protected]>
Newsgroups gmane.comp.lib.sdl
Message-ID <[email protected]>
If all you want is a blinking cursor you can do it like this:


Code:

float g_Timer = 1.0f;

// How fast the cursor blinks
float g_BlinkSpeed	= 2.0f;

// The blinking cursor
SDL_Rect CursorQuad = {200, 200, 10, 20};

int main(int argc, char* argv[])
{
	while(g_Running)
	{
		g_Timer -= g_BlinkSpeed * (float)g_DeltaTime;

		if(g_Timer <= 0.0f)
			g_Timer = 2.0f;

		//////////////////////////////////////////////////////////////////////////

		// The cursor quad will be black
		SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, SDL_ALPHA_OPAQUE);

		// Whenever the g_Timer value is even (even/odd), render the cursor
		if(((int)g_Timer & 1) == 0)
		{
			// Render the cursor quad
			SDL_RenderFillRect(g_pRenderer, &CursorQuad);

			/*
			If you want to use a font for rendering instead,
			remove 'SDL_RenderFillRect' above and use the code below
			For simplicity I've not done any error checking,
			i.e, checking that the surface and texture was successfully created and such
			*/

			SDL_Surface*	pSurface	= TTF_RenderText_Blended(Myfont, "|", CursorColor);
			SDL_Texture*	pTexture	= SDL_CreateTextureFromSurface(MyRenderer, pSurface);
			int				TexWidth	= 0;
			int				TexHeight	= 0;

			SDL_QueryTexture(pTexture, NULL, NULL, &TexWidth, &TexHeight);

			SDL_Rect PositionQuad = {200, 200, TexWidth, TexHeight};

			SDL_RenderCopy(MyRenderer, pTexture, NULL, &PositionQuad);

			SDL_DestroyTexture(pTexture);
			SDL_FreeSurface(pSurface);
		}
	}

	return 0;
}





Hope it helps.

_______________________________________________
SDL mailing list
[email protected]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.