Advanced C++/Graphics Tutorial 22 pt.1: SpriteBatch, VAO, std::sort

preview_player
Показать описание
Source code is in part two!

Today we begin creating the SpriteBatch class, which will help us optimize our rendering! This is a rather long video so I split it into two parts! You will learn a lot today :)

Рекомендации по теме
Комментарии
Автор

I've done so many OGL tutorials utilizing the VAO and I have never understood exactly what it did, until this tutorial that actually explained it properly for once.

carbon
Автор

A NOTE TO CODEBLOCKS USERS: You can also compile both projects at the same time. The way you do this in Code::Blocks is first, you need to make certain that the Bengine project is the first project listed in the Workspace window, and the reason for this is that it will compile the projects in the order listed, so you want Bengine to always compile first. If Bengine is not first, not a problem, just right click on the Bengine project in the Workspace window, then select Project Tree then select "Move Project Up" so it is moved to the top of the Workspace window.

Once you have the projects in order, just go to the BUILD menu and select BUILD, REBUILD or CLEAN WORKSPACE and it will build ALL your projects you have loaded, in order that they are listed (which is why you need Bengine first).

You can also SAVE your WORKSPACE to a file, preferably in the folder that contains your graphics tutorial and bengine folders. Just name the workspace file to something like GRAPHICS TUTORIAL or whatever you wish and then when you go to reload these projects, instead of opening them individually, you can just double click the Workspace file you just saved and it will automatically load both projects just as they were when you saved it. Very handy.

For my own setup I have a "graphics tutorials" folder, inside that I have a Bengine folder with all the Bengine files in it, then I have another "graphics_tutorial" folder with all the tutorial's files (maingame.cpp etc). The graphics_tutorial folder has my graphics_tutorial.cpb file you used to click on to load the project inside of it. Now in this main folder with all the various project folders is where I save my Workspace files to, so I can just select the workspace file for the appropriate project and it loads up Bengine and the project together. For each project I set "..\Bengine\bin" as my Linker search folder, so it looks there for the libraries (I don't use a deps folder, they are all in a separate folder I used for all my programming), that is where Bengine compiles the libs to. And I set all project search folders to "..\..\graphics_tutorials" for my compiler search folders (this will vary depending on where you keep your projects, basically the main folder that contains all the other folders, then when you load the Library headers, you enter <Bengine/whatever.h> and it looks in here for the Bengine folder etc.

NeilRoy
Автор

Cool thing I found in Code::Blocks (I'm almost sure VS has something of the like): go to Bengine project's Build Options -> Debug (or Release) -> Pre-post build steps, then add the following Windows commands to post build:

cmd /c copy "C:\CB\deps\lib"
cmd /c for %I in (*.h *.cpp) do copy %I C:\CB\deps\include\Bengine\

My libs folder is located in C:\CB\deps\lib, and my includes folder is at C:\CB\deps\include\Bengine. If you want to compile your stuff in whatever folder, but then add them in a common target lib/include folder this is a nice automated way of doing such! :)

paulocbbf
Автор

Great series! Thoughts on this episode: I'm not convinced I want to use pointers for glyphs: 1) memory allocation/deallocation is not free so less they're used the better. 2) using proper vector ensures better cache friendliness . 3) less allocations you use - less chance of fragmenting memory. I just added `glyphs.reserve(32)` in SpriteBatch::init.

etopowertwon
Автор

shoving disgustingly written lambdas into std::sort is my jam

wade
Автор

Hey Ben, I think you could add some annotations to the video stating that although a vec4 expects fully-specified 3D coordinates in homogenous space (hence x, y, z, and w), you've changed how you INTERPRET z and w. For this draw() method you are really using z for width and w for height, since you're only concerned with 2D coordinates (x, y). You're just simplifying how you're storing x, y, width (z) and height (w). You could use Vector struct but then there would be many unused components such as color, and for destRect you only want spatial/dimensional values.

The annotation could simply be "Here I'm using Z component for my WIDTH" "And here I'm using W component for my HEIGHT". ^^

paulocbbf
Автор

1:39 So wouldn't be easier let the additional libraries linked in the Engine and make graphics tutorial project only links with engine.lib?

BlackPhillip-swxf
Автор

Voice of an angel. "Hey guys!"lol

LittleMikeStarCraft
Автор

the glyph pointers (a and b ) in the comparative functions is acting
really weird. The b might be a nullptr for some reason. It is giving me
an error when comparing a->texture and b->texture.

There is a window coming up saying "Debug Assertion failed", something with <algorithm> line 2899 and then it says "Expression: Invalid iterator range". I can say "try again". and then it continues to debug until another window comes up saying "SDL.exe has triggered a breakpoint. If I try: std::cout << a->texture << std::endl; it gives me "'a' was 0x8" attempting the same thing with b gives me that it was a nullptr. So it is comparing two different values. Which obviously would be giving me an error, the question is why would be be a nullptr? I'l assume that b is "glyphs.begin(), because my code is written like yours, with it as the second parameter in std::stable_sort. under case case GlyphSortType::TEXTURE.

Any idea what might be wrong?

Zetzitsen
Автор

Can we use lambda functions in std::stable_sort (C++11)? I find them incredibly convenient for such task

yimoawanardo
Автор

Is there any benefit of using a glm::vec4 verses an SDL_Rect? because SDL_Rect already has x, y, width, and height defined inside it

jhlcompositions
Автор

But now he is storing his glyphs on the heap with new Glyph. Isn't it faster to have your variables on the stack?

fahdv
Автор

+MakingGamesWithBen shouldn't it be
, destRect.y+destRect.w)

atulitsrivastava
Автор

Why do we need to sort it, because I didn't exactly understand and why do you so many classes and structs instead of just one Texture class?

scshout
Автор

Just wondering, but wouldn't it make more sense to include the libraries in the engine rather than the game, removing the need to link them each time you create a new game?

Technogux
Автор

why does stable sort matter? I mean if they have a different texture the order is going to be ignored anyways so might aswell use the slightly faster sort :)

llaapk
Автор

In GLSLProgram, in function compileShader() why the Shader ID is not passed by referrence?

alanduarte
Автор

For production efficiency, is there a way to leave all the dependencies to the engine and not have the developer have to include them all in everytime the engine is used in the project?

Zephemus
Автор

anyone has an idea why i have to use Vertex::Color as a parameter in void draw() instead of just using Color like Ben did?

morrisbirkholz
Автор

any idea how it was still working even though we only enabled glEnableVertexAttribArray(0)?

smodman