Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <SDL/SDL.h>
- #include <math.h>
- typedef struct {
- Uint8 r;
- Uint8 g;
- Uint8 b;
- } pixel;
- void pause();
- void setPixel(SDL_Surface* screen, int x, int y, pixel* p);
- int main(int argc, char *argv[]) {
- SDL_Surface *ecran = NULL;
- if (SDL_Init(SDL_INIT_VIDEO) == -1) {
- fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError());
- exit(EXIT_FAILURE);
- }
- ecran = SDL_SetVideoMode(512, 512, 32, SDL_HWSURFACE);
- if (ecran == NULL) {
- fprintf(stderr, "Erreur d'initialisation du mode vidéo : %s\n", SDL_GetError());
- exit(EXIT_FAILURE);
- }
- SDL_WM_SetCaption("Test SDL", NULL);
- pixel pv1 = {255, 0, 0};
- pixel pv2 = {0, 255, 0};
- pixel pv3 = {0, 0, 255};
- pixel pv4 = {255, 255, 255};
- pixel p = {0, 0, 0};
- float d1, d2, d3, w1, w2, w3;
- for (int x = 0; x < 512; x++) {
- for (int y = 0; y <= x; y++) {
- w1 = (float)((-512)*(x-512))/(float)(pow(-512,2));
- w2 = (float)(512*(x-512)+(-512)*(y-512))/(float)(pow(-512,2));
- w3 = 1-w1-w2;
- p.r = (float)((float)pv1.r*w1+(float)pv2.r*w2+(float)pv3.r*w3)/(w1+w2+w3);
- p.g = (float)((float)pv1.g*w1+(float)pv2.g*w2+(float)pv3.g*w3)/(w1+w2+w3);
- p.b = (float)((float)pv1.b*w1+(float)pv2.b*w2+(float)pv3.b*w3)/(w1+w2+w3);
- //p.b = (int)((float)pv1.b*w1+(float)pv2.b*w2+(float)pv3.b*w3);
- setPixel(ecran, x, y,&p);
- }
- for (int y = x; y < 512; y++) {
- w1 = (float)(512*(y-512))/(float)(512*(-512));
- w2 = (float)(512*(x-512)+(-512)*(y-512))/(float)(512*(-512));
- w3 = 1-w1-w2;
- p.r = (float)((float)pv1.r*w1+(float)pv4.r*w2+(float)pv3.r*w3)/(w1+w2+w3);
- p.g = (float)((float)pv1.g*w1+(float)pv4.g*w2+(float)pv3.g*w3)/(w1+w2+w3);
- p.b = (float)((float)pv1.b*w1+(float)pv4.b*w2+(float)pv3.b*w3)/(w1+w2+w3);
- setPixel(ecran, x, y,&p);
- }
- }
- SDL_Flip(ecran);
- pause();
- SDL_Quit();
- return EXIT_SUCCESS;
- }
- void setPixel(SDL_Surface* screen, int x, int y, pixel* p) {
- Uint32* p_screen = (Uint32*)screen->pixels;
- p_screen += y* screen->w+x;
- *p_screen = SDL_MapRGB(screen->format, p->r, p->g, p->b);
- }
- void pause() {
- int continuer = 1;
- SDL_Event event;
- while(continuer) {
- SDL_WaitEvent(&event);
- switch(event.type) {
- case SDL_QUIT:
- continuer = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment