begin process at 2012 02 12 06:49:00
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Maths & Algorithmes

 > PROPA [SDL]

PROPA [SDL]


 Information sur la source

Note :
Aucune note
Catégorie :Maths & Algorithmes Classé sous :propagation, sdl, pile, algorithme Niveau :Initié Date de création :12/03/2006 Date de mise à jour :12/03/2006 00:22:06 Vu / téléchargé :2 831 / 368

Auteur : aerith

Ecrire un message privé
Site perso
Commentaire sur cette source (10)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
C'est un algorithme de propagation, un peut comme les virus a la télé.

On peut dire qu'il simule du récursif multitache en utilisant une pile.

Le rendu est simpat, l'algo aussi...

Source

  • //##############################################################################
  • //# #
  • //# fichier : propa.c version : V1.0 #
  • //# projet : propa date : 26/11/2005 #
  • //# par : aerith #
  • //# #
  • //# algo de propagation #
  • //# #
  • //##############################################################################
  • #include <string.h>
  • #include <SDL\SDL.h>
  • #include <stdio.h>
  • #include <stdlib.h>
  • #include "SDL text.h"
  • #ifdef WIN32
  • #include <windows.h>
  • #include <winbase.h>
  • #include <time.h>
  • #else
  • #include <unistd.h>
  • #include <sys/types.h>
  • #include <arpa/inet.h>
  • #include <sys/time.h>
  • #endif
  • #define WIDTH 800
  • #define HEIGHT 600
  • #define NBWALL (WIDTH * HEIGHT) / 3
  • #define NBBOOST 0//(WIDTH * HEIGHT) / 5
  • #define BLOCK 1
  • typedef struct _vecteur
  • {
  • int x;
  • int y;
  • } vecteur;
  • SDL_Surface *Screen;
  • char Matrice[WIDTH][HEIGHT];
  • int NbThread;
  • int MaxThread;
  • int MinThread;
  • int NoThread;
  • int NbBlock;
  • vecteur Stack[WIDTH * HEIGHT];
  • rect Zone;
  • color Top, Back;
  • char Text[1024];
  • void TempWait(int iDelai);
  • void Propa(vecteur *vPt);
  • void Point(int x, int y, int r, int g, int b);
  • void Block(int iX0, int iY0, int r, int g, int b);
  • void AddThread(vecteur *vPt);
  • int main(int argc, char **argv)
  • {
  • int i, nb;
  • vecteur pt;
  • Uint32 Color;
  • clock_t start;
  • SDL_Event event;
  • int done = 0;
  • srand(time(NULL));
  • SDL_Init(SDL_INIT_VIDEO);
  • atexit(SDL_Quit);
  • Screen = SDL_SetVideoMode(WIDTH * BLOCK, HEIGHT * BLOCK, 16, SDL_SWSURFACE|SDL_DOUBLEBUF);
  • Zone.x = 1;
  • Zone.y = 1;
  • Zone.l = Screen->w;
  • Zone.h = Screen->h;
  • Top.r = 255;
  • Top.g = 255;
  • Top.b = 255;
  • Back.r = 0;
  • Back.g = 0;
  • Back.b = 0;
  • for(pt.y = 0; pt.y < HEIGHT; pt.y++)
  • for(pt.x = 0; pt.x < WIDTH; pt.x++)
  • Matrice[pt.x][pt.y] = 0;
  • for(i = 0; i < NBWALL;)
  • {
  • pt.x = (int)(rand() * ((float)WIDTH / RAND_MAX));
  • pt.y = (int)(rand() * ((float)HEIGHT / RAND_MAX));
  • if(!Matrice[pt.x][pt.y])
  • {
  • Matrice[pt.x][pt.y] = 1;
  • i++;
  • if(BLOCK > 1)
  • Block(pt.x, pt.y, 255, 0, 0);
  • else
  • Point(pt.x, pt.y, 255, 0, 0);
  • }
  • }
  • for(i = 0; i < NBBOOST;)
  • {
  • pt.x = (int)(rand() * ((float)WIDTH / RAND_MAX));
  • pt.y = (int)(rand() * ((float)HEIGHT / RAND_MAX));
  • if(Matrice[pt.x][pt.y] != 1)
  • {
  • Matrice[pt.x][pt.y] = 2;
  • i++;
  • if(BLOCK > 1)
  • Block(pt.x, pt.y, 0, 255, 0);
  • else
  • Point(pt.x, pt.y, 0, 255, 0);
  • }
  • }
  • do
  • {
  • pt.x = (int)(rand() * ((float)WIDTH / RAND_MAX));
  • pt.y = (int)(rand() * ((float)HEIGHT / RAND_MAX));
  • }
  • while(Matrice[pt.x][pt.y] != 0);
  • NbThread = 0;
  • MaxThread = 0;
  • NoThread = 0;
  • MinThread = 1;
  • NbBlock = 0;
  • start = clock();
  • Propa(&pt);
  • do
  • {
  • nb = NbThread;
  • if(NbThread > MaxThread)
  • MaxThread = NbThread;
  • NbThread = 0;
  • if(SDL_MUSTLOCK(Screen))
  • if(SDL_LockSurface(Screen) < 0)
  • return 0;
  • for(i = 0; i < nb; i++)
  • Propa(&Stack[MinThread + i]);
  • MinThread += i;
  • sprintf(Text, "Thread:%i (%i) \nCases :%i\nTime :%i ms", NbThread, MaxThread, NbBlock, clock() - start);
  • SDLText(Screen, Text, Zone, 2, Top, Back);
  • TempWait(1);
  • if(SDL_MUSTLOCK(Screen))
  • SDL_UnlockSurface(Screen);
  • SDL_Flip(Screen);
  • }
  • while(nb);
  • while(!done)
  • {
  • TempWait(10);
  • while(SDL_PollEvent(&event))
  • {
  • if(event.type == SDL_QUIT)
  • done = 1;
  • }
  • }
  • return 0;
  • }
  • void Propa(vecteur *vPt)
  • {
  • vecteur pt;
  • if(Matrice[vPt->x][vPt->y] != 1)
  • {
  • Matrice[vPt->x][vPt->y] = 1;
  • if(BLOCK > 1)
  • Block(vPt->x, vPt->y, 0, 0, 255);
  • else
  • Point(vPt->x, vPt->y, 0, 0, 255);
  • NbBlock++;
  • }
  • pt.y = vPt->y - 1;
  • pt.x = vPt->x;
  • AddThread(&pt);
  • if(Matrice[pt.x][pt.y] == 2)
  • pt.y = vPt->y - 2;
  • AddThread(&pt);
  • pt.y = vPt->y + 1;
  • AddThread(&pt);
  • if(Matrice[pt.x][pt.y] == 2)
  • pt.y = vPt->y + 2;
  • AddThread(&pt);
  • pt.y = vPt->y;
  • pt.x = vPt->x + 1;
  • AddThread(&pt);
  • if(Matrice[pt.x][pt.y] == 2)
  • pt.x = vPt->x + 2;
  • AddThread(&pt);
  • pt.x = vPt->x - 1;
  • AddThread(&pt);
  • if(Matrice[pt.x][pt.y] == 2)
  • pt.x = vPt->x - 2;
  • AddThread(&pt);
  • }
  • void AddThread(vecteur *vPt)
  • {
  • int i;
  • if((vPt->x >= 0) && (vPt->x < WIDTH) && (vPt->y >= 0) && (vPt->y < HEIGHT))
  • {
  • if(Matrice[vPt->x][vPt->y] != 1)
  • {
  • for(i = MinThread; i <= NoThread; i++)
  • if((Stack[i].x == vPt->x) && (Stack[i].y == vPt->y))
  • return;
  • NoThread++;
  • NbThread++;
  • Stack[NoThread].x = vPt->x;
  • Stack[NoThread].y = vPt->y;
  • }
  • }
  • }
  • void Point(int x, int y, int r, int g, int b)
  • {
  • Uint16 *pPxl;
  • pPxl = (Uint16*)Screen->pixels + ((Screen->pitch / 2 * y) + x);
  • *pPxl = SDL_MapRGB(Screen->format, (Uint8)r, (Uint8)g, (Uint8)b);
  • }
  • void Block(int iX0, int iY0, int r, int g, int b)
  • {
  • int x, y, px, py;
  • px = iX0 * BLOCK;
  • py = iY0 * BLOCK;
  • for(y = 0; y < BLOCK; y++)
  • for(x = 0; x < BLOCK; x++)
  • Point(px + x, py + y, r, g, b);
  • }
  • void TempWait(int iDelai)
  • {
  • #ifdef WIN32
  • Sleep(iDelai);
  • #else
  • usleep(iDelai * 1000);
  • #endif
  • }
//##############################################################################
//#																			   #
//# fichier	: propa.c									version : V1.0		   #
//# projet	: propa										date :	26/11/2005	   #
//# par		: aerith														   #
//#																			   #
//# algo de propagation														   #
//#																			   #
//##############################################################################

#include	<string.h>
#include	<SDL\SDL.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	"SDL text.h"

#ifdef WIN32
	#include	<windows.h>
	#include	<winbase.h>
	#include 	<time.h>
#else
	#include 	<unistd.h>
	#include 	<sys/types.h>
	#include 	<arpa/inet.h>
	#include 	<sys/time.h>
#endif

#define		WIDTH	800
#define		HEIGHT	600
#define		NBWALL	(WIDTH * HEIGHT) / 3
#define		NBBOOST	0//(WIDTH * HEIGHT) / 5
#define		BLOCK  1

typedef struct _vecteur
{
	int		x;
	int		y;
} vecteur;

SDL_Surface *Screen;
char		Matrice[WIDTH][HEIGHT];
int			NbThread;
int			MaxThread;
int			MinThread;
int			NoThread;
int			NbBlock;
vecteur		Stack[WIDTH * HEIGHT];
rect		Zone;
color		Top, Back;
char		Text[1024];

void	TempWait(int iDelai);
void	Propa(vecteur *vPt);
void	Point(int x, int y, int r, int g, int b);
void	Block(int iX0, int iY0, int r, int g, int b);
void	AddThread(vecteur *vPt);

