debuxar4.c (1496B)
1 /* 2 * Debuxar sen render, modificando a superficie da xanela 3 * Autor: G. Xoel Otero 4 * zlib license 5 */ 6 7 #include <SDL2/SDL.h> 8 #include <stdio.h> 9 #define DIMX 400 10 #define DIMY 300 11 12 // Igual que memset pero copia 4 bytes de value tantas veces como 13 // indique size na direccion de memoria indicada polo punteiro dest 14 // A funcion orixinal de memset so copia 1 byte x veces nunha direccion 15 void memset32( void * dest, Uint32 value, uintptr_t size ) { 16 uintptr_t i; 17 for( i = 0; i < (size & (~3)); i+=4 ) 18 memcpy( ((char*)dest) + i, &value, 4 ); 19 } 20 21 int eventos() { 22 SDL_Event e; 23 while (SDL_PollEvent(&e) != 0) { 24 switch (e.type) { 25 case SDL_QUIT: 26 return 1; 27 } 28 } 29 return 0; 30 } 31 32 int main() { 33 SDL_Init(SDL_INIT_EVERYTHING); 34 SDL_Window *win = SDL_CreateWindow("debuxar", SDL_WINDOWPOS_UNDEFINED, 35 SDL_WINDOWPOS_UNDEFINED, 400, 300, 0); 36 SDL_Surface *window_surface = SDL_GetWindowSurface(win); 37 SDL_Surface *canvas = SDL_CreateRGBSurfaceWithFormat( 38 0, DIMX, DIMY, 32, SDL_PIXELFORMAT_RGBA8888); 39 40 Uint32* buffer = (Uint32*) canvas->pixels; 41 Uint32 cor = SDL_MapRGBA(canvas->format, 0, 0, 255, 255); 42 SDL_LockSurface(window_surface); 43 memset32(buffer, cor, 400 * 300 * 4); 44 SDL_UnlockSurface(window_surface); 45 46 for(;;) { 47 if ( eventos() ) break; 48 SDL_BlitSurface(canvas, 0, window_surface, 0); 49 SDL_UpdateWindowSurface(win); 50 } 51 SDL_Quit(); 52 return 0; 53 }