SDL_Tests

Exemplos simples para o uso da libreria grafica SDL
Log | Files | Refs | README

debuxar.c (1707B)


      1 #include <stdio.h>
      2 #include <SDL2/SDL.h>
      3 #define MIN(A,B) ((A>B)?A:B)
      4 #define DIMX 400 
      5 #define DIMY 300
      6 
      7 SDL_Renderer* rend;
      8 SDL_Window* win;
      9 SDL_Surface* s;
     10 
     11 void debuxar_rectangulo(SDL_Surface* s, SDL_Point p1, SDL_Point p2, uint8_t cor){
     12     // p1 (esquina superior esquerda) p2 (esquina inferior dereita)
     13     uint8_t* offscreen = (uint8_t*)s->pixels;
     14     offscreen += (s->pitch*p1.y); // Situamolo na primeira fila a debuxar
     15     for (int i = p1.y; i<=MIN(s->h, p2.y); i++){
     16         for (int j = p1.x; j<=MIN(s->w, p2.x); j++){
     17             offscreen[j] = cor;
     18         }
     19         offscreen += s->pitch;
     20     }
     21 }
     22 
     23 int eventos(){
     24     SDL_Event e;
     25     while (SDL_PollEvent(&e) != 0) {
     26         switch (e.type) {
     27             case SDL_QUIT:
     28                 return 1;
     29         }
     30     }
     31     return 0;
     32 }
     33 
     34 int debuxar(){
     35     SDL_RenderClear(rend);
     36     SDL_Point p1 = {.x = 0, .y = 0}; 
     37     SDL_Point p2 = {.x = 20, .y = 20};
     38     debuxar_rectangulo(s, p1, p2, 1);
     39     SDL_Texture* tex = SDL_CreateTextureFromSurface(rend, s);
     40     SDL_Rect dst = {.x = 0, .y = 0, .w = 20, .h = 20};
     41     SDL_RenderCopy(rend, tex, NULL, &dst);
     42     SDL_DestroyTexture(tex);
     43 
     44     SDL_RenderPresent(rend);
     45     return 0;
     46 }
     47 
     48 
     49 int main(){
     50     SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO);
     51     win = SDL_CreateWindow("Debuxar",
     52         SDL_WINDOWPOS_CENTERED,
     53         SDL_WINDOWPOS_CENTERED,
     54         400, 300, 0);
     55 
     56     //s = SDL_CreateRGBSurface(0, 20, 20, 32, 0, 0, 0, 255);
     57     
     58     Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
     59     rend = SDL_CreateRenderer(win, -1, render_flags);
     60 
     61     for(;;) {
     62         if ( eventos() == 1 )
     63             break;
     64         debuxar();
     65     }
     66     SDL_Quit();
     67     return 0;
     68 }