Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <SDL/SDL.h>
- #include "sdl_gen.h"
- #include "sdl_text.h"
- #include "die_piss_logs.h"
- #include "tga_v4.h"
- typedef struct point_t point_t;
- struct point_t {
- s32 x;
- s32 y;
- };
- typedef struct waypoint_t waypoint_t;
- struct waypoint_t {
- u32 frame;
- point_t pos;
- u32 scale;
- };
- // fourcc: 'wayp'
- #define FILE_MAGIC (0x77617970)
- #define VID_WIDTH (848U)
- #define VID_HEIGHT (480U)
- #define FP_ONE (0x1000)
- #define FP_MUL( n1, n2 ) ( S32( ( S64(n1) * S64(n2) ) / S64(FP_ONE) ) )
- #define FP_DIV( n1, n2 ) ( S32( ( S64(n1) * S64(FP_ONE) ) / S64(n2) ) )
- #define SCREEN_WIDTH (VID_WIDTH)
- #define SCREEN_HEIGHT ((VID_HEIGHT)+32)
- #define DEFAULT_SCALE (FP_ONE)
- #define SCALE_STEP (128U)
- SDL_Surface* screen = NULL;
- SDL_Surface* screen_raw = NULL;
- // cache of the current frame
- SDL_Surface* current_frame = NULL;
- // the frame number of the cached frame
- u32 current_frame_num = 0xFFFFFFFF;
- SDL_Surface* mask = NULL;
- u32 mask_width = 1024U;
- u32 mask_target_width = 50U;
- waypoint_t* waypoints = NULL;
- SDL_Event event;
- _Bool done = False;
- _Bool updated = True;
- _Bool is_playing = False;
- int is_playing_dir = 1;
- int play_speed = 1;
- _Bool force_full_face = False;
- _Bool new_w = False;
- _Bool is_setting_scale = False;
- _Bool do_draw_lines = False;
- _Bool do_lighten = False;
- // our current frame
- u32 frame_num = 0;
- // the total frame count
- u32 frame_count = 0;
- u32 scale_set_start_frame = 0;
- u32 scale_set_end_frame = 0;
- u32 current_scale = DEFAULT_SCALE;
- u32 current_scale_divisor = 20U;
- int mask_init( void ) {
- SDL_Surface *temp, *temp2;
- temp = SDL_LoadBMP( "mask.bmp" );
- if( !temp ) {
- printf("Could load mask.bmp: %s\n", SDL_GetError());
- return -1;
- }
- temp2 = SDL_ConvertSurface( temp, screen->format, SDL_SWSURFACE );
- SDL_FreeSurface( temp ); temp = NULL;
- if( !temp2 ) {
- printf("Could load mask.bmp: %s\n", SDL_GetError());
- return -2;
- }
- if( mask ) {
- SDL_FreeSurface( mask ); mask = NULL;
- }
- mask = temp2;
- mask_width = mask->w;
- return 0;
- }
- #define WAYPOINT_DEFAULT ((waypoint_t){ .frame=0, .pos=(point_t){.x=0,.y=0}, .scale=DEFAULT_SCALE })
- int waypoint_init( u32 frame_count ) {
- if( waypoints ) return -1;
- waypoints = (waypoint_t*)malloc( sizeof(waypoint_t) * frame_count );
- if( !waypoints ) return -2;
- memset( waypoints, 0, sizeof(waypoint_t) * frame_count );
- // you always need at least one
- waypoints[0].scale = DEFAULT_SCALE;
- return 0;
- }
- int waypoints_free( u32 frame_count ) {
- if( !waypoints ) return -1;
- memset( waypoints, 0, sizeof(waypoint_t) * frame_count );
- free( waypoints ); waypoints = NULL;
- return 0;
- }
- int waypoint_add( u32 n, point_t pos, u32 scale ) {
- if( !waypoints ) return -1;
- waypoints[ n ].scale = scale; // this marks it as a good waypoint
- waypoints[ n ].pos = pos;
- waypoints[ n ].frame = n;
- return 0;
- }
- int waypoint_delete( u32 n ) {
- if( !waypoints ) return -1;
- if( n == 0 ) {
- puts("you can't delete the first waypoint");
- return -2;
- }
- memset( &waypoints[ n ], 0, sizeof(waypoint_t) );
- return 0;
- }
- waypoint_t waypoint_get_first( void ) {
- u32 n;
- if( !waypoints ) return WAYPOINT_DEFAULT;
- for( n = 0; n < frame_count; ++n ) {
- if( waypoints[ n ].scale ) {
- waypoints[ n ].frame = n;
- return waypoints[ n ];
- }
- }
- return WAYPOINT_DEFAULT;
- }
- waypoint_t waypoint_get_last( void ) {
- u32 i;
- if( !waypoints ) return WAYPOINT_DEFAULT;
- i = frame_count - 1;
- while( True ) {
- if( waypoints[ i ].scale ) {
- waypoints[ i ].frame = i;
- return waypoints[ i ];
- }
- if( i == 0 ) break;
- --i;
- }
- return WAYPOINT_DEFAULT;
- }
- waypoint_t waypoint_get_prev( u32 n ) {
- if( !waypoints || n >= frame_count ) return WAYPOINT_DEFAULT;
- while( True ) {
- if( waypoints[ n ].scale ) {
- waypoints[ n ].frame = n;
- return waypoints[ n ];
- }
- if( n == 0 ) break;
- --n;
- }
- // if we didn't find one
- return waypoint_get_first();
- }
- waypoint_t waypoint_get_next( u32 n ) {
- if( !waypoints ) return WAYPOINT_DEFAULT;
- while( n < frame_count ) {
- if( waypoints[ n ].scale ) {
- waypoints[ n ].frame = n;
- return waypoints[ n ];
- }
- ++n;
- }
- // if we didn't find one
- return waypoint_get_last();
- }
- int scale_set_range( u32 n1, u32 n2, u32 scale ) {
- if( !waypoints ) return -1;
- while( n1 < n2 ) {
- if( waypoints[ n1 ].scale ) {
- waypoints[ n1 ].scale = scale;
- }
- ++n1;
- }
- return 0;
- }
- void print_waypoint( waypoint_t w ) {
- printf("frame %u, x %i, y %i, scale %08X\n",
- w.frame,
- w.pos.x,
- w.pos.y,
- w.scale
- );
- }
- point_t mouse_transform( point_t pos ) {
- s64 x, y;
- if( !screen || !screen_raw ) return pos;
- x = pos.x; x *= screen->w; x /= screen_raw->w; pos.x = x;
- y = pos.y; y *= screen->h; y /= screen_raw->h; pos.y = y;
- return pos;
- }
- _Bool file_exists( char* path ) {
- FILE* fp;
- if( !path ) return 0;
- fp = fopen( path, "rb" );
- if( fp ) {
- fclose( fp );
- return 1;
- }
- return 0;
- }
- u32 get_frame_count( void ) {
- char namebuf[ 32 ];
- u32 count;
- count = 0;
- while( True ) {
- memset( namebuf, 0, 32 );
- snprintf( namebuf, 31, "../frames/frame_%06u.bmp", count );
- if( file_exists( namebuf ) ) {
- ++count;
- } else {
- break;
- }
- }
- printf("found %u frames\n", count);
- return count;
- }
- u32 get_num_clipped( u32 n, s32 diff, u32 min, u32 max ) {
- s64 N;
- N = n;
- N += diff;
- if( N < min ) N = min;
- if( N > max ) N = max;
- return N;
- }
- // load a new bitmap from the framestore
- int load_frame( u32 n ) {
- SDL_Surface* temp;
- SDL_Surface* temp_converted;
- char namebuf[ 256 ];
- if( !screen ) return -1;
- if( current_frame_num == n ) return 0;
- memset( namebuf, 0, 256 );
- snprintf( namebuf, 255, "../frames/frame_%06u.bmp", n );
- temp = SDL_LoadBMP( namebuf );
- if( !temp ) {
- printf("Could not load %s\n", namebuf);
- return -3;
- }
- temp_converted = SDL_ConvertSurface( temp, screen->format, SDL_SWSURFACE );
- SDL_FreeSurface( temp ); temp = NULL;
- if( !temp_converted ) {
- return -4;
- }
- if( current_frame ) {
- SDL_FreeSurface( current_frame ); current_frame = NULL;
- }
- current_frame = temp_converted;
- current_frame_num = n;
- printf("Loaded %s\n", namebuf);
- fflush( stdout );
- return 0;
- }
- u32 interp_fixed( u32 pos1, u32 pos_cur, u32 pos2, u32 pos1val, u32 pos2val ) {
- s64 P, O, H, L; // P=percentage, O=one(or offset), H=half, L=length
- if( pos1 == pos2 ) return pos1val;
- if( pos1 > pos2 ) {
- u32 T;
- T = pos1; pos1 = pos2; pos2 = T;
- T = pos1val; pos1val = pos2val; pos2val = T;
- }
- if( pos_cur <= pos1 ) return pos1val;
- if( pos_cur >= pos2 ) return pos2val;
- O = pos2 - pos1; H = O>>1;
- P = pos_cur - pos1;
- L = pos2val; L -= pos1val;
- return pos1val + ( ( ( L * P ) + H ) / O );
- }
- float interp( u32 pos1, u32 pos_cur, u32 pos2, float pos1val, float pos2val ) {
- float percentage;
- if( pos1 == pos2 ) return pos1val;
- if( pos1 > pos2 ) {
- float TF;
- u32 T;
- T = pos1; pos1 = pos2; pos2 = T;
- TF = pos1val; pos1val = pos2val; pos2val = TF;
- }
- if( pos_cur <= pos1 ) return pos1val;
- if( pos_cur >= pos2 ) return pos2val;
- percentage = ((float)( pos_cur - pos1 )) / ((float)( pos2 - pos1 ));
- return pos1val + ( pos2val - pos1val ) * percentage;
- }
- waypoint_t get_pos_scale( u32 n ) {
- waypoint_t w1, w2, out;
- w1 = waypoint_get_prev( n );
- w2 = waypoint_get_next( n );
- out.pos.x = interp( w1.frame, n, w2.frame, w1.pos.x, w2.pos.x );
- out.pos.y = interp( w1.frame, n, w2.frame, w1.pos.y, w2.pos.y );
- out.scale = interp_fixed( w1.frame, n, w2.frame, w1.scale, w2.scale );
- return out;
- }
- u32 fp_mul_scale( u64 n1, u64 n2 ) {
- n1 *= n2;
- n1 += ( FP_ONE / 2U ); // round
- n1 /= FP_ONE;
- return n1;
- }
- int fmul( float n, float m ) {
- return ( (n*m)+0.5f );
- }
- int draw_mask( SDL_Surface* surf, int x, int y, u32 scale, u32 mode ) {
- u32 S;
- if( mode == 0 ) return 0;
- if( mode == 1 || mask == NULL ) {
- // render a circle
- S = fp_mul_scale( mask_width / 2U, scale );
- S /= current_scale_divisor;
- SDL_Circle( surf, x, y, S, c_red );
- } else if( mode == 2 ) {
- u32 S;
- // render the full face
- SDL_Surface* scaled;
- S = fp_mul_scale( mask->w, scale );
- S /= current_scale_divisor;
- scaled = scale_surface( mask, NULL, S, S );
- if( scaled ) {
- SDL_Rect r;
- r.x = scaled->w; r.x /= 2; r.x += x;
- r.y = scaled->h; r.y /= 2; r.y += y;
- r = (SDL_Rect){ x - ( scaled->w / 2 ), y - ( scaled->h / 2 ), 0, 0 };
- SDL_SetColorKey( scaled, SDL_SRCCOLORKEY, color_to_surf( scaled, c_trans ) );
- SDL_BlitSurface( scaled, NULL, surf, &r );
- SDL_FreeSurface( scaled ); scaled = NULL;
- }
- }
- return 0;
- }
- u32 get_waypoint_count( void ) {
- u32 i, wc;
- if( !waypoints ) return 0;
- wc = 0;
- for( i = 0; i < frame_count; ++i ) {
- if( waypoints[ i ].scale ) {
- waypoints[ i ].frame = i;
- ++wc;
- }
- }
- return wc;
- }
- int draw_waypoints( SDL_Surface* surf ) {
- u32 last, i;
- if( !waypoints ) return -1;
- last = 0;
- for( i = 1; i < frame_count; ++i ) {
- if( waypoints[ i ].scale ) {
- SDL_Line(
- surf,
- waypoints[ last ].pos.x,
- waypoints[ last ].pos.y,
- waypoints[ i ].pos.x,
- waypoints[ i ].pos.y,
- c_white
- );
- last = i;
- }
- }
- return 0;
- }
- rgb96_t pixel_get_box( SDL_Surface* surf, long x, long y, u32 rad, u32* count ) {
- u32 dx, dy; rgb96_t r;
- r = (rgb96_t){.r=0,.g=0,.b=0};
- if( !surf ) return r;
- if( !rad ) return pixel_high_get( surf, x, y );
- rad = ( rad << 1 ) | 1;
- if( count ) *count = 0;
- for( dy = 0; dy < rad; ++dy ) {
- for( dx = 0; dx < rad; ++dx ) {
- r = pixel_high_add( r, pixel_high_get( surf, ( x + dx ) - ( rad >> 1 ), ( y + dy ) - ( rad >> 1 ) ) );
- if( count ) ++*count;
- }
- }
- return r;
- }
- s32 fp_scale( s32 n, s32 min, s32 one, s32 d ) {
- s64 r;
- r = n; r -= min; r *= one, r /= d;
- if( r < 0 ) r = 0;
- if( r > one ) r = one;
- return (s32)r;
- }
- tga_t* tga_mask = NULL;
- void blast_frame( SDL_Surface* surf ) {
- u32 min, max, x, y, diff;
- u32 last;
- if( !surf ) return;
- if( !tga_mask ) return;
- // uh this makes the frame lighter
- min = 0xFFFFFFFF;
- max = 0;
- last = 0;
- for( y = 0; y < surf->h; ++y ) {
- for( x = 0; x < surf->w; ++x ) {
- u32 v;
- if( tga_pixel_get8( tga_mask, x, y ) ) {
- v = last;
- } else {
- v = pixel_high_gray( pixel_get_box( surf, x, y, 2, NULL ) );
- }
- //v = pixel_high_gray( pixel_high_get( surf, x, y ) );
- if( v < min ) min = v;
- if( v > max ) max = v;
- last = v;
- }
- }
- min /= 25;
- max /= 25;
- diff = max - min;
- printf("min %u max %u diff %u\n", min, max, diff);
- for( y = 0; y < surf->h; ++y ) {
- for( x = 0; x < surf->w; ++x ) {
- rgb96_t c;
- c = pixel_high_get( surf, x, y );
- c.r = fp_scale( c.r, min, 0xFFF, diff );
- c.g = fp_scale( c.g, min, 0xFFF, diff );
- c.b = fp_scale( c.b, min, 0xFFF, diff );
- pixel_high_put( surf, x, y, c );
- }
- }
- }
- int draw_screen( SDL_Surface* surf, u32 n, int draw_face, _Bool draw_lines, _Bool lighten ) {
- waypoint_t pos;
- SDL_Surface* temp;
- if( !surf ) return -1;
- temp = surf_new( VID_WIDTH, VID_HEIGHT );
- if( !temp ) return -2;
- if( load_frame( n ) < 0 ) return -3;
- SDL_BlitSurface( current_frame, NULL, temp, NULL );
- if( lighten ) {
- blast_frame( temp );
- }
- if( draw_lines ) {
- draw_waypoints( temp );
- }
- pos = get_pos_scale( n );
- if( draw_face > 0 ) {
- draw_mask( temp, pos.pos.x, pos.pos.y, pos.scale, draw_face );
- }
- SDL_BlitSurface( temp, NULL, surf, NULL );
- SDL_FreeSurface( temp ); temp = NULL;
- return 0;
- }
- void file_write_u16( FILE* fp, u16 n ) {
- if( !fp ) return;
- fputc( n >> 8, fp );
- fputc( n&255U, fp );
- }
- void file_write_u32( FILE* fp, u32 n ) {
- if( !fp ) return;
- fputc( ( n >> 24 ) & 0xFF, fp );
- fputc( ( n >> 16 ) & 0xFF, fp );
- fputc( ( n >> 8 ) & 0xFF, fp );
- fputc( ( n ) & 0xFF, fp );
- }
- void file_write_u64( FILE* fp, u64 n ) {
- if( !fp ) return;
- fputc( ( n >> 56 ) & 0xFF, fp );
- fputc( ( n >> 48 ) & 0xFF, fp );
- fputc( ( n >> 40 ) & 0xFF, fp );
- fputc( ( n >> 32 ) & 0xFF, fp );
- fputc( ( n >> 24 ) & 0xFF, fp );
- fputc( ( n >> 16 ) & 0xFF, fp );
- fputc( ( n >> 8 ) & 0xFF, fp );
- fputc( ( n >> 0 ) & 0xFF, fp );
- }
- u16 file_read_u16( FILE* fp ) {
- u16 n;
- if( !fp ) return 0;
- n = fgetc( fp ); n <<= 8; n |= fgetc( fp );
- return n;
- }
- u32 file_read_u32( FILE* fp ) {
- u32 n;
- if( !fp ) return 0;
- n = fgetc( fp ) & 0xFF; n <<= 8;
- n |= fgetc( fp ) & 0xFF; n <<= 8;
- n |= fgetc( fp ) & 0xFF; n <<= 8;
- n |= fgetc( fp ) & 0xFF;
- return n;
- }
- u64 file_read_u64( FILE* fp ) {
- u64 n;
- if( !fp ) return 0ULL;
- n = fgetc( fp ) & 0xFF; n <<= 8;
- n |= fgetc( fp ) & 0xFF; n <<= 8;
- n |= fgetc( fp ) & 0xFF; n <<= 8;
- n |= fgetc( fp ) & 0xFF; n <<= 8;
- n |= fgetc( fp ) & 0xFF; n <<= 8;
- n |= fgetc( fp ) & 0xFF; n <<= 8;
- n |= fgetc( fp ) & 0xFF; n <<= 8;
- n |= fgetc( fp ) & 0xFF;
- return n;
- }
- void file_waypoint_write( FILE* fp, waypoint_t w ) {
- u64 n;
- if( !fp ) return;
- n = ( w.frame ) & 0xFFFFFF;
- n <<= 12; n |= ( w.pos.x ) & 0xFFF;
- n <<= 12; n |= ( w.pos.y ) & 0xFFF;
- n <<= 16; n |= ( w.scale ) & 0xFFFF;
- file_write_u64( fp, n );
- }
- waypoint_t file_waypoint_read( FILE* fp ) {
- u64 n;
- waypoint_t w;
- n = file_read_u64( fp );
- w.scale = n & 0xFFFF; n >>= 16;
- w.pos.y = n & 0xFFF; n >>= 12;
- w.pos.x = n & 0xFFF; n >>= 12;
- w.frame = n & 0xFFFFFF;
- if( w.scale == 0 ) {
- w.scale = 0x10000;
- }
- return w;
- }
- // ----------------------------------- buttons --------------------------------------
- void btn_waypoint_save( void ) {
- u32 waypoint_count, i;
- char out_path[ 8192 ];
- FILE* fp;
- if( !waypoints ) return;
- if( !dialog_save( "Save waypoints", out_path, 8192 ) ) return;
- // get the amount of waypoints
- waypoint_count = 0;
- for( i = 0; i < frame_count; ++i ) {
- if( waypoints[ i ].scale ) {
- ++waypoint_count;
- }
- }
- // open up the file
- fp = fopen( out_path, "wb" );
- if( !fp ) {
- printf( "Couldn't open %s for saving\n", out_path );
- return;
- }
- file_write_u32( fp, FILE_MAGIC );
- file_write_u32( fp, waypoint_count );
- file_write_u32( fp, frame_num );
- file_write_u16( fp, current_scale & 0xFFFF );
- file_write_u16( fp, current_scale_divisor & 0xFFFF );
- for( i = 0; i < frame_count; ++i ) {
- if( waypoints[ i ].scale ) {
- file_waypoint_write( fp, waypoints[ i ] );
- }
- }
- fclose( fp ); fp = NULL;
- printf("Saved %u waypoints to %s\n", waypoint_count, out_path);
- }
- void btn_waypoint_load( void ) {
- FILE* fp;
- u32 count, magic, cur_frame, cur_scale, i, loaded_count, div;
- char in_path[ 8192 ];
- if( !waypoints ) return;
- if( !dialog_load( "Load waypoints", in_path, 8192 ) ) return;
- fp = fopen( in_path, "rb" );
- if( !fp ) {
- printf("Cannot open %s\n", in_path);
- return;
- }
- magic = file_read_u32( fp );
- if( magic != FILE_MAGIC ) {
- puts("This is not a waypoint file");
- fclose( fp ); fp = NULL;
- return;
- }
- count = file_read_u32( fp );
- if( count == 0 ) {
- puts("Waypoint file has no waypoints");
- fclose( fp ); fp = NULL;
- return;
- }
- cur_frame = file_read_u32( fp );
- cur_scale = file_read_u16( fp );
- div = file_read_u16( fp );
- memset( waypoints, 0, sizeof(waypoint_t) * frame_count );
- waypoints[0] = WAYPOINT_DEFAULT;
- loaded_count = 0;
- for( i = 0; i < count; ++i ) {
- waypoint_t w;
- w = file_waypoint_read( fp );
- if( w.frame < frame_count ) {
- waypoints[ w.frame ] = w;
- ++loaded_count;
- }
- }
- fclose( fp ); fp = NULL;
- frame_num = cur_frame;
- current_scale = cur_scale;
- if( div == 0 ) {
- puts("Scale divisor is zero");
- } else {
- current_scale_divisor = div;
- }
- printf("Loaded %u waypoints from %s\n", loaded_count, in_path);
- }
- void btn_goto_start( void ) {
- frame_num = 0;
- }
- void btn_goto_end( void ) {
- frame_num = frame_count - 1;
- }
- void btn_goto_waypoint_first( void ) {
- waypoint_t w;
- w = waypoint_get_first();
- frame_num = w.frame;
- }
- void btn_goto_waypoint_last( void ) {
- waypoint_t w;
- w = waypoint_get_last();
- frame_num = w.frame;
- }
- void video_resize( u32 w_new, u32 h_new ) {
- w_new = h_new; w_new *= SCREEN_WIDTH; w_new += SCREEN_WIDTH / 2; w_new /= SCREEN_HEIGHT;
- screen_raw = SDL_SetVideoMode( w_new, h_new, 24, SDL_SWSURFACE | SDL_RESIZABLE );
- updated=True;
- }
- void btn_zoom1( void ) {
- video_resize( SCREEN_WIDTH, SCREEN_HEIGHT );
- }
- void btn_waypoint_delete( void ) {
- waypoint_delete( frame_num );
- }
- void btn_scale_set_range_start( void ) {
- scale_set_start_frame = frame_num;
- }
- void btn_scale_set_range_end( void ) {
- scale_set_end_frame = frame_num;
- }
- void btn_scale_set_range_do( void ) {
- u32 i;
- if( !waypoints ) return;
- if( scale_set_start_frame < 0 ) scale_set_start_frame = 0;
- if( scale_set_start_frame > frame_count ) scale_set_start_frame = frame_count;
- if( scale_set_end_frame < 0 ) scale_set_end_frame = 0;
- if( scale_set_end_frame > frame_count ) scale_set_end_frame = frame_count;
- if( scale_set_end_frame < scale_set_start_frame ) {
- i = scale_set_end_frame; scale_set_end_frame = scale_set_start_frame; scale_set_start_frame = i;
- }
- for( i = scale_set_start_frame; i < scale_set_end_frame; ++i ) {
- if( waypoints[ i ].scale ) {
- waypoints[ i ].frame = i;
- waypoints[ i ].scale = current_scale;
- }
- }
- }
- int screen_flush( SDL_Surface* in, SDL_Surface* out );
- void emit_frames( _Bool lightened ) {
- u32 i;
- if( !waypoints ) return;
- for( i = 0; i < frame_count; ++i ) {
- SDL_Surface* out;
- out = surf_new( VID_WIDTH, VID_HEIGHT );
- if( out ) {
- char out_fname[ 64 ];
- draw_screen( out, i, 2, 0, lightened );
- memset( out_fname, 0, 64 );
- snprintf( out_fname, 63, "../frames_out/out_%06u.tga", i );
- save_to_tga( out, out_fname );
- screen_flush( out, screen_raw );
- SDL_UpdateRect( screen_raw, 0, 0, screen_raw->w, screen_raw->h );
- printf("Saved %s\n", out_fname);
- SDL_FreeSurface( out ); out = NULL;
- }
- }
- }
- void btn_emit_frames( void ) {
- emit_frames( 0 );
- puts("Emit frames");
- }
- void btn_emit_frames_bright( void ) {
- emit_frames( 1 );
- puts("Emit frames bright");
- }
- typedef struct button_t button_t;
- struct button_t {
- int x;
- int y;
- const char* str;
- void (*func)(void);
- };
- button_t buttons[] = {
- (button_t){ 160, 0, "save waypoints", btn_waypoint_save },
- (button_t){ 160, 16, "load waypoints", btn_waypoint_load },
- (button_t){ 0, 16, "Fbgn", btn_goto_start },
- (button_t){ 29, 16, "Fend", btn_goto_end },
- (button_t){ 29*2, 16, "Wbgn", btn_goto_waypoint_first },
- (button_t){ 29*3, 16, "Wend", btn_goto_waypoint_last },
- (button_t){ 132, 16, "1x", btn_zoom1 },
- (button_t){ 256, 0, "delete waypoint", btn_waypoint_delete },
- (button_t){ 256, 16, "RangeStart", btn_scale_set_range_start },
- (button_t){ 322, 16, "RangeEnd", btn_scale_set_range_end },
- (button_t){ 352, 0, "RangeSet", btn_scale_set_range_do },
- (button_t){ 385, 16, "Emit", btn_emit_frames },
- (button_t){ 420, 16, "Emit bright", btn_emit_frames_bright }
- };
- #define BUTTON_COUNT (sizeof(buttons) / sizeof(buttons[0]))
- int click_buttons( point_t mouse, int off_x, int off_y ) {
- int i;
- for( i = 0; i < BUTTON_COUNT; ++i ) {
- int x, y, w, h;
- if( !buttons[i].func ) continue;
- w = h = 0;
- if( buttons[i].str != NULL ) {
- text_len_get( buttons[i].str, &w, &h );
- }
- w += 4; h += 4;
- x = buttons[i].x + off_x;
- y = buttons[i].y + off_y;
- if( mouse.x >= x && mouse.x < ( x + w ) && mouse.y >= y && mouse.y < ( y + h ) ) {
- buttons[i].func();
- return i;
- }
- }
- return -1;
- }
- int draw_button( SDL_Surface* surf, int x, int y, const char* str ) {
- int sl;
- SDL_Rect r;
- if( !surf ) return -1;
- sl = 0;
- if( str ) {
- sl = strlen( str );
- sl *= 6;
- }
- r.w = 2+sl+2; r.h = 2+8+2; r.x = x; r.y = y;
- SDL_FillRect( surf, &r, color_to_surf( surf, c_white ) );
- r.x += 1; r.y += 1; r.w -= 2; r.h -= 2;
- SDL_FillRect( surf, &r, color_to_surf( surf, c_black ) );
- if( str )
- text_print_str( x+2, y+2, (char*)str, c_white, surf );
- return 0;
- }
- int draw_buttons( SDL_Surface* surf, u32 y ) {
- int i;
- for( i = 0; i < BUTTON_COUNT; ++i ) {
- draw_button( surf, buttons[ i ].x, buttons[ i ].y + y, buttons[ i ].str );
- }
- return 0;
- }
- void timecode_print( int x, int y, u32 n, color_t color, SDL_Surface* t ) {
- u32 mins, sex, frames;
- char temp_buf[ 64 ];
- if( !t ) return;
- memset( temp_buf, 0, 64 );
- mins = n / ( 30U * 60U );
- sex = ( n / 30U ) % 60U;
- frames = n % 30U;
- snprintf( temp_buf, 63, "frame %i %02i:%02i.%02i", n, mins, sex, frames );
- text_print_str( x, y, temp_buf, color, t );
- }
- int draw_hud( SDL_Surface* surf ) {
- int x, y;
- waypoint_t w;
- if( !surf ) return -1;
- SDL_Clear_rect_easy( surf, 0, VID_HEIGHT, VID_WIDTH, 32, c_gray25 );
- w = get_pos_scale( frame_num );
- x = 0; y = VID_HEIGHT+1;
- draw_mask( surf, 700, y+16, current_scale, 1 );
- if( is_setting_scale )
- text_print_char( 118, y+16, 'S', c_white, surf );
- timecode_print( x, y, frame_num, c_white, surf );
- y += 8;
- text_print__str_u32_u32( x, y, "scale", current_scale, w.scale, c_white, surf );
- y=VID_HEIGHT+1;
- if( waypoints ) {
- if( waypoints[ frame_num ].scale ) {
- text_print_str( 420, y, "Frame has waypoint", c_white, surf );
- y+=8;
- }
- }
- text_print__str_int_str_int( 420, y, "WS S", scale_set_start_frame, "| WS E", scale_set_end_frame, c_white, surf );
- if( draw_buttons( surf, VID_HEIGHT+1 ) < 0 ) return -2;
- return 0;
- }
- int screen_flush( SDL_Surface* in, SDL_Surface* out ) {
- if( !in ) return -1;
- if( !out ) return -2;
- if( in->w == out->w && in->h == out->h ) {
- //scale_surface( in, out, out->w, out->h );
- SDL_BlitSurface( in, NULL, out, NULL );
- } else {
- scale_surface( in, out, out->w, out->h );
- }
- return 0;
- }
- int main( int argc, char** argv ) {
- if( SDL_WasInit( SDL_INIT_VIDEO ) )
- return 0;
- SDL_Init(SDL_INIT_VIDEO);
- screen = surf_new( SCREEN_WIDTH, SCREEN_HEIGHT );
- if( !screen ) {
- SDL_Quit();
- return 0;
- }
- screen_raw = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 24, SDL_SWSURFACE | SDL_RESIZABLE );
- if( !screen_raw ) {
- SDL_FreeSurface( screen ); screen = NULL;
- SDL_Quit();
- return 0;
- }
- SDL_Clear( screen, c_gray25 );
- frame_count = get_frame_count();
- if( !frame_count ) {
- SDL_FreeSurface( screen ); screen = NULL;
- SDL_Quit();
- return 0;
- }
- if( waypoint_init( frame_count ) < 0 ) {
- puts("waypoint_init fail" );
- abort();
- }
- if( mask_init() < 0 ) {
- puts("mask_init fail");
- abort();
- }
- tga_mask = tga_load( "shitmask.tga" );
- if( !tga_mask ) abort();
- done = 0;
- while( !done ) {
- while(SDL_PollEvent(&event)) {
- if( event.type == SDL_QUIT )
- done = 1;
- if( event.type == SDL_VIDEORESIZE ) {
- video_resize( event.resize.w, event.resize.h );
- }
- #define K (event.key.keysym.sym)
- if( event.type == SDL_KEYDOWN ) {
- if( K == SDLK_RIGHT ) {
- frame_num = get_num_clipped( frame_num, play_speed, 0, frame_count-1 );
- updated=True;
- }
- if( K == SDLK_LEFT ) {
- frame_num = get_num_clipped( frame_num, -play_speed, 0, frame_count-1 );
- updated=True;
- }
- if( K == SDLK_p ) {
- is_playing = True;
- }
- if( K == SDLK_o ) {
- is_playing_dir = -1;
- }
- if( K == SDLK_f ) {
- play_speed = 30;
- }
- if( K == SDLK_u ) {
- force_full_face = True;
- updated = True;
- }
- if( K == SDLK_v ) {
- new_w = True;
- updated = True;
- }
- if( K == SDLK_c ) {
- is_setting_scale = True;
- updated = True;
- }
- if( K == SDLK_l ) {
- do_draw_lines = True;
- updated = True;
- }
- if( K == SDLK_UP ) {
- current_scale += SCALE_STEP;
- updated=True;
- }
- if( K == SDLK_DOWN ) {
- current_scale -= SCALE_STEP;
- updated=True;
- }
- if( K == SDLK_k ) {
- do_lighten = True;
- updated = True;
- }
- //printf( "SDL_KEYDOWN %i\n", K );
- }
- if( event.type == SDL_KEYUP ) {
- if( K == SDLK_p ) {
- is_playing = False;
- updated = True;
- }
- if( K == SDLK_o ) {
- is_playing_dir = 1;
- }
- if( K == SDLK_f ) {
- play_speed = 1;
- }
- if( K == SDLK_u ) {
- force_full_face = False;
- updated = True;
- }
- if( K == SDLK_v ) {
- new_w = False;
- updated = True;
- }
- if( K == SDLK_c ) {
- is_setting_scale = False;
- updated = True;
- }
- if( K == SDLK_l ) {
- do_draw_lines = False;
- updated = True;
- }
- if( K == SDLK_k ) {
- do_lighten = False;
- updated = True;
- }
- //printf( "SDL_KEYUP %i\n", K );
- }
- #undef K
- if( event.type == SDL_MOUSEBUTTONDOWN ) {
- point_t pos;
- pos.x = event.button.x;
- pos.y = event.button.y;
- pos = mouse_transform( pos );
- if( event.button.button == 1 ) {
- if( event.button.y >= VID_HEIGHT ) {
- click_buttons( pos, 0, VID_HEIGHT+1 );
- updated = True;
- }
- } else if( event.button.button == 4 ) {
- // wheel up
- if( is_setting_scale ) {
- current_scale += SCALE_STEP;
- } else {
- frame_num = get_num_clipped( frame_num, play_speed, 0, frame_count-1 );
- }
- updated = True;
- } else if( event.button.button == 5 ) {
- // wheel down
- if( is_setting_scale ) {
- current_scale -= SCALE_STEP;
- // it can't be zero since that means "no waypoint is here"
- if( !current_scale ) current_scale = DEFAULT_SCALE;
- } else {
- frame_num = get_num_clipped( frame_num, -play_speed, 0, frame_count-1 );
- }
- updated = True;
- }
- }
- }
- if( is_playing ) {
- frame_num = get_num_clipped( frame_num, play_speed*is_playing_dir, 0, frame_count-1 );
- updated=True;
- }
- if( new_w ) {
- updated = True;
- }
- if( updated ) {
- int mask_state;
- if( current_scale == 0 ) {
- current_scale = FP_ONE;
- }
- if( new_w ) {
- point_t mouse_pos;
- SDL_GetMouseState( &mouse_pos.x, &mouse_pos.y );
- mouse_pos = mouse_transform( mouse_pos );
- waypoint_add( frame_num, mouse_pos, current_scale );
- }
- mask_state = 1; mask_state += force_full_face;
- draw_screen( screen, frame_num, mask_state, do_draw_lines, do_lighten );
- draw_hud( screen );
- screen_flush( screen, screen_raw );
- SDL_UpdateRect( screen_raw, 0, 0, screen_raw->w, screen_raw->h );
- updated=False;
- }
- SDL_Delay(32);
- }
- waypoints_free( frame_count );
- tga_mask = tga_free( tga_mask );
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement