c - SDL2 program stops responding after a while -


so trying simple sprite animation. using image sprite: http://answers.unity3d.com/storage/temp/5358-1123_01_01.jpg

this code:

#include <sdl.h> #include <sdl_image.h> #include <stdio.h> #include <stdlib.h> #include <stdbool.h>   //screen dimension constants const int screen_width = 640; const int screen_height = 480;   #define path_to_image "sprite.jpg"  int main(int argc, char* args[]) {      const int walking_animation_frames = 8;      sdl_rect gspriteclips[ 8 ] = {         (sdl_rect) {.h = 112, .w = 88, .x = 16, .y = 16},         (sdl_rect) {.h = 112, .w = 88, .x = 133, .y = 16},         (sdl_rect) {.h = 112, .w = 88, .x = 265, .y = 16},         (sdl_rect) {.h = 112, .w = 88, .x = 398, .y = 16},         (sdl_rect) {.h = 112, .w = 88, .x = 16, .y = 139},         (sdl_rect) {.h = 112, .w = 88, .x = 132, .y = 139},         (sdl_rect) {.h = 112, .w = 88, .x = 264, .y = 139},         (sdl_rect) {.h = 112, .w = 88, .x = 397, .y = 139},     };      //the window renderer     sdl_renderer *renderer = null;      //the window we'll rendering     sdl_window *gwindow;      //initialize sdl     if (sdl_init(sdl_init_video) == 0) {         //create window         gwindow = sdl_createwindow("character animation", sdl_windowpos_undefined, sdl_windowpos_undefined, screen_width, screen_height, sdl_window_shown);          if (gwindow != null) {             //get window surface             renderer = sdl_createrenderer(gwindow, -1, sdl_renderer_accelerated | sdl_renderer_presentvsync);             int imgflags = img_init_jpg;              if (img_init(imgflags) & imgflags) {                 bool quit = false;                   sdl_texture *texture = img_loadtexture(renderer, path_to_image);                 int frame = 0;                 int selected_frame = 0;                  while (!quit) {                               sdl_renderclear(renderer);                      selected_frame = (frame / walking_animation_frames);                      sdl_rect *currentclip = &gspriteclips[selected_frame];                     sdl_rendercopy(renderer, texture, currentclip, &((sdl_rect){ .x = 16, .y = 16, .h = 112, .w = 88}) );                     sdl_renderpresent(renderer);                      printf("selected frame: %d -- %d - %d\n", selected_frame, frame, walking_animation_frames);                      ++frame;                     if ((frame / walking_animation_frames) >= walking_animation_frames) {                         printf("entrei aqui\n");                         frame = 0;                                             }                 }             }           } else {             printf("sdl_init failed on window: %s\n", sdl_geterror());         }     }      //destroy window     sdl_destroyrenderer(renderer);     renderer = null;     sdl_destroywindow(gwindow);     gwindow = null;      //quit sdl subsystems     img_quit();     sdl_quit();       return (exit_success); } 

the code compiles without issue. based on lazy foo lesson 14.

the problem having after 10 or 15 secs program stops responding (the window greys out the sprite animation stops). let me clear, animation work , works few times before program freezes (i see cycling @ least 3 or 4 times).

i have debug printf works , keeps on working after window of program stops responding.

i thought might going on gspriteclips in printf see numbers equal or below 7.

does see obvious problems missing?

thanks.

your main loop doesn't flush sdl event queue. copy-pasted sdl tutorial:

while( !quit ){     // optionally code comes here...      // following inner loop flushes event queue.     // (all events have been queued between 2 frames.)      // declare event struct somewhere     sdl_event event;      /* poll events */     while( sdl_pollevent( &event ) ){            switch( event.type ){             /* keyboard event */             /* pass event data onto printkeyinfo() */             case sdl_keydown:             case sdl_keyup:                 printkeyinfo( &event.key );                 break;              /* sdl_quit event (window close) */             case sdl_quit:                 quit = 1;                 break;              default:                 break;         }     }      // optionally code comes here... } 

the sdl event loop equivalent of windows getmessage() loop of typical native windows gui application. not consuming messages in gui application on windows results in greyed , stuck window symptom.


Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -