Guest User

Untitled

a guest
Feb 18th, 2025
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.27 KB | None | 0 0
  1. /*
  2. Notes I kept while testing:
  3.  
  4. try-1/ - 2000 frames in 11 seconds
  5. - works but uses surfaces rather than rendering
  6.   - seems rendering may be better as uses GPU:
  7.     - https://gamedev.stackexchange.com/questions/180077/in-sdl-what-is-the-difference-between-using-a-surfaces-and-a-renderer
  8. - started here which was surfaces:
  9.   - https://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php
  10. - also looked at this which is rendering:
  11.   - https://www.geeksforgeeks.org/sdl-library-in-c-c-with-examples/
  12.  
  13. try-2/ - 2000 frames in 5.2 seconds - NOTE MAY NEED LockSurface!!!
  14. - surfaces accessing pixels directly:
  15.   - https://stackoverflow.com/questions/20070155/how-to-set-a-pixel-in-a-sdl-surface?rq=3
  16.  
  17. try-3/ - 200 (not 2000) frames in 10.7 seconds (ie 2000 frames in ~107 seconds)
  18. - switch to renderer from surfaces
  19.   - https://stackoverflow.com/questions/20579658/how-to-draw-pixels-in-sdl-2-0
  20.  
  21. try-4/ - 2000 frames in 1 second - BEST result and stretches to match window!
  22. - render accessing pixels directly:
  23.   - https://gist.github.com/mmozeiko/729860eeb414f1a2ee345d9d3ab4dd4e
  24.   - https://stackoverflow.com/questions/62379457/how-to-draw-a-grid-of-pixels-fast-with-sdl2#comment110428713_62394835
  25. */
  26.  
  27. //try-4/main.c
  28. #include <stdbool.h>
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <SDL2/SDL.h>
  32.  
  33. //Constants
  34. //=========
  35. #define WINDOW_TITLE        "SDL2 test"
  36.  
  37. #define CALC_SCREEN_WIDTH   384
  38. #define CALC_SCREEN_HEIGHT  216
  39. #define SCALE_FACTOR        3
  40. #define SCREEN_WIDTH        CALC_SCREEN_WIDTH*SCALE_FACTOR
  41. #define SCREEN_HEIGHT       CALC_SCREEN_HEIGHT*SCALE_FACTOR
  42.  
  43. //Commented out below but see SDL_Delay below to set framerate
  44. //#define FPS 24
  45.  
  46. //Globals
  47. //=======
  48. uint32_t screen[CALC_SCREEN_HEIGHT][CALC_SCREEN_WIDTH];
  49.  
  50. int main(int argc, char *argv[])
  51. {
  52.     //Set up pixel data
  53.     for (int y=0;y<CALC_SCREEN_HEIGHT;y++)
  54.     {
  55.         for (int x=0;x<CALC_SCREEN_WIDTH;x++)
  56.         {
  57.             screen[y][x]=((y<<16)|(x/2)|0xFF000000);
  58.         }
  59.     }
  60.  
  61.     //Initialize SDL
  62.     SDL_Window *window=NULL;
  63.     SDL_Renderer *renderer=NULL;
  64.     SDL_Texture *texture=NULL;
  65.     if (SDL_Init(SDL_INIT_VIDEO)<0)
  66.     {
  67.         printf("Error: SDL2 did not initialize. SDL error: %s\n", SDL_GetError());
  68.         exit(1);
  69.     }
  70.    
  71.     //Create window
  72.     window=SDL_CreateWindow(WINDOW_TITLE,SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
  73.     if (window==NULL)
  74.     {
  75.         printf("Error: SDL2 could not create window. SDL error: %s\n", SDL_GetError());
  76.         exit(1);
  77.     }
  78.  
  79.     //Draw to texture
  80.     renderer=SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
  81.     texture=SDL_CreateTexture(renderer,SDL_PIXELFORMAT_ARGB8888,SDL_TEXTUREACCESS_STREAMING,CALC_SCREEN_WIDTH,CALC_SCREEN_HEIGHT);
  82.  
  83.     //Keep track of frames drawn and exit after 2000
  84.     unsigned int debug_counter=0;
  85.  
  86.     //Event loop
  87.     SDL_Event event;
  88.     bool done=false;
  89.     while (done==false)
  90.     {
  91.         while (SDL_PollEvent(&event))
  92.         {
  93.             switch (event.type)
  94.             {
  95.                 case SDL_QUIT:
  96.                     //User closed window
  97.                     done=true;
  98.                     break;
  99.                 case SDL_KEYDOWN:
  100.                     switch (event.key.keysym.scancode)
  101.                     {
  102.                         case SDL_SCANCODE_ESCAPE:
  103.                             //User pressed escape
  104.                             done=true;
  105.                             break;
  106.                     }
  107.             }
  108.         }
  109.  
  110.         //Logic goes here
  111.  
  112.         //Update window with drawn pixels
  113.         SDL_UpdateTexture(texture,NULL,screen,CALC_SCREEN_WIDTH*sizeof(uint32_t));
  114.         SDL_RenderCopy(renderer,texture,NULL,NULL);
  115.         SDL_RenderPresent(renderer);
  116.  
  117.         //End test after 2000 frames drwn
  118.         if (debug_counter==2000) done=true;
  119.         else debug_counter++;
  120.  
  121.         //Delay to limit FPS
  122.         //SDL_Delay(1000/FPS);
  123.     }
  124.  
  125.     //Free memory
  126.     SDL_DestroyTexture(texture);
  127.     SDL_DestroyRenderer(renderer);
  128.     SDL_DestroyWindow(window);
  129.     SDL_Quit();
  130.  
  131.     return 0;    
  132. }
  133.  
  134.  
  135.  
  136.  
  137. //build script
  138. #!/bin/bash
  139. FILENAME=main
  140.  
  141. gcc -O3 ${FILENAME}.c -o $FILENAME `sdl2-config --cflags --libs`
  142. ret=$?
  143. if [ $ret -eq 0 ]; then
  144.     ./$FILENAME
  145. fi
  146.  
  147.  
  148.  
  149.  
Advertisement
Add Comment
Please, Sign In to add comment