Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __sdl_gen_c
- #define __sdl_gen_c
- #include "sdl_gen.h"
- color_t color_new( u8 r, u8 g, u8 b, u8 a ) {
- color_t out;
- out = r; out <<= 8;
- out |= g; out <<= 8;
- out |= b; out <<= 8;
- out |= a;
- return out;
- }
- size_t fsize( FILE* fp ){
- size_t prev, fs;
- if( fp == NULL ) return 0;
- prev = ftell( fp );
- fseek( fp, 0L, SEEK_END );
- fs = ftell( fp );
- fseek( fp, prev, SEEK_SET );
- return fs;
- }
- u32 color_to_surf( SDL_Surface* surf, color_t color ) {
- if( !surf ) return 0;
- if( !surf->format ) return 0;
- return SDL_MapRGBA(
- surf->format,
- color>>24,
- (color>>16)&0xFF,
- (color>>8)&0xFF,
- color&0xFF
- );
- }
- u32 surf_to_color( SDL_Surface* surf, u32 color_sdl ) {
- u8 r, g, b, a;
- color_t out;
- if( !surf ) return 0;
- if( !surf->format ) return 0;
- SDL_GetRGBA( color_sdl, surf->format, &(r), &(g), &(b), &(a) );
- out = ( ((color_t)r)<<24 )|( ((color_t)r)<<16 )|( ((color_t)r)<<8 )|a;
- return out;
- }
- void SDL_Clear( SDL_Surface* surf, color_t color ) {
- if( surf == NULL ) return;
- if( surf->format == NULL ) return;
- SDL_FillRect( surf, NULL, color_to_surf( surf, color ) );
- }
- void SDL_Clear_rect_easy( SDL_Surface* surf, int x, int y, int w, int h, color_t color ) {
- SDL_Rect r;
- if( surf == NULL ) return;
- if( surf->format == NULL ) return;
- color = color_to_surf( surf, color );
- r.x = x; r.y = y; r.w = w; r.h = h;
- SDL_FillRect( surf, &r, color );
- }
- void SDL_PutPixel( SDL_Surface* surf, long x, long y, color_t color ) {
- u32 color_sdl;
- int bpp; void* p;
- if( surf == NULL ) return;
- if( surf->pixels == NULL ) return;
- if( surf->format == NULL ) return;
- if( x < 0 || x >= surf->w || y < 0 || y >= surf->h ) return;
- color_sdl = color_to_surf( surf, color );
- bpp = surf->format->BytesPerPixel;
- x *= bpp;
- y *= surf->pitch; y += x;
- p = surf->pixels; p += y;
- switch( bpp ) {
- case 1: {
- *(u8*)p = color_sdl;
- } break;
- case 2: {
- *(u16*)p = color_sdl;
- } break;
- case 3: {
- u8* p8;
- p8 = (u8*)p;
- #if SDL_BYTEORDER == SDL_BIG_ENDIAN
- p8[0] = (color_sdl >> 16) & 0xFF;
- p8[1] = (color_sdl >> 8) & 0xFF;
- p8[2] = color_sdl & 0xFF;
- #else
- p8[0] = color_sdl & 0xFF;
- p8[1] = (color_sdl >> 8) & 0xFF;
- p8[2] = (color_sdl >> 16) & 0xFF;
- #endif
- } break;
- case 4: {
- *(u32*)p = color_sdl;
- } break;
- }
- }
- color_t SDL_GetPixel( SDL_Surface* surf, long x, long y ) {
- u8* px;
- if( !surf ) return c_black;
- if( x < 0 || y < 0 || x >= surf->w || y >= surf->h || surf->pixels == NULL )
- return c_black;
- px = (u8*)surf->pixels; px += ( y * surf->pitch ) + ( x * surf->format->BytesPerPixel );
- if( surf->format->BytesPerPixel == 1 ) {
- return color_new( *px, *px, *px, 255 );
- } else if( surf->format->BytesPerPixel == 3 ) {
- return color_new( px[2], px[1], px[0], 255 );
- } else if( surf->format->BytesPerPixel == 4 ) {
- return color_new( px[3], px[2], px[1], px[0] );
- }
- return c_black;
- }
- #define int_abs( n ) ( (n) < 0 ? -(n) : (n) )
- void SDL_Line( SDL_Surface* surf, int x1, int y1, int x2, int y2, color_t color ) {
- int dx, dy, sx, sy, err, e2, count;
- if( surf == NULL ) return;
- if( surf->pixels == NULL || surf->w == 0 || surf->h == 0 ) return;
- dx = int_abs( x2 - x1 );
- sx = x1 < x2 ? 1 : -1;
- dy = int_abs( y2 - y1 );
- sy = y1 < y2 ? 1 : -1;
- err = ( dx > dy ? dx : -dy ) / 2;
- count = 0;
- while(1) {
- SDL_PutPixel( surf, x1, y1, color );
- if( ( x1 == x2 && y1 == y2 ) || count > 1000000 )
- break;
- else {
- e2 = err;
- if( e2 > -dx ) { err -= dy; x1 += sx; }
- if( e2 < dy ) { err += dx; y1 += sy; }
- }
- ++count;
- }
- }
- void SDL_Line_Straight( SDL_Surface* surf, int x, int y, int dx, int dy, int length, color_t color ) {
- if( !surf ) return;
- while( length-- ) {
- SDL_PutPixel( surf, x, y, color );
- x += dx; y += dy;
- }
- }
- void SDL_Circle( SDL_Surface* surf, int x0, int y0, int radius, color_t color ) {
- int f, ddF_x, ddF_y, x, y;
- if( !surf ) return;
- f = 1 - radius;
- ddF_x = 0;
- ddF_y = -2 * radius;
- x = 0;
- y = radius;
- SDL_PutPixel( surf, x0, y0 + radius, color );
- SDL_PutPixel( surf, x0, y0 - radius, color );
- SDL_PutPixel( surf, x0 + radius, y0, color );
- SDL_PutPixel( surf, x0 - radius, y0, color );
- while( x < y ) {
- if( f >= 0 ) {
- --y;
- ddF_y += 2;
- f += ddF_y;
- }
- ++x;
- ddF_x += 2;
- f += ddF_x + 1;
- SDL_PutPixel( surf, x0 + x, y0 + y, color);
- SDL_PutPixel( surf, x0 - x, y0 + y, color);
- SDL_PutPixel( surf, x0 + x, y0 - y, color);
- SDL_PutPixel( surf, x0 - x, y0 - y, color);
- SDL_PutPixel( surf, x0 + y, y0 + x, color);
- SDL_PutPixel( surf, x0 - y, y0 + x, color);
- SDL_PutPixel( surf, x0 + y, y0 - x, color);
- SDL_PutPixel( surf, x0 - y, y0 - x, color);
- }
- }
- SDL_Surface* surf_new( int w, int h ) {
- SDL_Surface* r;
- if( w <= 0 || h <= 0 ) return NULL;
- r = SDL_CreateRGBSurface(
- SDL_SWSURFACE,
- w,
- h,
- 24, 0,0,0,0
- /*#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- 0xFF000000,
- 0x00FF0000,
- 0x0000FF00,
- 0x000000FF
- #else
- 0x000000FF,
- 0x0000FF00,
- 0x00FF0000,
- 0xFF000000
- #endif*/
- );
- return r;
- }
- int scale_surface_rgb24( SDL_Surface* in, SDL_Surface* out ) {
- u32 x, y;
- if( !in ) return -1;
- if( !in->format ) return -2;
- if( in->format->BitsPerPixel != 24 ) return -3;
- if( !out ) return -4;
- if( !out->format ) return -5;
- if( out->format->BitsPerPixel != 24 ) return -6;
- if( in->w == out->w && in->h == out->h ) {
- SDL_BlitSurface( in, NULL, out, NULL );
- return 0;
- }
- for( y = 0; y < out->h; ++y ) {
- u32 dy;
- dy = ( y * in->h ) / out->h;
- if( dy >= in->h ) continue;
- for( x = 0; x < out->w; ++x ) {
- u8 c[3];
- u8* px;
- u32 dx;
- dx = ( x * in->w ) / out->w;
- if( dx >= in->w ) continue;
- px = (u8*)in->pixels; px += ( dy * in->pitch ) + ( dx * in->format->BytesPerPixel );
- c[0] = px[0];
- c[1] = px[1];
- c[2] = px[2];
- px = (u8*)out->pixels; px += ( y * out->pitch ) + ( x * out->format->BytesPerPixel );
- px[0] = c[0];
- px[1] = c[1];
- px[2] = c[2];
- }
- }
- return 0;
- }
- SDL_Surface* scale_surface( SDL_Surface* in, SDL_Surface* out, int new_w, int new_h ) {
- int x, y;
- int w2, h2;
- if( !in || new_w <= 0 || new_h <= 0 ) return NULL;
- if( !out ) {
- out = surf_new( new_w, new_h );
- if( !out ) return NULL;
- }
- w2 = new_w/2;
- h2 = new_h/2;
- // scale it
- for( y = 0; y < new_h; ++y )
- for( x = 0; x < new_w; ++x )
- SDL_PutPixel( out, x, y, SDL_GetPixel( in, ( ( ((s64)x) * in->w ) + w2 ) / new_w, ( ( ((s64)y) * in->h ) + h2 ) / new_h ) );
- return out;
- }
- // write out a TGA
- void file_write_u16le( u16 n, FILE* fp ) {
- if( !fp ) return;
- fputc( n & 0xFF, fp );
- fputc( n >> 8, fp );
- }
- void file_write_zeroes( unsigned int len, FILE* fp ) {
- if( !fp ) return;
- while( len-- ) {
- fputc( 0, fp );
- }
- }
- /*
- simple TGA header
- /---------------- uint8_t pixel type, 2 for direct RGB
- | /---------- int16_t X origin, pretty much always 0
- | | /-------- int16_t Y origin, pretty much always == height
- | | | /------ uint16_t width
- | | | | /---- uint16_t height
- | | | | | /-- uint8_t bits-per-pixel
- | | | | | |/- uint8_t bit-field: 00S0BBBB B=bits per alpha S=screen origin
- | | | | | ||
- 00P00000XXYYWWHHBC
- values are little-endian
- RGBA values are stored as BGRA
- */
- int save_to_tga( SDL_Surface* surf, char* path ) {
- FILE* fp;
- if( !surf ) return -1;
- if( surf->format->BytesPerPixel != 3 ) return -2;
- if( !path ) return -3;
- fp = fopen( path, "wb" );
- if( !fp ) return -4;
- file_write_zeroes( 2U, fp );
- fputc( 2U, fp );
- file_write_zeroes( 7U, fp );
- file_write_u16le( surf->h, fp );
- file_write_u16le( surf->w, fp );
- file_write_u16le( surf->h, fp );
- //fputc( surf->format->BytesPerPixel * 8, fp );
- fputc( 24U, fp );
- fputc( 32U , fp ); // 0b00100000
- {
- u8* px; u32 y;
- px = (u8*)surf->pixels;
- for( y = 0; y < surf->h; ++y ) {
- fwrite( px, 3, surf->w, fp ); // demb
- px += surf->pitch;
- }
- }
- fclose( fp ); fp = NULL;
- return 0;
- }
- #include "color_table.c"
- u32 pixel_high_gray( rgb96_t c ) {
- u32 n;
- // 1.0 is 4095
- n = ( U32(c.r) * 1224U ) / 4095U;
- n += ( U32(c.g) * 2404U ) / 4095U;
- n += ( U32(c.b) * 467U ) / 4095U;
- return n;
- }
- rgb96_t pixel_high_get( SDL_Surface* surf, long x, long y ) {
- color_t c;
- rgb96_t r;
- if( !surf ) return RGB96_BLACK;
- // keep it within bounds
- if( x < 0 ) x = 0; if( x >= surf->w ) x = surf->w - 1;
- if( y < 0 ) y = 0; if( y >= surf->h ) y = surf->h - 1;
- c = SDL_GetPixel( surf, x, y );
- r.r = srgb_to_linear_table[ ( c >> 24 ) & 0xFF ];
- r.g = srgb_to_linear_table[ ( c >> 16 ) & 0xFF ];
- r.b = srgb_to_linear_table[ ( c >> 8 ) & 0xFF ];
- return r;
- }
- int pixel_high_put( SDL_Surface* surf, long x, long y, rgb96_t c ) {
- if( !surf ) return -1;
- // keep it within bounds
- if( x < 0 ) x = 0; if( x >= surf->w ) x = surf->w - 1;
- if( y < 0 ) y = 0; if( y >= surf->h ) y = surf->h - 1;
- if( c.r > 0xFFF ) c.r = 0xFFF;
- if( c.g > 0xFFF ) c.g = 0xFFF;
- if( c.b > 0xFFF ) c.b = 0xFFF;
- SDL_PutPixel( surf, x, y, color_new(
- linear_to_srgb_table[ c.r ],
- linear_to_srgb_table[ c.g ],
- linear_to_srgb_table[ c.b ],
- 255U
- ) );
- return 0;
- }
- rgb96_t pixel_high_add( rgb96_t c1, rgb96_t c2 ) {
- c1.r += c2.r;
- c1.g += c2.g;
- c1.b += c2.b;
- return c1;
- }
- rgb96_t pixel_high_div_n( rgb96_t c1, u32 n ) {
- c1.r /= n;
- c1.g /= n;
- c1.b /= n;
- return c1;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement