Image over another image in allegro5 (C++) - c++

I have a problem with adding a background in my game. My code looks like that:
while (true)
{
al_draw_bitmap(background, 0, 0, NULL);
al_flip_display();
some code(...);
al_draw_bitmap(snake,0,0,NULL);
/*drawing my snake's tail*/
al_clear_to_color(al_map_rgb(0,0,0));
al_draw_bitmap(apple,0,0,NULL);
al_flip_display();
}
And I have visible an apple and a black screen most of the time.
I've been changing order of some lines in code and none of these combination worked (best case was when I had visible snake and background but there was no apple).
Before adding a background, my snake's tail sometimes disappered, but it wasn't very visible, and except that, everything seemed to be okay.
Anyone knows how to add a background properly?
Or maybe it's fault of my computer?

Once per draw loop, you should:
clear the display
draw your background
draw bitmaps on top of the background
flip the display
For example:
al_clear_to_color(al_map_rgb(0,0,0));
al_draw_bitmap(background, 0, 0, NULL);
al_draw_bitmap(apple,0,0,NULL);
al_draw_bitmap(snake,0,0,NULL);
al_flip_display();
Note that you should only call al_flip_display once per draw loop, after you have drawn everything for that loop. al_clear_to_color should be called first as it will wipe out everything you have drawn. In the example you gave, you are drawing your apple and snake to the same location, so I wouldn't be suprised if one is blocking part of the other.
Also, you probably don't want to stick your draw loop in a while(true) as the framerate will be unconstrained. That is, it will run as fast as it is allowed, and the framerate won't be consistent. The typical approach is to use an event-driven draw loop.

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.

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

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.

SFML flickering renderwindow?

I'm hoping someone can help me out, I'm using SFML 2.1 and when I draw my renderWindow it just keeps flickering, now I know SFML uses two buffers so i'm sure one is not getting drawn at all but I can't understand why.
Here is my loop
while(!quit)
{
rs.Canvas.pollEvent(gameEvent);
colour += 1;
rs.Update(colour);
if(gameEvent.type == sf::Event::Closed)
quit = true;
rs.Canvas.clear( sf::Color(colour,0,0) );
rs.Canvas.draw( sprite );
rs.Canvas.display();
if(colour >= 300)
quit = true;
}
the rs.Canvas has these set
Canvas.setVerticalSyncEnabled(true);
Canvas.setFramerateLimit(60);
Can anyone see why my renderWindow would flicker?
From the SFML 2.0 tutorial, under the section Controlling the framerate:
Never use both setVerticalSyncEnabled and setFramerateLimit at the same time! They would badly mix and make things worse.
http://www.sfml-dev.org/tutorials/2.0/window-window.php#controlling-the-framerate
In the comments of your post, you state that you were calling clear twice on the window. This would effectively treat your window as if it were single buffered. I understand that the effect of this could create some screen tearing, but on my end I am not getting a glaring flicker effect. Make sure to remove your a call to either setting the framerate or to using the vertical sync, just to be sure.
Basically I was calling
rs.Canvas.clear
twice. I removed the second call and everything then worked fine

C++ SDL How to undo a BlitSurface

I have used SDL to display an image:
SDL_BlitSurface(sprite, NULL, screen, NULL);
My question is: Is it possible to remove the image from the window?
Generally speaking, no. SDL_BlitSurface() overwrites (a subset of) the contents of the destination surface, essentially the same as an assignment to an array of pixel data. One solution is to redraw the entire screen every frame, first clearing with:
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, r, g, b));
For better performance, you could keep track of which regions on the screen are “dirty” and need to be repainted each frame, and only repaint those regions. SDL offers some functions for doing that (SDL_UpdateRect() and SDL_UpdateRects()) but I wouldn’t bother unless rendering speed becomes a serious issue. Most SDL applications seem to be able to do 30–50 frames per second; beyond that, you’ll want to look at OpenGL.

SDL_Delay doesn't delay before each action

I have written program which has a delay between each drawn circle, but when i put for example SDL_Delay(2) everything is black for like 5 seconds and then i see everything already drawn, but i need to see everything from begining so that it would seem like an animation.
Here is my code:
while (t<tk){
l.a = l.a + (l.b - l.a) * t;
a=l.a;
Circle cir1(a,o);
draw_circle(cir1, canvas);
SDL_Delay(2);
t=t+0.001;
}
The thing is after each draw_circle you actually have to update the screen. Draw functions in almost all graphical engines write to a buffer and don't update the screen until you tell them to!
I don't know how this works with SDL that doesn't use OpenGL, but with double buffer OpenGL windows you should write SDL_GL_SwapBuffers() and then after it, start clearing the screen etc as if you are drawing the screen anew!
If it is a single buffer window, you should flush the buffer to update the screen. I never used SDL by itself (without OpenGL) so I don't know the function names, but with this hint you know why your code doesn't work and you should be able to find the functions you need from SDL documentation.