Files
glider-pixel/glisseur_10.c
2026-03-08 04:13:03 +01:00

415 lines
10 KiB
C

#include <stdio.h>
#include <unistd.h>
#include "SDL.h"
#include <math.h>
/*
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
*/
/// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// ///
/// COMPILING (SDL and SDL-dev or -devel librairies needed: ///
/// $ gcc `sdl-config --cflags --libs` glisseur_10.c -o glisseur_10 ///
/// ///
/// RUNING: ///
/// $ ./sdl-glisseur_10 ///
/// ///
/// STOPING: ///
/// $ top ---> get PID of the glider proccess ///
/// $ kill -9 PID ///
/// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// ///
// Canvas height and width //
#define WIDTH 640
#define HEIGHT 480
// 4 colors definition //
#define COLOR_0_R 254
#define COLOR_0_G 254
#define COLOR_0_B 254
#define COLOR_1_R 254
#define COLOR_1_G 0
#define COLOR_1_B 0
#define COLOR_2_R 0
#define COLOR_2_G 254
#define COLOR_2_B 0
#define COLOR_3_R 0
#define COLOR_3_G 0
#define COLOR_3_B 254
/*
#define COLOR_0_R 254
#define COLOR_0_G 254
#define COLOR_0_B 254
#define COLOR_1_R 0
#define COLOR_1_G 0
#define COLOR_1_B 0
#define COLOR_2_R 240
#define COLOR_2_G 240
#define COLOR_2_B 0
#define COLOR_3_R 254
#define COLOR_3_G 0
#define COLOR_3_B 0
*/
// END - 4 colors definition //
Uint32 getpixel(SDL_Surface *surface,int x,int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
void putPixel(SDL_Surface * surface, Uint16 x, Uint16 y, Uint32 color)
{
/* Nombre de bits par pixels de la surface d'ecran */
Uint8 bpp = surface->format->BytesPerPixel;
/* Pointeur vers le pixel a remplacer (pitch correspond a la taille
d'une ligne d'ecran, c'est a dire (longueur * bitsParPixel)
pour la plupart des cas) */
Uint8 * p = ((Uint8 *)surface->pixels) + y * surface->pitch + x * bpp;
switch(bpp)
{
case 1:
*p = (Uint8) color;
break;
case 2:
*(Uint16 *)p = (Uint16) color;
break;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
*(Uint16 *)p = ((color >> 8) & 0xff00) | ((color >> 8) & 0xff);
*(p + 2) = color & 0xff;
}
else
{
*(Uint16 *)p = color & 0xffff;
*(p + 2) = ((color >> 16) & 0xff) ;
}
break;
case 4:
*(Uint32 *)p = color;
break;
}
}
int main(int argc, char * argv[])
{
SDL_Surface * screen;
// SDL_Surface * image, * tmp;
SDL_Rect blitrect = {0, 0, 0, 0};
int i, j;
int n, direction, couleur, new_couleur;
unsigned char r,g,b;
int nr, ng, nb;
if (SDL_Init(SDL_INIT_VIDEO) == -1)
{
printf("Erreur lors de l'initialisation de SDL: %s\n", SDL_GetError());
return 1;
}
#define SDL_VIDEO_FLAGS (SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT)
//screen = SDL_SetVideoMode(640, 480, 24, SDL_VIDEO_FLAGS);
screen = SDL_SetVideoMode(WIDTH, HEIGHT, 24, SDL_VIDEO_FLAGS);
// screen = SDL_SetVideoMode(270, 16, 24, SDL_VIDEO_FLAGS);
printf("Mode video: %dx%dx%d\n", screen->w, screen->h,
screen->format->BitsPerPixel);
//SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 254, 254, 254));
//i=320;
//j=240;
i = floor(WIDTH / 2);
j = floor(HEIGHT / 2);
direction=1;
//for (j = 0; j < screen->h; j++) {
//for (i = 0; i < screen->w; i++) {
// for (n = 0; n < 2000; n++) {
while (0!=1) {
//SDL_LockSurface(screen);
//r=0;
//GetPixelColor(screen,i,j,r,g,b);
//r=getpixel(screen,i,j);
SDL_GetRGB(getpixel(screen,i,j),screen->format,&r,&g,&b);
//printf("direction=%i - ",direction);
/* printf("n=%i - ",n);
printf("i=%i - ",i);
printf("j=%i - ",j);
printf("r=%i - ",r);
printf("g=%i - ",g);
printf("b=%i - ",b);
*/
//if ((r==254) && (g==254) && (b==254))
if ((r==COLOR_0_R) && (g==COLOR_0_G) && (b==COLOR_0_B))
couleur=0;
//else if (r==254 && g==0 && b==0)
else if (r==COLOR_1_R && g==COLOR_1_G && b==COLOR_1_B)
couleur=1;
//else if (r==0 && g==254 && b==0)
else if (r==COLOR_2_R && g==COLOR_2_G && b==COLOR_2_B)
couleur=2;
//else if (r==0 && g==0 && b==254)
else if (r==COLOR_3_R && g==COLOR_3_G && b==COLOR_3_B)
couleur=3;
// printf("couleur=%i - ",couleur);
//printf("direction=%i - ",direction);
// ----- Glider moving rules ----- //
/// /// /// /// /// /// /// /// /// /// /// /// /// ///
/// for different gliders ///
/// on each 4 following cases ///
/// change new_couleur from 0 to 3 ///
/// change direction from direction+0 to direction+3 ///
// /// /// /// /// /// /// /// /// /// /// /// /// ///
/// ///
/// examples: ///
/// ///
/// --- 1: geometric shape: ///
/// case 0: new_couleur=1 / direction=direction+3 ///
/// case 1: new_couleur=2 / direction=direction+1 ///
/// case 2: new_couleur=3 / direction=direction+1 ///
/// case 3: new_couleur=0 / direction=direction+3 ///
/// ///
/// --- 2: center fractal with up down left right arms ///
/// case 0: new_couleur=1 / direction=direction+3 ///
/// case 1: new_couleur=2 / direction=direction+1 ///
/// case 2: new_couleur=3 / direction=direction+1 ///
/// case 3: new_couleur=0 / direction=direction+3 ///
/// ///
/// --- 2: center fractal then canon ///
/// case 0: new_couleur=1 / direction=direction+2 ///
/// case 1: new_couleur=2 / direction=direction+3 ///
/// case 2: new_couleur=3 / direction=direction+2 ///
/// case 3: new_couleur=0 / direction=direction+1 ///
// /// /// /// /// /// /// /// /// /// /// /// /// ///
/// --- 1: geometric shape: ///
switch(couleur){
case 0:
new_couleur=1;
direction=direction+3;
break;
case 1:
new_couleur=2;
direction=direction+1;
break;
case 2:
new_couleur=3;
direction=direction+1;
break;
case 3:
new_couleur=0;
direction=direction+3;
break;
}
/// --- 2: center fractal with up down left right arms ///
/*
switch(couleur){
case 0:
new_couleur=1;
direction=direction+1;
break;
case 1:
new_couleur=2;
direction=direction+1;
break;
case 2:
new_couleur=3;
direction=direction+1;
break;
case 3:
new_couleur=0;
direction=direction+2;
break;
}
*/
/// --- 3: center fractal then canon ///
/*
switch(couleur){
case 0:
new_couleur=1;
direction=direction+2;
break;
case 1:
new_couleur=2;
direction=direction+3;
break;
case 2:
new_couleur=3;
direction=direction+2;
break;
case 3:
new_couleur=0;
direction=direction+1;
break;
}
*/
// END ----- Glider moving rules ----- //
direction = direction % 4;
// printf("direction=%i\n",direction);
nr=0;
ng=0;
nb=0;
//SDL_LockSurface(screen);
switch(new_couleur){
case 0:
//nr=254;
//ng=254;
//nb=254;
nr=COLOR_0_R;
ng=COLOR_0_G;
nb=COLOR_0_B;
break;
case 1:
//nr=254;
//ng=0;
//nb=0;
nr=COLOR_1_R;
ng=COLOR_1_G;
nb=COLOR_1_B;
break;
case 2:
//nr=0;
//ng=254;
//nb=0;
nr=COLOR_2_R;
ng=COLOR_2_G;
nb=COLOR_2_B;
break;
case 3:
//nr=0;
//ng=0;
//nb=254;
nr=COLOR_3_R;
ng=COLOR_3_G;
nb=COLOR_3_B;
break;
}
putPixel(screen, i, j, SDL_MapRGB(screen->format, nr, ng, nb));
//putPixel(screen, i, j, SDL_MapRGB(screen->format, 0xfe, 0, 0));
// BORDER BOUNCE //
switch(direction){
case 0:
j=j-1;
if (j<0)
//j=479;
j = 0;
break;
case 1:
i=i+1;
//if (i>639)
if ( i > ( WIDTH - 1 ) )
//i=0;
i = WIDTH -1;
break;
case 2:
j=j+1;
//if (j>479)
if ( j > ( HEIGHT - 1 ) )
//j=0;
j = HEIGHT -1;
break;
case 3:
i=i-1;
if (i<0)
//i=639;
//i = WIDTH - 1;
i = 0;
break;
direction = ( direction + 2 ) % 4;
}
// END - BORDER BOUNCE //
// BORDER PASS THROUGH //
/*
switch(direction){
case 0:
j=j-1;
if (j<0)
//j=479;
j = HEIGHT - 1;
break;
case 1:
i=i+1;
//if (i>639)
if ( i > ( WIDTH - 1 ) )
i=0;
break;
case 2:
j=j+1;
//if (j>479)
if ( j > ( HEIGHT - 1 ) )
j=0;
break;
case 3:
i=i-1;
if (i<0)
//i=639;
i = WIDTH - 1;
break;
}
*/
// END - BORDER PASS THROUGH //
//SDL_UnlockSurface(screen);
SDL_Flip(screen);
}
//}
//sleep(5);
SDL_Quit();
return 0;
}