Advertisement

How to make a Splash Screen with fading effect

Started by August 06, 2024 01:02 AM
4 comments, last by linda45 3 weeks, 5 days ago

I want to make a Game Splash Screen using of course OpenGL and SDL, I searched how to make it, you know the first thing people do when they don't know how to do something? I didn't find shit about how to do it, apparently I'm the first in the world asking this. So I had the idea of ​​simply drawing a image the size of the screen, because I'm running the game in fullscreen mode, and I found out about “BlitFramebuffer”, here is the code I'm using for loading the Texture:

int channels = 0;
	    
unsigned char* image = SOIL_load_image(path.c_str(), &mWidth, &mHeight, &channels, SOIL_LOAD_AUTO);
	    
if (image == nullptr)
{
    SDL_Log("SOIL failed to load image %s: %s", path.c_str(), SOIL_last_result());
    return false;
}
	    
int format = GL_RGB;
if (channels == 4)
{
    format = GL_RGBA;
}
	    
glGenTextures(1, &mTextureID);
glBindTexture(GL_TEXTURE_2D, mTextureID);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	    
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

SOIL_free_image_data(image);

And to draw the BlitFramebuffer:

GLuint fboID = 0;
glGenFramebuffers(1, &fboID);
glBindFramebuffer(GL_READ_FRAMEBUFFER, fboID);
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

glBlitFramebuffer(0, 0, textureWidth, textureHeight, 0, 0, textureWidth, textureHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR);

It works, despite the image being drawn upside down (simple to solve, just use some image editor and flip the image) and the alpha being drawn as black for some reason, something that I also didn't found shit about. But ok I guess.

The other thing some Splash Screens in games have is the fading effect, it should be something simple as changing the alpha, but apparently it isn't, I didn't found shit about how to do it even less because I'm using “BlitFramebuffer”, I found out about using shader and quads, but I don't want to use shaders if possible. But this is the way I tried, I don't know if there is another ways.

I don't know if there is another ways.

Overly a giant quad over the top, textured or not, with alpha seems easy. A quick shader is also easy. Lighting is a little more effort but can look nice.

You will figure out more as you gain experience.

Advertisement

I don't know about that, to make this way, besides loading the texture, the only thing I needed was a the ID for the Frame Buffer. I guess to create a quad I would need something else, maybe a Vertex Buffer ID, which is more stuff and per definition of the word, harder. I will not even mention shaders, I will need 3 ID's, 1 for the Shader Program and the other two for each shader, and also two const chars, for each shader, which is more straight forward that loading each shader from a file, which would be even more code. So no, with shaders is also per definition of the word, harder. And also, I don't know how to code shaders, which is even more harder…

At this early stage of learning, yes, it feels like more work.

Later on when you are more comfortable with larger systems the other solutions will end up being less work.

It is fine, struggling with it is an essential part of the process. Right now at your current skill level that approach of manipulating the frame buffer is your best solution. The performance implications and systemic complexity are both part of a bigger picture that over time will be a skill you develop.

If you already have the systems in place a shader makes it extremely easy, probably the easiest option. But for now, learning to use the other methods will expand your knowledge and skill.

You are not understanding, as always of course, I'm not using shaders because it's more work, I'm not using them because I don't want to. I don't care about performance, I didn't ask anyone about this, I didn't even mention this word in my question…

Advertisement
Advertisement