Accueil > > > PROPA [SDL]
PROPA [SDL]
Information sur la source
Description
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...
Historique
- 12 mars 2006 00:22:06 :
- erreur de fichiers
Sources du même auteur
SERVEUR METEO, IHM EVOLUEE (VC6, CONSOLE WINDOWS, SOCK, THRE...SERVEUR METEO, IHM EVOLUEE (VC6, CONSOLE WINDOWS, SOCK, THREAD)Ceci est notre dernier projet de formation, on doit crée 4 applications, en 8*8h (64h pour faire ça).
Le serveur est a moitier fait, il manque la p...
Sources de la même categorie
Commentaires et avis
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
|
Derniers Blogs
TECHDAYS PARIS 2010 : SHAREPOINT 2010 POUR LES DéVELOPPEURSTECHDAYS PARIS 2010 : SHAREPOINT 2010 POUR LES DéVELOPPEURS par ROMELARD Fabrice
Animé par: Laurent Cotton Le développement dans SharePoint 2010 passe par plusieurs axes qui seront évoqués dans cette session, mais plus particulièrement les développements simples lié au besoin Business Business Connectivity Services Ce BCS es...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : PLEINIèRE DERNIER JOURTECHDAYS PARIS 2010 : PLEINIèRE DERNIER JOUR par ROMELARD Fabrice
Cette session est la dernière pleinière de ces 3 jours de TechDays Paris 2010. Généralement, cette troisième journée est plus axée sur l'avenir vu par Microsoft. Après un retour sur l'avenir vu par la Science Fiction ou par ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion
Forum
RE : WIN APIRE : WIN API par racpp
Cliquez pour lire la suite par racpp
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
Comparez les prix

HTC Hero
Entre 550€ et 550€
|