void run() { struct stat st; if (stat(datafilepath, &st)) { fprintf(stderr, "can't stat \"%s\": %s\n", datafilepath, strerror(errno)); exit(1); } // file truncated? whatever. int states_len = st.st_size / sizeof(pstate); int fd = open(datafilepath, O_RDONLY); if (fd == -1) { fprintf(stderr, "can't open \"%s\": %s\n", datafilepath, strerror(errno)); exit(1); } void *states = mmap(NULL, states_len*sizeof(pstate), PROT_READ, MAP_PRIVATE, fd, 0); if (states == MAP_FAILED) { fprintf(stderr, "can't mmap \"%s\n: %s\n", datafilepath, strerror(errno)); exit(1); } close(fd); fprintf(stderr, "drawing pendulum with %d states on %s...\n", states_len, whitebg?"white":"black"); drawPendulum(states, states_len); munmap(states, states_len*sizeof(pstate)); flushSHMSurface(sf); }
unsigned int viewer(state* s, double* a, double reward, void* instance) { char str[255]; double cartPoleScalingFactor = (screenWidth / 2.0) - 20; double ratioPixels = cartPoleScalingFactor / (2.0 * parameters[1]); int done = 0; SDL_Event event; SDL_FillRect(screen, NULL, SDL_MapRGBA(screen->format, 255,255,255,255)); if(a != NULL) { sprintf(str, "Applied X Force : % f", ((parameters[7] + parameters[7]) * a[0]) - parameters[7]); stringRGBA(screen, 5, 5, str, 0, 0, 0, 255); } sprintf(str, "Angular Position: % f", s->angularPosition); stringRGBA(screen, 5, 15, str, 0, 0, 0, 255); sprintf(str, "Angular Velocity: % f", s->angularVelocity); stringRGBA(screen, 5, 25, str, 0, 0, 0, 255); sprintf(str, "X Position : % f", s->xPosition); stringRGBA(screen, 5, 35, str, 0, 0, 0, 255); sprintf(str, "X Velocity : % f", s->xVelocity); stringRGBA(screen, 5, 45, str, 0, 0, 0, 255); sprintf(str, "Reward : % f", reward); stringRGBA(screen, 5, 55, str, 0, 0, 0, 255); stringRGBA(screen, 5, screenHeight - 10, "Press escape to quit", 0, 0, 0, 255); lineRGBA(screen, 10, screenHeight / 2.0, (screenWidth / 2.0) - 10, screenHeight / 2.0, 0, 0, 0, 255); drawPendulum(s, 0, 0, 0, 255, ratioPixels); if(a != NULL) { if(a[0] < 0.5) filledTrigonRGBA(screen, (screenWidth / 4.0) - 10, screenHeight * 0.9, (screenWidth / 4.0), (screenHeight * 0.9) - 5, (screenWidth / 4.0) - 10, (screenHeight * 0.9) - 10 , 0, 0, 0, 255); else if(a[0] > 0.5) filledTrigonRGBA(screen, (screenWidth / 4.0) + 10, screenHeight * 0.9, (screenWidth / 4.0) + 20, (screenHeight * 0.9) - 5, (screenWidth / 4.0) + 10, (screenHeight * 0.9) - 10, 0, 0, 0, 255); else boxRGBA(screen, (screenWidth / 4.0) - 5, screenHeight * 0.9, (screenWidth / 4.0) + 5, (screenHeight * 0.9) - 10, 0, 0, 0, 255); } if(algorithm_drawingProcedure != NULL) algorithm_drawingProcedure(screen, screenWidth, screenHeight, instance); SDL_Flip(screen); while(SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: if(event.key.keysym.sym == SDLK_ESCAPE) done = 1; if(event.key.keysym.sym == SDLK_SPACE) done = waitForAnotherSpace(); break; case SDL_QUIT: done = 1; break; } } SDL_framerateDelay(&fpsm); return done; }