SDL_Tests

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

debuxar2.c (1817B)


      1 #include <stdio.h>
      2 #include <SDL2/SDL.h>
      3 #define DIMX 400 
      4 #define DIMY 300
      5 
      6 int eventos(){
      7     SDL_Event e;
      8     while (SDL_PollEvent(&e) != 0) {
      9         switch (e.type) {
     10             case SDL_QUIT:
     11                 return 1;
     12         }
     13     }
     14     return 0;
     15 }
     16 
     17 void memset32( void* dest, Uint32 value, uintptr_t size ) {
     18     uintptr_t i;
     19     for( i = 0; i < (size & (~3)); i+=4 )
     20         memcpy( ((char*)dest) + i, &value, 4 );
     21 }
     22 
     23 int main(){
     24     SDL_Renderer* rend;
     25     SDL_Window* win;
     26     SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO);
     27     win = SDL_CreateWindow("Debuxar",
     28         SDL_WINDOWPOS_CENTERED,
     29         SDL_WINDOWPOS_CENTERED,
     30         DIMX, DIMY, 0);
     31     
     32     Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
     33     rend = SDL_CreateRenderer(win, -1, render_flags);
     34     SDL_Texture* tex = SDL_CreateTexture(rend, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, DIMX, DIMY);   
     35     uint32_t* pixels = malloc(DIMX * DIMY * sizeof(Uint32));
     36     Uint32 valor = 0x00ffffff; 
     37     memset32(pixels, valor, DIMX * DIMY * sizeof(Uint32));
     38     SDL_UpdateTexture(tex, NULL, pixels, DIMX * sizeof(Uint32));
     39     for(;;) {
     40         if ( eventos() == 1 ) break;
     41         SDL_RenderClear(rend);
     42         SDL_RenderCopy(rend, tex, NULL, NULL);
     43         valor -= 0x00000111;
     44         valor &= 0x00ffffff;
     45         //printf("Valor : %x\n", valor);
     46         //memset(pixels, valor, 640 * 480 * sizeof(uint32_t));
     47         memset32(pixels, valor, DIMX * DIMY * sizeof(Uint32));
     48         SDL_UpdateTexture(tex, NULL, pixels, DIMX * sizeof(Uint32));
     49         //SDL_UpdateTexture(tex, NULL, pixels, 640 * sizeof(uint32_t));
     50         SDL_RenderPresent(rend);
     51     }
     52     free(pixels);
     53     SDL_DestroyTexture(tex);
     54     SDL_DestroyRenderer(rend);
     55     SDL_DestroyWindow(win);
     56     SDL_Quit();
     57     return 0;
     58 }