Ejemplo n.º 1
0
int main()
{
	const int FRAMES_PER_SECOND = 25;
    const int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;
	const int MAX_FRAMESKIP = 10;

    unsigned long next_game_tick = GetTickCount();
	int loops = 0;

	Tetris game;
	printf("Game initializing\n");
	game.init();
	
	printf("Game initialized\n");
	View view;
	view.init(game);

    //int sleep_time = 0;
	int quit = 0;

    bool game_is_running = true;
    while( game_is_running ) 
	{
		while( GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) 
		{
			quit = view.getInput();
			next_game_tick += SKIP_TICKS;
			loops++;
		}
		if(quit == 1)
		{
			game.pause();
			quit = 0;
		}
		else if(quit == 2)
		{
			game_is_running = false;
			quit = 0;
		}
		game.update();
        view.update();
    }
	return 0;
}
Ejemplo n.º 2
0
void Game::start(const int TICKS_PER_SECOND, const int MAX_FRAMESKIP) {

    bool quit = false;
    const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
    Uint32 next_tick = SDL_GetTicks();

	Uint32 fps_ticks = SDL_GetTicks();
	Uint32 fps = 0;
	SDL_Rect fpsRenderRect = { 10, 10, 50, 30 };
    SDL_Color fpsColor = {255, 255, 255, 255};

    int loops;
    float interpolation;
	Tetris tetris;
	tetris.init();
    SDL_Event e;
    
    GLuint MatrixID = glGetUniformLocation(_programID, "mvp");
    
    bool moveUp = false;
    bool moveDown = false;
    bool moveLeft = false;
    bool moveRight = false;
    float testValY = 0.0f;
    float testValX = 0.0f;
    bool projectionMod = false;
    float angle = 0.0f;
    float prevAngle = angle;
    
    glm::mat4 Projection = glm::perspective(glm::radians(90.0f), _screen.x / _screen.y, 0.1f, 100.0f);
    
    glm::mat4 View = glm::lookAt(
                                 glm::vec3(0, 0, 1),
                                 glm::vec3(0, 0, 0),
                                 glm::vec3(0, 1, 0)
                                 );
    
    glm::mat4 Translate = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f));
    
    glm::mat4 Scale = glm::scale(glm::mat4(1.0f), glm::vec3(1.0));

    glm::mat4 Rotate = glm::rotate(glm::mat4(1.0f), 0.0f, glm::vec3(0.0f));
    
    glm::mat4 Model = glm::mat4(1.0f);
    
    glm::mat4 MVP = Projection * View * Model;

    while(!quit) {
        loops = 0;
        while (SDL_GetTicks() > next_tick && loops < MAX_FRAMESKIP) {

			if (SDL_GetTicks() > fps_ticks) {
				float fps_delta = (SDL_GetTicks() - fps_ticks) / 1000.0f;
				fps = Uint32(1.0 / fps_delta);
			}

            while(SDL_PollEvent(&e)) {
                if (e.type == SDL_QUIT) {
                    quit = true;
                }
                if (e.type == SDL_KEYDOWN) {
                    switch( e.key.keysym.sym ) {
                        case SDLK_UP:
                            moveUp = true;
                            break;
                        case SDLK_DOWN:
                            moveDown = true;
                            break;
                        case SDLK_LEFT:
                            moveLeft = true;
                            break;
                        case SDLK_RIGHT:
                            moveRight = true;
                            break;
                        case SDLK_RETURN:
                            projectionMod = !projectionMod;
                        default:
                            break;
                    }
                }
                if (e.type == SDL_KEYUP) {
                    switch( e.key.keysym.sym ) {
                        case SDLK_UP:
                            moveUp = false;
                            break;
                        case SDLK_DOWN:
                            moveDown = false;
                            break;
                        case SDLK_LEFT:
                            moveLeft = false;
                            break;
                        case SDLK_RIGHT:
                            moveRight = false;
                            break;
                        default:
                            break;
                    }
                }
				tetris.handleInput(e);
            }
            
            tetris.update();
            
            if (moveRight) {
                testValX += 0.1f;
            }
            if (moveLeft) {
                testValX -= 0.1f;
            }
            if (moveUp) {
                testValY += 0.1f;
            }
            if (moveDown) {
                testValY -= 0.1f;
            }
            
            prevAngle = angle;
            angle += 0.1f;
            if (angle > 360.f) {
                angle = 0.f;
            }
            
            next_tick += SKIP_TICKS;
            loops++;
        }
        
        interpolation = float(SDL_GetTicks() + SKIP_TICKS - next_tick) / float(SKIP_TICKS);
        
        Projection = glm::perspective(glm::radians(45.0f), _screen.x / _screen.y, 0.1f, 100.0f);
        
        if (projectionMod) {
            Projection = glm::ortho(-10.0f * (_screen.x / _screen.y), 10.0f * (_screen.x / _screen.y), -10.0f, 10.0f, 0.0f, 100.0f);
        }
        
        View = glm::lookAt(
                           glm::vec3(0, 0, 4),
                           glm::vec3(0, 0, 0),
                           glm::vec3(0, 1, 0)
                           );
        
        Translate = glm::translate(glm::mat4(1.0f), glm::vec3(testValX, testValY, 0.0f));
        
        Scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.5));
        
        Rotate = glm::rotate(glm::mat4(1.0f), glm::mix(prevAngle, angle, interpolation), glm::vec3(1.0f, 1.0f, 0.0f));
        
        Model = Translate * Rotate * Scale;
        
        MVP = Projection * View * Model;

		//Clear the screen
        glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        glUseProgram(_programID);
        
        glUniformMatrix4fv(MatrixID, 1, GL_FALSE, glm::value_ptr(MVP));
        
        glBindBuffer(GL_ARRAY_BUFFER, _VBO);
        glEnableVertexAttribArray(_vertexPos2DLocation);
        glVertexAttribPointer(_vertexPos2DLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
        
        glBindBuffer(GL_ARRAY_BUFFER, _cVBO);
        glEnableVertexAttribArray(_vertexColor);
        glVertexAttribPointer(_vertexColor, 3, GL_FLOAT, GL_FALSE, 0, 0);
        
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _IBO);
        glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, NULL);
        
        glDisableVertexAttribArray(_vertexPos2DLocation);
        glDisableVertexAttribArray(_vertexColor);
        
        glUseProgram(NULL);

        
		tetris.render(interpolation);

		//Render FPS

		fps_ticks = SDL_GetTicks();

        SDL_GL_SwapWindow(_window);
    }

}