1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <SDL/SDL.h>//librairie graphique SDL
#include <SDL/SDL_ttf.h>//librairie graphique SDL_ttf permettant d'ecrire du texte
#include <gtk/gtk.h>// librairie graphique GTK toolkit
Uint32 coul;//pour la couleur
SDL_Surface* affichage;//pour le pixel
Uint32 SDL_MapRGB(SDL_PixelFormat *fmt, Uint8 r, Uint8 g, Uint8 b);
SDL_Event event;// variable permettant de gerer les evenements
//pour les couleurs en SDL
enum {
C_NOIR, C_BLEU_FONCE, C_VERT_FONCE, C_CYAN_FONCE, C_ROUGE_FONCE,
C_MAGENTA_FONCE, C_OCRE, C_GRIS_CLAIR, C_GRIS, C_BLEU, C_VERT,
C_CYAN, C_ROUGE, C_MAGENTA, C_JAUNE, C_BLANC,
NB_COULEURS
};
Uint32 couleurs[NB_COULEURS];
void initCouleurs(void)
{
couleurs[C_NOIR] = SDL_MapRGB(affichage->format, 0x00, 0x00, 0x00);
couleurs[C_BLEU_FONCE] = SDL_MapRGB(affichage->format, 0x00, 0x00, 0x80);
couleurs[C_VERT_FONCE] = SDL_MapRGB(affichage->format, 0x00, 0x80, 0x00);
couleurs[C_CYAN_FONCE] = SDL_MapRGB(affichage->format, 0x00, 0x80, 0x80);
couleurs[C_ROUGE_FONCE] = SDL_MapRGB(affichage->format, 0x80, 0x00, 0x00);
couleurs[C_MAGENTA_FONCE] = SDL_MapRGB(affichage->format, 0x80, 0x00, 0x80);
couleurs[C_OCRE] = SDL_MapRGB(affichage->format, 0x80, 0x80, 0x00);
couleurs[C_GRIS_CLAIR] = SDL_MapRGB(affichage->format, 0xC0, 0xC0, 0xC0);
couleurs[C_GRIS] = SDL_MapRGB(affichage->format, 0x80, 0x80, 0x80);
couleurs[C_BLEU] = SDL_MapRGB(affichage->format, 0x00, 0x00, 0xFF);
couleurs[C_VERT] = SDL_MapRGB(affichage->format, 0x00, 0xFF, 0x00);
couleurs[C_CYAN] = SDL_MapRGB(affichage->format, 0x00, 0xFF, 0xFF);
couleurs[C_ROUGE] = SDL_MapRGB(affichage->format, 0xFF, 0x00, 0x00);
couleurs[C_MAGENTA] = SDL_MapRGB(affichage->format, 0xFF, 0x00, 0xFF);
couleurs[C_JAUNE] = SDL_MapRGB(affichage->format, 0xFF, 0xFF, 0x00);
couleurs[C_BLANC] = SDL_MapRGB(affichage->format, 0xFF, 0xFF, 0xFF);
}
/************************Fonction permettant d'afficher un pixel*********************************/
void setPixel(int x, int y, Uint32 coul)
{
*((Uint32*)(affichage->pixels) + x + y * affichage->w) = coul;
}
/**********************************************************************************************/
/*************************Fonction permettant d'ecrire un pixel securise************************/
void setPixelVerif(int x, int y, Uint32 coul)
{
if (x >= 0 && x < affichage->w &&
y >= 0 && y < affichage->h)
setPixel(x, y, coul);
}
/*********************************************************************************************/
/***********************Fonction permettant de tracer un cercle******************************/
void cercle(int cx, int cy, int rayon, int coul)
{
int d, y, x;
d = 3 - (2 * rayon);
x = 0;
y = rayon;
while (y >= x) {
setPixelVerif(cx + x, cy + y, coul);
setPixelVerif(cx + y, cy + x, coul);
setPixelVerif(cx - x, cy + y, coul);
setPixelVerif(cx - y, cy + x, coul);
setPixelVerif(cx + x, cy - y, coul);
setPixelVerif(cx + y, cy - x, coul);
setPixelVerif(cx - x, cy - y, coul);
setPixelVerif(cx - y, cy - x, coul);
if (d < 0)
d = d + (4 * x) + 6;
else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}
/**********************************************************************************************/
int main(int argc, char *argv[])
{
SDL_Surface *ecran = NULL;
int continuer = 1;
SDL_Init(SDL_INIT_VIDEO);
initCouleurs() ;//initialise le tableau de couleurs
SDL_WM_SetIcon(SDL_LoadBMP("sdl_icone.bmp"), NULL);// Chargement de l'icône AVANT SDL_SetVideoMode
ecran=SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE); // Ouverture de la fenêtre
cercle(400,300,100,couleurs[C_JAUNE]);
while (continuer != 0)
{
SDL_WaitEvent(&event);//recuperation de l'evenement dans event
switch(event.type)//test du type d'evenement
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN: switch (event.key.keysym.sym)
{
case SDLK_ESCAPE://appui sur la touche escape ,quitte le programme
continuer = 0;
break;
}
break;
}
SDL_Flip(ecran);
}
return(0);
} |
Partager