Exemple #1
0
void t_pgrid::run()
{
    bool quit= false;
    
    setvbuf(stdout, 0, _IONBF, 0);
    
    SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
    SDL_WM_SetCaption("pgrid", "pgrid");
    
    lastmillis= curmillis= SDL_GetTicks();
    SDL_ShowCursor(0);
    SDL_EnableUNICODE(true);
    
    gui= new flux_gui();
    
    bool guimouse= false;
    
    while(!quit)
    {
	lastframephystime= physicstime;
	lastmillis= curmillis;
	curmillis= SDL_GetTicks();
	
	SDL_Event event;
	while(SDL_PollEvent(&event))
	{
	    switch(event.type)
	    {
		case SDL_KEYDOWN:
		case SDL_KEYUP:
		{
		    if(event.key.keysym.sym==SDLK_ESCAPE) quit= true;
		    
		    if(wnd_getkbdfocus()!=NOWND)
		    {
			flux_keyboard_event(event.type==SDL_KEYDOWN, event.key.keysym.sym, event.key.keysym.unicode&0x7F);
		    }
		    else
		    {
			current_workspace()->keyboard_event(event.key);
			editor->keyboard_event(event.key);
			for(t_dyninputhandler_list::iterator i= dyninputhandlers.begin(); i!=dyninputhandlers.end(); i++)
			    (*i)->keyboard_event(event.key);
		    }
		    
		    keystate[event.key.keysym.sym]= (event.key.state==SDL_PRESSED? 1: 0);
		    break;
		}
		
		case SDL_MOUSEMOTION:
		    flux_mouse_move_event(event.motion.xrel, event.motion.yrel);
		    
		    if( find_prim_pos(event.motion.x, event.motion.y, true) || 
			find_mouse_handler(event.motion.x, event.motion.y) )
			guimouse= true;
		    else
		    {
			guimouse= false;
			mouse.x= event.motion.x;
			mouse.y= event.motion.y;
			mouse.buttons= event.motion.state;
			current_workspace()->mouse_motion_event(event.motion);
			editor->mouse_motion_event(event.motion);
			for(t_dyninputhandler_list::iterator i= dyninputhandlers.begin(); i!=dyninputhandlers.end(); i++)
			    (*i)->mouse_motion_event(event.motion);
		    }
		    break;
		
		case SDL_MOUSEBUTTONUP:
		case SDL_MOUSEBUTTONDOWN:
		{
		    int b= event.button.button;
		    int fluxbutton= (b==SDL_BUTTON_RIGHT? 2: b==SDL_BUTTON_MIDDLE? 3: b)-1;
		    flux_mouse_button_event(fluxbutton, event.type==SDL_MOUSEBUTTONDOWN);
		    
		    if( find_prim_pos(event.button.x, event.button.y, true) || 
			find_mouse_handler(event.button.x, event.button.y) )
		    {
		    	guimouse= true;
		    }
		    else
		    {
			guimouse= false;
			wnd_setkbdfocus(NOWND);
			if(event.type==SDL_MOUSEBUTTONUP)
			    mouse.buttons&= ~(SDL_BUTTON(event.button.button));
			else
			    mouse.buttons|= SDL_BUTTON(event.button.button);
			mouse.x= event.button.x;
			mouse.y= event.button.y;
			current_workspace()->mouse_button_event(event.button);
			editor->mouse_button_event(event.button);
			for(t_dyninputhandler_list::iterator i= dyninputhandlers.begin(); i!=dyninputhandlers.end(); i++)
			    (*i)->mouse_button_event(event.button);
		    }
		    break;
		}

		case SDL_QUIT:
		    quit= true;
		    break;
		
		case SDL_VIDEORESIZE:
		{
		    SDL_Surface *surf= SDL_SetVideoMode(event.resize.w, event.resize.h, 0, SDL_OPENGL|SDL_RESIZABLE);
		    if(!surf) break;
		    screen.sdl_surface= surf;
		    screen.gl_init(event.resize.w, event.resize.h);
		    current_workspace()->centerimage();
		    break;
		}
		
		case SDL_ACTIVEEVENT:
		{
		    if(event.active.state&SDL_APPINPUTFOCUS)
		    {
			hasinputfocus= event.active.gain;
			SDL_ShowCursor(!hasinputfocus);
		    }
		    if(event.active.state&SDL_APPMOUSEFOCUS)
		    	hasmousefocus= event.active.gain;
		    break;
		}
		
		case SDL_VIDEOEXPOSE:
		{
		    if(!hasinputfocus)
		    {
    			glClear(GL_DEPTH_BUFFER_BIT);
			current_workspace()->render();
			SDL_GL_SwapBuffers();
			checkglerror();
		    }
		    break;
		}
	    }
	}
	
	int x, y;
	SDL_GetMouseState(&x, &y);
	mousepos.x= x; mousepos.y= y;

	SDL_GetRelativeMouseState(&x, &y);
	mousevel.x= x; mousevel.y= y;
	mousevel.mul( 1000.0/(curmillis-lastmillis) );
	
	current_workspace()->image->frame_tick((curmillis-lastmillis)*0.001);
	runphysics();
	

    	if(hasinputfocus)
	{
	    glClear(GL_DEPTH_BUFFER_BIT);
	    //~ screen.setperspective();
	    current_workspace()->render();
	    if(hasmousefocus && !guimouse) editor->render();

#if 1
	    next_event();
	    //~ aq_exec();
	    run_timers();
	    
	    glMatrixMode(GL_MODELVIEW);
	    glPushMatrix();
	    glLoadIdentity();
	    
	    glMatrixMode(GL_PROJECTION);
	    glPushMatrix();
	    glLoadIdentity();
	    glOrtho(0, screen.width,screen.height, 0, -1000, 1000);

	    glDisable(GL_DEPTH_TEST);
	    glEnable(GL_BLEND);
	    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	    //~ glEnable(GL_POINT_SMOOTH);
	    //~ glPointSize(1.0);
	    
	    //~ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	    //~ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	    glColor3f(1,1,1);
	    glDisable(GL_TEXTURE_2D);
	    
	    redraw_rect(&viewport);
	    //~ cliptext(0,0,0,0);
	    update_rect(&viewport);

	    glEnable(GL_DEPTH_TEST);
	    glDisable(GL_BLEND);
	    
	    glMatrixMode(GL_PROJECTION);
	    glPopMatrix();
	    glMatrixMode(GL_MODELVIEW);
	    glPopMatrix();
#endif
	    
	    gui->tick( (curmillis-lastmillis)*0.001 );
	    
	    if(guimouse) { glMatrixMode(GL_TEXTURE); glLoadIdentity(); glDisable(GL_DEPTH_TEST); redraw_cursor(); }
	    
	    SDL_GL_SwapBuffers();
	    checkglerror();
	}
	
	
	int delay= 10 - (curmillis-lastmillis);
	if(!hasinputfocus) SDL_Delay(100);
	if(delay>0) SDL_Delay(delay);
    }
    
    SDL_WM_GrabInput(SDL_GRAB_OFF);
    
    delete gui;
    gui= 0;
}
Exemple #2
0
    // internal initialization. performed when run is invoked
    void Raspi_App::init()
    {
        gettimeofday(&m_startTime, NULL);

        esInitContext(m_context.get());
        esCreateWindow(m_context.get(), name().c_str(),
                       ES_WINDOW_RGB | ES_WINDOW_ALPHA | ES_WINDOW_DEPTH /*| ES_WINDOW_MULTISAMPLE*/);

        // set graphical log stream
        Logger::get()->add_outstream(&m_outstream_gl);

        // version
        LOG_INFO<<"OpenGL: " << glGetString(GL_VERSION);
        LOG_INFO<<"GLSL: " << glGetString(GL_SHADING_LANGUAGE_VERSION);

        set_window_size(gl::vec2(m_context->width, m_context->height));
        LOG_INFO<<"Context: " << gl::window_dimension().x << " x " << gl::window_dimension().y;

        // file search paths
        if(!args().empty()){ fs::add_search_path(fs::get_directory_part(args().front())); }
        // fs::add_search_path("./");
        fs::add_search_path("./res");
        fs::add_search_path("/usr/local/share/fonts");

        get_input_file_descriptors(&m_mouse_fd, &m_keyboard_fd, &m_touch_fd);

        m_timer_device_scan = Timer(background_queue().io_service(), [this]()
        {
            int mouse_fd = m_mouse_fd, keyboard_fd = m_keyboard_fd;
            int *mp = nullptr, *kp = nullptr;
            bool has_changed = false;

            // check for keyboard and mouse being added/removed
            auto mouse_handler = find_mouse_handler();
            auto kb_handler = find_keyboard_handler();

            if((!m_keyboard_fd && !kb_handler.empty()) ||
               (m_keyboard_fd && kb_handler.empty()))
            {
                keyboard_fd = 0;
                kp = &keyboard_fd;
                has_changed = true;
            }
            if((!m_mouse_fd && !mouse_handler.empty()) ||
               (m_mouse_fd && mouse_handler.empty()))
            {
                mouse_fd = 0;
                mp = &mouse_fd;
                has_changed = true;
            }
            if(!has_changed){ return; }

            get_input_file_descriptors(mp, kp, nullptr);
            main_queue().submit([this, mouse_fd, keyboard_fd]
            {
                if(mouse_fd && !m_mouse_fd)
                {
                    LOG_TRACE << "mouse connected";
                    m_mouse_fd = mouse_fd;
                }
                else if(!mouse_fd && m_mouse_fd)
                {
                     LOG_TRACE << "mouse disconnected";
                     close(m_mouse_fd); m_mouse_fd = 0;
                }
                if(keyboard_fd && !m_keyboard_fd)
                {
                    LOG_TRACE << "keyboard connected";
                    m_keyboard_fd = keyboard_fd;
                }
                else if(!keyboard_fd && m_keyboard_fd)
                {
                    LOG_TRACE << "keyboard disconnected";
                    close(m_keyboard_fd); m_keyboard_fd = 0;
                }
            });
        });
        m_timer_device_scan.set_periodic();
        m_timer_device_scan.expires_from_now(5.0);

        // make sure touchscreen backlight stays on
        // TODO: use timer here
        // set_lcd_backlight(true);

        // center cursor
        current_mouse_pos = gl::window_dimension() / 2.f;

        // user setup hook
        setup();
    }