I wrote a test program yesterday to compare SDL’s and Allegro’s performance
under Win32. To my horror I found out that SDL is, in the best case, twice
as slow as Allegro (even when colour depths match, and even in full-screen
mode). Ouch! I’m on a 1.4GHz T-Bird running Win2k with a Matrox G450 card.
I checked SDL_GetVideoInfo() and SDL_VideoDriverName() – it’s using directx
alright and everything other than Alpha is accelerated (HW/SW/SW->HW
blitting, etc). I can’t get SDL to take less than 30%-60% CPU in any mode
(full-screen/hw-surface/double-buffered, windowed/sw-surface, etc.), while
with Allegro I get <5% CPU in full-screen mode, and 27% is windowed mode.
My first guess would have been that Allegro does any bpp conversion via
DDraw whereas SDL does it “manually”, but that would not explain the <5%
(Allegro) vs. 30% (SDL) performance hit while in full-screen mode. Any
thoughts? Am I doing something particularly stupid here?!########################################################
SDL code
########################################################
#include <stdlib.h>
#include “SDL.h”
int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_TIMER);
SDL_Surface *screen;
screen = SDL_SetVideoMode(640, 480, 24,
SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
atexit(SDL_Quit);
SDL_ShowCursor(SDL_DISABLE);
SDL_Event event;
int i, j, c = 0;
while(1) {
SDL_LockSurface(screen);
for (i = 0; i < screen->h; i++) {
for (j = 0; j < (screen->w3); j++) {
((unsigned char)(screen->pixels))[i * screen->pitch + j] = c;
}
}
c+=16;
SDL_UnlockSurface(screen);
SDL_Flip(screen);
if (SDL_PollEvent(&event) && (((event.type == SDL_KEYDOWN) &&
(event.key.keysym.sym == SDLK_ESCAPE)) ||
(event.type == SDL_QUIT)))
{
exit(0);
}
SDL_Delay(30);
}
}
########################################################
Allegro code
########################################################
#include <stdlib.h>
#include “allegro.h”
int main(int argc, char *argv[]) {
allegro_init();
install_keyboard();
install_timer();
set_display_switch_mode(SWITCH_BACKGROUND);
set_color_depth(24);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
BITMAP* pages[2];
pages[0] = create_video_bitmap(640, 480);
pages[1] = create_video_bitmap(640, 480);
int cpage = 0;
int i, j, c = 0;
while (1) {
BITMAP* pixels = pages[cpage];
acquire_bitmap(pixels);
for (i = 0; i < pixels->h; i++) {
unsigned long address = bmp_write_line(pixels, i);
bmp_select(pixels);
for (j = 0; j < (pixels->w*3); j++) {
bmp_write8(address+j, c);
}
bmp_unwrite_line(pixels);
}
c+=16;
release_bitmap(pixels);
show_video_bitmap(pixels);
cpage = 1 - cpage;
poll_keyboard();
if (key[KEY_ESC])
break;
rest(30);
}
return 0;
}
END_OF_MAIN();