I'm currently working on a 2D-Game and i want to draw and calculate the whole screen myself through a pixel array. I'm programming in C++, using SDL 2 and OpenGL specifically a OpenGL Texture which is generated through my manipulated pixel array. My rendering System basically works like this:
- Pixelvalues from a spritesheet are getting put into the pixel array (those are all the tiles that should be rendered).
- The RGB values of every pixel are getting multiplied by a certain value to get the effect of lighting.
- The manipulated pixel array is being passed to OpenGL to generate a Texture which is then drawn to the screen.
And the problem is that especially the lighting calculations cost a lot of time since its all being calculated on the CPU and even though all i'm doing is drawing the very basic things my FPS is maxed out at like 160 (on a quite good computer). And after profiling my code i also got to the conclusion that mostly the lighting function takes up a lot of time.
Lighting function:
void PixelRenderer::applyLight(Uint32& color, Uint32 factor){
Uint32 red = (((color >> 16) & 0xff)* factor) >> 8;
Uint32 green = (((color >> 8) & 0xff)* factor) >> 8;
Uint32 blue = (((color) & 0xff)* factor) >> 8;
if (red > 255) red = 255;
if (green > 255) green = 255;
if (blue > 255) blue = 255;
color = (color & 0xFF000000) | red << 16 | green << 8 | blue;
}
and this is done for every single pixel on the screen (1024*576 resolution and 32x32 tileSize)
So my question basically is if theres any way i can do those calculations on the GPU to improve my performance.