From e1c0a3963cd5ce16b614e9e060bd0468f3aa01f1 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Sun, 18 Sep 2022 01:45:22 +0300 Subject: [PATCH] added SDL tests --- .gitignore | 2 + Makefile | 2 +- Makefile.sdl | 12 ++++++ Makefile.sdl2 | 12 ++++++ sdl2pixels.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sdlpixels.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 Makefile.sdl create mode 100644 Makefile.sdl2 create mode 100644 sdl2pixels.c create mode 100644 sdlpixels.c diff --git a/.gitignore b/.gitignore index 198a5c8..32b030d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.o *.swp glpixels +sdlpixels +sdl2pixels diff --git a/Makefile b/Makefile index 69d29a1..35c5fb9 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ obj = glpixels.o bin = glpixels -CFLAGS = -pedantic -Wall -g +CFLAGS = -pedantic -Wall -g -O3 LDFLAGS = -lX11 -lGL -lglut -lm $(bin): $(obj) diff --git a/Makefile.sdl b/Makefile.sdl new file mode 100644 index 0000000..c8c4405 --- /dev/null +++ b/Makefile.sdl @@ -0,0 +1,12 @@ +obj = sdlpixels.o +bin = sdlpixels + +CFLAGS = -pedantic -Wall -g -O3 `sdl-config --cflags` +LDFLAGS = `sdl-config --libs` -lm + +$(bin): $(obj) + $(CC) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff --git a/Makefile.sdl2 b/Makefile.sdl2 new file mode 100644 index 0000000..60e487b --- /dev/null +++ b/Makefile.sdl2 @@ -0,0 +1,12 @@ +obj = sdl2pixels.o +bin = sdl2pixels + +CFLAGS = -pedantic -Wall -g -O3 `sdl2-config --cflags` +LDFLAGS = `sdl2-config --libs` -lm + +$(bin): $(obj) + $(CC) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff --git a/sdl2pixels.c b/sdl2pixels.c new file mode 100644 index 0000000..08b363e --- /dev/null +++ b/sdl2pixels.c @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include + +int init(void); +void display(void); + +int win_width, win_height; +int max_xscroll, max_yscroll; + +#define IMG_W 1400 +#define IMG_H 1200 +unsigned int img[IMG_W * IMG_H]; + +unsigned int start_tm; +unsigned int num_frames; + +int quit; +SDL_Window *win; +SDL_Renderer *sdlrend; +SDL_Texture *sdltex; + + +int main(int argc, char **argv) +{ + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); + + if(SDL_CreateWindowAndRenderer(800, 600, 0, &win, &sdlrend) == -1) { + fprintf(stderr, "failed to set video mode\n"); + return 1; + } + SDL_SetWindowTitle(win, "SDL pixel drawing benchmark"); + + printf("SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver()); + + if(init() == -1) { + return 1; + } + + start_tm = SDL_GetTicks(); + while(!quit) { + SDL_Event ev; + while(SDL_PollEvent(&ev)) { + switch(ev.type) { + case SDL_QUIT: + goto done; + + case SDL_KEYDOWN: + if(ev.key.keysym.sym == 27) goto done; + break; + + default: + break; + } + if(quit) goto done; + } + + display(); + } + +done: + SDL_Quit(); + return 0; +} + + +int init(void) +{ + int i, j, xor, r, g, b; + unsigned int *ptr; + + ptr = img; + for(i=0; i> 1) & 0xff; + g = xor & 0xff; + b = (xor << 1) & 0xff; + *ptr++ = b | (g << 8) | (r << 16); + } + } + + win_width = 800; + win_height = 600; + + max_xscroll = IMG_W - win_width; + max_yscroll = IMG_H - win_height; + + if(!(sdltex = SDL_CreateTexture(sdlrend, SDL_PIXELFORMAT_XRGB8888, + SDL_TEXTUREACCESS_STREAMING, win_width, win_height))) { + fprintf(stderr, "failed to create SDL texture\n"); + return -1; + } + + return 0; +} + +void display(void) +{ + int i, j; + unsigned int tm = SDL_GetTicks() - start_tm; + unsigned int interv; + float t = tm / 256.0f; + int xoffs = (int)((sin(t) * 0.5f + 0.5f) * max_xscroll); + int yoffs = (int)((cos(t) * 0.5f + 0.5f) * max_yscroll); + unsigned int *start = img + yoffs * IMG_W + xoffs; + + SDL_UpdateTexture(sdltex, 0, start, IMG_W * 4); + SDL_RenderCopy(sdlrend, sdltex, 0, 0); + SDL_RenderPresent(sdlrend); + + num_frames++; + tm = SDL_GetTicks(); + interv = tm - start_tm; + if(interv >= 4000) { + unsigned int fps = 100000 * num_frames / interv; + printf("SDL2: %.2f fps\n", fps / 100.0f); + num_frames = 0; + start_tm = tm; + quit = 1; + } +} diff --git a/sdlpixels.c b/sdlpixels.c new file mode 100644 index 0000000..46d075d --- /dev/null +++ b/sdlpixels.c @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include + +int init(void); +void display(void); + +int win_width, win_height; +int max_xscroll, max_yscroll; + +#define IMG_W 1400 +#define IMG_H 1200 +unsigned int img[IMG_W * IMG_H]; + +unsigned int start_tm; +unsigned int num_frames; + +int quit; +SDL_Surface *fbsurf; + + +int main(int argc, char **argv) +{ + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); + if(!(fbsurf = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE))) { + fprintf(stderr, "failed to set video mode\n"); + return 1; + } + SDL_WM_SetCaption("SDL pixel drawing benchmark", 0); + + if(init() == -1) { + return 1; + } + + start_tm = SDL_GetTicks(); + while(!quit) { + SDL_Event ev; + while(SDL_PollEvent(&ev)) { + switch(ev.type) { + case SDL_QUIT: + goto done; + + case SDL_KEYDOWN: + if(ev.key.keysym.sym == 27) goto done; + break; + + default: + break; + } + if(quit) goto done; + } + + display(); + } + +done: + SDL_Quit(); + return 0; +} + + +int init(void) +{ + int i, j, xor, r, g, b; + unsigned int *ptr; + + ptr = img; + for(i=0; i> 1) & 0xff; + g = xor & 0xff; + b = (xor << 1) & 0xff; + *ptr++ = b | (g << 8) | (r << 16); + } + } + + win_width = 800; + win_height = 600; + + max_xscroll = IMG_W - win_width; + max_yscroll = IMG_H - win_height; + return 0; +} + +void display(void) +{ + int i, j; + unsigned int tm = SDL_GetTicks() - start_tm; + unsigned int interv; + float t = tm / 256.0f; + int xoffs = (int)((sin(t) * 0.5f + 0.5f) * max_xscroll); + int yoffs = (int)((cos(t) * 0.5f + 0.5f) * max_yscroll); + unsigned int *start = img + yoffs * IMG_W + xoffs; + unsigned int *dest; + + if(SDL_MUSTLOCK(fbsurf)) { + SDL_LockSurface(fbsurf); + } + dest = fbsurf->pixels; + + for(i=0; i= 4000) { + unsigned int fps = 100000 * num_frames / interv; + printf("SDL: %.2f fps\n", fps / 100.0f); + num_frames = 0; + start_tm = tm; + quit = 1; + } +} -- 1.7.10.4