added SDL tests master
authorJohn Tsiombikas <nuclear@member.fsf.org>
Sat, 17 Sep 2022 22:45:22 +0000 (01:45 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Sat, 17 Sep 2022 22:45:22 +0000 (01:45 +0300)
.gitignore
Makefile
Makefile.sdl [new file with mode: 0644]
Makefile.sdl2 [new file with mode: 0644]
sdl2pixels.c [new file with mode: 0644]
sdlpixels.c [new file with mode: 0644]

index 198a5c8..32b030d 100644 (file)
@@ -1,3 +1,5 @@
 *.o
 *.swp
 glpixels
+sdlpixels
+sdl2pixels
index 69d29a1..35c5fb9 100644 (file)
--- 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 (file)
index 0000000..c8c4405
--- /dev/null
@@ -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 (file)
index 0000000..60e487b
--- /dev/null
@@ -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 (file)
index 0000000..08b363e
--- /dev/null
@@ -0,0 +1,124 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <SDL2/SDL.h>
+
+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<IMG_H; i++) {
+               for(j=0; j<IMG_W; j++) {
+                       xor = i ^ j;
+                       r = (xor >> 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 (file)
index 0000000..46d075d
--- /dev/null
@@ -0,0 +1,126 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <SDL/SDL.h>
+
+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<IMG_H; i++) {
+               for(j=0; j<IMG_W; j++) {
+                       xor = i ^ j;
+                       r = (xor >> 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<win_height; i++) {
+               for(j=0; j<win_width; j++) {
+                       *dest++ = start[j];
+               }
+               start += IMG_W;
+       }
+
+       if(SDL_MUSTLOCK(fbsurf)) {
+               SDL_UnlockSurface(fbsurf);
+       }
+       SDL_Flip(fbsurf);
+
+       num_frames++;
+       tm = SDL_GetTicks();
+       interv = tm - start_tm;
+       if(interv >= 4000) {
+               unsigned int fps = 100000 * num_frames / interv;
+               printf("SDL: %.2f fps\n", fps / 100.0f);
+               num_frames = 0;
+               start_tm = tm;
+               quit = 1;
+       }
+}