int main(int argc, char **argv)
{
	int		i, nb;
	vecteur	pt;
	Uint32	Color;
	clock_t start;
	SDL_Event 	event;
	int		done = 0;

	srand(time(NULL));

	SDL_Init(SDL_INIT_VIDEO);
	atexit(SDL_Quit);

	Screen = SDL_SetVideoMode(WIDTH * BLOCK, HEIGHT * BLOCK, 16, SDL_SWSURFACE|SDL_DOUBLEBUF);

	Zone.x = 1;
	Zone.y = 1;
	Zone.l = Screen->w;
	Zone.h = Screen->h;
	Top.r = 255;
	Top.g = 255;
	Top.b = 255;
	Back.r = 0;
	Back.g = 0;
	Back.b = 0;

	for(pt.y = 0; pt.y < HEIGHT; pt.y++)
		for(pt.x = 0; pt.x < WIDTH; pt.x++)
			Matrice[pt.x][pt.y] = 0;

	for(i = 0; i < NBWALL;)
	{
		pt.x = (int)(rand() * ((float)WIDTH / RAND_MAX));
		pt.y = (int)(rand() * ((float)HEIGHT / RAND_MAX));

		if(!Matrice[pt.x][pt.y])
		{
			Matrice[pt.x][pt.y] = 1;
			i++;
			if(BLOCK > 1)
				Block(pt.x, pt.y, 255, 0, 0);
			else
				Point(pt.x, pt.y, 255, 0, 0);
		}
	}
	for(i = 0; i < NBBOOST;)
	{
		pt.x = (int)(rand() * ((float)WIDTH / RAND_MAX));
		pt.y = (int)(rand() * ((float)HEIGHT / RAND_MAX));

		if(Matrice[pt.x][pt.y] != 1)
		{
			Matrice[pt.x][pt.y] = 2;
			i++;
			if(BLOCK > 1)
				Block(pt.x, pt.y, 0, 255, 0);
			else
				Point(pt.x, pt.y, 0, 255, 0);
		}
	}

	do
	{
		pt.x = (int)(rand() * ((float)WIDTH / RAND_MAX));
		pt.y = (int)(rand() * ((float)HEIGHT / RAND_MAX));
	}
	while(Matrice[pt.x][pt.y] != 0);

	NbThread = 0;
	MaxThread = 0;
	NoThread = 0;
	MinThread = 1;
	NbBlock = 0;

	start = clock();
	Propa(&pt);

	do
	{
		nb = NbThread;
		if(NbThread > MaxThread)
			MaxThread = NbThread;
		NbThread = 0;

		if(SDL_MUSTLOCK(Screen))
			if(SDL_LockSurface(Screen) < 0)
				return 0;

		for(i = 0; i < nb; i++)
			Propa(&Stack[MinThread + i]);
		MinThread += i;

		sprintf(Text, "Thread:%i (%i) \nCases :%i\nTime  :%i ms", NbThread, MaxThread, NbBlock, clock() - start);
		SDLText(Screen, Text, Zone, 2, Top, Back);
		TempWait(1);

		if(SDL_MUSTLOCK(Screen))
			SDL_UnlockSurface(Screen);
		SDL_Flip(Screen);
	}
	while(nb);

	while(!done)
	{
		TempWait(10);

		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
				done = 1;
		}

	}

	return 0;
}

