Is there a way I can use SDL_RenderClear() without clearing everything? - c++

I have some homegrown code to draw a circle in SDL2 (It's slow). Each time I want to update the window, I have to call SDL_RenderClear() and redraw the same circle. Is there any way I can avoid clearing the circle but still clear the rest of the screen to allow me to draw everything else.
The circle has to be drawn over the top of everything else.
https://imgur.com/a/0qAgd1Y
I could draw the circle each time over the rest of the image, but that takes about 500ms.

Related

how can i draw a sprite by left clicking?

i've tried to make a project, but i can't draw a sprite as i want. I mean that everything works when i just draw a sprite, but it stop working when i am trying to draw the sprite by clicking left mouse button. There's code i tried:
if(zdarzenie.type == Event::MouseButtonPressed && zdarzenie.mouseButton.button == Mouse::Left)
{
pocisk.setPosition(10, 10);
oknoAplikacji.draw(pocisk);
}
Btw, I am writing in Polish as if it would change something.
And yes, i have everything good besides that.
(and i am using 2.4.1 version of SFML)
I don't know what you are doing now because you didn't provide enough of your code and I actually don't understand your if statement but, it can just be :
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
sprite.setPosition(sf::Mouse::getPosition());
renderTarget.draw(sprite);
}
By the way I strongly suggest that you do not use the draw function here but organize your code to have a draw method called in a render loop, because depending on where your code is in your program, the sprite could be drawn for only one frame and then erased since it's not updated.
From what I understand in your code in Polish, you have the right code to do what you want, but the problem comes from the fact that you draw the sprite only once.
The draw method is called every frame and it will erase everything on screen and then redraw them. Doing it only once, like in your code, will only draw it a single time then delete it the very next frame.
At that point multiple solution can be used. If its a GameObject clicking can activate the object to then draw it or a simple bool could be used has a switch in your draw to make it appear.

Drawing a line with open GL

I am a newbie with OpenGL. I need to draw a line with it. I browsed the web and found this code:
glBegin(GL_LINES);
glVertex2f(.25,0.25);
glVertex2f(.75,.75);
glEnd();
However, I don't see any line. The consoler appears only for some milliseconds. I need a program that will draw a line and at least visible for some moments.
Thanks in advance.
Bevor you can draw something, you first need some canvas to draw upon. That's be a window with a pixel framebuffer; without doing extra effort you don't have such.
So first step is to create a window which you can draw into, that gives you the canvas.
Next you need the actual pens to draw with. That would be a OpenGL context you have to create and connect with the window.
Only after you did that you can actually ask OpenGL to draw some line. If you just call the drawing commands, there's nothing going to happen, because you neither have the canvas to draw to, nor the pen to draw with.

Is it possible to draw on an SFML window regardless of views?

I have a game I'm currently working on, and it uses multiple views (for a minimap for example).
Thing is, I would like to have a fading effect added at some point, so I thought I'd create a black image that is the size of the screen and change its alpha value with a timer. That part is not a problem.
What happens right now is the main area (ie window default view) is fading (because the opacity of the image is increasing), but the minimap (minimap view) is unaffected. This is normal behaviour for views, but is there a way to draw an image to the whole window, regardless of the views ?
Thanks in advance
To clarify, you have the default view where you'll draw the main game, then you'll have the minimap view where you would draw the minimap. At some point in the game you want the whole screen to fade to black. It sounds like you've been trying to draw a black image on the default view (changing the alpha) to make this effect work.
So, you need a third view that you draw your black image on to get this fading effect.

How to avoid pixel fighting when drawing on display with SetPixel?

I am using SetPixel(GetDC(0),x,y,color) to write on the screen but as I do that, some other program updates it's screen and overwrites my drawn pixel thus the image drawn on screen appear to sparkle.
How Can I avoid this and draw something on the screen without the fear that it will be overwritten?
The screen is a shared resource. If you want something that is exclusively yours, create a window and draw into that.

Drawing text in Cinder

I was wondering if there is a way to draw a gl::texture file with out having to use the gl::draw command every loop. Is there a way I can draw it once, and then not worry about it.
Drawing the image on every loop of draw() is slowing down my application, so I'd like to only draw things once on the screen and then update them if need be.
Quoting from Cinder's tutorials:
"When you create a new Cinder project, you will notice there are a few functions declared for you. Every Cinder project is made up of three main functions. You initialize your variables in the setup() method which is called once when your program begins. You make changes to those variables in the update() method. And finally, you draw() content in your program window. Update and draw are the heartbeat of any Cinder project. UpdateSetup, then update and draw, update and draw, update and draw, on and on until you quit the application."
There's a way though to draw objects permanently in OpenGL and concequently in Cinder but I wouldn't recommend it. Is to disable gl::clear() in your draw function. You can't though selectively delete any unneeded object. You will have to render your scene from scratch. Think of OpenGL's frame-buffer more of a canvas. Everytime you call gl::clear() you take the brush and you paint your canvas black or what ever color you specify with gl::clear(). After that the frame-buffer is "tabula rasa" you have to draw everything you want to display from scratch. If you don't state any gl::clear() command when you draw a new object is like your canvas stays intact and you draw your object on top of the already drawn.