Пример #1
0
static void process_events( void )
{
    /* Our SDL event placeholder. */
    SDL_Event event;

    /* Grab all the events off the queue. */
    while( SDL_PollEvent( &event ) ) {

        switch( event.type ) {
        case SDL_MOUSEMOTION:
            s.pos.x =  30*( event.motion.x - width /2) / (double)width;
            s.pos.y = -12*( event.motion.y - height/2) / (double)height;
            printf("%f %f\n",s.pos.x,s.pos.y);
            break;

        case SDL_KEYDOWN:
            /* Handle key presses. */
            handle_key_down( &event.key.keysym );
            break;

        case SDL_QUIT:
            /* Handle quit requests (like Ctrl-c). */
            quit_tutorial( 0 );
            break;
        }

    }
}
Пример #2
0
static void process_events( void )
{
    /* Our SDL event placeholder. */
    SDL_Event event;

    /* Grab all the events off the queue. */
    while( SDL_PollEvent( &event ) ) {

        switch( event.type ) {
        case SDL_KEYUP:
            handle_key_up( &event.key.keysym );
			break;

        case SDL_KEYDOWN:
            /* Handle key presses. */
            handle_key_down( &event.key.keysym );
            break;

        case SDL_QUIT:
            /* Handle quit requests (like Ctrl-c). */
            quit_tutorial( 0 );
            break;
        }

    }
}
Пример #3
0
static void handle_key_down( SDL_keysym* keysym )
{
    switch( keysym->sym ) {
    case SDLK_ESCAPE:
        quit_tutorial( 0 );
        break;

	case SDLK_SPACE:
		s.expand();
		s.expand();
		s.expand();
		s.expand();
		s.expand();
		s.expand();
		s.expand();
		s.expand();
		s.expand();
		s.expand();
		break;

    default:
        break;
    }

}
Пример #4
0
static void handle_key_down( SDL_keysym* keysym )
{
    switch( keysym->sym ) {
    case SDLK_ESCAPE:
        quit_tutorial( 0 );
        break;

    case SDLK_LEFT:
		if( tim_x.running() )
			{
			tim_x.pause();
			}
		else
			{
			tim_x.count_up();
			tim_x.resume();
			}
        break;

    case SDLK_RIGHT:
		if( tim_x.running() )
			{
			tim_x.pause();
			}
		else
			{
			tim_x.count_down();
			tim_x.resume();
			}
        break;

    case SDLK_DOWN:
		if( tim_y.running() )
			{
			tim_y.pause();
			}
		else
			{
			tim_y.count_up();
			tim_y.resume();
			}
        break;

    case SDLK_UP:
		if( tim_y.running() )
			{
			tim_y.pause();
			}
		else
			{
			tim_y.count_down();
			tim_y.resume();
			}
		break;

    default:
        break;
    }

}
Пример #5
0
static void process_events( void )
{
    /* Our SDL event placeholder. */
    SDL_Event event;

    /* Grab all the events off the queue. */
    while( SDL_PollEvent( &event ) ) {

        switch( event.type ) {
        case SDL_KEYDOWN:
            /* Handle key presses. */
            handle_key_down( &event.key.keysym );
            break;
        case SDL_QUIT:
            /* Handle quit requests (like Ctrl-c). */
            quit_tutorial( 0 );
            break;
		case SDL_VIDEORESIZE:
			{
				glViewport(0, 0, (GLsizei) event.resize.w, (GLsizei) event.resize.h);
				glMatrixMode(GL_PROJECTION);
				glLoadIdentity();
				float w = (float)event.resize.w;
				float h = (float)event.resize.h;
				gluPerspective( 60.0, w/h, 1.0, 1024.0 );
				glMatrixMode(GL_MODELVIEW);

				g_pTextDriver->getScreenParams(event.resize.w, event.resize.h);
			}
			break;
        }

    }

}
Пример #6
0
static void process_events( void )
{
    /* Our SDL event placeholder. */
    SDL_Event event;
	g_mouse.rel_x  = g_mouse.rel_y = 0;

    /* Grab all the events off the queue. */
    while( SDL_PollEvent( &event ) ) {

		if(UI::Update(&event))
			continue;

        switch( event.type ) {
        case SDL_KEYDOWN:
            handle_key_down( &event.key.keysym );
            break;
        case SDL_MOUSEMOTION:
            g_mouse.x = event.motion.x;
            g_mouse.y = event.motion.y;
            g_mouse.rel_x = event.motion.xrel;
            g_mouse.rel_y = event.motion.yrel;
            break;
        case SDL_MOUSEBUTTONDOWN:
            if(event.button.button == SDL_BUTTON_WHEELUP || event.button.button == SDL_BUTTON_WHEELDOWN)
                g_camera.dist += event.button.button == SDL_BUTTON_WHEELUP ? 2 : -2;
            else
                g_mouse.buttons[event.button.button-1] = true;
            break;
        case SDL_MOUSEBUTTONUP:
            if(event.button.button == SDL_BUTTON_WHEELUP || event.button.button == SDL_BUTTON_WHEELDOWN)
                g_camera.dist += event.button.button == SDL_BUTTON_WHEELUP ? 2 : -2;
            else
                g_mouse.buttons[event.button.button-1] = false;
            break;
        case SDL_QUIT:
            /* Handle quit requests (like Ctrl-c). */
            quit_tutorial( 0 );
            break;
		case SDL_VIDEORESIZE:
			{
				glViewport(0, 0, (GLsizei) event.resize.w, (GLsizei) event.resize.h);
				glMatrixMode(GL_PROJECTION);
				glLoadIdentity();
				float w = (float)event.resize.w;
				float h = (float)event.resize.h;
                g_width = event.resize.w;
                g_height = event.resize.h;
				//gluPerspective( 60.0, w/h, 1.0, 1024.0 );
                gluOrtho2D(-1.0*w/h,1.0*w/h,-1,1);
				glMatrixMode(GL_MODELVIEW);

				g_pTextDriver->getScreenParams(event.resize.w, event.resize.h);
			}
			break;
        }

    }

}
Пример #7
0
static void handle_key_down( SDL_keysym* keysym )
{
	// handle only non-unicode symbols

	if(keysym->mod & KMOD_LALT && keysym->sym == SDLK_RETURN)
	{
		//SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
		//if(g_flags&SDL_FULLSCREEN)
			g_flags ^= SDL_FULLSCREEN;
			SDL_SetVideoMode(g_width, g_height, g_bpp, g_flags);
		return;
	}

	if(!dconsole::Instance().isOpened() && SDLK_TAB == keysym->sym)
	{
		dconsole::Instance().setOpen(true);
	}
	else if(dconsole::Instance().isOpened())
	{
		bool mod_pressed = !!(keysym->mod & KMOD_LCTRL);
		if(!mod_pressed) // things like "crtl+space" should not add space!
			dconsole::Console::onKbdEvent(keysym);

		switch( keysym->sym ) {
			// execute command
			case SDLK_RETURN: dconsole::Console::onKbdExecute(); break;
				// match list
			case SDLK_UP: dconsole::Console::onKbdPrev(mod_pressed); break;
			case SDLK_DOWN: dconsole::Console::onKbdNext(mod_pressed); break;
			case SDLK_PAGEUP: dconsole::Console::onKbdPrevPage(mod_pressed); break;
			case SDLK_PAGEDOWN: dconsole::Console::onKbdNextPage(mod_pressed); break;
				// cmd line
			case SDLK_SPACE: if(mod_pressed) dconsole::Console::onKbdAutocomplete(); break;
			case SDLK_BACKSPACE: dconsole::Console::onKbdBackspace(); break;
			case SDLK_HOME: dconsole::Console::onKbdMoveHome(); break;
			case SDLK_END: dconsole::Console::onKbdMoveEnd(); break;
			case SDLK_LEFT: dconsole::Console::onKbdMoveLeft(); break;
			case SDLK_RIGHT: dconsole::Console::onKbdMoveRight(); break;
			case SDLK_DELETE: dconsole::Console::onKbdDelete(); break;
		}
	}


    switch( keysym->sym ) {
    case SDLK_ESCAPE:
        quit_tutorial( 0 );
        break;
    /*case SDLK_SPACE:
        should_rotate = !should_rotate;
        break;*/
    default:
        break;
    }

}
Пример #8
0
static void process_events( void )
{
    /* Our SDL event placeholder. */
    SDL_Event event;

    /* Grab all the events off the queue. */
    while( SDL_PollEvent( &event ) ) {

        switch( event.type ) {
        case SDL_QUIT:
            /* Handle quit requests (like Ctrl-c). */
            quit_tutorial( 0 );
            break;
        }

    }

}
Пример #9
0
Файл: eg.c Проект: darkria/aima
static void handle_key_down( SDL_keysym* keysym ) {
  /*
   * We’re only interested if ’Esc’ has
   * been presssed.
   *
   * EXERCISE:
   * Handle the arrow keys and have that change the
   * viewing position/angle.
   */
  switch( keysym->sym ) {
  case SDLK_ESCAPE:
    quit_tutorial( 0 );
    break;
  case SDLK_SPACE:
    should_rotate = !should_rotate;
    break;
  default:
    break;
  }
}
Пример #10
0
int main()
{
    /* Information about the current video settings. */
    const SDL_VideoInfo* info = NULL;
    /* Color depth in bits of our window. */
    int bpp = 0;
    /* Flags we will pass into SDL_SetVideoMode. */
    int flags = 0;

    /* First, initialize SDL's video subsystem. */
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
        /* Failed, exit. */
        fprintf( stderr, "Video initialization failed: %s\n",
             SDL_GetError( ) );
        quit_tutorial( 1 );
    }

    SDL_ShowCursor( false );

    /* Let's get some video information. */
    info = SDL_GetVideoInfo( );

    if( !info ) {
        /* This should probably never happen. */
        fprintf( stderr, "Video query failed: %s\n",
             SDL_GetError( ) );
        quit_tutorial( 1 );
    }

    /*
     * Set our width/height to 640/480 (you would
     * of course let the user decide this in a normal
     * app). We get the bpp we will request from
     * the display. On X11, VidMode can't change
     * resolution, so this is probably being overly
     * safe. Under Win32, ChangeDisplaySettings
     * can change the bpp.
     */
    width = 1200;
    height = 650;