void	Propa(vecteur *vPt)
{
	vecteur	pt;

	if(Matrice[vPt->x][vPt->y] != 1)
	{
		Matrice[vPt->x][vPt->y] = 1;
		if(BLOCK > 1)
			Block(vPt->x, vPt->y, 0, 0, 255);
		else
			Point(vPt->x, vPt->y, 0, 0, 255);
		NbBlock++;
	}

	pt.y = vPt->y - 1;
	pt.x = vPt->x;
	AddThread(&pt);
	if(Matrice[pt.x][pt.y] == 2)
		pt.y = vPt->y - 2;
	AddThread(&pt);

	pt.y = vPt->y + 1;
	AddThread(&pt);
	if(Matrice[pt.x][pt.y] == 2)
		pt.y = vPt->y + 2;
	AddThread(&pt);

	pt.y = vPt->y;
	pt.x = vPt->x + 1;
	AddThread(&pt);
	if(Matrice[pt.x][pt.y] == 2)
		pt.x = vPt->x + 2;
	AddThread(&pt);

	pt.x = vPt->x - 1;
	AddThread(&pt);
	if(Matrice[pt.x][pt.y] == 2)
		pt.x = vPt->x - 2;
	AddThread(&pt);
}

void	AddThread(vecteur *vPt)
{
	int		i;

	if((vPt->x >= 0) && (vPt->x < WIDTH) && (vPt->y >= 0) && (vPt->y < HEIGHT))
	{
		if(Matrice[vPt->x][vPt->y] != 1)
		{
			for(i = MinThread; i <= NoThread; i++)
				if((Stack[i].x == vPt->x) && (Stack[i].y == vPt->y))
					return;

			NoThread++;
			NbThread++;
			Stack[NoThread].x = vPt->x;
			Stack[NoThread].y = vPt->y;
		}
	}
}

void	Point(int x, int y, int r, int g, int b)
{
	Uint16	*pPxl;

	pPxl = (Uint16*)Screen->pixels + ((Screen->pitch / 2 * y) + x);
	*pPxl = SDL_MapRGB(Screen->format, (Uint8)r, (Uint8)g, (Uint8)b);
}

