void defer() { if(!kiss_init()) return; glfwSleep(0); }
int MainLoop() { Renderer::Initialize(); Game g; g.Initialize(); double time = glfwGetTime(); do { double newTime = glfwGetTime(); double dt = newTime - time; time = newTime; g.Update(dt); g.Draw(); Renderer::DrawCallback(); const double wantedFrametime = 1.0 / 30.0; const double sleepThreshold = 1.0/1000.0; double sleepTime = wantedFrametime - (glfwGetTime() - newTime) - sleepThreshold; if (sleepTime > 0.0) glfwSleep(sleepTime); } while (Renderer::IsRunning()); return 0; }
int main(int argc, char *argv[]) { video_init(0); audio_init(argc, argv); game_init(); double last = glfwGetTime(); while (running) { double current = glfwGetTime(); int delta = (current - last) * 1000; if (delta < MIN_DELTA) { glfwSleep((MIN_DELTA-delta)/1000.0); continue; } last = current; now += delta; if (delta > MAX_DELTA) continue; game_tick(delta); } audio_shutdown(); video_shutdown(); return EXIT_SUCCESS; }
int main(int argc, char const **argv) { glfwInit(); glfwOpenWindow(800, 600, 8, 8, 8, 8, 16, 0, GLFW_WINDOW); glfwSetWindowSizeCallback(set_window_size); glfwSetKeyCallback(set_key); init(); int running = 1; while(running) { update(); update_keys(); draw(); glfwSwapBuffers(); running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED); glfwSleep(0.01); } terminate(); glfwCloseWindow(); glfwTerminate(); return 0; }
void Controller::run() { double last_render = 0; double last_update = 0; double frame_interval = 1.f/60.f; double update_interval = 1.f/90.f; double cur_time = 0; while(m_bRunning) { cur_time = glfwGetTime(); if(cur_time - last_update > update_interval) { onUpdate(); last_update = glfwGetTime(); } if(cur_time - last_render > frame_interval) { last_render = glfwGetTime(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); onRender(); glfwSwapBuffers(); } glfwSleep(update_interval - (cur_time + glfwGetTime())); } }
int main( void ) { //记录程序运行状态 int running = GL_TRUE; //初始化 GLFW if( !glfwInit() ) { exit( EXIT_FAILURE ); } // 创建一个OpenGL 窗口 if( !glfwOpenWindow( WINDOW_WIDTH,WINDOW_HEIGHT,6,6,6,0,32,0,GLFW_WINDOW) ) { glfwTerminate(); exit( EXIT_FAILURE ); } //初始化OpenGL窗口 initScene(WINDOW_WIDTH, WINDOW_HEIGHT); //设置窗口大小发生变化时的回调函数 glfwSetWindowSizeCallback(resizeGL); //主循环 while( running ) { // OpenGL rendering goes here... glClear( GL_COLOR_BUFFER_BIT ); // 当按下ESC键的时候触发 running = !glfwGetKey( GLFW_KEY_ESC ) &&glfwGetWindowParam( GLFW_OPENED ); drawScene(); //延时0.05秒 glfwSleep(0.05 ); } glfwTerminate(); //退出程序 exit( EXIT_SUCCESS ); }
int main( int argc, char** argv ) { // Initialize GLFW glfwInit(); if( !setupWindow( appWidth, appHeight, fullScreen ) ) return -1; // Initalize application and engine app = new Application( generatePath( argv[0], "../Content" ) ); if ( !app->init() ) { // Fake message box glfwCloseWindow(); glfwOpenWindow( 800, 16, 8, 8, 8, 8, 24, 8, GLFW_WINDOW ); glfwSetWindowTitle( "Unable to initalize engine - Make sure you have an OpenGL 2.0 compatible graphics card" ); glfwSleep( 5 ); std::cout << "Unable to initalize engine" << std::endl; std::cout << "Make sure you have an OpenGL 2.0 compatible graphics card"; glfwTerminate(); return -1; } app->resize( appWidth, appHeight ); TwWindowSize( appWidth, appHeight ); //glfwDisable( GLFW_MOUSE_CURSOR ); int frames = 0; float fps = 30.0f; t0 = glfwGetTime(); running = true; // Game loop while( running ) { // Calc FPS ++frames; if( frames >= 3 ) { double t = glfwGetTime(); fps = frames / (float)(t - t0); frames = 0; t0 = t; } // Render app->mainLoop( fps ); TwDraw(); glfwSwapBuffers(); } glfwEnable( GLFW_MOUSE_CURSOR ); // Quit app->release(); delete app; glfwTerminate(); return 0; }
// This is executed in the sound thread. void *TSExec::Thread(void *p) { // TSExec *context = (TSExec *)p; while(1) { glfwSleep(0.1); if (gMode.Get() == GameMode::EXIT) pthread_exit(0); // printf("TSExec::Thread %d\n", context->fThread); View::gSoundControl.RequestSound(View::SoundControl::STSExec); // This will wakeup this process } return 0; // Not reached }
bool PerformLoginProcedure(const string &email, const string &licencekey, const string &password, bool testOverride) { // Wait for acknowledge from server (in the form of a protocol command) auto beginTime = glfwGetTime(); if (gMode.Get() != GameMode::LOGIN) { auto &ss = View::gErrorManager.GetStream(true, false); ss << "Login in wrong state " << gMode.Get(); return false; } LoginMessage(testOverride ? "test0" : email.c_str()); gMode.Set(GameMode::PASSWORD); while (gMode.Get() != GameMode::GAME) { glfwSleep(0.01); // Avoid a busy wait ListenForServerMessages(); // Wait for automatic login without password switch (gMode.Get()) { case GameMode::PASSWORD: case GameMode::WAIT_ACK: continue; // Nothing to do yet. case GameMode::REQ_PASSWD: // printf("Parse: mode GameMode::REQ_PASSWD, %d chall bytes\n", gLoginChallengeLength); Password(password.c_str(), licencekey.c_str()); gMode.Set(GameMode::WAIT_ACK); // printf("loginDialog::UpdateDialog: Mode GameMode::WAIT_ACK\n"); continue; case GameMode::LOGIN_FAILED: { auto &ss = View::gErrorManager.GetStream(true, false); ss << "Login failed"; return false; } case GameMode::ESC: exit(1); case GameMode::GAME: break; // Now we are done! case GameMode::TELEPORT: case GameMode::CONSTRUCT: case GameMode::INIT: case GameMode::LOGIN: case GameMode::EXIT: std::cerr << "PerformLoginProcedure Unexpected mode" << gMode.Get(); exit(1); } } double connectionDelay = glfwGetTime() - beginTime; if (connectionDelay > 0.5 && gDebugOpenGL) printf("PerformLoginProcedure connection delay %f\n", connectionDelay); unsigned char b[] = { 0x03, 0x00, CMD_GET_COORDINATE }; // GET_COORDINATE SendMsg(b, sizeof b); View::gSoundControl.RequestMusicMode(View::SoundControl::SMusicModeTourist); return true; }
Thread * Thread::GetThisThreadAndRevertArgument(void *& pArgument) { void ** pArguments = static_cast<void **>(pArgument); Thread * pThread = static_cast<Thread *>(pArguments[0]); pArgument = pArguments[1]; // Delay the thread from starting until the end of its Thread object constructor while (!pThread->m_bCanStart && pThread->ShouldBeRunning()) glfwSleep(0.001); return pThread; }
void mainLoop() { double oldTime = glfwGetTime(); while(1 && runlevel != 0) { double currTime = glfwGetTime(); if(currTime - oldTime < 1 / g_desiredFPS) glfwSleep(currTime - oldTime); cbRender(); glfwPollEvents(); oldTime = currTime; } cleanup(); }
int main(int argc, char** argv) { int running = GL_TRUE; if (!glfwInit()) { fprintf(stderr, "failed to initialize GLFW\n"); exit(EXIT_FAILURE); } glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4); glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 4); glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); if (!glfwOpenWindow(WIN_WIDTH, WIN_HEIGHT, 0,0,0,0,0,0, GLFW_WINDOW)) { fprintf(stderr, "Failed to open GLFW window\n"); glfwTerminate(); exit(EXIT_FAILURE); } glewExperimental = true; if (glewInit() != GLEW_OK) { return -1; } glfwSetWindowTitle("Hello, OpenGL"); if (argc != 2) { printf("Usage: shaderquad <fragment shader>\n"); exit(0); } setup(argv[1]); if (p == 0) running = GL_FALSE; while (running) { t += 0.01f; render(); glfwSwapBuffers(); running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED); if (glfwGetKey(76)) { setup(argv[1]); } glfwSleep(.1); } glfwTerminate(); exit(EXIT_SUCCESS); }
void RocketLibSystem::EventLoop(RocketLibSystemIdleFunction idle_function) { while (running) { if( !glfwGetWindowParam( GLFW_OPENED ) ) { running = false; } idle_function(); if( !glfwGetWindowParam( GLFW_ACTIVE ) ) { glfwSleep( 0.1 ); } } }
void Engine_run() { double timestart = glfwGetTime(); int fps = 0; time = Timer_getTime(); while(!done) { Timer_update(); time = Timer_getTime(); Input_handleInput(time); World_refresh(); Logic_update(time); Physics_update(theworld); Physics_detectCollisions(theworld); Logic_handleCollisions(theworld); Graphics_renderScene(theworld); Graphics_refresh(); fps++; if (glfwGetTime() - timestart >= 1.0) { fprintf(stderr, "Fps: %i, Objects: %u, Collision checks: %i, Script calls: %i\n", fps, World_getObjectCount(), Physics_getCheckCounter(), Script_getCallCounter()); Physics_resetCheckCounter(); Script_resetCallCounter(); fps = 0; timestart = glfwGetTime(); } glfwSleep(0.033); } WorldObject_destroy(theworld); }
int main( int argc, char* argv[] ) { if (glfwInit() != GL_TRUE){ std::cout << "Could not init glfw"<< std::endl; return 2; } if (glfwOpenWindow(600, 600, 8, 8, 8, 8, 32, 0, GLFW_WINDOW) != GL_TRUE){ std::cout << "Could not create window"<< std::endl; return 3; } glfwSetWindowTitle ("rGraphicsLibrary - Game of Life"); if(glewInit() != GLEW_OK) { std::cerr << "Failed to init glew" << std::endl; exit(1); } chkGLErr(); std::cout << "OpenGL Version: " << OpenGLInfo::getOpenGLVersion() << std::endl; std::cout << "Glew Version: " << OpenGLInfo::getGlewVersion() << std::endl; std::cout << "GLSL Version: " << OpenGLInfo::getGLSLVersion() << std::endl; std::cout << "OpenGL Vendor: " << OpenGLInfo::getOpenGLVendor() << std::endl; std::cout << "OpenGL Renderer: "<< OpenGLInfo::getOpenGLRenderer() << std::endl; chkGLErr(); init(); glfwSetWindowSizeCallback(GLFWwindowsizefun(resize)); while(true){ if (glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS) break; evolve(); draw(); glfwSleep(0.5); } return 0; }
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmd, int show) #endif { // setup random generator from start srand((unsigned int)time(NULL)); glfwInit(); GameManager manager = GameManager(); double current_time, old_time, dt, next_update; old_time = glfwGetTime(); while(true) { current_time = glfwGetTime(); dt = current_time - old_time; if (dt <= 0.0) continue; old_time = current_time; next_update = current_time + 1.0 / FRAMERATE; // printf("%f\n", 1.0 / dt); if (!manager.update(dt)) break; manager.draw(); glfwSwapBuffers(); if (!glfwGetWindowParam(GLFW_OPENED)) break; dt = next_update - glfwGetTime(); if (dt > 0.0) { glfwSleep(dt); } } return 0; }
void Game::run() { double lastTime = glfwGetTime(), currentTime = 0.0; float delta = 0.0f; running = true; while(running && glfwGetWindowParam(GLFW_OPENED)) { // Calculate delta t currentTime = glfwGetTime(); delta = (float)(currentTime - lastTime); lastTime = currentTime; update(delta); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw(); camera->update(delta); glErrorCheck("main loop"); glfwSwapBuffers(); glfwSleep(0.001); } }
void GLFWTimerProvider::Sleep(double amount) { glfwSleep(amount); }
int runScene(Drawable & scene){ glfwInit(); if( !glfwOpenWindow( 500, 500, 0,0,0,0, 16,0, GLFW_WINDOW ) ) { glfwTerminate(); } glfwSetWindowTitle( "3D" ); glfwEnable( GLFW_STICKY_KEYS ); glfwEnable( GLFW_MOUSE_CURSOR ); glfwDisable( GLFW_AUTO_POLL_EVENTS ); setupCallbacks(); do //Main Loop. { setupProjection(); // stereo_state.Ping(); projection_state.Load(); float zoom=2.0; glScalef(zoom*2.0/256.0, zoom*2.0/256.0, zoom*2.0/256.0); glDisable (GL_BLEND); glDisable(GL_LIGHTING); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); setupModelview(); /* Temporary light insertion: */ navigator.Draw(); //reference plane DrawPlane(V3f(0,0,0), V3f(20,0,0), V3f(0,20,0), 5); SetupLighting(); scene.Draw(); //Clear modified mouse state. mouse_state.Moved(); glfwSwapBuffers(); if(!stereo_state.enabled){ glfwWaitEvents(); }else{ glfwPollEvents(); glfwSleep(0.01); //just sleep. }; navigator.idle_move(); } while(!glfwGetKey( GLFW_KEY_ESC ) && glfwGetWindowParam( GLFW_OPENED )); return true; };
void PullInfo(){ printf("================================================================================\n"); int major, minor, rev; glfwGetVersion(&major, &minor, &rev); printf("GLFW version is %i.%i.%i\n", major, minor, rev); int width, height; glfwGetWindowSize(&width, &height); printf("Window size is %i %i\n", width, height); int status = glfwGetKey(GLFW_KEY_LCTRL); if(status == GLFW_PRESS) printf("Left control is pressed\n"); else printf("Left control is released\n"); status = glfwGetMouseButton(GLFW_MOUSE_BUTTON_1); if(status == GLFW_PRESS) printf("Mouse button 1 is pressed\n"); else printf("Mouse button 1 is released\n"); int x, y; glfwGetMousePos(&x, &y); printf("Mouse position is %i %i\n", x, y); int wheel = glfwGetMouseWheel(); printf("Mouse wheel pos is %i\n", wheel); double time = glfwGetTime(); printf("Time is %f\n", time); glfwGetGLVersion(&major, &minor, &rev); printf("GL version is %i.%i.%i\n", major, minor, rev); int proc = glfwGetNumberOfProcessors(); printf("%i processors are available\n", proc); unsigned int i; for(i = 0; i<nb_params; i++) printf(" - %-27s : %i\n", GetParamName(params[i]), glfwGetWindowParam(params[i])); const char* extension = "MOZ_WEBGL_compressed_texture_s3tc"; printf("'%s' extension is %s.\n", extension, glfwExtensionSupported(extension) ? "supported" : "not supported"); extension = "GL_EXT_framebuffer_object"; printf("'%s' extension is %s.\n", extension, glfwExtensionSupported(extension) ? "supported" : "not supported"); extension = "glBindBuffer"; void* proc_addr = glfwGetProcAddress(extension); printf("'%s' extension proc address is %p.\n", extension, proc_addr); printf("Sleeping 1 sec...\n"); glfwSleep(1); printf("...Done.\n"); printf("================================================================================\n"); #ifdef REPORT_RESULT int result = 1; REPORT_RESULT(); #endif }
/* MMDAgent_sleep: sleep in ms */ void MMDAgent_sleep(double t) { glfwSleep(t * 0.001); }
void Thread_Manager::Sleep(float secs) { glfwSleep((double)secs); }
// Main program. Open a window. Continuously read commands from the // command line interface and draw frames with 60Hz onto the // screen. Commands for multiple frames can be cached within a ring // buffer, so that a consistent frame rate can be maintained even if // the control program doesn't respond within 16ms. The control // program is written in Common Lisp and a 16ms time granularity cannot // always be maintained, due to the time a garbage collection may // take. int main(int argc,char**argv) { // make sure frame rate update cycle is phase locked to vertical // refresh of screen. On Nvidia hardware this can be done by setting // the following environment variable. setenv("__GL_SYNC_TO_VBLANK","1",1); if(!glfwInit()) exit(EXIT_FAILURE); int width=1280,height=1024; if(argc==3){ width=atoi(argv[1]); height=atoi(argv[2]); } if(!glfwOpenWindow(width,height,8,8,8, 0,0,0, GLFW_WINDOW )){ glfwTerminate(); exit(EXIT_FAILURE); } printf("lcos started %dx%d\n",width,height); fflush(stdout); glfwSetWindowTitle("LCoS"); //glfwSetWindowPos(-8,-31); // use glfw method to sync to vertical refresh glfwSwapInterval(1); glfwSetKeyCallback(keyhandler); init_matrix(); glMatrixMode(GL_PROJECTION); glOrtho(0,1280,1024,0,-1,1); glMatrixMode(GL_MODELVIEW); while(running){ while(check_stdin()>0){ char*s=malloc(CMDLEN); char*line=fgets(s,CMDLEN,stdin); if(line!=s) printf("fgets error\n"); parse_line(line); } glClear(GL_COLOR_BUFFER_BIT); glLoadMatrixf(m); frame_count++; // run all commands which have been stored in the queue while(!emptyp()){ char*cmd=pop(); if(0==strncmp(cmd,"swap",4)){ struct timeval tv; gettimeofday(&tv,0); printf("q swap frame-count=%d sec=%lu usec=%lu\n", frame_count,tv.tv_sec,tv.tv_usec); free(cmd); goto nextframe; } parse_line(cmd); printf("q cread=%5d cwrite=%5d cmd=%s\n",circread,circwrite,cmd); fflush(stdout); free(cmd); } nextframe: if(show_calibration_stripes){ float v = 100+20*(frame_count%10); glRectf(v,0,v+2,400); } glfwSleep(1./72); glfwSwapBuffers(); } glfwCloseWindow(); glfwTerminate(); exit(EXIT_SUCCESS); }
int GameMenu( void ) { int option; // Enable sticky keys glfwEnable( GLFW_STICKY_KEYS ); // Wait for a game menu key to be pressed do { // Get window size glfwGetWindowSize( &width, &height ); // Set viewport glViewport( 0, 0, width, height ); // Clear display glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); glClear( GL_COLOR_BUFFER_BIT ); // Setup projection matrix glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho( 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f ); // Setup modelview matrix glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); // Display title glColor3f( 1.0f, 1.0f, 1.0f ); DrawImage( TEX_TITLE, 0.1f, 0.9f, 0.0f, 0.3f ); // Display menu glColor3f( 1.0f, 1.0f, 0.0f ); DrawImage( TEX_MENU, 0.38f, 0.62f, 0.35f, 0.5f ); // Display instructions glColor3f( 0.0f, 1.0f, 1.0f ); DrawImage( TEX_INSTR, 0.32f, 0.68f, 0.65f, 0.85f ); // Swap buffers glfwSwapBuffers(); // Check for keys if( glfwGetKey( 'Q' ) || !glfwGetWindowParam( GLFW_OPENED ) ) { option = MENU_QUIT; } else if( glfwGetKey( GLFW_KEY_F1 ) ) { option = MENU_PLAY; } else { option = MENU_NONE; } // To avoid horrible busy waiting, sleep for at least 20 ms glfwSleep( 0.02 ); } while( option == MENU_NONE ); // Disable sticky keys glfwDisable( GLFW_STICKY_KEYS ); return option; }
int main(int argc, char** argv) { int running = GL_TRUE; if(!glfwInit()) { std::cerr << "Failed to initialize GLFW." << std::endl; exit(EXIT_FAILURE); } static const int width = 640; static const int height = 480; static const int red_bits = 8; static const int green_bits = 8; static const int blue_bits = 8; static const int alpha_bits = 8; static const int depth_bits = 24; static const int stencil_bits = 8; static const int mode = GLFW_WINDOW; glfwOpenWindowHint(GLFW_VERSION_MAJOR, 3); glfwOpenWindowHint(GLFW_VERSION_MINOR, 3); if (!glfwOpenWindow(width, height, red_bits, green_bits, blue_bits, alpha_bits, depth_bits, stencil_bits, mode)) { std::cerr << "Failed to open window." << std::endl; exit(EXIT_FAILURE); } if (gl3wInit()) { std::cerr << "Failed on gl3w init" << std::endl; exit(EXIT_FAILURE); } if (!gl3wIsSupported(3, 3)) { std::cerr << "OpenGL 3.3 not supported." << std::endl; exit(EXIT_FAILURE); } glViewport(0, 0, (GLsizei)width, (GLsizei)height); init(); CHECK_GL_ERROR; static const double fps = 1./60.; static const double ups = 1./60.; double last_update = 0.; double last_render = 0.; while (running) { if (glfwGetTime() - last_render > fps) { glClearColor(0.f, 0.f, 0.f, 0.f); glClear(GL_COLOR_BUFFER_BIT); display(); glfwSwapBuffers(); last_render = glfwGetTime(); } if (glfwGetTime() - last_update > ups) { //do updating stuff, I guess last_update = glfwGetTime(); } running = !glfwGetKey('q') && glfwGetWindowParam(GLFW_OPENED); glfwSleep(std::min(fps - (glfwGetTime() - last_render), ups - (glfwGetTime() - last_update))); CHECK_GL_ERROR; } }
void GameOver() { //stop all movement game->SetDropAccel(0.0f); game->SetDropInterval(0.0f); float startx = game->GetGrid()->GetGridXStart(); startx -= 50; float starty = game->GetGrid()->GetGridYStart(); starty += game->GetGrid()->GetGridBlockSize() * (GRID_HEIGHT / 2) - (100/2); glBindTexture(GL_TEXTURE_2D, game->GetTextures()[FONT_TEXTURE]); glListBase(game->GetTextureBase()-32); game->CaptureKeys(false); bool loop = true; do { glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); UpdateDisplay(); glEnable(GL_BLEND); glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); glColor4f(0.3f, 0.3f, 0.3f, 0.3f); glBegin(GL_QUADS); glVertex2f(startx, starty); glVertex2f(startx + 250, starty); glVertex2f(startx + 250, starty + 100); glVertex2f(startx, starty + 100); glEnd(); //save where we are glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glBlendFunc(GL_ONE, GL_DST_ALPHA); glTranslated(startx + ((250.0f / 2.0f) - float((9.0f * 16.0f) / 2)), starty + ((100.0f / 2.0f) - (16.0f / 2.0f)), 0); glColor4f(1.0f, 1.0f, 1.0f, 0.0f); glCallLists(9, GL_UNSIGNED_BYTE, "GAME OVER"); glPopMatrix(); glDisable(GL_BLEND); glfwSwapBuffers(); glfwSleep(0.003); loop = !glfwGetKey(GLFW_KEY_F2); if (glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS) { //show the menu menu = new MenuControl(); menu->EscDown = true; MenuLoop(); loop = false; delete menu; menu = NULL; } } while(loop); game->StartNewGame(); }
//main game loop //everything gets triggered from here void GameLoop() { double interval = 0; double oldtime = 0; double newtime = 0; double lastDropTime = 0; int frames = 0; game->SetFPS(0); double sleeptime = 0.0; game->SetGameState(true); sound->Play(SOUND_MUSIC); glfwSleep(1); game->GetNextTetromino()->Create(); do { if (game->GetPaused()) { newtime = -1; //printf("newtime: %f lastdrop: %f\n", newtime, lastDropTime); } else newtime = glfwGetTime(); if ((newtime - oldtime) > 1.0 || frames == 0) { game->SetFPS((double)frames / (newtime - oldtime)); frames = 0; oldtime = newtime; } frames++; if (game->GetFPS() > 80) { sleeptime += 0.00001; //printf("sleep: %f.4\n", sleeptime); } if (game->GetFPS() > 30 && game->GetFPS() < 60 ) { sleeptime -= 0.00001; //printf("sleep: %f.4\n", sleeptime); } glViewport(0, 0, 640, 480); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); interval = game->GetDropInterval(); if (game->GetDropAccel() > 0) { interval *= game->GetDropAccel(); } if (newtime - lastDropTime >= interval) { MoveDown(game->GetGrid(), game->GetTetromino()); lastDropTime = newtime; } if (game->GetTetromino()->IsLocked()) { //clear the current tetromino and set the next to be the current game->GetTetromino()->Reset(); game->SetTetromino(game->GetNextTetromino()); //create a new "next" to draw the preview from game->GetNextTetromino()->Reset(); game->GetNextTetromino()->Create(); //reset drop accel so that new tetromino goes at normal speed //and not the speed the previous was going as it was locked. //would be better if i could simulate a GLFW_RELEASE game->SetDropAccel(0.0f); //game over check if (game->GetGrid()->CheckCollision(game->GetTetromino())) { GameOver(); } } UpdateDisplay(); glfwSwapBuffers(); glfwSleep(sleeptime); game->SetGameState(/* !glfwGetKey(GLFW_KEY_ESC) && */ glfwGetWindowParam(GLFW_OPENED) && game->IsGameRunning()); } while (game->IsGameRunning()); glDeleteLists(game->GetTextureBase() ,256); }
int Canvas::_flush(lua_State *l) { if (!lua_istable(l, 1)) return luaL_error(l, "canvas expected as first argument"); // clear key events keys_down.clear(); keys_up.clear(); glfwSwapBuffers(); glfwSleep(0.005); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // if (!_canvas->view.is2d) glLoadIdentity(); // set the game time double time = glfwGetTime(); lua_getfield(l, 1, "time"); double old = luaL_checknumber(l, -1); lua_pop(l, 1); setnumber("time" , time); setnumber("dt" , time - old); int x, y; double cx, cy; // canvas mouse coordinates glfwGetMousePos(&x, &y); Viewport &view = _canvas->view; if (view.is2d) { double vwidth = view.right - view.left; double vheight = view.bottom - view.top; cx = view.left + (x * vwidth / _canvas->width()); cy = view.top + (y * vheight / _canvas->height()); } else { cx = (double)x; cy = (double)y; } // update the mouse point lua_getfield(l, 1, "mouse"); setnumber("x", cx); setnumber("y", cy); setbool("left", glfwGetMouseButton(GLFW_MOUSE_BUTTON_LEFT)); setbool("right", glfwGetMouseButton(GLFW_MOUSE_BUTTON_RIGHT)); lua_getfield(l, 1, "input"); float axis[2]; glfwGetJoystickPos(GLFW_JOYSTICK_1, axis, 2); setnumber("xaxis", axis[0]); setnumber("yaxis", axis[1]); setbool("left", glfwGetKey(GLFW_KEY_LEFT)); setbool("right", glfwGetKey(GLFW_KEY_RIGHT)); setbool("up", glfwGetKey(GLFW_KEY_UP)); setbool("down", glfwGetKey(GLFW_KEY_DOWN)); setbool("a", glfwGetKey('Q')); setbool("b", glfwGetKey('W')); lua_pushboolean(l, glfwGetWindowParam(GLFW_OPENED) > 0); return 1; }
// Main function int main(int argc, char * argv[]) { /*{ PointerState test; test.UpdateButtonState(0) = true; test.UpdateButtonState(1) = true; test.UpdateButtonState(2) = true; PointerState test2(test); test2.UpdateButtonState(2) = false; std::cout << test.GetButtonState(2) << &std::endl; std::cout << test2.GetButtonState(2) << &std::endl; return 0; }*/ #if 0 { std::function<void()> Test = []() { std::cout << "Hi from anon func.\n"; }; //std::function<ConceptString(const std::vector<ConceptId> &)> Test = [](const std::vector<ConceptId> & Parameters){ return ConceptString({FindConcept("<"), GetParameterIfExists(Parameters, 0), FindConcept(">")}); }; // Call Test() //Test(); printf("size of func %ld\n", sizeof(Test)); return 0; } #endif // Set env vars std::string GoPath; // This has to exist even after putenv() call because putenv simply adds a pointer rather than copying the value std::string Path = "PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/go/bin"; { // Initialize the value of GoPath GoPath = "GOPATH="; // Get current working directory { auto cwd = getcwd(nullptr, 0); if (nullptr == cwd) { printf("Fatal Error: getcwd() failed."); exit(1); } printf("Current-working-dir is '%s' (should be the folder where README.md is).\n", cwd); GoPath = GoPath + cwd + "/GoLand"; GoPath += ":"; GoPath = GoPath + cwd + "/GoLanding"; Path = Path + ":" + cwd + "/GoLand/bin"; free(cwd); } putenv(const_cast<char *>("DYLD_INSERT_LIBRARIES=")); // HACK: Const cast putenv(const_cast<char *>("TERM=xterm")); // HACK: Const cast putenv(const_cast<char *>(GoPath.c_str())); // HACK: Const cast // HACK: Add go/bin to $PATH by hardcoding the whole PATH for OS X putenv(const_cast<char *>(Path.c_str())); // HACK: Const cast } glfwInit(); // Verify the GLFW library and header versions match { int Major, Minor, Revision; glfwGetVersion(&Major, &Minor, &Revision); bool Match = (GLFW_VERSION_MAJOR == Major && GLFW_VERSION_MINOR == Minor && GLFW_VERSION_REVISION == Revision); if (!Match) { std::cerr << "Error: GLFW library and header versions do not match." << std::endl; throw 0; } else { std::cout << "Using GLFW " << Major << "." << Minor << "." << Revision << "." << std::endl; } } // Open main window { GLFWvidmode DesktopMode; glfwGetDesktopMode(&DesktopMode); glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 32); const bool Fullscreen = static_cast<bool>(0); const Vector2n WindowDimensons(1536, 960); if (!Fullscreen) { glfwOpenWindow(WindowDimensons.X(), WindowDimensons.Y(), DesktopMode.RedBits, DesktopMode.GreenBits, DesktopMode.BlueBits, 0, 0, 0, GLFW_WINDOW); glfwSetWindowPos((DesktopMode.Width - WindowDimensons.X()) / 2, (DesktopMode.Height - WindowDimensons.Y()) / 2); // Center the window } else { glfwOpenWindow(DesktopMode.Width, DesktopMode.Height, DesktopMode.RedBits, DesktopMode.GreenBits, DesktopMode.BlueBits, 0, 0, 0, GLFW_FULLSCREEN); glfwEnable(GLFW_MOUSE_CURSOR); } { std::ostringstream x; x << "CPU Count: " << glfwGetNumberOfProcessors() << std::endl << "GL Renderer: " << glGetString(GL_VENDOR) << " " << glGetString(GL_RENDERER) << " " << glGetString(GL_VERSION) << std::endl << "GLFW_ACCELERATED: " << glfwGetWindowParam(GLFW_ACCELERATED) << std::endl << "GLFW_RED_BITS: " << glfwGetWindowParam(GLFW_RED_BITS) << std::endl << "GLFW_GREEN_BITS: " << glfwGetWindowParam(GLFW_GREEN_BITS) << std::endl << "GLFW_BLUE_BITS: " << glfwGetWindowParam(GLFW_BLUE_BITS) << std::endl << "GLFW_ALPHA_BITS: " << glfwGetWindowParam(GLFW_ALPHA_BITS) << std::endl << "GLFW_DEPTH_BITS: " << glfwGetWindowParam(GLFW_DEPTH_BITS) << std::endl << "GLFW_STENCIL_BITS: " << glfwGetWindowParam(GLFW_STENCIL_BITS) << std::endl << "GLFW_REFRESH_RATE: " << glfwGetWindowParam(GLFW_REFRESH_RATE) << std::endl << "GLFW_FSAA_SAMPLES: " << glfwGetWindowParam(GLFW_FSAA_SAMPLES) << std::endl; std::cout << x.str(); } { //glfwSetWindowTitle("Conception"); glfwSwapInterval(1); // Set Vsync glfwDisable(GLFW_AUTO_POLL_EVENTS); glfwEnable(GLFW_KEY_REPEAT); glfwDisable(GLFW_SYSTEM_KEYS); } } { InputManager InputManager; g_InputManager = &InputManager; ConceptionApp MainApp(InputManager); //LiveEditorApp MainApp(InputManager); //ConceptionTestApp MainApp(InputManager); //MultitouchTestApp MainApp(InputManager); //SentienceApp MainApp(InputManager); glfwSetWindowTitle(MainApp.GetTitle().c_str()); // Perform the layout of UI widgets MainApp.Layout(); // OpenGL settings InitializeOpenGL(); std::cout << std::endl; // Done loading // Main loop while (glfwGetWindowParam(GLFW_OPENED)) { auto CurrentTime = glfwGetTime(); static auto LastTime = CurrentTime; auto TimePassed = CurrentTime - LastTime; LastTime = CurrentTime; // DEBUG: Moved to top of loop to enable debug printing from input handling code glClear(GL_COLOR_BUFFER_BIT); // Clear frame // Process input { // Populate InputEventQueue if (MainApp.ShouldRedrawRegardless()) glfwPollEvents(); else { glfwWaitEvents(); //if (glfwGetTime() - LastTime >= 1) printf("Slept for %f secs\n", glfwGetTime() - LastTime); LastTime = glfwGetTime(); } //InputManager.ProcessTimePassed(TimePassed); MainApp.ProcessEventQueue(InputManager.ModifyInputEventQueue()); MainApp.ProcessTimePassed(TimePassed); } // Render { // DEBUG: Moved to top of loop to enable debug printing from input handling code ///glClear(GL_COLOR_BUFFER_BIT); // Clear frame MainApp.Render(); } // Display new frame glfwSwapBuffers(); //glFinish(); ///printf("%f ms frame\n", TimePassed * 1000); // Use less CPU in background if (!glfwGetWindowParam(GLFW_ACTIVE)) { glfwSleep(0.100); } } } // Clean up OglUtilsKillFont(); glfwTerminate(); std::cout << "\nReturning 0 from main().\n"; return 0; }
/* MMDAgent_sleep: sleep in sec */ void MMDAgent_sleep(double t) { glfwSleep(t); }