Tuesday, March 1, 2016

c++ - Which buffering process would be more efficient?


I'm working on the base engine of my game right now, specifically converting a text file into a level on-screen. As a 2D platformer, each level has an array of rectangles that can be drawn to the screen. Obviously I don't want to draw all these bad boys to the screen every frame, as that would slow everything down. I've thought up two solutions to this problem myself:




  • Draw these rects to a separate texture (I'm using SDL2, by the way) and paint a specific portion to the screen every frame





  • Choose which rects to draw every frame based on the player's position by checking collision between every rect and an imaginary moving "chunk"




My question: Which of these two processes, if either, would be less of a burden to the cpu? If neither seem reasonable, what other alternatives are there?



Answer



I found that with plain SDL2, even using the accelerated renderer, that redrawing all the tiles every frame was a huge bottleneck. I would suggest drawing as much as you can to a static texture that you then draw using the camera's clipping rectangle. Now, I was drawing around 3600 tiles per frame (8x8 tiles at 640x360) on a relatively low-powered laptop, so your use case may not be a problem.


The best solution would probably be to dip into OpenGL and store all the tiles in a vertex buffer (or a grid of vertex buffers) and draw those instead. Advantage being that it would save memory and you wouldn't be constricted to using basically only one tile layer per texture.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...