void	Block(int iX0, int iY0, int r, int g, int b)
{
	int  	x, y, px, py;

	px = iX0 * BLOCK;
	py = iY0 * BLOCK;

	for(y = 0; y < BLOCK; y++)
		for(x = 0; x < BLOCK; x++)
			Point(px + x, py + y, r, g, b);
}

void	TempWait(int iDelai)
{
#ifdef WIN32
	Sleep(iDelai);
#else
	usleep(iDelai * 1000);
#endif
}

 Conclusion

On peut chager quelque paramètre dans les defines.

Il ne faut pas toucher a la fenètre pendant le calcul, sinon vous perder l'affichage.

Le screen est a chier car on ne peut pas fait de capture d'écran, donc photo...

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !
  •   bin
    • propa.exeTélécharger ce fichier [Réservé aux membres club]120 199 octets
    • SDL.dllTélécharger ce fichier [Réservé aux membres club]237 568 octets
  • makefileTélécharger ce fichier [Réservé aux membres club]354 octets
  • Makefile.winTélécharger ce fichier [Réservé aux membres club]848 octets
  • propa.cTélécharger ce fichier [Réservé aux membres club]Voir ce fichier5 180 octets
  • propa.devTélécharger ce fichier [Réservé aux membres club]1 107 octets
  • propa.layoutTélécharger ce fichier [Réservé aux membres club]272 octets
  • propa.oTélécharger ce fichier [Réservé aux membres club]5 158 octets
  • SDL text.cTélécharger ce fichier [Réservé aux membres club]Voir ce fichier27 413 octets
  • SDL text.hTélécharger ce fichier [Réservé aux membres club]Voir ce fichier1 256 octets
  • SDL text.oTélécharger ce fichier [Réservé aux membres club]35 518 octets

Télécharger le zip


 Historique

12 mars 2006 00:22:06 :
erreur de fichiers

 Sources du même auteur

