trying to do a blinking text cursor |
trying to do a blinking text cursor |
Jonny D
|
To invert existing colors on a render target, you have to read the pixels first so you can calculate the inverted colors. This is a very slow operation and should not be done lightly.
The best way to do it nowadays (i.e. without direct video memory access) is to use a shader and keep the color calculations on the GPU. I can't help any further than that with plain SDL (is anyone using shaders with SDL_Renderer?), but I can do it all in a couple minutes with SDL_gpu. Jonny D On Sat, Aug 6, 2016 at 11:20 AM, Jack Andrews wrote:
|
|||||||||||||
|
trying to do a blinking text cursor |
Chris Bush
Guest
|
Could you re-render the glyph in the opposite color on top of the cursor?
Hope this helps. CB |
|||||||||||
|
trying to do a blinking text cursor |
Jack Andrews
Guest
|
Hey Johnny D
I'll use SDL_gpu - it must be portable if as it has Android.mk . But I was under the impression that SDL did gpu operations... I'm new.. Please share your solution (or we can discuss it in SDL_gpu forum if it's off topic for the list) Thanks Jack. On 7 August 2016 at 01:43, Jonathan Dearborn wrote:
|
|||||||||||||||||
|
trying to do a blinking text cursor |
Jack Andrews
Guest
|
Thanks Chris. That would work, but for the default, I'd like a browser-like blinking "I" cursor between pos-1 and pos.I kinda thought SDL was low level and so changing pixels in a window wouldn't be a big deal. Best, Jack. On 7 August 2016 at 01:50, Chris Bush wrote:
|
|||||||||||||||
|
trying to do a blinking text cursor |
Chris Bush
Guest
|
On 6 August 2016 at 11:55, Jack Andrews wrote:
My initial thought: would it be enough to skip rendering the cursor entirely on the blink-off phases? If you just want to invert the render draw color for a rect, would something like this work? Uint8 r, g, b, a; SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a); SDL_SetRenderDrawColor(renderer, 255 - r, 255 - g, 255 - b, a); Since the rect only defines the draw area, you'd want to update the render draw color. If your cursor is a fancy image or something, you may want to store an inverted version and alternate between on and off. Getting into fancier territory (altering pixels under the rect, etc.), follow Jonathan's lead. Hope this helps. CB |
|||||||||||||
|
Naith
|
If all you want is a blinking cursor you can do it like this:
Hope it helps. |
|||||||||||||
|
trying to do a blinking text cursor |
Jack Andrews
Guest
|
So would I have a texture which is the text without the cursor, and a texture with only cursor then SDL_Texture ts[2];SDL_Rect*rs[2]={NULL,&cursor_rect}; id=SDL_AddTimer(500,ontimer,(V*)1);ie=SDL_AddTimer(500,ontimer,(V*)2); int ontimer(V*x) { Â t=(int)x; Â for(int i=0;i<t;i++) Â Â SDL_RenderCopy(renderer,ts[i],0,rs[i]); } Â Â Â or something like that...? On 7 August 2016 at 02:12, Chris Bush wrote:
|
|||||||||||||||||
|
trying to do a blinking text cursor |
Jonny D
|
Well, it would be simple to just draw a cursor without inverting the colors of the underlying pixels. If you know the color of the background, you can do that. It's a good first step. Porting to SDL_gpu is easy, but still takes a few additional changes. If SDL_Renderer satisfies your needs, stick with it.
SDL is quite low level and can change the color of pixels only by drawing over them. The trouble is reading the color of the underlying pixels. That is a different matter. But I realized inverting the color doesn't require a shader, only a subtractive blend mode. Still, SDL doesn't have one. If inverted color is what you really need, you can set the shape blend equation to subtract the RGB color values (and add alpha values): GPU_SetShapeBlendEquation(GPU_EQ_SUBTRACT, GPU_EQ_ADD); Then set the shape blend function to use all of the RGB values and ignore the source alpha: GPU_SetShapeBlendFunction(GPU_FUNC_ONE, GPU_FUNC_ONE, GPU_FUNC_ZERO, GPU_FUNC_ONE); Lastly, draw a white rectangle to invert the color under it (1.0 - R = R'): GPU_RectangleFilled(screen, x, y, x + w-1, y + h-1, GPU_MakeColor(255,255,255,255)); Jonny D On Sat, Aug 6, 2016 at 11:55 AM, Jack Andrews wrote:
|
|||||||||||||||
|
Re: trying to do a blinking text cursor |
Naith
|
See my answer above for a simple solution on how you can do it. |
|||||||||||||
|
trying to do a blinking text cursor |
Jack Andrews
Guest
|
thanks for your code, naith. Â i see my error in my proposed code - having two timers. but where do you erase the cursor? Â what if the position changes and the text is not re-rendered (hold down SDLK_RIGHT) - couldn't you end up with cursor phantoms left in places when it was not invisible? On 7 August 2016 at 03:22, Naith wrote:
|
|||||||||||||||
|
Re: trying to do a blinking text cursor |
Naith
|
Not sure I understand. Please elaborate more on what you mean. The cursor is never erased. It is rendered whenever the timer value is an even number and not rendered elsewhere. |
|||||||||||||
|
trying to do a blinking text cursor |
Jonny D
|
Prior rendering artifacts don't appear if you clear the screen each frame, before drawing. I think we're assuming this.
Jonny D On Sat, Aug 6, 2016 at 2:18 PM, Naith wrote:
|
|||||||||||||
|