Lab: Game Engine Refactor
This lab starts from the beginning of the fancy sprites lab. That is, it has the sprite renderer from the 2D Scenes and Geometry lecture notes but doesn't rely on the fancy stuff.
These were the steps I took to refactor my game engine code, but you can do what you want:
- I made a WGPU struct to store the device, queue, adapter, instance, surface, and surface config. Its new() function takes a window and sets up the surface. I also changed load_texture to be a method of WGPU.
- I tried to make a function on WGPU to create a render pass and automatically present the frame when it finished, but this was annoying with lifetimes because of some constraints imposed by winit and the types coming out of wgpu, so I didn’t end up doing that.
- Render pipeline and bind group layout creation (along with a camera buffer) moved into a new SpriteRenderer struct. It has a Vec<SpriteGroup> in it, and each spritegroup has a texture, a texture bind group, a sprite buffer, a vec of sprites, and a sprite bind group. SpriteRenderer has some conveniences for getting a slice of sprites from a numbered group or for uploading a range of sprites to the gpu, as well as a function to call (with a renderpass argument) to render all the sprite groups.
- After the video ended, I wrapped up gpu, input, sprite_renderer into an Engine struct, along with the window and event loop. It has a start() function with most of what used to be main, and it takes a value implementing the Game trait (which has an init(&mut self, &mut Engine) function and an update(&mut self, &mut Engine) function).
- Finally I made a TestGame struct implementing Game and moved the sprite sheet texture loading and sprite renderer add_sprite_group call into the implementation of TestGame::init . I changed main to call Engine::start with a value of my new TestGame type.
I made a couple of videos, but the points above should be sufficient:
- https://pomona.box.com/s/iuxnux9fpd26trj0k77pvmju73ukuctc – you can skip a good twenty minutes of this one while I fumble around with lifetimes.
- https://pomona.box.com/s/px9febq9yt752b362cxwtsm22prt5gm4
I ended up with https://cs.pomona.edu/classes/cs181g/examples/engine2d.zip and https://cs.pomona.edu/classes/cs181g/examples/test2d.zip , but you can come up with your own solution too.
Either way, show me or a TA what you have when things are nice and organized!