Put the render loop back in its right place, but with delaying functionality to keep it down to roughly 60fps, not eating up all the X11 time.
authorEli Gottlieb <eligottlieb@gmail.com>
Mon, 19 Jul 2010 00:24:02 -0400
changeset 4805de3b3455f8ec
parent 4804 b33752266d8f
child 4806 007567dbb8c1
Put the render loop back in its right place, but with delaying functionality to keep it down to roughly 60fps, not eating up all the X11 time.
test/testshape.c
     1.1 --- a/test/testshape.c	Sun Jul 18 23:51:47 2010 -0400
     1.2 +++ b/test/testshape.c	Mon Jul 19 00:24:02 2010 -0400
     1.3 @@ -6,11 +6,14 @@
     1.4  #include <SDL_video.h>
     1.5  #include <SDL_shape.h>
     1.6  #include <SDL_keysym.h>
     1.7 +#include <SDL_timer.h>
     1.8  
     1.9  #define SHAPED_WINDOW_X 150
    1.10  #define SHAPED_WINDOW_Y 150
    1.11  #define SHAPED_WINDOW_DIMENSION 640
    1.12  
    1.13 +#define TICK_INTERVAL 18
    1.14 +
    1.15  void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) {
    1.16  	SDL_SelectRenderer(window);
    1.17  	
    1.18 @@ -24,6 +27,16 @@
    1.19  	SDL_RenderPresent();
    1.20  }
    1.21  
    1.22 +static Uint32 next_time;
    1.23 +
    1.24 +Uint32 time_left() {
    1.25 +	Uint32 now = SDL_GetTicks();
    1.26 +	if(next_time <= now)
    1.27 +		return 0;
    1.28 +	else
    1.29 +		return next_time - now;
    1.30 +}
    1.31 +
    1.32  int main(int argc,char** argv) {
    1.33          if(argc < 2) {
    1.34          	printf("SDL_Shape requires at least one bitmap file as argument.\n");
    1.35 @@ -106,7 +119,7 @@
    1.36  	SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
    1.37  	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
    1.38  	SDL_SetWindowShape(window,pictures[current_picture],&mode);
    1.39 -	render(window,textures[current_picture],texture_dimensions);
    1.40 +	next_time = SDL_GetTicks() + TICK_INTERVAL;
    1.41  	while(should_exit == 0) {
    1.42  		event_pending = SDL_PollEvent(&event);
    1.43  		if(event_pending == 1) {
    1.44 @@ -123,13 +136,14 @@
    1.45  				SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
    1.46  				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
    1.47  				SDL_SetWindowShape(window,pictures[current_picture],&mode);
    1.48 -				render(window,textures[current_picture],texture_dimensions);
    1.49 -				
    1.50  			}
    1.51  			if(event.type == SDL_QUIT)
    1.52  				should_exit = 1;
    1.53  			event_pending = 0;
    1.54  		}
    1.55 +		render(window,textures[current_picture],texture_dimensions);
    1.56 +		SDL_Delay(time_left());
    1.57 +		next_time += TICK_INTERVAL;
    1.58  	}
    1.59  	
    1.60  	//Free the textures.