SDL_Tests

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

commit 50e059333d1b70163c0296cf5c83230043c86bc8
Author: xoel <x.otero@udc.es>
Date:   Thu,  3 Aug 2023 18:36:49 +0200

xanela baleira e debuxar

Diffstat:
A01-Xanela Baleira/Makefile | 15+++++++++++++++
A01-Xanela Baleira/test | 0
A01-Xanela Baleira/xanela.c | 41+++++++++++++++++++++++++++++++++++++++++
A01-Xanela Baleira/xanela.o | 0
A02-Debuxar Formas/Makefile | 19+++++++++++++++++++
A02-Debuxar Formas/debuxar.c | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A02-Debuxar Formas/debuxar2.c | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
A02-Debuxar Formas/debuxar2.o | 0
A02-Debuxar Formas/debuxar3.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
A02-Debuxar Formas/debuxar3.o | 0
A02-Debuxar Formas/debuxar4.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
A02-Debuxar Formas/debuxar4.o | 0
A02-Debuxar Formas/test | 0
13 files changed, 302 insertions(+), 0 deletions(-)

diff --git a/01-Xanela Baleira/Makefile b/01-Xanela Baleira/Makefile @@ -0,0 +1,15 @@ +EXEC := test +CFLAGS := -Wall +LDFLAGS := +LIBS := -lSDL2 -lm +CC := gcc +SRCS := xanela.c +OBX := $(SRCS:.c=.o) + +all: $(EXEC) +$(EXEC): ${OBX} + $(CC) $(LDFLAGS) $^ -o $@ $(LIBS) +$(OBX): ${EXEC} + $(CC) $(CFLAGS) $< -c -o $@ +clean: + rm -f $(EXEC) $(OBX) diff --git a/01-Xanela Baleira/test b/01-Xanela Baleira/test Binary files differ. diff --git a/01-Xanela Baleira/xanela.c b/01-Xanela Baleira/xanela.c @@ -0,0 +1,41 @@ +#include <stdio.h> +#include <SDL2/SDL.h> + +SDL_Renderer* rend; +SDL_Window* win; + +int eventos(){ + SDL_Event e; + while (SDL_PollEvent(&e) != 0) + switch (e.type) { + case SDL_QUIT: + printf("Pechase\n"); + return 1; + } + return 0; +} + +int debuxar(){ + SDL_RenderClear(rend); + SDL_RenderPresent(rend); + return 0; +} + + +int main(){ + SDL_Init(SDL_INIT_VIDEO); + win = SDL_CreateWindow("Xogo", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + 200, 150, 0); + + Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC; + rend = SDL_CreateRenderer(win, -1, render_flags); + + for(;;) { + if ( eventos() == 1 ) + break; + debuxar(); + } + return 0; +} diff --git a/01-Xanela Baleira/xanela.o b/01-Xanela Baleira/xanela.o Binary files differ. diff --git a/02-Debuxar Formas/Makefile b/02-Debuxar Formas/Makefile @@ -0,0 +1,19 @@ +EXEC := test +CFLAGS := -Wall +LDFLAGS := +LIBS := -lSDL2 -lm +CC := gcc +#SRCS := $(wildcard *.c) +# Asignar a SRCS o nome do ficheiro fornte a compilar +SRCS := debuxar3.c +OBX := $(SRCS:.c=.o) + +all: $(EXEC) +$(EXEC): ${OBX} + $(CC) $(LDFLAGS) $^ -o $@ $(LIBS) +$(OBX): ${SRCS} + $(CC) $(CFLAGS) $< -c -o $@ +clean: + rm -f $(EXEC) $(OBX) + +#.PHONY: clean diff --git a/02-Debuxar Formas/debuxar.c b/02-Debuxar Formas/debuxar.c @@ -0,0 +1,67 @@ +#include <stdio.h> +#include <SDL2/SDL.h> +#define MIN(A,B) ((A>B)?A:B) + +SDL_Renderer* rend; +SDL_Window* win; +SDL_Surface* s; + +void debuxar_rectangulo(SDL_Surface* s, SDL_Point p1, SDL_Point p2, uint8_t cor){ + // p1 (esquina superior esquerda) p2 (esquina inferior dereita) + uint8_t* offscreen = (uint8_t*)s->pixels; + offscreen += (s->pitch*p1.y); // Situamolo na primeira fila a debuxar + for (int i = p1.y; i<=MIN(s->h, p2.y); i++){ + for (int j = p1.x; j<=MIN(s->w, p2.x); j++){ + offscreen[j] = cor; + } + offscreen += s->pitch; + } +} + +int eventos(){ + SDL_Event e; + while (SDL_PollEvent(&e) != 0) { + switch (e.type) { + case SDL_QUIT: + printf("Pechase"); + return 1; + } + } + return 0; +} + +int debuxar(){ + SDL_RenderClear(rend); + SDL_Point p1 = {.x = 0, .y = 0}; + SDL_Point p2 = {.x = 20, .y = 20}; + debuxar_rectangulo(s, p1, p2, 1); + SDL_Texture* tex = SDL_CreateTextureFromSurface(rend, s); + SDL_Rect dst = {.x = 0, .y = 0, .w = 20, .h = 20}; + SDL_RenderCopy(rend, tex, NULL, &dst); + SDL_DestroyTexture(tex); + + SDL_RenderPresent(rend); + return 0; +} + + +int main(){ + SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO); + win = SDL_CreateWindow("Xogo", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + 400, 300, 0); + + //s = SDL_CreateRGBSurface(0, 20, 20, 32, 0, 0, 0, 255); + + Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC; + rend = SDL_CreateRenderer(win, -1, render_flags); + + for(;;) { + if ( eventos() == 1 ) + break; + debuxar(); + } + SDL_Quit(); + return 0; +} diff --git a/02-Debuxar Formas/debuxar2.c b/02-Debuxar Formas/debuxar2.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <SDL2/SDL.h> + +int eventos(){ + SDL_Event e; + while (SDL_PollEvent(&e) != 0) { + switch (e.type) { + case SDL_QUIT: + return 1; + } + } + return 0; +} + +int debuxar(SDL_Renderer* rend, SDL_Texture* tex){ + SDL_RenderClear(rend); + SDL_RenderCopy(rend, tex, NULL, NULL); + SDL_RenderPresent(rend); + return 0; +} + + +int main(){ + SDL_Renderer* rend; + SDL_Window* win; + SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO); + win = SDL_CreateWindow("Debuxar", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + 640, 480, 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)); + for(;;) { + if ( eventos() == 1 ) break; + SDL_RenderClear(rend); + SDL_RenderCopy(rend, tex, NULL, NULL); + //valor -= 1000; + //printf("Valor : %x\n", valor); + //memset(pixels, valor, 640 * 480 * sizeof(uint32_t)); + //SDL_UpdateTexture(tex, NULL, pixels, 640 * sizeof(uint32_t)); + SDL_RenderPresent(rend); + } + free(pixels); + SDL_DestroyTexture(tex); + SDL_DestroyRenderer(rend); + SDL_DestroyWindow(win); + SDL_Quit(); + return 0; +} 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 @@ -0,0 +1,52 @@ +/* + * Debuxar sen render, modificando a superficie da xanela + * Autor: G. Xoel Otero + * zlib license + */ + +#include <SDL2/SDL.h> +#include <stdio.h> +#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 +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 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); + SDL_Window *win = SDL_CreateWindow("debuxar", SDL_WINDOWPOS_UNDEFINED, + 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); + memset32(buffer, cor, 400 * 300 * 4); + } + 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/debuxar4.c b/02-Debuxar Formas/debuxar4.c @@ -0,0 +1,53 @@ +/* + * Debuxar sen render, modificando a superficie da xanela + * Autor: G. Xoel Otero + * zlib license + */ + +#include <SDL2/SDL.h> +#include <stdio.h> +#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 +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 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); + SDL_Window *win = SDL_CreateWindow("debuxar", SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, 400, 300, 0); + SDL_Surface *window_surface = SDL_GetWindowSurface(win); + SDL_Surface *canvas = SDL_CreateRGBSurfaceWithFormat( + 0, DIMX, DIMY, 32, SDL_PIXELFORMAT_RGBA8888); + + Uint32* buffer = (Uint32*) canvas->pixels; + Uint32 cor = SDL_MapRGBA(canvas->format, 0, 0, 255, 255); + SDL_LockSurface(window_surface); + memset32(buffer, cor, 400 * 300 * 4); + SDL_UnlockSurface(window_surface); + + for(;;) { + if ( eventos() ) break; + SDL_BlitSurface(canvas, 0, window_surface, 0); + SDL_UpdateWindowSurface(win); + } + SDL_Quit(); + return 0; +} diff --git a/02-Debuxar Formas/debuxar4.o b/02-Debuxar Formas/debuxar4.o Binary files differ. diff --git a/02-Debuxar Formas/test b/02-Debuxar Formas/test Binary files differ.