void Renderer::renderFrame(Uint8* pixels, int width, int height, int pixelScale) { //Set up reference frame double yaw = camera->rotY; double pitch = camera->rotX; float ref[9] = { sin(pitch) * cos(yaw), sin(pitch) * sin(yaw), -cos(pitch), -sin(yaw), cos(yaw), 0, cos(pitch) * cos(yaw), cos(pitch) * sin(yaw), 2 * sin(pitch) }; //Raycast for each pixel thread* array = new thread[MAX_THREADS]; // start up to MAX_THREADS threads int y = 0; int cy = height / MAX_THREADS; for (int t = 0; t < MAX_THREADS; t++) { array[t] = thread(&Renderer::renderThread, this, pixels, y, y + cy, width, height, ref, pixelScale); y += cy; } // start thread for final bit of work if (y != height) { renderThread(pixels, y, height, width, height, ref, pixelScale); } // join threads for (int t = 0; t < MAX_THREADS; t++) { array[t].join(); } }
int main() { sf::Thread inputThread(&inputThreadFunc); sf::Thread renderThread(&renderThreadFunc); sf::Thread logicThread(&logicThreadFunc); inputThread.Launch(); // wait for input thread to create window while(!global.windowOpen); renderThread.Launch(); logicThread.Launch(); inputThread.Wait(); renderThread.Wait(); logicThread.Wait(); global.window->Close(); delete global.window; return EXIT_SUCCESS; }
// Main program //============================================================================ int main(int argc, char **argv){ // Load files to watch struct stat st; for (uint i = 1; i < argc ; i++){ std::string argument = std::string(argv[i]); if ( iFrag == -1 && ( haveExt(argument,"frag") || haveExt(argument,"fs") ) ) { int ierr = stat(argument.c_str(), &st); if (ierr != 0) { std::cerr << "Error watching file " << argv[i] << std::endl; } else { WatchFile file; file.type = "fragment"; file.path = argument; file.lastChange = st.st_mtime; files.push_back(file); iFrag = files.size()-1; } } else if ( iVert == -1 && ( haveExt(argument,"vert") || haveExt(argument,"vs") ) ) { int ierr = stat(argument.c_str(), &st); if (ierr != 0) { std::cerr << "Error watching file " << argument << std::endl; } else { WatchFile file; file.type = "vertex"; file.path = argument; file.lastChange = st.st_mtime; files.push_back(file); iVert = files.size()-1; } } else if ( iGeom == -1 && ( haveExt(argument,"ply") || haveExt(argument,"PLY") || haveExt(argument,"obj") || haveExt(argument,"OBJ") ) ) { int ierr = stat(argument.c_str(), &st); if (ierr != 0) { std::cerr << "Error watching file " << argument << std::endl; } else { WatchFile file; file.type = "geometry"; file.path = argument; file.lastChange = st.st_mtime; files.push_back(file); iGeom = files.size()-1; } } else if ( haveExt(argument,"png") || haveExt(argument,"PNG") || haveExt(argument,"jpg") || haveExt(argument,"JPG") || haveExt(argument,"jpeg") || haveExt(argument,"JPEG") ){ int ierr = stat(argument.c_str(), &st); if (ierr != 0) { // std::cerr << "Error watching file " << argument << std::endl; } else { WatchFile file; file.type = "image"; file.path = argument; file.lastChange = st.st_mtime; files.push_back(file); } } } // If no shader if( iFrag == -1 && iVert == -1 && iGeom == -1) { std::cerr << "Usage: " << argv[0] << " shader.frag [shader.vert] [mesh.(obj/.ply)] [texture.(png/jpg)] [-textureNameA texture.(png/jpg)] [-u] [-x x] [-y y] [-w width] [-h height] [-l/--livecoding] [--square] [-s seconds] [-o screenshot.png]\n"; return EXIT_FAILURE; } // Fork process with a shared variable // int shmId = shmget(IPC_PRIVATE, sizeof(bool), 0666); pid_t pid = fork(); iHasChanged = (int *) shmat(shmId, NULL, 0); switch(pid) { case -1: //error break; case 0: // child { *iHasChanged = -1; watchThread(); } break; default: { // Initialize openGL context initGL(argc,argv); // OpenGL Render Loop renderThread(argc,argv); // Kill the iNotify watcher once you finish kill(pid, SIGKILL); onExit(); } break; } shmctl(shmId, IPC_RMID, NULL); return 0; }
void n2dEngineImpl::MultiThreadMainLoop(ncTStr title, nuInt FPS) { UpdateThread updateThread(this, FPS); RenderThread renderThread(this, FPS); if (!m_pListener->EngineInit()) { TerminateApplication(); } while (m_IsProgramLooping.load(std::memory_order_acquire)) { if (m_Window.Create(title, m_ClassName, m_hInstance, this)) { MSG msg; m_Window.Show(); CommonInit(); if (!m_pListener->WindowInit()) { TerminateApplication(); } else { wglMakeCurrent(NULL, NULL); updateThread.Resume(); renderThread.Resume(); bool isMessagePumpActive = true; m_Keys.Clear(); while (isMessagePumpActive) { if (PeekMessage(&msg, m_Window.GetWnd(), 0, 0, PM_REMOVE)) { if (msg.message != WM_QUIT) { DispatchMessage(&msg); } else { isMessagePumpActive = false; } } if (!m_IsVisible) { WaitMessage(); } } if (updateThread.Wait(10u)) if (renderThread.Wait(10u)) break; if (PeekMessage(&msg, m_Window.GetWnd(), 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } } m_pListener->WindowUninit(); } else { m_IsProgramLooping.store(false, std::memory_order_release); nat_Throw(natException, _T("Failed to create GL window.")); } } updateThread.Wait(); renderThread.Wait(); m_pListener->EngineUninit(); }
int main() { //Loading screen if(!assets.loadTexture("player.png")) return -1; sf::RenderWindow window(sf::VideoMode(900, 600), "CORE MMO - Unstable Developmental Build", sf::Style::Titlebar | sf::Style::Close); window.setFramerateLimit(60); window.setVerticalSyncEnabled(false); window.setActive(false); window.setKeyRepeatEnabled(false); sf::Thread renderThread(&render, &window); renderThread.launch(); //Menu stuff //*After* clicking join sf::TcpSocket socket; if(!game.connect(&socket)) return -1; sf::Thread recThread(&Game::receive, &game); recThread.launch(); //AFTER LOGIN sf::Clock clock; while(window.isOpen()) { clock.restart(); sf::Event event; while (window.pollEvent(event)) { if(event.type == sf::Event::Closed) window.close(); if(event.type == sf::Event::KeyPressed) { if(event.key.code == sf::Keyboard::W) { RelayPacket move; move.entityID = game.localEntityID; move.msg = "move up"; sf::Packet out; out << move; game.socket->send(out); } else if(event.key.code == sf::Keyboard::S) { RelayPacket move; move.entityID = game.localEntityID; move.msg = "move down"; sf::Packet out; out << move; game.socket->send(out); } if(event.key.code == sf::Keyboard::A) { RelayPacket move; move.entityID = game.localEntityID; move.msg = "move left"; sf::Packet out; out << move; game.socket->send(out); } else if(event.key.code == sf::Keyboard::D) { RelayPacket move; move.entityID = game.localEntityID; move.msg = "move down"; sf::Packet out; out << move; game.socket->send(out); } } if(event.type == sf::Event::KeyReleased) { if(event.key.code == sf::Keyboard::W || event.key.code == sf::Keyboard::S) { RelayPacket move; move.entityID = game.localEntityID; move.msg = "!move v"; sf::Packet out; out << move; game.socket->send(out); } if(event.key.code == sf::Keyboard::A || event.key.code == sf::Keyboard::D) { RelayPacket move; move.entityID = game.localEntityID; move.msg = "!move h"; sf::Packet out; out << move; game.socket->send(out); } } } sf::Time elapsed = clock.getElapsedTime(); if(elapsed.asMicroseconds() < OPTIMAL_TIME) { sf::Time sleepTime = sf::microseconds(elapsed.asMicroseconds() - OPTIMAL_TIME); sf::sleep(sf::milliseconds(sleepTime.asMicroseconds()/1000)); } } game.disconnect(); recThread.terminate(); renderThread.terminate(); //renderThread.terminate(); return 0; }
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here. //MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_ITERATION12, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (FAILED(InitWindow(hInstance, nCmdShow))) { return FALSE; } if( FAILED( InitDevice() ) ) { CleanupDevice(); return 0; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ITERATION12)); // Start render thread RenderThreadWork worker(pRenderSystem); boost::thread renderThread(worker); // Main message loop MSG msg = {0}; while( WM_QUIT != msg.message ) { if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { DoFrame(); } }/* // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { DoFrame(); } }*/ return (int) msg.wParam; }