Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Notes I kept while testing:
- try-1/ - 2000 frames in 11 seconds
- - works but uses surfaces rather than rendering
- - seems rendering may be better as uses GPU:
- - https://gamedev.stackexchange.com/questions/180077/in-sdl-what-is-the-difference-between-using-a-surfaces-and-a-renderer
- - started here which was surfaces:
- - https://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php
- - also looked at this which is rendering:
- - https://www.geeksforgeeks.org/sdl-library-in-c-c-with-examples/
- try-2/ - 2000 frames in 5.2 seconds - NOTE MAY NEED LockSurface!!!
- - surfaces accessing pixels directly:
- - https://stackoverflow.com/questions/20070155/how-to-set-a-pixel-in-a-sdl-surface?rq=3
- try-3/ - 200 (not 2000) frames in 10.7 seconds (ie 2000 frames in ~107 seconds)
- - switch to renderer from surfaces
- - https://stackoverflow.com/questions/20579658/how-to-draw-pixels-in-sdl-2-0
- try-4/ - 2000 frames in 1 second - BEST result and stretches to match window!
- - render accessing pixels directly:
- - https://gist.github.com/mmozeiko/729860eeb414f1a2ee345d9d3ab4dd4e
- - https://stackoverflow.com/questions/62379457/how-to-draw-a-grid-of-pixels-fast-with-sdl2#comment110428713_62394835
- */
- //try-4/main.c
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <SDL2/SDL.h>
- //Constants
- //=========
- #define WINDOW_TITLE "SDL2 test"
- #define CALC_SCREEN_WIDTH 384
- #define CALC_SCREEN_HEIGHT 216
- #define SCALE_FACTOR 3
- #define SCREEN_WIDTH CALC_SCREEN_WIDTH*SCALE_FACTOR
- #define SCREEN_HEIGHT CALC_SCREEN_HEIGHT*SCALE_FACTOR
- //Commented out below but see SDL_Delay below to set framerate
- //#define FPS 24
- //Globals
- //=======
- uint32_t screen[CALC_SCREEN_HEIGHT][CALC_SCREEN_WIDTH];
- int main(int argc, char *argv[])
- {
- //Set up pixel data
- for (int y=0;y<CALC_SCREEN_HEIGHT;y++)
- {
- for (int x=0;x<CALC_SCREEN_WIDTH;x++)
- {
- screen[y][x]=((y<<16)|(x/2)|0xFF000000);
- }
- }
- //Initialize SDL
- SDL_Window *window=NULL;
- SDL_Renderer *renderer=NULL;
- SDL_Texture *texture=NULL;
- if (SDL_Init(SDL_INIT_VIDEO)<0)
- {
- printf("Error: SDL2 did not initialize. SDL error: %s\n", SDL_GetError());
- exit(1);
- }
- //Create window
- window=SDL_CreateWindow(WINDOW_TITLE,SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
- if (window==NULL)
- {
- printf("Error: SDL2 could not create window. SDL error: %s\n", SDL_GetError());
- exit(1);
- }
- //Draw to texture
- renderer=SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
- texture=SDL_CreateTexture(renderer,SDL_PIXELFORMAT_ARGB8888,SDL_TEXTUREACCESS_STREAMING,CALC_SCREEN_WIDTH,CALC_SCREEN_HEIGHT);
- //Keep track of frames drawn and exit after 2000
- unsigned int debug_counter=0;
- //Event loop
- SDL_Event event;
- bool done=false;
- while (done==false)
- {
- while (SDL_PollEvent(&event))
- {
- switch (event.type)
- {
- case SDL_QUIT:
- //User closed window
- done=true;
- break;
- case SDL_KEYDOWN:
- switch (event.key.keysym.scancode)
- {
- case SDL_SCANCODE_ESCAPE:
- //User pressed escape
- done=true;
- break;
- }
- }
- }
- //Logic goes here
- //Update window with drawn pixels
- SDL_UpdateTexture(texture,NULL,screen,CALC_SCREEN_WIDTH*sizeof(uint32_t));
- SDL_RenderCopy(renderer,texture,NULL,NULL);
- SDL_RenderPresent(renderer);
- //End test after 2000 frames drwn
- if (debug_counter==2000) done=true;
- else debug_counter++;
- //Delay to limit FPS
- //SDL_Delay(1000/FPS);
- }
- //Free memory
- SDL_DestroyTexture(texture);
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
- return 0;
- }
- //build script
- #!/bin/bash
- FILENAME=main
- gcc -O3 ${FILENAME}.c -o $FILENAME `sdl2-config --cflags --libs`
- ret=$?
- if [ $ret -eq 0 ]; then
- ./$FILENAME
- fi
Advertisement
Add Comment
Please, Sign In to add comment