Handmade Hero»Forums»Code
Burmi
4 posts
code philosophy and the UpdateandRender function
Hello Everyone,
I started to watch the Videos of Casey a while ago and I'm currently at Day 16 - Compiler Switches, but I took a look at Day 26+27.

I got two little questions:

- 1st: I'm still not completly sure about the code philosophy "write the usage code first". And I want to give an example:
- Let's say I did some foundational work for an game engine.
- Now I want to add an vectorgraphic renderer to this engine, but never did this.
So the first thing I doo is to google what vectorgraphics are about, to learn the basic foundation and the different techniques. In this case I would try to learn about the mathematical concept of vectors and which rendering techniques for vectorgraphics are common.
- After I did this I will some code that will endup being neither performance-optimized nor structural-optimized. "usage code".
- When I got the thinks I want to work and therefore I know what my renderer need to be able to and now I could write some little diagramms and try to think about the structure of this subsystem of the engine. And this structure fits in to the rest of the engine. If I'm going to encapsulate it or not and so on.
- Now I rewrite the code to fit this structure and after this, this topic is done for now (may be changed later if some other subsystems needs more stuff from this vectorrenderer etc etc.)

So does this example fit in the "write usage code first" philosophy?


- 2nd: In the "Day 26" video, Casey talks about that he wants to have the update and render functionality in one function to avoid that one object is loaded twice into the CPU cache, once for updating and once for rendering. So the whole process is brought done to an per object basis. I update an object I do some other stuff with it and render it. Then I'm going to repeat this process for the next object. But the order I update objects and the order I render objects matters for some operations. But both of these operations are dependent on other factors which determin the sequence in which these operations are executed.
What I mean is that you could create a bug if you update the wrong object first you might get a glitch (Casey used an example of a buggy collision detection of a bullet if the player moved first). But if you just depend the sequence of the update part and just render an object directly after it's updated, some rendering techniques won't work correclty. I know that 3D Alpha blending requires that you order Alpha-Rendered Objects with the distance to the camera. The Alpha-Blendet-Object that is behind the other need to be rendered first, or your blending will have a glitch, afaik. There are maybe some 2D Render techniques that require, that you render something in a specific order (layer?). So some rendering techniques require, that you render in a specific order and some updates require that you update in a specific order.
So at the moment I don't know how to avoid this problem (maybe Casey talks about this in a later Episode). So is this a problem?

Thanks in advance B)
Mārtiņš Možeiko
2568 posts / 2 projects
code philosophy and the UpdateandRender function
Answering second part of your question - "rendering and updating in one function" doesn't mean that you need to call render functionality (pixel blitting, GL/D3D calls) at the same time as you update objects.

Typically you create render commands in separate buffer and only when you have finished with processing all objects, then you actually call rendering functionality. This way there is no issues with transparency, because render commands or draw list (whatever you want to call it) will take care of processing render commands in correct order.

And this doesn't mean there should be only one render command buffer. You could create separate ones for smaller render calls. For example, shadow mapping could go into separate render buffer which get's processed before the real rendering starts (because you need shadow textures to be rendered).
Burmi
4 posts
code philosophy and the UpdateandRender function
So if I understand correctly, only because I have one Function, the CPU will load all my objects into cache at once (as long as the memory size of all my objects and temp Variablr in the UpdateAndRender Function do not exceed the size of the Cache?

So I call the UpdateAbdRender Function, which uses some kind of List of all the Entities/Objects that exist in the Game. By calling this function everything the function needs is loaded into cache. Then I process all Updates in an sequence I desire and every objects writes itself into o e of the renderqueues. After evrything is done, I just go through every renderqueue in the right sequence and it works. Would it make sence to habdle Layers in an 2D Game that way? Or only via thr Z-Axis and the Depthtest?
I know that all stuff i put on the cache with the UpdateAndRender Fu tion might get thrown off qgain, if other programms or windows needs that place. But atleast no code I wrote caused that the objects were loaded twice into the cache xD