⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdl.c

📁 LTris a tetris clone for Linux
💻 C
📖 第 1 页 / 共 3 页
字号:
    return pixel;}/* get pixel */Uint32 get_pixel( SDL_Surface *surf, int x, int y ){    int pos = 0;    Uint32 pixel = 0;    pos = y * surf->pitch + x * surf->format->BytesPerPixel;    memcpy( &pixel, surf->pixels + pos, surf->format->BytesPerPixel );    return pixel;}/* draw a shadowed frame and darken contents which starts at cx,cy */void draw_3dframe( SDL_Surface *surf, int cx, int cy, int w, int h, int border ){    int i, j;    SDL_Surface *frame = 0;    SDL_Surface *contents = 0;    frame = create_surf( w + border * 2, h + border * 2, SDL_SWSURFACE );    SDL_SetColorKey( frame, SDL_SRCCOLORKEY, SDL_MapRGB( surf->format, 0xff, 0, 0 ) );    FULL_DEST( frame ); fill_surf ( 0xff0000 );    /* move contents by border size -1 */    DEST( surf, cx, cy, w, h );    SOURCE( surf, cx - border + 1, cy - border + 1 );    blit_surf();    /* shadow part */    FULL_DEST( frame ); fill_surf ( 0xff0000 );    DEST( frame, 0, 0, w + border, border ); fill_surf( 0x0 );    DEST( frame, 0, 0, border, h + border ); fill_surf( 0x0 );    for ( i = 0; i < border; i++ ) {        for ( j = 0; j < border; j++ ) {            if ( i < j )                set_pixel( frame, border + w + i, border - j - 1, SDL_MapRGB( frame->format, 0,0,0 ) );        }    }    for ( i = 0; i < border; i++ ) {        for ( j = 0; j < border; j++ ) {            if ( i > j )                set_pixel( frame, border - i - 1, border + h + j, SDL_MapRGB( frame->format, 0,0,0 ) );        }    }    DEST( surf, cx - border, cy - border, w + border * 2, h + border * 2 );    SOURCE( frame, 0, 0 );    alpha_blit_surf( 48 );    /* bright part */    FULL_DEST( frame ); fill_surf ( 0xff0000 );    DEST( frame, w + border, border, border, h + border ); fill_surf( 0xffffff );    DEST( frame, border, h + border, w + border, border ); fill_surf( 0xffffff );    for ( i = 0; i < border; i++ ) {        for ( j = 0; j < border; j++ ) {            if ( i >= j )                set_pixel( frame, border + w + i, border - j - 1, SDL_MapRGB( frame->format, 0xff,0xff,0xff ) );        }    }    for ( i = 0; i < border; i++ ) {        for ( j = 0; j < border; j++ ) {            if ( i <= j )                set_pixel( frame, border - i - 1, border + h + j, SDL_MapRGB( frame->format, 0xff,0xff,0xff ) );        }    }    DEST( surf, cx - border, cy - border, w + border * 2, h + border * 2 );    SOURCE( frame, 0, 0 );    alpha_blit_surf( 128 );    /* darken contents */    contents = create_surf( w, h, SDL_SWSURFACE );    SDL_SetColorKey( contents, 0, 0 );    FULL_DEST( contents ); fill_surf( 0x0 );    DEST( surf, cx, cy, w, h );    SOURCE( contents, 0, 0 );    alpha_blit_surf( 96 );    SDL_FreeSurface( contents );    SDL_FreeSurface( frame ); }/* sdl font *//* return full font path */void get_full_font_path( char *path, char *file_name ){    strcpy( path, file_name );/*    sprintf(path, "./gfx/fonts/%s", file_name ); */}/*    load a font using the width values in the file*/Font* load_font(char *fname){    Font    *fnt = 0;    FILE    *file = 0;    char    path[512];    int     i;    get_full_font_path( path, fname );    fnt = malloc(sizeof(Font));    if (fnt == 0) {        fprintf(stderr, "load_font: not enough memory\n");        exit(1);    }    if ((fnt->pic = load_surf(path, SDL_HWSURFACE)) == 0)        exit(1);    /* use very first pixel as transparency key */    SDL_SetColorKey( fnt->pic, SDL_SRCCOLORKEY, get_pixel( fnt->pic, 0, 0 ) );				    fnt->align = ALIGN_X_LEFT | ALIGN_Y_TOP;    fnt->color = 0x00FFFFFF;    fnt->height = fnt->pic->h;	    /* table */    file = fopen(path, "r");    fseek(file, -1, SEEK_END);    fread(&fnt->offset, 1, 1, file);#ifdef SDL_DEBUG    printf("offset: %i\n", fnt->offset);#endif    fseek(file, -2, SEEK_END);    fread(&fnt->length, 1, 1, file);#ifdef SDL_DEBUG    printf("number: %i\n", fnt->length);#endif    fseek(file, -2 - fnt->length, SEEK_END);    fread(fnt->char_width, 1, fnt->length, file);#ifdef SDL_DEBUG    printf("letter width: %i\n", fnt->length);    for (i = 0; i < fnt->length; i++)        printf("%i ", fnt->char_width[i]);    printf("\n");#endif    fclose(file);    /* letter offsets */    fnt->char_offset[0] = 0;    for (i = 1; i < fnt->length; i++)	        fnt->char_offset[i] = fnt->char_offset[i - 1] + fnt->char_width[i - 1];    /* allowed keys */    memset(fnt->keys, 0, 256);    for (i = 0; i < fnt->length; i++) {        fnt->keys[i + fnt->offset] = 1;    }    fnt->last_x = fnt->last_y = fnt->last_width = fnt->last_height = 0;    return fnt;}/*    load a font with fixed size*/Font *load_fixed_font(char *f, int off, int len, int w){    int     i;    Font    *fnt;    char    path[512];    get_full_font_path( path, f );    fnt = malloc(sizeof(Font));    if (fnt == 0) {        fprintf(stderr, "load_fixed_font: not enough memory\n");        exit(1);    }    if ((fnt->pic = load_surf(path, SDL_HWSURFACE)) == 0)        exit(1);    /* use very first pixel as transparency key */    SDL_SetColorKey( fnt->pic, SDL_SRCCOLORKEY, get_pixel( fnt->pic, 0, 0 ) );		    fnt->align = ALIGN_X_LEFT | ALIGN_Y_TOP;    fnt->color = 0x00FFFFFF;    fnt->height = fnt->pic->h;		fnt->offset = off;	fnt->length = len;		for (i = 0; i < len; i++)	    fnt->char_width[i] = w;	    /* letter offsets */    fnt->char_offset[0] = 0;    for (i = 1; i < fnt->length; i++)	        fnt->char_offset[i] = fnt->char_offset[i - 1] + w;	    /* allowed keys*/    memset(fnt->keys, 0, 256);    for (i = 0; i < fnt->length; i++) {        fnt->keys[i + fnt->offset] = 1;    }	    fnt->last_x = fnt->last_y = fnt->last_width = fnt->last_height = 0;    return fnt;}/*    free memory*/void free_font(Font **fnt){    if ( (*fnt)->pic) SDL_FreeSurface( (*fnt)->pic);    free( *fnt );    *fnt = 0;}/*    write something with transparency*/int write_text(Font *fnt, SDL_Surface *dest, int x, int y, char *str, int alpha){    int	c_abs;    int len = strlen(str);    int pix_len = 0;    int px = x, py = y;    int i;    SDL_Surface *spf = SDL_GetVideoSurface();	    pix_len = text_width(fnt, str);	for (i = 0; i < len; i++)	    if (!fnt->keys[(int)str[i]])	        str[i] = ' ';    /* alignment */    if (fnt->align & ALIGN_X_CENTER)        px -= pix_len >> 1;    else        if (fnt->align & ALIGN_X_RIGHT)            px -= pix_len;    if (fnt->align & ALIGN_Y_CENTER)        py -= (fnt->height >> 1 ) + 1;    else        if (fnt->align & ALIGN_Y_BOTTOM)            py -= fnt->height;    /* do only set last rect if font->save_last is true */    if ( fnt->save_last ) {        fnt->last_x = px; if (fnt->last_x < 0) fnt->last_x = 0;        fnt->last_y = py; if (fnt->last_y < 0) fnt->last_y = 0;        fnt->last_width = pix_len; if (fnt->last_x + fnt->last_width >= spf->w) fnt->last_width = spf->w - fnt->last_x;        fnt->last_height = fnt->height; if (fnt->last_y + fnt->last_height >= spf->h) fnt->last_height = spf->h - fnt->last_y;    }    if (alpha != 0)        SDL_SetAlpha(fnt->pic, SDL_SRCALPHA, alpha);    else        SDL_SetAlpha(fnt->pic, 0, 0);    for (i = 0; i < len; i++) {       	c_abs = str[i] - fnt->offset;       	DEST(dest, px, py, fnt->char_width[c_abs], fnt->height);       	SOURCE(fnt->pic, fnt->char_offset[c_abs], 0);       	blit_surf();        px += fnt->char_width[c_abs];    }	    return 0;}/*    lock font surface*/inline void lock_font(Font *fnt){    if (SDL_MUSTLOCK(fnt->pic))        SDL_LockSurface(fnt->pic);}/*    unlock font surface*/inline void unlock_font(Font *fnt){    if (SDL_MUSTLOCK(fnt->pic))        SDL_UnlockSurface(fnt->pic);}	/*    return last update region*/SDL_Rect last_write_rect(Font *fnt){    SDL_Rect    rect={fnt->last_x, fnt->last_y, fnt->last_width, fnt->last_height};    return rect;}/*    return the text width in pixels*/int text_width(Font *fnt, char *str){    unsigned int i;    int pix_len = 0;    for (i = 0; i < strlen(str); i++)        pix_len += fnt->char_width[str[i] - fnt->offset];    return pix_len;}/* sdl *//*    initialize sdl*/void init_sdl( int f ){    int i;    int valid_depth = 0;    /* check flags: if SOUND is not enabled flag SDL_INIT_AUDIO musn't be set */#ifndef WITH_SOUND    if ( f & SDL_INIT_AUDIO )        f = f & ~SDL_INIT_AUDIO;#endif    sdl.screen = 0;    if (SDL_Init(f) < 0) {        fprintf(stderr, "ERR: sdl_init: %s", SDL_GetError());        exit(1);    }    SDL_EnableUNICODE(1);    atexit(SDL_Quit);    /* check resolutions */    for ( i = 0; i < mode_count; i++ ) {        if ( ( valid_depth = SDL_VideoModeOK( modes[i].width, modes[i].height, modes[i].depth, modes[i].flags ) ) != 0 ) {            modes[i].depth = valid_depth;            modes[i].ok = 1;            printf( "Mode %s valid\n", modes[i].name );        }    }    /* reset default video mode if none found exit */    if ( !def_mode->ok ) {        for ( i = 0; i < mode_count; i++ )            if ( modes[i].ok ) {                def_mode = &modes[i];                break;            }        /* no valid default mode found? exit */        if ( i == mode_count ) {            fprintf( stderr, "No valid video mode found!\n" );            exit( 1 );        }    }    /* create empty cursor */    empty_cursor = create_cursor( 16, 16, 8, 8,                                  "                "                                  "                "                                  "                "                                  "                "                                  "                "                                  "                "                                  "                "                                  "                "                                  "                "                                  "                "                                  "                "                                  "                "                                  "                "                                  "                "                                  "                "                                  "                " );    std_cursor = SDL_GetCursor();}/*    free screen*/void quit_sdl(){    if (sdl.screen) SDL_FreeSurface(sdl.screen);    if ( empty_cursor ) SDL_FreeCursor( empty_cursor );}/*====================================================================Get a verified video mode.====================================================================*/Video_Mode def_video_mode(){    return *def_mode;}Video_Mode std_video_mode( int id ){    return modes[id];}Video_Mode video_mode( int width, int height, int depth, int flags ){    Video_Mode mode;    /* set name */    sprintf( mode.name, "%ix%ix%i", width, height, depth );    if ( flags & SDL_FULLSCREEN )        strcat( mode.name, " Fullscreen" );    else        strcat( mode.name, " Window" );    /* check mode */    if ( SDL_VideoModeOK( width, height, depth, flags ) != depth ) {        fprintf( stderr, "video_mode: %s invalid: using default mode\n", mode.name );        return def_video_mode();    }    /* set and return this mode */    mode.id = -1;    mode.width = width;    mode.height = height;    mode.depth = depth;    mode.flags = flags;    return mode;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -