MMBC

colored square

Aug 3rd, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <SDL/SDL.h>
  4. #include <math.h>
  5.  
  6. typedef struct {
  7.     Uint8 r;
  8.     Uint8 g;
  9.     Uint8 b;
  10. } pixel;
  11.  
  12. void pause();
  13. void setPixel(SDL_Surface* screen, int x, int y, pixel* p);
  14.  
  15. int main(int argc, char *argv[]) {
  16.     SDL_Surface *ecran = NULL;
  17.  
  18.     if (SDL_Init(SDL_INIT_VIDEO) == -1) {
  19.         fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError());
  20.         exit(EXIT_FAILURE);
  21.     }
  22.  
  23.     ecran = SDL_SetVideoMode(512, 512, 32, SDL_HWSURFACE);
  24.     if (ecran == NULL) {
  25.         fprintf(stderr, "Erreur d'initialisation du mode vidéo : %s\n", SDL_GetError());
  26.         exit(EXIT_FAILURE);
  27.     }
  28.     SDL_WM_SetCaption("Test SDL", NULL);
  29.  
  30.     pixel pv1 = {255, 0, 0};
  31.     pixel pv2 = {0, 255, 0};
  32.     pixel pv3 = {0, 0, 255};
  33.     pixel pv4 = {255, 255, 255};
  34.     pixel p = {0, 0, 0};
  35.     float d1, d2, d3, w1, w2, w3;
  36.     for (int x = 0; x < 512; x++) {
  37.         for (int y = 0; y <= x; y++) {
  38.             w1 = (float)((-512)*(x-512))/(float)(pow(-512,2));
  39.             w2 = (float)(512*(x-512)+(-512)*(y-512))/(float)(pow(-512,2));
  40.             w3 = 1-w1-w2;
  41.             p.r = (float)((float)pv1.r*w1+(float)pv2.r*w2+(float)pv3.r*w3)/(w1+w2+w3);
  42.             p.g = (float)((float)pv1.g*w1+(float)pv2.g*w2+(float)pv3.g*w3)/(w1+w2+w3);
  43.             p.b = (float)((float)pv1.b*w1+(float)pv2.b*w2+(float)pv3.b*w3)/(w1+w2+w3);
  44.             //p.b = (int)((float)pv1.b*w1+(float)pv2.b*w2+(float)pv3.b*w3);
  45.             setPixel(ecran, x, y,&p);
  46.         }
  47.         for (int y = x; y < 512; y++) {
  48.             w1 = (float)(512*(y-512))/(float)(512*(-512));
  49.             w2 = (float)(512*(x-512)+(-512)*(y-512))/(float)(512*(-512));
  50.             w3 = 1-w1-w2;
  51.             p.r = (float)((float)pv1.r*w1+(float)pv4.r*w2+(float)pv3.r*w3)/(w1+w2+w3);
  52.             p.g = (float)((float)pv1.g*w1+(float)pv4.g*w2+(float)pv3.g*w3)/(w1+w2+w3);
  53.             p.b = (float)((float)pv1.b*w1+(float)pv4.b*w2+(float)pv3.b*w3)/(w1+w2+w3);
  54.             setPixel(ecran, x, y,&p);
  55.         }
  56.     }
  57.  
  58.     SDL_Flip(ecran);
  59.  
  60.     pause();
  61.  
  62.     SDL_Quit();
  63.     return EXIT_SUCCESS;
  64. }
  65.  
  66. void setPixel(SDL_Surface* screen, int x, int y, pixel* p) {
  67.     Uint32* p_screen = (Uint32*)screen->pixels;
  68.     p_screen += y* screen->w+x;
  69.     *p_screen = SDL_MapRGB(screen->format, p->r, p->g, p->b);
  70. }
  71.  
  72. void pause() {
  73.     int continuer = 1;
  74.     SDL_Event event;
  75.  
  76.     while(continuer) {
  77.         SDL_WaitEvent(&event);
  78.         switch(event.type) {
  79.         case SDL_QUIT:
  80.             continuer = 0;
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment