示例#1
0
void Menu::Draw(bool nextImg, SDL_Surface *screen)
{
	//Move the objects before drawing.
  for (unsigned int i = PhysicObjects.size(); i; i-- )
  {
      PhysicObject* tempObj = PhysicObjects.front();
      PhysicObjects.pop_front();
      tempObj->Move(PhysicObjects);
      PhysicObjects.push_back(tempObj);
  }
	
	//Draw the background.
	SDL_Rect *pos = new SDL_Rect;
	pos->x = 0;
	pos->y = 0;

	SDL_BlitSurface( background, NULL, screen, pos);
	

	//Draw the enities.
  for (unsigned int i = Entities.size(); i; i-- )
  {
    Entity* tempEnt = Entities.front();
    Entities.pop_front();
		
		tempEnt->Draw(nextImg, screen); 
		if (tempEnt->getVisible())
			tempEnt->DrawText(screen);
		
    Entities.push_back(tempEnt);
  }
}
示例#2
0
int main(int argc, char *argv[]) {
    Matrix projectionMatrix;
    Matrix modelMatrix;
    Matrix viewMatrix;
    
    init();
    ShaderProgram program(RESOURCE_FOLDER "vertex_textured.glsl", RESOURCE_FOLDER "fragment_textured.glsl");
    glUseProgram(program.programID);

    float lastFrameTicks = 0.0f;

//    float vertices[] = {-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5};
//    float texCoords[] = {0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0};
//    float vertices[] = {-0.09, -0.09, 0.09, -0.09, 0.09, 0.09, -0.09, -0.09, 0.09, 0.09, -0.09, 0.09};
    Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 4096 );
    SDL_Event event;
    bool done = false;
    
/*    Sprites courtesy of http://gooperblooper22.deviantart.com/art/Space-Invaders-Sprite-Sheet-135338373  */
    GLuint spriteSheetTexture = LoadTexture("space_invaders_sprite_sheet_by_gooperblooper22.png");
    GLuint white = LoadTexture("white.png");
    GLuint textTexture = LoadTexture("font1.png");
    Mix_Music *bgm = Mix_LoadMUS("bgm.mp3");
    Mix_Chunk *missile = Mix_LoadWAV("157439__nengisuls__misslie-1.wav");
    bool pressed = false;
    int score = 0;
    int currentIndex = 0;
    int state;
    const int numFrames = 2;
    float animation[] = {(74.0f/617.0f),(107.0f/617.0f)};
    float animationElapsed = 0.0f;
    float framesPerSecond = 0.0004f;
    std::vector<SheetSprite> aliens;
    
    while (!done) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
                done = true;
            }
        }
        program.setProjectionMatrix(projectionMatrix);
        program.setModelMatrix(modelMatrix);
        program.setViewMatrix(viewMatrix);
        
        float ticks = (float)SDL_GetTicks()/1000;
        float elapsed = ticks - lastFrameTicks;
        lastFrameTicks = ticks;
        
        animationElapsed += 1/elapsed;
        if(animationElapsed > 1.0/framesPerSecond) {
            currentIndex++;
            animationElapsed = 0.0;
            if(currentIndex > numFrames-1) {
                currentIndex = 0;
            }
        }
        
        glClear(GL_COLOR_BUFFER_BIT);
        
        score = ticks;
        SheetSprite menu(spriteSheetTexture, 160.0f/617.0f, 1.0f/2035.0f, 250.0f/617.0f, 171.0f/2035.0f, 0.5);
        Entity text;
        Player cannon(spriteSheetTexture, 277.0f/617.0f, 227.0f/2035.0f, 26.0f/617.0f, 17.0f/2035.0f, 0.05);
        SheetSprite alien(spriteSheetTexture, animation[currentIndex], (225.0f/2035.0f), 22.0f/617.0f, 16.0f/2035.0f, 0.05);
        
        const Uint8 *keys = SDL_GetKeyboardState(NULL);
        
        switch (state) {
            case STATE_MAIN_MENU:
                menu.update(lastFrameTicks, elapsed, projectionMatrix, viewMatrix, program, true);
                menu.draw(program);
                if (!Mix_PlayingMusic()) {
                    Mix_PlayMusic(bgm, -1);
                }
                if (keys[SDL_SCANCODE_RETURN]) {
                    state++;
                }
                break;
                
            case STATE_GAME_LEVEL:
                text.DrawText(program, textTexture, "Score:" + std::to_string(score), 0.075, 0, -0.23, 0.9);
                cannon.update(lastFrameTicks, elapsed, projectionMatrix, viewMatrix, program, white, alien);
                cannon.draw(program);
                alien.update(lastFrameTicks, elapsed, projectionMatrix, viewMatrix, program, false);
                alien.draw(program);
                if (keys[SDL_SCANCODE_SPACE]) {
                    Mix_PlayChannel( -1, missile, 0);
                    Mix_VolumeChunk(missile, 15);
                }
                if (keys[SDL_SCANCODE_ESCAPE] && (state == 1)) {
                    pressed = true;
                    state++;
                }
                
                break;
            case STATE_PAUSE:
                if (Mix_PlayingMusic()) {
                    Mix_PausedMusic();
                }
                text.DrawText(program, textTexture, "Paused", 0.1, 0, -0.23, 0);
                if (keys[SDL_SCANCODE_ESCAPE] && pressed) {
                    pressed = false;
                    state--;
                }
                break;
                
        }
        SDL_GL_SwapWindow(displayWindow);
    }
    Mix_FreeMusic(bgm);
    Mix_FreeChunk(missile);
    Mix_Quit();
    cleanup();
    return 0;
}