1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/*
* Benchmark for memory blenders.
*/
#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include <time.h>
#include "common.c"
/* Do a few un-timed runs to switch CPU to performance mode and cache
* data and so on - seems to make the results more stable here.
* Also used to guess the number of timed iterations.
*/
#define WARMUP 100
/* How many seconds the timing should approximately take - a fixed
* number of iterations is not enough on very fast systems but takes
* too long on slow systems.
*/
#define TEST_TIME 5.0
enum Mode {
ALL,
PLAIN_BLIT,
SCALED_BLIT,
ROTATE_BLIT
};
static char const *names[] = {
"", "Plain blit", "Scaled blit", "Rotated blit"
};
ALLEGRO_DISPLAY *display;
static void step(enum Mode mode, ALLEGRO_BITMAP *b2)
{
switch (mode) {
case ALL: break;
case PLAIN_BLIT:
al_draw_bitmap(b2, 0, 0, 0);
break;
case SCALED_BLIT:
al_draw_scaled_bitmap(b2, 0, 0, 320, 200, 0, 0, 640, 480, 0);
break;
case ROTATE_BLIT:
al_draw_scaled_rotated_bitmap(b2, 10, 10, 10, 10, 2.0, 2.0,
ALLEGRO_PI/30, 0);
break;
}
}
/* al_get_current_time() measures wallclock time - but for the benchmark
* result we prefer CPU time so clock() is better.
*/
static double current_clock(void)
{
clock_t c = clock();
return (double)c / CLOCKS_PER_SEC;
}
static bool do_test(enum Mode mode)
{
ALLEGRO_STATE state;
ALLEGRO_BITMAP *b1;
ALLEGRO_BITMAP *b2;
int REPEAT;
double t0, t1;
int i;
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
b1 = al_load_bitmap("data/mysha.pcx");
if (!b1) {
abort_example("Error loading data/mysha.pcx\n");
return false;
}
b2 = al_load_bitmap("data/allegro.pcx");
if (!b2) {
abort_example("Error loading data/mysha.pcx\n");
return false;
}
al_set_target_bitmap(b1);
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
step(mode, b2);
/* Display the blended bitmap to the screen so we can see something. */
al_store_state(&state, ALLEGRO_STATE_ALL);
al_set_target_backbuffer(display);
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);
al_draw_bitmap(b1, 0, 0, 0);
al_flip_display();
al_restore_state(&state);
log_printf("Benchmark: %s\n", names[mode]);
log_printf("Please wait...\n");
/* Do warmup run and estimate required runs for real test. */
t0 = current_clock();
for (i = 0; i < WARMUP; i++) {
step(mode, b2);
}
t1 = current_clock();
REPEAT = TEST_TIME * 100 / (t1 - t0);
/* Do the real test. */
t0 = current_clock();
for (i = 0; i < REPEAT; i++) {
step(mode, b2);
}
t1 = current_clock();
log_printf("Time = %g s, %d steps\n",
t1 - t0, REPEAT);
log_printf("%s: %g FPS\n", names[mode], REPEAT / (t1 - t0));
log_printf("Done\n");
al_destroy_bitmap(b1);
al_destroy_bitmap(b2);
return true;
}
int main(int argc, char **argv)
{
enum Mode mode = ALL;
int i;
if (argc > 1) {
i = strtol(argv[1], NULL, 10);
switch (i) {
case 0:
mode = PLAIN_BLIT;
break;
case 1:
mode = SCALED_BLIT;
break;
case 2:
mode = ROTATE_BLIT;
break;
}
}
if (!al_init()) {
abort_example("Could not init Allegro\n");
}
open_log();
al_init_image_addon();
al_init_primitives_addon();
init_platform_specific();
display = al_create_display(640, 480);
if (!display) {
abort_example("Error creating display\n");
}
if (mode == ALL) {
for (mode = PLAIN_BLIT; mode <= ROTATE_BLIT; mode++) {
do_test(mode);
}
}
else {
do_test(mode);
}
al_destroy_display(display);
close_log(true);
return 0;
}
/* vim: set sts=3 sw=3 et: */
|