Skip to content

Instantly share code, notes, and snippets.

@JohnnyonFlame
Last active August 29, 2015 14:21
Show Gist options
  • Save JohnnyonFlame/54393ac27b089a1a2ace to your computer and use it in GitHub Desktop.
Save JohnnyonFlame/54393ac27b089a1a2ace to your computer and use it in GitHub Desktop.
Fade between two 16bpp images w/ dithering
#include <stdio.h>
#include <math.h>
#include <SDL.h>
#include <SDL_image.h>
int main(int argc, char *argv[])
{
SDL_Surface *vid = SDL_SetVideoMode(320, 240, 16, 0);
SDL_Surface *image = IMG_Load("pepe.png");
SDL_Surface *image2 = IMG_Load("spurdo.png");
image = SDL_ConvertSurface(image, vid->format, 0);
image2 = SDL_ConvertSurface(image2, vid->format, 0);
if (!vid || !image)
{
printf("Image loading/Video setting failed, faggot. %s\n", IMG_GetError());
return -1;
}
while (1)
{
SDL_Event ev;
while (SDL_PollEvent(&ev))
{
switch(ev.type)
{
case SDL_QUIT:
return 0;
default: break;
}
}
SDL_FillRect(vid, NULL, 0);
Uint32 mul_a = (Uint32)((sinf(SDL_GetTicks() / 1000.f)+1.f) * 8388607.f);
Uint32 mul_b = mul_a ^ 0xFFFFFF;
Uint32 r, g, b, err_r=0, err_g=0, err_b=0;
Uint16 c;
Uint16 c2;
Uint8 *dest = vid->pixels;
Uint8 *src = image->pixels;
Uint8 *src2 = image2->pixels;
int i, j;
for (i=0; i<image->h; i++)
{
for (j=0; j<image->w; j++)
{
c = *(Uint16*)src;
c2 = *(Uint16*)src2;
r =
(((c >> 11) & 0b11111 ) * mul_a) +
(((c2 >> 11) & 0b11111 ) * mul_b) +
err_r;
g =
(((c >> 5) & 0b111111) * mul_a) +
(((c2 >> 5) & 0b111111) * mul_b) +
err_g;
b =
(((c ) & 0b11111 ) * mul_a) +
(((c2 ) & 0b11111 ) * mul_b) +
err_b;
err_r = r & 0xFFFFFF;
err_g = g & 0xFFFFFF;
err_b = b & 0xFFFFFF;
r &= 0x1F000000;
g &= 0x3F000000;
b &= 0x1F000000;
*(Uint16*)dest = (r >> 13) | (g >> 19) | (b >> 24);
//skip color (2 bytes = 16bit)
dest += 2;
src += 2;
src2 += 2;
}
//skip line (line width - width * color depth)
dest += vid->pitch - (image->w * 2);
src += image->pitch - (image->w * 2);
src2 += image->pitch - (image->w * 2);
}
SDL_Flip(vid);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment