Changeset 134 for abuse/trunk/src/sdlport/video.cpp
- Timestamp:
- Apr 10, 2008, 11:08:14 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
abuse/trunk/src/sdlport/video.cpp
r129 r134 21 21 22 22 #include <SDL.h> 23 #ifdef HAVE_OPENGL 24 #ifdef __APPLE__ 25 #include <OpenGL/gl.h> 26 #include <OpenGL/glu.h> 27 #else 28 #include <GL/gl.h> 29 #include <GL/glu.h> 30 #endif /* __APPLE__ */ 23 24 #ifdef HAVE_OPENGL 25 # ifdef __APPLE__ 26 # include <OpenGL/gl.h> 27 # include <OpenGL/glu.h> 28 # else 29 # include <GL/gl.h> 30 # include <GL/glu.h> 31 # endif /* __APPLE__ */ 31 32 #endif /* HAVE_OPENGL */ 33 32 34 #include "filter.hpp" 33 35 #include "system.h" … … 61 63 { 62 64 int value; 63 for( 65 for(value = 1 ; value < input ; value <<= 1); 64 66 return value; 65 67 } … … 69 71 // Set the video mode 70 72 // 71 void set_mode( int mode, int argc, char **argv)73 void set_mode(int mode, int argc, char **argv) 72 74 { 73 75 const SDL_VideoInfo *vidInfo; … … 76 78 // Check for video capabilities 77 79 vidInfo = SDL_GetVideoInfo(); 78 if( vidInfo->hw_available ) 79 { 80 if(vidInfo->hw_available) 80 81 vidFlags |= SDL_HWSURFACE; 81 } 82 else 83 { 82 else 84 83 vidFlags |= SDL_SWSURFACE; 85 } 86 if( flags.fullscreen ) 87 { 84 85 if(flags.fullscreen) 88 86 vidFlags |= SDL_FULLSCREEN; 89 } 90 if( flags.doublebuf ) 91 { 87 88 if(flags.doublebuf) 92 89 vidFlags |= SDL_DOUBLEBUF; 93 }94 90 95 91 // Calculate the window scale … … 98 94 99 95 // Try using opengl hw accell 100 if( flags.gl) {101 #ifdef HAVE_OPENGL 102 printf( "Video : OpenGL enabled\n");96 if(flags.gl) { 97 #ifdef HAVE_OPENGL 98 printf("Video : OpenGL enabled\n"); 103 99 // allow doublebuffering in with gl too 104 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, flags.doublebuf);100 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, flags.doublebuf); 105 101 // set video gl capability 106 102 vidFlags |= SDL_OPENGL; … … 109 105 #else 110 106 // ignore the option if not available 111 printf( 107 printf("Video : OpenGL disabled (Support missing in executable)\n"); 112 108 flags.gl = 0; 113 109 #endif … … 115 111 116 112 // Set the icon for this window. Looks nice on taskbars etc. 117 SDL_WM_SetIcon( SDL_LoadBMP("abuse.bmp"), NULL);113 SDL_WM_SetIcon(SDL_LoadBMP("abuse.bmp"), NULL); 118 114 119 115 // Create the window with a preference for 8-bit (palette animations!), but accept any depth */ 120 window = SDL_SetVideoMode(flags.xres, flags.yres, 8, vidFlags | SDL_ANYFORMAT 121 if( window == NULL)122 { 123 printf( "Video : Unable to set video mode : %s\n", SDL_GetError());124 exit( 1);116 window = SDL_SetVideoMode(flags.xres, flags.yres, 8, vidFlags | SDL_ANYFORMAT); 117 if(window == NULL) 118 { 119 printf("Video : Unable to set video mode : %s\n", SDL_GetError()); 120 exit(1); 125 121 } 126 122 127 123 // Create the screen image 128 screen = new image( xres, yres, NULL, 2);129 if( screen == NULL)124 screen = new image(xres, yres, NULL, 2); 125 if(screen == NULL) 130 126 { 131 127 // Our screen image is no good, we have to bail. 132 printf( "Video : Unable to create screen image.\n");133 exit( 1);128 printf("Video : Unable to create screen image.\n"); 129 exit(1); 134 130 } 135 131 screen->clear(); … … 138 134 { 139 135 #ifdef HAVE_OPENGL 140 int w, h;136 int w, h; 141 137 142 138 // texture width/height should be power of 2 139 // FIXME: we can use GL_ARB_texture_non_power_of_two or 140 // GL_ARB_texture_rectangle to avoid the extra memory allocation 143 141 w = power_of_two(xres); 144 142 h = power_of_two(yres); 145 143 146 144 // create texture surface 147 texture = SDL_CreateRGBSurface( SDL_SWSURFACE, w ,h ,32,145 texture = SDL_CreateRGBSurface(SDL_SWSURFACE, w , h , 32, 148 146 #if SDL_BYTEORDER == SDL_LIL_ENDIAN 149 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 147 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); 150 148 #else 151 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF 149 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF); 152 150 #endif 153 151 … … 164 162 glLoadIdentity(); 165 163 166 glOrtho(0.0, (GLdouble)window->w, (GLdouble)window->h, 0.0, 0.0, 1.0);164 glOrtho(0.0, (GLdouble)window->w, (GLdouble)window->h, 0.0, 0.0, 1.0); 167 165 168 166 glMatrixMode(GL_MODELVIEW); … … 181 179 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, flags.antialias); 182 180 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, flags.antialias); 183 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels 181 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels); 184 182 #endif 185 183 } 186 184 187 185 // Create our 8-bit surface 188 surface = SDL_CreateRGBSurface( SDL_SWSURFACE, window->w, window->h, 8, 0xff, 0xff, 0xff, 0xff);189 if( surface == NULL)186 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, window->w, window->h, 8, 0xff, 0xff, 0xff, 0xff); 187 if(surface == NULL) 190 188 { 191 189 // Our surface is no good, we have to bail. 192 printf( "Video : Unable to create 8-bit surface.\n");193 exit( 1);194 } 195 196 printf( "Video : %dx%d %dbpp\n", window->w, window->h, window->format->BitsPerPixel);190 printf("Video : Unable to create 8-bit surface.\n"); 191 exit(1); 192 } 193 194 printf("Video : %dx%d %dbpp\n", window->w, window->h, window->format->BitsPerPixel); 197 195 198 196 // Set the window caption 199 SDL_WM_SetCaption( "Abuse-SDL", "Abuse-SDL");197 SDL_WM_SetCaption("Abuse", "Abuse"); 200 198 201 199 // Grab and hide the mouse cursor 202 SDL_ShowCursor( 0 ); 203 if( flags.grabmouse ) 204 { 205 SDL_WM_GrabInput( SDL_GRAB_ON ); 206 } 207 208 update_dirty( screen ); 200 SDL_ShowCursor(0); 201 if(flags.grabmouse) 202 SDL_WM_GrabInput(SDL_GRAB_ON); 203 204 update_dirty(screen); 209 205 } 210 206 … … 215 211 void close_graphics() 216 212 { 217 if( lastl ) 218 { 213 if(lastl) 219 214 delete lastl; 220 }221 215 lastl = NULL; 222 216 // Free our 8-bit surface 223 if( surface ) 224 { 225 SDL_FreeSurface( surface ); 226 } 227 #ifdef HAVE_OPENGL 228 if ( texture ) 229 { 230 SDL_FreeSurface( texture ); 231 } 217 if(surface) 218 SDL_FreeSurface(surface); 219 220 #ifdef HAVE_OPENGL 221 if (texture) 222 SDL_FreeSurface(texture); 232 223 #endif 233 224 delete screen; … … 237 228 // Draw only dirty parts of the image 238 229 // 239 void put_part_image( image *im, int x, int y, int x1, int y1, int x2, int y2)230 void put_part_image(image *im, int x, int y, int x1, int y1, int x2, int y2) 240 231 { 241 232 int xe, ye; … … 246 237 Uint16 dinset; 247 238 248 if( (unsigned)y > yres || (unsigned)x > xres ) 249 { 239 if((unsigned)y > yres || (unsigned)x > xres) 250 240 return; 251 } 252 CHECK( x1 >= 0 && x2 >= x1 && y1 >= 0 && y2 >= y1);241 242 CHECK(x1 >= 0 && x2 >= x1 && y1 >= 0 && y2 >= y1); 253 243 254 244 // Adjust if we are trying to draw off the screen 255 if( x < 0)245 if(x < 0) 256 246 { 257 247 x1 += -x; … … 259 249 } 260 250 srcrect.x = x1; 261 if( (unsigned)(x + ( x2 - x1 )) >= xres ) 262 { 251 if((unsigned)(x + (x2 - x1)) >= xres) 263 252 xe = xres - x + x1 - 1; 264 } 265 else 266 { 253 else 267 254 xe = x2; 268 } 269 if( y < 0)255 256 if(y < 0) 270 257 { 271 258 y1 += -y; … … 273 260 } 274 261 srcrect.y = y1; 275 if( (unsigned)(y + ( y2 - y1 )) >= yres ) 276 { 262 if((unsigned)(y + (y2 - y1)) >= yres) 277 263 ye = yres - y + y1 - 1; 278 } 279 else 280 { 264 else 281 265 ye = y2; 282 } 283 if( srcrect.x >= xe || srcrect.y >= ye)266 267 if(srcrect.x >= xe || srcrect.y >= ye) 284 268 return; 285 269 … … 299 283 300 284 // Lock the surface if necessary 301 if( SDL_MUSTLOCK( surface ) ) 302 { 303 SDL_LockSurface( surface ); 304 } 285 if(SDL_MUSTLOCK(surface)) 286 SDL_LockSurface(surface); 287 305 288 dpixel = (Uint8 *)surface->pixels; 306 289 dpixel += (dstrect.x + ((dstrect.y) * surface->w)) * surface->format->BytesPerPixel; … … 311 294 srcy = srcrect.y; 312 295 dpixel = ((Uint8 *)surface->pixels) + y * surface->w + x ; 313 for( ii=0 ; ii < srcrect.h; ii++)296 for(ii=0 ; ii < srcrect.h; ii++) 314 297 { 315 memcpy( dpixel, im->scan_line( srcy ) + srcrect.x , srcrect.w);298 memcpy(dpixel, im->scan_line(srcy) + srcrect.x , srcrect.w); 316 299 dpixel += surface->w; 317 300 srcy ++; … … 328 311 dpixel = (Uint8 *)surface->pixels + (dstrect.x + ((dstrect.y) * surface->w)) * surface->format->BytesPerPixel; 329 312 330 for( ii = 0; ii < dstrect.h; ii++)313 for(ii = 0; ii < dstrect.h; ii++) 331 314 { 332 315 srcx = (srcrect.x << 16); 333 for( jj = 0; jj < dstrect.w; jj++)316 for(jj = 0; jj < dstrect.w; jj++) 334 317 { 335 memcpy( dpixel, im->scan_line( (srcy >> 16) ) + ((srcx >> 16) * surface->format->BytesPerPixel), surface->format->BytesPerPixel);318 memcpy(dpixel, im->scan_line((srcy >> 16)) + ((srcx >> 16) * surface->format->BytesPerPixel), surface->format->BytesPerPixel); 336 319 dpixel += surface->format->BytesPerPixel; 337 320 srcx += xstep; … … 345 328 346 329 // Unlock the surface if we locked it. 347 if( SDL_MUSTLOCK( surface ) ) 348 { 349 SDL_UnlockSurface( surface ); 350 } 330 if(SDL_MUSTLOCK(surface)) 331 SDL_UnlockSurface(surface); 351 332 352 333 // Now blit the surface 353 update_window_part( 334 update_window_part(&dstrect); 354 335 } 355 336 … … 358 339 // Draw the entire image 359 340 // 360 void put_image( image * im, int x, int y)361 { 362 put_part_image( im, x, y, 0, 0, im->width() - 1, im->height() - 1);341 void put_image(image * im, int x, int y) 342 { 343 put_part_image(im, x, y, 0, 0, im->width() - 1, im->height() - 1); 363 344 } 364 345 … … 367 348 // Update the dirty parts of the window 368 349 // 369 void update_dirty_window( image *im, int xoff, int yoff)350 void update_dirty_window(image *im, int xoff, int yoff) 370 351 { 371 352 int count; 372 353 dirty_rect *dr, *q; 373 CHECK( im->special); // make sure the image has the ability to contain dirty areas374 if( im->special->keep_dirt == 0)375 { 376 put_image( im, xoff, yoff);354 CHECK(im->special); // make sure the image has the ability to contain dirty areas 355 if(im->special->keep_dirt == 0) 356 { 357 put_image(im, xoff, yoff); 377 358 } 378 359 else 379 360 { 380 361 count = im->special->dirties.number_nodes(); 381 if( !count)362 if(!count) 382 363 return; // if nothing to update, return 383 dr = (dirty_rect *)( im->special->dirties.first());384 while( count > 0)364 dr = (dirty_rect *)(im->special->dirties.first()); 365 while(count > 0) 385 366 { 386 put_part_image( im, xoff + dr->dx1, yoff + dr->dy1, dr->dx1, dr->dy1, dr->dx2 + 1, dr->dy2 + 1);367 put_part_image(im, xoff + dr->dx1, yoff + dr->dy1, dr->dx1, dr->dy1, dr->dx2 + 1, dr->dy2 + 1); 387 368 q = dr; 388 dr = (dirty_rect *)( dr->next());389 im->special->dirties.unlink( ( linked_node *)q);369 dr = (dirty_rect *)(dr->next()); 370 im->special->dirties.unlink((linked_node *)q); 390 371 delete q; 391 372 count--; … … 398 379 // Update the dirty parts of the image 399 380 // 400 void update_dirty( image *im, int xoff, int yoff)401 { 402 update_dirty_window( im, xoff, yoff);381 void update_dirty(image *im, int xoff, int yoff) 382 { 383 update_dirty_window(im, xoff, yoff); 403 384 update_window_done(); 404 385 } … … 407 388 // make_page() 408 389 // 409 void image::make_page( short width, short height, unsigned char *page_buffer)410 { 411 if( page_buffer)390 void image::make_page(short width, short height, unsigned char *page_buffer) 391 { 392 if(page_buffer) 412 393 data = page_buffer; 413 394 else 414 data = (unsigned char *)malloc( width * height);395 data = (unsigned char *)malloc(width * height); 415 396 } 416 397 … … 420 401 void image::delete_page() 421 402 { 422 if( !special || !special->static_mem)423 free( data);403 if(!special || !special->static_mem) 404 free(data); 424 405 } 425 406 … … 430 411 void palette::load() 431 412 { 432 if( lastl ) 433 { 413 if(lastl) 434 414 delete lastl; 435 }436 415 lastl = copy(); 437 416 438 417 // Force to only 256 colours. 439 418 // Shouldn't be needed, but best to be safe. 440 if( ncolors > 256 ) 441 { 419 if(ncolors > 256) 442 420 ncolors = 256; 443 }444 421 445 422 SDL_Color colors[ncolors]; 446 for( int ii = 0; ii < ncolors; ii++)423 for(int ii = 0; ii < ncolors; ii++) 447 424 { 448 425 colors[ii].r = red(ii); … … 450 427 colors[ii].b = blue(ii); 451 428 } 452 SDL_SetColors( surface, colors, 0, ncolors ); 453 if( window->format->BitsPerPixel == 8 ) 454 { 455 SDL_SetColors( window, colors, 0, ncolors ); 456 } 429 SDL_SetColors(surface, colors, 0, ncolors); 430 if(window->format->BitsPerPixel == 8) 431 SDL_SetColors(window, colors, 0, ncolors); 457 432 458 433 // Now redraw the surface 459 update_window_part( NULL);434 update_window_part(NULL); 460 435 update_window_done(); 461 436 } … … 473 448 void update_window_done() 474 449 { 450 #ifdef HAVE_OPENGL 475 451 // opengl blit complete surface to window 476 if (flags.gl) 477 { 478 #ifdef HAVE_OPENGL 452 if(flags.gl) 453 { 479 454 // convert color-indexed surface to RGB texture 480 SDL_BlitSurface( surface, NULL, texture, NULL);455 SDL_BlitSurface(surface, NULL, texture, NULL); 481 456 482 457 // Texturemap complete texture to surface so we have free scaling 483 458 // and antialiasing 484 glTexSubImage2D( GL_TEXTURE_2D,0,485 0,0,texture->w,texture->h,486 GL_RGBA,GL_UNSIGNED_BYTE,texture->pixels);459 glTexSubImage2D(GL_TEXTURE_2D, 0, 460 0, 0, texture->w, texture->h, 461 GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels); 487 462 glBegin(GL_TRIANGLE_STRIP); 488 463 glTexCoord2f(texcoord[0], texcoord[1]); glVertex3i(0, 0, 0); … … 491 466 glTexCoord2f(texcoord[2], texcoord[3]); glVertex3i(window->w, window->h, 0); 492 467 glEnd(); 493 #endif 494 } 495 468 469 if(flags.doublebuf) 470 SDL_GL_SwapBuffers(); 471 } 472 #else 496 473 // swap buffers in case of double buffering 497 if (flags.doublebuf)498 {499 if (flags.gl)500 {501 #ifdef HAVE_OPENGL502 SDL_GL_SwapBuffers();503 #endif504 }505 else506 {507 SDL_Flip(window);508 }509 }510 511 474 // do nothing in case of single buffering 512 } 513 514 void update_window_part( SDL_Rect *rect) 475 if(flags.doublebuf) 476 SDL_Flip(window); 477 #endif 478 } 479 480 void update_window_part(SDL_Rect *rect) 515 481 { 516 482 // no partial blit's in case of opengl 517 483 // complete blit + scaling just before flip 518 484 if (flags.gl) 519 {520 485 return; 521 } 522 523 SDL_BlitSurface( surface, rect, window, rect ); 486 487 SDL_BlitSurface(surface, rect, window, rect); 524 488 525 489 // no window update needed until end of run 526 if( flags.doublebuf) 527 { 490 if(flags.doublebuf) 528 491 return; 529 }530 492 531 493 // update window part for single buffer 532 if( rect == NULL ) 533 { 534 SDL_UpdateRect( window, 0, 0, 0, 0 ); 535 } 536 else 537 { 538 SDL_UpdateRect( window, rect->x, rect->y, rect->w, rect->h ); 539 } 540 } 494 if(rect == NULL) 495 SDL_UpdateRect(window, 0, 0, 0, 0); 496 else 497 SDL_UpdateRect(window, rect->x, rect->y, rect->w, rect->h); 498 }
Note: See TracChangeset
for help on using the changeset viewer.