SDL_Tests

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

commit a6bfe0a073c32e2713870090abfebdcdf4030ab0
parent 50e059333d1b70163c0296cf5c83230043c86bc8
Author: xoel <x.otero@udc.es>
Date:   Mon,  7 Aug 2023 11:03:46 +0200

Cambios

Diffstat:
M02-Debuxar Formas/Makefile | 2+-
M02-Debuxar Formas/debuxar.c | 5+++--
M02-Debuxar Formas/debuxar2.c | 29++++++++++++++++-------------
M02-Debuxar Formas/debuxar2.o | 0
M02-Debuxar Formas/debuxar3.c | 32+++++++++++---------------------
M02-Debuxar Formas/debuxar3.o | 0
M02-Debuxar Formas/test | 0
7 files changed, 31 insertions(+), 37 deletions(-)

diff --git a/02-Debuxar Formas/Makefile b/02-Debuxar Formas/Makefile @@ -5,7 +5,7 @@ LIBS := -lSDL2 -lm CC := gcc #SRCS := $(wildcard *.c) # Asignar a SRCS o nome do ficheiro fornte a compilar -SRCS := debuxar3.c +SRCS := debuxar2.c OBX := $(SRCS:.c=.o) all: $(EXEC) diff --git a/02-Debuxar Formas/debuxar.c b/02-Debuxar Formas/debuxar.c @@ -1,6 +1,8 @@ #include <stdio.h> #include <SDL2/SDL.h> #define MIN(A,B) ((A>B)?A:B) +#define DIMX 400 +#define DIMY 300 SDL_Renderer* rend; SDL_Window* win; @@ -23,7 +25,6 @@ int eventos(){ while (SDL_PollEvent(&e) != 0) { switch (e.type) { case SDL_QUIT: - printf("Pechase"); return 1; } } @@ -47,7 +48,7 @@ int debuxar(){ int main(){ SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO); - win = SDL_CreateWindow("Xogo", + win = SDL_CreateWindow("Debuxar", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 400, 300, 0); diff --git a/02-Debuxar Formas/debuxar2.c b/02-Debuxar Formas/debuxar2.c @@ -1,5 +1,7 @@ #include <stdio.h> #include <SDL2/SDL.h> +#define DIMX 400 +#define DIMY 300 int eventos(){ SDL_Event e; @@ -12,14 +14,12 @@ int eventos(){ return 0; } -int debuxar(SDL_Renderer* rend, SDL_Texture* tex){ - SDL_RenderClear(rend); - SDL_RenderCopy(rend, tex, NULL, NULL); - SDL_RenderPresent(rend); - return 0; +void memset32( void * dest, Uint32 value, uintptr_t size ) { + uintptr_t i; + for( i = 0; i < (size & (~3)); i+=4 ) + memcpy( ((char*)dest) + i, &value, 4 ); } - int main(){ SDL_Renderer* rend; SDL_Window* win; @@ -27,22 +27,25 @@ int main(){ win = SDL_CreateWindow("Debuxar", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - 640, 480, 0); + DIMX, DIMY, 0); Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC; rend = SDL_CreateRenderer(win, -1, render_flags); - //SDL_Texture* tex = SDL_CreateTexture(rend, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 640, 480); - //uint32_t* pixels = malloc(640 * 480 * sizeof(uint32_t)); - //uint32_t valor = 0xffff0000; - //memset(pixels, valor, 640 * 480 * sizeof(uint32_t)); - //SDL_UpdateTexture(tex, NULL, pixels, 640 * sizeof(uint32_t)); + SDL_Texture* tex = SDL_CreateTexture(rend, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, DIMX, DIMY); + uint32_t* pixels = malloc(DIMX * DIMY * sizeof(Uint32)); + Uint32 valor = 0x00ffffff; + memset32(pixels, valor, DIMX * DIMY * sizeof(Uint32)); + SDL_UpdateTexture(tex, NULL, pixels, DIMX * sizeof(Uint32)); for(;;) { if ( eventos() == 1 ) break; SDL_RenderClear(rend); SDL_RenderCopy(rend, tex, NULL, NULL); - //valor -= 1000; + valor -= 0x00000111; + valor &= 0x00ffffff; //printf("Valor : %x\n", valor); //memset(pixels, valor, 640 * 480 * sizeof(uint32_t)); + memset32(pixels, valor, DIMX * DIMY * sizeof(Uint32)); + SDL_UpdateTexture(tex, NULL, pixels, DIMX * sizeof(Uint32)); //SDL_UpdateTexture(tex, NULL, pixels, 640 * sizeof(uint32_t)); SDL_RenderPresent(rend); } diff --git a/02-Debuxar Formas/debuxar2.o b/02-Debuxar Formas/debuxar2.o Binary files differ. diff --git a/02-Debuxar Formas/debuxar3.c b/02-Debuxar Formas/debuxar3.c @@ -1,7 +1,6 @@ /* * Debuxar sen render, modificando a superficie da xanela * Autor: G. Xoel Otero - * zlib license */ #include <SDL2/SDL.h> @@ -9,6 +8,7 @@ #define DIMX 400 #define DIMY 300 + // Igual que memset pero copia 4 bytes de value tantas veces como // indique size na direccion de memoria indicada polo punteiro dest // A funcion orixinal de memset so copia 1 byte x veces nunha direccion @@ -18,16 +18,6 @@ void memset32( void * dest, Uint32 value, uintptr_t size ) { memcpy( ((char*)dest) + i, &value, 4 ); } -int eventos() { - SDL_Event e; - while (SDL_PollEvent(&e) != 0) { - switch (e.type) { - case SDL_QUIT: - return 1; - } - } - return 0; -} int main() { SDL_Init(SDL_INIT_EVERYTHING); @@ -35,18 +25,18 @@ int main() { SDL_WINDOWPOS_UNDEFINED, 400, 300, 0); SDL_Surface *window_surface = SDL_GetWindowSurface(win); Uint32* buffer = (Uint32*) window_surface->pixels; - Uint32 cor = SDL_MapRGBA(window_surface->format, 255, 0, 0, 255); - memset32(buffer, cor, 400 * 300 * 4); - printf("Cor: %x\n", cor); - for(;;) { - if ( eventos() ) break; - SDL_UpdateWindowSurface(win); - cor += 0x00000100; - cor -= 0x01000000; - cor &= 0xff00ffff; - printf("cor: %x \n", cor); + //Uint32 cor = SDL_MapRGBA(window_surface->format, 255, 0, 0, 255); + Uint32 cor = 0x00ff0000; + for (int i=0; i<1000; i++) { + cor += 0x00000001; + cor -= 0x00010000; + cor &= 0x00ffffff; memset32(buffer, cor, 400 * 300 * 4); + SDL_UpdateWindowSurface(win); + SDL_Delay(10); + printf("Cor: %x\n", cor); } + SDL_DestroyWindow(win); SDL_Quit(); return 0; } diff --git a/02-Debuxar Formas/debuxar3.o b/02-Debuxar Formas/debuxar3.o Binary files differ. diff --git a/02-Debuxar Formas/test b/02-Debuxar Formas/test Binary files differ.