Source avec Zip Source avec une capture TEXT SDL
Source avec Zip Source avec une capture SERVEUR METEO, IHM EVOLUEE (VC6, CONSOLE WINDOWS, SOCK, THRE...
SUITE LOGIQUE (PORTABLE)
Source avec Zip Source avec une capture FIBO, CALCULE DE LA SUITE DE FIBONACCI AVEC SUPORT DES GRAND...
Source avec Zip Source avec une capture GESTION ACCESS, PROJET FORMATION, CONSOLE WINDOWS

 Sources de la même categorie

Source avec Zip UN EXAMPLE D'APPLICATION EN CUDA DE L'ALGORITHME DE SCAN POU... par oguzaras
Source avec Zip Source avec une capture CHIFFREMENT DE VIGENERE par lajouad
Source avec Zip Source avec une capture ANALYSE SYNTAXIQUE par lajouad
Source avec Zip Source avec une capture STRUCTURE D'UNE MATRICE PAR LES LISTE LINÉAIRE (NON CONTUGUS... par benzarabel
Source avec Zip Source avec une capture DESSINER UNE ARBRE BINAIRE( MODE CONSOLE): par benzarabel

 Sources en rapport avec celle ci

Source avec Zip UN EXAMPLE D'APPLICATION EN CUDA DE L'ALGORITHME DE SCAN POU... par oguzaras
Source avec Zip Source avec une capture JEUX SERPENT par antho974
Source avec Zip Source avec une capture PENDU EN SDL par Damsou91
Source avec Zip Source avec une capture FRACTALES DE JULIA ET MANDELBROT EN SDL par patarotalexandre
GESTION D'UNE PILE PAR LES CLASSES par UKR6900

Commentaires et avis

Commentaire de mogwai93 le 12/03/2006 10:01:25

avec Devcpp 4.9.9.2 :
à la compilation :

  [Linker error] undefined reference to `__gxx_personality_v0'
  ld returned 1 exit status
C:\cppfrance_source_36504\Makefile.win [Build Error]  [bin/propa.exe] Error 1

comment corriger ?
merci

Commentaire de MuPuF le 12/03/2006 14:28:51

bien des matheux ça lol, bac + 100 et pas capable de faire une capture d'ecran pour du direct x (coup de gueulle fini c'était pas contre toi mais c'est un ras le bol des matheux qui font des maths juste pour faire des maths sans but utile ou clair)

Now je m'excuse et pour me faire pardonner, je te dis comment faire:
2 solutions, soit tu desactive l'acceleration matérielle de la carte video et la touche impr ecran sera ton amis
soit tu utilise un soft comme Fraps qui gere ça.

C'est jolie lol sinon, j'ai pas SDL domage ... Mais pourquoi ne pas utiliser un simple random ?

Commentaire de aerith le 12/03/2006 21:39:10

Hum, c'est quoi le rapport avec les math ?
ET SDL c'est baser sur OpenGL...

Commentaire de aerith le 12/03/2006 21:43:14

MOGWAI93 : Il faut installer les lib de SDL...

MUPUF : Il y a le dll de SDL...

Commentaire de mogwai93 le 13/03/2006 08:37:42

a AERITH : j'avait installé les lib de SDL  (enfin le package que Devcpp me proposait)

Commentaire de le_duche le 13/03/2006 13:59:17

Moi j'aime bien programmer des math ^^'
( je sais je suis un matheux ascendant info :p )

Commentaire de MuPuF le 13/03/2006 21:16:36

je m'excuse encore lol, bon, moi je veux bien tester mais cpp france supprime les exe lors du dl.
Si tu pouvais rennomer l'exe en .ex_ par exemple ce serais super gentil !
moi je suis info ascendant moyen math lol.
Pour le rapport avec les maths, j'ai pas du comprendre ce que tu faisais alors ;-)

Commentaire de le_duche le 13/03/2006 23:59:08

Ben je suis en bac+3 math, et j'addore programmer des trucs de math qui ne serviront jamais à personne ^^

Commentaire de MuPuF le 14/03/2006 20:33:36

ah ah mon detecteur de matheux a super fonctionné lol ;-)

Commentaire de le_duche le 16/03/2006 11:27:53

Pas tant que ca, si tu regarde bien, ce n'est pas moi qui ait posté la source ^^

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Algorithme de compression STAR amélioré [ par hi_vivie2 ] Bonjour à tous,Je dois réaliser de manière urgente l'implémentation en java de l'algorithme de compression STAR amélioré appliqué aux images en mouvem Algorithme de compression STAR amélioré [ par hi_vivie2 ] Bonjour à tous,Je dois réaliser de manière urgente l'implémentation en java de l'algorithme de compression STAR amélioré appliqué aux images en mouvem un programme à creer [ par yoyo ] je dois creer un programme permettant d trouver les nombres premiers.l'algorithme est donné, et il utilise des tableaux dont les cases sont remplies p problème SDL [ par Synhok ] J'ai essayer le tut sur la librairie SDL et quand je compile avec DevC++, le linker me marque :C:\...\BIN\ld.exe: cannot open -lSDLmain: No such file Texte & SDL en mode Video [ par Gaelle ] Bonjour à tous,Je suis en train d'interfacer une application C à l'aide de SDL. Pour ce qui est de la gestion vidéo et évènements souris aucun problèm pile memoire avec structure de pointeur [ par Nonobis ] slttjs avec ma calculatrice des p'titsproblemes ...ils faut que je recupere les valeurs saisies que cela soit nombre ou signe et les stock dans la pil Qui sait l'algorithme pour calculer les racines? [ par TMT ] Aidez-moi! librairie SDL et coonio.h [ par gloom ] salut peuple svp qq1 opurrait t'il m'aider voila je cherche a afficher une photo sous console DOS pour cela je sait qu'il ¸faute une librairie SDL mai [C++] Optimisation de pile [ par guiguikun ] conversion de la partie fractionnaire en base n [ par Alucard ] J'ai vu qu'il y avait beaucoup d'algorithme de la partie entière (int) d'un nombre en n'importe quel base mais je voulais savoir si quelqu'un avait un


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 5,460 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales