📄 sdl.c
字号:
}/*====================================================================Current video mode.====================================================================*/Video_Mode* cur_video_mode(){ return &cur_mode;}/*====================================================================Get a list with all valid standard mode names.====================================================================*/char** get_mode_names( int *count ){ char **lines; int i, j; *count = 0; for ( i = 0; i < mode_count; i++ ) if ( modes[i].ok ) (*count)++; lines = calloc( *count, sizeof( char* ) ); for ( i = 0, j = 0; i < mode_count; i++ ) if ( modes[i].ok ) lines[j++] = strdup( modes[i].name ); return lines;}/*====================================================================Switch to passed video mode.====================================================================*/int set_video_mode( Video_Mode mode ){#ifdef SDL_DEBUG SDL_PixelFormat *fmt;#endif /* free old screen */ if (sdl.screen) SDL_FreeSurface( sdl.screen ); /* check again */ mode = video_mode( mode.width, mode.height, mode.depth, mode.flags ); /* set as current mode */ cur_mode = mode; /* set video mode */ if ( ( sdl.screen = SDL_SetVideoMode( mode.width, mode.height, mode.depth, mode.flags ) ) == 0 ) { fprintf(stderr, "set_video_mode: cannot allocate screen: %s", SDL_GetError()); return 1; }#ifdef SDL_DEBUG if (f & SDL_HWSURFACE && !(sdl.screen->flags & SDL_HWSURFACE)) fprintf(stderr, "unable to create screen in hardware memory...\n"); if (f & SDL_DOUBLEBUF && !(sdl.screen->flags & SDL_DOUBLEBUF)) fprintf(stderr, "unable to create double buffered screen...\n"); if (f & SDL_FULLSCREEN && !(sdl.screen->flags & SDL_FULLSCREEN)) fprintf(stderr, "unable to switch to fullscreen...\n"); fmt = sdl.screen->format; printf("video mode format:\n"); printf("Masks: R=%i, G=%i, B=%i\n", fmt->Rmask, fmt->Gmask, fmt->Bmask); printf("LShft: R=%i, G=%i, B=%i\n", fmt->Rshift, fmt->Gshift, fmt->Bshift); printf("RShft: R=%i, G=%i, B=%i\n", fmt->Rloss, fmt->Gloss, fmt->Bloss); printf("BBP: %i\n", fmt->BitsPerPixel); printf("-----\n");#endif return 0;}/* show hardware capabilities*/void hardware_cap(){ const SDL_VideoInfo *vi = SDL_GetVideoInfo(); char *ny[2] = {"No", "Yes"}; printf("video hardware capabilities:\n"); printf("Hardware Surfaces: %s\n", ny[vi->hw_available]); printf("HW_Blit (CC, A): %s (%s, %s)\n", ny[vi->blit_hw], ny[vi->blit_hw_CC], ny[vi->blit_hw_A]); printf("SW_Blit (CC, A): %s (%s, %s)\n", ny[vi->blit_sw], ny[vi->blit_sw_CC], ny[vi->blit_sw_A]); printf("HW_Fill: %s\n", ny[vi->blit_fill]); printf("Video Memory: %i\n", vi->video_mem); printf("------\n");}/* update rectangle (0,0,0,0)->fullscreen*/inline void refresh_screen(int x, int y, int w, int h){ SDL_UpdateRect(sdl.screen, x, y, w, h);}/* draw all update regions*/void refresh_rects(){ if (sdl.rect_count == RECT_LIMIT) SDL_UpdateRect(sdl.screen, 0, 0, sdl.screen->w, sdl.screen->h); else SDL_UpdateRects(sdl.screen, sdl.rect_count, sdl.rect); sdl.rect_count = 0;}/* add update region*/void add_refresh_rect(int x, int y, int w, int h){ if (sdl.rect_count == RECT_LIMIT) return; if (x < 0) { w += x; x = 0; } if (y < 0) { h += y; y = 0; } if (x + w > sdl.screen->w) w = sdl.screen->w - x; if (y + h > sdl.screen->h) h = sdl.screen->h - y; if (w <= 0 || h <= 0) return; sdl.rect[sdl.rect_count].x = x; sdl.rect[sdl.rect_count].y = y; sdl.rect[sdl.rect_count].w = w; sdl.rect[sdl.rect_count].h = h; sdl.rect_count++;}/* fade screen to black*/void dim_screen(int steps, int delay, int trp){#ifndef NODIM SDL_Surface *buffer; int per_step = trp / steps; int i; if (term_game) return; buffer = create_surf(sdl.screen->w, sdl.screen->h, SDL_SWSURFACE); SDL_SetColorKey(buffer, 0, 0); FULL_DEST(buffer); FULL_SOURCE(sdl.screen); blit_surf(); for (i = 0; i <= trp; i += per_step) { FULL_DEST(sdl.screen); fill_surf(0x0); FULL_SOURCE(buffer); alpha_blit_surf(i); refresh_screen( 0, 0, 0, 0); SDL_Delay(delay); } if (trp == 255) { FULL_DEST(sdl.screen); fill_surf(0x0); refresh_screen( 0, 0, 0, 0); } SDL_FreeSurface(buffer);#else refresh_screen( 0, 0, 0, 0);#endif}/* undim screen*/void undim_screen(int steps, int delay, int trp){#ifndef NODIM SDL_Surface *buffer; int per_step = trp / steps; int i; if (term_game) return; buffer = create_surf(sdl.screen->w, sdl.screen->h, SDL_SWSURFACE); SDL_SetColorKey(buffer, 0, 0); FULL_DEST(buffer); FULL_SOURCE(sdl.screen); blit_surf(); for (i = trp; i >= 0; i -= per_step) { FULL_DEST(sdl.screen); fill_surf(0x0); FULL_SOURCE(buffer); alpha_blit_surf(i); refresh_screen( 0, 0, 0, 0); SDL_Delay(delay); } FULL_DEST(sdl.screen); FULL_SOURCE(buffer); blit_surf(); refresh_screen( 0, 0, 0, 0); SDL_FreeSurface(buffer);#else refresh_screen( 0, 0, 0, 0);#endif}/* wait for a key*/int wait_for_key(){ /* wait for key */ SDL_Event event; while (1) { SDL_WaitEvent(&event); if (event.type == SDL_QUIT) { term_game = 1; return 0; } if (event.type == SDL_KEYDOWN) return event.key.keysym.sym; }}/* wait for a key or mouse click*/void wait_for_click(){ /* wait for key or button */ SDL_Event event; while (1) { SDL_WaitEvent(&event); if (event.type == SDL_QUIT) { term_game = 1; return; } if (event.type == SDL_KEYDOWN || event.type == SDL_MOUSEBUTTONUP) return; }}/* lock surface*/inline void lock_screen(){ if (SDL_MUSTLOCK(sdl.screen)) SDL_LockSurface(sdl.screen);}/* unlock surface*/inline void unlock_screen(){ if (SDL_MUSTLOCK(sdl.screen)) SDL_UnlockSurface(sdl.screen);}/* flip hardware screens (double buffer)*/inline void flip_screen(){ SDL_Flip(sdl.screen);}/* cursor *//* creates cursor */SDL_Cursor* create_cursor( int width, int height, int hot_x, int hot_y, char *source ){ char *mask = 0, *data = 0; SDL_Cursor *cursor = 0; int i, j, k; char data_byte, mask_byte; int pot; /* meaning of char from source: b : black, w: white, ' ':transparent */ /* create mask&data */ mask = malloc( width * height * sizeof ( char ) / 8 ); data = malloc( width * height * sizeof ( char ) / 8 ); k = 0; for (j = 0; j < width * height; j += 8, k++) { pot = 1; data_byte = mask_byte = 0; /* create byte */ for (i = 7; i >= 0; i--) { switch ( source[j + i] ) { case 'b': data_byte += pot; case 'w': mask_byte += pot; break; } pot *= 2; } /* add to mask */ data[k] = data_byte; mask[k] = mask_byte; } /* create and return cursor */ cursor = SDL_CreateCursor( data, mask, width, height, hot_x, hot_y ); free( mask ); free( data ); return cursor;}/* get milliseconds since last call*/inline int get_time(){ int ms; cur_time = SDL_GetTicks(); ms = cur_time - last_time; last_time = cur_time; if (ms == 0) { ms = 1; SDL_Delay(1); } return ms;}/* reset timer*/inline void reset_timer(){ last_time = SDL_GetTicks();}void fade_screen( int type, int length ){ SDL_Surface *buffer = 0; float alpha; float alpha_change; /* per ms */ int leave = 0; int ms; if ( !sdl.fade ) { if ( type == FADE_IN ) refresh_screen( 0, 0, 0, 0 ); else { FULL_DEST( sdl.screen ); fill_surf( 0x0 ); refresh_screen( 0, 0, 0, 0 ); } } /* get screen contents */ buffer = create_surf( sdl.screen->w, sdl.screen->h, SDL_SWSURFACE ); SDL_SetColorKey( buffer, 0, 0 ); FULL_DEST( buffer ); FULL_SOURCE( sdl.screen ); blit_surf(); /* compute alpha and alpha change */ if ( type == FADE_OUT ) { alpha = 0; alpha_change = 255.0 / length; } else { alpha = 255; alpha_change = -255.0 / length; } /* fade */ reset_timer(); while ( !leave ) { ms = get_time(); alpha += alpha_change * ms; if ( type == FADE_OUT && alpha >= 255 ) break; if ( type == FADE_IN && alpha <= 0 ) break; /* update */ FULL_DEST( sdl.screen ); fill_surf(0x0); FULL_SOURCE( buffer ); alpha_blit_surf( (int)alpha ); refresh_screen( 0, 0, 0, 0); } /* update screen */ FULL_DEST( sdl.screen ); FULL_SOURCE( buffer ); if ( type == FADE_IN ) blit_surf(); else fill_surf( 0x0 ); refresh_screen( 0, 0, 0, 0 ); SDL_FreeSurface(buffer);}void take_screenshot( int i ){ char str[32]; sprintf( str, "screenshot%i.bmp", i ); SDL_SaveBMP( sdl.screen, str );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -