MessageHandler::Result ScriptMessageHandler::doHandle(Message &message, QString *reason) { Q_UNUSED(reason); if (message.isIncoming()) return MessageHandler::Accept; QLatin1Literal command("/script"); QString text = message.text(); if (text.size() < command.size() + 2 || !text.startsWith(QLatin1String(command.data())) || !text.at(command.size()).isSpace()) { static QRegExp regexp("\\[\\[(.*)\\]\\]", Qt::CaseInsensitive); Q_ASSERT(regexp.isValid()); int pos = 0; bool first = true; while ((pos = regexp.indexIn(text, pos)) != -1) { if (first) { first = false; openContext(message.chatUnit()); } QString result = m_engine->evaluate(regexp.cap(1)).toString(); debug() << regexp.cap(1) << result; text.replace(pos, regexp.matchedLength(), result); pos += result.length(); } if (!first) { closeContext(); message.setText(text); } return MessageHandler::Accept; } openContext(message.chatUnit()); m_engine->evaluate(message.text().mid(command.size() + 1)); closeContext(); return MessageHandler::Reject; }
void MediaEngine::closeMedia() { closeContext(); if (m_pdata) delete m_pdata; if (m_demux) delete m_demux; m_pdata = 0; m_demux = 0; AudioClose(&m_audioContext); m_isVideoEnd = false; }
bool MediaEngine::openContext() { #ifdef USE_FFMPEG InitFFmpeg(); if (m_pFormatCtx || !m_pdata) return false; m_mpegheaderReadPos = 0; m_decodingsize = 0; u8* tempbuf = (u8*)av_malloc(m_bufSize); m_pFormatCtx = avformat_alloc_context(); m_pIOContext = avio_alloc_context(tempbuf, m_bufSize, 0, (void*)this, _MpegReadbuffer, NULL, 0); m_pFormatCtx->pb = m_pIOContext; // Open video file if (avformat_open_input((AVFormatContext**)&m_pFormatCtx, NULL, NULL, NULL) != 0) return false; if (avformat_find_stream_info(m_pFormatCtx, NULL) < 0) { closeContext(); return false; } if (m_videoStream >= (int)m_pFormatCtx->nb_streams) { WARN_LOG_REPORT(ME, "Bad video stream %d", m_videoStream); m_videoStream = -1; } if (m_videoStream == -1) { // Find the first video stream for(int i = 0; i < (int)m_pFormatCtx->nb_streams; i++) { if(m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { m_videoStream = i; break; } } if(m_videoStream == -1) return false; } if (!setVideoStream(m_videoStream, true)) return false; setVideoDim(); m_audioContext = AT3Create(); m_isVideoEnd = false; m_noAudioData = false; m_mpegheaderReadPos++; av_seek_frame(m_pFormatCtx, m_videoStream, 0, 0); #endif // USE_FFMPEG return true; }
void main() { assert(line_line(line(vec2(0, 0), vec2(5, 5)), line(vec2(0, 5), vec2(5, 0))).result == true); assert(line_line(line(vec2(2.5, 0), vec2(2.5, 5)), line(vec2(0, 2.5), vec2(2.5, 5))).result == true); assert(line_rect(line(vec2(0,1),vec2(5,1)),rect(vec2(5,1),vec2(2,2))).result == true); assert(line_rect(line(vec2(1, 0), vec2(1, 5)), rect(vec2(1, 5), vec2(2, 2))).result == true); srand(time(NULL)); auto w = openContext(1280, 800); //size of window auto m = mat4::ortho(-25, 1280, -25, 800, 0, 100); //size of drawing area //drawing area is shrunk or stretchd to the size of the window Graph *g = Graph::genGrid(32,32,1280,800, 5); Solver dfs(g->getNumNodes(), g->getadjacencyMatrix(), g->getPos()); Entity e(200, g->getPos()[0]); Entity lala(100, vec2(400, 400)); float dt, current, total = 0; glfwSetTime(0); int lastGoal = 0; int newGoal = 0; bool newPath = true; std::vector<rect> obstacles; //used to keep track of the obstacles in the graph obstacles.push_back(rect(vec2( 165, 325), vec2( 50, 300))); //left side obstacles.push_back(rect(vec2(1115, 325), vec2( 50, 300))); //right top obstacles.push_back(rect(vec2( 640, 200), vec2(1000, 50))); //bottom obstacles.push_back(rect(vec2( 640, 800), vec2( 300, 300))); //top box obstacles.push_back(rect(vec2( 640, 0), vec2( 300, 150))); //bottom box for each(auto o in obstacles) g->addObstacle(o.min(), o.max()); //opens the game loop while (updateContext(w)) //calls the draw functions { current = glfwGetTime(); dt = current - total; total = current; if (dt > .16) dt = .16; if (newPath) { bool trying = true; lastGoal = newGoal; while (trying) { newGoal = rand() % g->getNumNodes(); //newGoal = 800; for (int i = 0; i < g->getNumNodes(); i++) if (g->getadjacencyMatrix()[newGoal][i] > 0) trying = false; } e.m_path = Solver::solve(g->getNumNodes(), g->getadjacencyMatrix(), g->getPos(), lastGoal, newGoal); e.m_path = dfs.smoothPath(e.m_path, obstacles); lala.m_path = Solver::solve(g->getNumNodes(), g->getadjacencyMatrix(), g->getPos(), lastGoal, newGoal); lala.m_path = dfs.smoothPath(e.m_path, obstacles); newPath = false; } e.update(dt); lala.update(dt); e.draw(m, 7, vec4(1,1,1,1)); lala.draw(m, 7, vec4(0,1,1,0)); for (int j = 0; j < obstacles.size(); j++) { draw_rect(m, rect(obstacles[j]), vec4(0, 0, 1, 0)); } for (int i = 1; i < e.m_path.size(); i++) //draw path { draw_line(m, line(e.m_path[i - 1], e.m_path[i]), vec4(0, 1, 0, 0)); } if (e.m_targetIndex == e.m_path.size()) { e.m_targetIndex = 0; newPath = true; } } //delete g; //closes the game loop closeContext(w); }
int main() { // creates a window and GLES context if (makeContext() != 0) exit(-1); // all the shaders have at least texture unit 0 active so // activate it now and leave it active glActiveTexture(GL_TEXTURE0); flareTex = loadPNG("resources/textures/cloud.png"); width = getDisplayWidth(); height = getDisplayHeight(); glViewport(0, 0, getDisplayWidth(), getDisplayHeight()); // initialises glprint's matrix, shader and texture initGlPrint(getDisplayWidth(), getDisplayHeight()); font1=createFont("resources/textures/font.png",0,256,16,16,16); initSprite(getDisplayWidth(), getDisplayHeight()); fileid = 0; loadfile(); for (int i=0; i<max_flares; i++) { flares[i].x=rand_range(0,getDisplayWidth()); flares[i].y=rand_range(0,getDisplayHeight()); flares[i].vx=rand_range(0,10)-5; flares[i].vy=rand_range(0,10)-5; } /*for (int i=0; i<max_targets; i++) { targets[i].x=rand_range(0,getDisplayWidth()); targets[i].y=rand_range(0,getDisplayHeight()); targets[i].flares = 0; }*/ /*for (int i=0; i<max_flares; i++) { flares[i].x=rand_range(0,getDisplayWidth()); flares[i].y=rand_range(0,getDisplayHeight()); flares[i].vx=rand_range(0,10)-5; flares[i].vy=rand_range(0,10)-5; flares[i].target = (int)rand_range(0,max_targets); targets[flares[i].target].flares ++; }*/ // we don't want to draw the back of triangles // the blending is set up for glprint but disabled // while not in use glCullFace(GL_BACK); glEnable(GL_CULL_FACE); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); glClearColor(0, 0.5, 1, 1); // count each frame int num_frames = 0; // set to true to leave main loop bool quit = false; // get a pointer to the key down array keys = getKeys(); int* mouse = getMouse(); while (!quit) { // the main loop doEvents(); // update mouse and key arrays if (keys[KEY_ESC]) quit = true; // exit if escape key pressed if (keys[KEY_SPACE]) relocate_targets(); // exit if escape key pressed if (keys[KEY_RETURN]) reassign_flares(); // exit if escape key pressed if (keys[KEY_S] && !lastkeys[KEY_S]) save(); // exit if escape key pressed if (keys[KEY_CURSL] && !lastkeys[KEY_CURSL]){ fileid--; if (fileid <0) fileid = 0; loadfile(); // exit if escape key pressed } if (keys[KEY_CURSR] && !lastkeys[KEY_CURSR]){ fileid++; loadfile(); // exit if escape key pressed } if (keys[KEY_C]){ max_targets = 1; // exit if escape key pressed reassign_flares(); } lastkeys[KEY_S] = keys[KEY_S]; lastkeys[KEY_CURSL] = keys[KEY_CURSL]; lastkeys[KEY_CURSR] = keys[KEY_CURSR]; mx = mouse[0]; my = mouse[1]; if(mouse[2] != 0){ spawn_target(mx,my); } for (int i=0; i<RE_MULTIPLY; i++){ random_events(); } think(); render(); // the render loop usleep(1600); // no need to run cpu/gpu full tilt } closeContext(); // tidy up return 0; }
void RendererWidget::closeEvent(QCloseEvent*) { closeContext(); }
int main() { lightDir.x=0.5; lightDir.y=.7; lightDir.z=-0.5; kmVec3Normalize(&lightDir,&lightDir); // creates a window and GLES context if (makeContext() != 0) exit(-1); // all the shaders have at least texture unit 0 active so // activate it now and leave it active glActiveTexture(GL_TEXTURE0); // The obj shapes and their textures are loaded cubeTex = loadPNG("resources/textures/dice.png"); loadObj(&cubeObj, "resources/models/cube.gbo", "resources/shaders/textured.vert", "resources/shaders/textured.frag"); shipTex = loadPNG("resources/textures/shipv2.png"); loadObjCopyShader(&shipObj,"resources/models/ship.gbo",&cubeObj); alienTex = loadPNG("resources/textures/alien.png"); loadObjCopyShader(&alienObj, "resources/models/alien.gbo", &cubeObj); shotTex = loadPNG("resources/textures/shot.png"); loadObjCopyShader(&shotObj, "resources/models/shot.gbo", &cubeObj); expTex = loadPNG("resources/textures/explosion.png"); playerPos.x = 0; playerPos.y = 0; playerPos.z = 0; kmMat4Identity(&view); pEye.x = 0; pEye.y = 2; pEye.z = 4; pCenter.x = 0; pCenter.y = 0; pCenter.z = -5; pUp.x = 0; pUp.y = 1; pUp.z = 0; kmMat4LookAt(&view, &pEye, &pCenter, &pUp); // projection matrix, as distance increases // the way the model is drawn is effected kmMat4Identity(&projection); kmMat4PerspectiveProjection(&projection, 45, (float)getDisplayWidth() / getDisplayHeight(), 0.1, 100); glViewport(0, 0, getDisplayWidth(), getDisplayHeight()); // these two matrices are pre combined for use with each model render kmMat4Assign(&vp, &projection); kmMat4Multiply(&vp, &vp, &view); // initialises glprint's matrix shader and texture initGlPrint(getDisplayWidth(), getDisplayHeight()); font1=createFont("resources/textures/font.png",0,256,16,16,16); font2=createFont("resources/textures/bigfont.png",32,512,9.5,32,48); glCullFace(GL_BACK); glEnable(GL_CULL_FACE); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_BLEND); // only used by glprintf glEnable(GL_DEPTH_TEST); struct timeval t, ta, t1, t2; // fps stuff gettimeofday(&t1, NULL); int num_frames = 0; bool quit = false; mouse = getMouse(); keys = getKeys(); resetAliens(); for (int n = 0; n < MAX_PLAYER_SHOTS; n++) { playerShots[n].alive = false; } initPointClouds("resources/shaders/particle.vert", "resources/shaders/particle.frag",(float)getDisplayWidth()/24.0); for (int n = 0; n < MAX_ALIENS; n++) { aliens[n].explosion=createPointCloud(40); resetExposion(aliens[n].explosion); // sets initials positions } while (!quit) { // the main loop doEvents(); // update mouse and key arrays // mask of 4 is right mouse if (keys[KEY_ESC]) quit = true; glClearColor(0, .5, 1, 1); // render between two gettimeofday calls so // we can sleep long enough to roughly sync // to ~60fps but not on the pi! // TODO find something a tad more elegent long i; gettimeofday(&t, NULL); i = t.tv_sec * 1e6 + t.tv_usec; // render(); float rad; // radians rotation based on frame counter glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); frame++; rad = frame * (0.0175f * 2); kmMat4Identity(&model); kmMat4Translation(&model, playerPos.x, playerPos.y, playerPos.z); playerCroll += (PIDcal(playerRoll, playerCroll, &playerPre_error, &playerIntegral) / 2); kmMat4RotationPitchYawRoll(&model, 0, 3.1416, playerCroll * 3); // kmMat4Assign(&mvp, &vp); kmMat4Multiply(&mvp, &mvp, &model); kmMat4Assign(&mv, &view); kmMat4Multiply(&mv, &mv, &model); glBindTexture(GL_TEXTURE_2D, shipTex); drawObj(&shipObj, &mvp, &mv, lightDir, viewDir); glPrintf(50 + sinf(rad) * 16, 240 + cosf(rad) * 16, font2,"frame=%i fps=%3.2f", frame, lfps); kmVec3 tmp; playerFireCount--; if (keys[KEY_LCTRL] && playerFireCount < 0) { struct playerShot_t *freeShot; freeShot = getFreeShot(); if (freeShot != 0) { playerFireCount = 15; freeShot->alive = true; kmVec3Assign(&freeShot->pos, &playerPos); } } for (int n = 0; n < MAX_PLAYER_SHOTS; n++) { if (playerShots[n].alive) { playerShots[n].pos.z -= .08; if (playerShots[n].pos.z < -10) playerShots[n].alive = false; kmMat4Identity(&model); kmMat4Translation(&model, playerShots[n].pos.x, playerShots[n].pos.y, playerShots[n].pos.z); kmMat4RotationPitchYawRoll(&model, rad * 4, 0, -rad * 4); kmMat4Assign(&mvp, &vp); kmMat4Multiply(&mvp, &mvp, &model); kmMat4Assign(&mv, &view); kmMat4Multiply(&mv, &mv, &model); glBindTexture(GL_TEXTURE_2D, shotTex); drawObj(&shotObj, &mvp, &mv, lightDir, viewDir); } } playerRoll = 0; if (keys[KEY_CURSL] && playerPos.x > -10) { playerPos.x -= 0.1; playerRoll = .2; } if (keys[KEY_CURSR] && playerPos.x < 10) { playerPos.x += 0.1; playerRoll = -.2; } pEye.x = playerPos.x * 1.25; pCenter.x = playerPos.x; pCenter.y = playerPos.y + 1; pCenter.z = playerPos.z; int deadAliens; deadAliens = 0; for (int n = 0; n < MAX_ALIENS; n++) { if (aliens[n].alive == true) { kmMat4Identity(&model); kmMat4Translation(&model, aliens[n].pos.x, aliens[n].pos.y, aliens[n].pos.z); kmMat4RotationPitchYawRoll(&model, -.4, 0, 0); kmMat4Assign(&mvp, &vp); kmMat4Multiply(&mvp, &mvp, &model); kmMat4Assign(&mv, &view); kmMat4Multiply(&mv, &mv, &model); glBindTexture(GL_TEXTURE_2D, alienTex); drawObj(&alienObj, &mvp, &mv, lightDir, viewDir); kmVec3 d; for (int i = 0; i < MAX_PLAYER_SHOTS; i++) { kmVec3Subtract(&d, &aliens[n].pos, &playerShots[i].pos); if (kmVec3Length(&d) < .7 && playerShots[i].alive) { aliens[n].alive = false; playerShots[i].alive = false; aliens[n].exploding = true; resetExposion(aliens[n].explosion); } } } if (aliens[n].alive != true && aliens[n].exploding != true) { deadAliens++; } } if (deadAliens == MAX_ALIENS) { resetAliens(); } // draw explosions after ALL aliens for (int n = 0; n < MAX_ALIENS; n++) { if (aliens[n].exploding==true) { kmMat4Identity(&model); kmMat4Translation(&model, aliens[n].pos.x, aliens[n].pos.y, aliens[n].pos.z); kmMat4Assign(&mvp, &vp); kmMat4Multiply(&mvp, &mvp, &model); glBindTexture(GL_TEXTURE_2D, expTex); drawPointCloud(aliens[n].explosion, &mvp); aliens[n].explosion->tick=aliens[n].explosion->tick+0.05; if (aliens[n].explosion->tick>1.25) { aliens[n].exploding=false; } else { // update the explosion for (int i=0; i<aliens[n].explosion->totalPoints; i++) { float t; t=aliens[n].explosion->tick; if (i>aliens[n].explosion->totalPoints/2) t=t/2.0; aliens[n].explosion->pos[i*3]=aliens[n].explosion->vel[i*3] * t; aliens[n].explosion->pos[i*3+1]=aliens[n].explosion->vel[i*3+1] * t; aliens[n].explosion->pos[i*3+2]=aliens[n].explosion->vel[i*3+2] * t; } } } } // move camera kmMat4LookAt(&view, &pEye, &pCenter, &pUp); kmMat4Assign(&vp, &projection); kmMat4Multiply(&vp, &vp, &view); kmVec3Subtract(&viewDir,&pEye,&pCenter); kmVec3Normalize(&viewDir,&viewDir); // dump values glPrintf(100, 280, font1,"eye %3.2f %3.2f %3.2f ", pEye.x, pEye.y, pEye.z); glPrintf(100, 296, font1,"centre %3.2f %3.2f %3.2f ", pCenter.x, pCenter.y, pCenter.z); glPrintf(100, 320, font1,"mouse %i,%i %i ", mouse[0], mouse[1], mouse[2]); glPrintf(100, 340, font1,"frame %i %i ", frame, frame % 20); swapBuffers(); gettimeofday(&ta, NULL); long j = (ta.tv_sec * 1e6 + ta.tv_usec); i = j - i; if (i < 0) i = 1000000; // pass through - slower that 60fps if (i < 16000) usleep(16000 - i); // every 10 frames average the time taken and store // fps value for later printing with glprintf if (++num_frames % 10 == 0) { gettimeofday(&t2, NULL); float dtf = t2.tv_sec - t1.tv_sec + (t2.tv_usec - t1.tv_usec) * 1e-6; lfps = num_frames / dtf; num_frames = 0; t1 = t2; } } closeContext(); return 0; }