// gcc -std=c99 -O2 -W cccs.c -lSDL -o cccs && ./cccs
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <SDL/SDL.h>
#include <math.h>
typedef struct{
unsigned int uWidth;
unsigned int uHeight;
SDL_Surface *pData;
} SBitMapFont;
SBitMapFont* LoadBitMapFontFromFile(char *pFileName)
{
SBitMapFont* pReturn = NULL;
SDL_Surface *pTmpSurface;
if((pTmpSurface = SDL_LoadBMP(pFileName))
&& !(pReturn = malloc(sizeof(SBitMapFont))))
{
pReturn->uWidth = (unsigned int)pTmpSurface->w/255;
pReturn->uHeight = (unsigned int)pTmpSurface->h;
pReturn->pData = pTmpSurface;
}
else
fprintf(stderr,"[Erreur ::LoadFontFromFile] : Can't load %s\n",pFileName);
return pReturn;
}
/* amusons nous à dessiner avec les nombres premiers! */
uint8_t isprime(uint32_t n) {
for (uint32_t d=2;d*d<=n;d++)
if(!(n%d))
return 0;
return 42;
}
/* oui c'est moche et lent */
void addPixelSat(SDL_Surface *s, int x, int y, Uint32 c) {
Uint32 cc = *((Uint32*)(s->pixels) + x + y * s->w);
Uint16 nr = ((c >> 16) & 255)+((cc >> 16) & 255);
Uint16 ng = ((c >> 8) & 255)+((cc >> 8) & 255);
Uint16 nb = (c & 255)+(cc & 255);
if (nr > 255) nr = 255;
if (ng > 255) ng = 255;
if (nb > 255) nb = 255;
*((Uint32*)(s->pixels) + x + y * s->w) = (nr<<16)|(ng<<8)|nb;
}
void line (SDL_Surface *s, float x1, float y1, float x2, float y2, Uint32 c) {
double hl=fabs(x2-x1), vl=fabs(y2-y1), length=(hl>vl)?hl:vl;
float deltax=(x2-x1)/(float)length, deltay=(y2-y1)/(float)length;
for (int i=0; i<(int)length; i++)
addPixelSat(s, (int)(x1+=deltax), (int)(y1+=deltay), c);
}
char *lipsum =
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit "
"Aenean volutpat lorem ut enim. Cras accumsan porta augue "
"Nulla quis magna. Morbi at dolor. Vivamus quam. Fusce feugiat egestas elit "
"Nullam tincidunt. Donec et felis "
"Fusce purus dui, semper vitae, tincidunt nec, malesuada id, purus "
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit "
"Etiam a risus nec tellus ultrices tempor. Nunc mauris velit, euismod vitae, hendrerit et, auctor eget, odio "
"Praesent dapibus nulla. Nunc et nulla "
"Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec lobortis rhoncus tortor "
;
int font[26][11]={{7,24,36,66,126,66,66,66,0,0,0},{8,240,136,136,252,130,130,252,0,0,0},{8,28,98,128,128,128,64,62,0,0,0},
{8,240,136,132,130,130,130,252,0,0,0},{8,252,128,128,240,128,128,254,0,0,0},{6,62,32,32,60,32,32,32,0,0,0},
{8,60,66,128,142,130,66,60,0,0,0},{7,66,66,66,126,66,66,66,0,0,0},{2,2,2,2,2,2,2,2,0,0,0},{5,2,2,2,2,18,18,12,0,0,0},
{7,66,68,72,80,104,68,66,0,0,0},{5,16,16,16,16,16,16,30,0,0,0},{8,130,198,170,146,130,130,130,0,0,0},
{6,34,34,50,42,38,34,34,0,0,0},{7,24,36,66,66,66,66,60,0,0,0},{6,56,36,34,60,32,32,32,0,0,0},
{8,48,72,132,132,148,140,118,0,0,0},{7,112,72,68,120,72,68,66,0,0,0},{6,14,16,32,28,2,4,56,0,0,0},
{6,62,8,8,8,8,8,8,0,0,0},{5,18,18,18,18,18,18,12,0,0,0},{8,130,130,68,68,40,40,16,0,0,0},
{8,130,130,130,146,146,146,108,0,0,0},{8,130,68,40,16,40,68,130,0,0,0},{6,34,34,20,20,8,8,16,0,0,0},
{8,254,4,8,16,32,64,252,0,0,0}};
#define mulRGB_tmp(k,x,n) ((128+(((x)>>n)&255)*(k))>>8)<<n
inline uint32_t mulRGB(int k,uint32_t x) { return mulRGB_tmp(k,x,16) | mulRGB_tmp(k,x,8) | mulRGB_tmp(k,x,0); }
int main(int argc,char *argv[]){
SDL_Surface *screen;
SDL_Event event;
if(SDL_Init(SDL_INIT_VIDEO) == -1)
exit(1);
atexit(SDL_Quit);
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
if (screen == NULL)
exit(1);
SDL_WM_SetCaption("Enjoy !", NULL);
/* youpi, ça bouge! */
for (uint32_t n=0,z=160+320*120,c=0x123456; !SDL_PollEvent(&event)
|| event.type!=SDL_QUIT; n+=64*48,c=(c*189+123)%0xFF7821,z=c/21<320*240?c/21:z) {
for (uint32_t x=0; x<64*48; x++)
if (isprime(x+n)) {
SDL_Rect r={z%320+x%64*5,z/320+x/64*5,5,5};
SDL_FillRect(screen, &r, c);
}
/* il est flou afflelou */
uint32_t *pix = (uint32_t *)screen->pixels;
int pp[] = {1,640*480-1,640,640*480-640,1,640*480-1,640,640*480-640,0};
for (int *p = pp; *p; p++) {
uint32_t x=0,y=*p;
for (; y<640*480; x++,y++)
pix[x] = pix[x]-((pix[x]>>4)&0x0F0F0F)+((pix[y]>>4)&0x0F0F0F);
for (y=0; x<640*480; x++,y++)
pix[x] = pix[x]-((pix[x]>>4)&0x0F0F0F)+((pix[y]>>4)&0x0F0F0F);
}
static int tpos = 0;
for (Uint16 l = 0; l < tpos; l++) {
char huhu = lipsum[l];
if ((huhu = (huhu > 'Z') ? (huhu - 'a') : (huhu - 'A')) != ' '-'A')
for (Uint16 y = 1; y < 10; y++)
for (Uint16 x = 0; x < 8; x++)
if (font[huhu][y]&(1<<(7-x)))
addPixelSat(screen, 160 + (x+((l * 8)))%320,
120 + (y + ((l * 8) / 320) * 10)%240, 0xffffff);
}
if(lipsum[++tpos] == 0) tpos = 0;
static float xs = 0.0, ys = 0.0;
line(screen, sin(xs+=0.1)*320+320, cos(ys+=0.07)*240+240, cos(xs)*320+320, sin(ys)*240+240,rand());
SDL_Flip(screen);
SDL_Delay(20);
}
return 0;
}
EDIT : résumé : optimisation du flou, correction d'un bug de addPixelSat ^^