HitResult StarPuzzle::hitTest(const CCPoint& point) { int row = std::max(0, (int)((point.y - m_bottom) / STAR_HEIGHT)); int col = std::max(0, (int)((point.x - m_left) / STAR_WIDTH)); int index = row * m_col + col; //CCLog("point %0.2f, %0.2f, row %d, col %d", point.x, point.y, row, col); HitResult result; if (index < m_size && m_stars[index] != 0) { if (m_selectedStarIndex.find(index) != m_selectedStarIndex.end()) { m_changes = m_solver->step(m_matrix, index); result.type = HitResult::HIT_POPSTAR; result.data = m_selectedStarIndex.size(); result.location = m_stars[index]->getPosition(); } else { std::set<int> adjacent = m_solver->adjacent(m_matrix, index); doUnSelection(); if (adjacent.size() <= 1) { result.type = HitResult::HIT_NOTHING; result.data = 0; } else { doSelection(adjacent); result.type = HitResult::HIT_STAR; result.data = m_selectedStarIndex.size(); } } } else { doUnSelection(); result.type = HitResult::HIT_NOTHING; result.data = 0; } return result; }
HitResult StarPuzzle::hitTestAuto() { int index; if (m_selectedStarIndex.size() <= 0) index = m_solver->next(m_matrix); else index = *m_selectedStarIndex.begin(); HitResult result; if (index < m_size && m_stars[index] != 0) { if (m_selectedStarIndex.find(index) != m_selectedStarIndex.end()) { m_changes = m_solver->step(m_matrix, index); result.type = HitResult::HIT_POPSTAR; result.data = m_selectedStarIndex.size(); result.location = m_stars[index]->getPosition(); } else { std::set<int> adjacent = m_solver->adjacent(m_matrix, index); doUnSelection(); if (adjacent.size() <= 1) { m_selectedStarIndex.clear(); result.type = HitResult::HIT_NOTHING; result.data = 0; } else { doSelection(adjacent); result.type = HitResult::HIT_STAR; result.data = m_selectedStarIndex.size(); } } } else { result.type = HitResult::HIT_NOTHING; result.data = 0; } return result; }
void main() { ofstream outfile("fence8.out"); init(); doSelection(); outfile << ir << endl; }
HitResult StarPuzzle::autoSelect() { int index = m_solver->next(m_matrix); HitResult result; std::set<int> adjacent = m_solver->adjacent(m_matrix, index); doUnSelection(); if (adjacent.size() <= 1) { m_selectedStarIndex.clear(); result.type = HitResult::HIT_NOTHING; result.data = 0; } else { doSelection(adjacent); result.type = HitResult::HIT_STAR; result.data = m_selectedStarIndex.size(); } }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->actionBackground, SIGNAL(triggered()), this, SLOT(background_file_open())); connect(ui->actionClosed_Aperature, SIGNAL(triggered()), this, SLOT(closedapp_file_open())); connect(ui->actionImage_Stack, SIGNAL(triggered()), this, SLOT(imagelist_file_open())); connect(ui->actionRun_Experiment, SIGNAL(triggered()), this, SLOT(run_experiment())); set_background = false; set_closed = false; set_image_list = false; ui->actionClosed_Aperature->setDisabled(true); ui->actionImage_Stack->setDisabled(true); ui->actionRun_Experiment->setDisabled(true); ui->actionShow_Graph->setDisabled(true); frapmodel = new FrapModel(0); connect(this,SIGNAL(primaset(QString)),frapmodel,SLOT(setPrima(QString))); connect(this,SIGNAL(closedset(QString)),frapmodel,SLOT(setClosed(QString))); connect(this,SIGNAL(doselection()),frapmodel,SLOT(doSelection())); connect(this,SIGNAL(imagelistset(QStringList)),frapmodel,SLOT(setImageList(QStringList))); connect(ui->actionShow_Graph, SIGNAL(triggered()),frapmodel, SLOT(prepareLinearFit())); //connect(frapmodel,SIGNAL(dataChanged(QModelIndex,QModelIndex)), ui->tableView,SLOT(dataChanged(QModelIndex,QModelIndex))); connect(frapmodel,SIGNAL(update_result(QString)), this, SLOT(show_result(QString))); connect(frapmodel,SIGNAL(plotLinearFit(int,std::vector<double>&,std::vector<double>&,std::vector<double>&,double,double)), ui->pl_widget, SLOT(plotLinearFit(int,std::vector<double>&,std::vector<double>&,std::vector<double>&,double,double))); ui->tabWidget->setCurrentIndex(0); starting_dir = "/home/jon/Programming/C/frap-tool-old"; }
// the main function int main (int argc, char **argv) { Init(); SetupCubeMap(); // load our shaders and compile them.. create a program and link it GLuint glShaderV = glCreateShader(GL_VERTEX_SHADER); GLuint glShaderF = glCreateShader(GL_FRAGMENT_SHADER); const GLchar* vShaderSource = loadFile("skybox.vert.hlsl"); const GLchar* fShaderSource = loadFile("skybox.frag.hlsl"); glShaderSource(glShaderV, 1, &vShaderSource, NULL); glShaderSource(glShaderF, 1, &fShaderSource, NULL); glCompileShader(glShaderV); CheckShaderCompilation(glShaderV); glCompileShader(glShaderF); CheckShaderCompilation(glShaderF); delete[] vShaderSource; delete[] fShaderSource; GLuint cubeMapProg = glCreateProgram(); glAttachShader(cubeMapProg, glShaderV); glAttachShader(cubeMapProg, glShaderF); glLinkProgram(cubeMapProg); glUseProgram(cubeMapProg); int vlength, flength; char vlog[2048], flog[2048]; glGetShaderInfoLog(glShaderV, 2048, &vlength, vlog); glGetShaderInfoLog(glShaderF, 2048, &flength, flog); std::cout << vlog << std::endl << std::endl << flog << std::endl << std::endl; // load shaders for the keyboard geometry glShaderV = glCreateShader (GL_VERTEX_SHADER); glShaderF = glCreateShader (GL_FRAGMENT_SHADER); const GLchar * mVSource = loadFile ("model.vert.hlsl"); const GLchar * mFSource = loadFile ("model.frag.hlsl"); glShaderSource(glShaderV, 1, &mVSource, NULL); glShaderSource(glShaderF, 1, &mFSource, NULL); delete [] mVSource; delete [] mFSource; glCompileShader(glShaderV); CheckShaderCompilation(glShaderV); glCompileShader(glShaderF); CheckShaderCompilation(glShaderF); GLuint modelProg = glCreateProgram(); glAttachShader(modelProg, glShaderV); glAttachShader(modelProg, glShaderF); glLinkProgram(modelProg); glUseProgram(modelProg); glGetShaderInfoLog(glShaderV, 2048, &vlength, vlog); glGetShaderInfoLog(glShaderF, 2048, &flength, flog); std::cout << vlog << std::endl << std::endl << flog << std::endl << std::endl; // load texture with letters on keyboard SDL_Surface *keySurface = IMG_Load ("keys.png"); // I made this image with GIMP GLuint keys_texture; glActiveTexture (GL_TEXTURE0); glEnable (GL_TEXTURE_2D); glGenTextures (1, &keys_texture); glBindTexture (GL_TEXTURE_2D, keys_texture); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, keySurface->w, keySurface->h, 0, keySurface->format->BytesPerPixel == 4? GL_RGBA: GL_RGB, GL_UNSIGNED_BYTE, keySurface->pixels); glBindTexture (GL_TEXTURE_2D, 0); SDL_FreeSurface (keySurface); GLint uniform_keys = glGetUniformLocation (modelProg, "keys"); // load premade bitmap font // http://www.amanithvg.com/testsuite/amanithvg_sre/data/font_bitmap.png SDL_Surface *bmf = IMG_Load ("font_bitmap.png"); GLuint bmf_texture; glActiveTexture(GL_TEXTURE0); glEnable (GL_TEXTURE_2D); glGenTextures (1, &bmf_texture); glBindTexture (GL_TEXTURE_2D, bmf_texture); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, bmf->w, bmf->h, 0, bmf->format->BytesPerPixel == 4? GL_RGBA: GL_RGB, GL_UNSIGNED_BYTE, bmf->pixels); glBindTexture (GL_TEXTURE_2D, 0); SDL_FreeSurface (bmf); // grab the pvm matrix and vertex location from our shader program GLint PVM = glGetUniformLocation(cubeMapProg, "PVM"); GLint vertex = glGetAttribLocation(cubeMapProg, "vertex"); // these won't change for now glm::mat4 Projection = glm::perspective(45.0f, (float)WIDTH / (float)HEIGHT, 0.1f, 100.0f); glm::mat4 View = glm::mat4(1.0f); glm::mat4 Model = glm::scale(glm::mat4(1.0f),glm::vec3(50,50,50)); // cube vertices for vertex buffer object GLfloat cube_vertices[] = { -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, }; // vertex array buffer GLuint vbo_cube_vertices; glGenBuffers(1, &vbo_cube_vertices); glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices); glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW); glEnableVertexAttribArray(vertex); glVertexAttribPointer(vertex, 3, GL_FLOAT, GL_FALSE, 0, 0); // cube indices for index buffer object GLushort cube_indices[] = { 0, 1, 2, 3, 3, 2, 6, 7, 7, 6, 5, 4, 4, 5, 1, 0, 0, 3, 7, 4, 1, 2, 6, 5, }; // index array buffer GLuint ibo_cube_indices; glGenBuffers(1, &ibo_cube_indices); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cube_indices); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_indices), cube_indices, GL_STATIC_DRAW); // vars float alpha = 0.f, beta = 0.f, gamma = 50.f; int locX = 0, locY = 0, pressedX = 0, pressedY = 0; float origX = 0.f, origY = 0.f; bool done = false; bool mouse_down = false; char * ch = NULL; unsigned int num_ticks = 0; // for handling events SDL_Event event; // the game loop while(!done) { // get the current time... might be useful. num_ticks = SDL_GetTicks(); // process inputs while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: done = true; break; case SDL_MOUSEMOTION: locX = event.motion.x; locY = event.motion.y; break; case SDL_MOUSEBUTTONDOWN: mouse_down = true; pressedX = event.button.x; pressedY = event.button.y; switch (event.button.button) { case SDL_BUTTON_LEFT: doSelection(event.button.x, HEIGHT - event.button.y); break; } break; case SDL_MOUSEBUTTONUP: if (mouse_down) { mouse_down = false; origY = alpha; origX = beta; } switch (event.button.button) { case SDL_BUTTON_WHEELUP: gamma += 10.0; break; case SDL_BUTTON_WHEELDOWN: gamma -= 10.0; if (gamma < 10.0) gamma = 10.0; break; case SDL_BUTTON_LEFT: //just set all keys back to up for (int i = 0; i < KEYDOWN_ARRAY_SIZE; ++i) g_keysdown_arr[i] = false; break; } break; case SDL_KEYDOWN: ch = SDL_GetKeyName(event.key.keysym.sym); g_keysdown_arr[(unsigned char)*ch] = true; g_text.push_back(*ch); break; case SDL_KEYUP: ch = SDL_GetKeyName(event.key.keysym.sym); g_keysdown_arr[(unsigned char)*ch] = false; break; default: break; } } // do updates if (mouse_down) { alpha = origY + locY - pressedY; beta = origX + locX - pressedX; } // this should add a subtle sensation of movement to the scene //alpha += .005*sin(0.0005*(double)num_ticks); //beta += .005*cos(0.0005*(double)num_ticks); // this marks the start of the code that draws glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // set the program for the cubemap glUseProgram(cubeMapProg); // uniforms for sky box include the sampler and the MVP matrix glActiveTexture(GL_TEXTURE0); glEnable(GL_TEXTURE_CUBE_MAP); glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap_texture); // set up the ModelViewProj matrix for the cube map glm::mat4 RotateX = glm::rotate(glm::mat4(1.0f), alpha, glm::vec3(-1.0f, 0.0f, 0.0f)); glm::mat4 RotateY = glm::rotate(RotateX, beta, glm::vec3(0.0f, 1.0f, 0.0f)); glm::mat4 M = Projection * View * Model * RotateY; glUniformMatrix4fv(PVM, 1, GL_FALSE, glm::value_ptr(M)); // bind vertex array and index array glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices); glEnableVertexAttribArray(vertex); glVertexAttribPointer(vertex, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cube_indices); // draw cubemap glDrawElements(GL_QUADS, sizeof(cube_indices)/sizeof(GLushort), GL_UNSIGNED_SHORT, 0); // clean up OpenGL state for cubemap glDisable (GL_TEXTURE_CUBE_MAP); glDisableVertexAttribArray (vertex); // clear depth buffer because sky box is infinitely far away glClear (GL_DEPTH_BUFFER_BIT); // use program for rendering keyboard glUseProgram(modelProg); // enable blending for translucency glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // enable back face culling to make translucent keys look better glEnable (GL_CULL_FACE); glCullFace (GL_BACK); // set up projection matrix for keyboard glMatrixMode (GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, (float)WIDTH/(float)HEIGHT, 0.1, 1000.0); // set up modelview matrix for keyboard glMatrixMode (GL_MODELVIEW); glLoadIdentity(); gluLookAt (0.f, 0.f, gamma, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f); glRotatef (-alpha, 1, 0, 0); glRotatef (beta, 0, 1, 0); // set the texture for the keys glActiveTexture (GL_TEXTURE0); glEnable (GL_TEXTURE_2D); glBindTexture (GL_TEXTURE_2D, keys_texture); glUniform1i (uniform_keys, 0); // draw the keyboard drawKeyboard(); // clean up OpenGL state for keyboard glDisable (GL_BLEND); glDisable (GL_CULL_FACE); // now we print the text onto the screen // clear depth again glClear (GL_DEPTH_BUFFER_BIT); // render text to screen using orthographic projection glMatrixMode (GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D (0, WIDTH, 0, HEIGHT); // scale and translate to get origin in upper left hand corner of screen glScalef(1, -1, 1); glTranslatef(0, -HEIGHT, 0); glMatrixMode (GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glUseProgram(0); // using fixed function for the text, shaders not necessary // draw a translucent text box at the top of the screen glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBegin (GL_QUADS); glColor4f (1.0, 1.0, 1.0, 0.5); glVertex2f (0.0, 0.0); glVertex2f (0.0, 100.0); glVertex2f (WIDTH, 100.0); glVertex2f (WIDTH, 0.0); glEnd (); glDisable (GL_BLEND); // draw the text glActiveTexture (GL_TEXTURE0); glEnable (GL_TEXTURE_2D); glBindTexture (GL_TEXTURE_2D, bmf_texture); glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); renderText(); glMatrixMode (GL_PROJECTION); glPopMatrix (); glMatrixMode (GL_MODELVIEW); glPopMatrix(); glDisable (GL_TEXTURE_2D); glDisable (GL_BLEND); // finally, swap buffers SDL_GL_SwapBuffers(); } return 0; }
void GraphWidget::doCtrlPtSelection(const int iMouseX, const int iMouseY) { doSelection(iMouseX, iMouseY); }
//---------------------------------------------------------------------------------------------------------------------- void NGLDraw::mousePressEvent (const SDL_MouseButtonEvent &_event) { // this method is called when the mouse button is pressed in this case we // store the value where the maouse was clicked (x,y) and set the Rotate flag to true if(m_gameState != gameMenu && _event.button == SDL_BUTTON_MIDDLE) { m_origXPos = _event.x; m_origYPos = _event.y; m_translate=true; } if(_event.button == SDL_BUTTON_LEFT) { unsigned int numberOfButtons = m_buttons.size(); for (unsigned int i = 0; i < numberOfButtons; i++) { if (m_buttons[i]->isClicked(_event.x, _event.y) && m_buttons[i]->getIsActive() == true) { switch (m_buttons[i]->getName()) { case buttonPlay: { startGame(m_selectedLevel); m_gameState = gamePlay; m_buttonQuit->setIsActive(false); m_buttonLevel1->setIsActive(false); m_buttonLevel2->setIsActive(false); m_buttonLevel3->setIsActive(false); m_buttonLevel4->setIsActive(false); m_buttonLevel5->setIsActive(false); m_buttonPlay->setIsActive(false); m_buttonPause->setIsActive(true); m_buttonMenu->setIsActive(true); m_sliderSquadSize->setIsActive(true); m_buttonCreateSquad->setIsActive(true); break; } case buttonLevel1 : { m_selectedLevel = 1; break; } case buttonLevel2 : { m_selectedLevel = 2; break; } case buttonLevel3 : { m_selectedLevel = 3; break; } case buttonLevel4 : { m_selectedLevel = 4; break; } case buttonLevel5 : { m_selectedLevel = 5; break; } case buttonQuit : { m_gameState = gameQuit; break; } case buttonPause : { if (m_gameState == gamePlay) { m_gameState = gamePause; m_gameTimer.resetTimer(); m_buttonPause->updateButton(ngl::Vec2(0.9f, 0.95f), ngl::Vec2(0.2f, 0.1f), ngl::Vec4(0.2f, 0.2f, 0.9f, 1.f)); m_buttonPause->updateText("Play", ngl::Vec3(1.f, 1.f, 1.f), ngl::Vec2(-32.f, -17.f)); m_sliderSquadSize->setIsActive(false); m_buttonCreateSquad->setIsActive(false); break; } else if (m_gameState == gamePause) { m_gameState = gamePlay; m_gameTimer.resetTimer(); m_buttonPause->updateButton(ngl::Vec2(0.9f, 0.95f), ngl::Vec2(0.2f, 0.1f), ngl::Vec4(0.2f, 0.2f, 0.9f, 1.f)); m_buttonPause->updateText("Pause", ngl::Vec3(1.f, 1.f, 1.f), ngl::Vec2(-52.f, -17.f)); m_sliderSquadSize->setIsActive(true); m_buttonCreateSquad->setIsActive(true); break; } break; } case buttonMenu : { m_gameState = gameMenu; endGame(); m_buttonQuit->setIsActive(true); m_buttonPlay->setIsActive(true); m_buttonMenu->setIsActive(false); m_buttonPause->setIsActive(false); m_sliderSquadSize->setIsActive(false); m_buttonCreateSquad->setIsActive(false); m_buttonLevel1->setIsActive(true); m_buttonLevel2->setIsActive(true); m_buttonLevel3->setIsActive(true); m_buttonLevel4->setIsActive(true); m_buttonLevel5->setIsActive(true); break; } case buttonCreateSquad : { m_gameworld->createSquad(m_squadSize); break; } case buttonSquadPatrol : { m_selectedSquad->setSquadState("patrol", squadPatrol); m_selectedSquad->setSquadColour(m_patrolColour); m_selectedSquad->setSquadSelectedColour(m_patrolColour*2.0); break; } case buttonSquadAggressive : { m_selectedSquad->setSquadState("aggressive", squadAggressive); m_selectedSquad->setSquadColour(m_aggressiveColour); m_selectedSquad->setSquadSelectedColour(m_aggressiveColour*2.0); break; } case buttonSquadDefensive : { m_selectedSquad->setSquadState("defensive", squadDefensive); m_selectedSquad->setSquadColour(m_defensiveColour); m_selectedSquad->setSquadSelectedColour(m_defensiveColour*2.0); break; } case buttonSquadWall : { m_selectedSquad->setSquadState("wall", squadWall); m_selectedSquad->setSquadColour(m_wallColour); m_selectedSquad->setSquadSelectedColour(m_wallColour*2.0); break; } default: { break; } } return; } } if (m_sliderSquadSize->isClicked(_event.x, _event.y) && m_sliderSquadSize->getIsActive() == true) { m_sliderSquadSize->setIsSliding(true); m_squadSize = m_sliderSquadSize->slideBar(_event.x); m_ss.str(std::string()); m_ss << m_squadSize; m_squadSizeString = m_ss.str(); m_sliderSquadSize->setTextString(m_squadSizeString); return; } if (m_gameState == gamePlay) { doSelection(_event.x, _event.y); } } if(_event.button == SDL_BUTTON_RIGHT) { if (m_gameState == gamePlay) { if (m_selected) { m_selectedSquad->setSquadDrawColour(m_selectedSquad->getSquadColour()); m_selected = false; m_selectedSquad = NULL; m_selectedSquadID = -1; } } } }