//    width = 1280;
//    height = 768;
    bpp = info->vfmt->BitsPerPixel;

    /*
     * Now, we want to setup our requested
     * window attributes for our OpenGL window.
     * We want *at least* 5 bits of red, green
     * and blue. We also want at least a 16-bit
     * depth buffer.
     *
     * The last thing we do is request a double
     * buffered window. '1' turns on double
     * buffering, '0' turns it off.
     *
     * Note that we do not use SDL_DOUBLEBUF in
     * the flags to SDL_SetVideoMode. That does
     * not affect the GL attribute state, only
     * the standard 2D blitting setup.
     */
//    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
//    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
//    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
//    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 );

    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 6 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );

    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

    /*
     * We want to request that SDL provide us
     * with an OpenGL window, in a fullscreen
     * video mode.
     *
     * EXERCISE:
     * Make starting windowed an option, and
     * handle the resize events properly with
     * glViewport.
     */
    flags = SDL_OPENGL | SDL_FULLSCREEN;
//    flags = SDL_OPENGL;

    /*
     * Set the video mode
     */
    if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 ) {
        /* 
         * This could happen for a variety of reasons,
         * including DISPLAY not being set, the specified
         * resolution not being available, etc.
         */
        fprintf( stderr, "Video mode set failed: %s\n",
             SDL_GetError( ) );
        quit_tutorial( 1 );
    }

    /*
     * At this point, we should have a properly setup
     * double-buffered window for use with OpenGL.
     */
    setup_opengl( width, height );

    /*
     * Now we want to begin our normal app process--
     * an event loop with a lot of redrawing.
     */
    while( 1 ) {
        process_events();//Process incoming events.
        draw_screen();//Draw the screen.
    }

    /*
     * EXERCISE:
     * Record timings using SDL_GetTicks() and
     * and print out frames per second at program
     * end.
     */

    /* Never reached. */
    return 0;
}
Пример #11
0
static void process_events(void)
{
	/* Our SDL event placeholder. */
	SDL_Event event;

    /* Only poll + sleep if we are autoscrolling or doing
     * something else that is interactive */
    if (((autoscroll) && autoscroll_var) || key_button_down) {
        if (!SDL_PollEvent(&event)) {
            /* If we add a sleep, the scrolling won't be super smooth.
             * Regardless, I think we need to find something to make sure we
             * don't eat 100% cpu just checking for events.
             *
             * I found that 10ms is not a bad wait. Theoretically we want to
             * wait 1000ms / fps (usually 60) -> 16ms.
             * */
            usleep(16000);

            if (autoscroll) {
                if (key_button_down & LEAST_KEY_DOWN)
                    autoscroll_var += 1;

                if (key_button_down & LEAST_KEY_UP)
                    autoscroll_var -= 1;

                scroll -= autoscroll_var;
            } else {
                if (key_button_down & LEAST_KEY_DOWN)
                    scroll -= 5;

                if (key_button_down & LEAST_KEY_UP)
                    scroll += 5;
            }

            redraw = 1;
        }
    } else {
        SDL_WaitEvent(&event);
    }

next_event:

    switch (event.type) {
    case SDL_KEYDOWN:
        /* Handle key presses. */
        handle_key_down(&event.key.keysym);
        break;

    case SDL_KEYUP:
        handle_key_up(&event.key.keysym);
        break;

    case SDL_QUIT:
        /* Handle quit requests (like Ctrl-c). */
        quit_tutorial(0);
        break;

    case SDL_VIDEORESIZE:
        handle_resize(event.resize);
        break;

    case SDL_VIDEOEXPOSE:
        redraw = 1;
        break;

    case SDL_MOUSEBUTTONDOWN:
        handle_mouse_down(&event.button);
        break;

    case SDL_MOUSEBUTTONUP:
        handle_mouse_up(&event.button);
        break;

    case SDL_MOUSEMOTION:
        handle_mouse_motion(&event.motion);
        break;

    /* A thread completed its rendering
     *
     * The thread structure of the completed job is contained
     * within the data1 pointer of the event.
     */
    case LEAST_PAGE_COMPLETE:
        finish_page_render((struct least_thread*)event.user.data1);
        redraw = 1;
        break;

    }

    /* Clear event, just in case SDL doesn't do this (TODO) */
    memset(&event, 0, sizeof(SDL_Event));
    /* If there are more events, handle them before drawing.
     * This is required for scrolling with the mouse - without this,
     * it is pretty slow and lags. */
    if (SDL_PollEvent(&event)) {
        goto next_event;
    }
}
Пример #12
0
int setup_sdl(void)
{
	/* Information about the current video settings. */
	const SDL_VideoInfo *info = NULL;

	/* Color depth in bits of our window. */
	int bpp = 0;

	/* Flags we will pass into SDL_SetVideoMode. */
    int flags = 0;

	/* First, initialize SDL's video subsystem. */
	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		/* Failed, exit. */
		fprintf(stderr, "Video initialization failed: %s\n",
			SDL_GetError());
		quit_tutorial(1);
	}

	/* Let's get some video information. */
	info = SDL_GetVideoInfo();

	if (!info) {
		/* This should probably never happen. */
		fprintf(stderr, "Video query failed: %s\n", SDL_GetError());
		quit_tutorial(1);
	}

    /* Store current width and height, and more importantly
     * store GL backbuffer size
     */
    gl_w = w = info->current_w;
    gl_h = h = info->current_h;
    printf("W, H: (%f, %f)\n", w, h);

	bpp = info->vfmt->BitsPerPixel;

	/*
	 * Now, we want to setup our requested
	 * window attributes for our OpenGL window.
	 * We want *at least* 5 bits of red, green
	 * and blue. We also want at least a 16-bit
	 * depth buffer.
	 *
	 * The last thing we do is request a double
	 * buffered window. '1' turns on double
	 * buffering, '0' turns it off.
	 *
	 * Note that we do not use SDL_DOUBLEBUF in
	 * the flags to SDL_SetVideoMode. That does
	 * not affect the GL attribute state, only
	 * the standard 2D blitting setup.
	 */
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

	/* flags = SDL_OPENGL | SDL_FULLSCREEN; */
	flags = SDL_OPENGL | SDL_RESIZABLE | SDL_DOUBLEBUF;
	/* flags = SDL_OPENGL; */
    surface = SDL_SetVideoMode(w, h, bpp, flags);
	if (!surface) {
		/*
		 * This could happen for a variety of reasons,
		 * including DISPLAY not being set, the specified
		 * resolution not being available, etc.
		 */
		fprintf(stderr, "Video mode set failed: %s\n", SDL_GetError());
		quit_tutorial(1);
	}


    SDL_WM_SetCaption("least", "least");

	return 0;
}
Пример #13
0
static void handle_key_down(SDL_keysym * keysym)
{
    unsigned int i;

	switch (keysym->sym) {
	case SDLK_ESCAPE:
		quit_tutorial(0);
		break;

    case SDLK_DOWN:
        key_button_down |= LEAST_KEY_DOWN;
        break;

    case SDLK_UP:
        key_button_down |= LEAST_KEY_UP;
        break;

    case SDLK_PAGEDOWN:
        scroll -= imh + 20;
        redraw = 1;
        break;

    case SDLK_PAGEUP:
        scroll += imh + 20;
        redraw = 1;
        break;

    case SDLK_F5:
        printf("refresh: Killing cache\n");

        /* Kill all stored pages */
        for (i = 0; i < pagec; i++)
            if (pages[i].texture) {
                printf("refresh: Killing page %d\n", i);
                glDeleteTextures(1, &pages[i].texture);
                pages[i].texture = 0;
            } else if (pages[i].rendering) {
                printf("refresh: Removing render flag from active page %d\n",
                    i);
                pages[i].rendering = 0;
            }

        /* To prevent running renders with old settings from
         * entering the refreshed cache, mark all threads
         * as in pre-refresh state.
         */
        for (i = 0; i < (unsigned int)thread_count; i++)
            threads[i].pre_refresh = 1;
        printf("refresh: Marked %d running threads as pre-refresh renders\n",
            thread_count - idle_thread_count);

        /* Finally update the render resolution to current window size */
        printf("refresh: Changing size lock from %.2fx%.2f to %.2fx%.2f\n",
            lw, lh, w, h);

        lw = w;
        lh = h;
        break;

    case SDLK_F11:
        SDL_WM_ToggleFullScreen(surface);
        toggle_fullscreen();
        break;

    case SDLK_F12:
        if (autoscroll)
            autoscroll = 0;
        else
            autoscroll = 1;
        redraw = 1;
        break;

	default:
		break;
	}
}
Пример #14
0
int main( int argc, char* argv[] )
{
    /* Information about the current video settings. */
    const SDL_VideoInfo* info = NULL;

    g_Time = InitTime();

    /* First, initialize SDL's video subsystem. */
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
        /* Failed, exit. */
        fprintf( stderr, "Video initialization failed: %s\n",
             SDL_GetError( ) );
        quit_tutorial( 1 );
    }

    /* Let's get some video information. */
    info = SDL_GetVideoInfo( );

    if( !info ) {
        /* This should probably never happen. */
        fprintf( stderr, "Video query failed: %s\n",
             SDL_GetError( ) );
        quit_tutorial( 1 );
    }

    g_bpp = info->vfmt->BitsPerPixel;

    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
	SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

	g_flags = SDL_OPENGL|SDL_HWPALETTE|/*SDL_NOFRAME|*/SDL_HWSURFACE/*|SDL_RESIZABLE*//*| SDL_FULLSCREEN*/;

    /*
     * Set the video mode
     */
    if( SDL_SetVideoMode( g_width, g_height, g_bpp, g_flags ) == 0 ) {
        /* 
         * This could happen for a variety of reasons,
         * including DISPLAY not being set, the specified
         * resolution not being available, etc.
         */
        fprintf( stderr, "Video mode set failed: %s\n",
             SDL_GetError( ) );
        quit_tutorial( 1 );
    }

    /*
     * At this point, we should have a properly setup
     * double-buffered window for use with OpenGL.
     */
    setup_opengl( g_width, g_height );


    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        
        /* Problem: glewInit failed, something is seriously wrong. */
        fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
        quit_tutorial(1);
    }

    fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
    if (!GLEW_ARB_vertex_program || !GLEW_ARB_vertex_program)
    {
        fprintf(stderr, "No shader program support\n");
        quit_tutorial(1);
    }

    if (glewIsSupported("GL_VERSION_2_0"))
        printf("Ready for OpenGL 2.0\n");
    else {
        printf("OpenGL 2.0 not supported\n");
        quit_tutorial(1);
    }

	/*
	 * Init OpenGL text driver which will 
	 * be used by GraphicsConsole
	 */
	g_pTextDriver = new OGLTextDriver();
	if( false == g_pTextDriver->init("FixedWidth.bmp", 8, 16, 16, g_width, g_height))
		quit_tutorial( 1 );


	register_commands();
	GraphicsConsole::Instance().setTextDriver(g_pTextDriver);
	SDL_EnableKeyRepeat(500, 30);

	draw_velocity = glsl_program::makeProgram("drawVelocity", DATA_PATH"quad.vs", DATA_PATH"draw_velocity.fs");
	draw_fluids = glsl_program::makeProgram("drawFluids", DATA_PATH"quad.vs", DATA_PATH"draw_fluids.fs");
	assert(draw_velocity);

	start_time = time(0);

	g_fluids.createShaders();
	g_fluids.createBuffers();
	
	UI::Init(g_width, g_height);
	g_fluids.createUI();

	SDL_WM_SetCaption("console", 0);

    /*
     * Now we want to begin our normal app process--
     * an event loop with a lot of redrawing.
     */
    while( !g_exit ) {
        UpdateTime(&g_Time);
        /* Process incoming events. */
        process_events( );
        /* Draw the screen. */
        draw_screen( );
    }

	quit_tutorial(0);

    return 0;
}
Пример #15
0
int main( int argc, char* argv[] )
{
    /* Information about the current video settings. */
    const SDL_VideoInfo* info = NULL;


    /* First, initialize SDL's video subsystem. */
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
        /* Failed, exit. */
        fprintf( stderr, "Video initialization failed: %s\n",
             SDL_GetError( ) );
        quit_tutorial( 1 );
    }

    /* Let's get some video information. */
    info = SDL_GetVideoInfo( );

    if( !info ) {
        /* This should probably never happen. */
        fprintf( stderr, "Video query failed: %s\n",
             SDL_GetError( ) );
        quit_tutorial( 1 );
    }

    g_bpp = info->vfmt->BitsPerPixel;

    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
	SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

	g_flags = SDL_OPENGL|SDL_HWPALETTE|/*SDL_NOFRAME|*/SDL_HWSURFACE/*|SDL_RESIZABLE*//*| SDL_FULLSCREEN*/;

    /*
     * Set the video mode
     */
    if( SDL_SetVideoMode( g_width, g_height, g_bpp, g_flags ) == 0 ) {
        /* 
         * This could happen for a variety of reasons,
         * including DISPLAY not being set, the specified
         * resolution not being available, etc.
         */
        fprintf( stderr, "Video mode set failed: %s\n",
             SDL_GetError( ) );
        quit_tutorial( 1 );
    }

    /*
     * At this point, we should have a properly setup
     * double-buffered window for use with OpenGL.
     */
    setup_opengl( g_width, g_height );

	/*
	 * Init OpenGL text driver which will 
	 * be used by GraphicsConsole
	 */
	g_pTextDriver = new OGLTextDriver();
	if( false == g_pTextDriver->init("FixedWidth.bmp", 8, 16, 16, g_width, g_height))
		quit_tutorial( 1 );


	register_commands();
	GraphicsConsole::Instance().setTextDriver(g_pTextDriver);
	SDL_EnableKeyRepeat(500, 30);

	SDL_WM_SetCaption("console", 0);

    /*
     * Now we want to begin our normal app process--
     * an event loop with a lot of redrawing.
     */
    while( !g_exit ) {
        /* Process incoming events. */
        process_events( );
        /* Draw the screen. */
        draw_screen( );
    }

	quit_tutorial(0);

    /*
     * EXERCISE:
     * Record timings using SDL_GetTicks() and
     * and print out frames per second at program
     * end.
     */

    /* Never reached. */
    return 0;
}
Пример #16
0
int main( int argc, char* argv[] )
{
    OBJSC model;
    load_objsc("./Models/Dress.objsc", &model);

    /* Information about the current video settings. */
    const SDL_VideoInfo* info = NULL;
    /* Dimensions of our window. */
    int width = 0;
    int height = 0;
    /* Color depth in bits of our window. */
    int bpp = 0;
    /* Flags we will pass into SDL_SetVideoMode. */
    int flags = 0;

    /* First, initialize SDL's video subsystem. */
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
        /* Failed, exit. */
        fprintf( stderr, "Video initialization failed: %s\n",
             SDL_GetError( ) );
        quit_tutorial( 1 );
    }

    /* Let's get some video information. */
    info = SDL_GetVideoInfo( );

    if( !info ) {
        /* This should probably never happen. */
        fprintf( stderr, "Video query failed: %s\n",
             SDL_GetError( ) );
        quit_tutorial( 1 );
    }

    /*
     * Set our width/height to 640/480 (you would
     * of course let the user decide this in a normal
     * app). We get the bpp we will request from
     * the display. On X11, VidMode can't change
     * resolution, so this is probably being overly
     * safe. Under Win32, ChangeDisplaySettings
     * can change the bpp.
     */
    width = 640;
    height = 480;
    bpp = info->vfmt->BitsPerPixel;

    /*
     * Now, we want to setup our requested
     * window attributes for our OpenGL window.
     * We want *at least* 5 bits of red, green
     * and blue. We also want at least a 16-bit
     * depth buffer.
     *
     * The last thing we do is request a double
     * buffered window. '1' turns on double
     * buffering, '0' turns it off.
     *
     * Note that we do not use SDL_DOUBLEBUF in
     * the flags to SDL_SetVideoMode. That does
     * not affect the GL attribute state, only
     * the standard 2D blitting setup.
     */
    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

    /*
     * We want to request that SDL provide us
     * with an OpenGL window, in a fullscreen
     * video mode.
     *
     * EXERCISE:
     * Make starting windowed an option, and
     * handle the resize events properly with
     * glViewport.
     */
    flags = SDL_OPENGL;

    /*
     * Set the video mode
     */
    if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 ) {
        /* 
         * This could happen for a variety of reasons,
         * including DISPLAY not being set, the specified
         * resolution not being available, etc.
         */
        fprintf( stderr, "Video mode set failed: %s\n",
             SDL_GetError( ) );
        quit_tutorial( 1 );
    }

    /*
     * At this point, we should have a properly setup
     * double-buffered window for use with OpenGL.
     */
    setup_opengl( width, height );



    /*
     * Now we want to begin our normal app process--
     * an event loop with a lot of redrawing.
     */
    while( 1 ) {
        /* Process incoming events. */
        process_events( );
        /* Draw the screen. */
        draw_screen( &model );

        SDL_PumpEvents();
        uint8_t * keys = SDL_GetKeyState(NULL);

        game_time += 0.4;
        float friction = (speed < 0.4) ? (speed < 0.2) ? 0.5 : 0.85 : 0.98;
        speed *= friction;

        if(keys[SDLK_w]){
            speed += 0.13*(1-speed) + 0.1*speed;
        }
        if(keys[SDLK_a]){
            angle += .1;
            speed += 0.13*(1-speed) - 0.1*speed;
        }
        if(keys[SDLK_d]){
            angle -= .1;
            speed += 0.13*(1-speed) - 0.1*speed;
        }
        if(speed > 1) speed = 1;
    }

    /*
     * EXERCISE:
     * Record timings using SDL_GetTicks() and
     * and print out frames per second at program
     * end.
     */

    /* Never reached. */
    return 0;
}