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
SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE !MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE ! par Vko
Hier durant une session dédiée aux Techdays 2012, j'ai eu le plaisir d'annoncer la sortie de la Béta 2 de Mishra Reader. C'est quoi ? Pour les utilisateurs, c'est une vraie expérience de lecture de flux RSS sur Windows. Rien à voir avec les produit...
Cliquez pour lire la suite de l'article par Vko
Logiciels
Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning
|