André Wagner
2013-09-03 16:49:52 UTC
I'm using SDL2 to write a game that displays a tilemap at every frame,
but the performance is too slow. I wrote a little program to isolate
the problem. Consider that "temp.bmp" is a 16x16 image.
#include <stdio.h>
#include "SDL2/SDL.h"
#include "SDL2/SDL_timer.h"
#include "SDL2/SDL_image.h"
int main()
{
SDL_Window* win;
SDL_Renderer* ren;
int x, y;
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(800, 600, 0, &win, &ren);
SDL_Surface* sf = IMG_Load("temp.bmp");
SDL_Texture* tx = SDL_CreateTextureFromSurface(ren, sf);
for(;;) {
Uint32 t = SDL_GetTicks();
for(x=0; x<800; x+=16) {
for(y=0; y<600; y+=16) {
SDL_Rect src = { 0, 0, 16, 16 };
SDL_Rect dst = { x, y, 16, 16 };
SDL_RenderCopy(ren, tx, &src, &dst);
}
}
SDL_RenderPresent(ren);
printf("%ld ms\n", SDL_GetTicks() - t);
}
}
Running the program, I see that it takes about 16ms to render a frame.
This is exactly 60 FPS (1000/60), which leaves no room for game logic.
Also, I'm running it in a pretty fast computer.
I'm pretty sure I'm using the wrong strategy, but I'm not sure what be
the right one. Maybe creating a big texture and updating it less often
would be the way to go, but I couldn't find any documentation on how
to copy one texture over another.
Hacking a little into it, I found out that:
- acceleration is active;
- vsync is disabled;
- most of the time (90%) is spent in SDL_RenderPreset.
So, how can I improve the tilemap performance?
Thank you in advance,
André
but the performance is too slow. I wrote a little program to isolate
the problem. Consider that "temp.bmp" is a 16x16 image.
#include <stdio.h>
#include "SDL2/SDL.h"
#include "SDL2/SDL_timer.h"
#include "SDL2/SDL_image.h"
int main()
{
SDL_Window* win;
SDL_Renderer* ren;
int x, y;
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(800, 600, 0, &win, &ren);
SDL_Surface* sf = IMG_Load("temp.bmp");
SDL_Texture* tx = SDL_CreateTextureFromSurface(ren, sf);
for(;;) {
Uint32 t = SDL_GetTicks();
for(x=0; x<800; x+=16) {
for(y=0; y<600; y+=16) {
SDL_Rect src = { 0, 0, 16, 16 };
SDL_Rect dst = { x, y, 16, 16 };
SDL_RenderCopy(ren, tx, &src, &dst);
}
}
SDL_RenderPresent(ren);
printf("%ld ms\n", SDL_GetTicks() - t);
}
}
Running the program, I see that it takes about 16ms to render a frame.
This is exactly 60 FPS (1000/60), which leaves no room for game logic.
Also, I'm running it in a pretty fast computer.
I'm pretty sure I'm using the wrong strategy, but I'm not sure what be
the right one. Maybe creating a big texture and updating it less often
would be the way to go, but I couldn't find any documentation on how
to copy one texture over another.
Hacking a little into it, I found out that:
- acceleration is active;
- vsync is disabled;
- most of the time (90%) is spent in SDL_RenderPreset.
So, how can I improve the tilemap performance?
Thank you in advance,
André
--
Ars longa, vita brevis, occasio praeceps, experientia fallax, iudicium
difficile.
Ars longa, vita brevis, occasio praeceps, experientia fallax, iudicium
difficile.