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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include "SDL.h"
#include "sge.h"
#define NUM_RANDOM 1000
using namespace std;
/* Bitmap fonts */
sge_bmpFont *font;
sge_bmpFont *cfont;
/* The texture */
SDL_Surface *texture;
/* Coordinates */
Sint16 xl1[NUM_RANDOM], xl2[NUM_RANDOM], y1[NUM_RANDOM], y2[NUM_RANDOM];
Sint16 xl3[NUM_RANDOM], xl4[NUM_RANDOM], y3[NUM_RANDOM], y4[NUM_RANDOM];
Sint16 xr1[NUM_RANDOM], xr2[NUM_RANDOM]; //xl - left, xr -right coord
Sint16 xr3[NUM_RANDOM], xr4[NUM_RANDOM];
/* Radii and offsets */
Sint16 r1[NUM_RANDOM], r2[NUM_RANDOM];
/* Colors and alpha */
Uint32 col1[NUM_RANDOM], col2[NUM_RANDOM], col3[NUM_RANDOM];
Uint8 r[NUM_RANDOM], g[NUM_RANDOM], b[NUM_RANDOM];
Uint8 a[NUM_RANDOM];
/* Clear and redraw left */
void init_screen_left(SDL_Surface *screen)
{
SDL_Rect r;
r.x=0; r.y=0; r.w=screen->w; r.h=screen->h;
SDL_SetClipRect(screen, &r);
sge_ClearSurface(screen,SDL_MapRGB(screen->format,0,0,0));
sge_BF_textoutf(screen, font, 5, 2, "%s","SGE drawing primitives performance test - LMB for next test, RMB for previous.");
sge_HLine(screen,0, screen->w, 16, 255,255,255);
sge_HLine(screen,0, screen->w, 35, 255,255,255);
sge_VLine(screen, screen->w/2, 16,screen->h, 255,255,255);
if (SDL_MUSTLOCK(screen))
if (SDL_LockSurface(screen) < 0)
return;
/* Draw the colored area */
Sint16 x,y;
float stepx, stepy, fx, fy, fxy;
stepx=1.0/(screen->w/2-screen->w/8);
stepy=1.0/(screen->h-36 - (screen->h-36)/4);
fx=0.0;
for (x=0; x<screen->w/2; x++) {
fy=0.0;
for (y=36; y<screen->w; y++) {
fxy=1.0-fx*fy;
_PutPixel(screen,x,y,(int)(128.0*fx*fx),(int)(128.0*fxy*fxy),(int)(128.0*fy*fy));
fy += stepy;
}
fx += stepx;
}
if (SDL_MUSTLOCK(screen)) {
SDL_UnlockSurface(screen);
}
SDL_UpdateRect(screen,0,0,0,0);
r.x=0; r.y=37; r.w=screen->w/2; r.h=screen->h-37;
SDL_SetClipRect(screen, &r);
}
/* Clear and redraw right */
void init_screen_right(SDL_Surface *screen)
{
SDL_Rect r;
r.x=0; r.y=0; r.w=screen->w; r.h=screen->h;
SDL_SetClipRect(screen, &r);
if (SDL_MUSTLOCK(screen))
if (SDL_LockSurface(screen) < 0)
return;
/* Draw the colored area */
Sint16 x,y;
float stepx, stepy, fx, fy, fxy;
stepx=1.0/(screen->w/2-screen->w/8);
stepy=1.0/(screen->h-36 - (screen->h-36)/4);
fx=0.0;
for (x=screen->w/2+1; x<screen->w; x++) {
fy=0.0;
for (y=36; y<screen->w; y++) {
fxy=1.0-fx*fy;
_PutPixel(screen,x,y,(int)(128.0*fx*fx),(int)(128.0*fxy*fxy),(int)(128.0*fy*fy));
fy += stepy;
}
fx += stepx;
}
if (SDL_MUSTLOCK(screen)) {
SDL_UnlockSurface(screen);
}
SDL_UpdateRect(screen,0,0,0,0);
r.x=screen->w/2+1; r.y=37; r.w=screen->w/2; r.h=screen->h-37;
SDL_SetClipRect(screen, &r);
}
/* Macro to test 'function' in the left part of the screen */
#define RUN_TEST_LEFT(screen,title,testloops,showloops,lock,function)\
{\
SDL_Rect rect;\
rect.x=0; rect.y=37; rect.w=screen->w/2; rect.h=screen->h-37;\
SDL_SetClipRect(screen, &rect);\
\
if (SDL_MUSTLOCK(screen) && lock){\
sge_Lock_OFF();\
if (SDL_LockSurface(screen) < 0)\
exit(-1);\
}else\
sge_Lock_ON();\
\
int i=0;\
Uint32 start = SDL_GetTicks();\
for (int j=0; j<testloops; j++) {\
i=j%(NUM_RANDOM-1);\
function;\
}\
Uint32 stop = SDL_GetTicks();\
\
if (SDL_MUSTLOCK(screen) && lock) {\
SDL_UnlockSurface(screen);\
}\
sge_Lock_ON();\
\
init_screen_left(screen);\
\
if (SDL_MUSTLOCK(screen) && lock){\
sge_Lock_OFF();\
if (SDL_LockSurface(screen) < 0)\
exit(-1);\
}else\
sge_Lock_ON();\
\
for (int j=0; j<showloops; j++) {\
i=j%(NUM_RANDOM-1);\
function;\
}\
\
if (SDL_MUSTLOCK(screen) && lock) {\
SDL_UnlockSurface(screen);\
}\
sge_Lock_ON();\
\
\
rect.x=0; rect.y=0; rect.w=screen->w; rect.h=screen->h;\
SDL_SetClipRect(screen, &rect);\
sge_BF_textoutf(screen, font, 5, 20, "%s - %.0f/s.",title,((double)testloops*1000)/(stop-start));\
printf("%s - %.0f/s.\n",title,((double)testloops*1000)/(stop-start));\
SDL_UpdateRect(screen,0,0,0,0);\
}
/* Macro to test 'function' in the right part of the screen */
#define RUN_TEST_RIGHT(screen,title,testloops,showloops,lock,function)\
{\
SDL_Rect rect;\
rect.x=screen->w/2+1; rect.y=37; rect.w=screen->w/2; rect.h=screen->h-37;\
SDL_SetClipRect(screen, &rect);\
\
if (SDL_MUSTLOCK(screen) && lock){\
sge_Lock_OFF();\
if (SDL_LockSurface(screen) < 0)\
exit(-1);\
}else\
sge_Lock_ON();\
\
int i=0;\
Uint32 start = SDL_GetTicks();\
for (int j=0; j<testloops; j++) {\
i=j%(NUM_RANDOM-1);\
function;\
}\
Uint32 stop = SDL_GetTicks();\
\
if (SDL_MUSTLOCK(screen) && lock) {\
SDL_UnlockSurface(screen);\
}\
sge_Lock_ON();\
\
init_screen_right(screen);\
\
if (SDL_MUSTLOCK(screen) && lock){\
sge_Lock_OFF();\
if (SDL_LockSurface(screen) < 0)\
exit(-1);\
}else\
sge_Lock_ON();\
\
for (int j=0; j<showloops; j++) {\
i=j%(NUM_RANDOM-1);\
function;\
}\
\
if (SDL_MUSTLOCK(screen) && lock) {\
SDL_UnlockSurface(screen);\
}\
sge_Lock_ON();\
\
\
rect.x=0; rect.y=0; rect.w=screen->w; rect.h=screen->h;\
SDL_SetClipRect(screen, &rect);\
sge_BF_textoutf(screen, font, screen->w/2+6, 20, "%s - %.0f/s.",title,((double)testloops*1000)/(stop-start));\
printf("%s - %.0f/s.\n",title,((double)testloops*1000)/(stop-start));\
SDL_UpdateRect(screen,0,0,0,0);\
}
/* Setup random data */
void rand_data(SDL_Surface *screen)
{
Uint8 R,G,B;
for (int i=0; i<NUM_RANDOM; i++) {
xl1[i]=sge_Random(0,screen->w/2-1);
xl2[i]=sge_Random(0,screen->w/2-1);
xl3[i]=sge_Random(0,screen->w/2-1);
xl4[i]=sge_Random(0,screen->w/2-1);
y1[i]=sge_Random(37,screen->h);
y2[i]=sge_Random(37,screen->h);
y3[i]=sge_Random(37,screen->h);
y4[i]=sge_Random(37,screen->h);
xr1[i]=screen->w/2 + 1 + xl1[i];
xr2[i]=screen->w/2 + 1 + xl2[i];
xr3[i]=screen->w/2 + 1 + xl3[i];
xr4[i]=screen->w/2 + 1 + xl4[i];
r1[i]=sge_Random(5,60);
r2[i]=sge_Random(5,60);
R=sge_Random(0,255);
G=sge_Random(0,255);
B=sge_Random(0,255);
col1[i]=SDL_MapRGB(screen->format,R,G,B);
R=sge_Random(0,255);
G=sge_Random(0,255);
B=sge_Random(0,255);
col2[i]=SDL_MapRGB(screen->format,R,G,B);
R=sge_Random(0,255);
G=sge_Random(0,255);
B=sge_Random(0,255);
col3[i]=SDL_MapRGB(screen->format,R,G,B);
r[i]=R;
g[i]=G;
b[i]=B;
a[i]=sge_Random(40,240);
}
}
int main(int argc, char** argv)
{
/* Init SDL */
if ( SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't load SDL: %s\n", SDL_GetError());
exit(1);
}
/* Clean up on exit */
atexit(SDL_Quit);
/* Set window title */
SDL_WM_SetCaption("SpeedTest", "speedtest");
/* Handle arguments */
int video_bpp = 0;
Uint32 videoflags = SDL_SWSURFACE;
while ( argc > 1 ) {
--argc;
if ( string(argv[argc-1]) == "-bpp" ) {
video_bpp = atoi(argv[argc]);
--argc;
} else
if ( string(argv[argc]) == "-hw" )
videoflags |= SDL_HWSURFACE;
else
if ( string(argv[argc]) == "-fullscreen" )
videoflags |= SDL_FULLSCREEN;
else {
fprintf(stderr,"Usage: %s [-bpp N] [-hw] [-fullscreen]\n",argv[0]);
exit(1);
}
}
/* Initialize the display */
SDL_Surface *screen;
screen = SDL_SetVideoMode(640, 480, video_bpp, videoflags);
if ( screen == NULL ) {
fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
exit(1);
}
/* Open bitmap fonts, optimized for blits or color change */
font=sge_BF_OpenFont("font.bmp",SGE_BFTRANSP);
if(font==NULL){fprintf(stderr,"Error: %s\n", SDL_GetError());exit(1);}
cfont=sge_BF_OpenFont("font.bmp",SGE_BFTRANSP|SGE_BFPALETTE);
if(cfont==NULL){fprintf(stderr,"Error: %s\n", SDL_GetError());exit(1);}
/* Load the texture */
SDL_Surface *temp;
temp = SDL_LoadBMP("tux.bmp");
if ( temp == NULL ) {
fprintf(stderr, "Error: %s\n", SDL_GetError());
exit(1);
}
texture = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
sge_Update_OFF();
sge_Lock_OFF();
sge_Randomize();
rand_data(screen);
SDL_Delay(100); /* Time for the system to calm down */
/* Main loop */
SDL_Event event;
int op = 1;
int next_op = 1;
do{
switch ( op ) {
case 1:
RUN_TEST_LEFT(screen,"Pixels",500000,1000,true,_PutPixel(screen,xl1[i],y1[i],col1[i]));
RUN_TEST_RIGHT(screen,"Pixels (alpha)",300000,1000,true,_PutPixelAlpha(screen,xr1[i],y1[i],col1[i],a[i]));
op = 0;
break;
case 2:
RUN_TEST_LEFT(screen,"Horizontal lines",150000,200,false,sge_HLine(screen,xl1[i],xl2[i],y1[i],col1[i]));
RUN_TEST_RIGHT(screen,"Horizontal lines (alpha)",15000,200,true,sge_HLineAlpha(screen,xr1[i],xr2[i],y1[i],col1[i],a[i]));
op = 0;
break;
case 3:
RUN_TEST_LEFT(screen,"Vertical lines",7000,200,false,sge_VLine(screen,xl1[i],y1[i],y2[i],col1[i]));
RUN_TEST_RIGHT(screen,"Vertical lines (alpha)",6000,200,true,sge_VLineAlpha(screen,xr1[i],y1[i],y2[i],col1[i],a[i]));
op = 0;
break;
case 4:
RUN_TEST_LEFT(screen,"Normal lines",7000,200,true,sge_Line(screen,xl1[i],y1[i],xl2[i],y2[i],col1[i]));
RUN_TEST_RIGHT(screen,"Normal lines (alpha)",4000,200,true,sge_LineAlpha(screen,xr1[i],y1[i],xr2[i],y2[i],col1[i],a[i]));
op = 0;
break;
case 5:
RUN_TEST_LEFT(screen,"Anti-aliased lines",1000,200,true,sge_AALine(screen,xl1[i],y1[i],xl2[i],y2[i],col1[i]));
RUN_TEST_RIGHT(screen,"Anti-aliased lines (alpha)",1000,200,true,sge_AALineAlpha(screen,xr1[i],y1[i],xr2[i],y2[i],col1[i],a[i]));
op = 0;
break;
case 6:
RUN_TEST_LEFT(screen,"Rectangles",6000,100,false,sge_Rect(screen,xl1[i],y1[i],xl2[i],y2[i],col1[i]));
RUN_TEST_RIGHT(screen,"Rectangles (alpha)",2000,100,true,sge_RectAlpha(screen,xr1[i],y1[i],xr2[i],y2[i],col1[i],a[i]));
op = 0;
break;
case 7:
RUN_TEST_LEFT(screen,"Filled rectangles",2000,100,false,sge_FilledRect(screen,xl1[i],y1[i],xl2[i],y2[i],col1[i]));
RUN_TEST_RIGHT(screen,"Filled rectangles (alpha)",100,100,true,sge_FilledRectAlpha(screen,xr1[i],y1[i],xr2[i],y2[i],col1[i],a[i]));
op = 0;
break;
case 8:
RUN_TEST_LEFT(screen,"Circles",7000,100,true,sge_Circle(screen,xl1[i],y1[i],r1[i],col1[i]));
RUN_TEST_RIGHT(screen,"Circles (alpha)",4000,100,true,sge_CircleAlpha(screen,xr1[i],y1[i],r1[i],col1[i],a[i]));
op = 0;
break;
case 9:
RUN_TEST_LEFT(screen,"AA circles",3000,100,true,sge_AACircle(screen,xl1[i],y1[i],r1[i],col1[i]));
RUN_TEST_RIGHT(screen,"AA circles (alpha)",1000,100,true,sge_AACircleAlpha(screen,xr1[i],y1[i],r1[i],col1[i],a[i]));
op = 0;
break;
case 10:
RUN_TEST_LEFT(screen,"Filled circles",1000,100,false,sge_FilledCircle(screen,xl1[i],y1[i],r1[i],col1[i]));
RUN_TEST_RIGHT(screen,"Filled circles (alpha)",500,100,true,sge_FilledCircleAlpha(screen,xr1[i],y1[i],r1[i],col1[i],a[i]));
op = 0;
break;
case 11:
RUN_TEST_LEFT(screen,"Ellipses",4000,100,true,sge_Ellipse(screen,xl1[i],y1[i],r1[i],r2[i],col1[i]));
RUN_TEST_RIGHT(screen,"Ellipses (alpha)",3000,100,true,sge_EllipseAlpha(screen,xr1[i],y1[i],r1[i],r2[i],col1[i],a[i]));
op = 0;
break;
case 12:
RUN_TEST_LEFT(screen,"AA ellipses",1000,100,true,sge_AAEllipse(screen,xl1[i],y1[i],r1[i],r2[i],col1[i]));
RUN_TEST_RIGHT(screen,"AA ellipses (alpha)",1000,100,true,sge_AAEllipseAlpha(screen,xr1[i],y1[i],r1[i],r2[i],col1[i],a[i]));
op = 0;
break;
case 13:
RUN_TEST_LEFT(screen,"Filled ellipses",1000,100,false,sge_FilledEllipse(screen,xl1[i],y1[i],r1[i],r2[i],col1[i]));
RUN_TEST_RIGHT(screen,"Filled ellipses (alpha)",500,100,true,sge_FilledEllipseAlpha(screen,xr1[i],y1[i],r1[i],r2[i],col1[i],a[i]));
op = 0;
break;
case 14:
RUN_TEST_LEFT(screen,"AA filled circles",500,100,false,sge_AAFilledCircle(screen,xl1[i],y1[i],r1[i],col1[i]));
RUN_TEST_RIGHT(screen,"AA filled ellipses",500,100,false,sge_AAFilledEllipse(screen,xr1[i],y1[i],r1[i],r2[i],col1[i]));
op = 0;
break;
case 15:
RUN_TEST_LEFT(screen,"Bezier curves",1500,50,true,sge_Bezier(screen, xl1[i],y1[i],xl2[i],y2[i],xl3[i],y3[i],xl4[i],y4[i], 6, col1[i]));
RUN_TEST_RIGHT(screen,"Bezier curves (alpha)",800,50,true,sge_BezierAlpha(screen, xr1[i],y1[i],xr2[i],y2[i],xr3[i],y3[i],xr4[i],y4[i], 6, col1[i], a[i]));
op = 0;
break;
case 16:
RUN_TEST_LEFT(screen,"AA bezier curves",800,50,true,sge_AABezier(screen, xl1[i],y1[i],xl2[i],y2[i],xl3[i],y3[i],xl4[i],y4[i], 6, col1[i]));
RUN_TEST_RIGHT(screen,"AA bezier curves (alpha)",800,50,true,sge_AABezierAlpha(screen, xr1[i],y1[i],xr2[i],y2[i],xr3[i],y3[i],xr4[i],y4[i], 6, col1[i], a[i]));
op = 0;
break;
case 17:
RUN_TEST_LEFT(screen,"Triangles",1500,30,true,sge_Trigon(screen, xl1[i],y1[i],xl2[i],y2[i],xl3[i],y3[i], col1[i]));
RUN_TEST_RIGHT(screen,"Triangles (alpha)",1000,30,true,sge_TrigonAlpha(screen, xr1[i],y1[i],xr2[i],y2[i],xr3[i],y3[i], col1[i], a[i]));
op = 0;
break;
case 18:
RUN_TEST_LEFT(screen,"AA triangles",500,30,true,sge_AATrigon(screen, xl1[i],y1[i],xl2[i],y2[i],xl3[i],y3[i], col1[i]));
RUN_TEST_RIGHT(screen,"AA triangles (alpha)",500,30,true,sge_AATrigonAlpha(screen, xr1[i],y1[i],xr2[i],y2[i],xr3[i],y3[i], col1[i], a[i]));
op = 0;
break;
case 19:
RUN_TEST_LEFT(screen,"Filled triangles",800,30,false,sge_FilledTrigon(screen, xl1[i],y1[i],xl2[i],y2[i],xl3[i],y3[i], col1[i]));
RUN_TEST_RIGHT(screen,"Filled triangles (alpha)",100,30,true,sge_FilledTrigonAlpha(screen, xr1[i],y1[i],xr2[i],y2[i],xr3[i],y3[i], col1[i], a[i]));
op = 0;
break;
case 20:
RUN_TEST_LEFT(screen,"Gourand shaded triangles",400,20,true,sge_FadedTrigon(screen, xl1[i],y1[i],xl2[i],y2[i],xl3[i],y3[i], col1[i],col2[i],col3[i]));
if (SDL_MUSTLOCK(texture))
if (SDL_LockSurface(texture) < 0)
break;
RUN_TEST_RIGHT(screen,"Texture mapped triangles",500,20,true,sge_TexturedTrigon(screen, xr1[i],y1[i],xr2[i],y2[i],xr3[i],y3[i], texture,0,0,0,texture->h-1,texture->w-1,0));
if (SDL_MUSTLOCK(texture))
SDL_UnlockSurface(texture);
op = 0;
break;
case 21:
RUN_TEST_LEFT(screen,"Bitmap text",4000,100,false,sge_BF_textout(screen, font, "Hello World!", xl1[i], y1[i]));
RUN_TEST_RIGHT(screen,"Colored bitmap text",4000,100,false,sge_BF_SetColor(cfont,r[i],g[i],b[i]);sge_BF_textout(screen, cfont, "Hello World!", xr1[i], y1[i]));
op = 0;
break;
}
/* Wait for user input */
SDL_WaitEvent(&event);
if(event.type==SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT){
/* Goto next test */
op = ++next_op;
if(op > 21){
op=1;
next_op=1;
rand_data(screen);
}
}
if(event.type==SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_RIGHT){
/* Goto previous test */
op = --next_op;
if(op < 1){
op=21;
next_op=21;
rand_data(screen);
}
}
if(event.type==SDL_KEYDOWN && event.key.keysym.sym==SDLK_ESCAPE){break;}
if(event.type==SDL_QUIT){break;}
}while(true);
/* Clean up */
sge_BF_CloseFont(font);
sge_BF_CloseFont(cfont);
SDL_FreeSurface(texture);
return 0;
}
|